2 * Copyright (C) 2005, 2006
3 * Avishay Traeger (avishay@gmail.com)
4 * Copyright (C) 2008, 2009
5 * Boaz Harrosh <bharrosh@panasas.com>
7 * Copyrights for code taken from ext2:
8 * Copyright (C) 1992, 1993, 1994, 1995
9 * Remy Card (card@masi.ibp.fr)
10 * Laboratoire MASI - Institut Blaise Pascal
11 * Universite Pierre et Marie Curie (Paris VI)
13 * linux/fs/minix/inode.c
14 * Copyright (C) 1991, 1992 Linus Torvalds
16 * This file is part of exofs.
18 * exofs is free software; you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation. Since it is based on ext2, and the only
21 * valid version of GPL for the Linux kernel is version 2, the only valid
22 * version of GPL for exofs is version 2.
24 * exofs is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
29 * You should have received a copy of the GNU General Public License
30 * along with exofs; if not, write to the Free Software
31 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
34 #include <linux/smp_lock.h>
35 #include <linux/string.h>
36 #include <linux/parser.h>
37 #include <linux/vfs.h>
38 #include <linux/random.h>
39 #include <linux/exportfs.h>
43 /******************************************************************************
45 *****************************************************************************/
48 * struct to hold what we get from mount options
50 struct exofs_mountopt {
57 * exofs-specific mount-time options.
59 enum { Opt_pid, Opt_to, Opt_mkfs, Opt_format, Opt_err };
62 * Our mount-time options. These should ideally be 64-bit unsigned, but the
63 * kernel's parsing functions do not currently support that. 32-bit should be
64 * sufficient for most applications now.
66 static match_table_t tokens = {
73 * The main option parsing method. Also makes sure that all of the mandatory
74 * mount options were set.
76 static int parse_options(char *options, struct exofs_mountopt *opts)
79 substring_t args[MAX_OPT_ARGS];
83 EXOFS_DBGMSG("parse_options %s\n", options);
85 memset(opts, 0, sizeof(*opts));
86 opts->timeout = BLK_DEFAULT_SG_TIMEOUT;
88 while ((p = strsep(&options, ",")) != NULL) {
95 token = match_token(p, tokens, args);
98 if (0 == match_strlcpy(str, &args[0], sizeof(str)))
100 opts->pid = simple_strtoull(str, NULL, 0);
101 if (opts->pid < EXOFS_MIN_PID) {
102 EXOFS_ERR("Partition ID must be >= %u",
109 if (match_int(&args[0], &option))
112 EXOFS_ERR("Timout must be > 0");
115 opts->timeout = option * HZ;
121 EXOFS_ERR("Need to specify the following options:\n");
122 EXOFS_ERR(" -o pid=pid_no_to_use\n");
129 /******************************************************************************
131 *****************************************************************************/
134 * Our inode cache. Isn't it pretty?
136 static struct kmem_cache *exofs_inode_cachep;
139 * Allocate an inode in the cache
141 static struct inode *exofs_alloc_inode(struct super_block *sb)
143 struct exofs_i_info *oi;
145 oi = kmem_cache_alloc(exofs_inode_cachep, GFP_KERNEL);
149 oi->vfs_inode.i_version = 1;
150 return &oi->vfs_inode;
154 * Remove an inode from the cache
156 static void exofs_destroy_inode(struct inode *inode)
158 kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
162 * Initialize the inode
164 static void exofs_init_once(void *foo)
166 struct exofs_i_info *oi = foo;
168 inode_init_once(&oi->vfs_inode);
172 * Create and initialize the inode cache
174 static int init_inodecache(void)
176 exofs_inode_cachep = kmem_cache_create("exofs_inode_cache",
177 sizeof(struct exofs_i_info), 0,
178 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
180 if (exofs_inode_cachep == NULL)
186 * Destroy the inode cache
188 static void destroy_inodecache(void)
190 kmem_cache_destroy(exofs_inode_cachep);
193 /******************************************************************************
194 * SUPERBLOCK FUNCTIONS
195 *****************************************************************************/
196 static const struct super_operations exofs_sops;
197 static const struct export_operations exofs_export_ops;
200 * Write the superblock to the OSD
202 int exofs_sync_fs(struct super_block *sb, int wait)
204 struct exofs_sb_info *sbi;
205 struct exofs_fscb *fscb;
206 struct exofs_io_state *ios;
213 ret = exofs_get_io_state(&sbi->layout, &ios);
217 /* Note: We only write the changing part of the fscb. .i.e upto the
218 * the fscb->s_dev_table_oid member. There is no read-modify-write
221 ios->length = offsetof(struct exofs_fscb, s_dev_table_oid);
222 memset(fscb, 0, ios->length);
223 fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
224 fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
225 fscb->s_magic = cpu_to_le16(sb->s_magic);
227 fscb->s_version = EXOFS_FSCB_VER;
229 ios->obj.id = EXOFS_SUPER_ID;
231 ios->kern_buff = fscb;
232 ios->cred = sbi->s_cred;
234 ret = exofs_sbi_write(ios);
236 EXOFS_ERR("%s: exofs_sbi_write failed.\n", __func__);
242 EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
243 exofs_put_io_state(ios);
248 static void exofs_write_super(struct super_block *sb)
250 if (!(sb->s_flags & MS_RDONLY))
251 exofs_sync_fs(sb, 1);
256 static void _exofs_print_device(const char *msg, const char *dev_path,
257 struct osd_dev *od, u64 pid)
259 const struct osd_dev_info *odi = osduld_device_info(od);
261 printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
262 msg, dev_path ?: "", odi->osdname, _LLU(pid));
265 void exofs_free_sbi(struct exofs_sb_info *sbi)
267 while (sbi->layout.s_numdevs) {
268 int i = --sbi->layout.s_numdevs;
269 struct osd_dev *od = sbi->layout.s_ods[i];
272 sbi->layout.s_ods[i] = NULL;
273 osduld_put_device(od);
280 * This function is called when the vfs is freeing the superblock. We just
281 * need to free our own part.
283 static void exofs_put_super(struct super_block *sb)
286 struct exofs_sb_info *sbi = sb->s_fs_info;
289 exofs_write_super(sb);
291 /* make sure there are no pending commands */
292 for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
293 num_pend = atomic_read(&sbi->s_curr_pending)) {
294 wait_queue_head_t wq;
295 init_waitqueue_head(&wq);
296 wait_event_timeout(wq,
297 (atomic_read(&sbi->s_curr_pending) == 0),
298 msecs_to_jiffies(100));
301 _exofs_print_device("Unmounting", NULL, sbi->layout.s_ods[0],
305 sb->s_fs_info = NULL;
308 static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
309 struct exofs_device_table *dt)
311 sbi->data_map.odm_num_comps =
312 le32_to_cpu(dt->dt_data_map.cb_num_comps);
313 sbi->data_map.odm_stripe_unit =
314 le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
315 sbi->data_map.odm_group_width =
316 le32_to_cpu(dt->dt_data_map.cb_group_width);
317 sbi->data_map.odm_group_depth =
318 le32_to_cpu(dt->dt_data_map.cb_group_depth);
319 sbi->data_map.odm_mirror_cnt =
320 le32_to_cpu(dt->dt_data_map.cb_mirror_cnt);
321 sbi->data_map.odm_raid_algorithm =
322 le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);
324 /* FIXME: Hard coded mirror only for now. if not so do not mount */
325 if ((sbi->data_map.odm_num_comps != numdevs) ||
326 (sbi->data_map.odm_stripe_unit != EXOFS_BLKSIZE) ||
327 (sbi->data_map.odm_raid_algorithm != PNFS_OSD_RAID_0) ||
328 (sbi->data_map.odm_mirror_cnt != (numdevs - 1)))
334 /* @odi is valid only as long as @fscb_dev is valid */
335 static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
336 struct osd_dev_info *odi)
338 odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
339 memcpy(odi->systemid, dt_dev->systemid, odi->systemid_len);
341 odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
342 odi->osdname = dt_dev->osdname;
344 /* FIXME support long names. Will need a _put function */
345 if (dt_dev->long_name_offset)
348 /* Make sure osdname is printable!
349 * mkexofs should give us space for a null-terminator else the
350 * device-table is invalid.
352 if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
353 odi->osdname_len = sizeof(dt_dev->osdname) - 1;
354 dt_dev->osdname[odi->osdname_len] = 0;
356 /* If it's all zeros something is bad we read past end-of-obj */
357 return !(odi->systemid_len || odi->osdname_len);
360 static int exofs_read_lookup_dev_table(struct exofs_sb_info **psbi,
361 unsigned table_count)
363 struct exofs_sb_info *sbi = *psbi;
364 struct osd_dev *fscb_od;
365 struct osd_obj_id obj = {.partition = sbi->layout.s_pid,
366 .id = EXOFS_DEVTABLE_ID};
367 struct exofs_device_table *dt;
368 unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
373 dt = kmalloc(table_bytes, GFP_KERNEL);
375 EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
380 fscb_od = sbi->layout.s_ods[0];
381 sbi->layout.s_ods[0] = NULL;
382 sbi->layout.s_numdevs = 0;
383 ret = exofs_read_kern(fscb_od, sbi->s_cred, &obj, 0, dt, table_bytes);
385 EXOFS_ERR("ERROR: reading device table\n");
389 numdevs = le64_to_cpu(dt->dt_num_devices);
390 if (unlikely(!numdevs)) {
394 WARN_ON(table_count != numdevs);
396 ret = _read_and_match_data_map(sbi, numdevs, dt);
400 if (likely(numdevs > 1)) {
401 unsigned size = numdevs * sizeof(sbi->layout.s_ods[0]);
403 sbi = krealloc(sbi, sizeof(*sbi) + size, GFP_KERNEL);
404 if (unlikely(!sbi)) {
408 memset(&sbi->layout.s_ods[1], 0,
409 size - sizeof(sbi->layout.s_ods[0]));
413 for (i = 0; i < numdevs; i++) {
414 struct exofs_fscb fscb;
415 struct osd_dev_info odi;
418 if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
419 EXOFS_ERR("ERROR: Read all-zeros device entry\n");
424 printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
427 /* On all devices the device table is identical. The user can
428 * specify any one of the participating devices on the command
429 * line. We always keep them in device-table order.
431 if (fscb_od && osduld_device_same(fscb_od, &odi)) {
432 sbi->layout.s_ods[i] = fscb_od;
433 ++sbi->layout.s_numdevs;
438 od = osduld_info_lookup(&odi);
439 if (unlikely(IS_ERR(od))) {
441 EXOFS_ERR("ERROR: device requested is not found "
442 "osd_name-%s =>%d\n", odi.osdname, ret);
446 sbi->layout.s_ods[i] = od;
447 ++sbi->layout.s_numdevs;
449 /* Read the fscb of the other devices to make sure the FS
450 * partition is there.
452 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb,
455 EXOFS_ERR("ERROR: Malformed participating device "
456 "error reading fscb osd_name-%s\n",
461 /* TODO: verify other information is correct and FS-uuid
462 * matches. Benny what did you say about device table
463 * generation and old devices?
469 if (unlikely(!ret && fscb_od)) {
471 "ERROR: Bad device-table container device not present\n");
472 osduld_put_device(fscb_od);
480 * Read the superblock from the OSD and fill in the fields
482 static int exofs_fill_super(struct super_block *sb, void *data, int silent)
485 struct exofs_mountopt *opts = data;
486 struct exofs_sb_info *sbi; /*extended info */
487 struct osd_dev *od; /* Master device */
488 struct exofs_fscb fscb; /*on-disk superblock info */
489 struct osd_obj_id obj;
490 unsigned table_count;
493 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
497 /* use mount options to fill superblock */
498 od = osduld_path_lookup(opts->dev_name);
504 /* Default layout in case we do not have a device-table */
505 sbi->layout.s_ods[0] = od;
506 sbi->layout.s_numdevs = 1;
507 sbi->layout.s_pid = opts->pid;
508 sbi->s_timeout = opts->timeout;
510 /* fill in some other data by hand */
511 memset(sb->s_id, 0, sizeof(sb->s_id));
512 strcpy(sb->s_id, "exofs");
513 sb->s_blocksize = EXOFS_BLKSIZE;
514 sb->s_blocksize_bits = EXOFS_BLKSHIFT;
515 sb->s_maxbytes = MAX_LFS_FILESIZE;
516 atomic_set(&sbi->s_curr_pending, 0);
520 obj.partition = sbi->layout.s_pid;
521 obj.id = EXOFS_SUPER_ID;
522 exofs_make_credential(sbi->s_cred, &obj);
524 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb, sizeof(fscb));
528 sb->s_magic = le16_to_cpu(fscb.s_magic);
529 sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
530 sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
532 /* make sure what we read from the object store is correct */
533 if (sb->s_magic != EXOFS_SUPER_MAGIC) {
535 EXOFS_ERR("ERROR: Bad magic value\n");
539 if (le32_to_cpu(fscb.s_version) != EXOFS_FSCB_VER) {
540 EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
541 EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
546 /* start generation numbers from a random point */
547 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
548 spin_lock_init(&sbi->s_next_gen_lock);
550 table_count = le64_to_cpu(fscb.s_dev_table_count);
552 ret = exofs_read_lookup_dev_table(&sbi, table_count);
557 /* set up operation vectors */
559 sb->s_op = &exofs_sops;
560 sb->s_export_op = &exofs_export_ops;
561 root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
563 EXOFS_ERR("ERROR: exofs_iget failed\n");
567 sb->s_root = d_alloc_root(root);
570 EXOFS_ERR("ERROR: get root inode failed\n");
575 if (!S_ISDIR(root->i_mode)) {
578 EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
584 _exofs_print_device("Mounting", opts->dev_name, sbi->layout.s_ods[0],
589 EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
590 opts->dev_name, sbi->layout.s_pid, ret);
596 * Set up the superblock (calls exofs_fill_super eventually)
598 static int exofs_get_sb(struct file_system_type *type,
599 int flags, const char *dev_name,
600 void *data, struct vfsmount *mnt)
602 struct exofs_mountopt opts;
605 ret = parse_options(data, &opts);
609 opts.dev_name = dev_name;
610 return get_sb_nodev(type, flags, &opts, exofs_fill_super, mnt);
614 * Return information about the file system state in the buffer. This is used
615 * by the 'df' command, for example.
617 static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
619 struct super_block *sb = dentry->d_sb;
620 struct exofs_sb_info *sbi = sb->s_fs_info;
621 struct exofs_io_state *ios;
622 struct osd_attr attrs[] = {
623 ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
624 OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
625 ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
626 OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
628 uint64_t capacity = ULLONG_MAX;
629 uint64_t used = ULLONG_MAX;
630 uint8_t cred_a[OSD_CAP_LEN];
633 ret = exofs_get_io_state(&sbi->layout, &ios);
635 EXOFS_DBGMSG("exofs_get_io_state failed.\n");
639 exofs_make_credential(cred_a, &ios->obj);
640 ios->cred = sbi->s_cred;
641 ios->in_attr = attrs;
642 ios->in_attr_len = ARRAY_SIZE(attrs);
644 ret = exofs_sbi_read(ios);
648 ret = extract_attr_from_ios(ios, &attrs[0]);
650 capacity = get_unaligned_be64(attrs[0].val_ptr);
651 if (unlikely(!capacity))
652 capacity = ULLONG_MAX;
654 EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
656 ret = extract_attr_from_ios(ios, &attrs[1]);
658 used = get_unaligned_be64(attrs[1].val_ptr);
660 EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
662 /* fill in the stats buffer */
663 buf->f_type = EXOFS_SUPER_MAGIC;
664 buf->f_bsize = EXOFS_BLKSIZE;
665 buf->f_blocks = capacity >> 9;
666 buf->f_bfree = (capacity - used) >> 9;
667 buf->f_bavail = buf->f_bfree;
668 buf->f_files = sbi->s_numfiles;
669 buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
670 buf->f_namelen = EXOFS_NAME_LEN;
673 exofs_put_io_state(ios);
677 static const struct super_operations exofs_sops = {
678 .alloc_inode = exofs_alloc_inode,
679 .destroy_inode = exofs_destroy_inode,
680 .write_inode = exofs_write_inode,
681 .delete_inode = exofs_delete_inode,
682 .put_super = exofs_put_super,
683 .write_super = exofs_write_super,
684 .sync_fs = exofs_sync_fs,
685 .statfs = exofs_statfs,
688 /******************************************************************************
690 *****************************************************************************/
692 struct dentry *exofs_get_parent(struct dentry *child)
694 unsigned long ino = exofs_parent_ino(child);
699 return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
702 static struct inode *exofs_nfs_get_inode(struct super_block *sb,
703 u64 ino, u32 generation)
707 inode = exofs_iget(sb, ino);
709 return ERR_CAST(inode);
710 if (generation && inode->i_generation != generation) {
711 /* we didn't find the right inode.. */
713 return ERR_PTR(-ESTALE);
718 static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
719 struct fid *fid, int fh_len, int fh_type)
721 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
722 exofs_nfs_get_inode);
725 static struct dentry *exofs_fh_to_parent(struct super_block *sb,
726 struct fid *fid, int fh_len, int fh_type)
728 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
729 exofs_nfs_get_inode);
732 static const struct export_operations exofs_export_ops = {
733 .fh_to_dentry = exofs_fh_to_dentry,
734 .fh_to_parent = exofs_fh_to_parent,
735 .get_parent = exofs_get_parent,
738 /******************************************************************************
740 *****************************************************************************/
743 * struct that describes this file system
745 static struct file_system_type exofs_type = {
746 .owner = THIS_MODULE,
748 .get_sb = exofs_get_sb,
749 .kill_sb = generic_shutdown_super,
752 static int __init init_exofs(void)
756 err = init_inodecache();
760 err = register_filesystem(&exofs_type);
766 destroy_inodecache();
771 static void __exit exit_exofs(void)
773 unregister_filesystem(&exofs_type);
774 destroy_inodecache();
777 MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
778 MODULE_DESCRIPTION("exofs");
779 MODULE_LICENSE("GPL");
781 module_init(init_exofs)
782 module_exit(exit_exofs)