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 /* VFS super_block ops */
36 static struct super_block *obdfs_read_super(struct super_block *, void *, int);
37 static int obdfs_notify_change(struct dentry *dentry, struct iattr *attr);
38 static void obdfs_write_inode(struct inode *);
39 static void obdfs_delete_inode(struct inode *);
40 static void obdfs_put_super(struct super_block *);
41 static int obdfs_statfs(struct super_block *sb, struct statfs *buf,
44 /* exported operations */
45 struct super_operations obdfs_super_operations =
47 obdfs_read_inode, /* read_inode */
48 obdfs_write_inode, /* write_inode */
50 obdfs_delete_inode, /* delete_inode */
51 obdfs_notify_change, /* notify_change */
52 obdfs_put_super, /* put_super */
53 NULL, /* write_super */
54 obdfs_statfs, /* statfs */
58 struct list_head obdfs_super_list;
60 static char *obdfs_read_opt(const char *opt, char *data)
65 CDEBUG(D_SUPER, "option: %s, data %s\n", opt, data);
66 if ( strncmp(opt, data, strlen(opt)) )
69 if ( (value = strchr(data, '=')) == NULL )
73 OBD_ALLOC(retval, char *, strlen(value) + 1);
75 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
79 memcpy(retval, value, strlen(value)+1);
80 CDEBUG(D_SUPER, "Assigned option: %s, value %s\n", opt, retval);
84 void obdfs_options(char *options, char **dev, char **vers)
91 for (this_char = strtok (options, ",");
93 this_char = strtok (NULL, ",")) {
94 CDEBUG(D_SUPER, "this_char %s\n", this_char);
95 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
96 (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
102 static int obdfs_getdev(char *devpath, int *dev)
104 struct dentry *dentry;
107 dentry = lookup_dentry(devpath, NULL, 0);
109 return PTR_ERR(dentry);
111 if (!S_ISCHR(dentry->d_inode->i_mode))
114 devno = dentry->d_inode->i_rdev;
115 if ( MAJOR(devno) != OBD_PSDEV_MAJOR )
118 if ( MINOR(devno) >= MAX_OBD_DEVICES )
126 /* XXX allocate a super_entry, and add the super to the obdfs_super_list */
127 static struct super_block * obdfs_read_super(struct super_block *sb,
128 void *data, int silent)
130 struct inode *root = 0;
131 struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
132 struct obd_device *obddev;
134 char *version = NULL;
137 unsigned long blocksize;
138 unsigned long blocksize_bits;
139 unsigned long root_ino;
146 memset(sbi, 0, sizeof(*sbi));
148 obdfs_options(data, &device, &version);
150 printk(__FUNCTION__ ": no device\n");
156 if ( (err = obdfs_getdev(device, &devno)) ) {
157 printk("Cannot get devno of %s, error %d\n", device, err);
163 if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
164 printk(__FUNCTION__ ": wrong major number %d!\n", MAJOR(devno));
170 if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
171 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
172 device, MINOR(devno));
178 obddev = &obd_dev[MINOR(devno)];
180 if ( ! (obddev->obd_flags & OBD_ATTACHED) ||
181 ! (obddev->obd_flags & OBD_SET_UP) ){
182 printk("device %s not attached or not set up (%d)\n",
183 device, MINOR(devno));
189 sbi->osi_obd = obddev;
190 sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
192 sbi->osi_conn.oc_dev = obddev;
193 err = sbi->osi_ops->o_connect(&sbi->osi_conn);
195 printk("OBDFS: cannot connect to %s\n", device);
199 /* list of dirty inodes, and a mutex to hold while modifying it */
200 INIT_LIST_HEAD(&sbi->osi_inodes);
201 sema_init(&sbi->osi_list_mutex, 1);
205 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
206 "blocksize", &scratch,
209 printk("getinfo call to drive failed (blocksize)\n");
213 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
214 "blocksize_bits", &scratch,
215 (void *)&blocksize_bits);
217 printk("getinfo call to drive failed (blocksize_bits)\n");
221 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"),
222 "root_ino", &scratch, (void *)&root_ino);
224 printk("getinfo call to drive failed (root_ino)\n");
230 sb->s_blocksize = blocksize;
231 sb->s_blocksize_bits = (unsigned char)blocksize_bits;
232 sb->s_magic = OBDFS_SUPER_MAGIC;
233 sb->s_op = &obdfs_super_operations;
235 /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
237 /* make root inode */
238 root = iget(sb, root_ino);
239 if (!root || is_bad_inode(root)) {
240 printk("OBDFS: bad iget for root\n");
247 CDEBUG(D_SUPER, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
248 "minor: %d, blocksize: %ld, blocksize bits %ld\n",
249 sb->s_dev, root->i_ino, device, MINOR(devno),
250 blocksize, blocksize_bits);
251 sb->s_root = d_alloc_root(root);
252 list_add(&sbi->osi_list, &obdfs_super_list);
261 sbi->osi_super = NULL;
271 static void obdfs_put_super(struct super_block *sb)
273 struct obdfs_sb_info *sbi;
278 sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
279 obdfs_flush_reqs(&sbi->osi_inodes, 0);
281 OPS(sb,disconnect)(ID(sb));
282 list_del(&sbi->osi_list);
283 memset(sbi, 0, sizeof(*sbi));
285 printk(KERN_INFO "OBDFS: Bye bye.\n");
289 } /* obdfs_put_super */
292 /* all filling in of inodes postponed until lookup */
293 void obdfs_read_inode(struct inode *inode)
298 oa = obdo_fromid(IID(inode), inode->i_ino,
299 OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
301 printk(__FUNCTION__ ": obdo_fromid failed\n");
303 return /* PTR_ERR(oa) */;
307 obdfs_to_inode(inode, oa);
308 INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
309 INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
312 /* OIDEBUG(inode); */
314 if (S_ISREG(inode->i_mode)) {
315 inode->i_op = &obdfs_file_inode_operations;
317 } else if (S_ISDIR(inode->i_mode)) {
318 inode->i_op = &obdfs_dir_inode_operations;
320 } else if (S_ISLNK(inode->i_mode)) {
321 inode->i_op = &obdfs_symlink_inode_operations;
324 init_special_inode(inode, inode->i_mode,
325 /* XXX need to fill in the ext2 side */
326 ((long *)obdfs_i2info(inode)->oi_inline)[0]);
330 } /* obdfs_read_inode */
332 static void obdfs_write_inode(struct inode *inode)
340 printk(__FUNCTION__ ": obdo_alloc failed\n");
344 oa->o_valid = OBD_MD_FLNOTOBD;
345 obdfs_from_inode(oa, inode);
346 err = IOPS(inode, setattr)(IID(inode), oa);
349 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
352 /* Copy back attributes from oa, as there may have been
353 * changes at the target (e.g. obdo becomes a redirector
354 * in the snapshot layer).
356 obdfs_to_inode(inode, oa);
361 } /* obdfs_write_inode */
364 static void obdfs_delete_inode(struct inode *inode)
372 printk(__FUNCTION__ ": obdo_alloc failed\n");
375 oa->o_valid = OBD_MD_FLNOTOBD;
376 obdfs_from_inode(oa, inode);
378 /* free the cache pages that might be hangning around */
379 obdfs_dequeue_reqs(inode);
381 err = IOPS(inode, destroy)(IID(inode), oa);
385 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
391 } /* obdfs_delete_inode */
394 static int obdfs_notify_change(struct dentry *de, struct iattr *attr)
396 struct inode *inode = de->d_inode;
403 printk(__FUNCTION__ ": obdo_alloc failed\n");
407 oa->o_id = inode->i_ino;
408 obdo_from_iattr(oa, attr);
409 err = IOPS(inode, setattr)(IID(inode), oa);
412 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
415 /* Copy back attributes from oa, as there may have been
416 * changes at the target (e.g. obdo becomes a redirector
417 * in the snapshot layer).
419 obdfs_to_inode(inode, oa);
425 } /* obdfs_notify_change */
428 static int obdfs_statfs(struct super_block *sb, struct statfs *buf,
436 err = OPS(sb,statfs)(ID(sb), &tmp);
438 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
441 copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
448 struct file_system_type obdfs_fs_type = {
449 "obdfs", 0, obdfs_read_super, NULL
456 printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
460 INIT_LIST_HEAD(&obdfs_super_list);
461 err = obdfs_init_pgrqcache();
466 return register_filesystem(&obdfs_fs_type);
471 int init_module(void)
476 void cleanup_module(void)
480 obdfs_flushd_cleanup();
481 obdfs_sysctl_clean();
482 obdfs_cleanup_pgrqcache();
483 unregister_filesystem(&obdfs_fs_type);