blob: 6cf5e4e84d6162dcb461eb4a0521023e2142c00a [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
Alexey Dobriyan405f5572009-07-11 22:08:37 +040034#include <linux/smp_lock.h>
Boaz Harroshba9e5e92008-10-28 16:11:41 +020035#include <linux/string.h>
36#include <linux/parser.h>
37#include <linux/vfs.h>
38#include <linux/random.h>
Boaz Harrosh8cf74b32009-03-22 12:47:26 +020039#include <linux/exportfs.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
153/*
154 * Remove an inode from the cache
155 */
156static void exofs_destroy_inode(struct inode *inode)
157{
158 kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
159}
160
161/*
162 * Initialize the inode
163 */
164static void exofs_init_once(void *foo)
165{
166 struct exofs_i_info *oi = foo;
167
168 inode_init_once(&oi->vfs_inode);
169}
170
171/*
172 * Create and initialize the inode cache
173 */
174static int init_inodecache(void)
175{
176 exofs_inode_cachep = kmem_cache_create("exofs_inode_cache",
177 sizeof(struct exofs_i_info), 0,
178 SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD,
179 exofs_init_once);
180 if (exofs_inode_cachep == NULL)
181 return -ENOMEM;
182 return 0;
183}
184
185/*
186 * Destroy the inode cache
187 */
188static void destroy_inodecache(void)
189{
190 kmem_cache_destroy(exofs_inode_cachep);
191}
192
193/******************************************************************************
194 * SUPERBLOCK FUNCTIONS
195 *****************************************************************************/
196static const struct super_operations exofs_sops;
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200197static const struct export_operations exofs_export_ops;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200198
199/*
200 * Write the superblock to the OSD
201 */
Boaz Harroshbaaf94c2009-06-14 16:52:10 +0300202int exofs_sync_fs(struct super_block *sb, int wait)
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200203{
204 struct exofs_sb_info *sbi;
205 struct exofs_fscb *fscb;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200206 struct exofs_io_state *ios;
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200207 int ret = -ENOMEM;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200208
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200209 lock_super(sb);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200210 sbi = sb->s_fs_info;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200211 fscb = &sbi->s_fscb;
212
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200213 ret = exofs_get_io_state(&sbi->layout, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200214 if (ret)
215 goto out;
216
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200217 /* Note: We only write the changing part of the fscb. .i.e upto the
218 * the fscb->s_dev_table_oid member. There is no read-modify-write
219 * here.
220 */
221 ios->length = offsetof(struct exofs_fscb, s_dev_table_oid);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200222 memset(fscb, 0, ios->length);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200223 fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
224 fscb->s_numfiles = cpu_to_le32(sbi->s_numfiles);
225 fscb->s_magic = cpu_to_le16(sb->s_magic);
226 fscb->s_newfs = 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200227 fscb->s_version = EXOFS_FSCB_VER;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200228
Boaz Harrosh06886a52009-11-08 14:54:08 +0200229 ios->obj.id = EXOFS_SUPER_ID;
230 ios->offset = 0;
231 ios->kern_buff = fscb;
232 ios->cred = sbi->s_cred;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200233
Boaz Harrosh06886a52009-11-08 14:54:08 +0200234 ret = exofs_sbi_write(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200235 if (unlikely(ret)) {
Boaz Harrosh06886a52009-11-08 14:54:08 +0200236 EXOFS_ERR("%s: exofs_sbi_write failed.\n", __func__);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200237 goto out;
238 }
239 sb->s_dirt = 0;
240
241out:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200242 EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
243 exofs_put_io_state(ios);
Christoph Hellwigebc1ac12009-05-11 23:35:03 +0200244 unlock_super(sb);
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200245 return ret;
246}
247
248static void exofs_write_super(struct super_block *sb)
249{
250 if (!(sb->s_flags & MS_RDONLY))
251 exofs_sync_fs(sb, 1);
252 else
253 sb->s_dirt = 0;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200254}
255
Boaz Harrosh19fe2942009-09-03 20:38:02 +0300256static void _exofs_print_device(const char *msg, const char *dev_path,
257 struct osd_dev *od, u64 pid)
258{
259 const struct osd_dev_info *odi = osduld_device_info(od);
260
261 printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
262 msg, dev_path ?: "", odi->osdname, _LLU(pid));
263}
264
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200265void exofs_free_sbi(struct exofs_sb_info *sbi)
266{
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200267 while (sbi->layout.s_numdevs) {
268 int i = --sbi->layout.s_numdevs;
269 struct osd_dev *od = sbi->layout.s_ods[i];
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200270
271 if (od) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200272 sbi->layout.s_ods[i] = NULL;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200273 osduld_put_device(od);
274 }
275 }
276 kfree(sbi);
277}
278
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200279/*
280 * This function is called when the vfs is freeing the superblock. We just
281 * need to free our own part.
282 */
283static void exofs_put_super(struct super_block *sb)
284{
285 int num_pend;
286 struct exofs_sb_info *sbi = sb->s_fs_info;
287
Christoph Hellwig8c85e122009-04-28 18:00:26 +0200288 if (sb->s_dirt)
289 exofs_write_super(sb);
290
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200291 /* make sure there are no pending commands */
292 for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
293 num_pend = atomic_read(&sbi->s_curr_pending)) {
294 wait_queue_head_t wq;
295 init_waitqueue_head(&wq);
296 wait_event_timeout(wq,
297 (atomic_read(&sbi->s_curr_pending) == 0),
298 msecs_to_jiffies(100));
299 }
300
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200301 _exofs_print_device("Unmounting", NULL, sbi->layout.s_ods[0],
302 sbi->layout.s_pid);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200303
304 exofs_free_sbi(sbi);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200305 sb->s_fs_info = NULL;
306}
307
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200308static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
309 struct exofs_device_table *dt)
310{
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200311 u64 stripe_length;
312
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200313 sbi->data_map.odm_num_comps =
314 le32_to_cpu(dt->dt_data_map.cb_num_comps);
315 sbi->data_map.odm_stripe_unit =
316 le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
317 sbi->data_map.odm_group_width =
318 le32_to_cpu(dt->dt_data_map.cb_group_width);
319 sbi->data_map.odm_group_depth =
320 le32_to_cpu(dt->dt_data_map.cb_group_depth);
321 sbi->data_map.odm_mirror_cnt =
322 le32_to_cpu(dt->dt_data_map.cb_mirror_cnt);
323 sbi->data_map.odm_raid_algorithm =
324 le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);
325
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200326/* FIXME: Only raid0 for now. if not so, do not mount */
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200327 if (sbi->data_map.odm_num_comps != numdevs) {
328 EXOFS_ERR("odm_num_comps(%u) != numdevs(%u)\n",
329 sbi->data_map.odm_num_comps, numdevs);
330 return -EINVAL;
331 }
332 if (sbi->data_map.odm_raid_algorithm != PNFS_OSD_RAID_0) {
333 EXOFS_ERR("Only RAID_0 for now\n");
334 return -EINVAL;
335 }
336 if (0 != (numdevs % (sbi->data_map.odm_mirror_cnt + 1))) {
337 EXOFS_ERR("Data Map wrong, numdevs=%d mirrors=%d\n",
338 numdevs, sbi->data_map.odm_mirror_cnt);
339 return -EINVAL;
340 }
341
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200342 if (0 != (sbi->data_map.odm_stripe_unit & ~PAGE_MASK)) {
343 EXOFS_ERR("Stripe Unit(0x%llx)"
344 " must be Multples of PAGE_SIZE(0x%lx)\n",
345 _LLU(sbi->data_map.odm_stripe_unit), PAGE_SIZE);
346 return -EINVAL;
347 }
348
349 sbi->layout.stripe_unit = sbi->data_map.odm_stripe_unit;
350 sbi->layout.mirrors_p1 = sbi->data_map.odm_mirror_cnt + 1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200351
352 if (sbi->data_map.odm_group_width) {
353 sbi->layout.group_width = sbi->data_map.odm_group_width;
354 sbi->layout.group_depth = sbi->data_map.odm_group_depth;
355 if (!sbi->layout.group_depth) {
356 EXOFS_ERR("group_depth == 0 && group_width != 0\n");
357 return -EINVAL;
358 }
359 sbi->layout.group_count = sbi->data_map.odm_num_comps /
360 sbi->layout.mirrors_p1 /
361 sbi->data_map.odm_group_width;
362 } else {
363 if (sbi->data_map.odm_group_depth) {
364 printk(KERN_NOTICE "Warning: group_depth ignored "
365 "group_width == 0 && group_depth == %d\n",
366 sbi->data_map.odm_group_depth);
367 sbi->data_map.odm_group_depth = 0;
368 }
369 sbi->layout.group_width = sbi->data_map.odm_num_comps /
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200370 sbi->layout.mirrors_p1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200371 sbi->layout.group_depth = -1;
372 sbi->layout.group_count = 1;
373 }
374
375 stripe_length = (u64)sbi->layout.group_width * sbi->layout.stripe_unit;
376 if (stripe_length >= (1ULL << 32)) {
377 EXOFS_ERR("Total Stripe length(0x%llx)"
378 " >= 32bit is not supported\n", _LLU(stripe_length));
379 return -EINVAL;
380 }
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200381
382 return 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200383}
384
385/* @odi is valid only as long as @fscb_dev is valid */
386static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
387 struct osd_dev_info *odi)
388{
389 odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
390 memcpy(odi->systemid, dt_dev->systemid, odi->systemid_len);
391
392 odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
393 odi->osdname = dt_dev->osdname;
394
395 /* FIXME support long names. Will need a _put function */
396 if (dt_dev->long_name_offset)
397 return -EINVAL;
398
399 /* Make sure osdname is printable!
400 * mkexofs should give us space for a null-terminator else the
401 * device-table is invalid.
402 */
403 if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
404 odi->osdname_len = sizeof(dt_dev->osdname) - 1;
405 dt_dev->osdname[odi->osdname_len] = 0;
406
407 /* If it's all zeros something is bad we read past end-of-obj */
408 return !(odi->systemid_len || odi->osdname_len);
409}
410
411static int exofs_read_lookup_dev_table(struct exofs_sb_info **psbi,
412 unsigned table_count)
413{
414 struct exofs_sb_info *sbi = *psbi;
415 struct osd_dev *fscb_od;
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200416 struct osd_obj_id obj = {.partition = sbi->layout.s_pid,
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200417 .id = EXOFS_DEVTABLE_ID};
418 struct exofs_device_table *dt;
419 unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
420 sizeof(*dt);
421 unsigned numdevs, i;
422 int ret;
423
424 dt = kmalloc(table_bytes, GFP_KERNEL);
425 if (unlikely(!dt)) {
426 EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
427 table_bytes);
428 return -ENOMEM;
429 }
430
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200431 fscb_od = sbi->layout.s_ods[0];
432 sbi->layout.s_ods[0] = NULL;
433 sbi->layout.s_numdevs = 0;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200434 ret = exofs_read_kern(fscb_od, sbi->s_cred, &obj, 0, dt, table_bytes);
435 if (unlikely(ret)) {
436 EXOFS_ERR("ERROR: reading device table\n");
437 goto out;
438 }
439
440 numdevs = le64_to_cpu(dt->dt_num_devices);
441 if (unlikely(!numdevs)) {
442 ret = -EINVAL;
443 goto out;
444 }
445 WARN_ON(table_count != numdevs);
446
447 ret = _read_and_match_data_map(sbi, numdevs, dt);
448 if (unlikely(ret))
449 goto out;
450
451 if (likely(numdevs > 1)) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200452 unsigned size = numdevs * sizeof(sbi->layout.s_ods[0]);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200453
454 sbi = krealloc(sbi, sizeof(*sbi) + size, GFP_KERNEL);
455 if (unlikely(!sbi)) {
456 ret = -ENOMEM;
457 goto out;
458 }
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200459 memset(&sbi->layout.s_ods[1], 0,
460 size - sizeof(sbi->layout.s_ods[0]));
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200461 *psbi = sbi;
462 }
463
464 for (i = 0; i < numdevs; i++) {
465 struct exofs_fscb fscb;
466 struct osd_dev_info odi;
467 struct osd_dev *od;
468
469 if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
470 EXOFS_ERR("ERROR: Read all-zeros device entry\n");
471 ret = -EINVAL;
472 goto out;
473 }
474
475 printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
476 i, odi.osdname);
477
478 /* On all devices the device table is identical. The user can
479 * specify any one of the participating devices on the command
480 * line. We always keep them in device-table order.
481 */
482 if (fscb_od && osduld_device_same(fscb_od, &odi)) {
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200483 sbi->layout.s_ods[i] = fscb_od;
484 ++sbi->layout.s_numdevs;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200485 fscb_od = NULL;
486 continue;
487 }
488
489 od = osduld_info_lookup(&odi);
490 if (unlikely(IS_ERR(od))) {
491 ret = PTR_ERR(od);
492 EXOFS_ERR("ERROR: device requested is not found "
493 "osd_name-%s =>%d\n", odi.osdname, ret);
494 goto out;
495 }
496
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200497 sbi->layout.s_ods[i] = od;
498 ++sbi->layout.s_numdevs;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200499
500 /* Read the fscb of the other devices to make sure the FS
501 * partition is there.
502 */
503 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb,
504 sizeof(fscb));
505 if (unlikely(ret)) {
506 EXOFS_ERR("ERROR: Malformed participating device "
507 "error reading fscb osd_name-%s\n",
508 odi.osdname);
509 goto out;
510 }
511
512 /* TODO: verify other information is correct and FS-uuid
513 * matches. Benny what did you say about device table
514 * generation and old devices?
515 */
516 }
517
518out:
519 kfree(dt);
520 if (unlikely(!ret && fscb_od)) {
521 EXOFS_ERR(
522 "ERROR: Bad device-table container device not present\n");
523 osduld_put_device(fscb_od);
524 ret = -EINVAL;
525 }
526
527 return ret;
528}
529
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200530/*
531 * Read the superblock from the OSD and fill in the fields
532 */
533static int exofs_fill_super(struct super_block *sb, void *data, int silent)
534{
535 struct inode *root;
536 struct exofs_mountopt *opts = data;
537 struct exofs_sb_info *sbi; /*extended info */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200538 struct osd_dev *od; /* Master device */
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200539 struct exofs_fscb fscb; /*on-disk superblock info */
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200540 struct osd_obj_id obj;
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200541 unsigned table_count;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200542 int ret;
543
544 sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
545 if (!sbi)
546 return -ENOMEM;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200547
548 /* use mount options to fill superblock */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200549 od = osduld_path_lookup(opts->dev_name);
550 if (IS_ERR(od)) {
551 ret = PTR_ERR(od);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200552 goto free_sbi;
553 }
554
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200555 /* Default layout in case we do not have a device-table */
Boaz Harrosh5d952b82010-02-01 13:35:51 +0200556 sbi->layout.stripe_unit = PAGE_SIZE;
557 sbi->layout.mirrors_p1 = 1;
558 sbi->layout.group_width = 1;
Boaz Harrosh50a76fd2010-02-11 13:01:39 +0200559 sbi->layout.group_depth = -1;
560 sbi->layout.group_count = 1;
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200561 sbi->layout.s_ods[0] = od;
562 sbi->layout.s_numdevs = 1;
563 sbi->layout.s_pid = opts->pid;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200564 sbi->s_timeout = opts->timeout;
565
566 /* fill in some other data by hand */
567 memset(sb->s_id, 0, sizeof(sb->s_id));
568 strcpy(sb->s_id, "exofs");
569 sb->s_blocksize = EXOFS_BLKSIZE;
570 sb->s_blocksize_bits = EXOFS_BLKSHIFT;
571 sb->s_maxbytes = MAX_LFS_FILESIZE;
572 atomic_set(&sbi->s_curr_pending, 0);
573 sb->s_bdev = NULL;
574 sb->s_dev = 0;
575
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200576 obj.partition = sbi->layout.s_pid;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200577 obj.id = EXOFS_SUPER_ID;
578 exofs_make_credential(sbi->s_cred, &obj);
579
Boaz Harrosh06886a52009-11-08 14:54:08 +0200580 ret = exofs_read_kern(od, sbi->s_cred, &obj, 0, &fscb, sizeof(fscb));
581 if (unlikely(ret))
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200582 goto free_sbi;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200583
584 sb->s_magic = le16_to_cpu(fscb.s_magic);
585 sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
586 sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
587
588 /* make sure what we read from the object store is correct */
589 if (sb->s_magic != EXOFS_SUPER_MAGIC) {
590 if (!silent)
591 EXOFS_ERR("ERROR: Bad magic value\n");
592 ret = -EINVAL;
593 goto free_sbi;
594 }
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200595 if (le32_to_cpu(fscb.s_version) != EXOFS_FSCB_VER) {
596 EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
597 EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
598 ret = -EINVAL;
599 goto free_sbi;
600 }
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200601
602 /* start generation numbers from a random point */
603 get_random_bytes(&sbi->s_next_generation, sizeof(u32));
604 spin_lock_init(&sbi->s_next_gen_lock);
605
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200606 table_count = le64_to_cpu(fscb.s_dev_table_count);
607 if (table_count) {
608 ret = exofs_read_lookup_dev_table(&sbi, table_count);
609 if (unlikely(ret))
610 goto free_sbi;
611 }
612
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200613 /* set up operation vectors */
Boaz Harrosh06886a52009-11-08 14:54:08 +0200614 sb->s_fs_info = sbi;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200615 sb->s_op = &exofs_sops;
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200616 sb->s_export_op = &exofs_export_ops;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200617 root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
618 if (IS_ERR(root)) {
619 EXOFS_ERR("ERROR: exofs_iget failed\n");
620 ret = PTR_ERR(root);
621 goto free_sbi;
622 }
623 sb->s_root = d_alloc_root(root);
624 if (!sb->s_root) {
625 iput(root);
626 EXOFS_ERR("ERROR: get root inode failed\n");
627 ret = -ENOMEM;
628 goto free_sbi;
629 }
630
631 if (!S_ISDIR(root->i_mode)) {
632 dput(sb->s_root);
633 sb->s_root = NULL;
634 EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
635 root->i_mode);
636 ret = -EINVAL;
637 goto free_sbi;
638 }
639
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200640 _exofs_print_device("Mounting", opts->dev_name, sbi->layout.s_ods[0],
641 sbi->layout.s_pid);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200642 return 0;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200643
644free_sbi:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200645 EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200646 opts->dev_name, sbi->layout.s_pid, ret);
Boaz Harrosh04dc1e82009-11-16 16:03:05 +0200647 exofs_free_sbi(sbi);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200648 return ret;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200649}
650
651/*
652 * Set up the superblock (calls exofs_fill_super eventually)
653 */
654static int exofs_get_sb(struct file_system_type *type,
655 int flags, const char *dev_name,
656 void *data, struct vfsmount *mnt)
657{
658 struct exofs_mountopt opts;
659 int ret;
660
661 ret = parse_options(data, &opts);
662 if (ret)
663 return ret;
664
665 opts.dev_name = dev_name;
666 return get_sb_nodev(type, flags, &opts, exofs_fill_super, mnt);
667}
668
669/*
670 * Return information about the file system state in the buffer. This is used
671 * by the 'df' command, for example.
672 */
673static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
674{
675 struct super_block *sb = dentry->d_sb;
676 struct exofs_sb_info *sbi = sb->s_fs_info;
Boaz Harrosh06886a52009-11-08 14:54:08 +0200677 struct exofs_io_state *ios;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200678 struct osd_attr attrs[] = {
679 ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
680 OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
681 ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
682 OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
683 };
684 uint64_t capacity = ULLONG_MAX;
685 uint64_t used = ULLONG_MAX;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200686 uint8_t cred_a[OSD_CAP_LEN];
687 int ret;
688
Boaz Harrosh45d3abc2010-01-28 11:46:16 +0200689 ret = exofs_get_io_state(&sbi->layout, &ios);
Boaz Harrosh06886a52009-11-08 14:54:08 +0200690 if (ret) {
691 EXOFS_DBGMSG("exofs_get_io_state failed.\n");
692 return ret;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200693 }
694
Boaz Harrosh06886a52009-11-08 14:54:08 +0200695 exofs_make_credential(cred_a, &ios->obj);
696 ios->cred = sbi->s_cred;
697 ios->in_attr = attrs;
698 ios->in_attr_len = ARRAY_SIZE(attrs);
699
700 ret = exofs_sbi_read(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200701 if (unlikely(ret))
702 goto out;
703
Boaz Harrosh06886a52009-11-08 14:54:08 +0200704 ret = extract_attr_from_ios(ios, &attrs[0]);
Boaz Harroshcae012d2009-11-02 18:19:24 +0200705 if (likely(!ret)) {
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200706 capacity = get_unaligned_be64(attrs[0].val_ptr);
Boaz Harroshcae012d2009-11-02 18:19:24 +0200707 if (unlikely(!capacity))
708 capacity = ULLONG_MAX;
709 } else
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200710 EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
711
Boaz Harrosh06886a52009-11-08 14:54:08 +0200712 ret = extract_attr_from_ios(ios, &attrs[1]);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200713 if (likely(!ret))
714 used = get_unaligned_be64(attrs[1].val_ptr);
715 else
716 EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
717
718 /* fill in the stats buffer */
719 buf->f_type = EXOFS_SUPER_MAGIC;
720 buf->f_bsize = EXOFS_BLKSIZE;
Boaz Harroshcae012d2009-11-02 18:19:24 +0200721 buf->f_blocks = capacity >> 9;
722 buf->f_bfree = (capacity - used) >> 9;
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200723 buf->f_bavail = buf->f_bfree;
724 buf->f_files = sbi->s_numfiles;
725 buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
726 buf->f_namelen = EXOFS_NAME_LEN;
727
728out:
Boaz Harrosh06886a52009-11-08 14:54:08 +0200729 exofs_put_io_state(ios);
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200730 return ret;
731}
732
733static const struct super_operations exofs_sops = {
734 .alloc_inode = exofs_alloc_inode,
735 .destroy_inode = exofs_destroy_inode,
736 .write_inode = exofs_write_inode,
737 .delete_inode = exofs_delete_inode,
738 .put_super = exofs_put_super,
739 .write_super = exofs_write_super,
Christoph Hellwig80e09fb2009-06-08 10:03:58 +0200740 .sync_fs = exofs_sync_fs,
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200741 .statfs = exofs_statfs,
742};
743
744/******************************************************************************
Boaz Harrosh8cf74b32009-03-22 12:47:26 +0200745 * EXPORT OPERATIONS
746 *****************************************************************************/
747
748struct dentry *exofs_get_parent(struct dentry *child)
749{
750 unsigned long ino = exofs_parent_ino(child);
751
752 if (!ino)
753 return NULL;
754
755 return d_obtain_alias(exofs_iget(child->d_inode->i_sb, ino));
756}
757
758static struct inode *exofs_nfs_get_inode(struct super_block *sb,
759 u64 ino, u32 generation)
760{
761 struct inode *inode;
762
763 inode = exofs_iget(sb, ino);
764 if (IS_ERR(inode))
765 return ERR_CAST(inode);
766 if (generation && inode->i_generation != generation) {
767 /* we didn't find the right inode.. */
768 iput(inode);
769 return ERR_PTR(-ESTALE);
770 }
771 return inode;
772}
773
774static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
775 struct fid *fid, int fh_len, int fh_type)
776{
777 return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
778 exofs_nfs_get_inode);
779}
780
781static struct dentry *exofs_fh_to_parent(struct super_block *sb,
782 struct fid *fid, int fh_len, int fh_type)
783{
784 return generic_fh_to_parent(sb, fid, fh_len, fh_type,
785 exofs_nfs_get_inode);
786}
787
788static const struct export_operations exofs_export_ops = {
789 .fh_to_dentry = exofs_fh_to_dentry,
790 .fh_to_parent = exofs_fh_to_parent,
791 .get_parent = exofs_get_parent,
792};
793
794/******************************************************************************
Boaz Harroshba9e5e92008-10-28 16:11:41 +0200795 * INSMOD/RMMOD
796 *****************************************************************************/
797
798/*
799 * struct that describes this file system
800 */
801static struct file_system_type exofs_type = {
802 .owner = THIS_MODULE,
803 .name = "exofs",
804 .get_sb = exofs_get_sb,
805 .kill_sb = generic_shutdown_super,
806};
807
808static int __init init_exofs(void)
809{
810 int err;
811
812 err = init_inodecache();
813 if (err)
814 goto out;
815
816 err = register_filesystem(&exofs_type);
817 if (err)
818 goto out_d;
819
820 return 0;
821out_d:
822 destroy_inodecache();
823out:
824 return err;
825}
826
827static void __exit exit_exofs(void)
828{
829 unregister_filesystem(&exofs_type);
830 destroy_inodecache();
831}
832
833MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
834MODULE_DESCRIPTION("exofs");
835MODULE_LICENSE("GPL");
836
837module_init(init_exofs)
838module_exit(exit_exofs)