super.c 26.6 KB
Newer Older
1 2
/*
 * Copyright (C) 2005, 2006
B
Boaz Harrosh 已提交
3
 * Avishay Traeger (avishay@gmail.com)
4
 * Copyright (C) 2008, 2009
5
 * Boaz Harrosh <ooo@electrozaur.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
 *
 * Copyrights for code taken from ext2:
 *     Copyright (C) 1992, 1993, 1994, 1995
 *     Remy Card (card@masi.ibp.fr)
 *     Laboratoire MASI - Institut Blaise Pascal
 *     Universite Pierre et Marie Curie (Paris VI)
 *     from
 *     linux/fs/minix/inode.c
 *     Copyright (C) 1991, 1992  Linus Torvalds
 *
 * This file is part of exofs.
 *
 * exofs is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation.  Since it is based on ext2, and the only
 * valid version of GPL for the Linux kernel is version 2, the only valid
 * version of GPL for exofs is version 2.
 *
 * exofs is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with exofs; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <linux/string.h>
#include <linux/parser.h>
#include <linux/vfs.h>
#include <linux/random.h>
38
#include <linux/module.h>
B
Boaz Harrosh 已提交
39
#include <linux/exportfs.h>
40
#include <linux/slab.h>
J
Jeff Layton 已提交
41
#include <linux/iversion.h>
42 43 44

#include "exofs.h"

45 46
#define EXOFS_DBGMSG2(M...) do {} while (0)

47 48 49 50 51 52 53 54
/******************************************************************************
 * MOUNT OPTIONS
 *****************************************************************************/

/*
 * struct to hold what we get from mount options
 */
struct exofs_mountopt {
55
	bool is_osdname;
56 57 58 59 60 61 62 63
	const char *dev_name;
	uint64_t pid;
	int timeout;
};

/*
 * exofs-specific mount-time options.
 */
64
enum { Opt_name, Opt_pid, Opt_to, Opt_err };
65 66 67 68 69 70 71

/*
 * Our mount-time options.  These should ideally be 64-bit unsigned, but the
 * kernel's parsing functions do not currently support that.  32-bit should be
 * sufficient for most applications now.
 */
static match_table_t tokens = {
72
	{Opt_name, "osdname=%s"},
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
	{Opt_pid, "pid=%u"},
	{Opt_to, "to=%u"},
	{Opt_err, NULL}
};

/*
 * The main option parsing method.  Also makes sure that all of the mandatory
 * mount options were set.
 */
static int parse_options(char *options, struct exofs_mountopt *opts)
{
	char *p;
	substring_t args[MAX_OPT_ARGS];
	int option;
	bool s_pid = false;

	EXOFS_DBGMSG("parse_options %s\n", options);
	/* defaults */
	memset(opts, 0, sizeof(*opts));
	opts->timeout = BLK_DEFAULT_SG_TIMEOUT;

	while ((p = strsep(&options, ",")) != NULL) {
		int token;
		char str[32];

		if (!*p)
			continue;

		token = match_token(p, tokens, args);
		switch (token) {
103
		case Opt_name:
104
			kfree(opts->dev_name);
105 106 107 108 109 110 111
			opts->dev_name = match_strdup(&args[0]);
			if (unlikely(!opts->dev_name)) {
				EXOFS_ERR("Error allocating dev_name");
				return -ENOMEM;
			}
			opts->is_osdname = true;
			break;
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
		case Opt_pid:
			if (0 == match_strlcpy(str, &args[0], sizeof(str)))
				return -EINVAL;
			opts->pid = simple_strtoull(str, NULL, 0);
			if (opts->pid < EXOFS_MIN_PID) {
				EXOFS_ERR("Partition ID must be >= %u",
					  EXOFS_MIN_PID);
				return -EINVAL;
			}
			s_pid = 1;
			break;
		case Opt_to:
			if (match_int(&args[0], &option))
				return -EINVAL;
			if (option <= 0) {
M
Masanari Iida 已提交
127
				EXOFS_ERR("Timeout must be > 0");
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
				return -EINVAL;
			}
			opts->timeout = option * HZ;
			break;
		}
	}

	if (!s_pid) {
		EXOFS_ERR("Need to specify the following options:\n");
		EXOFS_ERR("    -o pid=pid_no_to_use\n");
		return -EINVAL;
	}

	return 0;
}

/******************************************************************************
 * INODE CACHE
 *****************************************************************************/

/*
 * Our inode cache.  Isn't it pretty?
 */
static struct kmem_cache *exofs_inode_cachep;

/*
 * Allocate an inode in the cache
 */
static struct inode *exofs_alloc_inode(struct super_block *sb)
{
	struct exofs_i_info *oi;

