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.
12 #include <linux/config.h>
13 #include <linux/module.h>
14 #include <linux/kernel.h>
16 #include <linux/string.h>
17 #include <linux/stat.h>
18 #include <linux/errno.h>
19 #include <linux/locks.h>
20 #include <linux/unistd.h>
22 #include <asm/system.h>
23 #include <asm/uaccess.h>
26 #include <linux/stat.h>
27 #include <asm/uaccess.h>
28 #include <linux/vmalloc.h>
29 #include <asm/segment.h>
31 #include <linux/obd_support.h>
32 #include <linux/obd_class.h>
33 #include <linux/obdfs.h>
35 struct list_head obdfs_super_list;
36 struct super_operations obdfs_super_operations;
37 long obdfs_cache_count = 0;
40 static char *obdfs_read_opt(const char *opt, char *data)
45 CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
46 if ( strncmp(opt, data, strlen(opt)) )
49 if ( (value = strchr(data, '=')) == NULL )
53 OBD_ALLOC(retval, char *, strlen(value) + 1);
55 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
59 memcpy(retval, value, strlen(value)+1);
60 CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
64 static void obdfs_options(char *options, char **dev, char **vers)
71 for (this_char = strtok (options, ",");
73 this_char = strtok (NULL, ",")) {
74 CDEBUG(D_INFO, "this_char %s\n", this_char);
75 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
76 (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
82 static int obdfs_getdev(char *devpath, int *dev)
84 struct dentry *dentry;
87 dentry = lookup_dentry(devpath, NULL, 0);
89 return PTR_ERR(dentry);
91 if (!S_ISCHR(dentry->d_inode->i_mode))
94 devno = dentry->d_inode->i_rdev;
95 if ( MAJOR(devno) != OBD_PSDEV_MAJOR )
98 if ( MINOR(devno) >= MAX_OBD_DEVICES )
106 static struct super_block * obdfs_read_super(struct super_block *sb,
107 void *data, int silent)
109 struct inode *root = 0;
110 struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
111 struct obd_device *obddev;
113 char *version = NULL;
116 unsigned long blocksize;
117 unsigned long blocksize_bits;
118 unsigned long root_ino;
125 memset(sbi, 0, sizeof(*sbi));
127 obdfs_options(data, &device, &version);
129 printk(__FUNCTION__ ": no device\n");
134 if ( (err = obdfs_getdev(device, &devno)) ) {
135 printk("Cannot get devno of %s, error %d\n", device, err);
140 if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
141 printk(__FUNCTION__ ": wrong major number %d!\n", MAJOR(devno));
146 if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
147 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
148 device, MINOR(devno));
153 obddev = &obd_dev[MINOR(devno)];
155 if ( ! (obddev->obd_flags & OBD_ATTACHED) ||
156 ! (obddev->obd_flags & OBD_SET_UP) ){
157 printk("device %s not attached or not set up (%d)\n",
158 device, MINOR(devno));
163 sbi->osi_obd = obddev;
164 sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
166 sbi->osi_conn.oc_dev = obddev;
167 err = sbi->osi_ops->o_connect(&sbi->osi_conn);
169 printk("OBDFS: cannot connect to %s\n", device);
174 /* list of dirty inodes, and a mutex to hold while modifying it */
175 INIT_LIST_HEAD(&sbi->osi_inodes);
176 sema_init(&sbi->osi_list_mutex, 1);
180 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
181 "blocksize", &scratch,
184 printk("getinfo call to drive failed (blocksize)\n");
189 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
190 "blocksize_bits", &scratch,
191 (void *)&blocksize_bits);
193 printk("getinfo call to drive failed (blocksize_bits)\n");
198 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"),
199 "root_ino", &scratch, (void *)&root_ino);
201 printk("getinfo call to drive failed (root_ino)\n");
208 sb->s_blocksize = blocksize;
209 sb->s_blocksize_bits = (unsigned char)blocksize_bits;
210 sb->s_magic = OBDFS_SUPER_MAGIC;
211 sb->s_op = &obdfs_super_operations;
213 /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
215 /* make root inode */
216 root = iget(sb, root_ino);
217 if (!root || is_bad_inode(root)) {
218 printk("OBDFS: bad iget for root\n");
226 CDEBUG(D_INFO, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
227 "minor: %d, blocksize: %ld, blocksize bits %ld\n",
228 sb->s_dev, root->i_ino, device, MINOR(devno),
229 blocksize, blocksize_bits);
230 sb->s_root = d_alloc_root(root);
231 list_add(&sbi->osi_list, &obdfs_super_list);
233 OBD_FREE(device, strlen(device) + 1);
235 OBD_FREE(version, strlen(version) + 1);
242 OBD_FREE(device, strlen(device) + 1);
244 OBD_FREE(version, strlen(version) + 1);
246 sbi->osi_super = NULL;
253 } /* obdfs_read_super */
256 static void obdfs_put_super(struct super_block *sb)
258 struct obdfs_sb_info *sbi;
263 sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
264 obdfs_flush_reqs(&sbi->osi_inodes, ~0UL);
266 OPS(sb,disconnect)(ID(sb));
267 list_del(&sbi->osi_list);
268 memset(sbi, 0, sizeof(*sbi));
270 printk(KERN_INFO "OBDFS: Bye bye.\n");
274 } /* obdfs_put_super */
277 /* all filling in of inodes postponed until lookup */
278 static void obdfs_read_inode(struct inode *inode)
283 oa = obdo_fromid(IID(inode), inode->i_ino,
284 OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
286 printk(__FUNCTION__ ": obdo_fromid failed\n");
288 return /* PTR_ERR(oa) */;
292 obdfs_to_inode(inode, oa);
293 INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
294 INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
297 /* OIDEBUG(inode); */
299 if (S_ISREG(inode->i_mode)) {
300 inode->i_op = &obdfs_file_inode_operations;
302 } else if (S_ISDIR(inode->i_mode)) {
303 inode->i_op = &obdfs_dir_inode_operations;
305 } else if (S_ISLNK(inode->i_mode)) {
306 inode->i_op = &obdfs_symlink_inode_operations;
309 init_special_inode(inode, inode->i_mode,
310 /* XXX need to fill in the ext2 side */
311 ((long *)obdfs_i2info(inode)->oi_inline)[0]);
315 } /* obdfs_read_inode */
317 static void obdfs_write_inode(struct inode *inode)
325 printk(__FUNCTION__ ": obdo_alloc failed\n");
329 oa->o_valid = OBD_MD_FLNOTOBD;
330 obdfs_from_inode(oa, inode);
331 err = IOPS(inode, setattr)(IID(inode), oa);
334 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
337 /* Copy back attributes from oa, as there may have been
338 * changes at the target (e.g. obdo becomes a redirector
339 * in the snapshot layer).
341 obdfs_to_inode(inode, oa);
346 } /* obdfs_write_inode */
349 /* Dequeue cached pages for a dying inode without writing them to disk.
351 * This routine is called from iput() (for each unlink on the inode).
352 * We can't put this code into delete_inode() since that is called only
353 * when i_count == 0, and we need to keep a reference on the inode while
354 * it is in the page cache, which means i_count > 0. Catch 22.
356 static void obdfs_put_inode(struct inode *inode)
358 struct list_head *tmp;
361 if (inode->i_nlink) {
366 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
367 tmp = obdfs_islist(inode);
368 if ( list_empty(tmp) ) {
369 CDEBUG(D_INFO, "no dirty pages for inode %ld\n", inode->i_ino);
370 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
375 /* take it out of the super list */
377 INIT_LIST_HEAD(obdfs_islist(inode));
379 tmp = obdfs_iplist(inode);
380 while ( (tmp = tmp->prev) != obdfs_iplist(inode) ) {
381 struct obdfs_pgrq *req;
384 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
386 /* take it out of the list and free */
388 /* now put the page away */
392 /* decrement inode reference for page cache */
396 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
397 } /* obdfs_put_inode */
400 static void obdfs_delete_inode(struct inode *inode)
408 printk(__FUNCTION__ ": obdo_alloc failed\n");
411 oa->o_valid = OBD_MD_FLNOTOBD;
412 obdfs_from_inode(oa, inode);
414 err = IOPS(inode, destroy)(IID(inode), oa);
418 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
424 } /* obdfs_delete_inode */
427 static int obdfs_notify_change(struct dentry *de, struct iattr *attr)
429 struct inode *inode = de->d_inode;
436 printk(__FUNCTION__ ": obdo_alloc failed\n");
440 oa->o_id = inode->i_ino;
441 obdo_from_iattr(oa, attr);
442 err = IOPS(inode, setattr)(IID(inode), oa);
445 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
448 /* Copy back attributes from oa, as there may have been
449 * changes at the target (e.g. obdo becomes a redirector
450 * in the snapshot layer).
452 obdfs_to_inode(inode, oa);
458 } /* obdfs_notify_change */
461 static int obdfs_statfs(struct super_block *sb, struct statfs *buf,
469 err = OPS(sb,statfs)(ID(sb), &tmp);
471 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
474 copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
481 /* exported operations */
482 struct super_operations obdfs_super_operations =
484 obdfs_read_inode, /* read_inode */
485 obdfs_write_inode, /* write_inode */
486 obdfs_put_inode, /* put_inode */
487 obdfs_delete_inode, /* delete_inode */
488 obdfs_notify_change, /* notify_change */
489 obdfs_put_super, /* put_super */
490 NULL, /* write_super */
491 obdfs_statfs, /* statfs */
492 NULL /* remount_fs */
495 struct file_system_type obdfs_fs_type = {
496 "obdfs", 0, obdfs_read_super, NULL
503 printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
507 INIT_LIST_HEAD(&obdfs_super_list);
508 err = obdfs_init_pgrqcache();
513 return register_filesystem(&obdfs_fs_type);
518 int init_module(void)
523 void cleanup_module(void)
527 obdfs_flushd_cleanup();
528 obdfs_sysctl_clean();
529 obdfs_cleanup_pgrqcache();
530 unregister_filesystem(&obdfs_fs_type);
531 CDEBUG(D_MALLOC, "OBDFS mem used %ld, inodes %d, pages %d\n",
532 obd_memory, obd_inodes, obd_pages);