blob: 6d02a166f8ffe6d61068ced19f030063311d9408 [file] [log] [blame]
Sage Weil16725b92009-10-06 11:31:07 -07001
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
Sage Weil16725b92009-10-06 11:31:07 -070019#include "decode.h"
20#include "super.h"
21#include "mon_client.h"
Sage Weil07433042009-11-18 16:50:41 -080022#include "auth.h"
Sage Weil16725b92009-10-06 11:31:07 -070023
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);
Sage Weilf2cf4182009-10-14 14:09:07 -0700101 dout("sync_fs %d done\n", wait);
Sage Weil16725b92009-10-06 11:31:07 -0700102 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);
Sage Weil6b805182009-10-27 11:50:50 -0700114 struct ceph_mount_args *args = client->mount_args;
Sage Weil16725b92009-10-06 11:31:07 -0700115
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);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800132 if (args->name)
133 seq_printf(m, ",name=%s", args->name);
Sage Weil16725b92009-10-06 11:31:07 -0700134 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
153static int __init init_caches(void)
154{
155 ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
156 sizeof(struct ceph_inode_info),
157 __alignof__(struct ceph_inode_info),
158 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
159 ceph_inode_init_once);
160 if (ceph_inode_cachep == NULL)
161 return -ENOMEM;
162
163 ceph_cap_cachep = KMEM_CACHE(ceph_cap,
164 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
165 if (ceph_cap_cachep == NULL)
166 goto bad_cap;
167
168 ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
169 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
170 if (ceph_dentry_cachep == NULL)
171 goto bad_dentry;
172
173 ceph_file_cachep = KMEM_CACHE(ceph_file_info,
174 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
175 if (ceph_file_cachep == NULL)
176 goto bad_file;
177
178 return 0;
179
180bad_file:
181 kmem_cache_destroy(ceph_dentry_cachep);
182bad_dentry:
183 kmem_cache_destroy(ceph_cap_cachep);
184bad_cap:
185 kmem_cache_destroy(ceph_inode_cachep);
186 return -ENOMEM;
187}
188
189static void destroy_caches(void)
190{
191 kmem_cache_destroy(ceph_inode_cachep);
192 kmem_cache_destroy(ceph_cap_cachep);
193 kmem_cache_destroy(ceph_dentry_cachep);
194 kmem_cache_destroy(ceph_file_cachep);
195}
196
197
198/*
199 * ceph_umount_begin - initiate forced umount. Tear down down the
200 * mount, skipping steps that may hang while waiting for server(s).
201 */
202static void ceph_umount_begin(struct super_block *sb)
203{
204 struct ceph_client *client = ceph_sb_to_client(sb);
205
206 dout("ceph_umount_begin - starting forced umount\n");
207 if (!client)
208 return;
209 client->mount_state = CEPH_MOUNT_SHUTDOWN;
210 return;
211}
212
213static const struct super_operations ceph_super_ops = {
214 .alloc_inode = ceph_alloc_inode,
215 .destroy_inode = ceph_destroy_inode,
216 .write_inode = ceph_write_inode,
217 .sync_fs = ceph_syncfs,
218 .put_super = ceph_put_super,
219 .show_options = ceph_show_options,
220 .statfs = ceph_statfs,
221 .umount_begin = ceph_umount_begin,
222};
223
224
225const char *ceph_msg_type_name(int type)
226{
227 switch (type) {
228 case CEPH_MSG_SHUTDOWN: return "shutdown";
229 case CEPH_MSG_PING: return "ping";
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800230 case CEPH_MSG_AUTH: return "auth";
231 case CEPH_MSG_AUTH_REPLY: return "auth_reply";
Sage Weil16725b92009-10-06 11:31:07 -0700232 case CEPH_MSG_MON_MAP: return "mon_map";
233 case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
234 case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
235 case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
Sage Weil16725b92009-10-06 11:31:07 -0700236 case CEPH_MSG_STATFS: return "statfs";
237 case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
Sage Weil16725b92009-10-06 11:31:07 -0700238 case CEPH_MSG_MDS_MAP: return "mds_map";
239 case CEPH_MSG_CLIENT_SESSION: return "client_session";
240 case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
241 case CEPH_MSG_CLIENT_REQUEST: return "client_request";
242 case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
243 case CEPH_MSG_CLIENT_REPLY: return "client_reply";
244 case CEPH_MSG_CLIENT_CAPS: return "client_caps";
245 case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
246 case CEPH_MSG_CLIENT_SNAP: return "client_snap";
247 case CEPH_MSG_CLIENT_LEASE: return "client_lease";
Sage Weil16725b92009-10-06 11:31:07 -0700248 case CEPH_MSG_OSD_MAP: return "osd_map";
249 case CEPH_MSG_OSD_OP: return "osd_op";
250 case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
251 default: return "unknown";
252 }
253}
254
255
256/*
257 * mount options
258 */
259enum {
260 Opt_fsidmajor,
261 Opt_fsidminor,
262 Opt_monport,
263 Opt_wsize,
264 Opt_rsize,
265 Opt_osdtimeout,
266 Opt_mount_timeout,
267 Opt_caps_wanted_delay_min,
268 Opt_caps_wanted_delay_max,
269 Opt_readdir_max_entries,
Sage Weile53c2fe2009-10-27 10:19:28 -0700270 Opt_last_int,
Sage Weil16725b92009-10-06 11:31:07 -0700271 /* int args above */
272 Opt_snapdirname,
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800273 Opt_name,
Sage Weil16725b92009-10-06 11:31:07 -0700274 Opt_secret,
Sage Weile53c2fe2009-10-27 10:19:28 -0700275 Opt_last_string,
Sage Weil16725b92009-10-06 11:31:07 -0700276 /* string args above */
277 Opt_ip,
278 Opt_noshare,
279 Opt_dirstat,
280 Opt_nodirstat,
281 Opt_rbytes,
282 Opt_norbytes,
283 Opt_nocrc,
284 Opt_noasyncreaddir,
285};
286
287static match_table_t arg_tokens = {
288 {Opt_fsidmajor, "fsidmajor=%ld"},
289 {Opt_fsidminor, "fsidminor=%ld"},
290 {Opt_monport, "monport=%d"},
291 {Opt_wsize, "wsize=%d"},
292 {Opt_rsize, "rsize=%d"},
293 {Opt_osdtimeout, "osdtimeout=%d"},
294 {Opt_mount_timeout, "mount_timeout=%d"},
295 {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
296 {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
297 {Opt_readdir_max_entries, "readdir_max_entries=%d"},
298 /* int args above */
299 {Opt_snapdirname, "snapdirname=%s"},
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800300 {Opt_name, "name=%s"},
Sage Weil16725b92009-10-06 11:31:07 -0700301 {Opt_secret, "secret=%s"},
302 /* string args above */
303 {Opt_ip, "ip=%s"},
304 {Opt_noshare, "noshare"},
305 {Opt_dirstat, "dirstat"},
306 {Opt_nodirstat, "nodirstat"},
307 {Opt_rbytes, "rbytes"},
308 {Opt_norbytes, "norbytes"},
309 {Opt_nocrc, "nocrc"},
310 {Opt_noasyncreaddir, "noasyncreaddir"},
311 {-1, NULL}
312};
313
314
Sage Weil6b805182009-10-27 11:50:50 -0700315static struct ceph_mount_args *parse_mount_args(int flags, char *options,
316 const char *dev_name,
317 const char **path)
Sage Weil16725b92009-10-06 11:31:07 -0700318{
Sage Weil6b805182009-10-27 11:50:50 -0700319 struct ceph_mount_args *args;
Sage Weil16725b92009-10-06 11:31:07 -0700320 const char *c;
Sage Weil6b805182009-10-27 11:50:50 -0700321 int err = -ENOMEM;
Sage Weil16725b92009-10-06 11:31:07 -0700322 substring_t argstr[MAX_OPT_ARGS];
Sage Weil16725b92009-10-06 11:31:07 -0700323
Sage Weil6b805182009-10-27 11:50:50 -0700324 args = kzalloc(sizeof(*args), GFP_KERNEL);
325 if (!args)
326 return ERR_PTR(-ENOMEM);
327 args->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*args->mon_addr),
328 GFP_KERNEL);
329 if (!args->mon_addr)
330 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700331
Sage Weil6b805182009-10-27 11:50:50 -0700332 dout("parse_mount_args %p, dev_name '%s'\n", args, dev_name);
Sage Weil7b813c462009-10-26 22:07:53 -0700333
Sage Weil16725b92009-10-06 11:31:07 -0700334 /* start with defaults */
335 args->sb_flags = flags;
336 args->flags = CEPH_OPT_DEFAULT;
337 args->osd_timeout = 5; /* seconds */
338 args->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT; /* seconds */
339 args->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
340 args->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
Sage Weil8fa97652009-10-16 14:44:35 -0700341 args->rsize = CEPH_MOUNT_RSIZE_DEFAULT;
Sage Weil16725b92009-10-06 11:31:07 -0700342 args->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
343 args->cap_release_safety = CEPH_CAPS_PER_RELEASE * 4;
344 args->max_readdir = 1024;
345
346 /* ip1[:port1][,ip2[:port2]...]:/subdir/in/fs */
Sage Weil7b813c462009-10-26 22:07:53 -0700347 err = -EINVAL;
Sage Weil16725b92009-10-06 11:31:07 -0700348 if (!dev_name)
Sage Weil7b813c462009-10-26 22:07:53 -0700349 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700350 *path = strstr(dev_name, ":/");
351 if (*path == NULL) {
352 pr_err("device name is missing path (no :/ in %s)\n",
353 dev_name);
Sage Weil7b813c462009-10-26 22:07:53 -0700354 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700355 }
356
357 /* get mon ip(s) */
Sage Weil6b805182009-10-27 11:50:50 -0700358 err = ceph_parse_ips(dev_name, *path, args->mon_addr,
359 CEPH_MAX_MON, &args->num_mon);
Sage Weil16725b92009-10-06 11:31:07 -0700360 if (err < 0)
Sage Weil7b813c462009-10-26 22:07:53 -0700361 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700362
Sage Weil16725b92009-10-06 11:31:07 -0700363 /* path on server */
364 *path += 2;
365 dout("server path '%s'\n", *path);
366
367 /* parse mount options */
368 while ((c = strsep(&options, ",")) != NULL) {
369 int token, intval, ret;
370 if (!*c)
371 continue;
Sage Weil7b813c462009-10-26 22:07:53 -0700372 err = -EINVAL;
Sage Weil16725b92009-10-06 11:31:07 -0700373 token = match_token((char *)c, arg_tokens, argstr);
374 if (token < 0) {
375 pr_err("bad mount option at '%s'\n", c);
Sage Weil7b813c462009-10-26 22:07:53 -0700376 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700377 }
Sage Weile53c2fe2009-10-27 10:19:28 -0700378 if (token < Opt_last_int) {
Sage Weil16725b92009-10-06 11:31:07 -0700379 ret = match_int(&argstr[0], &intval);
380 if (ret < 0) {
381 pr_err("bad mount option arg (not int) "
382 "at '%s'\n", c);
383 continue;
384 }
Sage Weile53c2fe2009-10-27 10:19:28 -0700385 dout("got int token %d val %d\n", token, intval);
386 } else if (token > Opt_last_int && token < Opt_last_string) {
387 dout("got string token %d val %s\n", token,
388 argstr[0].from);
389 } else {
390 dout("got token %d\n", token);
Sage Weil16725b92009-10-06 11:31:07 -0700391 }
392 switch (token) {
393 case Opt_fsidmajor:
394 *(__le64 *)&args->fsid.fsid[0] = cpu_to_le64(intval);
395 break;
396 case Opt_fsidminor:
397 *(__le64 *)&args->fsid.fsid[8] = cpu_to_le64(intval);
398 break;
399 case Opt_ip:
400 err = ceph_parse_ips(argstr[0].from,
401 argstr[0].to,
402 &args->my_addr,
403 1, NULL);
404 if (err < 0)
Sage Weil6b805182009-10-27 11:50:50 -0700405 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700406 args->flags |= CEPH_OPT_MYIP;
407 break;
408
409 case Opt_snapdirname:
410 kfree(args->snapdir_name);
411 args->snapdir_name = kstrndup(argstr[0].from,
412 argstr[0].to-argstr[0].from,
413 GFP_KERNEL);
414 break;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800415 case Opt_name:
416 args->name = kstrndup(argstr[0].from,
417 argstr[0].to-argstr[0].from,
418 GFP_KERNEL);
419 break;
Sage Weil16725b92009-10-06 11:31:07 -0700420 case Opt_secret:
421 args->secret = kstrndup(argstr[0].from,
422 argstr[0].to-argstr[0].from,
423 GFP_KERNEL);
424 break;
425
426 /* misc */
427 case Opt_wsize:
428 args->wsize = intval;
429 break;
430 case Opt_rsize:
431 args->rsize = intval;
432 break;
433 case Opt_osdtimeout:
434 args->osd_timeout = intval;
435 break;
436 case Opt_mount_timeout:
437 args->mount_timeout = intval;
438 break;
439 case Opt_caps_wanted_delay_min:
440 args->caps_wanted_delay_min = intval;
441 break;
442 case Opt_caps_wanted_delay_max:
443 args->caps_wanted_delay_max = intval;
444 break;
445 case Opt_readdir_max_entries:
446 args->max_readdir = intval;
447 break;
448
449 case Opt_noshare:
450 args->flags |= CEPH_OPT_NOSHARE;
451 break;
452
453 case Opt_dirstat:
454 args->flags |= CEPH_OPT_DIRSTAT;
455 break;
456 case Opt_nodirstat:
457 args->flags &= ~CEPH_OPT_DIRSTAT;
458 break;
459 case Opt_rbytes:
460 args->flags |= CEPH_OPT_RBYTES;
461 break;
462 case Opt_norbytes:
463 args->flags &= ~CEPH_OPT_RBYTES;
464 break;
465 case Opt_nocrc:
466 args->flags |= CEPH_OPT_NOCRC;
467 break;
468 case Opt_noasyncreaddir:
469 args->flags |= CEPH_OPT_NOASYNCREADDIR;
470 break;
471
472 default:
473 BUG_ON(token);
474 }
475 }
Sage Weil6b805182009-10-27 11:50:50 -0700476 return args;
Sage Weil16725b92009-10-06 11:31:07 -0700477
Sage Weil7b813c462009-10-26 22:07:53 -0700478out:
Sage Weil6b805182009-10-27 11:50:50 -0700479 kfree(args->mon_addr);
480 kfree(args);
481 return ERR_PTR(err);
Sage Weil16725b92009-10-06 11:31:07 -0700482}
483
Sage Weil6b805182009-10-27 11:50:50 -0700484static void destroy_mount_args(struct ceph_mount_args *args)
Sage Weil16725b92009-10-06 11:31:07 -0700485{
Sage Weil6b805182009-10-27 11:50:50 -0700486 dout("destroy_mount_args %p\n", args);
Sage Weil16725b92009-10-06 11:31:07 -0700487 kfree(args->snapdir_name);
488 args->snapdir_name = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800489 kfree(args->name);
490 args->name = NULL;
Sage Weil16725b92009-10-06 11:31:07 -0700491 kfree(args->secret);
492 args->secret = NULL;
Sage Weil6b805182009-10-27 11:50:50 -0700493 kfree(args);
Sage Weil16725b92009-10-06 11:31:07 -0700494}
495
496/*
497 * create a fresh client instance
498 */
Sage Weil6b805182009-10-27 11:50:50 -0700499static struct ceph_client *ceph_create_client(struct ceph_mount_args *args)
Sage Weil16725b92009-10-06 11:31:07 -0700500{
501 struct ceph_client *client;
502 int err = -ENOMEM;
503
504 client = kzalloc(sizeof(*client), GFP_KERNEL);
505 if (client == NULL)
506 return ERR_PTR(-ENOMEM);
507
508 mutex_init(&client->mount_mutex);
509
510 init_waitqueue_head(&client->mount_wq);
511
512 client->sb = NULL;
513 client->mount_state = CEPH_MOUNT_MOUNTING;
Sage Weil6b805182009-10-27 11:50:50 -0700514 client->mount_args = args;
Sage Weil16725b92009-10-06 11:31:07 -0700515
516 client->msgr = NULL;
517
518 client->mount_err = 0;
Sage Weil16725b92009-10-06 11:31:07 -0700519
Sage Weil859e7b12009-11-02 09:32:47 -0800520 err = bdi_init(&client->backing_dev_info);
521 if (err < 0)
522 goto fail;
523
Sage Weil16725b92009-10-06 11:31:07 -0700524 err = -ENOMEM;
525 client->wb_wq = create_workqueue("ceph-writeback");
526 if (client->wb_wq == NULL)
Sage Weil859e7b12009-11-02 09:32:47 -0800527 goto fail_bdi;
Sage Weil16725b92009-10-06 11:31:07 -0700528 client->pg_inv_wq = create_singlethread_workqueue("ceph-pg-invalid");
529 if (client->pg_inv_wq == NULL)
530 goto fail_wb_wq;
531 client->trunc_wq = create_singlethread_workqueue("ceph-trunc");
532 if (client->trunc_wq == NULL)
533 goto fail_pg_inv_wq;
534
Sage Weilb9bfb932009-11-18 15:08:44 -0800535 /* set up mempools */
536 err = -ENOMEM;
537 client->wb_pagevec_pool = mempool_create_kmalloc_pool(10,
538 client->mount_args->wsize >> PAGE_CACHE_SHIFT);
539 if (!client->wb_pagevec_pool)
540 goto fail_trunc_wq;
541
542
Sage Weil16725b92009-10-06 11:31:07 -0700543 /* subsystems */
544 err = ceph_monc_init(&client->monc, client);
545 if (err < 0)
Sage Weilb9bfb932009-11-18 15:08:44 -0800546 goto fail_mempool;
Sage Weil16725b92009-10-06 11:31:07 -0700547 err = ceph_osdc_init(&client->osdc, client);
548 if (err < 0)
549 goto fail_monc;
Sage Weil5f44f142009-11-18 14:52:18 -0800550 err = ceph_mdsc_init(&client->mdsc, client);
551 if (err < 0)
552 goto fail_osdc;
Sage Weil16725b92009-10-06 11:31:07 -0700553 return client;
554
Sage Weil5f44f142009-11-18 14:52:18 -0800555fail_osdc:
556 ceph_osdc_stop(&client->osdc);
Sage Weil16725b92009-10-06 11:31:07 -0700557fail_monc:
558 ceph_monc_stop(&client->monc);
Sage Weilb9bfb932009-11-18 15:08:44 -0800559fail_mempool:
560 mempool_destroy(client->wb_pagevec_pool);
Sage Weil16725b92009-10-06 11:31:07 -0700561fail_trunc_wq:
562 destroy_workqueue(client->trunc_wq);
563fail_pg_inv_wq:
564 destroy_workqueue(client->pg_inv_wq);
565fail_wb_wq:
566 destroy_workqueue(client->wb_wq);
Sage Weil859e7b12009-11-02 09:32:47 -0800567fail_bdi:
568 bdi_destroy(&client->backing_dev_info);
Sage Weil16725b92009-10-06 11:31:07 -0700569fail:
570 kfree(client);
571 return ERR_PTR(err);
572}
573
574static void ceph_destroy_client(struct ceph_client *client)
575{
576 dout("destroy_client %p\n", client);
577
578 /* unmount */
579 ceph_mdsc_stop(&client->mdsc);
580 ceph_monc_stop(&client->monc);
581 ceph_osdc_stop(&client->osdc);
582
Sage Weil16725b92009-10-06 11:31:07 -0700583 ceph_debugfs_client_cleanup(client);
584 destroy_workqueue(client->wb_wq);
585 destroy_workqueue(client->pg_inv_wq);
586 destroy_workqueue(client->trunc_wq);
587
588 if (client->msgr)
589 ceph_messenger_destroy(client->msgr);
Sage Weilb9bfb932009-11-18 15:08:44 -0800590 mempool_destroy(client->wb_pagevec_pool);
Sage Weil16725b92009-10-06 11:31:07 -0700591
Sage Weil6b805182009-10-27 11:50:50 -0700592 destroy_mount_args(client->mount_args);
Sage Weil16725b92009-10-06 11:31:07 -0700593
594 kfree(client);
595 dout("destroy_client %p done\n", client);
596}
597
598/*
Sage Weil07433042009-11-18 16:50:41 -0800599 * Initially learn our fsid, or verify an fsid matches.
600 */
601int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
602{
603 if (client->have_fsid) {
604 if (ceph_fsid_compare(&client->fsid, fsid)) {
Sage Weil9ec7cab2009-12-14 15:13:47 -0800605 pr_err("bad fsid, had " FSID_FORMAT " got " FSID_FORMAT,
606 PR_FSID(&client->fsid), PR_FSID(fsid));
Sage Weil07433042009-11-18 16:50:41 -0800607 return -1;
608 }
609 } else {
610 pr_info("client%lld fsid " FSID_FORMAT "\n",
611 client->monc.auth->global_id, PR_FSID(fsid));
612 memcpy(&client->fsid, fsid, sizeof(*fsid));
613 ceph_debugfs_client_init(client);
614 client->have_fsid = true;
615 }
616 return 0;
617}
618
619/*
Sage Weil16725b92009-10-06 11:31:07 -0700620 * true if we have the mon map (and have thus joined the cluster)
621 */
622static int have_mon_map(struct ceph_client *client)
623{
624 return client->monc.monmap && client->monc.monmap->epoch;
625}
626
627/*
628 * Bootstrap mount by opening the root directory. Note the mount
629 * @started time from caller, and time out if this takes too long.
630 */
631static struct dentry *open_root_dentry(struct ceph_client *client,
632 const char *path,
633 unsigned long started)
634{
635 struct ceph_mds_client *mdsc = &client->mdsc;
636 struct ceph_mds_request *req = NULL;
637 int err;
638 struct dentry *root;
639
640 /* open dir */
641 dout("open_root_inode opening '%s'\n", path);
642 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
643 if (IS_ERR(req))
644 return ERR_PTR(PTR_ERR(req));
645 req->r_path1 = kstrdup(path, GFP_NOFS);
646 req->r_ino1.ino = CEPH_INO_ROOT;
647 req->r_ino1.snap = CEPH_NOSNAP;
648 req->r_started = started;
Sage Weil6b805182009-10-27 11:50:50 -0700649 req->r_timeout = client->mount_args->mount_timeout * HZ;
Sage Weil16725b92009-10-06 11:31:07 -0700650 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
651 req->r_num_caps = 2;
652 err = ceph_mdsc_do_request(mdsc, NULL, req);
653 if (err == 0) {
654 dout("open_root_inode success\n");
655 if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
656 client->sb->s_root == NULL)
657 root = d_alloc_root(req->r_target_inode);
658 else
659 root = d_obtain_alias(req->r_target_inode);
660 req->r_target_inode = NULL;
661 dout("open_root_inode success, root dentry is %p\n", root);
662 } else {
663 root = ERR_PTR(err);
664 }
665 ceph_mdsc_put_request(req);
666 return root;
667}
668
669/*
670 * mount: join the ceph cluster, and open root directory.
671 */
672static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt,
673 const char *path)
674{
675 struct ceph_entity_addr *myaddr = NULL;
676 int err;
Sage Weil6b805182009-10-27 11:50:50 -0700677 unsigned long timeout = client->mount_args->mount_timeout * HZ;
Sage Weil16725b92009-10-06 11:31:07 -0700678 unsigned long started = jiffies; /* note the start time */
679 struct dentry *root;
680
681 dout("mount start\n");
682 mutex_lock(&client->mount_mutex);
683
684 /* initialize the messenger */
685 if (client->msgr == NULL) {
686 if (ceph_test_opt(client, MYIP))
Sage Weil6b805182009-10-27 11:50:50 -0700687 myaddr = &client->mount_args->my_addr;
Sage Weil16725b92009-10-06 11:31:07 -0700688 client->msgr = ceph_messenger_create(myaddr);
689 if (IS_ERR(client->msgr)) {
690 err = PTR_ERR(client->msgr);
691 client->msgr = NULL;
692 goto out;
693 }
694 client->msgr->nocrc = ceph_test_opt(client, NOCRC);
695 }
696
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800697 /* open session, and wait for mon, mds, and osd maps */
698 err = ceph_monc_open_session(&client->monc);
Sage Weil16725b92009-10-06 11:31:07 -0700699 if (err < 0)
700 goto out;
701
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800702 while (!have_mon_map(client)) {
Sage Weil16725b92009-10-06 11:31:07 -0700703 err = -EIO;
704 if (timeout && time_after_eq(jiffies, started + timeout))
705 goto out;
706
707 /* wait */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800708 dout("mount waiting for mon_map\n");
709 err = wait_event_interruptible_timeout(client->mount_wq, /* FIXME */
Yehuda Sadehdc146572009-11-20 13:59:13 -0800710 have_mon_map(client) || (client->mount_err < 0),
Sage Weil16725b92009-10-06 11:31:07 -0700711 timeout);
712 if (err == -EINTR || err == -ERESTARTSYS)
713 goto out;
Yehuda Sadehdc146572009-11-20 13:59:13 -0800714 if (client->mount_err < 0) {
715 err = client->mount_err;
716 goto out;
717 }
Sage Weil16725b92009-10-06 11:31:07 -0700718 }
719
720 dout("mount opening root\n");
721 root = open_root_dentry(client, "", started);
722 if (IS_ERR(root)) {
723 err = PTR_ERR(root);
724 goto out;
725 }
726 if (client->sb->s_root)
727 dput(root);
728 else
729 client->sb->s_root = root;
730
731 if (path[0] == 0) {
732 dget(root);
733 } else {
734 dout("mount opening base mountpoint\n");
735 root = open_root_dentry(client, path, started);
736 if (IS_ERR(root)) {
737 err = PTR_ERR(root);
738 dput(client->sb->s_root);
739 client->sb->s_root = NULL;
740 goto out;
741 }
742 }
743
744 mnt->mnt_root = root;
745 mnt->mnt_sb = client->sb;
746
747 client->mount_state = CEPH_MOUNT_MOUNTED;
748 dout("mount success\n");
749 err = 0;
750
751out:
752 mutex_unlock(&client->mount_mutex);
753 return err;
754}
755
756static int ceph_set_super(struct super_block *s, void *data)
757{
758 struct ceph_client *client = data;
759 int ret;
760
761 dout("set_super %p data %p\n", s, data);
762
Sage Weil6b805182009-10-27 11:50:50 -0700763 s->s_flags = client->mount_args->sb_flags;
Sage Weil16725b92009-10-06 11:31:07 -0700764 s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */
765
766 s->s_fs_info = client;
767 client->sb = s;
768
769 s->s_op = &ceph_super_ops;
770 s->s_export_op = &ceph_export_ops;
771
772 s->s_time_gran = 1000; /* 1000 ns == 1 us */
773
774 ret = set_anon_super(s, NULL); /* what is that second arg for? */
775 if (ret != 0)
776 goto fail;
777
778 return ret;
779
780fail:
781 s->s_fs_info = NULL;
782 client->sb = NULL;
783 return ret;
784}
785
786/*
787 * share superblock if same fs AND options
788 */
789static int ceph_compare_super(struct super_block *sb, void *data)
790{
791 struct ceph_client *new = data;
Sage Weil6b805182009-10-27 11:50:50 -0700792 struct ceph_mount_args *args = new->mount_args;
Sage Weil16725b92009-10-06 11:31:07 -0700793 struct ceph_client *other = ceph_sb_to_client(sb);
794 int i;
795
796 dout("ceph_compare_super %p\n", sb);
797 if (args->flags & CEPH_OPT_FSID) {
798 if (ceph_fsid_compare(&args->fsid, &other->fsid)) {
799 dout("fsid doesn't match\n");
800 return 0;
801 }
802 } else {
803 /* do we share (a) monitor? */
804 for (i = 0; i < new->monc.monmap->num_mon; i++)
805 if (ceph_monmap_contains(other->monc.monmap,
806 &new->monc.monmap->mon_inst[i].addr))
807 break;
808 if (i == new->monc.monmap->num_mon) {
809 dout("mon ip not part of monmap\n");
810 return 0;
811 }
812 dout("mon ip matches existing sb %p\n", sb);
813 }
Sage Weil6b805182009-10-27 11:50:50 -0700814 if (args->sb_flags != other->mount_args->sb_flags) {
Sage Weil16725b92009-10-06 11:31:07 -0700815 dout("flags differ\n");
816 return 0;
817 }
818 return 1;
819}
820
821/*
822 * construct our own bdi so we can control readahead, etc.
823 */
Sage Weil859e7b12009-11-02 09:32:47 -0800824static int ceph_register_bdi(struct super_block *sb, struct ceph_client *client)
Sage Weil16725b92009-10-06 11:31:07 -0700825{
826 int err;
827
Sage Weilf2cf4182009-10-14 14:09:07 -0700828 sb->s_bdi = &client->backing_dev_info;
Sage Weil16725b92009-10-06 11:31:07 -0700829
830 /* set ra_pages based on rsize mount option? */
Sage Weil6b805182009-10-27 11:50:50 -0700831 if (client->mount_args->rsize >= PAGE_CACHE_SIZE)
Sage Weil16725b92009-10-06 11:31:07 -0700832 client->backing_dev_info.ra_pages =
Sage Weil6b805182009-10-27 11:50:50 -0700833 (client->mount_args->rsize + PAGE_CACHE_SIZE - 1)
Sage Weil16725b92009-10-06 11:31:07 -0700834 >> PAGE_SHIFT;
Sage Weil16725b92009-10-06 11:31:07 -0700835 err = bdi_register_dev(&client->backing_dev_info, sb->s_dev);
836 return err;
837}
838
839static int ceph_get_sb(struct file_system_type *fs_type,
840 int flags, const char *dev_name, void *data,
841 struct vfsmount *mnt)
842{
843 struct super_block *sb;
844 struct ceph_client *client;
845 int err;
846 int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
Sage Weil6a18be12009-11-04 11:40:05 -0800847 const char *path = NULL;
Sage Weil6b805182009-10-27 11:50:50 -0700848 struct ceph_mount_args *args;
Sage Weil16725b92009-10-06 11:31:07 -0700849
850 dout("ceph_get_sb\n");
Sage Weil6b805182009-10-27 11:50:50 -0700851 args = parse_mount_args(flags, data, dev_name, &path);
852 if (IS_ERR(args)) {
853 err = PTR_ERR(args);
854 goto out_final;
855 }
Sage Weil16725b92009-10-06 11:31:07 -0700856
857 /* create client (which we may/may not use) */
Sage Weil6b805182009-10-27 11:50:50 -0700858 client = ceph_create_client(args);
859 if (IS_ERR(client)) {
860 err = PTR_ERR(client);
861 goto out_final;
862 }
Sage Weil16725b92009-10-06 11:31:07 -0700863
Sage Weil6b805182009-10-27 11:50:50 -0700864 if (client->mount_args->flags & CEPH_OPT_NOSHARE)
Sage Weil16725b92009-10-06 11:31:07 -0700865 compare_super = NULL;
866 sb = sget(fs_type, compare_super, ceph_set_super, client);
867 if (IS_ERR(sb)) {
868 err = PTR_ERR(sb);
869 goto out;
870 }
871
872 if (ceph_client(sb) != client) {
873 ceph_destroy_client(client);
874 client = ceph_client(sb);
875 dout("get_sb got existing client %p\n", client);
876 } else {
877 dout("get_sb using new client %p\n", client);
Sage Weil859e7b12009-11-02 09:32:47 -0800878 err = ceph_register_bdi(sb, client);
Sage Weil16725b92009-10-06 11:31:07 -0700879 if (err < 0)
880 goto out_splat;
881 }
882
883 err = ceph_mount(client, mnt, path);
884 if (err < 0)
885 goto out_splat;
886 dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root,
887 mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode));
888 return 0;
889
890out_splat:
891 ceph_mdsc_close_sessions(&client->mdsc);
892 up_write(&sb->s_umount);
893 deactivate_super(sb);
894 goto out_final;
895
896out:
897 ceph_destroy_client(client);
898out_final:
899 dout("ceph_get_sb fail %d\n", err);
900 return err;
901}
902
903static void ceph_kill_sb(struct super_block *s)
904{
905 struct ceph_client *client = ceph_sb_to_client(s);
906 dout("kill_sb %p\n", s);
907 ceph_mdsc_pre_umount(&client->mdsc);
Sage Weil16725b92009-10-06 11:31:07 -0700908 kill_anon_super(s); /* will call put_super after sb is r/o */
Sage Weilf2cf4182009-10-14 14:09:07 -0700909 bdi_unregister(&client->backing_dev_info);
Sage Weil16725b92009-10-06 11:31:07 -0700910 bdi_destroy(&client->backing_dev_info);
911 ceph_destroy_client(client);
912}
913
914static struct file_system_type ceph_fs_type = {
915 .owner = THIS_MODULE,
916 .name = "ceph",
917 .get_sb = ceph_get_sb,
918 .kill_sb = ceph_kill_sb,
919 .fs_flags = FS_RENAME_DOES_D_MOVE,
920};
921
922#define _STRINGIFY(x) #x
923#define STRINGIFY(x) _STRINGIFY(x)
924
925static int __init init_ceph(void)
926{
927 int ret = 0;
928
929 ret = ceph_debugfs_init();
930 if (ret < 0)
931 goto out;
932
933 ret = ceph_msgr_init();
934 if (ret < 0)
935 goto out_debugfs;
936
937 ret = init_caches();
938 if (ret)
939 goto out_msgr;
940
941 ceph_caps_init();
942
943 ret = register_filesystem(&ceph_fs_type);
944 if (ret)
945 goto out_icache;
946
Sage Weilfa0b72e2009-10-07 10:59:10 -0700947 pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n",
948 CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH,
949 CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL);
Sage Weil16725b92009-10-06 11:31:07 -0700950 return 0;
951
952out_icache:
953 destroy_caches();
954out_msgr:
955 ceph_msgr_exit();
956out_debugfs:
957 ceph_debugfs_cleanup();
958out:
959 return ret;
960}
961
962static void __exit exit_ceph(void)
963{
964 dout("exit_ceph\n");
965 unregister_filesystem(&ceph_fs_type);
966 ceph_caps_finalize();
967 destroy_caches();
968 ceph_msgr_exit();
969 ceph_debugfs_cleanup();
970}
971
972module_init(init_ceph);
973module_exit(exit_ceph);
974
975MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
976MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
977MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
978MODULE_DESCRIPTION("Ceph filesystem for Linux");
979MODULE_LICENSE("GPL");