474989eeb7d6486208179c8ce1849651102903f0
[linux-2.6.git] / 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/string.h>
35 #include <linux/parser.h>
36 #include <linux/vfs.h>
37 #include <linux/random.h>
38 #include <linux/exportfs.h>
39 #include <linux/slab.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         bool is_osdname;
52         const char *dev_name;
53         uint64_t pid;
54         int timeout;
55 };
56
57 /*
58  * exofs-specific mount-time options.
59  */
60 enum { Opt_name, Opt_pid, Opt_to, 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  */
67 static match_table_t tokens = {
68         {Opt_name, "osdname=%s"},
69         {Opt_pid, "pid=%u"},
70         {Opt_to, "to=%u"},
71         {Opt_err, NULL}
72 };
73
74 /*
75  * The main option parsing method.  Also makes sure that all of the mandatory
76  * mount options were set.
77  */
78 static int parse_options(char *options, struct exofs_mountopt *opts)
79 {
80         char *p;
81         substring_t args[MAX_OPT_ARGS];
82         int option;
83         bool s_pid = false;
84
85         EXOFS_DBGMSG("parse_options %s\n", options);
86         /* defaults */
87         memset(opts, 0, sizeof(*opts));
88         opts->timeout = BLK_DEFAULT_SG_TIMEOUT;
89
90         while ((p = strsep(&options, ",")) != NULL) {
91                 int token;
92                 char str[32];
93
94                 if (!*p)
95                         continue;
96
97                 token = match_token(p, tokens, args);
98                 switch (token) {
99                 case Opt_name:
100                         opts->dev_name = match_strdup(&args[0]);
101                         if (unlikely(!opts->dev_name)) {
102                                 EXOFS_ERR("Error allocating dev_name");
103                                 return -ENOMEM;
104                         }
105                         opts->is_osdname = true;
106                         break;
107                 case Opt_pid:
108                         if (0 == match_strlcpy(str, &args[0], sizeof(str)))
109                                 return -EINVAL;
110                         opts->pid = simple_strtoull(str, NULL, 0);
111                         if (opts->pid < EXOFS_MIN_PID) {
112                                 EXOFS_ERR("Partition ID must be >= %u",
113                                           EXOFS_MIN_PID);
114                                 return -EINVAL;
115                         }
116                         s_pid = 1;
117                         break;
118                 case Opt_to:
119                         if (match_int(&args[0], &option))
120                                 return -EINVAL;
121                         if (option <= 0) {
122                                 EXOFS_ERR("Timout must be > 0");
123                                 return -EINVAL;
124                         }
125                         opts->timeout = option * HZ;
126                         break;
127                 }
128         }
129
130         if (!s_pid) {
131                 EXOFS_ERR("Need to specify the following options:\n");
132                 EXOFS_ERR("    -o pid=pid_no_to_use\n");
133                 return -EINVAL;
134         }
135
136         return 0;
137 }
138
139 /******************************************************************************
140  * INODE CACHE
141  *****************************************************************************/
142
143 /*
144  * Our inode cache.  Isn't it pretty?
145  */
146 static struct kmem_cache *exofs_inode_cachep;
147
148 /*
149  * Allocate an inode in the cache
150  */
151 static struct inode *exofs_alloc_inode(struct super_block *sb)
152 {
153         struct exofs_i_info *oi;
154
155         oi = kmem_cache_alloc(exofs_inode_cachep, GFP_KERNEL);
156         if (!oi)
157                 return NULL;
158
159         oi->vfs_inode.i_version = 1;
160         return &oi->vfs_inode;
161 }
162
163 static void exofs_i_callback(struct rcu_head *head)
164 {
165         struct inode *inode = container_of(head, struct inode, i_rcu);
166         INIT_LIST_HEAD(&inode->i_dentry);
167         kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
168 }
169
170 /*
171  * Remove an inode from the cache
172  */
173 static void exofs_destroy_inode(struct inode *inode)
174 {
175         call_rcu(&inode->i_rcu, exofs_i_callback);
176 }
177
178 /*
179  * Initialize the inode
180  */
181 static void exofs_init_once(void *foo)
182 {
183         struct exofs_i_info *oi = foo;
184
185         inode_init_once(&oi->vfs_inode);
186 }
187
188 /*
189  * Create and initialize the inode cache
190  */
191 static int init_inodecache(void)
192 {
193         exofs_inode_cachep = kmem_cache_create("exofs_inode_cache",
194                                 sizeof(struct exofs_i_info), 0,
195                                 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
196                                 exofs_init_once);
197         if (exofs_inode_cachep == NULL)
198                 return -ENOMEM;
199         return 0;
200 }
201
202 /*
203  * Destroy the inode cache
204  */
205 static void destroy_inodecache(void)
206 {
207         kmem_cache_destroy(exofs_inode_cachep);
208 }
209
210 /******************************************************************************
211  * SUPERBLOCK FUNCTIONS
212  *****************************************************************************/
213 static const struct super_operations exofs_sops;
214 static const struct export_operations exofs_export_ops;
215
216 /*
217  * Write the superblock to the OSD
218  */
219 int exofs_sync_fs(struct super_block *sb, int wait)
220 {
221         struct exofs_sb_info *sbi;
222         struct exofs_fscb *fscb;
223         struct exofs_io_state *ios;
224         int ret = -ENOMEM;
225
226         lock_super(sb);
227         sbi = sb->s_fs_info;
228         fscb = &sbi->s_fscb;
229
230         ret = exofs_get_io_state(&sbi->layout, &ios);
231         if (ret)
232                 goto out;
233
234         /* Note: We only write the changing part of the fscb. .i.e upto the
235          *       the fscb->s_dev_table_oid member. There is no read-modify-write
236          *       here.
237          */
238         ios->length = offsetof(struct exofs_fscb, s_dev_table_oid);
239         memset(fscb, 0, ios->length);
240         fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
241         fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
242         fscb->s_magic = cpu_to_le16(sb->s_magic);
243         fscb->s_newfs = 0;
244         fscb->s_version = EXOFS_FSCB_VER;
245
246         ios->obj.id = EXOFS_SUPER_ID;
247         ios->offset = 0;
248         ios->kern_buff = fscb;
249         ios->cred = sbi->s_cred;
250
251         ret = exofs_sbi_write(ios);
252         if (unlikely(ret)) {
253                 EXOFS_ERR("%s: exofs_sbi_write failed.\n", __func__);
254                 goto out;
255         }
256         sb->s_dirt = 0;
257
258 out:
259         EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
260         exofs_put_io_state(ios);
261         unlock_super(sb);
262         return ret;
263 }
264
265 static void exofs_write_super(struct super_block *sb)
266 {
267         if (!(sb->s_flags & MS_RDONLY))
268                 exofs_sync_fs(sb, 1);
269         else
270                 sb->s_dirt = 0;
271 }
272
273 static void _exofs_print_device(const char *msg, const char *dev_path,
274                                 struct osd_dev *od, u64 pid)
275 {
276         const struct osd_dev_info *odi = osduld_device_info(od);
277
278         printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
279                 msg, dev_path ?: "", odi->osdname, _LLU(pid));
280 }
281
282 void exofs_free_sbi(struct exofs_sb_info *sbi)
283 {
284         while (sbi->layout.s_numdevs) {
285                 int i = --sbi->layout.s_numdevs;
286                 struct osd_dev *od = sbi->layout.s_ods[i];
287
288                 if (od) {
289                         sbi->layout.s_ods[i] = NULL;
290                         osduld_put_device(od);
291                 }
292         }
293         kfree(sbi);
294 }
295
296 /*
297  * This function is called when the vfs is freeing the superblock.  We just
298  * need to free our own part.
299  */
300 static void exofs_put_super(struct super_block *sb)
301 {
302         int num_pend;
303         struct exofs_sb_info *sbi = sb->s_fs_info;
304
305         if (sb->s_dirt)
306                 exofs_write_super(sb);
307
308         /* make sure there are no pending commands */
309         for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
310              num_pend = atomic_read(&sbi->s_curr_pending)) {
311                 wait_queue_head_t wq;
312                 init_waitqueue_head(&wq);
313                 wait_event_timeout(wq,
314                                   (atomic_read(&sbi->s_curr_pending) == 0),
315                                   msecs_to_jiffies(100));
316         }
317
318         _exofs_print_device("Unmounting", NULL, sbi->layout.s_ods[0],
319                             sbi->layout.s_pid);
320
321         bdi_destroy(&sbi->bdi);
322         exofs_free_sbi(sbi);
323         sb->s_fs_info = NULL;
324 }
325
326 static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
327                                     struct exofs_device_table *dt)
328 {
329         u64 stripe_length;
330
331         sbi->data_map.odm_num_comps   =
332                                 le32_to_cpu(dt->dt_data_map.cb_num_comps);
333         sbi->data_map.odm_stripe_unit =
334                                 le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
335         sbi->data_map.odm_group_width =
336                                 le32_to_cpu(dt->dt_data_map.cb_group_width);
337         sbi->data_map.odm_group_depth =
338                                 le32_to_cpu(dt->dt_data_map.cb_group_depth);
339         sbi->data_map.odm_mirror_cnt  =
340                                 le32_to_cpu(dt->dt_data_map.cb_mirror_cnt);
341         sbi->data_map.odm_raid_algorithm  =
342                                 le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);
343
344 /* FIXME: Only raid0 for now. if not so, do not mount */
345         if (sbi->data_map.odm_num_comps != numdevs) {
346                 EXOFS_ERR("odm_num_comps(%u) != numdevs(%u)\n",
347                           sbi->data_map.odm_num_comps, numdevs);
348                 return -EINVAL;
349         }
350         if (sbi->data_map.odm_raid_algorithm != PNFS_OSD_RAID_0) {
351                 EXOFS_ERR("Only RAID_0 for now\n");
352                 return -EINVAL;
353         }
354         if (0 != (numdevs % (sbi->data_map.odm_mirror_cnt + 1))) {
355                 EXOFS_ERR("Data Map wrong, numdevs=%d mirrors=%d\n",
356                           numdevs, sbi->data_map.odm_mirror_cnt);
357                 return -EINVAL;
358         }
359
360         if (0 != (sbi->data_map.odm_stripe_unit & ~PAGE_MASK)) {
361                 EXOFS_ERR("Stripe Unit(0x%llx)"
362                           " must be Multples of PAGE_SIZE(0x%lx)\n",
363                           _LLU(sbi->data_map.odm_stripe_unit), PAGE_SIZE);
364                 return -EINVAL;
365         }
366
367         sbi->layout.stripe_unit = sbi->data_map.odm_stripe_unit;
368         sbi->layout.mirrors_p1 = sbi->data_map.odm_mirror_cnt + 1;
369
370         if (sbi->data_map.odm_group_width) {
371                 sbi->layout.group_width = sbi->data_map.odm_group_width;
372                 sbi->layout.group_depth = sbi->data_map.odm_group_depth;
373                 if (!sbi->layout.group_depth) {
374                         EXOFS_ERR("group_depth == 0 && group_width != 0\n");
375                         return -EINVAL;
376                 }
377                 sbi->layout.group_count = sbi->data_map.odm_num_comps /
378                                                 sbi->layout.mirrors_p1 /
379                                                 sbi->data_map.odm_group_width;
380         } else {
381                 if (sbi->data_map.odm_group_depth) {
382                         printk(KERN_NOTICE "Warning: group_depth ignored "
383                                 "group_width == 0 && group_depth == %d\n",
384                                 sbi->data_map.odm_group_depth);
385                         sbi->data_map.odm_group_depth = 0;
386                 }
387                 sbi->layout.group_width = sbi->data_map.odm_num_comps /
388                                                         sbi->layout.mirrors_p1;
389                 sbi->layout.group_depth = -1;
390                 sbi->layout.group_count = 1;
391         }
392
393         stripe_length = (u64)sbi->layout.group_width * sbi->layout.stripe_unit;
394         if (stripe_length >= (1ULL << 32)) {
395                 EXOFS_ERR("Total Stripe length(0x%llx)"
396                           " >= 32bit is not supported\n", _LLU(stripe_length));
397                 return -EINVAL;
398         }
399
400         return 0;
401 }
402
403 static unsigned __ra_pages(struct exofs_layout *layout)
404 {
405         const unsigned _MIN_RA = 32; /* min 128K read-ahead */
406         unsigned ra_pages = layout->group_width * layout->stripe_unit /
407                                 PAGE_SIZE;
408         unsigned max_io_pages = exofs_max_io_pages(layout, ~0);
409
410         ra_pages *= 2; /* two stripes */
411         if (ra_pages < _MIN_RA)
412                 ra_pages = roundup(_MIN_RA, ra_pages / 2);
413
414         if (ra_pages > max_io_pages)
415                 ra_pages = max_io_pages;
416
417         return ra_pages;
418 }
419
420 /* @odi is valid only as long as @fscb_dev is valid */
421 static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
422                              struct osd_dev_info *odi)
423 {
424         odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
425         memcpy(odi->systemid, dt_dev->systemid, odi->systemid_len);
426
427         odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
428         odi->osdname = dt_dev->osdname;
429
430         /* FIXME support long names. Will need a _put function */
431         if (dt_dev->long_name_offset)
432                 return -EINVAL;
433
434         /* Make sure osdname is printable!
435          * mkexofs should give us space for a null-terminator else the
436          * device-table is invalid.
437          */
438         if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
439                 odi->osdname_len = sizeof(dt_dev->osdname) - 1;
440         dt_dev->osdname[odi->osdname_len] = 0;
441
442         /* If it's all zeros something is bad we read past end-of-obj */
443         return !(odi->systemid_len || odi->osdname_len);
444 }
445
446 static int exofs_read_lookup_dev_table(struct exofs_sb_info **psbi,
447                                        unsigned table_count)
448 {
449         struct exofs_sb_info *sbi = *psbi;
450         struct osd_dev *fscb_od;
451         struct osd_obj_id obj = {.partition = sbi->layout.s_pid,
452                                  .id = EXOFS_DEVTABLE_ID};
453         struct exofs_device_table *dt;
454         unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
455                                              sizeof(*dt);
456         unsigned numdevs, i;
457         int ret;
458
459         dt = kmalloc(table_bytes, GFP_KERNEL);
460         if (unlikely(!dt)) {
461                 EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
462                           table_bytes);
463                 return -ENOMEM;
464         }
465
466         fscb_od = sbi->layout.s_ods[0];
467         sbi->layout.s_ods[0] = NULL;
468         sbi->layout.s_numdevs = 0;
469         ret = exofs_read_kern(fscb_od, sbi->s_cred, &obj, 0, dt, table_bytes);
470         if (unlikely(ret)) {
471                 EXOFS_ERR("ERROR: reading device table\n");
472                 goto out;
473         }
474
475         numdevs = le64_to_cpu(dt->dt_num_devices);
476         if (unlikely(!numdevs)) {
477                 ret = -EINVAL;
478                 goto out;
479         }
480         WARN_ON(table_count != numdevs);
481
482         ret = _read_and_match_data_map(sbi, numdevs, dt);
483         if (unlikely(ret))
484                 goto out;
485
486         if (likely(numdevs > 1)) {
487                 unsigned size = numdevs * sizeof(sbi->layout.s_ods[0]);
488
489                 sbi = krealloc(sbi, sizeof(*sbi) + size, GFP_KERNEL);
490                 if (unlikely(!sbi)) {
491                         ret = -ENOMEM;
492                         goto out;
493                 }
494                 memset(&sbi->layout.s_ods[1], 0,
495                        size - sizeof(sbi->layout.s_ods[0]));
496                 *psbi = sbi;
497         }
498
499         for (i = 0; i < numdevs; i++) {
500                 struct exofs_fscb fscb;
501                 struct osd_dev_info odi;
502                 struct osd_dev *od;
503
504                 if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
505                         EXOFS_ERR("ERROR: Read all-zeros device entry\n");
506                         ret = -EINVAL;
507                         goto out;
508                 }
509
510                 printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
511                        i, odi.osdname);
512
513                 /* On all devices the device table is identical. The user can
514                  * specify any one of the participating devices on the command
515                  * line. We always keep them in device-table order.
516                  */
517                 if (fscb_od && osduld_device_same(fscb_od, &odi)) {
518                         sbi->layout.s_ods[i] = fscb_od;
519                         ++sbi->layout.s_numdevs;
520                         fscb_od = NULL;
521                         continue;
522                 }
523
524                 od = osduld_info_lookup(&odi);
525                 if (IS_ERR(od)) {
526                         ret = PTR_ERR(od);
527                         EXOFS_ERR("ERROR: device requested is not found "
528                                   "osd_name-%s =>%d\n", odi.osdname, ret);
529                         goto out;
530                 }
531
532                 sbi->layout.s_ods[i] = od;
533                 ++sbi->layout.s_numdevs;
534
535                 /* Read the fscb of the other devices to make sure the FS
536                  * partition is there.
537                  */
538                 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb,
539                                       sizeof(fscb));
540                 if (unlikely(ret)) {
541                         EXOFS_ERR("ERROR: Malformed participating device "
542                                   "error reading fscb osd_name-%s\n",
543                                   odi.osdname);
544                         goto out;
545                 }
546
547                 /* TODO: verify other information is correct and FS-uuid
548                  *       matches. Benny what did you say about device table
549                  *       generation and old devices?
550                  */
551         }
552
553 out:
554         kfree(dt);
555         if (unlikely(!ret && fscb_od)) {
556                 EXOFS_ERR(
557                       "ERROR: Bad device-table container device not present\n");
558                 osduld_put_device(fscb_od);
559                 ret = -EINVAL;
560         }
561
562         return ret;
563 }
564
565 /*
566  * Read the superblock from the OSD and fill in the fields
567  */
568 static int exofs_fill_super(struct super_block *sb, void *data, int silent)
569 {
570         struct inode *root;
571         struct exofs_mountopt *opts = data;
572         struct exofs_sb_info *sbi;      /*extended info                  */
573         struct osd_dev *od;             /* Master device                 */
574         struct exofs_fscb fscb;         /*on-disk superblock info        */
575         struct osd_obj_id obj;
576         unsigned table_count;
577         int ret;
578
579         sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
580         if (!sbi)
581                 return -ENOMEM;
582
583         ret = bdi_setup_and_register(&sbi->bdi, "exofs", BDI_CAP_MAP_COPY);
584         if (ret)
585                 goto free_bdi;
586
587         /* use mount options to fill superblock */
588         if (opts->is_osdname) {
589                 struct osd_dev_info odi = {.systemid_len = 0};
590
591                 odi.osdname_len = strlen(opts->dev_name);
592                 odi.osdname = (u8 *)opts->dev_name;
593                 od = osduld_info_lookup(&odi);
594         } else {
595                 od = osduld_path_lookup(opts->dev_name);
596         }
597         if (IS_ERR(od)) {
598                 ret = -EINVAL;
599                 goto free_sbi;
600         }
601
602         /* Default layout in case we do not have a device-table */
603         sbi->layout.stripe_unit = PAGE_SIZE;
604         sbi->layout.mirrors_p1 = 1;
605         sbi->layout.group_width = 1;
606         sbi->layout.group_depth = -1;
607         sbi->layout.group_count = 1;
608         sbi->layout.s_ods[0] = od;
609         sbi->layout.s_numdevs = 1;
610         sbi->layout.s_pid = opts->pid;
611         sbi->s_timeout = opts->timeout;
612
613         /* fill in some other data by hand */
614         memset(sb->s_id, 0, sizeof(sb->s_id));
615         strcpy(sb->s_id, "exofs");
616         sb->s_blocksize = EXOFS_BLKSIZE;
617         sb->s_blocksize_bits = EXOFS_BLKSHIFT;
618         sb->s_maxbytes = MAX_LFS_FILESIZE;
619         atomic_set(&sbi->s_curr_pending, 0);
620         sb->s_bdev = NULL;
621         sb->s_dev = 0;
622
623         obj.partition = sbi->layout.s_pid;
624         obj.id = EXOFS_SUPER_ID;
625         exofs_make_credential(sbi->s_cred, &obj);
626
627         ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb, sizeof(fscb));
628         if (unlikely(ret))
629                 goto free_sbi;
630
631         sb->s_magic = le16_to_cpu(fscb.s_magic);
632         sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
633         sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
634
635         /* make sure what we read from the object store is correct */
636         if (sb->s_magic != EXOFS_SUPER_MAGIC) {
637                 if (!silent)
638                         EXOFS_ERR("ERROR: Bad magic value\n");
639                 ret = -EINVAL;
640                 goto free_sbi;
641         }
642         if (le32_to_cpu(fscb.s_version) != EXOFS_FSCB_VER) {
643                 EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
644                           EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
645                 ret = -EINVAL;
646                 goto free_sbi;
647         }
648
649         /* start generation numbers from a random point */
650         get_random_bytes(&sbi->s_next_generation, sizeof(u32));
651         spin_lock_init(&sbi->s_next_gen_lock);
652
653         table_count = le64_to_cpu(fscb.s_dev_table_count);
654         if (table_count) {
655                 ret = exofs_read_lookup_dev_table(&sbi, table_count);
656                 if (unlikely(ret))
657                         goto free_sbi;
658         }
659
660         /* set up operation vectors */
661         sbi->bdi.ra_pages = __ra_pages(&sbi->layout);
662         sb->s_bdi = &sbi->bdi;
663         sb->s_fs_info = sbi;
664         sb->s_op = &exofs_sops;
665         sb->s_export_op = &exofs_export_ops;
666         root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
667         if (IS_ERR(root)) {
668                 EXOFS_ERR("ERROR: exofs_iget failed\n");
669                 ret = PTR_ERR(root);
670                 goto free_sbi;
671         }
672         sb->s_root = d_alloc_root(root);
673         if (!sb->s_root) {
674                 iput(root);
675                 EXOFS_ERR("ERROR: get root inode failed\n");
676                 ret = -ENOMEM;
677                 goto free_sbi;
678         }
679
680         if (!S_ISDIR(root->i_mode)) {
681                 dput(sb->s_root);
682                 sb->s_root = NULL;
683                 EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
684                        root->i_mode);
685                 ret = -EINVAL;
686                 goto free_sbi;
687         }
688
689         _exofs_print_device("Mounting", opts->dev_name, sbi->layout.s_ods[0],
690                             sbi->layout.s_pid);
691         if (opts->is_osdname)
692                 kfree(opts->dev_name);
693         return 0;
694
695 free_sbi:
696         bdi_destroy(&sbi->bdi);
697 free_bdi:
698         EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
699                   opts->dev_name, sbi->layout.s_pid, ret);
700         exofs_free_sbi(sbi);
701         if (opts->is_osdname)
702                 kfree(opts->dev_name);
703         return ret;
704 }
705
706 /*
707  * Set up the superblock (calls exofs_fill_super eventually)
708  */
709 static struct dentry *exofs_mount(struct file_system_type *type,
710                           int flags, const char *dev_name,
711                           void *data)
712 {
713         struct exofs_mountopt opts;
714         int ret;
715
716         ret = parse_options(data, &opts);
717         if (ret)
718                 return ERR_PTR(ret);
719
720         if (!opts.dev_name)
721                 opts.dev_name = dev_name;
722         return mount_nodev(type, flags, &opts, exofs_fill_super);
723 }
724
725 /*
726  * Return information about the file system state in the buffer.  This is used
727  * by the 'df' command, for example.
728  */
729 static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
730 {
731         struct super_block *sb = dentry->d_sb;
732         struct exofs_sb_info *sbi = sb->s_fs_info;
733         struct exofs_io_state *ios;
734         struct osd_attr attrs[] = {
735                 ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
736                         OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
737                 ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
738                         OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
739         };
740         uint64_t capacity = ULLONG_MAX;
741         uint64_t used = ULLONG_MAX;
742         uint8_t cred_a[OSD_CAP_LEN];
743         int ret;
744
745         ret = exofs_get_io_state(&sbi->layout, &ios);
746         if (ret) {
747                 EXOFS_DBGMSG("exofs_get_io_state failed.\n");
748                 return ret;
749         }
750
751         exofs_make_credential(cred_a, &ios->obj);
752         ios->cred = sbi->s_cred;
753         ios->in_attr = attrs;
754         ios->in_attr_len = ARRAY_SIZE(attrs);
755
756         ret = exofs_sbi_read(ios);
757         if (unlikely(ret))
758                 goto out;
759
760         ret = extract_attr_from_ios(ios, &attrs[0]);
761         if (likely(!ret)) {
762                 capacity = get_unaligned_be64(attrs[0].val_ptr);
763                 if (unlikely(!capacity))
764                         capacity = ULLONG_MAX;
765         } else
766                 EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
767
768         ret = extract_attr_from_ios(ios, &attrs[1]);
769         if (likely(!ret))
770                 used = get_unaligned_be64(attrs[1].val_ptr);
771         else
772                 EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
773
774         /* fill in the stats buffer */
775         buf->f_type = EXOFS_SUPER_MAGIC;
776         buf->f_bsize = EXOFS_BLKSIZE;
777         buf->f_blocks = capacity >> 9;
778         buf->f_bfree = (capacity - used) >> 9;
779         buf->f_bavail = buf->f_bfree;
780         buf->f_files = sbi->s_numfiles;
781         buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
782         buf->f_namelen = EXOFS_NAME_LEN;
783
784 out:
785         exofs_put_io_state(ios);
786         return ret;
787 }
788
789 static const struct super_operations exofs_sops = {
790         .alloc_inode    = exofs_alloc_inode,
791         .destroy_inode  = exofs_destroy_inode,
792         .write_inode    = exofs_write_inode,
793         .evict_inode    = exofs_evict_inode,
794         .put_super      = exofs_put_super,
795         .write_super    = exofs_write_super,
796         .sync_fs        = exofs_sync_fs,
797         .statfs         = exofs_statfs,
798 };
799
800 /******************************************************************************
801  * EXPORT OPERATIONS
802  *****************************************************************************/
803
804 struct dentry *exofs_get_parent(struct dentry *child)
805 {
806         unsigned long ino = exofs_parent_ino(child);
807
808         if (!ino)
809                 return NULL;
810
811         return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
812 }
813
814 static struct inode *exofs_nfs_get_inode(struct super_block *sb,
815                 u64 ino, u32 generation)
816 {
817         struct inode *inode;
818
819         inode = exofs_iget(sb, ino);
820         if (IS_ERR(inode))
821                 return ERR_CAST(inode);
822         if (generation && inode->i_generation != generation) {
823                 /* we didn't find the right inode.. */
824                 iput(inode);
825                 return ERR_PTR(-ESTALE);
826         }
827         return inode;
828 }
829
830 static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
831                                 struct fid *fid, int fh_len, int fh_type)
832 {
833         return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
834                                     exofs_nfs_get_inode);
835 }
836
837 static struct dentry *exofs_fh_to_parent(struct super_block *sb,
838                                 struct fid *fid, int fh_len, int fh_type)
839 {
840         return generic_fh_to_parent(sb, fid, fh_len, fh_type,
841                                     exofs_nfs_get_inode);
842 }
843
844 static const struct export_operations exofs_export_ops = {
845         .fh_to_dentry = exofs_fh_to_dentry,
846         .fh_to_parent = exofs_fh_to_parent,
847         .get_parent = exofs_get_parent,
848 };
849
850 /******************************************************************************
851  * INSMOD/RMMOD
852  *****************************************************************************/
853
854 /*
855  * struct that describes this file system
856  */
857 static struct file_system_type exofs_type = {
858         .owner          = THIS_MODULE,
859         .name           = "exofs",
860         .mount          = exofs_mount,
861         .kill_sb        = generic_shutdown_super,
862 };
863
864 static int __init init_exofs(void)
865 {
866         int err;
867
868         err = init_inodecache();
869         if (err)
870                 goto out;
871
872         err = register_filesystem(&exofs_type);
873         if (err)
874                 goto out_d;
875
876         return 0;
877 out_d:
878         destroy_inodecache();
879 out:
880         return err;
881 }
882
883 static void __exit exit_exofs(void)
884 {
885         unregister_filesystem(&exofs_type);
886         destroy_inodecache();
887 }
888
889 MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
890 MODULE_DESCRIPTION("exofs");
891 MODULE_LICENSE("GPL");
892
893 module_init(init_exofs)
894 module_exit(exit_exofs)