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