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