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;
38 long obdfs_mutex_start = 0;
41 static char *obdfs_read_opt(const char *opt, char *data)
46 CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
47 if ( strncmp(opt, data, strlen(opt)) )
50 if ( (value = strchr(data, '=')) == NULL )
54 OBD_ALLOC(retval, char *, strlen(value) + 1);
56 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
60 memcpy(retval, value, strlen(value)+1);
61 CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
65 static void obdfs_options(char *options, char **dev, char **vers)
72 for (this_char = strtok (options, ",");
74 this_char = strtok (NULL, ",")) {
75 CDEBUG(D_INFO, "this_char %s\n", this_char);
76 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
77 (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
83 static int obdfs_getdev(char *devpath, int *dev)
85 struct dentry *dentry;
88 dentry = lookup_dentry(devpath, NULL, 0);
90 return PTR_ERR(dentry);
92 if (!S_ISCHR(dentry->d_inode->i_mode))
95 devno = dentry->d_inode->i_rdev;
96 if ( MAJOR(devno) != OBD_PSDEV_MAJOR )
99 if ( MINOR(devno) >= MAX_OBD_DEVICES )
107 static struct super_block * obdfs_read_super(struct super_block *sb,
108 void *data, int silent)
110 struct inode *root = 0;
111 struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
112 struct obd_device *obddev;
114 char *version = NULL;
117 unsigned long blocksize;
118 unsigned long blocksize_bits;
119 unsigned long root_ino;
126 memset(sbi, 0, sizeof(*sbi));
128 obdfs_options(data, &device, &version);
130 printk(__FUNCTION__ ": no device\n");
135 if ( (err = obdfs_getdev(device, &devno)) ) {
136 printk("Cannot get devno of %s, error %d\n", device, err);
141 if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
142 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));
154 obddev = &obd_dev[MINOR(devno)];
156 if ( ! (obddev->obd_flags & OBD_ATTACHED) ||
157 ! (obddev->obd_flags & OBD_SET_UP) ){
158 printk("device %s not attached or not set up (%d)\n",
159 device, MINOR(devno));
164 sbi->osi_obd = obddev;
165 sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
167 sbi->osi_conn.oc_dev = obddev;
168 err = sbi->osi_ops->o_connect(&sbi->osi_conn);
170 printk("OBDFS: cannot connect to %s\n", device);
175 /* list of dirty inodes, and a mutex to hold while modifying it */
176 INIT_LIST_HEAD(&sbi->osi_inodes);
177 sema_init(&sbi->osi_list_mutex, 1);
181 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
182 "blocksize", &scratch,
185 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");
199 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"),
200 "root_ino", &scratch, (void *)&root_ino);
202 printk("getinfo call to drive failed (root_ino)\n");
209 sb->s_blocksize = blocksize;
210 sb->s_blocksize_bits = (unsigned char)blocksize_bits;
211 sb->s_magic = OBDFS_SUPER_MAGIC;
212 sb->s_op = &obdfs_super_operations;
214 /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
216 /* make root inode */
217 root = iget(sb, root_ino);
218 if (!root || is_bad_inode(root)) {
219 printk("OBDFS: bad iget for root\n");
227 CDEBUG(D_INFO, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
228 "minor: %d, blocksize: %ld, blocksize bits %ld\n",
229 sb->s_dev, root->i_ino, device, MINOR(devno),
230 blocksize, blocksize_bits);
231 sb->s_root = d_alloc_root(root);
232 list_add(&sbi->osi_list, &obdfs_super_list);
234 OBD_FREE(device, strlen(device) + 1);
236 OBD_FREE(version, strlen(version) + 1);
243 OBD_FREE(device, strlen(device) + 1);
245 OBD_FREE(version, strlen(version) + 1);
247 sbi->osi_super = NULL;
254 } /* obdfs_read_super */
257 static void obdfs_put_super(struct super_block *sb)
259 struct obdfs_sb_info *sbi;
264 sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
265 obdfs_flush_reqs(&sbi->osi_inodes, ~0UL);
267 OPS(sb,disconnect)(ID(sb));
268 list_del(&sbi->osi_list);
269 memset(sbi, 0, sizeof(*sbi));
271 printk(KERN_INFO "OBDFS: Bye bye.\n");
275 } /* obdfs_put_super */
278 /* all filling in of inodes postponed until lookup */
279 static void obdfs_read_inode(struct inode *inode)
284 oa = obdo_fromid(IID(inode), inode->i_ino,
285 OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
287 printk(__FUNCTION__ ": obdo_fromid failed\n");
289 return /* PTR_ERR(oa) */;
293 obdfs_to_inode(inode, oa);
294 INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
295 INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
298 /* OIDEBUG(inode); */
300 if (S_ISREG(inode->i_mode)) {
301 inode->i_op = &obdfs_file_inode_operations;
303 } else if (S_ISDIR(inode->i_mode)) {
304 inode->i_op = &obdfs_dir_inode_operations;
306 } else if (S_ISLNK(inode->i_mode)) {
307 inode->i_op = &obdfs_symlink_inode_operations;
310 init_special_inode(inode, inode->i_mode,
311 ((int *)obdfs_i2info(inode)->oi_inline)[0]);
315 } /* obdfs_read_inode */
317 static void obdfs_write_inode(struct inode *inode)
323 if (IOPS(inode, setattr) == NULL) {
324 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
330 printk(__FUNCTION__ ": obdo_alloc failed\n");
334 oa->o_valid = OBD_MD_FLNOTOBD;
335 obdfs_from_inode(oa, inode);
336 err = IOPS(inode, setattr)(IID(inode), oa);
339 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
342 /* Copy back attributes from oa, as there may have been
343 * changes at the target (e.g. obdo becomes a redirector
344 * in the snapshot layer).
346 obdfs_to_inode(inode, oa);
351 } /* obdfs_write_inode */
354 /* This routine is called from iput() (for each unlink on the inode).
355 * We can't put this call into delete_inode() since that is called only
356 * when i_count == 0, and we need to keep a reference on the inode while
357 * it is in the page cache, which means i_count > 0. Catch 22.
359 static void obdfs_put_inode(struct inode *inode)
362 if (inode->i_nlink) {
367 obdfs_dequeue_pages(inode);
369 } /* obdfs_put_inode */
372 static void obdfs_delete_inode(struct inode *inode)
378 if (IOPS(inode, destroy) == NULL) {
379 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
386 printk(__FUNCTION__ ": obdo_alloc failed\n");
390 oa->o_valid = OBD_MD_FLNOTOBD;
391 obdfs_from_inode(oa, inode);
394 err = IOPS(inode, destroy)(IID(inode), oa);
398 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
404 } /* obdfs_delete_inode */
407 static int obdfs_notify_change(struct dentry *de, struct iattr *attr)
409 struct inode *inode = de->d_inode;
414 if (IOPS(inode, setattr) == NULL) {
415 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
421 printk(__FUNCTION__ ": obdo_alloc failed\n");
425 oa->o_id = inode->i_ino;
426 obdo_from_iattr(oa, attr);
427 err = IOPS(inode, setattr)(IID(inode), oa);
430 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
433 /* Copy back attributes from oa, as there may have been
434 * changes at the target (e.g. obdo becomes a redirector
435 * in the snapshot layer).
437 obdfs_to_inode(inode, oa);
443 } /* obdfs_notify_change */
446 static int obdfs_statfs(struct super_block *sb, struct statfs *buf,
454 err = OPS(sb,statfs)(ID(sb), &tmp);
456 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
459 copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
466 /* exported operations */
467 struct super_operations obdfs_super_operations =
469 obdfs_read_inode, /* read_inode */
470 obdfs_write_inode, /* write_inode */
471 obdfs_put_inode, /* put_inode */
472 obdfs_delete_inode, /* delete_inode */
473 obdfs_notify_change, /* notify_change */
474 obdfs_put_super, /* put_super */
475 NULL, /* write_super */
476 obdfs_statfs, /* statfs */
477 NULL /* remount_fs */
480 struct file_system_type obdfs_fs_type = {
481 "obdfs", 0, obdfs_read_super, NULL
488 printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
492 INIT_LIST_HEAD(&obdfs_super_list);
493 err = obdfs_init_pgrqcache();
498 return register_filesystem(&obdfs_fs_type);
503 int init_module(void)
508 void cleanup_module(void)
512 obdfs_flushd_cleanup();
513 obdfs_sysctl_clean();
514 obdfs_cleanup_pgrqcache();
515 unregister_filesystem(&obdfs_fs_type);
516 CDEBUG(D_MALLOC, "OBDFS mem used %ld\n", obd_memory);