	oi = kmem_cache_alloc(exofs_inode_cachep, GFP_KERNEL);
	if (!oi)
		return NULL;

J
Jeff Layton 已提交
164
	inode_set_iversion(&oi->vfs_inode, 1);
165 166 167
	return &oi->vfs_inode;
}

N
Nick Piggin 已提交
168 169 170 171 172 173
static void exofs_i_callback(struct rcu_head *head)
{
	struct inode *inode = container_of(head, struct inode, i_rcu);
	kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
}

174 175 176 177 178
/*
 * Remove an inode from the cache
 */
static void exofs_destroy_inode(struct inode *inode)
{
N
Nick Piggin 已提交
179
	call_rcu(&inode->i_rcu, exofs_i_callback);
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
}

/*
 * Initialize the inode
 */
static void exofs_init_once(void *foo)
{
	struct exofs_i_info *oi = foo;

	inode_init_once(&oi->vfs_inode);
}

/*
 * Create and initialize the inode cache
 */
static int init_inodecache(void)
{
197
	exofs_inode_cachep = kmem_cache_create_usercopy("exofs_inode_cache",
198
				sizeof(struct exofs_i_info), 0,
199
				SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD |
200 201 202 203
				SLAB_ACCOUNT,
				offsetof(struct exofs_i_info, i_data),
				sizeof_field(struct exofs_i_info, i_data),
				exofs_init_once);
204 205 206 207 208 209 210 211 212 213
	if (exofs_inode_cachep == NULL)
		return -ENOMEM;
	return 0;
}

/*
 * Destroy the inode cache
 */
static void destroy_inodecache(void)
{
214 215 216 217 218
	/*
	 * Make sure all delayed rcu free inodes are flushed before we
	 * destroy cache.
	 */
	rcu_barrier();
219 220 221 222
	kmem_cache_destroy(exofs_inode_cachep);
}

/******************************************************************************
223
 * Some osd helpers
224
 *****************************************************************************/
225 226 227 228 229 230 231 232
void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj)
{
	osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
}

static int exofs_read_kern(struct osd_dev *od, u8 *cred, struct osd_obj_id *obj,
		    u64 offset, void *p, unsigned length)
{
233
	struct osd_request *or = osd_start_request(od);
234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
/*	struct osd_sense_info osi = {.key = 0};*/
	int ret;

	if (unlikely(!or)) {
		EXOFS_DBGMSG("%s: osd_start_request failed.\n", __func__);
		return -ENOMEM;
	}
	ret = osd_req_read_kern(or, obj, offset, p, length);
	if (unlikely(ret)) {
		EXOFS_DBGMSG("%s: osd_req_read_kern failed.\n", __func__);
		goto out;
	}

	ret = osd_finalize_request(or, 0, cred, NULL);
	if (unlikely(ret)) {
		EXOFS_DBGMSG("Failed to osd_finalize_request() => %d\n", ret);
		goto out;
	}

	ret = osd_execute_request(or);
	if (unlikely(ret))
		EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
	/* osd_req_decode_sense(or, ret); */

out:
	osd_end_request(or);
	EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
		      "length=0x%llx dev=%p ret=>%d\n",
		      _LLU(obj->id), _LLU(offset), _LLU(length), od, ret);
	return ret;
}

266 267 268 269 270 271 272 273 274 275
static const struct osd_attr g_attr_sb_stats = ATTR_DEF(
	EXOFS_APAGE_SB_DATA,
	EXOFS_ATTR_SB_STATS,
	sizeof(struct exofs_sb_stats));

static int __sbi_read_stats(struct exofs_sb_info *sbi)
{
	struct osd_attr attrs[] = {
		[0] = g_attr_sb_stats,
	};
276
	struct ore_io_state *ios;
277 278
	int ret;

279
	ret = ore_get_io_state(&sbi->layout, &sbi->oc, &ios);
280
	if (unlikely(ret)) {
281
		EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
282 283 284 285 286 287
		return ret;
	}

	ios->in_attr = attrs;
	ios->in_attr_len = ARRAY_SIZE(attrs);

288
	ret = ore_read(ios);
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
	if (unlikely(ret)) {
		EXOFS_ERR("Error reading super_block stats => %d\n", ret);
		goto out;
	}

	ret = extract_attr_from_ios(ios, &attrs[0]);
	if (ret) {
		EXOFS_ERR("%s: extract_attr of sb_stats failed\n", __func__);
		goto out;
	}
	if (attrs[0].len) {
		struct exofs_sb_stats *ess;

		if (unlikely(attrs[0].len != sizeof(*ess))) {
			EXOFS_ERR("%s: Wrong version of exofs_sb_stats "
				  "size(%d) != expected(%zd)\n",
				  __func__, attrs[0].len, sizeof(*ess));
			goto out;
		}

		ess = attrs[0].val_ptr;
		sbi->s_nextid = le64_to_cpu(ess->s_nextid);
		sbi->s_numfiles = le32_to_cpu(ess->s_numfiles);
	}

out:
315
	ore_put_io_state(ios);
316 317 318
	return ret;
}

