2 * Copyright (C) 2005, 2006
3 * Avishay Traeger (avishay@gmail.com) (avishay@il.ibm.com)
4 * Copyright (C) 2005, 2006
5 * International Business Machines
6 * Copyright (C) 2008, 2009
7 * Boaz Harrosh <bharrosh@panasas.com>
9 * Copyrights for code taken from ext2:
10 * Copyright (C) 1992, 1993, 1994, 1995
11 * Remy Card (card@masi.ibp.fr)
12 * Laboratoire MASI - Institut Blaise Pascal
13 * Universite Pierre et Marie Curie (Paris VI)
15 * linux/fs/minix/inode.c
16 * Copyright (C) 1991, 1992 Linus Torvalds
18 * This file is part of exofs.
20 * exofs is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation. Since it is based on ext2, and the only
23 * valid version of GPL for the Linux kernel is version 2, the only valid
24 * version of GPL for exofs is version 2.
26 * exofs is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
31 * You should have received a copy of the GNU General Public License
32 * along with exofs; if not, write to the Free Software
33 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
36 #include <linux/string.h>
37 #include <linux/parser.h>
38 #include <linux/vfs.h>
39 #include <linux/random.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;
199 * Write the superblock to the OSD
201 static void exofs_write_super(struct super_block *sb)
203 struct exofs_sb_info *sbi;
204 struct exofs_fscb *fscb;
205 struct osd_request *or;
206 struct osd_obj_id obj;
209 fscb = kzalloc(sizeof(struct exofs_fscb), GFP_KERNEL);
211 EXOFS_ERR("exofs_write_super: memory allocation failed.\n");
217 fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
218 fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
219 fscb->s_magic = cpu_to_le16(sb->s_magic);
222 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
224 EXOFS_ERR("exofs_write_super: osd_start_request failed.\n");
228 obj.partition = sbi->s_pid;
229 obj.id = EXOFS_SUPER_ID;
230 ret = osd_req_write_kern(or, &obj, 0, fscb, sizeof(*fscb));
232 EXOFS_ERR("exofs_write_super: osd_req_write_kern failed.\n");
236 ret = exofs_sync_op(or, sbi->s_timeout, sbi->s_cred);
238 EXOFS_ERR("exofs_write_super: exofs_sync_op failed.\n");
251 * This function is called when the vfs is freeing the superblock. We just
252 * need to free our own part.
254 static void exofs_put_super(struct super_block *sb)
257 struct exofs_sb_info *sbi = sb->s_fs_info;
259 /* make sure there are no pending commands */
260 for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
261 num_pend = atomic_read(&sbi->s_curr_pending)) {
262 wait_queue_head_t wq;
263 init_waitqueue_head(&wq);
264 wait_event_timeout(wq,
265 (atomic_read(&sbi->s_curr_pending) == 0),
266 msecs_to_jiffies(100));
269 osduld_put_device(sbi->s_dev);
270 kfree(sb->s_fs_info);
271 sb->s_fs_info = NULL;
275 * Read the superblock from the OSD and fill in the fields
277 static int exofs_fill_super(struct super_block *sb, void *data, int silent)
280 struct exofs_mountopt *opts = data;
281 struct exofs_sb_info *sbi; /*extended info */
282 struct exofs_fscb fscb; /*on-disk superblock info */
283 struct osd_request *or = NULL;
284 struct osd_obj_id obj;
287 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
292 /* use mount options to fill superblock */
293 sbi->s_dev = osduld_path_lookup(opts->dev_name);
294 if (IS_ERR(sbi->s_dev)) {
295 ret = PTR_ERR(sbi->s_dev);
300 sbi->s_pid = opts->pid;
301 sbi->s_timeout = opts->timeout;
303 /* fill in some other data by hand */
304 memset(sb->s_id, 0, sizeof(sb->s_id));
305 strcpy(sb->s_id, "exofs");
306 sb->s_blocksize = EXOFS_BLKSIZE;
307 sb->s_blocksize_bits = EXOFS_BLKSHIFT;
308 sb->s_maxbytes = MAX_LFS_FILESIZE;
309 atomic_set(&sbi->s_curr_pending, 0);
313 /* read data from on-disk superblock object */
314 obj.partition = sbi->s_pid;
315 obj.id = EXOFS_SUPER_ID;
316 exofs_make_credential(sbi->s_cred, &obj);
318 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
322 "exofs_fill_super: osd_start_request failed.\n");
326 ret = osd_req_read_kern(or, &obj, 0, &fscb, sizeof(fscb));
330 "exofs_fill_super: osd_req_read_kern failed.\n");
335 ret = exofs_sync_op(or, sbi->s_timeout, sbi->s_cred);
338 EXOFS_ERR("exofs_fill_super: exofs_sync_op failed.\n");
343 sb->s_magic = le16_to_cpu(fscb.s_magic);
344 sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
345 sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
347 /* make sure what we read from the object store is correct */
348 if (sb->s_magic != EXOFS_SUPER_MAGIC) {
350 EXOFS_ERR("ERROR: Bad magic value\n");
355 /* start generation numbers from a random point */
356 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
357 spin_lock_init(&sbi->s_next_gen_lock);
359 /* set up operation vectors */
360 sb->s_op = &exofs_sops;
361 root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
363 EXOFS_ERR("ERROR: exofs_iget failed\n");
367 sb->s_root = d_alloc_root(root);
370 EXOFS_ERR("ERROR: get root inode failed\n");
375 if (!S_ISDIR(root->i_mode)) {
378 EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
391 osduld_put_device(sbi->s_dev); /* NULL safe */
397 * Set up the superblock (calls exofs_fill_super eventually)
399 static int exofs_get_sb(struct file_system_type *type,
400 int flags, const char *dev_name,
401 void *data, struct vfsmount *mnt)
403 struct exofs_mountopt opts;
406 ret = parse_options(data, &opts);
410 opts.dev_name = dev_name;
411 return get_sb_nodev(type, flags, &opts, exofs_fill_super, mnt);
415 * Return information about the file system state in the buffer. This is used
416 * by the 'df' command, for example.
418 static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
420 struct super_block *sb = dentry->d_sb;
421 struct exofs_sb_info *sbi = sb->s_fs_info;
422 struct osd_obj_id obj = {sbi->s_pid, 0};
423 struct osd_attr attrs[] = {
424 ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
425 OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
426 ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
427 OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
429 uint64_t capacity = ULLONG_MAX;
430 uint64_t used = ULLONG_MAX;
431 struct osd_request *or;
432 uint8_t cred_a[OSD_CAP_LEN];
435 /* get used/capacity attributes */
436 exofs_make_credential(cred_a, &obj);
438 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
440 EXOFS_DBGMSG("exofs_statfs: osd_start_request failed.\n");
444 osd_req_get_attributes(or, &obj);
445 osd_req_add_get_attr_list(or, attrs, ARRAY_SIZE(attrs));
446 ret = exofs_sync_op(or, sbi->s_timeout, cred_a);
450 ret = extract_attr_from_req(or, &attrs[0]);
452 capacity = get_unaligned_be64(attrs[0].val_ptr);
454 EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
456 ret = extract_attr_from_req(or, &attrs[1]);
458 used = get_unaligned_be64(attrs[1].val_ptr);
460 EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
462 /* fill in the stats buffer */
463 buf->f_type = EXOFS_SUPER_MAGIC;
464 buf->f_bsize = EXOFS_BLKSIZE;
465 buf->f_blocks = (capacity >> EXOFS_BLKSHIFT);
466 buf->f_bfree = ((capacity - used) >> EXOFS_BLKSHIFT);
467 buf->f_bavail = buf->f_bfree;
468 buf->f_files = sbi->s_numfiles;
469 buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
470 buf->f_namelen = EXOFS_NAME_LEN;
477 static const struct super_operations exofs_sops = {
478 .alloc_inode = exofs_alloc_inode,
479 .destroy_inode = exofs_destroy_inode,
480 .write_inode = exofs_write_inode,
481 .delete_inode = exofs_delete_inode,
482 .put_super = exofs_put_super,
483 .write_super = exofs_write_super,
484 .statfs = exofs_statfs,
487 /******************************************************************************
489 *****************************************************************************/
492 * struct that describes this file system
494 static struct file_system_type exofs_type = {
495 .owner = THIS_MODULE,
497 .get_sb = exofs_get_sb,
498 .kill_sb = generic_shutdown_super,
501 static int __init init_exofs(void)
505 err = init_inodecache();
509 err = register_filesystem(&exofs_type);
515 destroy_inodecache();
520 static void __exit exit_exofs(void)
522 unregister_filesystem(&exofs_type);
523 destroy_inodecache();
526 MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
527 MODULE_DESCRIPTION("exofs");
528 MODULE_LICENSE("GPL");
530 module_init(init_exofs)
531 module_exit(exit_exofs)