exofs: RAID0 support
[safe/jmp/linux-2.6] / fs / exofs / super.c
1 /*
2  * Copyright (C) 2005, 2006
3  * Avishay Traeger (avishay@gmail.com)
4  * 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
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>
40
41 #include "exofs.h"
42
43 /******************************************************************************
44  * MOUNT OPTIONS
45  *****************************************************************************/
46
47 /*
48  * struct to hold what we get from mount options
49  */
50 struct exofs_mountopt {
51         const char *dev_name;
52         uint64_t pid;
53         int timeout;
54 };
55
56 /*
57  * exofs-specific mount-time options.
58  */
59 enum { 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  */
66 static 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  */
76 static 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  */
136 static struct kmem_cache *exofs_inode_cachep;
137
138 /*
139  * Allocate an inode in the cache
140  */
141 static 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  */
156 static 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  */
164 static 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  */
174 static 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  */
188 static void destroy_inodecache(void)
189 {
190         kmem_cache_destroy(exofs_inode_cachep);
191 }
192
193 /******************************************************************************
194  * SUPERBLOCK FUNCTIONS
195  *****************************************************************************/
196 static const struct super_operations exofs_sops;
197 static const struct export_operations exofs_export_ops;
198
199 /*
200  * Write the superblock to the OSD
201  */
202 int exofs_sync_fs(struct super_block *sb, int wait)
203 {
204         struct exofs_sb_info *sbi;
205         struct exofs_fscb *fscb;
206         struct exofs_io_state *ios;
207         int ret = -ENOMEM;
208
209         lock_super(sb);
210         sbi = sb->s_fs_info;
211         fscb = &sbi->s_fscb;
212
213         ret = exofs_get_io_state(&sbi->layout, &ios);
214         if (ret)
215                 goto out;
216
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
219          *       here.
220          */
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);
226         fscb->s_newfs = 0;
227         fscb->s_version = EXOFS_FSCB_VER;
228
229         ios->obj.id = EXOFS_SUPER_ID;
230         ios->offset = 0;
231         ios->kern_buff = fscb;
232         ios->cred = sbi->s_cred;
233
234         ret = exofs_sbi_write(ios);
235         if (unlikely(ret)) {
236                 EXOFS_ERR("%s: exofs_sbi_write failed.\n", __func__);
237                 goto out;
238         }
239         sb->s_dirt = 0;
240
241 out:
242         EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
243         exofs_put_io_state(ios);
244         unlock_super(sb);
245         return ret;
246 }
247
248 static void exofs_write_super(struct super_block *sb)
249 {
250         if (!(sb->s_flags & MS_RDONLY))
251                 exofs_sync_fs(sb, 1);
252         else
253                 sb->s_dirt = 0;
254 }
255
256 static void _exofs_print_device(const char *msg, const char *dev_path,
257                                 struct osd_dev *od, u64 pid)
258 {
259         const struct osd_dev_info *odi = osduld_device_info(od);
260
261         printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
262                 msg, dev_path ?: "", odi->osdname, _LLU(pid));
263 }
264
265 void exofs_free_sbi(struct exofs_sb_info *sbi)
266 {
267         while (sbi->layout.s_numdevs) {
268                 int i = --sbi->layout.s_numdevs;
269                 struct osd_dev *od = sbi->layout.s_ods[i];
270
271                 if (od) {
272                         sbi->layout.s_ods[i] = NULL;
273                         osduld_put_device(od);
274                 }
275         }
276         kfree(sbi);
277 }
278
279 /*
280  * This function is called when the vfs is freeing the superblock.  We just
281  * need to free our own part.
282  */
283 static void exofs_put_super(struct super_block *sb)
284 {
285         int num_pend;
286         struct exofs_sb_info *sbi = sb->s_fs_info;
287
288         if (sb->s_dirt)
289                 exofs_write_super(sb);
290
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));
299         }
300
301         _exofs_print_device("Unmounting", NULL, sbi->layout.s_ods[0],
302                             sbi->layout.s_pid);
303
304         exofs_free_sbi(sbi);
305         sb->s_fs_info = NULL;
306 }
307
308 static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
309                                     struct exofs_device_table *dt)
310 {
311         u64 stripe_length;
312
313         sbi->data_map.odm_num_comps   =
314                                 le32_to_cpu(dt->dt_data_map.cb_num_comps);
315         sbi->data_map.odm_stripe_unit =
316                                 le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
317         sbi->data_map.odm_group_width =
318                                 le32_to_cpu(dt->dt_data_map.cb_group_width);
319         sbi->data_map.odm_group_depth =
320                                 le32_to_cpu(dt->dt_data_map.cb_group_depth);
321         sbi->data_map.odm_mirror_cnt  =
322                                 le32_to_cpu(dt->dt_data_map.cb_mirror_cnt);
323         sbi->data_map.odm_raid_algorithm  =
324                                 le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);
325
326 /* FIXME: Only raid0 !group_width/depth for now. if not so, do not mount */
327         if (sbi->data_map.odm_group_width || sbi->data_map.odm_group_depth) {
328                 EXOFS_ERR("Group width/depth not supported\n");
329                 return -EINVAL;
330         }
331         if (sbi->data_map.odm_num_comps != numdevs) {
332                 EXOFS_ERR("odm_num_comps(%u) != numdevs(%u)\n",
333                           sbi->data_map.odm_num_comps, numdevs);
334                 return -EINVAL;
335         }
336         if (sbi->data_map.odm_raid_algorithm != PNFS_OSD_RAID_0) {
337                 EXOFS_ERR("Only RAID_0 for now\n");
338                 return -EINVAL;
339         }
340         if (0 != (numdevs % (sbi->data_map.odm_mirror_cnt + 1))) {
341                 EXOFS_ERR("Data Map wrong, numdevs=%d mirrors=%d\n",
342                           numdevs, sbi->data_map.odm_mirror_cnt);
343                 return -EINVAL;
344         }
345
346         stripe_length = sbi->data_map.odm_stripe_unit *
347                         (numdevs / (sbi->data_map.odm_mirror_cnt + 1));
348         if (stripe_length >= (1ULL << 32)) {
349                 EXOFS_ERR("Total Stripe length(0x%llx)"
350                           " >= 32bit is not supported\n", _LLU(stripe_length));
351                 return -EINVAL;
352         }
353
354         if (0 != (sbi->data_map.odm_stripe_unit & ~PAGE_MASK)) {
355                 EXOFS_ERR("Stripe Unit(0x%llx)"
356                           " must be Multples of PAGE_SIZE(0x%lx)\n",
357                           _LLU(sbi->data_map.odm_stripe_unit), PAGE_SIZE);
358                 return -EINVAL;
359         }
360
361         sbi->layout.stripe_unit = sbi->data_map.odm_stripe_unit;
362         sbi->layout.mirrors_p1 = sbi->data_map.odm_mirror_cnt + 1;
363         sbi->layout.group_width = sbi->data_map.odm_num_comps /
364                                                         sbi->layout.mirrors_p1;
365
366         return 0;
367 }
368
369 /* @odi is valid only as long as @fscb_dev is valid */
370 static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
371                              struct osd_dev_info *odi)
372 {
373         odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
374         memcpy(odi->systemid, dt_dev->systemid, odi->systemid_len);
375
376         odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
377         odi->osdname = dt_dev->osdname;
378
379         /* FIXME support long names. Will need a _put function */
380         if (dt_dev->long_name_offset)
381                 return -EINVAL;
382
383         /* Make sure osdname is printable!
384          * mkexofs should give us space for a null-terminator else the
385          * device-table is invalid.
386          */
387         if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
388                 odi->osdname_len = sizeof(dt_dev->osdname) - 1;
389         dt_dev->osdname[odi->osdname_len] = 0;
390
391         /* If it's all zeros something is bad we read past end-of-obj */
392         return !(odi->systemid_len || odi->osdname_len);
393 }
394
395 static int exofs_read_lookup_dev_table(struct exofs_sb_info **psbi,
396                                        unsigned table_count)
397 {
398         struct exofs_sb_info *sbi = *psbi;
399         struct osd_dev *fscb_od;
400         struct osd_obj_id obj = {.partition = sbi->layout.s_pid,
401                                  .id = EXOFS_DEVTABLE_ID};
402         struct exofs_device_table *dt;
403         unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
404                                              sizeof(*dt);
405         unsigned numdevs, i;
406         int ret;
407
408         dt = kmalloc(table_bytes, GFP_KERNEL);
409         if (unlikely(!dt)) {
410                 EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
411                           table_bytes);
412                 return -ENOMEM;
413         }
414
415         fscb_od = sbi->layout.s_ods[0];
416         sbi->layout.s_ods[0] = NULL;
417         sbi->layout.s_numdevs = 0;
418         ret = exofs_read_kern(fscb_od, sbi->s_cred, &obj, 0, dt, table_bytes);
419         if (unlikely(ret)) {
420                 EXOFS_ERR("ERROR: reading device table\n");
421                 goto out;
422         }
423
424         numdevs = le64_to_cpu(dt->dt_num_devices);
425         if (unlikely(!numdevs)) {
426                 ret = -EINVAL;
427                 goto out;
428         }
429         WARN_ON(table_count != numdevs);
430
431         ret = _read_and_match_data_map(sbi, numdevs, dt);
432         if (unlikely(ret))
433                 goto out;
434
435         if (likely(numdevs > 1)) {
436                 unsigned size = numdevs * sizeof(sbi->layout.s_ods[0]);
437
438                 sbi = krealloc(sbi, sizeof(*sbi) + size, GFP_KERNEL);
439                 if (unlikely(!sbi)) {
440                         ret = -ENOMEM;
441                         goto out;
442                 }
443                 memset(&sbi->layout.s_ods[1], 0,
444                        size - sizeof(sbi->layout.s_ods[0]));
445                 *psbi = sbi;
446         }
447
448         for (i = 0; i < numdevs; i++) {
449                 struct exofs_fscb fscb;
450                 struct osd_dev_info odi;
451                 struct osd_dev *od;
452
453                 if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
454                         EXOFS_ERR("ERROR: Read all-zeros device entry\n");
455                         ret = -EINVAL;
456                         goto out;
457                 }
458
459                 printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
460                        i, odi.osdname);
461
462                 /* On all devices the device table is identical. The user can
463                  * specify any one of the participating devices on the command
464                  * line. We always keep them in device-table order.
465                  */
466                 if (fscb_od && osduld_device_same(fscb_od, &odi)) {
467                         sbi->layout.s_ods[i] = fscb_od;
468                         ++sbi->layout.s_numdevs;
469                         fscb_od = NULL;
470                         continue;
471                 }
472
473                 od = osduld_info_lookup(&odi);
474                 if (unlikely(IS_ERR(od))) {
475                         ret = PTR_ERR(od);
476                         EXOFS_ERR("ERROR: device requested is not found "
477                                   "osd_name-%s =>%d\n", odi.osdname, ret);
478                         goto out;
479                 }
480
481                 sbi->layout.s_ods[i] = od;
482                 ++sbi->layout.s_numdevs;
483
484                 /* Read the fscb of the other devices to make sure the FS
485                  * partition is there.
486                  */
487                 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb,
488                                       sizeof(fscb));
489                 if (unlikely(ret)) {
490                         EXOFS_ERR("ERROR: Malformed participating device "
491                                   "error reading fscb osd_name-%s\n",
492                                   odi.osdname);
493                         goto out;
494                 }
495
496                 /* TODO: verify other information is correct and FS-uuid
497                  *       matches. Benny what did you say about device table
498                  *       generation and old devices?
499                  */
500         }
501
502 out:
503         kfree(dt);
504         if (unlikely(!ret && fscb_od)) {
505                 EXOFS_ERR(
506                       "ERROR: Bad device-table container device not present\n");
507                 osduld_put_device(fscb_od);
508                 ret = -EINVAL;
509         }
510
511         return ret;
512 }
513
514 /*
515  * Read the superblock from the OSD and fill in the fields
516  */
517 static int exofs_fill_super(struct super_block *sb, void *data, int silent)
518 {
519         struct inode *root;
520         struct exofs_mountopt *opts = data;
521         struct exofs_sb_info *sbi;      /*extended info                  */
522         struct osd_dev *od;             /* Master device                 */
523         struct exofs_fscb fscb;         /*on-disk superblock info        */
524         struct osd_obj_id obj;
525         unsigned table_count;
526         int ret;
527
528         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
529         if (!sbi)
530                 return -ENOMEM;
531
532         /* use mount options to fill superblock */
533         od = osduld_path_lookup(opts->dev_name);
534         if (IS_ERR(od)) {
535                 ret = PTR_ERR(od);
536                 goto free_sbi;
537         }
538
539         /* Default layout in case we do not have a device-table */
540         sbi->layout.stripe_unit = PAGE_SIZE;
541         sbi->layout.mirrors_p1 = 1;
542         sbi->layout.group_width = 1;
543         sbi->layout.s_ods[0] = od;
544         sbi->layout.s_numdevs = 1;
545         sbi->layout.s_pid = opts->pid;
546         sbi->s_timeout = opts->timeout;
547
548         /* fill in some other data by hand */
549         memset(sb->s_id, 0, sizeof(sb->s_id));
550         strcpy(sb->s_id, "exofs");
551         sb->s_blocksize = EXOFS_BLKSIZE;
552         sb->s_blocksize_bits = EXOFS_BLKSHIFT;
553         sb->s_maxbytes = MAX_LFS_FILESIZE;
554         atomic_set(&sbi->s_curr_pending, 0);
555         sb->s_bdev = NULL;
556         sb->s_dev = 0;
557
558         obj.partition = sbi->layout.s_pid;
559         obj.id = EXOFS_SUPER_ID;
560         exofs_make_credential(sbi->s_cred, &obj);
561
562         ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb, sizeof(fscb));
563         if (unlikely(ret))
564                 goto free_sbi;
565
566         sb->s_magic = le16_to_cpu(fscb.s_magic);
567         sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
568         sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
569
570         /* make sure what we read from the object store is correct */
571         if (sb->s_magic != EXOFS_SUPER_MAGIC) {
572                 if (!silent)
573                         EXOFS_ERR("ERROR: Bad magic value\n");
574                 ret = -EINVAL;
575                 goto free_sbi;
576         }
577         if (le32_to_cpu(fscb.s_version) != EXOFS_FSCB_VER) {
578                 EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
579                           EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
580                 ret = -EINVAL;
581                 goto free_sbi;
582         }
583
584         /* start generation numbers from a random point */
585         get_random_bytes(&sbi->s_next_generation, sizeof(u32));
586         spin_lock_init(&sbi->s_next_gen_lock);
587
588         table_count = le64_to_cpu(fscb.s_dev_table_count);
589         if (table_count) {
590                 ret = exofs_read_lookup_dev_table(&sbi, table_count);
591                 if (unlikely(ret))
592                         goto free_sbi;
593         }
594
595         /* set up operation vectors */
596         sb->s_fs_info = sbi;
597         sb->s_op = &exofs_sops;
598         sb->s_export_op = &exofs_export_ops;
599         root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
600         if (IS_ERR(root)) {
601                 EXOFS_ERR("ERROR: exofs_iget failed\n");
602                 ret = PTR_ERR(root);
603                 goto free_sbi;
604         }
605         sb->s_root = d_alloc_root(root);
606         if (!sb->s_root) {
607                 iput(root);
608                 EXOFS_ERR("ERROR: get root inode failed\n");
609                 ret = -ENOMEM;
610                 goto free_sbi;
611         }
612
613         if (!S_ISDIR(root->i_mode)) {
614                 dput(sb->s_root);
615                 sb->s_root = NULL;
616                 EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
617                        root->i_mode);
618                 ret = -EINVAL;
619                 goto free_sbi;
620         }
621
622         _exofs_print_device("Mounting", opts->dev_name, sbi->layout.s_ods[0],
623                             sbi->layout.s_pid);
624         return 0;
625
626 free_sbi:
627         EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
628                   opts->dev_name, sbi->layout.s_pid, ret);
629         exofs_free_sbi(sbi);
630         return ret;
631 }
632
633 /*
634  * Set up the superblock (calls exofs_fill_super eventually)
635  */
636 static int exofs_get_sb(struct file_system_type *type,
637                           int flags, const char *dev_name,
638                           void *data, struct vfsmount *mnt)
639 {
640         struct exofs_mountopt opts;
641         int ret;
642
643         ret = parse_options(data, &opts);
644         if (ret)
645                 return ret;
646
647         opts.dev_name = dev_name;
648         return get_sb_nodev(type, flags, &opts, exofs_fill_super, mnt);
649 }
650
651 /*
652  * Return information about the file system state in the buffer.  This is used
653  * by the 'df' command, for example.
654  */
655 static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
656 {
657         struct super_block *sb = dentry->d_sb;
658         struct exofs_sb_info *sbi = sb->s_fs_info;
659         struct exofs_io_state *ios;
660         struct osd_attr attrs[] = {
661                 ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
662                         OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
663                 ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
664                         OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
665         };
666         uint64_t capacity = ULLONG_MAX;
667         uint64_t used = ULLONG_MAX;
668         uint8_t cred_a[OSD_CAP_LEN];
669         int ret;
670
671         ret = exofs_get_io_state(&sbi->layout, &ios);
672         if (ret) {
673                 EXOFS_DBGMSG("exofs_get_io_state failed.\n");
674                 return ret;
675         }
676
677         exofs_make_credential(cred_a, &ios->obj);
678         ios->cred = sbi->s_cred;
679         ios->in_attr = attrs;
680         ios->in_attr_len = ARRAY_SIZE(attrs);
681
682         ret = exofs_sbi_read(ios);
683         if (unlikely(ret))
684                 goto out;
685
686         ret = extract_attr_from_ios(ios, &attrs[0]);
687         if (likely(!ret)) {
688                 capacity = get_unaligned_be64(attrs[0].val_ptr);
689                 if (unlikely(!capacity))
690                         capacity = ULLONG_MAX;
691         } else
692                 EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
693
694         ret = extract_attr_from_ios(ios, &attrs[1]);
695         if (likely(!ret))
696                 used = get_unaligned_be64(attrs[1].val_ptr);
697         else
698                 EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
699
700         /* fill in the stats buffer */
701         buf->f_type = EXOFS_SUPER_MAGIC;
702         buf->f_bsize = EXOFS_BLKSIZE;
703         buf->f_blocks = capacity >> 9;
704         buf->f_bfree = (capacity - used) >> 9;
705         buf->f_bavail = buf->f_bfree;
706         buf->f_files = sbi->s_numfiles;
707         buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
708         buf->f_namelen = EXOFS_NAME_LEN;
709
710 out:
711         exofs_put_io_state(ios);
712         return ret;
713 }
714
715 static const struct super_operations exofs_sops = {
716         .alloc_inode    = exofs_alloc_inode,
717         .destroy_inode  = exofs_destroy_inode,
718         .write_inode    = exofs_write_inode,
719         .delete_inode   = exofs_delete_inode,
720         .put_super      = exofs_put_super,
721         .write_super    = exofs_write_super,
722         .sync_fs        = exofs_sync_fs,
723         .statfs         = exofs_statfs,
724 };
725
726 /******************************************************************************
727  * EXPORT OPERATIONS
728  *****************************************************************************/
729
730 struct dentry *exofs_get_parent(struct dentry *child)
731 {
732         unsigned long ino = exofs_parent_ino(child);
733
734         if (!ino)
735                 return NULL;
736
737         return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
738 }
739
740 static struct inode *exofs_nfs_get_inode(struct super_block *sb,
741                 u64 ino, u32 generation)
742 {
743         struct inode *inode;
744
745         inode = exofs_iget(sb, ino);
746         if (IS_ERR(inode))
747                 return ERR_CAST(inode);
748         if (generation && inode->i_generation != generation) {
749                 /* we didn't find the right inode.. */
750                 iput(inode);
751                 return ERR_PTR(-ESTALE);
752         }
753         return inode;
754 }
755
756 static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
757                                 struct fid *fid, int fh_len, int fh_type)
758 {
759         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
760                                     exofs_nfs_get_inode);
761 }
762
763 static struct dentry *exofs_fh_to_parent(struct super_block *sb,
764                                 struct fid *fid, int fh_len, int fh_type)
765 {
766         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
767                                     exofs_nfs_get_inode);
768 }
769
770 static const struct export_operations exofs_export_ops = {
771         .fh_to_dentry = exofs_fh_to_dentry,
772         .fh_to_parent = exofs_fh_to_parent,
773         .get_parent = exofs_get_parent,
774 };
775
776 /******************************************************************************
777  * INSMOD/RMMOD
778  *****************************************************************************/
779
780 /*
781  * struct that describes this file system
782  */
783 static struct file_system_type exofs_type = {
784         .owner          = THIS_MODULE,
785         .name           = "exofs",
786         .get_sb         = exofs_get_sb,
787         .kill_sb        = generic_shutdown_super,
788 };
789
790 static int __init init_exofs(void)
791 {
792         int err;
793
794         err = init_inodecache();
795         if (err)
796                 goto out;
797
798         err = register_filesystem(&exofs_type);
799         if (err)
800                 goto out_d;
801
802         return 0;
803 out_d:
804         destroy_inodecache();
805 out:
806         return err;
807 }
808
809 static void __exit exit_exofs(void)
810 {
811         unregister_filesystem(&exofs_type);
812         destroy_inodecache();
813 }
814
815 MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
816 MODULE_DESCRIPTION("exofs");
817 MODULE_LICENSE("GPL");
818
819 module_init(init_exofs)
820 module_exit(exit_exofs)