319
static void stats_done(struct ore_io_state *ios, void *p)
320
{
321
	ore_put_io_state(ios);
322 323 324 325 326 327 328 329 330
	/* Good thanks nothing to do anymore */
}

/* Asynchronously write the stats attribute */
int exofs_sbi_write_stats(struct exofs_sb_info *sbi)
{
	struct osd_attr attrs[] = {
		[0] = g_attr_sb_stats,
	};
331
	struct ore_io_state *ios;
332 333
	int ret;

334
	ret = ore_get_io_state(&sbi->layout, &sbi->oc, &ios);
335
	if (unlikely(ret)) {
336
		EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
337 338 339 340 341 342 343
		return ret;
	}

	sbi->s_ess.s_nextid   = cpu_to_le64(sbi->s_nextid);
	sbi->s_ess.s_numfiles = cpu_to_le64(sbi->s_numfiles);
	attrs[0].val_ptr = &sbi->s_ess;

344

345 346 347 348 349
	ios->done = stats_done;
	ios->private = sbi;
	ios->out_attr = attrs;
	ios->out_attr_len = ARRAY_SIZE(attrs);

350
	ret = ore_write(ios);
351
	if (unlikely(ret)) {
352 353
		EXOFS_ERR("%s: ore_write failed.\n", __func__);
		ore_put_io_state(ios);
354 355 356 357 358
	}

	return ret;
}

359 360 361 362 363 364
/******************************************************************************
 * SUPERBLOCK FUNCTIONS
 *****************************************************************************/
static const struct super_operations exofs_sops;
static const struct export_operations exofs_export_ops;

365 366 367
/*
 * Write the superblock to the OSD
 */
368
static int exofs_sync_fs(struct super_block *sb, int wait)
369 370 371
{
	struct exofs_sb_info *sbi;
	struct exofs_fscb *fscb;
372
	struct ore_comp one_comp;
373
	struct ore_components oc;
374
	struct ore_io_state *ios;
C
Christoph Hellwig 已提交
375
	int ret = -ENOMEM;
376

377 378 379 380
	fscb = kmalloc(sizeof(*fscb), GFP_KERNEL);
	if (unlikely(!fscb))
		return -ENOMEM;

381
	sbi = sb->s_fs_info;
382

383 384 385 386 387 388 389
	/* NOTE: We no longer dirty the super_block anywhere in exofs. The
	 * reason we write the fscb here on unmount is so we can stay backwards
	 * compatible with fscb->s_version == 1. (What we are not compatible
	 * with is if a new version FS crashed and then we try to mount an old
	 * version). Otherwise the exofs_fscb is read-only from mkfs time. All
	 * the writeable info is set in exofs_sbi_write_stats() above.
	 */
390

391
	exofs_init_comps(&oc, &one_comp, sbi, EXOFS_SUPER_ID);
392

393
	ret = ore_get_io_state(&sbi->layout, &oc, &ios);
394
	if (unlikely(ret))
395 396
		goto out;

B
Boaz Harrosh 已提交
397
	ios->length = offsetof(struct exofs_fscb, s_dev_table_oid);
398
	memset(fscb, 0, ios->length);
399
	fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
400
	fscb->s_numfiles = cpu_to_le64(sbi->s_numfiles);
401 402
	fscb->s_magic = cpu_to_le16(sb->s_magic);
	fscb->s_newfs = 0;
B
Boaz Harrosh 已提交
403
	fscb->s_version = EXOFS_FSCB_VER;
404

405 406
	ios->offset = 0;
	ios->kern_buff = fscb;
407

408
	ret = ore_write(ios);
409
	if (unlikely(ret))
410
		EXOFS_ERR("%s: ore_write failed.\n", __func__);
411

412
out:
413
	EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
414
	ore_put_io_state(ios);
415
	kfree(fscb);
C
Christoph Hellwig 已提交
416 417 418
	return ret;
}

B
Boaz Harrosh 已提交
419 420 421 422 423 424 425 426 427
static void _exofs_print_device(const char *msg, const char *dev_path,
				struct osd_dev *od, u64 pid)
{
	const struct osd_dev_info *odi = osduld_device_info(od);

	printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
		msg, dev_path ?: "", odi->osdname, _LLU(pid));
}

428
static void exofs_free_sbi(struct exofs_sb_info *sbi)
B
Boaz Harrosh 已提交
429
{
430 431 432 433 434
	unsigned numdevs = sbi->oc.numdevs;

	while (numdevs) {
		unsigned i = --numdevs;
		struct osd_dev *od = ore_comp_dev(&sbi->oc, i);
B
Boaz Harrosh 已提交
435 436

		if (od) {
437
			ore_comp_set_dev(&sbi->oc, i, NULL);
B
Boaz Harrosh 已提交
438 439 440
			osduld_put_device(od);
		}
	}
441
	kfree(sbi->oc.ods);
B
Boaz Harrosh 已提交
442 443 444
	kfree(sbi);
}

