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;
38 static char *obdfs_read_opt(const char *opt, char *data)
43 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
44 if ( strncmp(opt, data, strlen(opt)) )
47 if ( (value = strchr(data, '=')) == NULL )
51 OBD_ALLOC(retval, char *, strlen(value) + 1);
53 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
57 memcpy(retval, value, strlen(value)+1);
58 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
62 static void obdfs_options(char *options, char **dev, char **vers)
69 for (this_char = strtok (options, ",");
71 this_char = strtok (NULL, ",")) {
72 CDEBUG(D_SUPER, "this_char %s\n", this_char);
73 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
74 (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
80 static int obdfs_getdev(char *devpath, int *dev)
82 struct dentry *dentry;
85 dentry = lookup_dentry(devpath, NULL, 0);
87 return PTR_ERR(dentry);
89 if (!S_ISCHR(dentry->d_inode->i_mode))
92 devno = dentry->d_inode->i_rdev;
93 if ( MAJOR(devno) != OBD_PSDEV_MAJOR )
96 if ( MINOR(devno) >= MAX_OBD_DEVICES )
104 static struct super_block * obdfs_read_super(struct super_block *sb,
105 void *data, int silent)
107 struct inode *root = 0;
108 struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
109 struct obd_device *obddev;
111 char *version = NULL;
114 unsigned long blocksize;
115 unsigned long blocksize_bits;
116 unsigned long root_ino;
123 memset(sbi, 0, sizeof(*sbi));
125 obdfs_options(data, &device, &version);
127 printk(__FUNCTION__ ": no device\n");
133 if ( (err = obdfs_getdev(device, &devno)) ) {
134 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));
147 if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
148 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
149 device, MINOR(devno));
155 obddev = &obd_dev[MINOR(devno)];
157 if ( ! (obddev->obd_flags & OBD_ATTACHED) ||
158 ! (obddev->obd_flags & OBD_SET_UP) ){
159 printk("device %s not attached or not set up (%d)\n",
160 device, MINOR(devno));
166 sbi->osi_obd = obddev;
167 sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
169 sbi->osi_conn.oc_dev = obddev;
170 err = sbi->osi_ops->o_connect(&sbi->osi_conn);
172 printk("OBDFS: cannot connect to %s\n", device);
176 /* list of dirty inodes, and a mutex to hold while modifying it */
177 INIT_LIST_HEAD(&sbi->osi_inodes);
178 sema_init(&sbi->osi_list_mutex, 1);
182 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
183 "blocksize", &scratch,
186 printk("getinfo call to drive failed (blocksize)\n");
190 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
191 "blocksize_bits", &scratch,
192 (void *)&blocksize_bits);
194 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");
207 sb->s_blocksize = blocksize;
208 sb->s_blocksize_bits = (unsigned char)blocksize_bits;
209 sb->s_magic = OBDFS_SUPER_MAGIC;
210 sb->s_op = &obdfs_super_operations;
212 /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
214 /* make root inode */
215 root = iget(sb, root_ino);
216 if (!root || is_bad_inode(root)) {
217 printk("OBDFS: bad iget for root\n");
224 CDEBUG(D_SUPER, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
225 "minor: %d, blocksize: %ld, blocksize bits %ld\n",
226 sb->s_dev, root->i_ino, device, MINOR(devno),
227 blocksize, blocksize_bits);
228 sb->s_root = d_alloc_root(root);
229 list_add(&sbi->osi_list, &obdfs_super_list);
238 sbi->osi_super = NULL;
245 } /* obdfs_read_super */
248 static void obdfs_put_super(struct super_block *sb)
250 struct obdfs_sb_info *sbi;
255 sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
256 obdfs_flush_reqs(&sbi->osi_inodes, 0);
258 OPS(sb,disconnect)(ID(sb));
259 list_del(&sbi->osi_list);
260 memset(sbi, 0, sizeof(*sbi));
262 printk(KERN_INFO "OBDFS: Bye bye.\n");
266 } /* obdfs_put_super */
269 /* all filling in of inodes postponed until lookup */
270 static void obdfs_read_inode(struct inode *inode)
275 oa = obdo_fromid(IID(inode), inode->i_ino,
276 OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
278 printk(__FUNCTION__ ": obdo_fromid failed\n");
280 return /* PTR_ERR(oa) */;
284 obdfs_to_inode(inode, oa);
285 INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
286 INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
289 /* OIDEBUG(inode); */
291 if (S_ISREG(inode->i_mode)) {
292 inode->i_op = &obdfs_file_inode_operations;
294 } else if (S_ISDIR(inode->i_mode)) {
295 inode->i_op = &obdfs_dir_inode_operations;
297 } else if (S_ISLNK(inode->i_mode)) {
298 inode->i_op = &obdfs_symlink_inode_operations;
301 init_special_inode(inode, inode->i_mode,
302 /* XXX need to fill in the ext2 side */
303 ((long *)obdfs_i2info(inode)->oi_inline)[0]);
307 } /* obdfs_read_inode */
309 static void obdfs_write_inode(struct inode *inode)
317 printk(__FUNCTION__ ": obdo_alloc failed\n");
321 oa->o_valid = OBD_MD_FLNOTOBD;
322 obdfs_from_inode(oa, inode);
323 err = IOPS(inode, setattr)(IID(inode), oa);
326 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
329 /* Copy back attributes from oa, as there may have been
330 * changes at the target (e.g. obdo becomes a redirector
331 * in the snapshot layer).
333 obdfs_to_inode(inode, oa);
338 } /* obdfs_write_inode */
341 /* Dequeue cached pages for a dying inode without writing them to disk.
343 * This routine is called from iput() (for each unlink on the inode).
344 * We can't put this code into delete_inode() since that is called only
345 * when i_count == 0, and we need to keep a reference on the inode while
346 * it is in the page cache, which means i_count > 0. Catch 22.
348 static void obdfs_put_inode(struct inode *inode)
350 struct list_head *tmp;
353 if (inode->i_nlink) {
358 obd_down(&obdfs_i2sbi(inode)->osi_list_mutex);
359 tmp = obdfs_islist(inode);
360 if ( list_empty(tmp) ) {
361 CDEBUG(D_INODE, __FUNCTION__ ": no dirty pages for inode %ld\n",
363 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
368 /* take it out of the super list */
370 INIT_LIST_HEAD(obdfs_islist(inode));
372 tmp = obdfs_iplist(inode);
373 while ( (tmp = tmp->next) != obdfs_iplist(inode) ) {
374 struct obdfs_pgrq *req;
377 req = list_entry(tmp, struct obdfs_pgrq, rq_plist);
379 /* take it out of the list and free */
381 /* now put the page away */
385 /* decrement inode reference for page cache */
389 obd_up(&obdfs_i2sbi(inode)->osi_list_mutex);
390 } /* obdfs_put_inode */
393 static void obdfs_delete_inode(struct inode *inode)
401 printk(__FUNCTION__ ": obdo_alloc failed\n");
404 oa->o_valid = OBD_MD_FLNOTOBD;
405 obdfs_from_inode(oa, inode);
407 err = IOPS(inode, destroy)(IID(inode), oa);
411 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
417 } /* obdfs_delete_inode */
420 static int obdfs_notify_change(struct dentry *de, struct iattr *attr)
422 struct inode *inode = de->d_inode;
429 printk(__FUNCTION__ ": obdo_alloc failed\n");
433 oa->o_id = inode->i_ino;
434 obdo_from_iattr(oa, attr);
435 err = IOPS(inode, setattr)(IID(inode), oa);
438 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
441 /* Copy back attributes from oa, as there may have been
442 * changes at the target (e.g. obdo becomes a redirector
443 * in the snapshot layer).
445 obdfs_to_inode(inode, oa);
451 } /* obdfs_notify_change */
454 static int obdfs_statfs(struct super_block *sb, struct statfs *buf,
462 err = OPS(sb,statfs)(ID(sb), &tmp);
464 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
467 copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
474 /* exported operations */
475 struct super_operations obdfs_super_operations =
477 obdfs_read_inode, /* read_inode */
478 obdfs_write_inode, /* write_inode */
479 obdfs_put_inode, /* put_inode */
480 obdfs_delete_inode, /* delete_inode */
481 obdfs_notify_change, /* notify_change */
482 obdfs_put_super, /* put_super */
483 NULL, /* write_super */
484 obdfs_statfs, /* statfs */
485 NULL /* remount_fs */
488 struct file_system_type obdfs_fs_type = {
489 "obdfs", 0, obdfs_read_super, NULL
496 printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
500 INIT_LIST_HEAD(&obdfs_super_list);
501 err = obdfs_init_pgrqcache();
506 return register_filesystem(&obdfs_fs_type);
511 int init_module(void)
516 void cleanup_module(void)
520 obdfs_flushd_cleanup();
521 obdfs_sysctl_clean();
522 obdfs_cleanup_pgrqcache();
523 unregister_filesystem(&obdfs_fs_type);