blob: 5eb0851e548105955a3c81404b5cc94702a09775 [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 Pigginfa0d7e32011-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 Pigginfa0d7e32011-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
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200216static const struct osd_attr g_attr_sb_stats = ATTR_DEF(
217 EXOFS_APAGE_SB_DATA,
218 EXOFS_ATTR_SB_STATS,
219 sizeof(struct exofs_sb_stats));
220
221static int __sbi_read_stats(struct exofs_sb_info *sbi)
222{
223 struct osd_attr attrs[] = {
224 [0] = g_attr_sb_stats,
225 };
226 struct exofs_io_state *ios;
227 int ret;
228
229 ret = exofs_get_io_state(&sbi->layout, &ios);
230 if (unlikely(ret)) {
231 EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__);
232 return ret;
233 }
234
235 ios->cred = sbi->s_cred;
236
237 ios->in_attr = attrs;
238 ios->in_attr_len = ARRAY_SIZE(attrs);
239
240 ret = exofs_sbi_read(ios);
241 if (unlikely(ret)) {
242 EXOFS_ERR("Error reading super_block stats => %d\n", ret);
243 goto out;
244 }
245
246 ret = extract_attr_from_ios(ios, &attrs[0]);
247 if (ret) {
248 EXOFS_ERR("%s: extract_attr of sb_stats failed\n", __func__);
249 goto out;
250 }
251 if (attrs[0].len) {
252 struct exofs_sb_stats *ess;
253
254 if (unlikely(attrs[0].len != sizeof(*ess))) {
255 EXOFS_ERR("%s: Wrong version of exofs_sb_stats "
256 "size(%d) != expected(%zd)\n",
257 __func__, attrs[0].len, sizeof(*ess));
258 goto out;
259 }
260
261 ess = attrs[0].val_ptr;
262 sbi->s_nextid = le64_to_cpu(ess->s_nextid);
263 sbi->s_numfiles = le32_to_cpu(ess->s_numfiles);
264 }
265
266out:
267 exofs_put_io_state(ios);
268 return ret;
269}
270
271static void stats_done(struct exofs_io_state *ios, void *p)
272{
273 exofs_put_io_state(ios);
274 /* Good thanks nothing to do anymore */
275}
276
277/* Asynchronously write the stats attribute */
278int exofs_sbi_write_stats(struct exofs_sb_info *sbi)
279{
280 struct osd_attr attrs[] = {
281 [0] = g_attr_sb_stats,
282 };
283 struct exofs_io_state *ios;
284 int ret;
285
286 ret = exofs_get_io_state(&sbi->layout, &ios);
287 if (unlikely(ret)) {
288 EXOFS_ERR("%s: exofs_get_io_state failed.\n", __func__);
289 return ret;
290 }
291
292 sbi->s_ess.s_nextid = cpu_to_le64(sbi->s_nextid);
293 sbi->s_ess.s_numfiles = cpu_to_le64(sbi->s_numfiles);
294 attrs[0].val_ptr = &sbi->s_ess;
295
296 ios->cred = sbi->s_cred;
297 ios->done = stats_done;
298 ios->private = sbi;
299 ios->out_attr = attrs;
300 ios->out_attr_len = ARRAY_SIZE(attrs);
301
302 ret = exofs_sbi_write(ios);
303 if (unlikely(ret)) {
304 EXOFS_ERR("%s: exofs_sbi_write failed.\n", __func__);
305 exofs_put_io_state(ios);
306 }
307
308 return ret;
309}
310
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200311/*
312 * Write the superblock to the OSD
313 */
Boaz Harroshbaaf94c2009-06-14 16:52:10 +0300314int exofs_sync_fs(struct super_block *sb, int wait)
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200315{
316 struct exofs_sb_info *sbi;
317 struct exofs_fscb *fscb;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200318 struct exofs_io_state *ios;
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200319 int ret = -ENOMEM;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200320
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200321 fscb = kmalloc(sizeof(*fscb), GFP_KERNEL);
322 if (unlikely(!fscb))
323 return -ENOMEM;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200324
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200325 sbi = sb->s_fs_info;
326
327 /* NOTE: We no longer dirty the super_block anywhere in exofs. The
328 * reason we write the fscb here on unmount is so we can stay backwards
329 * compatible with fscb->s_version == 1. (What we are not compatible
330 * with is if a new version FS crashed and then we try to mount an old
331 * version). Otherwise the exofs_fscb is read-only from mkfs time. All
332 * the writeable info is set in exofs_sbi_write_stats() above.
333 */
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200334 ret = exofs_get_io_state(&sbi->layout, &ios);
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200335 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200336 goto out;
337
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200338 lock_super(sb);
339
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200340 ios->length = offsetof(struct exofs_fscb, s_dev_table_oid);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200341 memset(fscb, 0, ios->length);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200342 fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
343 fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
344 fscb->s_magic = cpu_to_le16(sb->s_magic);
345 fscb->s_newfs = 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200346 fscb->s_version = EXOFS_FSCB_VER;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200347
Boaz Harrosh06886a52009-11-08 14:54:08 +0200348 ios->obj.id = EXOFS_SUPER_ID;
349 ios->offset = 0;
350 ios->kern_buff = fscb;
351 ios->cred = sbi->s_cred;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200352
Boaz Harrosh06886a52009-11-08 14:54:08 +0200353 ret = exofs_sbi_write(ios);
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200354 if (unlikely(ret))
Boaz Harrosh06886a52009-11-08 14:54:08 +0200355 EXOFS_ERR("%s: exofs_sbi_write failed.\n", __func__);
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200356 else
357 sb->s_dirt = 0;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200358
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200359
360 unlock_super(sb);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200361out:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200362 EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
363 exofs_put_io_state(ios);
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200364 kfree(fscb);
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200365 return ret;
366}
367
368static void exofs_write_super(struct super_block *sb)
369{
370 if (!(sb->s_flags & MS_RDONLY))
371 exofs_sync_fs(sb, 1);
372 else
373 sb->s_dirt = 0;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200374}
375
Boaz Harrosh19fe2942009-09-03 20:38:02 +0300376static void _exofs_print_device(const char *msg, const char *dev_path,
377 struct osd_dev *od, u64 pid)
378{
379 const struct osd_dev_info *odi = osduld_device_info(od);
380
381 printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
382 msg, dev_path ?: "", odi->osdname, _LLU(pid));
383}
384
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200385void exofs_free_sbi(struct exofs_sb_info *sbi)
386{
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200387 while (sbi->layout.s_numdevs) {
388 int i = --sbi->layout.s_numdevs;
389 struct osd_dev *od = sbi->layout.s_ods[i];
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200390
391 if (od) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200392 sbi->layout.s_ods[i] = NULL;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200393 osduld_put_device(od);
394 }
395 }
396 kfree(sbi);
397}
398
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200399/*
400 * This function is called when the vfs is freeing the superblock. We just
401 * need to free our own part.
402 */
403static void exofs_put_super(struct super_block *sb)
404{
405 int num_pend;
406 struct exofs_sb_info *sbi = sb->s_fs_info;
407
408 /* make sure there are no pending commands */
409 for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
410 num_pend = atomic_read(&sbi->s_curr_pending)) {
411 wait_queue_head_t wq;
412 init_waitqueue_head(&wq);
413 wait_event_timeout(wq,
414 (atomic_read(&sbi->s_curr_pending) == 0),
415 msecs_to_jiffies(100));
416 }
417
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200418 _exofs_print_device("Unmounting", NULL, sbi->layout.s_ods[0],
419 sbi->layout.s_pid);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200420
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200421 bdi_destroy(&sbi->bdi);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200422 exofs_free_sbi(sbi);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200423 sb->s_fs_info = NULL;
424}
425
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200426static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
427 struct exofs_device_table *dt)
428{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200429 u64 stripe_length;
430
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200431 sbi->data_map.odm_num_comps =
432 le32_to_cpu(dt->dt_data_map.cb_num_comps);
433 sbi->data_map.odm_stripe_unit =
434 le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
435 sbi->data_map.odm_group_width =
436 le32_to_cpu(dt->dt_data_map.cb_group_width);
437 sbi->data_map.odm_group_depth =
438 le32_to_cpu(dt->dt_data_map.cb_group_depth);
439 sbi->data_map.odm_mirror_cnt =
440 le32_to_cpu(dt->dt_data_map.cb_mirror_cnt);
441 sbi->data_map.odm_raid_algorithm =
442 le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);
443
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200444/* FIXME: Only raid0 for now. if not so, do not mount */
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200445 if (sbi->data_map.odm_num_comps != numdevs) {
446 EXOFS_ERR("odm_num_comps(%u) != numdevs(%u)\n",
447 sbi->data_map.odm_num_comps, numdevs);
448 return -EINVAL;
449 }
450 if (sbi->data_map.odm_raid_algorithm != PNFS_OSD_RAID_0) {
451 EXOFS_ERR("Only RAID_0 for now\n");
452 return -EINVAL;
453 }
454 if (0 != (numdevs % (sbi->data_map.odm_mirror_cnt + 1))) {
455 EXOFS_ERR("Data Map wrong, numdevs=%d mirrors=%d\n",
456 numdevs, sbi->data_map.odm_mirror_cnt);
457 return -EINVAL;
458 }
459
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200460 if (0 != (sbi->data_map.odm_stripe_unit & ~PAGE_MASK)) {
461 EXOFS_ERR("Stripe Unit(0x%llx)"
462 " must be Multples of PAGE_SIZE(0x%lx)\n",
463 _LLU(sbi->data_map.odm_stripe_unit), PAGE_SIZE);
464 return -EINVAL;
465 }
466
467 sbi->layout.stripe_unit = sbi->data_map.odm_stripe_unit;
468 sbi->layout.mirrors_p1 = sbi->data_map.odm_mirror_cnt + 1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200469
470 if (sbi->data_map.odm_group_width) {
471 sbi->layout.group_width = sbi->data_map.odm_group_width;
472 sbi->layout.group_depth = sbi->data_map.odm_group_depth;
473 if (!sbi->layout.group_depth) {
474 EXOFS_ERR("group_depth == 0 && group_width != 0\n");
475 return -EINVAL;
476 }
477 sbi->layout.group_count = sbi->data_map.odm_num_comps /
478 sbi->layout.mirrors_p1 /
479 sbi->data_map.odm_group_width;
480 } else {
481 if (sbi->data_map.odm_group_depth) {
482 printk(KERN_NOTICE "Warning: group_depth ignored "
483 "group_width == 0 && group_depth == %d\n",
484 sbi->data_map.odm_group_depth);
485 sbi->data_map.odm_group_depth = 0;
486 }
487 sbi->layout.group_width = sbi->data_map.odm_num_comps /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200488 sbi->layout.mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200489 sbi->layout.group_depth = -1;
490 sbi->layout.group_count = 1;
491 }
492
493 stripe_length = (u64)sbi->layout.group_width * sbi->layout.stripe_unit;
494 if (stripe_length >= (1ULL << 32)) {
495 EXOFS_ERR("Total Stripe length(0x%llx)"
496 " >= 32bit is not supported\n", _LLU(stripe_length));
497 return -EINVAL;
498 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200499
500 return 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200501}
502
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -0400503static unsigned __ra_pages(struct exofs_layout *layout)
504{
505 const unsigned _MIN_RA = 32; /* min 128K read-ahead */
506 unsigned ra_pages = layout->group_width * layout->stripe_unit /
507 PAGE_SIZE;
508 unsigned max_io_pages = exofs_max_io_pages(layout, ~0);
509
510 ra_pages *= 2; /* two stripes */
511 if (ra_pages < _MIN_RA)
512 ra_pages = roundup(_MIN_RA, ra_pages / 2);
513
514 if (ra_pages > max_io_pages)
515 ra_pages = max_io_pages;
516
517 return ra_pages;
518}
519
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200520/* @odi is valid only as long as @fscb_dev is valid */
521static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
522 struct osd_dev_info *odi)
523{
524 odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
525 memcpy(odi->systemid, dt_dev->systemid, odi->systemid_len);
526
527 odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
528 odi->osdname = dt_dev->osdname;
529
530 /* FIXME support long names. Will need a _put function */
531 if (dt_dev->long_name_offset)
532 return -EINVAL;
533
534 /* Make sure osdname is printable!
535 * mkexofs should give us space for a null-terminator else the
536 * device-table is invalid.
537 */
538 if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
539 odi->osdname_len = sizeof(dt_dev->osdname) - 1;
540 dt_dev->osdname[odi->osdname_len] = 0;
541
542 /* If it's all zeros something is bad we read past end-of-obj */
543 return !(odi->systemid_len || odi->osdname_len);
544}
545
546static int exofs_read_lookup_dev_table(struct exofs_sb_info **psbi,
547 unsigned table_count)
548{
549 struct exofs_sb_info *sbi = *psbi;
550 struct osd_dev *fscb_od;
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200551 struct osd_obj_id obj = {.partition = sbi->layout.s_pid,
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200552 .id = EXOFS_DEVTABLE_ID};
553 struct exofs_device_table *dt;
554 unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
555 sizeof(*dt);
556 unsigned numdevs, i;
557 int ret;
558
559 dt = kmalloc(table_bytes, GFP_KERNEL);
560 if (unlikely(!dt)) {
561 EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
562 table_bytes);
563 return -ENOMEM;
564 }
565
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200566 fscb_od = sbi->layout.s_ods[0];
567 sbi->layout.s_ods[0] = NULL;
568 sbi->layout.s_numdevs = 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200569 ret = exofs_read_kern(fscb_od, sbi->s_cred, &obj, 0, dt, table_bytes);
570 if (unlikely(ret)) {
571 EXOFS_ERR("ERROR: reading device table\n");
572 goto out;
573 }
574
575 numdevs = le64_to_cpu(dt->dt_num_devices);
576 if (unlikely(!numdevs)) {
577 ret = -EINVAL;
578 goto out;
579 }
580 WARN_ON(table_count != numdevs);
581
582 ret = _read_and_match_data_map(sbi, numdevs, dt);
583 if (unlikely(ret))
584 goto out;
585
586 if (likely(numdevs > 1)) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200587 unsigned size = numdevs * sizeof(sbi->layout.s_ods[0]);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200588
589 sbi = krealloc(sbi, sizeof(*sbi) + size, GFP_KERNEL);
590 if (unlikely(!sbi)) {
591 ret = -ENOMEM;
592 goto out;
593 }
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200594 memset(&sbi->layout.s_ods[1], 0,
595 size - sizeof(sbi->layout.s_ods[0]));
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200596 *psbi = sbi;
597 }
598
599 for (i = 0; i < numdevs; i++) {
600 struct exofs_fscb fscb;
601 struct osd_dev_info odi;
602 struct osd_dev *od;
603
604 if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
605 EXOFS_ERR("ERROR: Read all-zeros device entry\n");
606 ret = -EINVAL;
607 goto out;
608 }
609
610 printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
611 i, odi.osdname);
612
613 /* On all devices the device table is identical. The user can
614 * specify any one of the participating devices on the command
615 * line. We always keep them in device-table order.
616 */
617 if (fscb_od && osduld_device_same(fscb_od, &odi)) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200618 sbi->layout.s_ods[i] = fscb_od;
619 ++sbi->layout.s_numdevs;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200620 fscb_od = NULL;
621 continue;
622 }
623
624 od = osduld_info_lookup(&odi);
Tobias Klauser2c722c92010-12-09 15:55:21 +0100625 if (IS_ERR(od)) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200626 ret = PTR_ERR(od);
627 EXOFS_ERR("ERROR: device requested is not found "
628 "osd_name-%s =>%d\n", odi.osdname, ret);
629 goto out;
630 }
631
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200632 sbi->layout.s_ods[i] = od;
633 ++sbi->layout.s_numdevs;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200634
635 /* Read the fscb of the other devices to make sure the FS
636 * partition is there.
637 */
638 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb,
639 sizeof(fscb));
640 if (unlikely(ret)) {
641 EXOFS_ERR("ERROR: Malformed participating device "
642 "error reading fscb osd_name-%s\n",
643 odi.osdname);
644 goto out;
645 }
646
647 /* TODO: verify other information is correct and FS-uuid
648 * matches. Benny what did you say about device table
649 * generation and old devices?
650 */
651 }
652
653out:
654 kfree(dt);
655 if (unlikely(!ret && fscb_od)) {
656 EXOFS_ERR(
657 "ERROR: Bad device-table container device not present\n");
658 osduld_put_device(fscb_od);
659 ret = -EINVAL;
660 }
661
662 return ret;
663}
664
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200665/*
666 * Read the superblock from the OSD and fill in the fields
667 */
668static int exofs_fill_super(struct super_block *sb, void *data, int silent)
669{
670 struct inode *root;
671 struct exofs_mountopt *opts = data;
672 struct exofs_sb_info *sbi; /*extended info */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200673 struct osd_dev *od; /* Master device */
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200674 struct exofs_fscb fscb; /*on-disk superblock info */
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200675 struct osd_obj_id obj;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200676 unsigned table_count;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200677 int ret;
678
679 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
680 if (!sbi)
681 return -ENOMEM;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200682
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200683 ret = bdi_setup_and_register(&sbi->bdi, "exofs", BDI_CAP_MAP_COPY);
684 if (ret)
685 goto free_bdi;
686
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200687 /* use mount options to fill superblock */
Boaz Harrosh9ed96482011-01-31 14:32:14 +0200688 if (opts->is_osdname) {
689 struct osd_dev_info odi = {.systemid_len = 0};
690
691 odi.osdname_len = strlen(opts->dev_name);
692 odi.osdname = (u8 *)opts->dev_name;
693 od = osduld_info_lookup(&odi);
694 } else {
695 od = osduld_path_lookup(opts->dev_name);
696 }
Boaz Harrosh06886a52009-11-08 14:54:08 +0200697 if (IS_ERR(od)) {
Boaz Harrosh9ed96482011-01-31 14:32:14 +0200698 ret = -EINVAL;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200699 goto free_sbi;
700 }
701
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200702 /* Default layout in case we do not have a device-table */
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200703 sbi->layout.stripe_unit = PAGE_SIZE;
704 sbi->layout.mirrors_p1 = 1;
705 sbi->layout.group_width = 1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200706 sbi->layout.group_depth = -1;
707 sbi->layout.group_count = 1;
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200708 sbi->layout.s_ods[0] = od;
709 sbi->layout.s_numdevs = 1;
710 sbi->layout.s_pid = opts->pid;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200711 sbi->s_timeout = opts->timeout;
712
713 /* fill in some other data by hand */
714 memset(sb->s_id, 0, sizeof(sb->s_id));
715 strcpy(sb->s_id, "exofs");
716 sb->s_blocksize = EXOFS_BLKSIZE;
717 sb->s_blocksize_bits = EXOFS_BLKSHIFT;
718 sb->s_maxbytes = MAX_LFS_FILESIZE;
719 atomic_set(&sbi->s_curr_pending, 0);
720 sb->s_bdev = NULL;
721 sb->s_dev = 0;
722
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200723 obj.partition = sbi->layout.s_pid;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200724 obj.id = EXOFS_SUPER_ID;
725 exofs_make_credential(sbi->s_cred, &obj);
726
Boaz Harrosh06886a52009-11-08 14:54:08 +0200727 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb, sizeof(fscb));
728 if (unlikely(ret))
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200729 goto free_sbi;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200730
731 sb->s_magic = le16_to_cpu(fscb.s_magic);
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200732 /* NOTE: we read below to be backward compatible with old versions */
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200733 sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
734 sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
735
736 /* make sure what we read from the object store is correct */
737 if (sb->s_magic != EXOFS_SUPER_MAGIC) {
738 if (!silent)
739 EXOFS_ERR("ERROR: Bad magic value\n");
740 ret = -EINVAL;
741 goto free_sbi;
742 }
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200743 if (le32_to_cpu(fscb.s_version) > EXOFS_FSCB_VER) {
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200744 EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
745 EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
746 ret = -EINVAL;
747 goto free_sbi;
748 }
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200749
750 /* start generation numbers from a random point */
751 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
752 spin_lock_init(&sbi->s_next_gen_lock);
753
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200754 table_count = le64_to_cpu(fscb.s_dev_table_count);
755 if (table_count) {
756 ret = exofs_read_lookup_dev_table(&sbi, table_count);
757 if (unlikely(ret))
758 goto free_sbi;
759 }
760
Boaz Harrosh1cea3122011-02-03 17:53:25 +0200761 __sbi_read_stats(sbi);
762
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200763 /* set up operation vectors */
bharrosh@panasas.com66cd6ca2010-10-07 14:28:18 -0400764 sbi->bdi.ra_pages = __ra_pages(&sbi->layout);
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200765 sb->s_bdi = &sbi->bdi;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200766 sb->s_fs_info = sbi;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200767 sb->s_op = &exofs_sops;
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200768 sb->s_export_op = &exofs_export_ops;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200769 root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
770 if (IS_ERR(root)) {
771 EXOFS_ERR("ERROR: exofs_iget failed\n");
772 ret = PTR_ERR(root);
773 goto free_sbi;
774 }
775 sb->s_root = d_alloc_root(root);
776 if (!sb->s_root) {
777 iput(root);
778 EXOFS_ERR("ERROR: get root inode failed\n");
779 ret = -ENOMEM;
780 goto free_sbi;
781 }
782
783 if (!S_ISDIR(root->i_mode)) {
784 dput(sb->s_root);
785 sb->s_root = NULL;
786 EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
787 root->i_mode);
788 ret = -EINVAL;
789 goto free_sbi;
790 }
791
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200792 _exofs_print_device("Mounting", opts->dev_name, sbi->layout.s_ods[0],
793 sbi->layout.s_pid);
Boaz Harrosh9ed96482011-01-31 14:32:14 +0200794 if (opts->is_osdname)
795 kfree(opts->dev_name);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200796 return 0;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200797
798free_sbi:
Jens Axboeb3d0ab72010-04-22 12:26:04 +0200799 bdi_destroy(&sbi->bdi);
800free_bdi:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200801 EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200802 opts->dev_name, sbi->layout.s_pid, ret);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200803 exofs_free_sbi(sbi);
Boaz Harrosh9ed96482011-01-31 14:32:14 +0200804 if (opts->is_osdname)
805 kfree(opts->dev_name);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200806 return ret;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200807}
808
809/*
810 * Set up the superblock (calls exofs_fill_super eventually)
811 */
Al Viro3c26ff62010-07-25 11:46:36 +0400812static struct dentry *exofs_mount(struct file_system_type *type,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200813 int flags, const char *dev_name,
Al Viro3c26ff62010-07-25 11:46:36 +0400814 void *data)
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200815{
816 struct exofs_mountopt opts;
817 int ret;
818
819 ret = parse_options(data, &opts);
820 if (ret)
Al Viro3c26ff62010-07-25 11:46:36 +0400821 return ERR_PTR(ret);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200822
Boaz Harrosh9ed96482011-01-31 14:32:14 +0200823 if (!opts.dev_name)
824 opts.dev_name = dev_name;
Al Viro3c26ff62010-07-25 11:46:36 +0400825 return mount_nodev(type, flags, &opts, exofs_fill_super);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200826}
827
828/*
829 * Return information about the file system state in the buffer. This is used
830 * by the 'df' command, for example.
831 */
832static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
833{
834 struct super_block *sb = dentry->d_sb;
835 struct exofs_sb_info *sbi = sb->s_fs_info;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200836 struct exofs_io_state *ios;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200837 struct osd_attr attrs[] = {
838 ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
839 OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
840 ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
841 OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
842 };
843 uint64_t capacity = ULLONG_MAX;
844 uint64_t used = ULLONG_MAX;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200845 uint8_t cred_a[OSD_CAP_LEN];
846 int ret;
847
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200848 ret = exofs_get_io_state(&sbi->layout, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200849 if (ret) {
850 EXOFS_DBGMSG("exofs_get_io_state failed.\n");
851 return ret;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200852 }
853
Boaz Harrosh06886a52009-11-08 14:54:08 +0200854 exofs_make_credential(cred_a, &ios->obj);
855 ios->cred = sbi->s_cred;
856 ios->in_attr = attrs;
857 ios->in_attr_len = ARRAY_SIZE(attrs);
858
859 ret = exofs_sbi_read(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200860 if (unlikely(ret))
861 goto out;
862
Boaz Harrosh06886a52009-11-08 14:54:08 +0200863 ret = extract_attr_from_ios(ios, &attrs[0]);
Boaz Harroshcae012d2009-11-02 18:19:24 +0200864 if (likely(!ret)) {
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200865 capacity = get_unaligned_be64(attrs[0].val_ptr);
Boaz Harroshcae012d2009-11-02 18:19:24 +0200866 if (unlikely(!capacity))
867 capacity = ULLONG_MAX;
868 } else
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200869 EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
870
Boaz Harrosh06886a52009-11-08 14:54:08 +0200871 ret = extract_attr_from_ios(ios, &attrs[1]);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200872 if (likely(!ret))
873 used = get_unaligned_be64(attrs[1].val_ptr);
874 else
875 EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
876
877 /* fill in the stats buffer */
878 buf->f_type = EXOFS_SUPER_MAGIC;
879 buf->f_bsize = EXOFS_BLKSIZE;
Boaz Harroshcae012d2009-11-02 18:19:24 +0200880 buf->f_blocks = capacity >> 9;
881 buf->f_bfree = (capacity - used) >> 9;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200882 buf->f_bavail = buf->f_bfree;
883 buf->f_files = sbi->s_numfiles;
884 buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
885 buf->f_namelen = EXOFS_NAME_LEN;
886
887out:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200888 exofs_put_io_state(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200889 return ret;
890}
891
892static const struct super_operations exofs_sops = {
893 .alloc_inode = exofs_alloc_inode,
894 .destroy_inode = exofs_destroy_inode,
895 .write_inode = exofs_write_inode,
Al Viro4ec70c92010-06-07 11:42:26 -0400896 .evict_inode = exofs_evict_inode,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200897 .put_super = exofs_put_super,
898 .write_super = exofs_write_super,
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200899 .sync_fs = exofs_sync_fs,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200900 .statfs = exofs_statfs,
901};
902
903/******************************************************************************
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200904 * EXPORT OPERATIONS
905 *****************************************************************************/
906
907struct dentry *exofs_get_parent(struct dentry *child)
908{
909 unsigned long ino = exofs_parent_ino(child);
910
911 if (!ino)
912 return NULL;
913
914 return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
915}
916
917static struct inode *exofs_nfs_get_inode(struct super_block *sb,
918 u64 ino, u32 generation)
919{
920 struct inode *inode;
921
922 inode = exofs_iget(sb, ino);
923 if (IS_ERR(inode))
924 return ERR_CAST(inode);
925 if (generation && inode->i_generation != generation) {
926 /* we didn't find the right inode.. */
927 iput(inode);
928 return ERR_PTR(-ESTALE);
929 }
930 return inode;
931}
932
933static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
934 struct fid *fid, int fh_len, int fh_type)
935{
936 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
937 exofs_nfs_get_inode);
938}
939
940static struct dentry *exofs_fh_to_parent(struct super_block *sb,
941 struct fid *fid, int fh_len, int fh_type)
942{
943 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
944 exofs_nfs_get_inode);
945}
946
947static const struct export_operations exofs_export_ops = {
948 .fh_to_dentry = exofs_fh_to_dentry,
949 .fh_to_parent = exofs_fh_to_parent,
950 .get_parent = exofs_get_parent,
951};
952
953/******************************************************************************
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200954 * INSMOD/RMMOD
955 *****************************************************************************/
956
957/*
958 * struct that describes this file system
959 */
960static struct file_system_type exofs_type = {
961 .owner = THIS_MODULE,
962 .name = "exofs",
Al Viro3c26ff62010-07-25 11:46:36 +0400963 .mount = exofs_mount,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200964 .kill_sb = generic_shutdown_super,
965};
966
967static int __init init_exofs(void)
968{
969 int err;
970
971 err = init_inodecache();
972 if (err)
973 goto out;
974
975 err = register_filesystem(&exofs_type);
976 if (err)
977 goto out_d;
978
979 return 0;
980out_d:
981 destroy_inodecache();
982out:
983 return err;
984}
985
986static void __exit exit_exofs(void)
987{
988 unregister_filesystem(&exofs_type);
989 destroy_inodecache();
990}
991
992MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
993MODULE_DESCRIPTION("exofs");
994MODULE_LICENSE("GPL");
995
996module_init(init_exofs)
997module_exit(exit_exofs)