445 446 447 448 449 450 451 452 453 454 455 456 457
/*
 * This function is called when the vfs is freeing the superblock.  We just
 * need to free our own part.
 */
static void exofs_put_super(struct super_block *sb)
{
	int num_pend;
	struct exofs_sb_info *sbi = sb->s_fs_info;

	/* make sure there are no pending commands */
	for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
	     num_pend = atomic_read(&sbi->s_curr_pending)) {
		wait_queue_head_t wq;
458 459 460 461

		printk(KERN_NOTICE "%s: !!Pending operations in flight. "
		       "This is a BUG. please report to osd-dev@open-osd.org\n",
		       __func__);
462 463 464 465 466 467
		init_waitqueue_head(&wq);
		wait_event_timeout(wq,
				  (atomic_read(&sbi->s_curr_pending) == 0),
				  msecs_to_jiffies(100));
	}

468
	_exofs_print_device("Unmounting", NULL, ore_comp_dev(&sbi->oc, 0),
469
			    sbi->one_comp.obj.partition);
B
Boaz Harrosh 已提交
470

471
	exofs_sysfs_sb_del(sbi);
B
Boaz Harrosh 已提交
472
	exofs_free_sbi(sbi);
473 474 475
	sb->s_fs_info = NULL;
}

B
Boaz Harrosh 已提交
476 477 478
static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
				    struct exofs_device_table *dt)
{
479
	int ret;
B
Boaz Harrosh 已提交
480

481
	sbi->layout.stripe_unit =
B
Boaz Harrosh 已提交
482
				le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
483
	sbi->layout.group_width =
B
Boaz Harrosh 已提交
484
				le32_to_cpu(dt->dt_data_map.cb_group_width);
485
	sbi->layout.group_depth =
B
Boaz Harrosh 已提交
486
				le32_to_cpu(dt->dt_data_map.cb_group_depth);
487 488 489
	sbi->layout.mirrors_p1  =
				le32_to_cpu(dt->dt_data_map.cb_mirror_cnt) + 1;
	sbi->layout.raid_algorithm  =
B
Boaz Harrosh 已提交
490 491
				le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);

492
	ret = ore_verify_layout(numdevs, &sbi->layout);
B
Boaz Harrosh 已提交
493

B
Boaz Harrosh 已提交
494 495 496 497 498 499 500 501
	EXOFS_DBGMSG("exofs: layout: "
		"num_comps=%u stripe_unit=0x%x group_width=%u "
		"group_depth=0x%llx mirrors_p1=%u raid_algorithm=%u\n",
		numdevs,
		sbi->layout.stripe_unit,
		sbi->layout.group_width,
		_LLU(sbi->layout.group_depth),
		sbi->layout.mirrors_p1,
502
		sbi->layout.raid_algorithm);
503
	return ret;
B
Boaz Harrosh 已提交
504 505
}

506
static unsigned __ra_pages(struct ore_layout *layout)
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522
{
	const unsigned _MIN_RA = 32; /* min 128K read-ahead */
	unsigned ra_pages = layout->group_width * layout->stripe_unit /
				PAGE_SIZE;
	unsigned max_io_pages = exofs_max_io_pages(layout, ~0);

	ra_pages *= 2; /* two stripes */
	if (ra_pages < _MIN_RA)
		ra_pages = roundup(_MIN_RA, ra_pages / 2);

	if (ra_pages > max_io_pages)
		ra_pages = max_io_pages;

	return ra_pages;
}

B
Boaz Harrosh 已提交
523 524 525 526 527
/* @odi is valid only as long as @fscb_dev is valid */
static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
			     struct osd_dev_info *odi)
{
	odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
D
Dan Carpenter 已提交
528 529
	if (likely(odi->systemid_len))
		memcpy(odi->systemid, dt_dev->systemid, OSD_SYSTEMID_LEN);
B
Boaz Harrosh 已提交
530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549

	odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
	odi->osdname = dt_dev->osdname;

	/* FIXME support long names. Will need a _put function */
	if (dt_dev->long_name_offset)
		return -EINVAL;

	/* Make sure osdname is printable!
	 * mkexofs should give us space for a null-terminator else the
	 * device-table is invalid.
	 */
	if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
		odi->osdname_len = sizeof(dt_dev->osdname) - 1;
	dt_dev->osdname[odi->osdname_len] = 0;

	/* If it's all zeros something is bad we read past end-of-obj */
	return !(odi->systemid_len || odi->osdname_len);
}

