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