xref: /openbmc/linux/fs/ceph/super.c (revision e6dec923)
1 
2 #include <linux/ceph/ceph_debug.h>
3 
4 #include <linux/backing-dev.h>
5 #include <linux/ctype.h>
6 #include <linux/fs.h>
7 #include <linux/inet.h>
8 #include <linux/in6.h>
9 #include <linux/module.h>
10 #include <linux/mount.h>
11 #include <linux/parser.h>
12 #include <linux/sched.h>
13 #include <linux/seq_file.h>
14 #include <linux/slab.h>
15 #include <linux/statfs.h>
16 #include <linux/string.h>
17 
18 #include "super.h"
19 #include "mds_client.h"
20 #include "cache.h"
21 
22 #include <linux/ceph/ceph_features.h>
23 #include <linux/ceph/decode.h>
24 #include <linux/ceph/mon_client.h>
25 #include <linux/ceph/auth.h>
26 #include <linux/ceph/debugfs.h>
27 
28 /*
29  * Ceph superblock operations
30  *
31  * Handle the basics of mounting, unmounting.
32  */
33 
34 /*
35  * super ops
36  */
37 static void ceph_put_super(struct super_block *s)
38 {
39 	struct ceph_fs_client *fsc = ceph_sb_to_client(s);
40 
41 	dout("put_super\n");
42 	ceph_mdsc_close_sessions(fsc->mdsc);
43 }
44 
45 static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
46 {
47 	struct ceph_fs_client *fsc = ceph_inode_to_client(d_inode(dentry));
48 	struct ceph_monmap *monmap = fsc->client->monc.monmap;
49 	struct ceph_statfs st;
50 	u64 fsid;
51 	int err;
52 
53 	dout("statfs\n");
54 	err = ceph_monc_do_statfs(&fsc->client->monc, &st);
55 	if (err < 0)
56 		return err;
57 
58 	/* fill in kstatfs */
59 	buf->f_type = CEPH_SUPER_MAGIC;  /* ?? */
60 
61 	/*
62 	 * express utilization in terms of large blocks to avoid
63 	 * overflow on 32-bit machines.
64 	 *
65 	 * NOTE: for the time being, we make bsize == frsize to humor
66 	 * not-yet-ancient versions of glibc that are broken.
67 	 * Someday, we will probably want to report a real block
68 	 * size...  whatever that may mean for a network file system!
69 	 */
70 	buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
71 	buf->f_frsize = 1 << CEPH_BLOCK_SHIFT;
72 	buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
73 	buf->f_bfree = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
74 	buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
75 
76 	buf->f_files = le64_to_cpu(st.num_objects);
77 	buf->f_ffree = -1;
78 	buf->f_namelen = NAME_MAX;
79 
80 	/* leave fsid little-endian, regardless of host endianness */
81 	fsid = *(u64 *)(&monmap->fsid) ^ *((u64 *)&monmap->fsid + 1);
82 	buf->f_fsid.val[0] = fsid & 0xffffffff;
83 	buf->f_fsid.val[1] = fsid >> 32;
84 
85 	return 0;
86 }
87 
88 
89 static int ceph_sync_fs(struct super_block *sb, int wait)
90 {
91 	struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
92 
93 	if (!wait) {
94 		dout("sync_fs (non-blocking)\n");
95 		ceph_flush_dirty_caps(fsc->mdsc);
96 		dout("sync_fs (non-blocking) done\n");
97 		return 0;
98 	}
99 
100 	dout("sync_fs (blocking)\n");
101 	ceph_osdc_sync(&fsc->client->osdc);
102 	ceph_mdsc_sync(fsc->mdsc);
103 	dout("sync_fs (blocking) done\n");
104 	return 0;
105 }
106 
107 /*
108  * mount options
109  */
110 enum {
111 	Opt_wsize,
112 	Opt_rsize,
113 	Opt_rasize,
114 	Opt_caps_wanted_delay_min,
115 	Opt_caps_wanted_delay_max,
116 	Opt_cap_release_safety,
117 	Opt_readdir_max_entries,
118 	Opt_readdir_max_bytes,
119 	Opt_congestion_kb,
120 	Opt_last_int,
121 	/* int args above */
122 	Opt_snapdirname,
123 	Opt_mds_namespace,
124 	Opt_fscache_uniq,
125 	Opt_last_string,
126 	/* string args above */
127 	Opt_dirstat,
128 	Opt_nodirstat,
129 	Opt_rbytes,
130 	Opt_norbytes,
131 	Opt_asyncreaddir,
132 	Opt_noasyncreaddir,
133 	Opt_dcache,
134 	Opt_nodcache,
135 	Opt_ino32,
136 	Opt_noino32,
137 	Opt_fscache,
138 	Opt_nofscache,
139 	Opt_poolperm,
140 	Opt_nopoolperm,
141 	Opt_require_active_mds,
142 	Opt_norequire_active_mds,
143 #ifdef CONFIG_CEPH_FS_POSIX_ACL
144 	Opt_acl,
145 #endif
146 	Opt_noacl,
147 };
148 
149 static match_table_t fsopt_tokens = {
150 	{Opt_wsize, "wsize=%d"},
151 	{Opt_rsize, "rsize=%d"},
152 	{Opt_rasize, "rasize=%d"},
153 	{Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
154 	{Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
155 	{Opt_cap_release_safety, "cap_release_safety=%d"},
156 	{Opt_readdir_max_entries, "readdir_max_entries=%d"},
157 	{Opt_readdir_max_bytes, "readdir_max_bytes=%d"},
158 	{Opt_congestion_kb, "write_congestion_kb=%d"},
159 	/* int args above */
160 	{Opt_snapdirname, "snapdirname=%s"},
161 	{Opt_mds_namespace, "mds_namespace=%s"},
162 	{Opt_fscache_uniq, "fsc=%s"},
163 	/* string args above */
164 	{Opt_dirstat, "dirstat"},
165 	{Opt_nodirstat, "nodirstat"},
166 	{Opt_rbytes, "rbytes"},
167 	{Opt_norbytes, "norbytes"},
168 	{Opt_asyncreaddir, "asyncreaddir"},
169 	{Opt_noasyncreaddir, "noasyncreaddir"},
170 	{Opt_dcache, "dcache"},
171 	{Opt_nodcache, "nodcache"},
172 	{Opt_ino32, "ino32"},
173 	{Opt_noino32, "noino32"},
174 	{Opt_fscache, "fsc"},
175 	{Opt_nofscache, "nofsc"},
176 	{Opt_poolperm, "poolperm"},
177 	{Opt_nopoolperm, "nopoolperm"},
178 	{Opt_require_active_mds, "require_active_mds"},
179 	{Opt_norequire_active_mds, "norequire_active_mds"},
180 #ifdef CONFIG_CEPH_FS_POSIX_ACL
181 	{Opt_acl, "acl"},
182 #endif
183 	{Opt_noacl, "noacl"},
184 	{-1, NULL}
185 };
186 
187 static int parse_fsopt_token(char *c, void *private)
188 {
189 	struct ceph_mount_options *fsopt = private;
190 	substring_t argstr[MAX_OPT_ARGS];
191 	int token, intval, ret;
192 
193 	token = match_token((char *)c, fsopt_tokens, argstr);
194 	if (token < 0)
195 		return -EINVAL;
196 
197 	if (token < Opt_last_int) {
198 		ret = match_int(&argstr[0], &intval);
199 		if (ret < 0) {
200 			pr_err("bad mount option arg (not int) "
201 			       "at '%s'\n", c);
202 			return ret;
203 		}
204 		dout("got int token %d val %d\n", token, intval);
205 	} else if (token > Opt_last_int && token < Opt_last_string) {
206 		dout("got string token %d val %s\n", token,
207 		     argstr[0].from);
208 	} else {
209 		dout("got token %d\n", token);
210 	}
211 
212 	switch (token) {
213 	case Opt_snapdirname:
214 		kfree(fsopt->snapdir_name);
215 		fsopt->snapdir_name = kstrndup(argstr[0].from,
216 					       argstr[0].to-argstr[0].from,
217 					       GFP_KERNEL);
218 		if (!fsopt->snapdir_name)
219 			return -ENOMEM;
220 		break;
221 	case Opt_mds_namespace:
222 		fsopt->mds_namespace = kstrndup(argstr[0].from,
223 						argstr[0].to-argstr[0].from,
224 						GFP_KERNEL);
225 		if (!fsopt->mds_namespace)
226 			return -ENOMEM;
227 		break;
228 	case Opt_fscache_uniq:
229 		fsopt->fscache_uniq = kstrndup(argstr[0].from,
230 					       argstr[0].to-argstr[0].from,
231 					       GFP_KERNEL);
232 		if (!fsopt->fscache_uniq)
233 			return -ENOMEM;
234 		fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
235 		break;
236 		/* misc */
237 	case Opt_wsize:
238 		fsopt->wsize = intval;
239 		break;
240 	case Opt_rsize:
241 		fsopt->rsize = intval;
242 		break;
243 	case Opt_rasize:
244 		fsopt->rasize = intval;
245 		break;
246 	case Opt_caps_wanted_delay_min:
247 		fsopt->caps_wanted_delay_min = intval;
248 		break;
249 	case Opt_caps_wanted_delay_max:
250 		fsopt->caps_wanted_delay_max = intval;
251 		break;
252 	case Opt_readdir_max_entries:
253 		fsopt->max_readdir = intval;
254 		break;
255 	case Opt_readdir_max_bytes:
256 		fsopt->max_readdir_bytes = intval;
257 		break;
258 	case Opt_congestion_kb:
259 		fsopt->congestion_kb = intval;
260 		break;
261 	case Opt_dirstat:
262 		fsopt->flags |= CEPH_MOUNT_OPT_DIRSTAT;
263 		break;
264 	case Opt_nodirstat:
265 		fsopt->flags &= ~CEPH_MOUNT_OPT_DIRSTAT;
266 		break;
267 	case Opt_rbytes:
268 		fsopt->flags |= CEPH_MOUNT_OPT_RBYTES;
269 		break;
270 	case Opt_norbytes:
271 		fsopt->flags &= ~CEPH_MOUNT_OPT_RBYTES;
272 		break;
273 	case Opt_asyncreaddir:
274 		fsopt->flags &= ~CEPH_MOUNT_OPT_NOASYNCREADDIR;
275 		break;
276 	case Opt_noasyncreaddir:
277 		fsopt->flags |= CEPH_MOUNT_OPT_NOASYNCREADDIR;
278 		break;
279 	case Opt_dcache:
280 		fsopt->flags |= CEPH_MOUNT_OPT_DCACHE;
281 		break;
282 	case Opt_nodcache:
283 		fsopt->flags &= ~CEPH_MOUNT_OPT_DCACHE;
284 		break;
285 	case Opt_ino32:
286 		fsopt->flags |= CEPH_MOUNT_OPT_INO32;
287 		break;
288 	case Opt_noino32:
289 		fsopt->flags &= ~CEPH_MOUNT_OPT_INO32;
290 		break;
291 	case Opt_fscache:
292 		fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
293 		break;
294 	case Opt_nofscache:
295 		fsopt->flags &= ~CEPH_MOUNT_OPT_FSCACHE;
296 		break;
297 	case Opt_poolperm:
298 		fsopt->flags &= ~CEPH_MOUNT_OPT_NOPOOLPERM;
299 		printk ("pool perm");
300 		break;
301 	case Opt_nopoolperm:
302 		fsopt->flags |= CEPH_MOUNT_OPT_NOPOOLPERM;
303 		break;
304 	case Opt_require_active_mds:
305 		fsopt->flags &= ~CEPH_MOUNT_OPT_MOUNTWAIT;
306 		break;
307 	case Opt_norequire_active_mds:
308 		fsopt->flags |= CEPH_MOUNT_OPT_MOUNTWAIT;
309 		break;
310 #ifdef CONFIG_CEPH_FS_POSIX_ACL
311 	case Opt_acl:
312 		fsopt->sb_flags |= MS_POSIXACL;
313 		break;
314 #endif
315 	case Opt_noacl:
316 		fsopt->sb_flags &= ~MS_POSIXACL;
317 		break;
318 	default:
319 		BUG_ON(token);
320 	}
321 	return 0;
322 }
323 
324 static void destroy_mount_options(struct ceph_mount_options *args)
325 {
326 	dout("destroy_mount_options %p\n", args);
327 	kfree(args->snapdir_name);
328 	kfree(args->mds_namespace);
329 	kfree(args->server_path);
330 	kfree(args->fscache_uniq);
331 	kfree(args);
332 }
333 
334 static int strcmp_null(const char *s1, const char *s2)
335 {
336 	if (!s1 && !s2)
337 		return 0;
338 	if (s1 && !s2)
339 		return -1;
340 	if (!s1 && s2)
341 		return 1;
342 	return strcmp(s1, s2);
343 }
344 
345 static int compare_mount_options(struct ceph_mount_options *new_fsopt,
346 				 struct ceph_options *new_opt,
347 				 struct ceph_fs_client *fsc)
348 {
349 	struct ceph_mount_options *fsopt1 = new_fsopt;
350 	struct ceph_mount_options *fsopt2 = fsc->mount_options;
351 	int ofs = offsetof(struct ceph_mount_options, snapdir_name);
352 	int ret;
353 
354 	ret = memcmp(fsopt1, fsopt2, ofs);
355 	if (ret)
356 		return ret;
357 
358 	ret = strcmp_null(fsopt1->snapdir_name, fsopt2->snapdir_name);
359 	if (ret)
360 		return ret;
361 	ret = strcmp_null(fsopt1->mds_namespace, fsopt2->mds_namespace);
362 	if (ret)
363 		return ret;
364 	ret = strcmp_null(fsopt1->server_path, fsopt2->server_path);
365 	if (ret)
366 		return ret;
367 	ret = strcmp_null(fsopt1->fscache_uniq, fsopt2->fscache_uniq);
368 	if (ret)
369 		return ret;
370 
371 	return ceph_compare_options(new_opt, fsc->client);
372 }
373 
374 static int parse_mount_options(struct ceph_mount_options **pfsopt,
375 			       struct ceph_options **popt,
376 			       int flags, char *options,
377 			       const char *dev_name)
378 {
379 	struct ceph_mount_options *fsopt;
380 	const char *dev_name_end;
381 	int err;
382 
383 	if (!dev_name || !*dev_name)
384 		return -EINVAL;
385 
386 	fsopt = kzalloc(sizeof(*fsopt), GFP_KERNEL);
387 	if (!fsopt)
388 		return -ENOMEM;
389 
390 	dout("parse_mount_options %p, dev_name '%s'\n", fsopt, dev_name);
391 
392 	fsopt->sb_flags = flags;
393 	fsopt->flags = CEPH_MOUNT_OPT_DEFAULT;
394 
395 	fsopt->rsize = CEPH_RSIZE_DEFAULT;
396 	fsopt->rasize = CEPH_RASIZE_DEFAULT;
397 	fsopt->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
398 	if (!fsopt->snapdir_name) {
399 		err = -ENOMEM;
400 		goto out;
401 	}
402 
403 	fsopt->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
404 	fsopt->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
405 	fsopt->cap_release_safety = CEPH_CAP_RELEASE_SAFETY_DEFAULT;
406 	fsopt->max_readdir = CEPH_MAX_READDIR_DEFAULT;
407 	fsopt->max_readdir_bytes = CEPH_MAX_READDIR_BYTES_DEFAULT;
408 	fsopt->congestion_kb = default_congestion_kb();
409 
410 	/*
411 	 * Distinguish the server list from the path in "dev_name".
412 	 * Internally we do not include the leading '/' in the path.
413 	 *
414 	 * "dev_name" will look like:
415 	 *     <server_spec>[,<server_spec>...]:[<path>]
416 	 * where
417 	 *     <server_spec> is <ip>[:<port>]
418 	 *     <path> is optional, but if present must begin with '/'
419 	 */
420 	dev_name_end = strchr(dev_name, '/');
421 	if (dev_name_end) {
422 		if (strlen(dev_name_end) > 1) {
423 			fsopt->server_path = kstrdup(dev_name_end, GFP_KERNEL);
424 			if (!fsopt->server_path) {
425 				err = -ENOMEM;
426 				goto out;
427 			}
428 		}
429 	} else {
430 		dev_name_end = dev_name + strlen(dev_name);
431 	}
432 	err = -EINVAL;
433 	dev_name_end--;		/* back up to ':' separator */
434 	if (dev_name_end < dev_name || *dev_name_end != ':') {
435 		pr_err("device name is missing path (no : separator in %s)\n",
436 				dev_name);
437 		goto out;
438 	}
439 	dout("device name '%.*s'\n", (int)(dev_name_end - dev_name), dev_name);
440 	if (fsopt->server_path)
441 		dout("server path '%s'\n", fsopt->server_path);
442 
443 	*popt = ceph_parse_options(options, dev_name, dev_name_end,
444 				 parse_fsopt_token, (void *)fsopt);
445 	if (IS_ERR(*popt)) {
446 		err = PTR_ERR(*popt);
447 		goto out;
448 	}
449 
450 	/* success */
451 	*pfsopt = fsopt;
452 	return 0;
453 
454 out:
455 	destroy_mount_options(fsopt);
456 	return err;
457 }
458 
459 /**
460  * ceph_show_options - Show mount options in /proc/mounts
461  * @m: seq_file to write to
462  * @root: root of that (sub)tree
463  */
464 static int ceph_show_options(struct seq_file *m, struct dentry *root)
465 {
466 	struct ceph_fs_client *fsc = ceph_sb_to_client(root->d_sb);
467 	struct ceph_mount_options *fsopt = fsc->mount_options;
468 	size_t pos;
469 	int ret;
470 
471 	/* a comma between MNT/MS and client options */
472 	seq_putc(m, ',');
473 	pos = m->count;
474 
475 	ret = ceph_print_client_options(m, fsc->client);
476 	if (ret)
477 		return ret;
478 
479 	/* retract our comma if no client options */
480 	if (m->count == pos)
481 		m->count--;
482 
483 	if (fsopt->flags & CEPH_MOUNT_OPT_DIRSTAT)
484 		seq_puts(m, ",dirstat");
485 	if ((fsopt->flags & CEPH_MOUNT_OPT_RBYTES))
486 		seq_puts(m, ",rbytes");
487 	if (fsopt->flags & CEPH_MOUNT_OPT_NOASYNCREADDIR)
488 		seq_puts(m, ",noasyncreaddir");
489 	if ((fsopt->flags & CEPH_MOUNT_OPT_DCACHE) == 0)
490 		seq_puts(m, ",nodcache");
491 	if (fsopt->flags & CEPH_MOUNT_OPT_FSCACHE) {
492 		if (fsopt->fscache_uniq)
493 			seq_printf(m, ",fsc=%s", fsopt->fscache_uniq);
494 		else
495 			seq_puts(m, ",fsc");
496 	}
497 	if (fsopt->flags & CEPH_MOUNT_OPT_NOPOOLPERM)
498 		seq_puts(m, ",nopoolperm");
499 
500 #ifdef CONFIG_CEPH_FS_POSIX_ACL
501 	if (fsopt->sb_flags & MS_POSIXACL)
502 		seq_puts(m, ",acl");
503 	else
504 		seq_puts(m, ",noacl");
505 #endif
506 
507 	if (fsopt->mds_namespace)
508 		seq_printf(m, ",mds_namespace=%s", fsopt->mds_namespace);
509 	if (fsopt->wsize)
510 		seq_printf(m, ",wsize=%d", fsopt->wsize);
511 	if (fsopt->rsize != CEPH_RSIZE_DEFAULT)
512 		seq_printf(m, ",rsize=%d", fsopt->rsize);
513 	if (fsopt->rasize != CEPH_RASIZE_DEFAULT)
514 		seq_printf(m, ",rasize=%d", fsopt->rasize);
515 	if (fsopt->congestion_kb != default_congestion_kb())
516 		seq_printf(m, ",write_congestion_kb=%d", fsopt->congestion_kb);
517 	if (fsopt->caps_wanted_delay_min != CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT)
518 		seq_printf(m, ",caps_wanted_delay_min=%d",
519 			 fsopt->caps_wanted_delay_min);
520 	if (fsopt->caps_wanted_delay_max != CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT)
521 		seq_printf(m, ",caps_wanted_delay_max=%d",
522 			   fsopt->caps_wanted_delay_max);
523 	if (fsopt->cap_release_safety != CEPH_CAP_RELEASE_SAFETY_DEFAULT)
524 		seq_printf(m, ",cap_release_safety=%d",
525 			   fsopt->cap_release_safety);
526 	if (fsopt->max_readdir != CEPH_MAX_READDIR_DEFAULT)
527 		seq_printf(m, ",readdir_max_entries=%d", fsopt->max_readdir);
528 	if (fsopt->max_readdir_bytes != CEPH_MAX_READDIR_BYTES_DEFAULT)
529 		seq_printf(m, ",readdir_max_bytes=%d", fsopt->max_readdir_bytes);
530 	if (strcmp(fsopt->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
531 		seq_show_option(m, "snapdirname", fsopt->snapdir_name);
532 
533 	return 0;
534 }
535 
536 /*
537  * handle any mon messages the standard library doesn't understand.
538  * return error if we don't either.
539  */
540 static int extra_mon_dispatch(struct ceph_client *client, struct ceph_msg *msg)
541 {
542 	struct ceph_fs_client *fsc = client->private;
543 	int type = le16_to_cpu(msg->hdr.type);
544 
545 	switch (type) {
546 	case CEPH_MSG_MDS_MAP:
547 		ceph_mdsc_handle_mdsmap(fsc->mdsc, msg);
548 		return 0;
549 	case CEPH_MSG_FS_MAP_USER:
550 		ceph_mdsc_handle_fsmap(fsc->mdsc, msg);
551 		return 0;
552 	default:
553 		return -1;
554 	}
555 }
556 
557 /*
558  * create a new fs client
559  */
560 static struct ceph_fs_client *create_fs_client(struct ceph_mount_options *fsopt,
561 					struct ceph_options *opt)
562 {
563 	struct ceph_fs_client *fsc;
564 	int page_count;
565 	size_t size;
566 	int err = -ENOMEM;
567 
568 	fsc = kzalloc(sizeof(*fsc), GFP_KERNEL);
569 	if (!fsc)
570 		return ERR_PTR(-ENOMEM);
571 
572 	fsc->client = ceph_create_client(opt, fsc);
573 	if (IS_ERR(fsc->client)) {
574 		err = PTR_ERR(fsc->client);
575 		goto fail;
576 	}
577 	fsc->client->extra_mon_dispatch = extra_mon_dispatch;
578 
579 	if (fsopt->mds_namespace == NULL) {
580 		ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
581 				   0, true);
582 	} else {
583 		ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_FSMAP,
584 				   0, false);
585 	}
586 
587 	fsc->mount_options = fsopt;
588 
589 	fsc->sb = NULL;
590 	fsc->mount_state = CEPH_MOUNT_MOUNTING;
591 
592 	atomic_long_set(&fsc->writeback_count, 0);
593 
594 	err = -ENOMEM;
595 	/*
596 	 * The number of concurrent works can be high but they don't need
597 	 * to be processed in parallel, limit concurrency.
598 	 */
599 	fsc->wb_wq = alloc_workqueue("ceph-writeback", 0, 1);
600 	if (fsc->wb_wq == NULL)
601 		goto fail_client;
602 	fsc->pg_inv_wq = alloc_workqueue("ceph-pg-invalid", 0, 1);
603 	if (fsc->pg_inv_wq == NULL)
604 		goto fail_wb_wq;
605 	fsc->trunc_wq = alloc_workqueue("ceph-trunc", 0, 1);
606 	if (fsc->trunc_wq == NULL)
607 		goto fail_pg_inv_wq;
608 
609 	/* set up mempools */
610 	err = -ENOMEM;
611 	page_count = fsc->mount_options->wsize >> PAGE_SHIFT;
612 	size = sizeof (struct page *) * (page_count ? page_count : 1);
613 	fsc->wb_pagevec_pool = mempool_create_kmalloc_pool(10, size);
614 	if (!fsc->wb_pagevec_pool)
615 		goto fail_trunc_wq;
616 
617 	/* caps */
618 	fsc->min_caps = fsopt->max_readdir;
619 
620 	return fsc;
621 
622 fail_trunc_wq:
623 	destroy_workqueue(fsc->trunc_wq);
624 fail_pg_inv_wq:
625 	destroy_workqueue(fsc->pg_inv_wq);
626 fail_wb_wq:
627 	destroy_workqueue(fsc->wb_wq);
628 fail_client:
629 	ceph_destroy_client(fsc->client);
630 fail:
631 	kfree(fsc);
632 	return ERR_PTR(err);
633 }
634 
635 static void destroy_fs_client(struct ceph_fs_client *fsc)
636 {
637 	dout("destroy_fs_client %p\n", fsc);
638 
639 	destroy_workqueue(fsc->wb_wq);
640 	destroy_workqueue(fsc->pg_inv_wq);
641 	destroy_workqueue(fsc->trunc_wq);
642 
643 	mempool_destroy(fsc->wb_pagevec_pool);
644 
645 	destroy_mount_options(fsc->mount_options);
646 
647 	ceph_destroy_client(fsc->client);
648 
649 	kfree(fsc);
650 	dout("destroy_fs_client %p done\n", fsc);
651 }
652 
653 /*
654  * caches
655  */
656 struct kmem_cache *ceph_inode_cachep;
657 struct kmem_cache *ceph_cap_cachep;
658 struct kmem_cache *ceph_cap_flush_cachep;
659 struct kmem_cache *ceph_dentry_cachep;
660 struct kmem_cache *ceph_file_cachep;
661 
662 static void ceph_inode_init_once(void *foo)
663 {
664 	struct ceph_inode_info *ci = foo;
665 	inode_init_once(&ci->vfs_inode);
666 }
667 
668 static int __init init_caches(void)
669 {
670 	int error = -ENOMEM;
671 
672 	ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
673 				      sizeof(struct ceph_inode_info),
674 				      __alignof__(struct ceph_inode_info),
675 				      SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
676 				      SLAB_ACCOUNT, ceph_inode_init_once);
677 	if (ceph_inode_cachep == NULL)
678 		return -ENOMEM;
679 
680 	ceph_cap_cachep = KMEM_CACHE(ceph_cap,
681 				     SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
682 	if (ceph_cap_cachep == NULL)
683 		goto bad_cap;
684 	ceph_cap_flush_cachep = KMEM_CACHE(ceph_cap_flush,
685 					   SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
686 	if (ceph_cap_flush_cachep == NULL)
687 		goto bad_cap_flush;
688 
689 	ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
690 					SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
691 	if (ceph_dentry_cachep == NULL)
692 		goto bad_dentry;
693 
694 	ceph_file_cachep = KMEM_CACHE(ceph_file_info, SLAB_MEM_SPREAD);
695 
696 	if (ceph_file_cachep == NULL)
697 		goto bad_file;
698 
699 	if ((error = ceph_fscache_register()))
700 		goto bad_file;
701 
702 	return 0;
703 bad_file:
704 	kmem_cache_destroy(ceph_dentry_cachep);
705 bad_dentry:
706 	kmem_cache_destroy(ceph_cap_flush_cachep);
707 bad_cap_flush:
708 	kmem_cache_destroy(ceph_cap_cachep);
709 bad_cap:
710 	kmem_cache_destroy(ceph_inode_cachep);
711 	return error;
712 }
713 
714 static void destroy_caches(void)
715 {
716 	/*
717 	 * Make sure all delayed rcu free inodes are flushed before we
718 	 * destroy cache.
719 	 */
720 	rcu_barrier();
721 
722 	kmem_cache_destroy(ceph_inode_cachep);
723 	kmem_cache_destroy(ceph_cap_cachep);
724 	kmem_cache_destroy(ceph_cap_flush_cachep);
725 	kmem_cache_destroy(ceph_dentry_cachep);
726 	kmem_cache_destroy(ceph_file_cachep);
727 
728 	ceph_fscache_unregister();
729 }
730 
731 
732 /*
733  * ceph_umount_begin - initiate forced umount.  Tear down down the
734  * mount, skipping steps that may hang while waiting for server(s).
735  */
736 static void ceph_umount_begin(struct super_block *sb)
737 {
738 	struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
739 
740 	dout("ceph_umount_begin - starting forced umount\n");
741 	if (!fsc)
742 		return;
743 	fsc->mount_state = CEPH_MOUNT_SHUTDOWN;
744 	ceph_mdsc_force_umount(fsc->mdsc);
745 	return;
746 }
747 
748 static const struct super_operations ceph_super_ops = {
749 	.alloc_inode	= ceph_alloc_inode,
750 	.destroy_inode	= ceph_destroy_inode,
751 	.write_inode    = ceph_write_inode,
752 	.drop_inode	= ceph_drop_inode,
753 	.sync_fs        = ceph_sync_fs,
754 	.put_super	= ceph_put_super,
755 	.show_options   = ceph_show_options,
756 	.statfs		= ceph_statfs,
757 	.umount_begin   = ceph_umount_begin,
758 };
759 
760 /*
761  * Bootstrap mount by opening the root directory.  Note the mount
762  * @started time from caller, and time out if this takes too long.
763  */
764 static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
765 				       const char *path,
766 				       unsigned long started)
767 {
768 	struct ceph_mds_client *mdsc = fsc->mdsc;
769 	struct ceph_mds_request *req = NULL;
770 	int err;
771 	struct dentry *root;
772 
773 	/* open dir */
774 	dout("open_root_inode opening '%s'\n", path);
775 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
776 	if (IS_ERR(req))
777 		return ERR_CAST(req);
778 	req->r_path1 = kstrdup(path, GFP_NOFS);
779 	if (!req->r_path1) {
780 		root = ERR_PTR(-ENOMEM);
781 		goto out;
782 	}
783 
784 	req->r_ino1.ino = CEPH_INO_ROOT;
785 	req->r_ino1.snap = CEPH_NOSNAP;
786 	req->r_started = started;
787 	req->r_timeout = fsc->client->options->mount_timeout;
788 	req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
789 	req->r_num_caps = 2;
790 	err = ceph_mdsc_do_request(mdsc, NULL, req);
791 	if (err == 0) {
792 		struct inode *inode = req->r_target_inode;
793 		req->r_target_inode = NULL;
794 		dout("open_root_inode success\n");
795 		root = d_make_root(inode);
796 		if (!root) {
797 			root = ERR_PTR(-ENOMEM);
798 			goto out;
799 		}
800 		dout("open_root_inode success, root dentry is %p\n", root);
801 	} else {
802 		root = ERR_PTR(err);
803 	}
804 out:
805 	ceph_mdsc_put_request(req);
806 	return root;
807 }
808 
809 
810 
811 
812 /*
813  * mount: join the ceph cluster, and open root directory.
814  */
815 static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc)
816 {
817 	int err;
818 	unsigned long started = jiffies;  /* note the start time */
819 	struct dentry *root;
820 	int first = 0;   /* first vfsmount for this super_block */
821 
822 	dout("mount start %p\n", fsc);
823 	mutex_lock(&fsc->client->mount_mutex);
824 
825 	if (!fsc->sb->s_root) {
826 		const char *path;
827 		err = __ceph_open_session(fsc->client, started);
828 		if (err < 0)
829 			goto out;
830 
831 		/* setup fscache */
832 		if (fsc->mount_options->flags & CEPH_MOUNT_OPT_FSCACHE) {
833 			err = ceph_fscache_register_fs(fsc);
834 			if (err < 0)
835 				goto out;
836 		}
837 
838 		if (!fsc->mount_options->server_path) {
839 			path = "";
840 			dout("mount opening path \\t\n");
841 		} else {
842 			path = fsc->mount_options->server_path + 1;
843 			dout("mount opening path %s\n", path);
844 		}
845 		root = open_root_dentry(fsc, path, started);
846 		if (IS_ERR(root)) {
847 			err = PTR_ERR(root);
848 			goto out;
849 		}
850 		fsc->sb->s_root = dget(root);
851 		first = 1;
852 
853 		err = ceph_fs_debugfs_init(fsc);
854 		if (err < 0)
855 			goto fail;
856 	} else {
857 		root = dget(fsc->sb->s_root);
858 	}
859 
860 	fsc->mount_state = CEPH_MOUNT_MOUNTED;
861 	dout("mount success\n");
862 	mutex_unlock(&fsc->client->mount_mutex);
863 	return root;
864 
865 fail:
866 	if (first) {
867 		dput(fsc->sb->s_root);
868 		fsc->sb->s_root = NULL;
869 	}
870 out:
871 	mutex_unlock(&fsc->client->mount_mutex);
872 	return ERR_PTR(err);
873 }
874 
875 static int ceph_set_super(struct super_block *s, void *data)
876 {
877 	struct ceph_fs_client *fsc = data;
878 	int ret;
879 
880 	dout("set_super %p data %p\n", s, data);
881 
882 	s->s_flags = fsc->mount_options->sb_flags;
883 	s->s_maxbytes = 1ULL << 40;  /* temp value until we get mdsmap */
884 
885 	s->s_xattr = ceph_xattr_handlers;
886 	s->s_fs_info = fsc;
887 	fsc->sb = s;
888 
889 	s->s_op = &ceph_super_ops;
890 	s->s_d_op = &ceph_dentry_ops;
891 	s->s_export_op = &ceph_export_ops;
892 
893 	s->s_time_gran = 1000;  /* 1000 ns == 1 us */
894 
895 	ret = set_anon_super(s, NULL);  /* what is that second arg for? */
896 	if (ret != 0)
897 		goto fail;
898 
899 	return ret;
900 
901 fail:
902 	s->s_fs_info = NULL;
903 	fsc->sb = NULL;
904 	return ret;
905 }
906 
907 /*
908  * share superblock if same fs AND options
909  */
910 static int ceph_compare_super(struct super_block *sb, void *data)
911 {
912 	struct ceph_fs_client *new = data;
913 	struct ceph_mount_options *fsopt = new->mount_options;
914 	struct ceph_options *opt = new->client->options;
915 	struct ceph_fs_client *other = ceph_sb_to_client(sb);
916 
917 	dout("ceph_compare_super %p\n", sb);
918 
919 	if (compare_mount_options(fsopt, opt, other)) {
920 		dout("monitor(s)/mount options don't match\n");
921 		return 0;
922 	}
923 	if ((opt->flags & CEPH_OPT_FSID) &&
924 	    ceph_fsid_compare(&opt->fsid, &other->client->fsid)) {
925 		dout("fsid doesn't match\n");
926 		return 0;
927 	}
928 	if (fsopt->sb_flags != other->mount_options->sb_flags) {
929 		dout("flags differ\n");
930 		return 0;
931 	}
932 	return 1;
933 }
934 
935 /*
936  * construct our own bdi so we can control readahead, etc.
937  */
938 static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
939 
940 static int ceph_setup_bdi(struct super_block *sb, struct ceph_fs_client *fsc)
941 {
942 	int err;
943 
944 	err = super_setup_bdi_name(sb, "ceph-%ld",
945 				   atomic_long_inc_return(&bdi_seq));
946 	if (err)
947 		return err;
948 
949 	/* set ra_pages based on rasize mount option? */
950 	if (fsc->mount_options->rasize >= PAGE_SIZE)
951 		sb->s_bdi->ra_pages =
952 			(fsc->mount_options->rasize + PAGE_SIZE - 1)
953 			>> PAGE_SHIFT;
954 	else
955 		sb->s_bdi->ra_pages = VM_MAX_READAHEAD * 1024 / PAGE_SIZE;
956 
957 	if (fsc->mount_options->rsize > fsc->mount_options->rasize &&
958 	    fsc->mount_options->rsize >= PAGE_SIZE)
959 		sb->s_bdi->io_pages =
960 			(fsc->mount_options->rsize + PAGE_SIZE - 1)
961 			>> PAGE_SHIFT;
962 	else if (fsc->mount_options->rsize == 0)
963 		sb->s_bdi->io_pages = ULONG_MAX;
964 
965 	return 0;
966 }
967 
968 static struct dentry *ceph_mount(struct file_system_type *fs_type,
969 		       int flags, const char *dev_name, void *data)
970 {
971 	struct super_block *sb;
972 	struct ceph_fs_client *fsc;
973 	struct dentry *res;
974 	int err;
975 	int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
976 	struct ceph_mount_options *fsopt = NULL;
977 	struct ceph_options *opt = NULL;
978 
979 	dout("ceph_mount\n");
980 
981 #ifdef CONFIG_CEPH_FS_POSIX_ACL
982 	flags |= MS_POSIXACL;
983 #endif
984 	err = parse_mount_options(&fsopt, &opt, flags, data, dev_name);
985 	if (err < 0) {
986 		res = ERR_PTR(err);
987 		goto out_final;
988 	}
989 
990 	/* create client (which we may/may not use) */
991 	fsc = create_fs_client(fsopt, opt);
992 	if (IS_ERR(fsc)) {
993 		res = ERR_CAST(fsc);
994 		destroy_mount_options(fsopt);
995 		ceph_destroy_options(opt);
996 		goto out_final;
997 	}
998 
999 	err = ceph_mdsc_init(fsc);
1000 	if (err < 0) {
1001 		res = ERR_PTR(err);
1002 		goto out;
1003 	}
1004 
1005 	if (ceph_test_opt(fsc->client, NOSHARE))
1006 		compare_super = NULL;
1007 	sb = sget(fs_type, compare_super, ceph_set_super, flags, fsc);
1008 	if (IS_ERR(sb)) {
1009 		res = ERR_CAST(sb);
1010 		goto out;
1011 	}
1012 
1013 	if (ceph_sb_to_client(sb) != fsc) {
1014 		ceph_mdsc_destroy(fsc);
1015 		destroy_fs_client(fsc);
1016 		fsc = ceph_sb_to_client(sb);
1017 		dout("get_sb got existing client %p\n", fsc);
1018 	} else {
1019 		dout("get_sb using new client %p\n", fsc);
1020 		err = ceph_setup_bdi(sb, fsc);
1021 		if (err < 0) {
1022 			res = ERR_PTR(err);
1023 			goto out_splat;
1024 		}
1025 	}
1026 
1027 	res = ceph_real_mount(fsc);
1028 	if (IS_ERR(res))
1029 		goto out_splat;
1030 	dout("root %p inode %p ino %llx.%llx\n", res,
1031 	     d_inode(res), ceph_vinop(d_inode(res)));
1032 	return res;
1033 
1034 out_splat:
1035 	ceph_mdsc_close_sessions(fsc->mdsc);
1036 	deactivate_locked_super(sb);
1037 	goto out_final;
1038 
1039 out:
1040 	ceph_mdsc_destroy(fsc);
1041 	destroy_fs_client(fsc);
1042 out_final:
1043 	dout("ceph_mount fail %ld\n", PTR_ERR(res));
1044 	return res;
1045 }
1046 
1047 static void ceph_kill_sb(struct super_block *s)
1048 {
1049 	struct ceph_fs_client *fsc = ceph_sb_to_client(s);
1050 	dev_t dev = s->s_dev;
1051 
1052 	dout("kill_sb %p\n", s);
1053 
1054 	ceph_mdsc_pre_umount(fsc->mdsc);
1055 	generic_shutdown_super(s);
1056 
1057 	fsc->client->extra_mon_dispatch = NULL;
1058 	ceph_fs_debugfs_cleanup(fsc);
1059 
1060 	ceph_fscache_unregister_fs(fsc);
1061 
1062 	ceph_mdsc_destroy(fsc);
1063 
1064 	destroy_fs_client(fsc);
1065 	free_anon_bdev(dev);
1066 }
1067 
1068 static struct file_system_type ceph_fs_type = {
1069 	.owner		= THIS_MODULE,
1070 	.name		= "ceph",
1071 	.mount		= ceph_mount,
1072 	.kill_sb	= ceph_kill_sb,
1073 	.fs_flags	= FS_RENAME_DOES_D_MOVE,
1074 };
1075 MODULE_ALIAS_FS("ceph");
1076 
1077 static int __init init_ceph(void)
1078 {
1079 	int ret = init_caches();
1080 	if (ret)
1081 		goto out;
1082 
1083 	ceph_flock_init();
1084 	ceph_xattr_init();
1085 	ret = register_filesystem(&ceph_fs_type);
1086 	if (ret)
1087 		goto out_xattr;
1088 
1089 	pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
1090 
1091 	return 0;
1092 
1093 out_xattr:
1094 	ceph_xattr_exit();
1095 	destroy_caches();
1096 out:
1097 	return ret;
1098 }
1099 
1100 static void __exit exit_ceph(void)
1101 {
1102 	dout("exit_ceph\n");
1103 	unregister_filesystem(&ceph_fs_type);
1104 	ceph_xattr_exit();
1105 	destroy_caches();
1106 }
1107 
1108 module_init(init_ceph);
1109 module_exit(exit_ceph);
1110 
1111 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
1112 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
1113 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
1114 MODULE_DESCRIPTION("Ceph filesystem for Linux");
1115 MODULE_LICENSE("GPL");
1116