550
static int __alloc_dev_table(struct exofs_sb_info *sbi, unsigned numdevs,
551 552
		      struct exofs_dev **peds)
{
K
Kees Cook 已提交
553 554 555 556
	/* Twice bigger table: See exofs_init_comps() and comment at
	 * exofs_read_lookup_dev_table()
	 */
	const size_t numores = numdevs * 2 - 1;
557 558 559
	struct exofs_dev *eds;
	unsigned i;

K
Kees Cook 已提交
560 561 562
	sbi->oc.ods = kzalloc(numores * sizeof(struct ore_dev *) +
			      numdevs * sizeof(struct exofs_dev), GFP_KERNEL);
	if (unlikely(!sbi->oc.ods)) {
563
		EXOFS_ERR("ERROR: failed allocating Device array[%d]\n",
564 565 566 567
			  numdevs);
		return -ENOMEM;
	}

K
Kees Cook 已提交
568 569 570
	/* Start of allocated struct exofs_dev entries */
	*peds = eds = (void *)sbi->oc.ods[numores];
	/* Initialize pointers into struct exofs_dev */
571
	for (i = 0; i < numdevs; ++i)
K
Kees Cook 已提交
572
		sbi->oc.ods[i] = &eds[i].ored;
573 574 575
	return 0;
}

B
Boaz Harrosh 已提交
576 577
static int exofs_read_lookup_dev_table(struct exofs_sb_info *sbi,
				       struct osd_dev *fscb_od,
B
Boaz Harrosh 已提交
578 579
				       unsigned table_count)
{
580
	struct ore_comp comp;
B
Boaz Harrosh 已提交
581
	struct exofs_device_table *dt;
582
	struct exofs_dev *eds;
B
Boaz Harrosh 已提交
583 584 585 586 587 588 589 590 591 592 593 594
	unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
					     sizeof(*dt);
	unsigned numdevs, i;
	int ret;

	dt = kmalloc(table_bytes, GFP_KERNEL);
	if (unlikely(!dt)) {
		EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
			  table_bytes);
		return -ENOMEM;
	}

595
	sbi->oc.numdevs = 0;
596 597 598 599 600 601 602

	comp.obj.partition = sbi->one_comp.obj.partition;
	comp.obj.id = EXOFS_DEVTABLE_ID;
	exofs_make_credential(comp.cred, &comp.obj);

	ret = exofs_read_kern(fscb_od, comp.cred, &comp.obj, 0, dt,
			      table_bytes);
B
Boaz Harrosh 已提交
603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
	if (unlikely(ret)) {
		EXOFS_ERR("ERROR: reading device table\n");
		goto out;
	}

	numdevs = le64_to_cpu(dt->dt_num_devices);
	if (unlikely(!numdevs)) {
		ret = -EINVAL;
		goto out;
	}
	WARN_ON(table_count != numdevs);

	ret = _read_and_match_data_map(sbi, numdevs, dt);
	if (unlikely(ret))
		goto out;

619 620 621 622 623 624 625 626 627 628
	ret = __alloc_dev_table(sbi, numdevs, &eds);
	if (unlikely(ret))
		goto out;
	/* exofs round-robins the device table view according to inode
	 * number. We hold a: twice bigger table hence inodes can point
	 * to any device and have a sequential view of the table
	 * starting at this device. See exofs_init_comps()
	 */
	memcpy(&sbi->oc.ods[numdevs], &sbi->oc.ods[0],
		(numdevs - 1) * sizeof(sbi->oc.ods[0]));
B
Boaz Harrosh 已提交
629

630 631 632 633 634 635
	/* create sysfs subdir under which we put the device table
	 * And cluster layout. A Superblock is identified by the string:
	 *	"dev[0].osdname"_"pid"
	 */
	exofs_sysfs_sb_add(sbi, &dt->dt_dev_table[0]);

B
Boaz Harrosh 已提交
636 637 638 639 640 641 642 643 644 645 646 647 648 649
	for (i = 0; i < numdevs; i++) {
		struct exofs_fscb fscb;
		struct osd_dev_info odi;
		struct osd_dev *od;

		if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
			EXOFS_ERR("ERROR: Read all-zeros device entry\n");
			ret = -EINVAL;
			goto out;
		}

		printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
		       i, odi.osdname);

650 651 652
		/* the exofs id is currently the table index */
		eds[i].did = i;

B
Boaz Harrosh 已提交
653 654 655 656 657
		/* On all devices the device table is identical. The user can
		 * specify any one of the participating devices on the command
		 * line. We always keep them in device-table order.
		 */
		if (fscb_od && osduld_device_same(fscb_od, &odi)) {
658
			eds[i].ored.od = fscb_od;
659
			++sbi->oc.numdevs;
B
Boaz Harrosh 已提交
660
			fscb_od = NULL;
661
			exofs_sysfs_odev_add(&eds[i], sbi);
B
Boaz Harrosh 已提交
662 663 664 665
			continue;
		}

		od = osduld_info_lookup(&odi);
