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