2 * OBDFS Super operations
4 * This code is issued under the GNU General Public License.
5 * See the file COPYING in this distribution
7 * Copryright (C) 1996 Peter J. Braam <braam@stelias.com>
8 * Copryright (C) 1999 Stelias Computing Inc. <braam@stelias.com>
9 * Copryright (C) 1999 Seagate Technology Inc.
10 * Copryright (C) 2001 Mountain View Data, Inc.
16 #include <linux/config.h>
17 #include <linux/module.h>
18 #include <linux/kernel.h>
20 #include <linux/string.h>
21 #include <linux/stat.h>
22 #include <linux/errno.h>
23 #include <linux/locks.h>
24 #include <linux/unistd.h>
26 #include <asm/system.h>
27 #include <asm/uaccess.h>
30 #include <linux/stat.h>
31 #include <asm/uaccess.h>
32 #include <linux/vmalloc.h>
33 #include <asm/segment.h>
35 #include <linux/obd_support.h>
36 #include <linux/obd_class.h>
37 #include <linux/obdfs.h>
39 struct list_head obdfs_super_list;
40 struct address_space_operations obdfs_aops;
41 struct super_operations obdfs_super_operations;
42 long obdfs_cache_count = 0;
43 long obdfs_mutex_start = 0;
46 static char *obdfs_read_opt(const char *opt, char *data)
51 CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
52 if ( strncmp(opt, data, strlen(opt)) )
55 if ( (value = strchr(data, '=')) == NULL )
59 OBD_ALLOC(retval, char *, strlen(value) + 1);
61 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
65 memcpy(retval, value, strlen(value)+1);
66 CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
70 static void obdfs_options(char *options, char **dev, char **vers)
77 for (this_char = strtok (options, ",");
79 this_char = strtok (NULL, ",")) {
80 CDEBUG(D_INFO, "this_char %s\n", this_char);
81 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
82 (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
88 static int obdfs_getdev(char *devpath, int *dev)
90 struct dentry *dentry;
96 if (path_init(devpath, LOOKUP_POSITIVE, &nd))
97 error = path_walk(devpath, &nd);
102 if (!S_ISCHR(dentry->d_inode->i_mode))
105 devno = dentry->d_inode->i_rdev;
106 if ( MAJOR(devno) != OBD_PSDEV_MAJOR )
109 if ( MINOR(devno) >= MAX_OBD_DEVICES )
117 static struct super_block * obdfs_read_super(struct super_block *sb,
118 void *data, int silent)
120 struct inode *root = 0;
121 struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
122 struct obd_device *obddev;
124 char *version = NULL;
127 unsigned long blocksize;
128 unsigned long blocksize_bits;
129 unsigned long root_ino;
136 memset(sbi, 0, sizeof(*sbi));
138 CDEBUG(D_INFO, "\n");
139 obdfs_options(data, &device, &version);
141 printk(__FUNCTION__ ": no device\n");
146 CDEBUG(D_INFO, "\n");
147 if ( (err = obdfs_getdev(device, &devno)) ) {
148 printk("Cannot get devno of %s, error %d\n", device, err);
153 CDEBUG(D_INFO, "\n");
154 if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
155 printk(__FUNCTION__ ": wrong major number %d!\n", MAJOR(devno));
160 CDEBUG(D_INFO, "\n");
161 if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
162 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
163 device, MINOR(devno));
168 CDEBUG(D_INFO, "\n");
169 obddev = &obd_dev[MINOR(devno)];
171 CDEBUG(D_INFO, "\n");
172 if ( ! (obddev->obd_flags & OBD_ATTACHED) ||
173 ! (obddev->obd_flags & OBD_SET_UP) ){
174 printk("device %s not attached or not set up (%d)\n",
175 device, MINOR(devno));
180 CDEBUG(D_INFO, "\n");
181 sbi->osi_obd = obddev;
182 sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
184 sbi->osi_conn.oc_dev = obddev;
185 err = sbi->osi_ops->o_connect(&sbi->osi_conn);
187 printk("OBDFS: cannot connect to %s\n", device);
192 CDEBUG(D_INFO, "\n");
193 /* list of dirty inodes, and a mutex to hold while modifying it */
194 INIT_LIST_HEAD(&sbi->osi_inodes);
195 init_MUTEX (&sbi->osi_list_mutex);
197 CDEBUG(D_INFO, "\n");
200 CDEBUG(D_INFO, "\n");
201 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
202 "blocksize", &scratch,
205 printk("getinfo call to drive failed (blocksize)\n");
210 CDEBUG(D_INFO, "\n");
211 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
212 "blocksize_bits", &scratch,
213 (void *)&blocksize_bits);
215 printk("getinfo call to drive failed (blocksize_bits)\n");
220 CDEBUG(D_INFO, "\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");
229 CDEBUG(D_INFO, "\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 CDEBUG(D_INFO, "\n");
239 root = iget(sb, root_ino);
240 if (!root || is_bad_inode(root)) {
241 printk("OBDFS: bad iget for root\n");
248 CDEBUG(D_INFO, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
249 "minor: %d, blocksize: %ld, blocksize bits %ld\n",
250 sb->s_dev, root->i_ino, device, MINOR(devno),
251 blocksize, blocksize_bits);
252 sb->s_root = d_alloc_root(root);
253 list_add(&sbi->osi_list, &obdfs_super_list);
254 OBD_FREE(device, strlen(device) + 1);
256 OBD_FREE(version, strlen(version) + 1);
263 OBD_FREE(device, strlen(device) + 1);
265 OBD_FREE(version, strlen(version) + 1);
267 sbi->osi_super = NULL;
274 } /* obdfs_read_super */
277 static void obdfs_put_super(struct super_block *sb)
279 struct obdfs_sb_info *sbi;
284 sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
285 obdfs_flush_reqs(&sbi->osi_inodes, ~0UL);
287 OPS(sb,disconnect)(ID(sb));
288 list_del(&sbi->osi_list);
290 printk(KERN_INFO "OBDFS: Bye bye.\n");
294 } /* obdfs_put_super */
297 /* all filling in of inodes postponed until lookup */
298 static void obdfs_read_inode(struct inode *inode)
302 oa = obdo_fromid(IID(inode), inode->i_ino,
303 OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
305 printk(__FUNCTION__ ": obdo_fromid failed\n");
307 return /* PTR_ERR(oa) */;
311 obdfs_to_inode(inode, oa);
312 INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
313 INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
316 /* OIDEBUG(inode); */
318 if (S_ISREG(inode->i_mode)) {
319 inode->i_op = &obdfs_file_inode_operations;
320 inode->i_fop = &obdfs_file_operations;
321 inode->i_mapping->a_ops = &obdfs_aops;
323 } else if (S_ISDIR(inode->i_mode)) {
324 inode->i_op = &obdfs_dir_inode_operations;
325 inode->i_fop = &obdfs_dir_operations;
326 inode->i_mapping->a_ops = &obdfs_aops;
328 } else if (S_ISLNK(inode->i_mode)) {
329 if (inode->i_blocks) {
330 inode->i_op = &obdfs_symlink_inode_operations;
331 inode->i_mapping->a_ops = &obdfs_aops;
333 inode->i_op = &obdfs_fast_symlink_inode_operations;
337 init_special_inode(inode, inode->i_mode,
338 ((int *)obdfs_i2info(inode)->oi_inline)[0]);
344 static void obdfs_write_inode(struct inode *inode, int wait)
350 if (IOPS(inode, setattr) == NULL) {
351 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
357 printk(__FUNCTION__ ": obdo_alloc failed\n");
362 oa->o_valid = OBD_MD_FLNOTOBD;
363 obdfs_from_inode(oa, inode);
364 err = IOPS(inode, setattr)(IID(inode), oa);
367 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
371 } /* obdfs_write_inode */
374 /* This routine is called from iput() (for each unlink on the inode).
375 * We can't put this call into delete_inode() since that is called only
376 * when i_count == 0, and we need to keep a reference on the inode while
377 * it is in the page cache, which means i_count > 0. Catch 22.
379 static void obdfs_put_inode(struct inode *inode)
382 if (inode->i_nlink) {
387 obdfs_dequeue_pages(inode);
389 } /* obdfs_put_inode */
392 static void obdfs_delete_inode(struct inode *inode)
398 if (IOPS(inode, destroy) == NULL) {
399 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
406 printk(__FUNCTION__ ": obdo_alloc failed\n");
410 oa->o_valid = OBD_MD_FLNOTOBD;
411 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 int obdfs_notify_change(struct dentry *de, struct iattr *attr)
429 struct inode *inode = de->d_inode;
434 if (IOPS(inode, setattr) == NULL) {
435 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
441 printk(__FUNCTION__ ": obdo_alloc failed\n");
445 oa->o_id = inode->i_ino;
446 obdo_from_iattr(oa, attr);
447 err = IOPS(inode, setattr)(IID(inode), oa);
450 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
455 } /* obdfs_notify_change */
458 static int obdfs_statfs(struct super_block *sb, struct statfs *buf)
461 int bufsize = sizeof(*buf);
466 err = OPS(sb,statfs)(ID(sb), &tmp);
468 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
471 copy_to_user(buf, &tmp, (bufsize<sizeof(tmp)) ? bufsize : sizeof(tmp));
478 /* exported operations */
479 struct super_operations obdfs_super_operations =
481 read_inode: obdfs_read_inode,
482 write_inode: obdfs_write_inode,
483 put_inode: obdfs_put_inode,
484 delete_inode: obdfs_delete_inode,
485 put_super: obdfs_put_super,
489 struct file_system_type obdfs_fs_type = {
490 "obdfs", 0, obdfs_read_super, NULL
497 printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
501 INIT_LIST_HEAD(&obdfs_super_list);
502 err = obdfs_init_pgrqcache();
507 return register_filesystem(&obdfs_fs_type);
510 struct address_space_operations obdfs_aops = {
511 readpage: obdfs_readpage,
512 writepage: obdfs_writepage,
513 sync_page: block_sync_page,
514 prepare_write: obdfs_prepare_write,
515 commit_write: obdfs_commit_write,
521 int init_module(void)
526 void cleanup_module(void)
530 obdfs_flushd_cleanup();
531 obdfs_sysctl_clean();
532 obdfs_cleanup_pgrqcache();
533 unregister_filesystem(&obdfs_fs_type);
534 CDEBUG(D_MALLOC, "OBDFS mem used %ld\n", obd_memory);