xref: /linux/fs/ceph/super.c (revision a33f32244d8550da8b4a26e277ce07d5c6d158b5)
1 
2 #include "ceph_debug.h"
3 
4 #include <linux/backing-dev.h>
5 #include <linux/fs.h>
6 #include <linux/inet.h>
7 #include <linux/in6.h>
8 #include <linux/module.h>
9 #include <linux/mount.h>
10 #include <linux/parser.h>
11 #include <linux/rwsem.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 #include <linux/version.h>
18 #include <linux/vmalloc.h>
19 
20 #include "decode.h"
21 #include "super.h"
22 #include "mon_client.h"
23 #include "auth.h"
24 
25 /*
26  * Ceph superblock operations
27  *
28  * Handle the basics of mounting, unmounting.
29  */
30 
31 
32 /*
33  * find filename portion of a path (/foo/bar/baz -> baz)
34  */
35 const char *ceph_file_part(const char *s, int len)
36 {
37 	const char *e = s + len;
38 
39 	while (e != s && *(e-1) != '/')
40 		e--;
41 	return e;
42 }
43 
44 
45 /*
46  * super ops
47  */
48 static void ceph_put_super(struct super_block *s)
49 {
50 	struct ceph_client *cl = ceph_client(s);
51 
52 	dout("put_super\n");
53 	ceph_mdsc_close_sessions(&cl->mdsc);
54 	return;
55 }
56 
57 static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
58 {
59 	struct ceph_client *client = ceph_inode_to_client(dentry->d_inode);
60 	struct ceph_monmap *monmap = client->monc.monmap;
61 	struct ceph_statfs st;
62 	u64 fsid;
63 	int err;
64 
65 	dout("statfs\n");
66 	err = ceph_monc_do_statfs(&client->monc, &st);
67 	if (err < 0)
68 		return err;
69 
70 	/* fill in kstatfs */
71 	buf->f_type = CEPH_SUPER_MAGIC;  /* ?? */
72 
73 	/*
74 	 * express utilization in terms of large blocks to avoid
75 	 * overflow on 32-bit machines.
76 	 */
77 	buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
78 	buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
79 	buf->f_bfree = (le64_to_cpu(st.kb) - le64_to_cpu(st.kb_used)) >>
80 		(CEPH_BLOCK_SHIFT-10);
81 	buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
82 
83 	buf->f_files = le64_to_cpu(st.num_objects);
84 	buf->f_ffree = -1;
85 	buf->f_namelen = PATH_MAX;
86 	buf->f_frsize = PAGE_CACHE_SIZE;
87 
88 	/* leave fsid little-endian, regardless of host endianness */
89 	fsid = *(u64 *)(&monmap->fsid) ^ *((u64 *)&monmap->fsid + 1);
90 	buf->f_fsid.val[0] = fsid & 0xffffffff;
91 	buf->f_fsid.val[1] = fsid >> 32;
92 
93 	return 0;
94 }
95 
96 
97 static int ceph_syncfs(struct super_block *sb, int wait)
98 {
99 	dout("sync_fs %d\n", wait);
100 	ceph_osdc_sync(&ceph_client(sb)->osdc);
101 	ceph_mdsc_sync(&ceph_client(sb)->mdsc);
102 	dout("sync_fs %d done\n", wait);
103 	return 0;
104 }
105 
106 
107 /**
108  * ceph_show_options - Show mount options in /proc/mounts
109  * @m: seq_file to write to
110  * @mnt: mount descriptor
111  */
112 static int ceph_show_options(struct seq_file *m, struct vfsmount *mnt)
113 {
114 	struct ceph_client *client = ceph_sb_to_client(mnt->mnt_sb);
115 	struct ceph_mount_args *args = client->mount_args;
116 
117 	if (args->flags & CEPH_OPT_FSID)
118 		seq_printf(m, ",fsidmajor=%llu,fsidminor%llu",
119 			   le64_to_cpu(*(__le64 *)&args->fsid.fsid[0]),
120 			   le64_to_cpu(*(__le64 *)&args->fsid.fsid[8]));
121 	if (args->flags & CEPH_OPT_NOSHARE)
122 		seq_puts(m, ",noshare");
123 	if (args->flags & CEPH_OPT_DIRSTAT)
124 		seq_puts(m, ",dirstat");
125 	if ((args->flags & CEPH_OPT_RBYTES) == 0)
126 		seq_puts(m, ",norbytes");
127 	if (args->flags & CEPH_OPT_NOCRC)
128 		seq_puts(m, ",nocrc");
129 	if (args->flags & CEPH_OPT_NOASYNCREADDIR)
130 		seq_puts(m, ",noasyncreaddir");
131 	if (strcmp(args->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
132 		seq_printf(m, ",snapdirname=%s", args->snapdir_name);
133 	if (args->name)
134 		seq_printf(m, ",name=%s", args->name);
135 	if (args->secret)
136 		seq_puts(m, ",secret=<hidden>");
137 	return 0;
138 }
139 
140 /*
141  * caches
142  */
143 struct kmem_cache *ceph_inode_cachep;
144 struct kmem_cache *ceph_cap_cachep;
145 struct kmem_cache *ceph_dentry_cachep;
146 struct kmem_cache *ceph_file_cachep;
147 
148 static void ceph_inode_init_once(void *foo)
149 {
150 	struct ceph_inode_info *ci = foo;
151 	inode_init_once(&ci->vfs_inode);
152 }
153 
154 static int default_congestion_kb(void)
155 {
156 	int congestion_kb;
157 
158 	/*
159 	 * Copied from NFS
160 	 *
161 	 * congestion size, scale with available memory.
162 	 *
163 	 *  64MB:    8192k
164 	 * 128MB:   11585k
165 	 * 256MB:   16384k
166 	 * 512MB:   23170k
167 	 *   1GB:   32768k
168 	 *   2GB:   46340k
169 	 *   4GB:   65536k
170 	 *   8GB:   92681k
171 	 *  16GB:  131072k
172 	 *
173 	 * This allows larger machines to have larger/more transfers.
174 	 * Limit the default to 256M
175 	 */
176 	congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
177 	if (congestion_kb > 256*1024)
178 		congestion_kb = 256*1024;
179 
180 	return congestion_kb;
181 }
182 
183 static int __init init_caches(void)
184 {
185 	ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
186 				      sizeof(struct ceph_inode_info),
187 				      __alignof__(struct ceph_inode_info),
188 				      (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
189 				      ceph_inode_init_once);
190 	if (ceph_inode_cachep == NULL)
191 		return -ENOMEM;
192 
193 	ceph_cap_cachep = KMEM_CACHE(ceph_cap,
194 				     SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
195 	if (ceph_cap_cachep == NULL)
196 		goto bad_cap;
197 
198 	ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
199 					SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
200 	if (ceph_dentry_cachep == NULL)
201 		goto bad_dentry;
202 
203 	ceph_file_cachep = KMEM_CACHE(ceph_file_info,
204 				      SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
205 	if (ceph_file_cachep == NULL)
206 		goto bad_file;
207 
208 	return 0;
209 
210 bad_file:
211 	kmem_cache_destroy(ceph_dentry_cachep);
212 bad_dentry:
213 	kmem_cache_destroy(ceph_cap_cachep);
214 bad_cap:
215 	kmem_cache_destroy(ceph_inode_cachep);
216 	return -ENOMEM;
217 }
218 
219 static void destroy_caches(void)
220 {
221 	kmem_cache_destroy(ceph_inode_cachep);
222 	kmem_cache_destroy(ceph_cap_cachep);
223 	kmem_cache_destroy(ceph_dentry_cachep);
224 	kmem_cache_destroy(ceph_file_cachep);
225 }
226 
227 
228 /*
229  * ceph_umount_begin - initiate forced umount.  Tear down down the
230  * mount, skipping steps that may hang while waiting for server(s).
231  */
232 static void ceph_umount_begin(struct super_block *sb)
233 {
234 	struct ceph_client *client = ceph_sb_to_client(sb);
235 
236 	dout("ceph_umount_begin - starting forced umount\n");
237 	if (!client)
238 		return;
239 	client->mount_state = CEPH_MOUNT_SHUTDOWN;
240 	return;
241 }
242 
243 static const struct super_operations ceph_super_ops = {
244 	.alloc_inode	= ceph_alloc_inode,
245 	.destroy_inode	= ceph_destroy_inode,
246 	.write_inode    = ceph_write_inode,
247 	.sync_fs        = ceph_syncfs,
248 	.put_super	= ceph_put_super,
249 	.show_options   = ceph_show_options,
250 	.statfs		= ceph_statfs,
251 	.umount_begin   = ceph_umount_begin,
252 };
253 
254 
255 const char *ceph_msg_type_name(int type)
256 {
257 	switch (type) {
258 	case CEPH_MSG_SHUTDOWN: return "shutdown";
259 	case CEPH_MSG_PING: return "ping";
260 	case CEPH_MSG_AUTH: return "auth";
261 	case CEPH_MSG_AUTH_REPLY: return "auth_reply";
262 	case CEPH_MSG_MON_MAP: return "mon_map";
263 	case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
264 	case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
265 	case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
266 	case CEPH_MSG_STATFS: return "statfs";
267 	case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
268 	case CEPH_MSG_MDS_MAP: return "mds_map";
269 	case CEPH_MSG_CLIENT_SESSION: return "client_session";
270 	case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
271 	case CEPH_MSG_CLIENT_REQUEST: return "client_request";
272 	case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
273 	case CEPH_MSG_CLIENT_REPLY: return "client_reply";
274 	case CEPH_MSG_CLIENT_CAPS: return "client_caps";
275 	case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
276 	case CEPH_MSG_CLIENT_SNAP: return "client_snap";
277 	case CEPH_MSG_CLIENT_LEASE: return "client_lease";
278 	case CEPH_MSG_OSD_MAP: return "osd_map";
279 	case CEPH_MSG_OSD_OP: return "osd_op";
280 	case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
281 	default: return "unknown";
282 	}
283 }
284 
285 
286 /*
287  * mount options
288  */
289 enum {
290 	Opt_fsidmajor,
291 	Opt_fsidminor,
292 	Opt_monport,
293 	Opt_wsize,
294 	Opt_rsize,
295 	Opt_osdtimeout,
296 	Opt_osdkeepalivetimeout,
297 	Opt_mount_timeout,
298 	Opt_osd_idle_ttl,
299 	Opt_caps_wanted_delay_min,
300 	Opt_caps_wanted_delay_max,
301 	Opt_readdir_max_entries,
302 	Opt_congestion_kb,
303 	Opt_last_int,
304 	/* int args above */
305 	Opt_snapdirname,
306 	Opt_name,
307 	Opt_secret,
308 	Opt_last_string,
309 	/* string args above */
310 	Opt_ip,
311 	Opt_noshare,
312 	Opt_dirstat,
313 	Opt_nodirstat,
314 	Opt_rbytes,
315 	Opt_norbytes,
316 	Opt_nocrc,
317 	Opt_noasyncreaddir,
318 };
319 
320 static match_table_t arg_tokens = {
321 	{Opt_fsidmajor, "fsidmajor=%ld"},
322 	{Opt_fsidminor, "fsidminor=%ld"},
323 	{Opt_monport, "monport=%d"},
324 	{Opt_wsize, "wsize=%d"},
325 	{Opt_rsize, "rsize=%d"},
326 	{Opt_osdtimeout, "osdtimeout=%d"},
327 	{Opt_osdkeepalivetimeout, "osdkeepalive=%d"},
328 	{Opt_mount_timeout, "mount_timeout=%d"},
329 	{Opt_osd_idle_ttl, "osd_idle_ttl=%d"},
330 	{Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
331 	{Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
332 	{Opt_readdir_max_entries, "readdir_max_entries=%d"},
333 	{Opt_congestion_kb, "write_congestion_kb=%d"},
334 	/* int args above */
335 	{Opt_snapdirname, "snapdirname=%s"},
336 	{Opt_name, "name=%s"},
337 	{Opt_secret, "secret=%s"},
338 	/* string args above */
339 	{Opt_ip, "ip=%s"},
340 	{Opt_noshare, "noshare"},
341 	{Opt_dirstat, "dirstat"},
342 	{Opt_nodirstat, "nodirstat"},
343 	{Opt_rbytes, "rbytes"},
344 	{Opt_norbytes, "norbytes"},
345 	{Opt_nocrc, "nocrc"},
346 	{Opt_noasyncreaddir, "noasyncreaddir"},
347 	{-1, NULL}
348 };
349 
350 
351 static struct ceph_mount_args *parse_mount_args(int flags, char *options,
352 						const char *dev_name,
353 						const char **path)
354 {
355 	struct ceph_mount_args *args;
356 	const char *c;
357 	int err = -ENOMEM;
358 	substring_t argstr[MAX_OPT_ARGS];
359 
360 	args = kzalloc(sizeof(*args), GFP_KERNEL);
361 	if (!args)
362 		return ERR_PTR(-ENOMEM);
363 	args->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*args->mon_addr),
364 				 GFP_KERNEL);
365 	if (!args->mon_addr)
366 		goto out;
367 
368 	dout("parse_mount_args %p, dev_name '%s'\n", args, dev_name);
369 
370 	/* start with defaults */
371 	args->sb_flags = flags;
372 	args->flags = CEPH_OPT_DEFAULT;
373 	args->osd_timeout = CEPH_OSD_TIMEOUT_DEFAULT;
374 	args->osd_keepalive_timeout = CEPH_OSD_KEEPALIVE_DEFAULT;
375 	args->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT; /* seconds */
376 	args->osd_idle_ttl = CEPH_OSD_IDLE_TTL_DEFAULT;   /* seconds */
377 	args->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
378 	args->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
379 	args->rsize = CEPH_MOUNT_RSIZE_DEFAULT;
380 	args->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
381 	args->cap_release_safety = CEPH_CAPS_PER_RELEASE * 4;
382 	args->max_readdir = 1024;
383 	args->congestion_kb = default_congestion_kb();
384 
385 	/* ip1[:port1][,ip2[:port2]...]:/subdir/in/fs */
386 	err = -EINVAL;
387 	if (!dev_name)
388 		goto out;
389 	*path = strstr(dev_name, ":/");
390 	if (*path == NULL) {
391 		pr_err("device name is missing path (no :/ in %s)\n",
392 		       dev_name);
393 		goto out;
394 	}
395 
396 	/* get mon ip(s) */
397 	err = ceph_parse_ips(dev_name, *path, args->mon_addr,
398 			     CEPH_MAX_MON, &args->num_mon);
399 	if (err < 0)
400 		goto out;
401 
402 	/* path on server */
403 	*path += 2;
404 	dout("server path '%s'\n", *path);
405 
406 	/* parse mount options */
407 	while ((c = strsep(&options, ",")) != NULL) {
408 		int token, intval, ret;
409 		if (!*c)
410 			continue;
411 		err = -EINVAL;
412 		token = match_token((char *)c, arg_tokens, argstr);
413 		if (token < 0) {
414 			pr_err("bad mount option at '%s'\n", c);
415 			goto out;
416 		}
417 		if (token < Opt_last_int) {
418 			ret = match_int(&argstr[0], &intval);
419 			if (ret < 0) {
420 				pr_err("bad mount option arg (not int) "
421 				       "at '%s'\n", c);
422 				continue;
423 			}
424 			dout("got int token %d val %d\n", token, intval);
425 		} else if (token > Opt_last_int && token < Opt_last_string) {
426 			dout("got string token %d val %s\n", token,
427 			     argstr[0].from);
428 		} else {
429 			dout("got token %d\n", token);
430 		}
431 		switch (token) {
432 		case Opt_fsidmajor:
433 			*(__le64 *)&args->fsid.fsid[0] = cpu_to_le64(intval);
434 			break;
435 		case Opt_fsidminor:
436 			*(__le64 *)&args->fsid.fsid[8] = cpu_to_le64(intval);
437 			break;
438 		case Opt_ip:
439 			err = ceph_parse_ips(argstr[0].from,
440 					     argstr[0].to,
441 					     &args->my_addr,
442 					     1, NULL);
443 			if (err < 0)
444 				goto out;
445 			args->flags |= CEPH_OPT_MYIP;
446 			break;
447 
448 		case Opt_snapdirname:
449 			kfree(args->snapdir_name);
450 			args->snapdir_name = kstrndup(argstr[0].from,
451 					      argstr[0].to-argstr[0].from,
452 					      GFP_KERNEL);
453 			break;
454 		case Opt_name:
455 			args->name = kstrndup(argstr[0].from,
456 					      argstr[0].to-argstr[0].from,
457 					      GFP_KERNEL);
458 			break;
459 		case Opt_secret:
460 			args->secret = kstrndup(argstr[0].from,
461 						argstr[0].to-argstr[0].from,
462 						GFP_KERNEL);
463 			break;
464 
465 			/* misc */
466 		case Opt_wsize:
467 			args->wsize = intval;
468 			break;
469 		case Opt_rsize:
470 			args->rsize = intval;
471 			break;
472 		case Opt_osdtimeout:
473 			args->osd_timeout = intval;
474 			break;
475 		case Opt_osdkeepalivetimeout:
476 			args->osd_keepalive_timeout = intval;
477 			break;
478 		case Opt_mount_timeout:
479 			args->mount_timeout = intval;
480 			break;
481 		case Opt_caps_wanted_delay_min:
482 			args->caps_wanted_delay_min = intval;
483 			break;
484 		case Opt_caps_wanted_delay_max:
485 			args->caps_wanted_delay_max = intval;
486 			break;
487 		case Opt_readdir_max_entries:
488 			args->max_readdir = intval;
489 			break;
490 		case Opt_congestion_kb:
491 			args->congestion_kb = intval;
492 			break;
493 
494 		case Opt_noshare:
495 			args->flags |= CEPH_OPT_NOSHARE;
496 			break;
497 
498 		case Opt_dirstat:
499 			args->flags |= CEPH_OPT_DIRSTAT;
500 			break;
501 		case Opt_nodirstat:
502 			args->flags &= ~CEPH_OPT_DIRSTAT;
503 			break;
504 		case Opt_rbytes:
505 			args->flags |= CEPH_OPT_RBYTES;
506 			break;
507 		case Opt_norbytes:
508 			args->flags &= ~CEPH_OPT_RBYTES;
509 			break;
510 		case Opt_nocrc:
511 			args->flags |= CEPH_OPT_NOCRC;
512 			break;
513 		case Opt_noasyncreaddir:
514 			args->flags |= CEPH_OPT_NOASYNCREADDIR;
515 			break;
516 
517 		default:
518 			BUG_ON(token);
519 		}
520 	}
521 	return args;
522 
523 out:
524 	kfree(args->mon_addr);
525 	kfree(args);
526 	return ERR_PTR(err);
527 }
528 
529 static void destroy_mount_args(struct ceph_mount_args *args)
530 {
531 	dout("destroy_mount_args %p\n", args);
532 	kfree(args->snapdir_name);
533 	args->snapdir_name = NULL;
534 	kfree(args->name);
535 	args->name = NULL;
536 	kfree(args->secret);
537 	args->secret = NULL;
538 	kfree(args);
539 }
540 
541 /*
542  * create a fresh client instance
543  */
544 static struct ceph_client *ceph_create_client(struct ceph_mount_args *args)
545 {
546 	struct ceph_client *client;
547 	int err = -ENOMEM;
548 
549 	client = kzalloc(sizeof(*client), GFP_KERNEL);
550 	if (client == NULL)
551 		return ERR_PTR(-ENOMEM);
552 
553 	mutex_init(&client->mount_mutex);
554 
555 	init_waitqueue_head(&client->auth_wq);
556 
557 	client->sb = NULL;
558 	client->mount_state = CEPH_MOUNT_MOUNTING;
559 	client->mount_args = args;
560 
561 	client->msgr = NULL;
562 
563 	client->auth_err = 0;
564 	atomic_long_set(&client->writeback_count, 0);
565 
566 	err = bdi_init(&client->backing_dev_info);
567 	if (err < 0)
568 		goto fail;
569 
570 	err = -ENOMEM;
571 	client->wb_wq = create_workqueue("ceph-writeback");
572 	if (client->wb_wq == NULL)
573 		goto fail_bdi;
574 	client->pg_inv_wq = create_singlethread_workqueue("ceph-pg-invalid");
575 	if (client->pg_inv_wq == NULL)
576 		goto fail_wb_wq;
577 	client->trunc_wq = create_singlethread_workqueue("ceph-trunc");
578 	if (client->trunc_wq == NULL)
579 		goto fail_pg_inv_wq;
580 
581 	/* set up mempools */
582 	err = -ENOMEM;
583 	client->wb_pagevec_pool = mempool_create_kmalloc_pool(10,
584 			      client->mount_args->wsize >> PAGE_CACHE_SHIFT);
585 	if (!client->wb_pagevec_pool)
586 		goto fail_trunc_wq;
587 
588 	/* caps */
589 	client->min_caps = args->max_readdir;
590 	ceph_adjust_min_caps(client->min_caps);
591 
592 	/* subsystems */
593 	err = ceph_monc_init(&client->monc, client);
594 	if (err < 0)
595 		goto fail_mempool;
596 	err = ceph_osdc_init(&client->osdc, client);
597 	if (err < 0)
598 		goto fail_monc;
599 	err = ceph_mdsc_init(&client->mdsc, client);
600 	if (err < 0)
601 		goto fail_osdc;
602 	return client;
603 
604 fail_osdc:
605 	ceph_osdc_stop(&client->osdc);
606 fail_monc:
607 	ceph_monc_stop(&client->monc);
608 fail_mempool:
609 	mempool_destroy(client->wb_pagevec_pool);
610 fail_trunc_wq:
611 	destroy_workqueue(client->trunc_wq);
612 fail_pg_inv_wq:
613 	destroy_workqueue(client->pg_inv_wq);
614 fail_wb_wq:
615 	destroy_workqueue(client->wb_wq);
616 fail_bdi:
617 	bdi_destroy(&client->backing_dev_info);
618 fail:
619 	kfree(client);
620 	return ERR_PTR(err);
621 }
622 
623 static void ceph_destroy_client(struct ceph_client *client)
624 {
625 	dout("destroy_client %p\n", client);
626 
627 	/* unmount */
628 	ceph_mdsc_stop(&client->mdsc);
629 	ceph_monc_stop(&client->monc);
630 	ceph_osdc_stop(&client->osdc);
631 
632 	ceph_adjust_min_caps(-client->min_caps);
633 
634 	ceph_debugfs_client_cleanup(client);
635 	destroy_workqueue(client->wb_wq);
636 	destroy_workqueue(client->pg_inv_wq);
637 	destroy_workqueue(client->trunc_wq);
638 
639 	if (client->msgr)
640 		ceph_messenger_destroy(client->msgr);
641 	mempool_destroy(client->wb_pagevec_pool);
642 
643 	destroy_mount_args(client->mount_args);
644 
645 	kfree(client);
646 	dout("destroy_client %p done\n", client);
647 }
648 
649 /*
650  * Initially learn our fsid, or verify an fsid matches.
651  */
652 int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
653 {
654 	if (client->have_fsid) {
655 		if (ceph_fsid_compare(&client->fsid, fsid)) {
656 			pr_err("bad fsid, had " FSID_FORMAT " got " FSID_FORMAT,
657 			       PR_FSID(&client->fsid), PR_FSID(fsid));
658 			return -1;
659 		}
660 	} else {
661 		pr_info("client%lld fsid " FSID_FORMAT "\n",
662 			client->monc.auth->global_id, PR_FSID(fsid));
663 		memcpy(&client->fsid, fsid, sizeof(*fsid));
664 		ceph_debugfs_client_init(client);
665 		client->have_fsid = true;
666 	}
667 	return 0;
668 }
669 
670 /*
671  * true if we have the mon map (and have thus joined the cluster)
672  */
673 static int have_mon_map(struct ceph_client *client)
674 {
675 	return client->monc.monmap && client->monc.monmap->epoch;
676 }
677 
678 /*
679  * Bootstrap mount by opening the root directory.  Note the mount
680  * @started time from caller, and time out if this takes too long.
681  */
682 static struct dentry *open_root_dentry(struct ceph_client *client,
683 				       const char *path,
684 				       unsigned long started)
685 {
686 	struct ceph_mds_client *mdsc = &client->mdsc;
687 	struct ceph_mds_request *req = NULL;
688 	int err;
689 	struct dentry *root;
690 
691 	/* open dir */
692 	dout("open_root_inode opening '%s'\n", path);
693 	req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
694 	if (IS_ERR(req))
695 		return ERR_PTR(PTR_ERR(req));
696 	req->r_path1 = kstrdup(path, GFP_NOFS);
697 	req->r_ino1.ino = CEPH_INO_ROOT;
698 	req->r_ino1.snap = CEPH_NOSNAP;
699 	req->r_started = started;
700 	req->r_timeout = client->mount_args->mount_timeout * HZ;
701 	req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
702 	req->r_num_caps = 2;
703 	err = ceph_mdsc_do_request(mdsc, NULL, req);
704 	if (err == 0) {
705 		dout("open_root_inode success\n");
706 		if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
707 		    client->sb->s_root == NULL)
708 			root = d_alloc_root(req->r_target_inode);
709 		else
710 			root = d_obtain_alias(req->r_target_inode);
711 		req->r_target_inode = NULL;
712 		dout("open_root_inode success, root dentry is %p\n", root);
713 	} else {
714 		root = ERR_PTR(err);
715 	}
716 	ceph_mdsc_put_request(req);
717 	return root;
718 }
719 
720 /*
721  * mount: join the ceph cluster, and open root directory.
722  */
723 static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt,
724 		      const char *path)
725 {
726 	struct ceph_entity_addr *myaddr = NULL;
727 	int err;
728 	unsigned long timeout = client->mount_args->mount_timeout * HZ;
729 	unsigned long started = jiffies;  /* note the start time */
730 	struct dentry *root;
731 
732 	dout("mount start\n");
733 	mutex_lock(&client->mount_mutex);
734 
735 	/* initialize the messenger */
736 	if (client->msgr == NULL) {
737 		if (ceph_test_opt(client, MYIP))
738 			myaddr = &client->mount_args->my_addr;
739 		client->msgr = ceph_messenger_create(myaddr);
740 		if (IS_ERR(client->msgr)) {
741 			err = PTR_ERR(client->msgr);
742 			client->msgr = NULL;
743 			goto out;
744 		}
745 		client->msgr->nocrc = ceph_test_opt(client, NOCRC);
746 	}
747 
748 	/* open session, and wait for mon, mds, and osd maps */
749 	err = ceph_monc_open_session(&client->monc);
750 	if (err < 0)
751 		goto out;
752 
753 	while (!have_mon_map(client)) {
754 		err = -EIO;
755 		if (timeout && time_after_eq(jiffies, started + timeout))
756 			goto out;
757 
758 		/* wait */
759 		dout("mount waiting for mon_map\n");
760 		err = wait_event_interruptible_timeout(client->auth_wq,
761 			       have_mon_map(client) || (client->auth_err < 0),
762 			       timeout);
763 		if (err == -EINTR || err == -ERESTARTSYS)
764 			goto out;
765 		if (client->auth_err < 0) {
766 			err = client->auth_err;
767 			goto out;
768 		}
769 	}
770 
771 	dout("mount opening root\n");
772 	root = open_root_dentry(client, "", started);
773 	if (IS_ERR(root)) {
774 		err = PTR_ERR(root);
775 		goto out;
776 	}
777 	if (client->sb->s_root)
778 		dput(root);
779 	else
780 		client->sb->s_root = root;
781 
782 	if (path[0] == 0) {
783 		dget(root);
784 	} else {
785 		dout("mount opening base mountpoint\n");
786 		root = open_root_dentry(client, path, started);
787 		if (IS_ERR(root)) {
788 			err = PTR_ERR(root);
789 			dput(client->sb->s_root);
790 			client->sb->s_root = NULL;
791 			goto out;
792 		}
793 	}
794 
795 	mnt->mnt_root = root;
796 	mnt->mnt_sb = client->sb;
797 
798 	client->mount_state = CEPH_MOUNT_MOUNTED;
799 	dout("mount success\n");
800 	err = 0;
801 
802 out:
803 	mutex_unlock(&client->mount_mutex);
804 	return err;
805 }
806 
807 static int ceph_set_super(struct super_block *s, void *data)
808 {
809 	struct ceph_client *client = data;
810 	int ret;
811 
812 	dout("set_super %p data %p\n", s, data);
813 
814 	s->s_flags = client->mount_args->sb_flags;
815 	s->s_maxbytes = 1ULL << 40;  /* temp value until we get mdsmap */
816 
817 	s->s_fs_info = client;
818 	client->sb = s;
819 
820 	s->s_op = &ceph_super_ops;
821 	s->s_export_op = &ceph_export_ops;
822 
823 	s->s_time_gran = 1000;  /* 1000 ns == 1 us */
824 
825 	ret = set_anon_super(s, NULL);  /* what is that second arg for? */
826 	if (ret != 0)
827 		goto fail;
828 
829 	return ret;
830 
831 fail:
832 	s->s_fs_info = NULL;
833 	client->sb = NULL;
834 	return ret;
835 }
836 
837 /*
838  * share superblock if same fs AND options
839  */
840 static int ceph_compare_super(struct super_block *sb, void *data)
841 {
842 	struct ceph_client *new = data;
843 	struct ceph_mount_args *args = new->mount_args;
844 	struct ceph_client *other = ceph_sb_to_client(sb);
845 	int i;
846 
847 	dout("ceph_compare_super %p\n", sb);
848 	if (args->flags & CEPH_OPT_FSID) {
849 		if (ceph_fsid_compare(&args->fsid, &other->fsid)) {
850 			dout("fsid doesn't match\n");
851 			return 0;
852 		}
853 	} else {
854 		/* do we share (a) monitor? */
855 		for (i = 0; i < new->monc.monmap->num_mon; i++)
856 			if (ceph_monmap_contains(other->monc.monmap,
857 					 &new->monc.monmap->mon_inst[i].addr))
858 				break;
859 		if (i == new->monc.monmap->num_mon) {
860 			dout("mon ip not part of monmap\n");
861 			return 0;
862 		}
863 		dout("mon ip matches existing sb %p\n", sb);
864 	}
865 	if (args->sb_flags != other->mount_args->sb_flags) {
866 		dout("flags differ\n");
867 		return 0;
868 	}
869 	return 1;
870 }
871 
872 /*
873  * construct our own bdi so we can control readahead, etc.
874  */
875 static int ceph_register_bdi(struct super_block *sb, struct ceph_client *client)
876 {
877 	int err;
878 
879 	sb->s_bdi = &client->backing_dev_info;
880 
881 	/* set ra_pages based on rsize mount option? */
882 	if (client->mount_args->rsize >= PAGE_CACHE_SIZE)
883 		client->backing_dev_info.ra_pages =
884 			(client->mount_args->rsize + PAGE_CACHE_SIZE - 1)
885 			>> PAGE_SHIFT;
886 	err = bdi_register_dev(&client->backing_dev_info, sb->s_dev);
887 	return err;
888 }
889 
890 static int ceph_get_sb(struct file_system_type *fs_type,
891 		       int flags, const char *dev_name, void *data,
892 		       struct vfsmount *mnt)
893 {
894 	struct super_block *sb;
895 	struct ceph_client *client;
896 	int err;
897 	int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
898 	const char *path = NULL;
899 	struct ceph_mount_args *args;
900 
901 	dout("ceph_get_sb\n");
902 	args = parse_mount_args(flags, data, dev_name, &path);
903 	if (IS_ERR(args)) {
904 		err = PTR_ERR(args);
905 		goto out_final;
906 	}
907 
908 	/* create client (which we may/may not use) */
909 	client = ceph_create_client(args);
910 	if (IS_ERR(client)) {
911 		err = PTR_ERR(client);
912 		goto out_final;
913 	}
914 
915 	if (client->mount_args->flags & CEPH_OPT_NOSHARE)
916 		compare_super = NULL;
917 	sb = sget(fs_type, compare_super, ceph_set_super, client);
918 	if (IS_ERR(sb)) {
919 		err = PTR_ERR(sb);
920 		goto out;
921 	}
922 
923 	if (ceph_client(sb) != client) {
924 		ceph_destroy_client(client);
925 		client = ceph_client(sb);
926 		dout("get_sb got existing client %p\n", client);
927 	} else {
928 		dout("get_sb using new client %p\n", client);
929 		err = ceph_register_bdi(sb, client);
930 		if (err < 0)
931 			goto out_splat;
932 	}
933 
934 	err = ceph_mount(client, mnt, path);
935 	if (err < 0)
936 		goto out_splat;
937 	dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root,
938 	     mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode));
939 	return 0;
940 
941 out_splat:
942 	ceph_mdsc_close_sessions(&client->mdsc);
943 	up_write(&sb->s_umount);
944 	deactivate_super(sb);
945 	goto out_final;
946 
947 out:
948 	ceph_destroy_client(client);
949 out_final:
950 	dout("ceph_get_sb fail %d\n", err);
951 	return err;
952 }
953 
954 static void ceph_kill_sb(struct super_block *s)
955 {
956 	struct ceph_client *client = ceph_sb_to_client(s);
957 	dout("kill_sb %p\n", s);
958 	ceph_mdsc_pre_umount(&client->mdsc);
959 	kill_anon_super(s);    /* will call put_super after sb is r/o */
960 	if (s->s_bdi == &client->backing_dev_info)
961 		bdi_unregister(&client->backing_dev_info);
962 	bdi_destroy(&client->backing_dev_info);
963 	ceph_destroy_client(client);
964 }
965 
966 static struct file_system_type ceph_fs_type = {
967 	.owner		= THIS_MODULE,
968 	.name		= "ceph",
969 	.get_sb		= ceph_get_sb,
970 	.kill_sb	= ceph_kill_sb,
971 	.fs_flags	= FS_RENAME_DOES_D_MOVE,
972 };
973 
974 #define _STRINGIFY(x) #x
975 #define STRINGIFY(x) _STRINGIFY(x)
976 
977 static int __init init_ceph(void)
978 {
979 	int ret = 0;
980 
981 	ret = ceph_debugfs_init();
982 	if (ret < 0)
983 		goto out;
984 
985 	ret = ceph_msgr_init();
986 	if (ret < 0)
987 		goto out_debugfs;
988 
989 	ret = init_caches();
990 	if (ret)
991 		goto out_msgr;
992 
993 	ceph_caps_init();
994 
995 	ret = register_filesystem(&ceph_fs_type);
996 	if (ret)
997 		goto out_icache;
998 
999 	pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n",
1000 		CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH,
1001 		CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL);
1002 	return 0;
1003 
1004 out_icache:
1005 	destroy_caches();
1006 out_msgr:
1007 	ceph_msgr_exit();
1008 out_debugfs:
1009 	ceph_debugfs_cleanup();
1010 out:
1011 	return ret;
1012 }
1013 
1014 static void __exit exit_ceph(void)
1015 {
1016 	dout("exit_ceph\n");
1017 	unregister_filesystem(&ceph_fs_type);
1018 	ceph_caps_finalize();
1019 	destroy_caches();
1020 	ceph_msgr_exit();
1021 	ceph_debugfs_cleanup();
1022 }
1023 
1024 module_init(init_ceph);
1025 module_exit(exit_ceph);
1026 
1027 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
1028 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
1029 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
1030 MODULE_DESCRIPTION("Ceph filesystem for Linux");
1031 MODULE_LICENSE("GPL");
1032