blob: 474989eeb7d6486208179c8ce1849651102903f0 [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 {
Boaz Harrosh9ed96482011-01-31 14:32:14 +020051 bool is_osdname;
Boaz Harroshba9e5e92008-10-28 16:11:41 +020052 const char *dev_name;
53 uint64_t pid;
54 int timeout;
55};
56
57/*
58 * exofs-specific mount-time options.
59 */
Boaz Harrosh9ed96482011-01-31 14:32:14 +020060enum { Opt_name, Opt_pid, Opt_to, Opt_err };
Boaz Harroshba9e5e92008-10-28 16:11:41 +020061
62/*
63 * Our mount-time options. These should ideally be 64-bit unsigned, but the
64 * kernel's parsing functions do not currently support that. 32-bit should be
65 * sufficient for most applications now.
66 */
67static match_table_t tokens = {
Boaz Harrosh9ed96482011-01-31 14:32:14 +020068 {Opt_name, "osdname=%s"},
Boaz Harroshba9e5e92008-10-28 16:11:41 +020069 {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 */
78static 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) {
Boaz Harrosh9ed96482011-01-31 14:32:14 +020099 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;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200107 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 */
146static struct kmem_cache *exofs_inode_cachep;
147
148/*
149 * Allocate an inode in the cache
150 */
151static 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
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100163static 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
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200170/*
171 * Remove an inode from the cache
172 */
173static void exofs_destroy_inode(struct inode *inode)
174{
Nick Pigginfa0d7e3d2011-01-07 17:49:49 +1100175 call_rcu(&inode->i_rcu, exofs_i_callback);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200176}
177
178/*
179 * Initialize the inode
180 */
181static 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 */
191static 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 */
205static void destroy_inodecache(void)
206{
207 kmem_cache_destroy(exofs_inode_cachep);
208}
209
210/******************************************************************************
211 * SUPERBLOCK FUNCTIONS
212 *****************************************************************************/
213static const struct super_operations exofs_sops;
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200214static const struct export_operations exofs_export_ops;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200215
216/*
217 * Write the superblock to the OSD
218 */
Boaz Harroshbaaf94c2009-06-14 16:52:10 +0300219int exofs_sync_fs(struct super_block *sb, int wait)
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200220{
221 struct exofs_sb_info *sbi;
222 struct exofs_fscb *fscb;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200223 struct exofs_io_state *ios;
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200224 int ret = -ENOMEM;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200225
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200226 lock_super(sb);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200227 sbi = sb->s_fs_info;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200228 fscb = &sbi->s_fscb;
229
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200230 ret = exofs_get_io_state(&sbi->layout, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200231 if (ret)
232 goto out;
233
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200234 /* 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);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200239 memset(fscb, 0, ios->length);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200240 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;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200244 fscb->s_version = EXOFS_FSCB_VER;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200245
Boaz Harrosh06886a52009-11-08 14:54:08 +0200246 ios->obj.id = EXOFS_SUPER_ID;
247 ios->offset = 0;
248 ios->kern_buff = fscb;
249 ios->cred = sbi->s_cred;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200250
Boaz Harrosh06886a52009-11-08 14:54:08 +0200251 ret = exofs_sbi_write(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200252 if (unlikely(ret)) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200253 EXOFS_ERR("%s: exofs_sbi_write failed.\n", __func__);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200254 goto out;
255 }
256 sb->s_dirt = 0;
257
258out:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200259 EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
260 exofs_put_io_state(ios);
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200261 unlock_super(sb);
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200262 return ret;
263}
264
265static 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;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200271}
272
Boaz Harrosh19fe2942009-09-03 20:38:02 +0300273static 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
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200282void exofs_free_sbi(struct exofs_sb_info *sbi)
283{
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200284 while (sbi->layout.s_numdevs) {
285 int i = --sbi->layout.s_numdevs;
286 struct osd_dev *od = sbi->layout.s_ods[i];
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200287
288 if (od) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200289 sbi->layout.s_ods[i] = NULL;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200290 osduld_put_device(od);
291 }
292 }
293 kfree(sbi);
294}
295
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200296/*
297 * This function is called when the vfs is freeing the superblock. We just
298 * need to free our own part.
299 */
300static void exofs_put_super(struct super_block *sb)
301{
302 int num_pend;
303 struct exofs_sb_info *sbi = sb->s_fs_info;
304
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200305 if (sb->s_dirt)
306 exofs_write_super(sb);
307
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200308 /* 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
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200318 _exofs_print_device("Unmounting", NULL, sbi->layout.s_ods[0],
319 sbi->layout.s_pid);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200320
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200321 bdi_destroy(&sbi->bdi);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200322 exofs_free_sbi(sbi);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200323 sb->s_fs_info = NULL;
324}
325
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200326static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
327 struct exofs_device_table *dt)
328{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200329 u64 stripe_length;
330
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200331 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
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200344/* FIXME: Only raid0 for now. if not so, do not mount */
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200345 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
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200360 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;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200369
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 /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200388 sbi->layout.mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200389 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 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200399
400 return 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200401}
402
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -0400403static 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
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200420/* @odi is valid only as long as @fscb_dev is valid */
421static 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
446static 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;
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200451 struct osd_obj_id obj = {.partition = sbi->layout.s_pid,
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200452 .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
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200466 fscb_od = sbi->layout.s_ods[0];
467 sbi->layout.s_ods[0] = NULL;
468 sbi->layout.s_numdevs = 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200469 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)) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200487 unsigned size = numdevs * sizeof(sbi->layout.s_ods[0]);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200488
489 sbi = krealloc(sbi, sizeof(*sbi) + size, GFP_KERNEL);
490 if (unlikely(!sbi)) {
491 ret = -ENOMEM;
492 goto out;
493 }
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200494 memset(&sbi->layout.s_ods[1], 0,
495 size - sizeof(sbi->layout.s_ods[0]));
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200496 *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)) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200518 sbi->layout.s_ods[i] = fscb_od;
519 ++sbi->layout.s_numdevs;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200520 fscb_od = NULL;
521 continue;
522 }
523
524 od = osduld_info_lookup(&odi);
Tobias Klauser2c722c92010-12-09 15:55:21 +0100525 if (IS_ERR(od)) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200526 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
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200532 sbi->layout.s_ods[i] = od;
533 ++sbi->layout.s_numdevs;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200534
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
553out:
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
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200565/*
566 * Read the superblock from the OSD and fill in the fields
567 */
568static 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 */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200573 struct osd_dev *od; /* Master device */
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200574 struct exofs_fscb fscb; /*on-disk superblock info */
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200575 struct osd_obj_id obj;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200576 unsigned table_count;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200577 int ret;
578
579 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
580 if (!sbi)
581 return -ENOMEM;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200582
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200583 ret = bdi_setup_and_register(&sbi->bdi, "exofs", BDI_CAP_MAP_COPY);
584 if (ret)
585 goto free_bdi;
586
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200587 /* use mount options to fill superblock */
Boaz Harrosh9ed96482011-01-31 14:32:14 +0200588 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 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200597 if (IS_ERR(od)) {
Boaz Harrosh9ed96482011-01-31 14:32:14 +0200598 ret = -EINVAL;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200599 goto free_sbi;
600 }
601
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200602 /* Default layout in case we do not have a device-table */
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200603 sbi->layout.stripe_unit = PAGE_SIZE;
604 sbi->layout.mirrors_p1 = 1;
605 sbi->layout.group_width = 1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200606 sbi->layout.group_depth = -1;
607 sbi->layout.group_count = 1;
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200608 sbi->layout.s_ods[0] = od;
609 sbi->layout.s_numdevs = 1;
610 sbi->layout.s_pid = opts->pid;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200611 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
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200623 obj.partition = sbi->layout.s_pid;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200624 obj.id = EXOFS_SUPER_ID;
625 exofs_make_credential(sbi->s_cred, &obj);
626
Boaz Harrosh06886a52009-11-08 14:54:08 +0200627 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb, sizeof(fscb));
628 if (unlikely(ret))
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200629 goto free_sbi;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200630
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 }
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200642 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 }
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200648
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
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200653 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
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200660 /* set up operation vectors */
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -0400661 sbi->bdi.ra_pages = __ra_pages(&sbi->layout);
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200662 sb->s_bdi = &sbi->bdi;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200663 sb->s_fs_info = sbi;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200664 sb->s_op = &exofs_sops;
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200665 sb->s_export_op = &exofs_export_ops;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200666 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
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200689 _exofs_print_device("Mounting", opts->dev_name, sbi->layout.s_ods[0],
690 sbi->layout.s_pid);
Boaz Harrosh9ed96482011-01-31 14:32:14 +0200691 if (opts->is_osdname)
692 kfree(opts->dev_name);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200693 return 0;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200694
695free_sbi:
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200696 bdi_destroy(&sbi->bdi);
697free_bdi:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200698 EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200699 opts->dev_name, sbi->layout.s_pid, ret);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200700 exofs_free_sbi(sbi);
Boaz Harrosh9ed96482011-01-31 14:32:14 +0200701 if (opts->is_osdname)
702 kfree(opts->dev_name);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200703 return ret;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200704}
705
706/*
707 * Set up the superblock (calls exofs_fill_super eventually)
708 */
Al Viro3c26ff62010-07-25 11:46:36 +0400709static struct dentry *exofs_mount(struct file_system_type *type,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200710 int flags, const char *dev_name,
Al Viro3c26ff62010-07-25 11:46:36 +0400711 void *data)
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200712{
713 struct exofs_mountopt opts;
714 int ret;
715
716 ret = parse_options(data, &opts);
717 if (ret)
Al Viro3c26ff62010-07-25 11:46:36 +0400718 return ERR_PTR(ret);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200719
Boaz Harrosh9ed96482011-01-31 14:32:14 +0200720 if (!opts.dev_name)
721 opts.dev_name = dev_name;
Al Viro3c26ff62010-07-25 11:46:36 +0400722 return mount_nodev(type, flags, &opts, exofs_fill_super);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200723}
724
725/*
726 * Return information about the file system state in the buffer. This is used
727 * by the 'df' command, for example.
728 */
729static 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;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200733 struct exofs_io_state *ios;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200734 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;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200742 uint8_t cred_a[OSD_CAP_LEN];
743 int ret;
744
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200745 ret = exofs_get_io_state(&sbi->layout, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200746 if (ret) {
747 EXOFS_DBGMSG("exofs_get_io_state failed.\n");
748 return ret;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200749 }
750
Boaz Harrosh06886a52009-11-08 14:54:08 +0200751 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);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200757 if (unlikely(ret))
758 goto out;
759
Boaz Harrosh06886a52009-11-08 14:54:08 +0200760 ret = extract_attr_from_ios(ios, &attrs[0]);
Boaz Harroshcae012d2009-11-02 18:19:24 +0200761 if (likely(!ret)) {
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200762 capacity = get_unaligned_be64(attrs[0].val_ptr);
Boaz Harroshcae012d2009-11-02 18:19:24 +0200763 if (unlikely(!capacity))
764 capacity = ULLONG_MAX;
765 } else
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200766 EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
767
Boaz Harrosh06886a52009-11-08 14:54:08 +0200768 ret = extract_attr_from_ios(ios, &attrs[1]);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200769 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;
Boaz Harroshcae012d2009-11-02 18:19:24 +0200777 buf->f_blocks = capacity >> 9;
778 buf->f_bfree = (capacity - used) >> 9;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200779 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
784out:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200785 exofs_put_io_state(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200786 return ret;
787}
788
789static const struct super_operations exofs_sops = {
790 .alloc_inode = exofs_alloc_inode,
791 .destroy_inode = exofs_destroy_inode,
792 .write_inode = exofs_write_inode,
Al Viro4ec70c92010-06-07 11:42:26 -0400793 .evict_inode = exofs_evict_inode,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200794 .put_super = exofs_put_super,
795 .write_super = exofs_write_super,
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200796 .sync_fs = exofs_sync_fs,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200797 .statfs = exofs_statfs,
798};
799
800/******************************************************************************
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200801 * EXPORT OPERATIONS
802 *****************************************************************************/
803
804struct 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
814static 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
830static 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
837static 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
844static 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/******************************************************************************
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200851 * INSMOD/RMMOD
852 *****************************************************************************/
853
854/*
855 * struct that describes this file system
856 */
857static struct file_system_type exofs_type = {
858 .owner = THIS_MODULE,
859 .name = "exofs",
Al Viro3c26ff62010-07-25 11:46:36 +0400860 .mount = exofs_mount,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200861 .kill_sb = generic_shutdown_super,
862};
863
864static 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;
877out_d:
878 destroy_inodecache();
879out:
880 return err;
881}
882
883static void __exit exit_exofs(void)
884{
885 unregister_filesystem(&exofs_type);
886 destroy_inodecache();
887}
888
889MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
890MODULE_DESCRIPTION("exofs");
891MODULE_LICENSE("GPL");
892
893module_init(init_exofs)
894module_exit(exit_exofs)