2 * OBDFS Super operations
4 * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
5 * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
6 * Copryright (C) 1999 Seagate Technology Inc.
7 * Copryright (C) 2001 Mountain View Data, Inc.
13 #include <linux/config.h>
14 #include <linux/module.h>
15 #include <linux/kernel.h>
17 #include <linux/string.h>
18 #include <linux/stat.h>
19 #include <linux/errno.h>
20 #include <linux/locks.h>
21 #include <linux/unistd.h>
23 #include <asm/system.h>
24 #include <asm/uaccess.h>
27 #include <linux/stat.h>
28 #include <asm/uaccess.h>
29 #include <linux/vmalloc.h>
30 #include <asm/segment.h>
32 #include <linux/obd_support.h>
33 #include <linux/obd_class.h>
34 #include <linux/obdfs.h>
36 struct list_head obdfs_super_list;
37 struct address_space_operations obdfs_aops;
38 struct super_operations obdfs_super_operations;
39 long obdfs_cache_count = 0;
40 long obdfs_mutex_start = 0;
43 static char *obdfs_read_opt(const char *opt, char *data)
48 CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
49 if ( strncmp(opt, data, strlen(opt)) )
52 if ( (value = strchr(data, '=')) == NULL )
56 OBD_ALLOC(retval, char *, strlen(value) + 1);
58 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
62 memcpy(retval, value, strlen(value)+1);
63 CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
67 static void obdfs_options(char *options, char **dev, char **vers)
74 for (this_char = strtok (options, ",");
76 this_char = strtok (NULL, ",")) {
77 CDEBUG(D_INFO, "this_char %s\n", this_char);
78 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
79 (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
85 static int obdfs_getdev(char *devpath, int *dev)
87 struct dentry *dentry;
93 if (path_init(devpath, LOOKUP_POSITIVE, &nd))
94 error = path_walk(devpath, &nd);
99 if (!S_ISCHR(dentry->d_inode->i_mode))
102 devno = dentry->d_inode->i_rdev;
103 if ( MAJOR(devno) != OBD_PSDEV_MAJOR )
106 if ( MINOR(devno) >= MAX_OBD_DEVICES )
114 static struct super_block * obdfs_read_super(struct super_block *sb,
115 void *data, int silent)
117 struct inode *root = 0;
118 struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
119 struct obd_device *obddev;
121 char *version = NULL;
124 unsigned long blocksize;
125 unsigned long blocksize_bits;
126 unsigned long root_ino;
133 memset(sbi, 0, sizeof(*sbi));
135 CDEBUG(D_INFO, "\n");
136 obdfs_options(data, &device, &version);
138 printk(__FUNCTION__ ": no device\n");
143 CDEBUG(D_INFO, "\n");
144 if ( (err = obdfs_getdev(device, &devno)) ) {
145 printk("Cannot get devno of %s, error %d\n", device, err);
150 CDEBUG(D_INFO, "\n");
151 if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
152 printk(__FUNCTION__ ": wrong major number %d!\n", MAJOR(devno));
157 CDEBUG(D_INFO, "\n");
158 if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
159 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
160 device, MINOR(devno));
165 CDEBUG(D_INFO, "\n");
166 obddev = &obd_dev[MINOR(devno)];
168 CDEBUG(D_INFO, "\n");
169 if ( ! (obddev->obd_flags & OBD_ATTACHED) ||
170 ! (obddev->obd_flags & OBD_SET_UP) ){
171 printk("device %s not attached or not set up (%d)\n",
172 device, MINOR(devno));
177 CDEBUG(D_INFO, "\n");
178 sbi->osi_obd = obddev;
179 sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
181 sbi->osi_conn.oc_dev = obddev;
182 err = sbi->osi_ops->o_connect(&sbi->osi_conn);
184 printk("OBDFS: cannot connect to %s\n", device);
189 CDEBUG(D_INFO, "\n");
190 /* list of dirty inodes, and a mutex to hold while modifying it */
191 INIT_LIST_HEAD(&sbi->osi_inodes);
192 sema_init(&sbi->osi_list_mutex, 1);
194 CDEBUG(D_INFO, "\n");
197 CDEBUG(D_INFO, "\n");
198 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
199 "blocksize", &scratch,
202 printk("getinfo call to drive failed (blocksize)\n");
207 CDEBUG(D_INFO, "\n");
208 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
209 "blocksize_bits", &scratch,
210 (void *)&blocksize_bits);
212 printk("getinfo call to drive failed (blocksize_bits)\n");
217 CDEBUG(D_INFO, "\n");
218 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"),
219 "root_ino", &scratch, (void *)&root_ino);
221 printk("getinfo call to drive failed (root_ino)\n");
226 CDEBUG(D_INFO, "\n");
227 sb->s_blocksize = blocksize;
228 sb->s_blocksize_bits = (unsigned char)blocksize_bits;
229 sb->s_magic = OBDFS_SUPER_MAGIC;
230 sb->s_op = &obdfs_super_operations;
232 /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
234 /* make root inode */
235 CDEBUG(D_INFO, "\n");
236 root = iget(sb, root_ino);
237 if (!root || is_bad_inode(root)) {
238 printk("OBDFS: bad iget for root\n");
245 CDEBUG(D_INFO, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
246 "minor: %d, blocksize: %ld, blocksize bits %ld\n",
247 sb->s_dev, root->i_ino, device, MINOR(devno),
248 blocksize, blocksize_bits);
249 sb->s_root = d_alloc_root(root);
250 list_add(&sbi->osi_list, &obdfs_super_list);
251 OBD_FREE(device, strlen(device) + 1);
253 OBD_FREE(version, strlen(version) + 1);
260 OBD_FREE(device, strlen(device) + 1);
262 OBD_FREE(version, strlen(version) + 1);
264 sbi->osi_super = NULL;
271 } /* obdfs_read_super */
274 static void obdfs_put_super(struct super_block *sb)
276 struct obdfs_sb_info *sbi;
281 sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
282 obdfs_flush_reqs(&sbi->osi_inodes, ~0UL);
284 OPS(sb,disconnect)(ID(sb));
285 list_del(&sbi->osi_list);
286 memset(sbi, 0, sizeof(*sbi));
288 printk(KERN_INFO "OBDFS: Bye bye.\n");
292 } /* obdfs_put_super */
295 /* all filling in of inodes postponed until lookup */
296 static void obdfs_read_inode(struct inode *inode)
301 oa = obdo_fromid(IID(inode), inode->i_ino,
302 OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
304 printk(__FUNCTION__ ": obdo_fromid failed\n");
306 return /* PTR_ERR(oa) */;
310 obdfs_to_inode(inode, oa);
311 INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
312 INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
315 /* OIDEBUG(inode); */
317 if (S_ISREG(inode->i_mode)) {
318 inode->i_op = &obdfs_file_inode_operations;
319 inode->i_fop = &obdfs_file_operations;
320 inode->i_mapping->a_ops = &obdfs_aops;
322 } else if (S_ISDIR(inode->i_mode)) {
323 inode->i_op = &obdfs_dir_inode_operations;
324 inode->i_fop = &obdfs_dir_operations;
326 } else if (S_ISLNK(inode->i_mode)) {
327 if (inode->i_blocks) {
328 inode->i_op = &obdfs_symlink_inode_operations;
329 inode->i_mapping->a_ops = &obdfs_aops;
331 inode->i_op = &obdfs_fast_symlink_inode_operations;
335 init_special_inode(inode, inode->i_mode,
336 ((int *)obdfs_i2info(inode)->oi_inline)[0]);
340 } /* obdfs_read_inode */
342 static void obdfs_write_inode(struct inode *inode, int wait)
348 if (IOPS(inode, setattr) == NULL) {
349 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
355 printk(__FUNCTION__ ": obdo_alloc failed\n");
360 oa->o_valid = OBD_MD_FLNOTOBD;
361 obdfs_from_inode(oa, inode);
362 err = IOPS(inode, setattr)(IID(inode), oa);
365 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
369 } /* obdfs_write_inode */
372 /* This routine is called from iput() (for each unlink on the inode).
373 * We can't put this call into delete_inode() since that is called only
374 * when i_count == 0, and we need to keep a reference on the inode while
375 * it is in the page cache, which means i_count > 0. Catch 22.
377 static void obdfs_put_inode(struct inode *inode)
380 if (inode->i_nlink) {
385 obdfs_dequeue_pages(inode);
387 } /* obdfs_put_inode */
390 static void obdfs_delete_inode(struct inode *inode)
396 if (IOPS(inode, destroy) == NULL) {
397 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
404 printk(__FUNCTION__ ": obdo_alloc failed\n");
408 oa->o_valid = OBD_MD_FLNOTOBD;
409 obdfs_from_inode(oa, inode);
412 err = IOPS(inode, destroy)(IID(inode), oa);
416 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
422 } /* obdfs_delete_inode */
425 int obdfs_notify_change(struct dentry *de, struct iattr *attr)
427 struct inode *inode = de->d_inode;
432 if (IOPS(inode, setattr) == NULL) {
433 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
439 printk(__FUNCTION__ ": obdo_alloc failed\n");
443 oa->o_id = inode->i_ino;
444 obdo_from_iattr(oa, attr);
445 err = IOPS(inode, setattr)(IID(inode), oa);
448 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
453 } /* obdfs_notify_change */
456 static int obdfs_statfs(struct super_block *sb, struct statfs *buf)
459 int bufsize = sizeof(*buf);
464 err = OPS(sb,statfs)(ID(sb), &tmp);
466 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
469 copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
476 /* exported operations */
477 struct super_operations obdfs_super_operations =
479 read_inode: obdfs_read_inode,
480 write_inode: obdfs_write_inode,
481 put_inode: obdfs_put_inode,
482 delete_inode: obdfs_delete_inode,
483 put_super: obdfs_put_super,
487 struct file_system_type obdfs_fs_type = {
488 "obdfs", 0, obdfs_read_super, NULL
495 printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
499 INIT_LIST_HEAD(&obdfs_super_list);
500 err = obdfs_init_pgrqcache();
505 return register_filesystem(&obdfs_fs_type);
508 struct address_space_operations obdfs_aops = {
509 readpage: obdfs_readpage,
510 writepage: obdfs_writepage,
511 sync_page: block_sync_page,
513 commit_write: generic_commit_write,
519 int init_module(void)
524 void cleanup_module(void)
528 obdfs_flushd_cleanup();
529 obdfs_sysctl_clean();
530 obdfs_cleanup_pgrqcache();
531 unregister_filesystem(&obdfs_fs_type);
532 CDEBUG(D_MALLOC, "OBDFS mem used %ld\n", obd_memory);