1 /*
2  * Copyright (C) 2005, 2006
3  * Avishay Traeger (avishay@gmail.com)
4  * Copyright (C) 2008, 2009
5  * Boaz Harrosh <ooo@electrozaur.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>
38 #include <linux/module.h>
39 #include <linux/exportfs.h>
40 #include <linux/slab.h>
41 #include <linux/iversion.h>
42 
43 #include "exofs.h"
44 
45 #define EXOFS_DBGMSG2(M...) do {} while (0)
46 
47 /******************************************************************************
48  * MOUNT OPTIONS
49  *****************************************************************************/
50 
51 /*
52  * struct to hold what we get from mount options
53  */
54 struct exofs_mountopt {
55 	bool is_osdname;
56 	const char *dev_name;
57 	uint64_t pid;
58 	int timeout;
59 };
60 
61 /*
62  * exofs-specific mount-time options.
63  */
64 enum { Opt_name, Opt_pid, Opt_to, Opt_err };
65 
66 /*
67  * Our mount-time options.  These should ideally be 64-bit unsigned, but the
68  * kernel's parsing functions do not currently support that.  32-bit should be
69  * sufficient for most applications now.
70  */
71 static match_table_t tokens = {
72 	{Opt_name, "osdname=%s"},
73 	{Opt_pid, "pid=%u"},
74 	{Opt_to, "to=%u"},
75 	{Opt_err, NULL}
76 };
77 
78 /*
79  * The main option parsing method.  Also makes sure that all of the mandatory
80  * mount options were set.
81  */
parse_options(char * options,struct exofs_mountopt * opts)82 static int parse_options(char *options, struct exofs_mountopt *opts)
83 {
84 	char *p;
85 	substring_t args[MAX_OPT_ARGS];
86 	int option;
87 	bool s_pid = false;
88 
89 	EXOFS_DBGMSG("parse_options %s\n", options);
90 	/* defaults */
91 	memset(opts, 0, sizeof(*opts));
92 	opts->timeout = BLK_DEFAULT_SG_TIMEOUT;
93 
94 	while ((p = strsep(&options, ",")) != NULL) {
95 		int token;
96 		char str[32];
97 
98 		if (!*p)
99 			continue;
100 
101 		token = match_token(p, tokens, args);
102 		switch (token) {
103 		case Opt_name:
104 			opts->dev_name = match_strdup(&args[0]);
105 			if (unlikely(!opts->dev_name)) {
106 				EXOFS_ERR("Error allocating dev_name");
107 				return -ENOMEM;
108 			}
109 			opts->is_osdname = true;
110 			break;
111 		case Opt_pid:
112 			if (0 == match_strlcpy(str, &args[0], sizeof(str)))
113 				return -EINVAL;
114 			opts->pid = simple_strtoull(str, NULL, 0);
115 			if (opts->pid < EXOFS_MIN_PID) {
116 				EXOFS_ERR("Partition ID must be >= %u",
117 					  EXOFS_MIN_PID);
118 				return -EINVAL;
119 			}
120 			s_pid = 1;
121 			break;
122 		case Opt_to:
123 			if (match_int(&args[0], &option))
124 				return -EINVAL;
125 			if (option <= 0) {
126 				EXOFS_ERR("Timeout must be > 0");
127 				return -EINVAL;
128 			}
129 			opts->timeout = option * HZ;
130 			break;
131 		}
132 	}
133 
134 	if (!s_pid) {
135 		EXOFS_ERR("Need to specify the following options:\n");
136 		EXOFS_ERR("    -o pid=pid_no_to_use\n");
137 		return -EINVAL;
138 	}
139 
140 	return 0;
141 }
142 
143 /******************************************************************************
144  * INODE CACHE
145  *****************************************************************************/
146 
147 /*
148  * Our inode cache.  Isn't it pretty?
149  */
150 static struct kmem_cache *exofs_inode_cachep;
151 
152 /*
153  * Allocate an inode in the cache
154  */
exofs_alloc_inode(struct super_block * sb)155 static struct inode *exofs_alloc_inode(struct super_block *sb)
156 {
157 	struct exofs_i_info *oi;
158 
159 	oi = kmem_cache_alloc(exofs_inode_cachep, GFP_KERNEL);
160 	if (!oi)
161 		return NULL;
162 
163 	inode_set_iversion(&oi->vfs_inode, 1);
164 	return &oi->vfs_inode;
165 }
166 
exofs_i_callback(struct rcu_head * head)167 static void exofs_i_callback(struct rcu_head *head)
168 {
169 	struct inode *inode = container_of(head, struct inode, i_rcu);
170 	kmem_cache_free(exofs_inode_cachep, exofs_i(inode));
171 }
172 
173 /*
174  * Remove an inode from the cache
175  */
exofs_destroy_inode(struct inode * inode)176 static void exofs_destroy_inode(struct inode *inode)
177 {
178 	call_rcu(&inode->i_rcu, exofs_i_callback);
179 }
180 
181 /*
182  * Initialize the inode
183  */
exofs_init_once(void * foo)184 static void exofs_init_once(void *foo)
185 {
186 	struct exofs_i_info *oi = foo;
187 
188 	inode_init_once(&oi->vfs_inode);
189 }
190 
191 /*
192  * Create and initialize the inode cache
193  */
init_inodecache(void)194 static int init_inodecache(void)
195 {
196 	exofs_inode_cachep = kmem_cache_create_usercopy("exofs_inode_cache",
197 				sizeof(struct exofs_i_info), 0,
198 				SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD |
199 				SLAB_ACCOUNT,
200 				offsetof(struct exofs_i_info, i_data),
201 				sizeof_field(struct exofs_i_info, i_data),
202 				exofs_init_once);
203 	if (exofs_inode_cachep == NULL)
204 		return -ENOMEM;
205 	return 0;
206 }
207 
208 /*
209  * Destroy the inode cache
210  */
destroy_inodecache(void)211 static void destroy_inodecache(void)
212 {
213 	/*
214 	 * Make sure all delayed rcu free inodes are flushed before we
215 	 * destroy cache.
216 	 */
217 	rcu_barrier();
218 	kmem_cache_destroy(exofs_inode_cachep);
219 }
220 
221 /******************************************************************************
222  * Some osd helpers
223  *****************************************************************************/
exofs_make_credential(u8 cred_a[OSD_CAP_LEN],const struct osd_obj_id * obj)224 void exofs_make_credential(u8 cred_a[OSD_CAP_LEN], const struct osd_obj_id *obj)
225 {
226 	osd_sec_init_nosec_doall_caps(cred_a, obj, false, true);
227 }
228 
exofs_read_kern(struct osd_dev * od,u8 * cred,struct osd_obj_id * obj,u64 offset,void * p,unsigned length)229 static int exofs_read_kern(struct osd_dev *od, u8 *cred, struct osd_obj_id *obj,
230 		    u64 offset, void *p, unsigned length)
231 {
232 	struct osd_request *or = osd_start_request(od);
233 /*	struct osd_sense_info osi = {.key = 0};*/
234 	int ret;
235 
236 	if (unlikely(!or)) {
237 		EXOFS_DBGMSG("%s: osd_start_request failed.\n", __func__);
238 		return -ENOMEM;
239 	}
240 	ret = osd_req_read_kern(or, obj, offset, p, length);
241 	if (unlikely(ret)) {
242 		EXOFS_DBGMSG("%s: osd_req_read_kern failed.\n", __func__);
243 		goto out;
244 	}
245 
246 	ret = osd_finalize_request(or, 0, cred, NULL);
247 	if (unlikely(ret)) {
248 		EXOFS_DBGMSG("Failed to osd_finalize_request() => %d\n", ret);
249 		goto out;
250 	}
251 
252 	ret = osd_execute_request(or);
253 	if (unlikely(ret))
254 		EXOFS_DBGMSG("osd_execute_request() => %d\n", ret);
255 	/* osd_req_decode_sense(or, ret); */
256 
257 out:
258 	osd_end_request(or);
259 	EXOFS_DBGMSG2("read_kern(0x%llx) offset=0x%llx "
260 		      "length=0x%llx dev=%p ret=>%d\n",
261 		      _LLU(obj->id), _LLU(offset), _LLU(length), od, ret);
262 	return ret;
263 }
264 
265 static const struct osd_attr g_attr_sb_stats = ATTR_DEF(
266 	EXOFS_APAGE_SB_DATA,
267 	EXOFS_ATTR_SB_STATS,
268 	sizeof(struct exofs_sb_stats));
269 
__sbi_read_stats(struct exofs_sb_info * sbi)270 static int __sbi_read_stats(struct exofs_sb_info *sbi)
271 {
272 	struct osd_attr attrs[] = {
273 		[0] = g_attr_sb_stats,
274 	};
275 	struct ore_io_state *ios;
276 	int ret;
277 
278 	ret = ore_get_io_state(&sbi->layout, &sbi->oc, &ios);
279 	if (unlikely(ret)) {
280 		EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
281 		return ret;
282 	}
283 
284 	ios->in_attr = attrs;
285 	ios->in_attr_len = ARRAY_SIZE(attrs);
286 
287 	ret = ore_read(ios);
288 	if (unlikely(ret)) {
289 		EXOFS_ERR("Error reading super_block stats => %d\n", ret);
290 		goto out;
291 	}
292 
293 	ret = extract_attr_from_ios(ios, &attrs[0]);
294 	if (ret) {
295 		EXOFS_ERR("%s: extract_attr of sb_stats failed\n", __func__);
296 		goto out;
297 	}
298 	if (attrs[0].len) {
299 		struct exofs_sb_stats *ess;
300 
301 		if (unlikely(attrs[0].len != sizeof(*ess))) {
302 			EXOFS_ERR("%s: Wrong version of exofs_sb_stats "
303 				  "size(%d) != expected(%zd)\n",
304 				  __func__, attrs[0].len, sizeof(*ess));
305 			goto out;
306 		}
307 
308 		ess = attrs[0].val_ptr;
309 		sbi->s_nextid = le64_to_cpu(ess->s_nextid);
310 		sbi->s_numfiles = le32_to_cpu(ess->s_numfiles);
311 	}
312 
313 out:
314 	ore_put_io_state(ios);
315 	return ret;
316 }
317 
stats_done(struct ore_io_state * ios,void * p)318 static void stats_done(struct ore_io_state *ios, void *p)
319 {
320 	ore_put_io_state(ios);
321 	/* Good thanks nothing to do anymore */
322 }
323 
324 /* Asynchronously write the stats attribute */
exofs_sbi_write_stats(struct exofs_sb_info * sbi)325 int exofs_sbi_write_stats(struct exofs_sb_info *sbi)
326 {
327 	struct osd_attr attrs[] = {
328 		[0] = g_attr_sb_stats,
329 	};
330 	struct ore_io_state *ios;
331 	int ret;
332 
333 	ret = ore_get_io_state(&sbi->layout, &sbi->oc, &ios);
334 	if (unlikely(ret)) {
335 		EXOFS_ERR("%s: ore_get_io_state failed.\n", __func__);
336 		return ret;
337 	}
338 
339 	sbi->s_ess.s_nextid   = cpu_to_le64(sbi->s_nextid);
340 	sbi->s_ess.s_numfiles = cpu_to_le64(sbi->s_numfiles);
341 	attrs[0].val_ptr = &sbi->s_ess;
342 
343 
344 	ios->done = stats_done;
345 	ios->private = sbi;
346 	ios->out_attr = attrs;
347 	ios->out_attr_len = ARRAY_SIZE(attrs);
348 
349 	ret = ore_write(ios);
350 	if (unlikely(ret)) {
351 		EXOFS_ERR("%s: ore_write failed.\n", __func__);
352 		ore_put_io_state(ios);
353 	}
354 
355 	return ret;
356 }
357 
358 /******************************************************************************
359  * SUPERBLOCK FUNCTIONS
360  *****************************************************************************/
361 static const struct super_operations exofs_sops;
362 static const struct export_operations exofs_export_ops;
363 
364 /*
365  * Write the superblock to the OSD
366  */
exofs_sync_fs(struct super_block * sb,int wait)367 static int exofs_sync_fs(struct super_block *sb, int wait)
368 {
369 	struct exofs_sb_info *sbi;
370 	struct exofs_fscb *fscb;
371 	struct ore_comp one_comp;
372 	struct ore_components oc;
373 	struct ore_io_state *ios;
374 	int ret = -ENOMEM;
375 
376 	fscb = kmalloc(sizeof(*fscb), GFP_KERNEL);
377 	if (unlikely(!fscb))
378 		return -ENOMEM;
379 
380 	sbi = sb->s_fs_info;
381 
382 	/* NOTE: We no longer dirty the super_block anywhere in exofs. The
383 	 * reason we write the fscb here on unmount is so we can stay backwards
384 	 * compatible with fscb->s_version == 1. (What we are not compatible
385 	 * with is if a new version FS crashed and then we try to mount an old
386 	 * version). Otherwise the exofs_fscb is read-only from mkfs time. All
387 	 * the writeable info is set in exofs_sbi_write_stats() above.
388 	 */
389 
390 	exofs_init_comps(&oc, &one_comp, sbi, EXOFS_SUPER_ID);
391 
392 	ret = ore_get_io_state(&sbi->layout, &oc, &ios);
393 	if (unlikely(ret))
394 		goto out;
395 
396 	ios->length = offsetof(struct exofs_fscb, s_dev_table_oid);
397 	memset(fscb, 0, ios->length);
398 	fscb->s_nextid = cpu_to_le64(sbi->s_nextid);
399 	fscb->s_numfiles = cpu_to_le64(sbi->s_numfiles);
400 	fscb->s_magic = cpu_to_le16(sb->s_magic);
401 	fscb->s_newfs = 0;
402 	fscb->s_version = EXOFS_FSCB_VER;
403 
404 	ios->offset = 0;
405 	ios->kern_buff = fscb;
406 
407 	ret = ore_write(ios);
408 	if (unlikely(ret))
409 		EXOFS_ERR("%s: ore_write failed.\n", __func__);
410 
411 out:
412 	EXOFS_DBGMSG("s_nextid=0x%llx ret=%d\n", _LLU(sbi->s_nextid), ret);
413 	ore_put_io_state(ios);
414 	kfree(fscb);
415 	return ret;
416 }
417 
_exofs_print_device(const char * msg,const char * dev_path,struct osd_dev * od,u64 pid)418 static void _exofs_print_device(const char *msg, const char *dev_path,
419 				struct osd_dev *od, u64 pid)
420 {
421 	const struct osd_dev_info *odi = osduld_device_info(od);
422 
423 	printk(KERN_NOTICE "exofs: %s %s osd_name-%s pid-0x%llx\n",
424 		msg, dev_path ?: "", odi->osdname, _LLU(pid));
425 }
426 
exofs_free_sbi(struct exofs_sb_info * sbi)427 static void exofs_free_sbi(struct exofs_sb_info *sbi)
428 {
429 	unsigned numdevs = sbi->oc.numdevs;
430 
431 	while (numdevs) {
432 		unsigned i = --numdevs;
433 		struct osd_dev *od = ore_comp_dev(&sbi->oc, i);
434 
435 		if (od) {
436 			ore_comp_set_dev(&sbi->oc, i, NULL);
437 			osduld_put_device(od);
438 		}
439 	}
440 	kfree(sbi->oc.ods);
441 	kfree(sbi);
442 }
443 
444 /*
445  * This function is called when the vfs is freeing the superblock.  We just
446  * need to free our own part.
447  */
exofs_put_super(struct super_block * sb)448 static void exofs_put_super(struct super_block *sb)
449 {
450 	int num_pend;
451 	struct exofs_sb_info *sbi = sb->s_fs_info;
452 
453 	/* make sure there are no pending commands */
454 	for (num_pend = atomic_read(&sbi->s_curr_pending); num_pend > 0;
455 	     num_pend = atomic_read(&sbi->s_curr_pending)) {
456 		wait_queue_head_t wq;
457 
458 		printk(KERN_NOTICE "%s: !!Pending operations in flight. "
459 		       "This is a BUG. please report to osd-dev@open-osd.org\n",
460 		       __func__);
461 		init_waitqueue_head(&wq);
462 		wait_event_timeout(wq,
463 				  (atomic_read(&sbi->s_curr_pending) == 0),
464 				  msecs_to_jiffies(100));
465 	}
466 
467 	_exofs_print_device("Unmounting", NULL, ore_comp_dev(&sbi->oc, 0),
468 			    sbi->one_comp.obj.partition);
469 
470 	exofs_sysfs_sb_del(sbi);
471 	exofs_free_sbi(sbi);
472 	sb->s_fs_info = NULL;
473 }
474 
_read_and_match_data_map(struct exofs_sb_info * sbi,unsigned numdevs,struct exofs_device_table * dt)475 static int _read_and_match_data_map(struct exofs_sb_info *sbi, unsigned numdevs,
476 				    struct exofs_device_table *dt)
477 {
478 	int ret;
479 
480 	sbi->layout.stripe_unit =
481 				le64_to_cpu(dt->dt_data_map.cb_stripe_unit);
482 	sbi->layout.group_width =
483 				le32_to_cpu(dt->dt_data_map.cb_group_width);
484 	sbi->layout.group_depth =
485 				le32_to_cpu(dt->dt_data_map.cb_group_depth);
486 	sbi->layout.mirrors_p1  =
487 				le32_to_cpu(dt->dt_data_map.cb_mirror_cnt) + 1;
488 	sbi->layout.raid_algorithm  =
489 				le32_to_cpu(dt->dt_data_map.cb_raid_algorithm);
490 
491 	ret = ore_verify_layout(numdevs, &sbi->layout);
492 
493 	EXOFS_DBGMSG("exofs: layout: "
494 		"num_comps=%u stripe_unit=0x%x group_width=%u "
495 		"group_depth=0x%llx mirrors_p1=%u raid_algorithm=%u\n",
496 		numdevs,
497 		sbi->layout.stripe_unit,
498 		sbi->layout.group_width,
499 		_LLU(sbi->layout.group_depth),
500 		sbi->layout.mirrors_p1,
501 		sbi->layout.raid_algorithm);
502 	return ret;
503 }
504 
__ra_pages(struct ore_layout * layout)505 static unsigned __ra_pages(struct ore_layout *layout)
506 {
507 	const unsigned _MIN_RA = 32; /* min 128K read-ahead */
508 	unsigned ra_pages = layout->group_width * layout->stripe_unit /
509 				PAGE_SIZE;
510 	unsigned max_io_pages = exofs_max_io_pages(layout, ~0);
511 
512 	ra_pages *= 2; /* two stripes */
513 	if (ra_pages < _MIN_RA)
514 		ra_pages = roundup(_MIN_RA, ra_pages / 2);
515 
516 	if (ra_pages > max_io_pages)
517 		ra_pages = max_io_pages;
518 
519 	return ra_pages;
520 }
521 
522 /* @odi is valid only as long as @fscb_dev is valid */
exofs_devs_2_odi(struct exofs_dt_device_info * dt_dev,struct osd_dev_info * odi)523 static int exofs_devs_2_odi(struct exofs_dt_device_info *dt_dev,
524 			     struct osd_dev_info *odi)
525 {
526 	odi->systemid_len = le32_to_cpu(dt_dev->systemid_len);
527 	if (likely(odi->systemid_len))
528 		memcpy(odi->systemid, dt_dev->systemid, OSD_SYSTEMID_LEN);
529 
530 	odi->osdname_len = le32_to_cpu(dt_dev->osdname_len);
531 	odi->osdname = dt_dev->osdname;
532 
533 	/* FIXME support long names. Will need a _put function */
534 	if (dt_dev->long_name_offset)
535 		return -EINVAL;
536 
537 	/* Make sure osdname is printable!
538 	 * mkexofs should give us space for a null-terminator else the
539 	 * device-table is invalid.
540 	 */
541 	if (unlikely(odi->osdname_len >= sizeof(dt_dev->osdname)))
542 		odi->osdname_len = sizeof(dt_dev->osdname) - 1;
543 	dt_dev->osdname[odi->osdname_len] = 0;
544 
545 	/* If it's all zeros something is bad we read past end-of-obj */
546 	return !(odi->systemid_len || odi->osdname_len);
547 }
548 
__alloc_dev_table(struct exofs_sb_info * sbi,unsigned numdevs,struct exofs_dev ** peds)549 static int __alloc_dev_table(struct exofs_sb_info *sbi, unsigned numdevs,
550 		      struct exofs_dev **peds)
551 {
552 	/* Twice bigger table: See exofs_init_comps() and comment at
553 	 * exofs_read_lookup_dev_table()
554 	 */
555 	const size_t numores = numdevs * 2 - 1;
556 	struct exofs_dev *eds;
557 	unsigned i;
558 
559 	sbi->oc.ods = kzalloc(numores * sizeof(struct ore_dev *) +
560 			      numdevs * sizeof(struct exofs_dev), GFP_KERNEL);
561 	if (unlikely(!sbi->oc.ods)) {
562 		EXOFS_ERR("ERROR: failed allocating Device array[%d]\n",
563 			  numdevs);
564 		return -ENOMEM;
565 	}
566 
567 	/* Start of allocated struct exofs_dev entries */
568 	*peds = eds = (void *)sbi->oc.ods[numores];
569 	/* Initialize pointers into struct exofs_dev */
570 	for (i = 0; i < numdevs; ++i)
571 		sbi->oc.ods[i] = &eds[i].ored;
572 	return 0;
573 }
574 
exofs_read_lookup_dev_table(struct exofs_sb_info * sbi,struct osd_dev * fscb_od,unsigned table_count)575 static int exofs_read_lookup_dev_table(struct exofs_sb_info *sbi,
576 				       struct osd_dev *fscb_od,
577 				       unsigned table_count)
578 {
579 	struct ore_comp comp;
580 	struct exofs_device_table *dt;
581 	struct exofs_dev *eds;
582 	unsigned table_bytes = table_count * sizeof(dt->dt_dev_table[0]) +
583 					     sizeof(*dt);
584 	unsigned numdevs, i;
585 	int ret;
586 
587 	dt = kmalloc(table_bytes, GFP_KERNEL);
588 	if (unlikely(!dt)) {
589 		EXOFS_ERR("ERROR: allocating %x bytes for device table\n",
590 			  table_bytes);
591 		return -ENOMEM;
592 	}
593 
594 	sbi->oc.numdevs = 0;
595 
596 	comp.obj.partition = sbi->one_comp.obj.partition;
597 	comp.obj.id = EXOFS_DEVTABLE_ID;
598 	exofs_make_credential(comp.cred, &comp.obj);
599 
600 	ret = exofs_read_kern(fscb_od, comp.cred, &comp.obj, 0, dt,
601 			      table_bytes);
602 	if (unlikely(ret)) {
603 		EXOFS_ERR("ERROR: reading device table\n");
604 		goto out;
605 	}
606 
607 	numdevs = le64_to_cpu(dt->dt_num_devices);
608 	if (unlikely(!numdevs)) {
609 		ret = -EINVAL;
610 		goto out;
611 	}
612 	WARN_ON(table_count != numdevs);
613 
614 	ret = _read_and_match_data_map(sbi, numdevs, dt);
615 	if (unlikely(ret))
616 		goto out;
617 
618 	ret = __alloc_dev_table(sbi, numdevs, &eds);
619 	if (unlikely(ret))
620 		goto out;
621 	/* exofs round-robins the device table view according to inode
622 	 * number. We hold a: twice bigger table hence inodes can point
623 	 * to any device and have a sequential view of the table
624 	 * starting at this device. See exofs_init_comps()
625 	 */
626 	memcpy(&sbi->oc.ods[numdevs], &sbi->oc.ods[0],
627 		(numdevs - 1) * sizeof(sbi->oc.ods[0]));
628 
629 	/* create sysfs subdir under which we put the device table
630 	 * And cluster layout. A Superblock is identified by the string:
631 	 *	"dev[0].osdname"_"pid"
632 	 */
633 	exofs_sysfs_sb_add(sbi, &dt->dt_dev_table[0]);
634 
635 	for (i = 0; i < numdevs; i++) {
636 		struct exofs_fscb fscb;
637 		struct osd_dev_info odi;
638 		struct osd_dev *od;
639 
640 		if (exofs_devs_2_odi(&dt->dt_dev_table[i], &odi)) {
641 			EXOFS_ERR("ERROR: Read all-zeros device entry\n");
642 			ret = -EINVAL;
643 			goto out;
644 		}
645 
646 		printk(KERN_NOTICE "Add device[%d]: osd_name-%s\n",
647 		       i, odi.osdname);
648 
649 		/* the exofs id is currently the table index */
650 		eds[i].did = i;
651 
652 		/* On all devices the device table is identical. The user can
653 		 * specify any one of the participating devices on the command
654 		 * line. We always keep them in device-table order.
655 		 */
656 		if (fscb_od && osduld_device_same(fscb_od, &odi)) {
657 			eds[i].ored.od = fscb_od;
658 			++sbi->oc.numdevs;
659 			fscb_od = NULL;
660 			exofs_sysfs_odev_add(&eds[i], sbi);
661 			continue;
662 		}
663 
664 		od = osduld_info_lookup(&odi);
665 		if (IS_ERR(od)) {
666 			ret = PTR_ERR(od);
667 			EXOFS_ERR("ERROR: device requested is not found "
668 				  "osd_name-%s =>%d\n", odi.osdname, ret);
669 			goto out;
670 		}
671 
672 		eds[i].ored.od = od;
673 		++sbi->oc.numdevs;
674 
675 		/* Read the fscb of the other devices to make sure the FS
676 		 * partition is there.
677 		 */
678 		ret = exofs_read_kern(od, comp.cred, &comp.obj, 0, &fscb,
679 				      sizeof(fscb));
680 		if (unlikely(ret)) {
681 			EXOFS_ERR("ERROR: Malformed participating device "
682 				  "error reading fscb osd_name-%s\n",
683 				  odi.osdname);
684 			goto out;
685 		}
686 		exofs_sysfs_odev_add(&eds[i], sbi);
687 
688 		/* TODO: verify other information is correct and FS-uuid
689 		 *	 matches. Benny what did you say about device table
690 		 *	 generation and old devices?
691 		 */
692 	}
693 
694 out:
695 	kfree(dt);
696 	if (unlikely(fscb_od && !ret)) {
697 			EXOFS_ERR("ERROR: Bad device-table container device not present\n");
698 			osduld_put_device(fscb_od);
699 			return -EINVAL;
700 	}
701 	return ret;
702 }
703 
704 /*
705  * Read the superblock from the OSD and fill in the fields
706  */
exofs_fill_super(struct super_block * sb,void * data,int silent)707 static int exofs_fill_super(struct super_block *sb, void *data, int silent)
708 {
709 	struct inode *root;
710 	struct exofs_mountopt *opts = data;
711 	struct exofs_sb_info *sbi;	/*extended info                  */
712 	struct osd_dev *od;		/* Master device                 */
713 	struct exofs_fscb fscb;		/*on-disk superblock info        */
714 	struct ore_comp comp;
715 	unsigned table_count;
716 	int ret;
717 
718 	sbi = kzalloc(sizeof(*sbi), GFP_KERNEL);
719 	if (!sbi)
720 		return -ENOMEM;
721 
722 	/* use mount options to fill superblock */
723 	if (opts->is_osdname) {
724 		struct osd_dev_info odi = {.systemid_len = 0};
725 
726 		odi.osdname_len = strlen(opts->dev_name);
727 		odi.osdname = (u8 *)opts->dev_name;
728 		od = osduld_info_lookup(&odi);
729 		kfree(opts->dev_name);
730 		opts->dev_name = NULL;
731 	} else {
732 		od = osduld_path_lookup(opts->dev_name);
733 	}
734 	if (IS_ERR(od)) {
735 		ret = -EINVAL;
736 		goto free_sbi;
737 	}
738 
739 	/* Default layout in case we do not have a device-table */
740 	sbi->layout.stripe_unit = PAGE_SIZE;
741 	sbi->layout.mirrors_p1 = 1;
742 	sbi->layout.group_width = 1;
743 	sbi->layout.group_depth = -1;
744 	sbi->layout.group_count = 1;
745 	sbi->s_timeout = opts->timeout;
746 
747 	sbi->one_comp.obj.partition = opts->pid;
748 	sbi->one_comp.obj.id = 0;
749 	exofs_make_credential(sbi->one_comp.cred, &sbi->one_comp.obj);
750 	sbi->oc.single_comp = EC_SINGLE_COMP;
751 	sbi->oc.comps = &sbi->one_comp;
752 
753 	/* fill in some other data by hand */
754 	memset(sb->s_id, 0, sizeof(sb->s_id));
755 	strcpy(sb->s_id, "exofs");
756 	sb->s_blocksize = EXOFS_BLKSIZE;
757 	sb->s_blocksize_bits = EXOFS_BLKSHIFT;
758 	sb->s_maxbytes = MAX_LFS_FILESIZE;
759 	sb->s_max_links = EXOFS_LINK_MAX;
760 	atomic_set(&sbi->s_curr_pending, 0);
761 	sb->s_bdev = NULL;
762 	sb->s_dev = 0;
763 
764 	comp.obj.partition = sbi->one_comp.obj.partition;
765 	comp.obj.id = EXOFS_SUPER_ID;
766 	exofs_make_credential(comp.cred, &comp.obj);
767 
768 	ret = exofs_read_kern(od, comp.cred, &comp.obj, 0, &fscb, sizeof(fscb));
769 	if (unlikely(ret))
770 		goto free_sbi;
771 
772 	sb->s_magic = le16_to_cpu(fscb.s_magic);
773 	/* NOTE: we read below to be backward compatible with old versions */
774 	sbi->s_nextid = le64_to_cpu(fscb.s_nextid);
775 	sbi->s_numfiles = le32_to_cpu(fscb.s_numfiles);
776 
777 	/* make sure what we read from the object store is correct */
778 	if (sb->s_magic != EXOFS_SUPER_MAGIC) {
779 		if (!silent)
780 			EXOFS_ERR("ERROR: Bad magic value\n");
781 		ret = -EINVAL;
782 		goto free_sbi;
783 	}
784 	if (le32_to_cpu(fscb.s_version) > EXOFS_FSCB_VER) {
785 		EXOFS_ERR("ERROR: Bad FSCB version expected-%d got-%d\n",
786 			  EXOFS_FSCB_VER, le32_to_cpu(fscb.s_version));
787 		ret = -EINVAL;
788 		goto free_sbi;
789 	}
790 
791 	/* start generation numbers from a random point */
792 	get_random_bytes(&sbi->s_next_generation, sizeof(u32));
793 	spin_lock_init(&sbi->s_next_gen_lock);
794 
795 	table_count = le64_to_cpu(fscb.s_dev_table_count);
796 	if (table_count) {
797 		ret = exofs_read_lookup_dev_table(sbi, od, table_count);
798 		if (unlikely(ret))
799 			goto free_sbi;
800 	} else {
801 		struct exofs_dev *eds;
802 
803 		ret = __alloc_dev_table(sbi, 1, &eds);
804 		if (unlikely(ret))
805 			goto free_sbi;
806 
807 		ore_comp_set_dev(&sbi->oc, 0, od);
808 		sbi->oc.numdevs = 1;
809 	}
810 
811 	__sbi_read_stats(sbi);
812 
813 	/* set up operation vectors */
814 	ret = super_setup_bdi(sb);
815 	if (ret) {
816 		EXOFS_DBGMSG("Failed to super_setup_bdi\n");
817 		goto free_sbi;
818 	}
819 	sb->s_bdi->ra_pages = __ra_pages(&sbi->layout);
820 	sb->s_fs_info = sbi;
821 	sb->s_op = &exofs_sops;
822 	sb->s_export_op = &exofs_export_ops;
823 	root = exofs_iget(sb, EXOFS_ROOT_ID - EXOFS_OBJ_OFF);
824 	if (IS_ERR(root)) {
825 		EXOFS_ERR("ERROR: exofs_iget failed\n");
826 		ret = PTR_ERR(root);
827 		goto free_sbi;
828 	}
829 	sb->s_root = d_make_root(root);
830 	if (!sb->s_root) {
831 		EXOFS_ERR("ERROR: get root inode failed\n");
832 		ret = -ENOMEM;
833 		goto free_sbi;
834 	}
835 
836 	if (!S_ISDIR(root->i_mode)) {
837 		dput(sb->s_root);
838 		sb->s_root = NULL;
839 		EXOFS_ERR("ERROR: corrupt root inode (mode = %hd)\n",
840 		       root->i_mode);
841 		ret = -EINVAL;
842 		goto free_sbi;
843 	}
844 
845 	exofs_sysfs_dbg_print();
846 	_exofs_print_device("Mounting", opts->dev_name,
847 			    ore_comp_dev(&sbi->oc, 0),
848 			    sbi->one_comp.obj.partition);
849 	return 0;
850 
851 free_sbi:
852 	EXOFS_ERR("Unable to mount exofs on %s pid=0x%llx err=%d\n",
853 		  opts->dev_name, sbi->one_comp.obj.partition, ret);
854 	exofs_free_sbi(sbi);
855 	return ret;
856 }
857 
858 /*
859  * Set up the superblock (calls exofs_fill_super eventually)
860  */
exofs_mount(struct file_system_type * type,int flags,const char * dev_name,void * data)861 static struct dentry *exofs_mount(struct file_system_type *type,
862 			  int flags, const char *dev_name,
863 			  void *data)
864 {
865 	struct exofs_mountopt opts;
866 	int ret;
867 
868 	ret = parse_options(data, &opts);
869 	if (ret)
870 		return ERR_PTR(ret);
871 
872 	if (!opts.dev_name)
873 		opts.dev_name = dev_name;
874 	return mount_nodev(type, flags, &opts, exofs_fill_super);
875 }
876 
877 /*
878  * Return information about the file system state in the buffer.  This is used
879  * by the 'df' command, for example.
880  */
exofs_statfs(struct dentry * dentry,struct kstatfs * buf)881 static int exofs_statfs(struct dentry *dentry, struct kstatfs *buf)
882 {
883 	struct super_block *sb = dentry->d_sb;
884 	struct exofs_sb_info *sbi = sb->s_fs_info;
885 	struct ore_io_state *ios;
886 	struct osd_attr attrs[] = {
887 		ATTR_DEF(OSD_APAGE_PARTITION_QUOTAS,
888 			OSD_ATTR_PQ_CAPACITY_QUOTA, sizeof(__be64)),
889 		ATTR_DEF(OSD_APAGE_PARTITION_INFORMATION,
890 			OSD_ATTR_PI_USED_CAPACITY, sizeof(__be64)),
891 	};
892 	uint64_t capacity = ULLONG_MAX;
893 	uint64_t used = ULLONG_MAX;
894 	int ret;
895 
896 	ret = ore_get_io_state(&sbi->layout, &sbi->oc, &ios);
897 	if (ret) {
898 		EXOFS_DBGMSG("ore_get_io_state failed.\n");
899 		return ret;
900 	}
901 
902 	ios->in_attr = attrs;
903 	ios->in_attr_len = ARRAY_SIZE(attrs);
904 
905 	ret = ore_read(ios);
906 	if (unlikely(ret))
907 		goto out;
908 
909 	ret = extract_attr_from_ios(ios, &attrs[0]);
910 	if (likely(!ret)) {
911 		capacity = get_unaligned_be64(attrs[0].val_ptr);
912 		if (unlikely(!capacity))
913 			capacity = ULLONG_MAX;
914 	} else
915 		EXOFS_DBGMSG("exofs_statfs: get capacity failed.\n");
916 
917 	ret = extract_attr_from_ios(ios, &attrs[1]);
918 	if (likely(!ret))
919 		used = get_unaligned_be64(attrs[1].val_ptr);
920 	else
921 		EXOFS_DBGMSG("exofs_statfs: get used-space failed.\n");
922 
923 	/* fill in the stats buffer */
924 	buf->f_type = EXOFS_SUPER_MAGIC;
925 	buf->f_bsize = EXOFS_BLKSIZE;
926 	buf->f_blocks = capacity >> 9;
927 	buf->f_bfree = (capacity - used) >> 9;
928 	buf->f_bavail = buf->f_bfree;
929 	buf->f_files = sbi->s_numfiles;
930 	buf->f_ffree = EXOFS_MAX_ID - sbi->s_numfiles;
931 	buf->f_namelen = EXOFS_NAME_LEN;
932 
933 out:
934 	ore_put_io_state(ios);
935 	return ret;
936 }
937 
938 static const struct super_operations exofs_sops = {
939 	.alloc_inode    = exofs_alloc_inode,
940 	.destroy_inode  = exofs_destroy_inode,
941 	.write_inode    = exofs_write_inode,
942 	.evict_inode    = exofs_evict_inode,
943 	.put_super      = exofs_put_super,
944 	.sync_fs	= exofs_sync_fs,
945 	.statfs         = exofs_statfs,
946 };
947 
948 /******************************************************************************
949  * EXPORT OPERATIONS
950  *****************************************************************************/
951 
exofs_get_parent(struct dentry * child)952 static struct dentry *exofs_get_parent(struct dentry *child)
953 {
954 	unsigned long ino = exofs_parent_ino(child);
955 
956 	if (!ino)
957 		return ERR_PTR(-ESTALE);
958 
959 	return d_obtain_alias(exofs_iget(child->d_sb, ino));
960 }
961 
exofs_nfs_get_inode(struct super_block * sb,u64 ino,u32 generation)962 static struct inode *exofs_nfs_get_inode(struct super_block *sb,
963 		u64 ino, u32 generation)
964 {
965 	struct inode *inode;
966 
967 	inode = exofs_iget(sb, ino);
968 	if (IS_ERR(inode))
969 		return ERR_CAST(inode);
970 	if (generation && inode->i_generation != generation) {
971 		/* we didn't find the right inode.. */
972 		iput(inode);
973 		return ERR_PTR(-ESTALE);
974 	}
975 	return inode;
976 }
977 
exofs_fh_to_dentry(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)978 static struct dentry *exofs_fh_to_dentry(struct super_block *sb,
979 				struct fid *fid, int fh_len, int fh_type)
980 {
981 	return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
982 				    exofs_nfs_get_inode);
983 }
984 
exofs_fh_to_parent(struct super_block * sb,struct fid * fid,int fh_len,int fh_type)985 static struct dentry *exofs_fh_to_parent(struct super_block *sb,
986 				struct fid *fid, int fh_len, int fh_type)
987 {
988 	return generic_fh_to_parent(sb, fid, fh_len, fh_type,
989 				    exofs_nfs_get_inode);
990 }
991 
992 static const struct export_operations exofs_export_ops = {
993 	.fh_to_dentry = exofs_fh_to_dentry,
994 	.fh_to_parent = exofs_fh_to_parent,
995 	.get_parent = exofs_get_parent,
996 };
997 
998 /******************************************************************************
999  * INSMOD/RMMOD
1000  *****************************************************************************/
1001 
1002 /*
1003  * struct that describes this file system
1004  */
1005 static struct file_system_type exofs_type = {
1006 	.owner          = THIS_MODULE,
1007 	.name           = "exofs",
1008 	.mount          = exofs_mount,
1009 	.kill_sb        = generic_shutdown_super,
1010 };
1011 MODULE_ALIAS_FS("exofs");
1012 
init_exofs(void)1013 static int __init init_exofs(void)
1014 {
1015 	int err;
1016 
1017 	err = init_inodecache();
1018 	if (err)
1019 		goto out;
1020 
1021 	err = register_filesystem(&exofs_type);
1022 	if (err)
1023 		goto out_d;
1024 
1025 	/* We don't fail if sysfs creation failed */
1026 	exofs_sysfs_init();
1027 
1028 	return 0;
1029 out_d:
1030 	destroy_inodecache();
1031 out:
1032 	return err;
1033 }
1034 
exit_exofs(void)1035 static void __exit exit_exofs(void)
1036 {
1037 	exofs_sysfs_uninit();
1038 	unregister_filesystem(&exofs_type);
1039 	destroy_inodecache();
1040 }
1041 
1042 MODULE_AUTHOR("Avishay Traeger <avishay@gmail.com>");
1043 MODULE_DESCRIPTION("exofs");
1044 MODULE_LICENSE("GPL");
1045 
1046 module_init(init_exofs)
1047 module_exit(exit_exofs)
1048