blob: 8216c5b77b5325db78970efc711ab3729a1ea46e [file] [log] [blame]
Boaz Harroshba9e5e92008-10-28 16:11:41 +02001/*
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>
8 *
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)
14 * from
15 * linux/fs/minix/inode.c
16 * Copyright (C) 1991, 1992 Linus Torvalds
17 *
18 * This file is part of exofs.
19 *
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.
25 *
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.
30 *
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
34 */
35
36#include <linux/string.h>
37#include <linux/parser.h>
38#include <linux/vfs.h>
39#include <linux/random.h>
Boaz Harrosh8cf74b32009-03-22 12:47:26 +020040#include <linux/exportfs.h>
Boaz Harroshba9e5e92008-10-28 16:11:41 +020041
42#include "exofs.h"
43
44/******************************************************************************
45 * MOUNT OPTIONS
46 *****************************************************************************/
47
48/*
49 * struct to hold what we get from mount options
50 */
51struct exofs_mountopt {
52 const char *dev_name;
53 uint64_t pid;
54 int timeout;
55};
56
57/*
58 * exofs-specific mount-time options.
59 */
60enum { Opt_pid, Opt_to, Opt_mkfs, Opt_format, Opt_err };
61
62/*
63 * Our mount-time options. These should ideally be 64-bit unsigned, but the
64 * kernel's parsing functions do not currently support that. 32-bit should be
65 * sufficient for most applications now.
66 */
67static match_table_t tokens = {
68 {Opt_pid, "pid=%u"},
69 {Opt_to, "to=%u"},
70 {Opt_err, NULL}
71};
72
73/*
74 * The main option parsing method. Also makes sure that all of the mandatory
75 * mount options were set.
76 */
77static int parse_options(char *options, struct exofs_mountopt *opts)
78{
79 char *p;
80 substring_t args[MAX_OPT_ARGS];
81 int option;
82 bool s_pid = false;
83
84 EXOFS_DBGMSG("parse_options %s\n", options);
85 /* defaults */
86 memset(opts, 0, sizeof(*opts));
87 opts->timeout = BLK_DEFAULT_SG_TIMEOUT;
88
89 while ((p = strsep(&options, ",")) != NULL) {
90 int token;
91 char str[32];
92
93 if (!*p)
94 continue;
95
96 token = match_token(p, tokens, args);
97 switch (token) {
98 case Opt_pid:
99 if (0 == match_strlcpy(str, &args[0], sizeof(str)))
100 return -EINVAL;
101 opts->pid = simple_strtoull(str, NULL, 0);
102 if (opts->pid < EXOFS_MIN_PID) {
103 EXOFS_ERR("Partition ID must be >= %u",
104 EXOFS_MIN_PID);
105 return -EINVAL;
106 }
107 s_pid = 1;
108 break;
109 case Opt_to:
110 if (match_int(&args[0], &option))
111 return -EINVAL;
112 if (option <= 0) {
113 EXOFS_ERR("Timout must be > 0");
114 return -EINVAL;
115 }
116 opts->timeout = option * HZ;
117 break;
118 }
119 }
120
121 if (!s_pid) {
122 EXOFS_ERR("Need to specify the following options:\n");
123 EXOFS_ERR(" -o pid=pid_no_to_use\n");
124 return -EINVAL;
125 }
126
127 return 0;
128}
129
130/******************************************************************************
131 * INODE CACHE
132 *****************************************************************************/
133
134/*
135 * Our inode cache. Isn't it pretty?
136 */
137static struct kmem_cache *exofs_inode_cachep;
138
139/*
140 * Allocate an inode in the cache
141 */
142static struct inode *exofs_alloc_inode(struct super_block *sb)
143{
144 struct exofs_i_info *oi;
145
146 oi = kmem_cache_alloc(exofs_inode_cachep, GFP_KERNEL);
147 if (!oi)
148 return NULL;
149
150 oi->vfs_inode.i_version = 1;
151 return &oi->vfs_inode;
152}
153
154/*
155 * Remove an inode from the cache
156 */
157static void exofs_destroy_inode(struct inode *inode)
158{
159 kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
160}
161
162/*
163 * Initialize the inode
164 */
165static void exofs_init_once(void *foo)
166{
167 struct exofs_i_info *oi = foo;
168
169 inode_init_once(&oi->vfs_inode);
170}
171
172/*
173 * Create and initialize the inode cache
174 */
175static int init_inodecache(void)
176{
177 exofs_inode_cachep = kmem_cache_create("exofs_inode_cache",
178 sizeof(struct exofs_i_info), 0,
179 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
180 exofs_init_once);
181 if (exofs_inode_cachep == NULL)
182 return -ENOMEM;
183 return 0;
184}
185
186/*
187 * Destroy the inode cache
188 */
189static void destroy_inodecache(void)
190{
191 kmem_cache_destroy(exofs_inode_cachep);
192}
193
194/******************************************************************************
195 * SUPERBLOCK FUNCTIONS
196 *****************************************************************************/
197static const struct super_operations exofs_sops;
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200198static const struct export_operations exofs_export_ops;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200199
200/*
201 * Write the superblock to the OSD
202 */
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200203static int exofs_sync_fs(struct super_block *sb, int wait)
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200204{
205 struct exofs_sb_info *sbi;
206 struct exofs_fscb *fscb;
207 struct osd_request *or;
208 struct osd_obj_id obj;
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200209 int ret = -ENOMEM;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200210
211 fscb = kzalloc(sizeof(struct exofs_fscb), GFP_KERNEL);
212 if (!fscb) {
213 EXOFS_ERR("exofs_write_super: memory allocation failed.\n");
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200214 return -ENOMEM;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200215 }
216
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200217 lock_super(sb);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200218 lock_kernel();
219 sbi = sb->s_fs_info;
220 fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
221 fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
222 fscb->s_magic = cpu_to_le16(sb->s_magic);
223 fscb->s_newfs = 0;
224
225 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
226 if (unlikely(!or)) {
227 EXOFS_ERR("exofs_write_super: osd_start_request failed.\n");
228 goto out;
229 }
230
231 obj.partition = sbi->s_pid;
232 obj.id = EXOFS_SUPER_ID;
233 ret = osd_req_write_kern(or, &obj, 0, fscb, sizeof(*fscb));
234 if (unlikely(ret)) {
235 EXOFS_ERR("exofs_write_super: osd_req_write_kern failed.\n");
236 goto out;
237 }
238
239 ret = exofs_sync_op(or, sbi->s_timeout, sbi->s_cred);
240 if (unlikely(ret)) {
241 EXOFS_ERR("exofs_write_super: exofs_sync_op failed.\n");
242 goto out;
243 }
244 sb->s_dirt = 0;
245
246out:
247 if (or)
248 osd_end_request(or);
249 unlock_kernel();
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200250 unlock_super(sb);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200251 kfree(fscb);
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200252 return ret;
253}
254
255static void exofs_write_super(struct super_block *sb)
256{
257 if (!(sb->s_flags & MS_RDONLY))
258 exofs_sync_fs(sb, 1);
259 else
260 sb->s_dirt = 0;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200261}
262
263/*
264 * This function is called when the vfs is freeing the superblock. We just
265 * need to free our own part.
266 */
267static void exofs_put_super(struct super_block *sb)
268{
269 int num_pend;
270 struct exofs_sb_info *sbi = sb->s_fs_info;
271
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200272 lock_kernel();
273
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200274 if (sb->s_dirt)
275 exofs_write_super(sb);
276
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200277 /* make sure there are no pending commands */
278 for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
279 num_pend = atomic_read(&sbi->s_curr_pending)) {
280 wait_queue_head_t wq;
281 init_waitqueue_head(&wq);
282 wait_event_timeout(wq,
283 (atomic_read(&sbi->s_curr_pending) == 0),
284 msecs_to_jiffies(100));
285 }
286
287 osduld_put_device(sbi->s_dev);
288 kfree(sb->s_fs_info);
289 sb->s_fs_info = NULL;
Christoph Hellwig6cfd0142009-05-05 15:40:36 +0200290
291 unlock_kernel();
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200292}
293
294/*
295 * Read the superblock from the OSD and fill in the fields
296 */
297static int exofs_fill_super(struct super_block *sb, void *data, int silent)
298{
299 struct inode *root;
300 struct exofs_mountopt *opts = data;
301 struct exofs_sb_info *sbi; /*extended info */
302 struct exofs_fscb fscb; /*on-disk superblock info */
303 struct osd_request *or = NULL;
304 struct osd_obj_id obj;
305 int ret;
306
307 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
308 if (!sbi)
309 return -ENOMEM;
310 sb->s_fs_info = sbi;
311
312 /* use mount options to fill superblock */
313 sbi->s_dev = osduld_path_lookup(opts->dev_name);
314 if (IS_ERR(sbi->s_dev)) {
315 ret = PTR_ERR(sbi->s_dev);
316 sbi->s_dev = NULL;
317 goto free_sbi;
318 }
319
320 sbi->s_pid = opts->pid;
321 sbi->s_timeout = opts->timeout;
322
323 /* fill in some other data by hand */
324 memset(sb->s_id, 0, sizeof(sb->s_id));
325 strcpy(sb->s_id, "exofs");
326 sb->s_blocksize = EXOFS_BLKSIZE;
327 sb->s_blocksize_bits = EXOFS_BLKSHIFT;
328 sb->s_maxbytes = MAX_LFS_FILESIZE;
329 atomic_set(&sbi->s_curr_pending, 0);
330 sb->s_bdev = NULL;
331 sb->s_dev = 0;
332
333 /* read data from on-disk superblock object */
334 obj.partition = sbi->s_pid;
335 obj.id = EXOFS_SUPER_ID;
336 exofs_make_credential(sbi->s_cred, &obj);
337
338 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
339 if (unlikely(!or)) {
340 if (!silent)
341 EXOFS_ERR(
342 "exofs_fill_super: osd_start_request failed.\n");
343 ret = -ENOMEM;
344 goto free_sbi;
345 }
346 ret = osd_req_read_kern(or, &obj, 0, &fscb, sizeof(fscb));
347 if (unlikely(ret)) {
348 if (!silent)
349 EXOFS_ERR(
350 "exofs_fill_super: osd_req_read_kern failed.\n");
351 ret = -ENOMEM;
352 goto free_sbi;
353 }
354
355 ret = exofs_sync_op(or, sbi->s_timeout, sbi->s_cred);
356 if (unlikely(ret)) {
357 if (!silent)
358 EXOFS_ERR("exofs_fill_super: exofs_sync_op failed.\n");
359 ret = -EIO;
360 goto free_sbi;
361 }
362
363 sb->s_magic = le16_to_cpu(fscb.s_magic);
364 sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
365 sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
366
367 /* make sure what we read from the object store is correct */
368 if (sb->s_magic != EXOFS_SUPER_MAGIC) {
369 if (!silent)
370 EXOFS_ERR("ERROR: Bad magic value\n");
371 ret = -EINVAL;
372 goto free_sbi;
373 }
374
375 /* start generation numbers from a random point */
376 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
377 spin_lock_init(&sbi->s_next_gen_lock);
378
379 /* set up operation vectors */
380 sb->s_op = &exofs_sops;
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200381 sb->s_export_op = &exofs_export_ops;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200382 root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
383 if (IS_ERR(root)) {
384 EXOFS_ERR("ERROR: exofs_iget failed\n");
385 ret = PTR_ERR(root);
386 goto free_sbi;
387 }
388 sb->s_root = d_alloc_root(root);
389 if (!sb->s_root) {
390 iput(root);
391 EXOFS_ERR("ERROR: get root inode failed\n");
392 ret = -ENOMEM;
393 goto free_sbi;
394 }
395
396 if (!S_ISDIR(root->i_mode)) {
397 dput(sb->s_root);
398 sb->s_root = NULL;
399 EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
400 root->i_mode);
401 ret = -EINVAL;
402 goto free_sbi;
403 }
404
405 ret = 0;
406out:
407 if (or)
408 osd_end_request(or);
409 return ret;
410
411free_sbi:
412 osduld_put_device(sbi->s_dev); /* NULL safe */
413 kfree(sbi);
414 goto out;
415}
416
417/*
418 * Set up the superblock (calls exofs_fill_super eventually)
419 */
420static int exofs_get_sb(struct file_system_type *type,
421 int flags, const char *dev_name,
422 void *data, struct vfsmount *mnt)
423{
424 struct exofs_mountopt opts;
425 int ret;
426
427 ret = parse_options(data, &opts);
428 if (ret)
429 return ret;
430
431 opts.dev_name = dev_name;
432 return get_sb_nodev(type, flags, &opts, exofs_fill_super, mnt);
433}
434
435/*
436 * Return information about the file system state in the buffer. This is used
437 * by the 'df' command, for example.
438 */
439static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
440{
441 struct super_block *sb = dentry->d_sb;
442 struct exofs_sb_info *sbi = sb->s_fs_info;
443 struct osd_obj_id obj = {sbi->s_pid, 0};
444 struct osd_attr attrs[] = {
445 ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
446 OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
447 ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
448 OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
449 };
450 uint64_t capacity = ULLONG_MAX;
451 uint64_t used = ULLONG_MAX;
452 struct osd_request *or;
453 uint8_t cred_a[OSD_CAP_LEN];
454 int ret;
455
456 /* get used/capacity attributes */
457 exofs_make_credential(cred_a, &obj);
458
459 or = osd_start_request(sbi->s_dev, GFP_KERNEL);
460 if (unlikely(!or)) {
461 EXOFS_DBGMSG("exofs_statfs: osd_start_request failed.\n");
462 return -ENOMEM;
463 }
464
465 osd_req_get_attributes(or, &obj);
466 osd_req_add_get_attr_list(or, attrs, ARRAY_SIZE(attrs));
467 ret = exofs_sync_op(or, sbi->s_timeout, cred_a);
468 if (unlikely(ret))
469 goto out;
470
471 ret = extract_attr_from_req(or, &attrs[0]);
472 if (likely(!ret))
473 capacity = get_unaligned_be64(attrs[0].val_ptr);
474 else
475 EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
476
477 ret = extract_attr_from_req(or, &attrs[1]);
478 if (likely(!ret))
479 used = get_unaligned_be64(attrs[1].val_ptr);
480 else
481 EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
482
483 /* fill in the stats buffer */
484 buf->f_type = EXOFS_SUPER_MAGIC;
485 buf->f_bsize = EXOFS_BLKSIZE;
486 buf->f_blocks = (capacity >> EXOFS_BLKSHIFT);
487 buf->f_bfree = ((capacity - used) >> EXOFS_BLKSHIFT);
488 buf->f_bavail = buf->f_bfree;
489 buf->f_files = sbi->s_numfiles;
490 buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
491 buf->f_namelen = EXOFS_NAME_LEN;
492
493out:
494 osd_end_request(or);
495 return ret;
496}
497
498static const struct super_operations exofs_sops = {
499 .alloc_inode = exofs_alloc_inode,
500 .destroy_inode = exofs_destroy_inode,
501 .write_inode = exofs_write_inode,
502 .delete_inode = exofs_delete_inode,
503 .put_super = exofs_put_super,
504 .write_super = exofs_write_super,
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200505 .sync_fs = exofs_sync_fs,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200506 .statfs = exofs_statfs,
507};
508
509/******************************************************************************
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200510 * EXPORT OPERATIONS
511 *****************************************************************************/
512
513struct dentry *exofs_get_parent(struct dentry *child)
514{
515 unsigned long ino = exofs_parent_ino(child);
516
517 if (!ino)
518 return NULL;
519
520 return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
521}
522
523static struct inode *exofs_nfs_get_inode(struct super_block *sb,
524 u64 ino, u32 generation)
525{
526 struct inode *inode;
527
528 inode = exofs_iget(sb, ino);
529 if (IS_ERR(inode))
530 return ERR_CAST(inode);
531 if (generation && inode->i_generation != generation) {
532 /* we didn't find the right inode.. */
533 iput(inode);
534 return ERR_PTR(-ESTALE);
535 }
536 return inode;
537}
538
539static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
540 struct fid *fid, int fh_len, int fh_type)
541{
542 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
543 exofs_nfs_get_inode);
544}
545
546static struct dentry *exofs_fh_to_parent(struct super_block *sb,
547 struct fid *fid, int fh_len, int fh_type)
548{
549 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
550 exofs_nfs_get_inode);
551}
552
553static const struct export_operations exofs_export_ops = {
554 .fh_to_dentry = exofs_fh_to_dentry,
555 .fh_to_parent = exofs_fh_to_parent,
556 .get_parent = exofs_get_parent,
557};
558
559/******************************************************************************
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200560 * INSMOD/RMMOD
561 *****************************************************************************/
562
563/*
564 * struct that describes this file system
565 */
566static struct file_system_type exofs_type = {
567 .owner = THIS_MODULE,
568 .name = "exofs",
569 .get_sb = exofs_get_sb,
570 .kill_sb = generic_shutdown_super,
571};
572
573static int __init init_exofs(void)
574{
575 int err;
576
577 err = init_inodecache();
578 if (err)
579 goto out;
580
581 err = register_filesystem(&exofs_type);
582 if (err)
583 goto out_d;
584
585 return 0;
586out_d:
587 destroy_inodecache();
588out:
589 return err;
590}
591
592static void __exit exit_exofs(void)
593{
594 unregister_filesystem(&exofs_type);
595 destroy_inodecache();
596}
597
598MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
599MODULE_DESCRIPTION("exofs");
600MODULE_LICENSE("GPL");
601
602module_init(init_exofs)
603module_exit(exit_exofs)