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.
14 #include <linux/config.h>
15 #include <linux/module.h>
16 #include <linux/kernel.h>
18 #include <linux/string.h>
19 #include <linux/stat.h>
20 #include <linux/errno.h>
21 #include <linux/locks.h>
22 #include <linux/unistd.h>
24 #include <asm/system.h>
25 #include <asm/uaccess.h>
28 #include <linux/stat.h>
29 #include <asm/uaccess.h>
30 #include <linux/vmalloc.h>
31 #include <asm/segment.h>
33 #include <linux/obd_support.h>
34 #include <linux/obd_class.h>
35 #include <linux/obdfs.h>
37 struct list_head obdfs_super_list;
38 struct address_space_operations obdfs_aops;
39 struct super_operations obdfs_super_operations;
40 long obdfs_cache_count = 0;
41 long obdfs_mutex_start = 0;
44 static char *obdfs_read_opt(const char *opt, char *data)
49 CDEBUG(D_INFO, "option: %s, data %s\n", opt, data);
50 if ( strncmp(opt, data, strlen(opt)) )
53 if ( (value = strchr(data, '=')) == NULL )
57 OBD_ALLOC(retval, char *, strlen(value) + 1);
59 printk(KERN_ALERT __FUNCTION__ ": out of memory!\n");
63 memcpy(retval, value, strlen(value)+1);
64 CDEBUG(D_PSDEV, "Assigned option: %s, value %s\n", opt, retval);
68 static void obdfs_options(char *options, char **dev, char **vers)
75 for (this_char = strtok (options, ",");
77 this_char = strtok (NULL, ",")) {
78 CDEBUG(D_INFO, "this_char %s\n", this_char);
79 if ( (!*dev && (*dev = obdfs_read_opt("device", this_char)))||
80 (!*vers && (*vers = obdfs_read_opt("version", this_char))) )
86 static int obdfs_getdev(char *devpath, int *dev)
88 struct dentry *dentry;
94 if (path_init(devpath, LOOKUP_POSITIVE, &nd))
95 error = path_walk(devpath, &nd);
100 if (!S_ISCHR(dentry->d_inode->i_mode))
103 devno = dentry->d_inode->i_rdev;
104 if ( MAJOR(devno) != OBD_PSDEV_MAJOR )
107 if ( MINOR(devno) >= MAX_OBD_DEVICES )
115 static struct super_block * obdfs_read_super(struct super_block *sb,
116 void *data, int silent)
118 struct inode *root = 0;
119 struct obdfs_sb_info *sbi = (struct obdfs_sb_info *)(&sb->u.generic_sbp);
120 struct obd_device *obddev;
122 char *version = NULL;
125 unsigned long blocksize;
126 unsigned long blocksize_bits;
127 unsigned long root_ino;
135 memset(sbi, 0, sizeof(*sbi));
137 CDEBUG(D_INFO, "\n");
138 obdfs_options(data, &device, &version);
140 printk(__FUNCTION__ ": no device\n");
145 CDEBUG(D_INFO, "\n");
146 if ( (err = obdfs_getdev(device, &devno)) ) {
147 printk("Cannot get devno of %s, error %d\n", device, err);
152 CDEBUG(D_INFO, "\n");
153 if ( MAJOR(devno) != OBD_PSDEV_MAJOR ) {
154 printk(__FUNCTION__ ": wrong major number %d!\n", MAJOR(devno));
159 CDEBUG(D_INFO, "\n");
160 if ( MINOR(devno) >= MAX_OBD_DEVICES ) {
161 printk(__FUNCTION__ ": minor of %s too high (%d)\n",
162 device, MINOR(devno));
167 CDEBUG(D_INFO, "\n");
168 obddev = &obd_dev[MINOR(devno)];
170 CDEBUG(D_INFO, "\n");
171 if ( ! (obddev->obd_flags & OBD_ATTACHED) ||
172 ! (obddev->obd_flags & OBD_SET_UP) ){
173 printk("device %s not attached or not set up (%d)\n",
174 device, MINOR(devno));
179 CDEBUG(D_INFO, "\n");
180 sbi->osi_obd = obddev;
181 sbi->osi_ops = sbi->osi_obd->obd_type->typ_ops;
183 sbi->osi_conn.oc_dev = obddev;
184 err = sbi->osi_ops->o_connect(&sbi->osi_conn);
186 printk("OBDFS: cannot connect to %s\n", device);
191 CDEBUG(D_INFO, "\n");
192 /* list of dirty inodes, and a mutex to hold while modifying it */
193 INIT_LIST_HEAD(&sbi->osi_inodes);
194 init_MUTEX (&sbi->osi_list_mutex);
196 CDEBUG(D_INFO, "\n");
199 CDEBUG(D_INFO, "\n");
200 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize"),
201 "blocksize", &scratch,
204 printk("getinfo call to drive failed (blocksize)\n");
209 CDEBUG(D_INFO, "\n");
210 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("blocksize_bits"),
211 "blocksize_bits", &scratch,
212 (void *)&blocksize_bits);
214 printk("getinfo call to drive failed (blocksize_bits)\n");
219 CDEBUG(D_INFO, "\n");
220 err = sbi->osi_ops->o_get_info(&sbi->osi_conn, strlen("root_ino"),
221 "root_ino", &scratch, (void *)&root_ino);
223 printk("getinfo call to drive failed (root_ino)\n");
228 CDEBUG(D_INFO, "\n");
229 sb->s_maxbytes = 1LL << 36;
230 printk("Max bytes: %Lx\n", sb->s_maxbytes);
231 sb->s_blocksize = PAGE_SIZE;
232 sb->s_blocksize_bits = (unsigned char)PAGE_SHIFT;
233 sb->s_magic = OBDFS_SUPER_MAGIC;
234 sb->s_op = &obdfs_super_operations;
236 /* XXX how to get "sb->s_flags |= MS_RDONLY" here for snapshots? */
238 /* make root inode */
239 CDEBUG(D_INFO, "\n");
240 oa = obdo_fromid(&sbi->osi_conn, root_ino,
241 OBD_MD_FLNOTOBD | OBD_MD_FLBLOCKS);
242 CDEBUG(D_INFO, "\n");
244 printk(__FUNCTION__ ": obdo_fromid failed\n");
249 CDEBUG(D_INFO, "\n");
250 root = iget4(sb, root_ino, NULL, oa);
252 CDEBUG(D_INFO, "\n");
254 printk("OBDFS: bad iget4 for root\n");
261 CDEBUG(D_INFO, "obdfs_read_super: sbdev %d, rootino: %ld, dev %s, "
262 "minor: %d, blocksize: %ld, blocksize bits %ld\n",
263 sb->s_dev, root->i_ino, device, MINOR(devno),
264 blocksize, blocksize_bits);
265 sb->s_root = d_alloc_root(root);
266 list_add(&sbi->osi_list, &obdfs_super_list);
267 OBD_FREE(device, strlen(device) + 1);
269 OBD_FREE(version, strlen(version) + 1);
276 OBD_FREE(device, strlen(device) + 1);
278 OBD_FREE(version, strlen(version) + 1);
280 sbi->osi_super = NULL;
287 } /* obdfs_read_super */
290 static void obdfs_put_super(struct super_block *sb)
292 struct obdfs_sb_info *sbi;
297 sbi = (struct obdfs_sb_info *) &sb->u.generic_sbp;
298 //obdfs_flush_reqs(&sbi->osi_inodes, ~0UL);
300 OPS(sb,disconnect)(ID(sb));
301 list_del(&sbi->osi_list);
303 printk(KERN_INFO "OBDFS: Bye bye.\n");
307 } /* obdfs_put_super */
310 void obdfs_do_change_inode(struct inode *inode, int valid)
316 if (IOPS(inode, setattr) == NULL) {
317 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
323 printk(__FUNCTION__ ": obdo_alloc failed\n");
328 oa->o_valid = OBD_MD_FLNOTOBD & (valid | OBD_MD_FLID);
329 obdfs_from_inode(oa, inode);
330 err = IOPS(inode, setattr)(IID(inode), oa);
333 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
337 } /* obdfs_write_inode */
339 void obdfs_change_inode(struct inode *inode, int mask)
341 return obdfs_do_change_inode(inode, OBD_MD_FLNLINK);
345 extern void write_inode_pages(struct inode *);
346 /* This routine is called from iput() (for each unlink on the inode).
347 * We can't put this call into delete_inode() since that is called only
348 * when i_count == 0, and we need to keep a reference on the inode while
349 * it is in the page cache, which means i_count > 0. Catch 22.
351 static void obdfs_put_inode(struct inode *inode)
354 if (inode->i_nlink && (atomic_read(&inode->i_count) == 1)) {
355 write_inode_pages(inode);
360 //obdfs_dequeue_pages(inode);
362 } /* obdfs_put_inode */
365 static void obdfs_delete_inode(struct inode *inode)
367 obdfs_do_change_inode(inode, ~0);
376 if (IOPS(inode, destroy) == NULL) {
377 printk(KERN_ERR __FUNCTION__ ": no destroy method!\n");
384 printk(__FUNCTION__ ": obdo_alloc failed\n");
388 oa->o_valid = OBD_MD_FLNOTOBD;
389 obdfs_from_inode(oa, inode);
391 /* XXX how do we know that this inode is now clean? */
392 printk("delete_inode ------> link %d\n", inode->i_nlink);
394 err = IOPS(inode, destroy)(IID(inode), oa);
398 printk(__FUNCTION__ ": obd_destroy fails (%d)\n", err);
404 } /* obdfs_delete_inode */
408 static int obdfs_attr2inode(struct inode * inode, struct iattr * attr)
410 unsigned int ia_valid = attr->ia_valid;
413 if (ia_valid & ATTR_SIZE) {
414 error = vmtruncate(inode, attr->ia_size);
419 if (ia_valid & ATTR_UID)
420 inode->i_uid = attr->ia_uid;
421 if (ia_valid & ATTR_GID)
422 inode->i_gid = attr->ia_gid;
423 if (ia_valid & ATTR_ATIME)
424 inode->i_atime = attr->ia_atime;
425 if (ia_valid & ATTR_MTIME)
426 inode->i_mtime = attr->ia_mtime;
427 if (ia_valid & ATTR_CTIME)
428 inode->i_ctime = attr->ia_ctime;
429 if (ia_valid & ATTR_MODE) {
430 inode->i_mode = attr->ia_mode;
431 if (!in_group_p(inode->i_gid) && !capable(CAP_FSETID))
432 inode->i_mode &= ~S_ISGID;
438 int obdfs_setattr(struct dentry *de, struct iattr *attr)
440 struct inode *inode = de->d_inode;
445 if (IOPS(inode, setattr) == NULL) {
446 printk(KERN_ERR __FUNCTION__ ": no setattr method!\n");
452 printk(__FUNCTION__ ": obdo_alloc failed\n");
456 obdfs_attr2inode(inode, attr);
457 oa->o_id = inode->i_ino;
458 obdo_from_iattr(oa, attr);
459 err = IOPS(inode, setattr)(IID(inode), oa);
462 printk(__FUNCTION__ ": obd_setattr fails (%d)\n", err);
467 } /* obdfs_setattr */
471 static int obdfs_statfs(struct super_block *sb, struct statfs *buf)
478 err = OPS(sb,statfs)(ID(sb), &tmp);
480 printk(__FUNCTION__ ": obd_statfs fails (%d)\n", err);
483 memcpy(buf, &tmp, sizeof(*buf));
484 CDEBUG(D_SUPER, "statfs returns avail %ld\n", tmp.f_bavail);
490 static inline void obdfs_read_inode2(struct inode *inode, void *opaque)
492 struct obdo *oa = opaque;
495 obdfs_to_inode(inode, oa);
497 INIT_LIST_HEAD(obdfs_iplist(inode)); /* list of dirty pages on inode */
498 INIT_LIST_HEAD(obdfs_islist(inode)); /* list of inodes in superblock */
500 /* OIDEBUG(inode); */
502 if (S_ISREG(inode->i_mode)) {
503 inode->i_op = &obdfs_file_inode_operations;
504 inode->i_fop = &obdfs_file_operations;
505 inode->i_mapping->a_ops = &obdfs_aops;
507 } else if (S_ISDIR(inode->i_mode)) {
508 inode->i_op = &obdfs_dir_inode_operations;
509 inode->i_fop = &obdfs_dir_operations;
510 inode->i_mapping->a_ops = &obdfs_aops;
512 } else if (S_ISLNK(inode->i_mode)) {
513 if (inode->i_blocks) {
514 inode->i_op = &obdfs_symlink_inode_operations;
515 inode->i_mapping->a_ops = &obdfs_aops;
517 inode->i_op = &obdfs_fast_symlink_inode_operations;
521 init_special_inode(inode, inode->i_mode,
522 ((int *)obdfs_i2info(inode)->oi_inline)[0]);
529 /* exported operations */
530 struct super_operations obdfs_super_operations =
532 read_inode2: obdfs_read_inode2,
533 put_inode: obdfs_put_inode,
534 delete_inode: obdfs_delete_inode,
535 put_super: obdfs_put_super,
540 struct file_system_type obdfs_fs_type = {
541 "obdfs", 0, obdfs_read_super, NULL
548 printk(KERN_INFO "OBDFS v0.1, braam@stelias.com\n");
552 INIT_LIST_HEAD(&obdfs_super_list);
553 //err = obdfs_init_pgrqcache();
557 //obdfs_flushd_init();
558 return register_filesystem(&obdfs_fs_type);
565 int init_module(void)
570 void cleanup_module(void)
574 //obdfs_flushd_cleanup();
575 obdfs_sysctl_clean();
576 //obdfs_cleanup_pgrqcache();
577 unregister_filesystem(&obdfs_fs_type);
578 CDEBUG(D_MALLOC, "OBDFS mem used %ld\n", obd_memory);