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