666
		if (IS_ERR(od)) {
B
Boaz Harrosh 已提交
667 668 669 670 671 672
			ret = PTR_ERR(od);
			EXOFS_ERR("ERROR: device requested is not found "
				  "osd_name-%s =>%d\n", odi.osdname, ret);
			goto out;
		}

673
		eds[i].ored.od = od;
674
		++sbi->oc.numdevs;
B
Boaz Harrosh 已提交
675 676 677 678

		/* Read the fscb of the other devices to make sure the FS
		 * partition is there.
		 */
679
		ret = exofs_read_kern(od, comp.cred, &comp.obj, 0, &fscb,
B
Boaz Harrosh 已提交
680 681 682 683 684 685 686
				      sizeof(fscb));
		if (unlikely(ret)) {
			EXOFS_ERR("ERROR: Malformed participating device "
				  "error reading fscb osd_name-%s\n",
				  odi.osdname);
			goto out;
		}
687
		exofs_sysfs_odev_add(&eds[i], sbi);
B
Boaz Harrosh 已提交
688 689 690 691 692 693 694 695 696

		/* TODO: verify other information is correct and FS-uuid
		 *	 matches. Benny what did you say about device table
		 *	 generation and old devices?
		 */
	}

out:
	kfree(dt);
697
	if (unlikely(fscb_od && !ret)) {
698 699 700 701
			EXOFS_ERR("ERROR: Bad device-table container device not present\n");
			osduld_put_device(fscb_od);
			return -EINVAL;
	}
B
Boaz Harrosh 已提交
702 703 704
	return ret;
}

705 706 707 708 709 710 711 712
/*
 * Read the superblock from the OSD and fill in the fields
 */
static int exofs_fill_super(struct super_block *sb, void *data, int silent)
{
	struct inode *root;
	struct exofs_mountopt *opts = data;
	struct exofs_sb_info *sbi;	/*extended info                  */
713
	struct osd_dev *od;		/* Master device                 */
714
	struct exofs_fscb fscb;		/*on-disk superblock info        */
715
	struct ore_comp comp;
B
Boaz Harrosh 已提交
716
	unsigned table_count;
717 718 719 720 721 722 723
	int ret;

	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
	if (!sbi)
		return -ENOMEM;

	/* use mount options to fill superblock */
724 725 726 727 728 729
	if (opts->is_osdname) {
		struct osd_dev_info odi = {.systemid_len = 0};

		odi.osdname_len = strlen(opts->dev_name);
		odi.osdname = (u8 *)opts->dev_name;
		od = osduld_info_lookup(&odi);
730 731
		kfree(opts->dev_name);
		opts->dev_name = NULL;
732 733 734
	} else {
		od = osduld_path_lookup(opts->dev_name);
	}
735
	if (IS_ERR(od)) {
736
		ret = -EINVAL;
737 738 739
		goto free_sbi;
	}

740
	/* Default layout in case we do not have a device-table */
B
Boaz Harrosh 已提交
741 742 743
	sbi->layout.stripe_unit = PAGE_SIZE;
	sbi->layout.mirrors_p1 = 1;
	sbi->layout.group_width = 1;
B
Boaz Harrosh 已提交
744 745
	sbi->layout.group_depth = -1;
	sbi->layout.group_count = 1;
746 747
	sbi->s_timeout = opts->timeout;

748 749 750
	sbi->one_comp.obj.partition = opts->pid;
	sbi->one_comp.obj.id = 0;
	exofs_make_credential(sbi->one_comp.cred, &sbi->one_comp.obj);
751 752
	sbi->oc.single_comp = EC_SINGLE_COMP;
	sbi->oc.comps = &sbi->one_comp;
753

754 755 756 757 758 759
	/* fill in some other data by hand */
	memset(sb->s_id, 0, sizeof(sb->s_id));
	strcpy(sb->s_id, "exofs");
	sb->s_blocksize = EXOFS_BLKSIZE;
	sb->s_blocksize_bits = EXOFS_BLKSHIFT;
	sb->s_maxbytes = MAX_LFS_FILESIZE;
760
	sb->s_max_links = EXOFS_LINK_MAX;
761 762 763 764
	atomic_set(&sbi->s_curr_pending, 0);
	sb->s_bdev = NULL;
	sb->s_dev = 0;

765 766 767
	comp.obj.partition = sbi->one_comp.obj.partition;
	comp.obj.id = EXOFS_SUPER_ID;
	exofs_make_credential(comp.cred, &comp.obj);
768

769
	ret = exofs_read_kern(od, comp.cred, &comp.obj, 0, &fscb, sizeof(fscb));
770
	if (unlikely(ret))
771 772 773
		goto free_sbi;

	sb->s_magic = le16_to_cpu(fscb.s_magic);
774
	/* NOTE: we read below to be backward compatible with old versions */
775 776 777 778 779 780 781 782 783 784
	sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
	sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);

	/* make sure what we read from the object store is correct */
	if (sb->s_magic != EXOFS_SUPER_MAGIC) {
		if (!silent)
			EXOFS_ERR("ERROR: Bad magic value\n");
		ret = -EINVAL;
		goto free_sbi;
	}
