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 osd_request *or;
207 struct osd_obj_id obj;
210 fscb = kzalloc(sizeof(struct exofs_fscb), GFP_KERNEL);
212 EXOFS_ERR("exofs_write_super: memory allocation failed.\n");
218 fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
219 fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
220 fscb->s_magic = cpu_to_le16(sb->s_magic);
223 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
225 EXOFS_ERR("exofs_write_super: osd_start_request failed.\n");
229 obj.partition = sbi->s_pid;
230 obj.id = EXOFS_SUPER_ID;
231 ret = osd_req_write_kern(or, &obj, 0, fscb, sizeof(*fscb));
233 EXOFS_ERR("exofs_write_super: osd_req_write_kern failed.\n");
237 ret = exofs_sync_op(or, sbi->s_timeout, sbi->s_cred);
239 EXOFS_ERR("exofs_write_super: exofs_sync_op failed.\n");
252 static void exofs_write_super(struct super_block *sb)
254 if (!(sb->s_flags & MS_RDONLY))
255 exofs_sync_fs(sb, 1);
260 static void _exofs_print_device(const char *msg, const char *dev_path,
261 struct osd_dev *od, u64 pid)
263 const struct osd_dev_info *odi = osduld_device_info(od);
265 printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
266 msg, dev_path ?: "", odi->osdname, _LLU(pid));
270 * This function is called when the vfs is freeing the superblock. We just
271 * need to free our own part.
273 static void exofs_put_super(struct super_block *sb)
276 struct exofs_sb_info *sbi = sb->s_fs_info;
279 exofs_write_super(sb);
281 /* make sure there are no pending commands */
282 for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
283 num_pend = atomic_read(&sbi->s_curr_pending)) {
284 wait_queue_head_t wq;
285 init_waitqueue_head(&wq);
286 wait_event_timeout(wq,
287 (atomic_read(&sbi->s_curr_pending) == 0),
288 msecs_to_jiffies(100));
291 _exofs_print_device("Unmounting", NULL, sbi->s_dev, sbi->s_pid);
292 osduld_put_device(sbi->s_dev);
293 kfree(sb->s_fs_info);
294 sb->s_fs_info = NULL;
298 * Read the superblock from the OSD and fill in the fields
300 static int exofs_fill_super(struct super_block *sb, void *data, int silent)
303 struct exofs_mountopt *opts = data;
304 struct exofs_sb_info *sbi; /*extended info */
305 struct exofs_fscb fscb; /*on-disk superblock info */
306 struct osd_request *or = NULL;
307 struct osd_obj_id obj;
310 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
315 /* use mount options to fill superblock */
316 sbi->s_dev = osduld_path_lookup(opts->dev_name);
317 if (IS_ERR(sbi->s_dev)) {
318 ret = PTR_ERR(sbi->s_dev);
323 sbi->s_pid = opts->pid;
324 sbi->s_timeout = opts->timeout;
326 /* fill in some other data by hand */
327 memset(sb->s_id, 0, sizeof(sb->s_id));
328 strcpy(sb->s_id, "exofs");
329 sb->s_blocksize = EXOFS_BLKSIZE;
330 sb->s_blocksize_bits = EXOFS_BLKSHIFT;
331 sb->s_maxbytes = MAX_LFS_FILESIZE;
332 atomic_set(&sbi->s_curr_pending, 0);
336 /* read data from on-disk superblock object */
337 obj.partition = sbi->s_pid;
338 obj.id = EXOFS_SUPER_ID;
339 exofs_make_credential(sbi->s_cred, &obj);
341 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
345 "exofs_fill_super: osd_start_request failed.\n");
349 ret = osd_req_read_kern(or, &obj, 0, &fscb, sizeof(fscb));
353 "exofs_fill_super: osd_req_read_kern failed.\n");
358 ret = exofs_sync_op(or, sbi->s_timeout, sbi->s_cred);
361 EXOFS_ERR("exofs_fill_super: exofs_sync_op failed.\n");
366 sb->s_magic = le16_to_cpu(fscb.s_magic);
367 sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
368 sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
370 /* make sure what we read from the object store is correct */
371 if (sb->s_magic != EXOFS_SUPER_MAGIC) {
373 EXOFS_ERR("ERROR: Bad magic value\n");
378 /* start generation numbers from a random point */
379 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
380 spin_lock_init(&sbi->s_next_gen_lock);
382 /* set up operation vectors */
383 sb->s_op = &exofs_sops;
384 sb->s_export_op = &exofs_export_ops;
385 root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
387 EXOFS_ERR("ERROR: exofs_iget failed\n");
391 sb->s_root = d_alloc_root(root);
394 EXOFS_ERR("ERROR: get root inode failed\n");
399 if (!S_ISDIR(root->i_mode)) {
402 EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
408 _exofs_print_device("Mounting", opts->dev_name, sbi->s_dev, sbi->s_pid);
416 osduld_put_device(sbi->s_dev); /* NULL safe */
422 * Set up the superblock (calls exofs_fill_super eventually)
424 static int exofs_get_sb(struct file_system_type *type,
425 int flags, const char *dev_name,
426 void *data, struct vfsmount *mnt)
428 struct exofs_mountopt opts;
431 ret = parse_options(data, &opts);
435 opts.dev_name = dev_name;
436 return get_sb_nodev(type, flags, &opts, exofs_fill_super, mnt);
440 * Return information about the file system state in the buffer. This is used
441 * by the 'df' command, for example.
443 static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
445 struct super_block *sb = dentry->d_sb;
446 struct exofs_sb_info *sbi = sb->s_fs_info;
447 struct osd_obj_id obj = {sbi->s_pid, 0};
448 struct osd_attr attrs[] = {
449 ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
450 OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
451 ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
452 OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
454 uint64_t capacity = ULLONG_MAX;
455 uint64_t used = ULLONG_MAX;
456 struct osd_request *or;
457 uint8_t cred_a[OSD_CAP_LEN];
460 /* get used/capacity attributes */
461 exofs_make_credential(cred_a, &obj);
463 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
465 EXOFS_DBGMSG("exofs_statfs: osd_start_request failed.\n");
469 osd_req_get_attributes(or, &obj);
470 osd_req_add_get_attr_list(or, attrs, ARRAY_SIZE(attrs));
471 ret = exofs_sync_op(or, sbi->s_timeout, cred_a);
475 ret = extract_attr_from_req(or, &attrs[0]);
477 capacity = get_unaligned_be64(attrs[0].val_ptr);
479 EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
481 ret = extract_attr_from_req(or, &attrs[1]);
483 used = get_unaligned_be64(attrs[1].val_ptr);
485 EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
487 /* fill in the stats buffer */
488 buf->f_type = EXOFS_SUPER_MAGIC;
489 buf->f_bsize = EXOFS_BLKSIZE;
490 buf->f_blocks = (capacity >> EXOFS_BLKSHIFT);
491 buf->f_bfree = ((capacity - used) >> EXOFS_BLKSHIFT);
492 buf->f_bavail = buf->f_bfree;
493 buf->f_files = sbi->s_numfiles;
494 buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
495 buf->f_namelen = EXOFS_NAME_LEN;
502 static const struct super_operations exofs_sops = {
503 .alloc_inode = exofs_alloc_inode,
504 .destroy_inode = exofs_destroy_inode,
505 .write_inode = exofs_write_inode,
506 .delete_inode = exofs_delete_inode,
507 .put_super = exofs_put_super,
508 .write_super = exofs_write_super,
509 .sync_fs = exofs_sync_fs,
510 .statfs = exofs_statfs,
513 /******************************************************************************
515 *****************************************************************************/
517 struct dentry *exofs_get_parent(struct dentry *child)
519 unsigned long ino = exofs_parent_ino(child);
524 return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
527 static struct inode *exofs_nfs_get_inode(struct super_block *sb,
528 u64 ino, u32 generation)
532 inode = exofs_iget(sb, ino);
534 return ERR_CAST(inode);
535 if (generation && inode->i_generation != generation) {
536 /* we didn't find the right inode.. */
538 return ERR_PTR(-ESTALE);
543 static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
544 struct fid *fid, int fh_len, int fh_type)
546 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
547 exofs_nfs_get_inode);
550 static struct dentry *exofs_fh_to_parent(struct super_block *sb,
551 struct fid *fid, int fh_len, int fh_type)
553 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
554 exofs_nfs_get_inode);
557 static const struct export_operations exofs_export_ops = {
558 .fh_to_dentry = exofs_fh_to_dentry,
559 .fh_to_parent = exofs_fh_to_parent,
560 .get_parent = exofs_get_parent,
563 /******************************************************************************
565 *****************************************************************************/
568 * struct that describes this file system
570 static struct file_system_type exofs_type = {
571 .owner = THIS_MODULE,
573 .get_sb = exofs_get_sb,
574 .kill_sb = generic_shutdown_super,
577 static int __init init_exofs(void)
581 err = init_inodecache();
585 err = register_filesystem(&exofs_type);
591 destroy_inodecache();
596 static void __exit exit_exofs(void)
598 unregister_filesystem(&exofs_type);
599 destroy_inodecache();
602 MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
603 MODULE_DESCRIPTION("exofs");
604 MODULE_LICENSE("GPL");
606 module_init(init_exofs)
607 module_exit(exit_exofs)