blob: e87510f4749e4a06824cba1f9e8ca2e2edeedc4d [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
34#include <linux/string.h>
35#include <linux/parser.h>
36#include <linux/vfs.h>
37#include <linux/random.h>
Boaz Harrosh8cf74b32009-03-22 12:47:26 +020038#include <linux/exportfs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090039#include <linux/slab.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
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100153static void exofs_i_callback(struct rcu_head *head)
154{
155 struct inode *inode = container_of(head, struct inode, i_rcu);
156 INIT_LIST_HEAD(&inode->i_dentry);
157 kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
158}
159
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200160/*
161 * Remove an inode from the cache
162 */
163static void exofs_destroy_inode(struct inode *inode)
164{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100165 call_rcu(&inode->i_rcu, exofs_i_callback);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200166}
167
168/*
169 * Initialize the inode
170 */
171static void exofs_init_once(void *foo)
172{
173 struct exofs_i_info *oi = foo;
174
175 inode_init_once(&oi->vfs_inode);
176}
177
178/*
179 * Create and initialize the inode cache
180 */
181static int init_inodecache(void)
182{
183 exofs_inode_cachep = kmem_cache_create("exofs_inode_cache",
184 sizeof(struct exofs_i_info), 0,
185 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
186 exofs_init_once);
187 if (exofs_inode_cachep == NULL)
188 return -ENOMEM;
189 return 0;
190}
191
192/*
193 * Destroy the inode cache
194 */
195static void destroy_inodecache(void)
196{
197 kmem_cache_destroy(exofs_inode_cachep);
198}
199
200/******************************************************************************
201 * SUPERBLOCK FUNCTIONS
202 *****************************************************************************/
203static const struct super_operations exofs_sops;
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200204static const struct export_operations exofs_export_ops;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200205
206/*
207 * Write the superblock to the OSD
208 */
Boaz Harroshbaaf94c2009-06-14 16:52:10 +0300209int exofs_sync_fs(struct super_block *sb, int wait)
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200210{
211 struct exofs_sb_info *sbi;
212 struct exofs_fscb *fscb;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200213 struct exofs_io_state *ios;
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200214 int ret = -ENOMEM;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200215
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200216 lock_super(sb);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200217 sbi = sb->s_fs_info;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200218 fscb = &sbi->s_fscb;
219
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200220 ret = exofs_get_io_state(&sbi->layout, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200221 if (ret)
222 goto out;
223
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200224 /* Note: We only write the changing part of the fscb. .i.e upto the
225 * the fscb->s_dev_table_oid member. There is no read-modify-write
226 * here.
227 */
228 ios->length = offsetof(struct exofs_fscb, s_dev_table_oid);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200229 memset(fscb, 0, ios->length);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200230 fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
231 fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
232 fscb->s_magic = cpu_to_le16(sb->s_magic);
233 fscb->s_newfs = 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200234 fscb->s_version = EXOFS_FSCB_VER;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200235
Boaz Harrosh06886a52009-11-08 14:54:08 +0200236 ios->obj.id = EXOFS_SUPER_ID;
237 ios->offset = 0;
238 ios->kern_buff = fscb;
239 ios->cred = sbi->s_cred;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200240
Boaz Harrosh06886a52009-11-08 14:54:08 +0200241 ret = exofs_sbi_write(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200242 if (unlikely(ret)) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200243 EXOFS_ERR("%s: exofs_sbi_write failed.\n", __func__);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200244 goto out;
245 }
246 sb->s_dirt = 0;
247
248out:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200249 EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
250 exofs_put_io_state(ios);
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200251 unlock_super(sb);
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
Boaz Harrosh19fe2942009-09-03 20:38:02 +0300263static void _exofs_print_device(const char *msg, const char *dev_path,
264 struct osd_dev *od, u64 pid)
265{
266 const struct osd_dev_info *odi = osduld_device_info(od);
267
268 printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
269 msg, dev_path ?: "", odi->osdname, _LLU(pid));
270}
271
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200272void exofs_free_sbi(struct exofs_sb_info *sbi)
273{
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200274 while (sbi->layout.s_numdevs) {
275 int i = --sbi->layout.s_numdevs;
276 struct osd_dev *od = sbi->layout.s_ods[i];
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200277
278 if (od) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200279 sbi->layout.s_ods[i] = NULL;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200280 osduld_put_device(od);
281 }
282 }
283 kfree(sbi);
284}
285
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200286/*
287 * This function is called when the vfs is freeing the superblock. We just
288 * need to free our own part.
289 */
290static void exofs_put_super(struct super_block *sb)
291{
292 int num_pend;
293 struct exofs_sb_info *sbi = sb->s_fs_info;
294
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200295 if (sb->s_dirt)
296 exofs_write_super(sb);
297
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200298 /* make sure there are no pending commands */
299 for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
300 num_pend = atomic_read(&sbi->s_curr_pending)) {
301 wait_queue_head_t wq;
302 init_waitqueue_head(&wq);
303 wait_event_timeout(wq,
304 (atomic_read(&sbi->s_curr_pending) == 0),
305 msecs_to_jiffies(100));
306 }
307
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200308 _exofs_print_device("Unmounting", NULL, sbi->layout.s_ods[0],
309 sbi->layout.s_pid);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200310
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200311 bdi_destroy(&sbi->bdi);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200312 exofs_free_sbi(sbi);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200313 sb->s_fs_info = NULL;
314}
315
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200316static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
317 struct exofs_device_table *dt)
318{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200319 u64 stripe_length;
320
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200321 sbi->data_map.odm_num_comps =
322 le32_to_cpu(dt->dt_data_map.cb_num_comps);
323 sbi->data_map.odm_stripe_unit =
324 le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
325 sbi->data_map.odm_group_width =
326 le32_to_cpu(dt->dt_data_map.cb_group_width);
327 sbi->data_map.odm_group_depth =
328 le32_to_cpu(dt->dt_data_map.cb_group_depth);
329 sbi->data_map.odm_mirror_cnt =
330 le32_to_cpu(dt->dt_data_map.cb_mirror_cnt);
331 sbi->data_map.odm_raid_algorithm =
332 le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);
333
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200334/* FIXME: Only raid0 for now. if not so, do not mount */
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200335 if (sbi->data_map.odm_num_comps != numdevs) {
336 EXOFS_ERR("odm_num_comps(%u) != numdevs(%u)\n",
337 sbi->data_map.odm_num_comps, numdevs);
338 return -EINVAL;
339 }
340 if (sbi->data_map.odm_raid_algorithm != PNFS_OSD_RAID_0) {
341 EXOFS_ERR("Only RAID_0 for now\n");
342 return -EINVAL;
343 }
344 if (0 != (numdevs % (sbi->data_map.odm_mirror_cnt + 1))) {
345 EXOFS_ERR("Data Map wrong, numdevs=%d mirrors=%d\n",
346 numdevs, sbi->data_map.odm_mirror_cnt);
347 return -EINVAL;
348 }
349
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200350 if (0 != (sbi->data_map.odm_stripe_unit & ~PAGE_MASK)) {
351 EXOFS_ERR("Stripe Unit(0x%llx)"
352 " must be Multples of PAGE_SIZE(0x%lx)\n",
353 _LLU(sbi->data_map.odm_stripe_unit), PAGE_SIZE);
354 return -EINVAL;
355 }
356
357 sbi->layout.stripe_unit = sbi->data_map.odm_stripe_unit;
358 sbi->layout.mirrors_p1 = sbi->data_map.odm_mirror_cnt + 1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200359
360 if (sbi->data_map.odm_group_width) {
361 sbi->layout.group_width = sbi->data_map.odm_group_width;
362 sbi->layout.group_depth = sbi->data_map.odm_group_depth;
363 if (!sbi->layout.group_depth) {
364 EXOFS_ERR("group_depth == 0 && group_width != 0\n");
365 return -EINVAL;
366 }
367 sbi->layout.group_count = sbi->data_map.odm_num_comps /
368 sbi->layout.mirrors_p1 /
369 sbi->data_map.odm_group_width;
370 } else {
371 if (sbi->data_map.odm_group_depth) {
372 printk(KERN_NOTICE "Warning: group_depth ignored "
373 "group_width == 0 && group_depth == %d\n",
374 sbi->data_map.odm_group_depth);
375 sbi->data_map.odm_group_depth = 0;
376 }
377 sbi->layout.group_width = sbi->data_map.odm_num_comps /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200378 sbi->layout.mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200379 sbi->layout.group_depth = -1;
380 sbi->layout.group_count = 1;
381 }
382
383 stripe_length = (u64)sbi->layout.group_width * sbi->layout.stripe_unit;
384 if (stripe_length >= (1ULL << 32)) {
385 EXOFS_ERR("Total Stripe length(0x%llx)"
386 " >= 32bit is not supported\n", _LLU(stripe_length));
387 return -EINVAL;
388 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200389
390 return 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200391}
392
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -0400393static unsigned __ra_pages(struct exofs_layout *layout)
394{
395 const unsigned _MIN_RA = 32; /* min 128K read-ahead */
396 unsigned ra_pages = layout->group_width * layout->stripe_unit /
397 PAGE_SIZE;
398 unsigned max_io_pages = exofs_max_io_pages(layout, ~0);
399
400 ra_pages *= 2; /* two stripes */
401 if (ra_pages < _MIN_RA)
402 ra_pages = roundup(_MIN_RA, ra_pages / 2);
403
404 if (ra_pages > max_io_pages)
405 ra_pages = max_io_pages;
406
407 return ra_pages;
408}
409
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200410/* @odi is valid only as long as @fscb_dev is valid */
411static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
412 struct osd_dev_info *odi)
413{
414 odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
415 memcpy(odi->systemid, dt_dev->systemid, odi->systemid_len);
416
417 odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
418 odi->osdname = dt_dev->osdname;
419
420 /* FIXME support long names. Will need a _put function */
421 if (dt_dev->long_name_offset)
422 return -EINVAL;
423
424 /* Make sure osdname is printable!
425 * mkexofs should give us space for a null-terminator else the
426 * device-table is invalid.
427 */
428 if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
429 odi->osdname_len = sizeof(dt_dev->osdname) - 1;
430 dt_dev->osdname[odi->osdname_len] = 0;
431
432 /* If it's all zeros something is bad we read past end-of-obj */
433 return !(odi->systemid_len || odi->osdname_len);
434}
435
436static int exofs_read_lookup_dev_table(struct exofs_sb_info **psbi,
437 unsigned table_count)
438{
439 struct exofs_sb_info *sbi = *psbi;
440 struct osd_dev *fscb_od;
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200441 struct osd_obj_id obj = {.partition = sbi->layout.s_pid,
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200442 .id = EXOFS_DEVTABLE_ID};
443 struct exofs_device_table *dt;
444 unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
445 sizeof(*dt);
446 unsigned numdevs, i;
447 int ret;
448
449 dt = kmalloc(table_bytes, GFP_KERNEL);
450 if (unlikely(!dt)) {
451 EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
452 table_bytes);
453 return -ENOMEM;
454 }
455
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200456 fscb_od = sbi->layout.s_ods[0];
457 sbi->layout.s_ods[0] = NULL;
458 sbi->layout.s_numdevs = 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200459 ret = exofs_read_kern(fscb_od, sbi->s_cred, &obj, 0, dt, table_bytes);
460 if (unlikely(ret)) {
461 EXOFS_ERR("ERROR: reading device table\n");
462 goto out;
463 }
464
465 numdevs = le64_to_cpu(dt->dt_num_devices);
466 if (unlikely(!numdevs)) {
467 ret = -EINVAL;
468 goto out;
469 }
470 WARN_ON(table_count != numdevs);
471
472 ret = _read_and_match_data_map(sbi, numdevs, dt);
473 if (unlikely(ret))
474 goto out;
475
476 if (likely(numdevs > 1)) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200477 unsigned size = numdevs * sizeof(sbi->layout.s_ods[0]);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200478
479 sbi = krealloc(sbi, sizeof(*sbi) + size, GFP_KERNEL);
480 if (unlikely(!sbi)) {
481 ret = -ENOMEM;
482 goto out;
483 }
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200484 memset(&sbi->layout.s_ods[1], 0,
485 size - sizeof(sbi->layout.s_ods[0]));
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200486 *psbi = sbi;
487 }
488
489 for (i = 0; i < numdevs; i++) {
490 struct exofs_fscb fscb;
491 struct osd_dev_info odi;
492 struct osd_dev *od;
493
494 if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
495 EXOFS_ERR("ERROR: Read all-zeros device entry\n");
496 ret = -EINVAL;
497 goto out;
498 }
499
500 printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
501 i, odi.osdname);
502
503 /* On all devices the device table is identical. The user can
504 * specify any one of the participating devices on the command
505 * line. We always keep them in device-table order.
506 */
507 if (fscb_od && osduld_device_same(fscb_od, &odi)) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200508 sbi->layout.s_ods[i] = fscb_od;
509 ++sbi->layout.s_numdevs;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200510 fscb_od = NULL;
511 continue;
512 }
513
514 od = osduld_info_lookup(&odi);
Tobias Klauser2c722c92010-12-09 15:55:21 +0100515 if (IS_ERR(od)) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200516 ret = PTR_ERR(od);
517 EXOFS_ERR("ERROR: device requested is not found "
518 "osd_name-%s =>%d\n", odi.osdname, ret);
519 goto out;
520 }
521
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200522 sbi->layout.s_ods[i] = od;
523 ++sbi->layout.s_numdevs;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200524
525 /* Read the fscb of the other devices to make sure the FS
526 * partition is there.
527 */
528 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb,
529 sizeof(fscb));
530 if (unlikely(ret)) {
531 EXOFS_ERR("ERROR: Malformed participating device "
532 "error reading fscb osd_name-%s\n",
533 odi.osdname);
534 goto out;
535 }
536
537 /* TODO: verify other information is correct and FS-uuid
538 * matches. Benny what did you say about device table
539 * generation and old devices?
540 */
541 }
542
543out:
544 kfree(dt);
545 if (unlikely(!ret && fscb_od)) {
546 EXOFS_ERR(
547 "ERROR: Bad device-table container device not present\n");
548 osduld_put_device(fscb_od);
549 ret = -EINVAL;
550 }
551
552 return ret;
553}
554
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200555/*
556 * Read the superblock from the OSD and fill in the fields
557 */
558static int exofs_fill_super(struct super_block *sb, void *data, int silent)
559{
560 struct inode *root;
561 struct exofs_mountopt *opts = data;
562 struct exofs_sb_info *sbi; /*extended info */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200563 struct osd_dev *od; /* Master device */
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200564 struct exofs_fscb fscb; /*on-disk superblock info */
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200565 struct osd_obj_id obj;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200566 unsigned table_count;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200567 int ret;
568
569 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
570 if (!sbi)
571 return -ENOMEM;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200572
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200573 ret = bdi_setup_and_register(&sbi->bdi, "exofs", BDI_CAP_MAP_COPY);
574 if (ret)
575 goto free_bdi;
576
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200577 /* use mount options to fill superblock */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200578 od = osduld_path_lookup(opts->dev_name);
579 if (IS_ERR(od)) {
580 ret = PTR_ERR(od);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200581 goto free_sbi;
582 }
583
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200584 /* Default layout in case we do not have a device-table */
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200585 sbi->layout.stripe_unit = PAGE_SIZE;
586 sbi->layout.mirrors_p1 = 1;
587 sbi->layout.group_width = 1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200588 sbi->layout.group_depth = -1;
589 sbi->layout.group_count = 1;
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200590 sbi->layout.s_ods[0] = od;
591 sbi->layout.s_numdevs = 1;
592 sbi->layout.s_pid = opts->pid;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200593 sbi->s_timeout = opts->timeout;
594
595 /* fill in some other data by hand */
596 memset(sb->s_id, 0, sizeof(sb->s_id));
597 strcpy(sb->s_id, "exofs");
598 sb->s_blocksize = EXOFS_BLKSIZE;
599 sb->s_blocksize_bits = EXOFS_BLKSHIFT;
600 sb->s_maxbytes = MAX_LFS_FILESIZE;
601 atomic_set(&sbi->s_curr_pending, 0);
602 sb->s_bdev = NULL;
603 sb->s_dev = 0;
604
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200605 obj.partition = sbi->layout.s_pid;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200606 obj.id = EXOFS_SUPER_ID;
607 exofs_make_credential(sbi->s_cred, &obj);
608
Boaz Harrosh06886a52009-11-08 14:54:08 +0200609 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb, sizeof(fscb));
610 if (unlikely(ret))
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200611 goto free_sbi;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200612
613 sb->s_magic = le16_to_cpu(fscb.s_magic);
614 sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
615 sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
616
617 /* make sure what we read from the object store is correct */
618 if (sb->s_magic != EXOFS_SUPER_MAGIC) {
619 if (!silent)
620 EXOFS_ERR("ERROR: Bad magic value\n");
621 ret = -EINVAL;
622 goto free_sbi;
623 }
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200624 if (le32_to_cpu(fscb.s_version) != EXOFS_FSCB_VER) {
625 EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
626 EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
627 ret = -EINVAL;
628 goto free_sbi;
629 }
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200630
631 /* start generation numbers from a random point */
632 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
633 spin_lock_init(&sbi->s_next_gen_lock);
634
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200635 table_count = le64_to_cpu(fscb.s_dev_table_count);
636 if (table_count) {
637 ret = exofs_read_lookup_dev_table(&sbi, table_count);
638 if (unlikely(ret))
639 goto free_sbi;
640 }
641
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200642 /* set up operation vectors */
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -0400643 sbi->bdi.ra_pages = __ra_pages(&sbi->layout);
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200644 sb->s_bdi = &sbi->bdi;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200645 sb->s_fs_info = sbi;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200646 sb->s_op = &exofs_sops;
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200647 sb->s_export_op = &exofs_export_ops;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200648 root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
649 if (IS_ERR(root)) {
650 EXOFS_ERR("ERROR: exofs_iget failed\n");
651 ret = PTR_ERR(root);
652 goto free_sbi;
653 }
654 sb->s_root = d_alloc_root(root);
655 if (!sb->s_root) {
656 iput(root);
657 EXOFS_ERR("ERROR: get root inode failed\n");
658 ret = -ENOMEM;
659 goto free_sbi;
660 }
661
662 if (!S_ISDIR(root->i_mode)) {
663 dput(sb->s_root);
664 sb->s_root = NULL;
665 EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
666 root->i_mode);
667 ret = -EINVAL;
668 goto free_sbi;
669 }
670
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200671 _exofs_print_device("Mounting", opts->dev_name, sbi->layout.s_ods[0],
672 sbi->layout.s_pid);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200673 return 0;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200674
675free_sbi:
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200676 bdi_destroy(&sbi->bdi);
677free_bdi:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200678 EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200679 opts->dev_name, sbi->layout.s_pid, ret);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200680 exofs_free_sbi(sbi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200681 return ret;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200682}
683
684/*
685 * Set up the superblock (calls exofs_fill_super eventually)
686 */
Al Viro3c26ff62010-07-25 11:46:36 +0400687static struct dentry *exofs_mount(struct file_system_type *type,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200688 int flags, const char *dev_name,
Al Viro3c26ff62010-07-25 11:46:36 +0400689 void *data)
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200690{
691 struct exofs_mountopt opts;
692 int ret;
693
694 ret = parse_options(data, &opts);
695 if (ret)
Al Viro3c26ff62010-07-25 11:46:36 +0400696 return ERR_PTR(ret);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200697
698 opts.dev_name = dev_name;
Al Viro3c26ff62010-07-25 11:46:36 +0400699 return mount_nodev(type, flags, &opts, exofs_fill_super);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200700}
701
702/*
703 * Return information about the file system state in the buffer. This is used
704 * by the 'df' command, for example.
705 */
706static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
707{
708 struct super_block *sb = dentry->d_sb;
709 struct exofs_sb_info *sbi = sb->s_fs_info;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200710 struct exofs_io_state *ios;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200711 struct osd_attr attrs[] = {
712 ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
713 OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
714 ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
715 OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
716 };
717 uint64_t capacity = ULLONG_MAX;
718 uint64_t used = ULLONG_MAX;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200719 uint8_t cred_a[OSD_CAP_LEN];
720 int ret;
721
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200722 ret = exofs_get_io_state(&sbi->layout, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200723 if (ret) {
724 EXOFS_DBGMSG("exofs_get_io_state failed.\n");
725 return ret;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200726 }
727
Boaz Harrosh06886a52009-11-08 14:54:08 +0200728 exofs_make_credential(cred_a, &ios->obj);
729 ios->cred = sbi->s_cred;
730 ios->in_attr = attrs;
731 ios->in_attr_len = ARRAY_SIZE(attrs);
732
733 ret = exofs_sbi_read(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200734 if (unlikely(ret))
735 goto out;
736
Boaz Harrosh06886a52009-11-08 14:54:08 +0200737 ret = extract_attr_from_ios(ios, &attrs[0]);
Boaz Harroshcae012d2009-11-02 18:19:24 +0200738 if (likely(!ret)) {
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200739 capacity = get_unaligned_be64(attrs[0].val_ptr);
Boaz Harroshcae012d2009-11-02 18:19:24 +0200740 if (unlikely(!capacity))
741 capacity = ULLONG_MAX;
742 } else
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200743 EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
744
Boaz Harrosh06886a52009-11-08 14:54:08 +0200745 ret = extract_attr_from_ios(ios, &attrs[1]);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200746 if (likely(!ret))
747 used = get_unaligned_be64(attrs[1].val_ptr);
748 else
749 EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
750
751 /* fill in the stats buffer */
752 buf->f_type = EXOFS_SUPER_MAGIC;
753 buf->f_bsize = EXOFS_BLKSIZE;
Boaz Harroshcae012d2009-11-02 18:19:24 +0200754 buf->f_blocks = capacity >> 9;
755 buf->f_bfree = (capacity - used) >> 9;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200756 buf->f_bavail = buf->f_bfree;
757 buf->f_files = sbi->s_numfiles;
758 buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
759 buf->f_namelen = EXOFS_NAME_LEN;
760
761out:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200762 exofs_put_io_state(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200763 return ret;
764}
765
766static const struct super_operations exofs_sops = {
767 .alloc_inode = exofs_alloc_inode,
768 .destroy_inode = exofs_destroy_inode,
769 .write_inode = exofs_write_inode,
Al Viro4ec70c92010-06-07 11:42:26 -0400770 .evict_inode = exofs_evict_inode,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200771 .put_super = exofs_put_super,
772 .write_super = exofs_write_super,
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200773 .sync_fs = exofs_sync_fs,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200774 .statfs = exofs_statfs,
775};
776
777/******************************************************************************
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200778 * EXPORT OPERATIONS
779 *****************************************************************************/
780
781struct dentry *exofs_get_parent(struct dentry *child)
782{
783 unsigned long ino = exofs_parent_ino(child);
784
785 if (!ino)
786 return NULL;
787
788 return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
789}
790
791static struct inode *exofs_nfs_get_inode(struct super_block *sb,
792 u64 ino, u32 generation)
793{
794 struct inode *inode;
795
796 inode = exofs_iget(sb, ino);
797 if (IS_ERR(inode))
798 return ERR_CAST(inode);
799 if (generation && inode->i_generation != generation) {
800 /* we didn't find the right inode.. */
801 iput(inode);
802 return ERR_PTR(-ESTALE);
803 }
804 return inode;
805}
806
807static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
808 struct fid *fid, int fh_len, int fh_type)
809{
810 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
811 exofs_nfs_get_inode);
812}
813
814static struct dentry *exofs_fh_to_parent(struct super_block *sb,
815 struct fid *fid, int fh_len, int fh_type)
816{
817 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
818 exofs_nfs_get_inode);
819}
820
821static const struct export_operations exofs_export_ops = {
822 .fh_to_dentry = exofs_fh_to_dentry,
823 .fh_to_parent = exofs_fh_to_parent,
824 .get_parent = exofs_get_parent,
825};
826
827/******************************************************************************
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200828 * INSMOD/RMMOD
829 *****************************************************************************/
830
831/*
832 * struct that describes this file system
833 */
834static struct file_system_type exofs_type = {
835 .owner = THIS_MODULE,
836 .name = "exofs",
Al Viro3c26ff62010-07-25 11:46:36 +0400837 .mount = exofs_mount,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200838 .kill_sb = generic_shutdown_super,
839};
840
841static int __init init_exofs(void)
842{
843 int err;
844
845 err = init_inodecache();
846 if (err)
847 goto out;
848
849 err = register_filesystem(&exofs_type);
850 if (err)
851 goto out_d;
852
853 return 0;
854out_d:
855 destroy_inodecache();
856out:
857 return err;
858}
859
860static void __exit exit_exofs(void)
861{
862 unregister_filesystem(&exofs_type);
863 destroy_inodecache();
864}
865
866MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
867MODULE_DESCRIPTION("exofs");
868MODULE_LICENSE("GPL");
869
870module_init(init_exofs)
871module_exit(exit_exofs)