785
	if (le32_to_cpu(fscb.s_version) > EXOFS_FSCB_VER) {
B
Boaz Harrosh 已提交
786 787 788 789 790
		EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
			  EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
		ret = -EINVAL;
		goto free_sbi;
	}
791 792 793 794 795

	/* start generation numbers from a random point */
	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
	spin_lock_init(&sbi->s_next_gen_lock);

B
Boaz Harrosh 已提交
796 797
	table_count = le64_to_cpu(fscb.s_dev_table_count);
	if (table_count) {
B
Boaz Harrosh 已提交
798
		ret = exofs_read_lookup_dev_table(sbi, od, table_count);
B
Boaz Harrosh 已提交
799 800
		if (unlikely(ret))
			goto free_sbi;
B
Boaz Harrosh 已提交
801
	} else {
802 803 804 805 806 807 808
		struct exofs_dev *eds;

		ret = __alloc_dev_table(sbi, 1, &eds);
		if (unlikely(ret))
			goto free_sbi;

		ore_comp_set_dev(&sbi->oc, 0, od);
809
		sbi->oc.numdevs = 1;
B
Boaz Harrosh 已提交
810 811
	}

812 813
	__sbi_read_stats(sbi);

814
	/* set up operation vectors */
815 816 817 818 819 820
	ret = super_setup_bdi(sb);
	if (ret) {
		EXOFS_DBGMSG("Failed to super_setup_bdi\n");
		goto free_sbi;
	}
	sb->s_bdi->ra_pages = __ra_pages(&sbi->layout);
821
	sb->s_fs_info = sbi;
822
	sb->s_op = &exofs_sops;
B
Boaz Harrosh 已提交
823
	sb->s_export_op = &exofs_export_ops;
824 825 826 827 828 829
	root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
	if (IS_ERR(root)) {
		EXOFS_ERR("ERROR: exofs_iget failed\n");
		ret = PTR_ERR(root);
		goto free_sbi;
	}
830
	sb->s_root = d_make_root(root);
831 832 833 834 835 836 837 838 839 840 841 842 843 844 845
	if (!sb->s_root) {
		EXOFS_ERR("ERROR: get root inode failed\n");
		ret = -ENOMEM;
		goto free_sbi;
	}

	if (!S_ISDIR(root->i_mode)) {
		dput(sb->s_root);
		sb->s_root = NULL;
		EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
		       root->i_mode);
		ret = -EINVAL;
		goto free_sbi;
	}

846
	exofs_sysfs_dbg_print();
847 848
	_exofs_print_device("Mounting", opts->dev_name,
			    ore_comp_dev(&sbi->oc, 0),
849
			    sbi->one_comp.obj.partition);
850
	return 0;
851 852

free_sbi:
853
	EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
854
		  opts->dev_name, sbi->one_comp.obj.partition, ret);
B
Boaz Harrosh 已提交
855
	exofs_free_sbi(sbi);
856
	return ret;
857 858 859 860 861
}

/*
 * Set up the superblock (calls exofs_fill_super eventually)
 */
A
Al Viro 已提交
862
static struct dentry *exofs_mount(struct file_system_type *type,
863
			  int flags, const char *dev_name,
A
Al Viro 已提交
864
			  void *data)
865 866 867 868 869
{
	struct exofs_mountopt opts;
	int ret;

	ret = parse_options(data, &opts);
870 871
	if (ret) {
		kfree(opts.dev_name);
A
Al Viro 已提交
872
		return ERR_PTR(ret);
873
	}
874

875 876
	if (!opts.dev_name)
		opts.dev_name = dev_name;
A
Al Viro 已提交
877
	return mount_nodev(type, flags, &opts, exofs_fill_super);
878 879 880 881 882 883 884 885 886 887
}

/*
 * Return information about the file system state in the buffer.  This is used
 * by the 'df' command, for example.
 */
static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
{
	struct super_block *sb = dentry->d_sb;
	struct exofs_sb_info *sbi = sb->s_fs_info;
888
	struct ore_io_state *ios;
889 890 891 892 893 894 895 896 897 898
	struct osd_attr attrs[] = {
		ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
			OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
		ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
			OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
	};
	uint64_t capacity = ULLONG_MAX;
	uint64_t used = ULLONG_MAX;
	int ret;

899
	ret = ore_get_io_state(&sbi->layout, &sbi->oc, &ios);
900
	if (ret) {
901
		EXOFS_DBGMSG("ore_get_io_state failed.\n");
902
		return ret;
903 904
	}

905 906 907
	ios->in_attr = attrs;
	ios->in_attr_len = ARRAY_SIZE(attrs);

908
	ret = ore_read(ios);
909 910 911
	if (unlikely(ret))
		goto out;

912
	ret = extract_attr_from_ios(ios, &attrs[0]);
913
	if (likely(!ret)) {
914
		capacity = get_unaligned_be64(attrs[0].val_ptr);
915 916 917
		if (unlikely(!capacity))
			capacity = ULLONG_MAX;
	} else
918 919
		EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");

920
	ret = extract_attr_from_ios(ios, &attrs[1]);
921 922 923 924 925 926 927 928
	if (likely(!ret))
		used = get_unaligned_be64(attrs[1].val_ptr);
	else
		EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");

	/* fill in the stats buffer */
	buf->f_type = EXOFS_SUPER_MAGIC;
	buf->f_bsize = EXOFS_BLKSIZE;
929 930
	buf->f_blocks = capacity >> 9;
	buf->f_bfree = (capacity - used) >> 9;
931 932 933 934 935 936
	buf->f_bavail = buf->f_bfree;
	buf->f_files = sbi->s_numfiles;
	buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
	buf->f_namelen = EXOFS_NAME_LEN;

out:
937
	ore_put_io_state(ios);
938 939 940 941 942 943 944
	return ret;
}

static const struct super_operations exofs_sops = {
	.alloc_inode    = exofs_alloc_inode,
	.destroy_inode  = exofs_destroy_inode,
	.write_inode    = exofs_write_inode,
A
Al Viro 已提交
945
	.evict_inode    = exofs_evict_inode,
946
	.put_super      = exofs_put_super,
C
Christoph Hellwig 已提交
947
	.sync_fs	= exofs_sync_fs,
948 949 950
	.statfs         = exofs_statfs,
};

B
Boaz Harrosh 已提交
951 952 953 954
/******************************************************************************
 * EXPORT OPERATIONS
 *****************************************************************************/

955
static struct dentry *exofs_get_parent(struct dentry *child)
B
Boaz Harrosh 已提交
956 957 958 959
{
	unsigned long ino = exofs_parent_ino(child);

	if (!ino)
A
Al Viro 已提交
960
		return ERR_PTR(-ESTALE);
B
Boaz Harrosh 已提交
961

962
	return d_obtain_alias(exofs_iget(child->d_sb, ino));
B
Boaz Harrosh 已提交
963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000
}

static struct inode *exofs_nfs_get_inode(struct super_block *sb,
		u64 ino, u32 generation)
{
	struct inode *inode;

	inode = exofs_iget(sb, ino);
	if (IS_ERR(inode))
		return ERR_CAST(inode);
	if (generation && inode->i_generation != generation) {
		/* we didn't find the right inode.. */
		iput(inode);
		return ERR_PTR(-ESTALE);
	}
	return inode;
}

static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
				struct fid *fid, int fh_len, int fh_type)
{
	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
				    exofs_nfs_get_inode);
}

static struct dentry *exofs_fh_to_parent(struct super_block *sb,
				struct fid *fid, int fh_len, int fh_type)
{
	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
				    exofs_nfs_get_inode);
}

static const struct export_operations exofs_export_ops = {
	.fh_to_dentry = exofs_fh_to_dentry,
	.fh_to_parent = exofs_fh_to_parent,
	.get_parent = exofs_get_parent,
};

1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
/******************************************************************************
 * INSMOD/RMMOD
 *****************************************************************************/

/*
 * struct that describes this file system
 */
static struct file_system_type exofs_type = {
	.owner          = THIS_MODULE,
	.name           = "exofs",
A
Al Viro 已提交
1011
	.mount          = exofs_mount,
1012 1013
	.kill_sb        = generic_shutdown_super,
};
1014
MODULE_ALIAS_FS("exofs");
1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027

static int __init init_exofs(void)
{
	int err;

	err = init_inodecache();
	if (err)
		goto out;

	err = register_filesystem(&exofs_type);
	if (err)
		goto out_d;

1028 1029 1030
	/* We don't fail if sysfs creation failed */
	exofs_sysfs_init();

1031 1032 1033 1034 1035 1036 1037 1038 1039
	return 0;
out_d:
	destroy_inodecache();
out:
	return err;
}

static void __exit exit_exofs(void)
{
1040
	exofs_sysfs_uninit();
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	unregister_filesystem(&exofs_type);
	destroy_inodecache();
}

MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
MODULE_DESCRIPTION("exofs");
MODULE_LICENSE("GPL");

module_init(init_exofs)
module_exit(exit_exofs)