blob: df05617aca86108ee9e88fdc6ad330ff7cce705b [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"
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{
48 struct ceph_client *cl = ceph_client(s);
49
50 dout("put_super\n");
51 ceph_mdsc_close_sessions(&cl->mdsc);
52 return;
53}
54
55static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
56{
57 struct ceph_client *client = ceph_inode_to_client(dentry->d_inode);
58 struct ceph_monmap *monmap = client->monc.monmap;
59 struct ceph_statfs st;
60 u64 fsid;
61 int err;
62
63 dout("statfs\n");
64 err = ceph_monc_do_statfs(&client->monc, &st);
65 if (err < 0)
66 return err;
67
68 /* fill in kstatfs */
69 buf->f_type = CEPH_SUPER_MAGIC; /* ?? */
70
71 /*
72 * express utilization in terms of large blocks to avoid
73 * overflow on 32-bit machines.
74 */
75 buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
76 buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
77 buf->f_bfree = (le64_to_cpu(st.kb) - le64_to_cpu(st.kb_used)) >>
78 (CEPH_BLOCK_SHIFT-10);
79 buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
80
81 buf->f_files = le64_to_cpu(st.num_objects);
82 buf->f_ffree = -1;
83 buf->f_namelen = PATH_MAX;
84 buf->f_frsize = PAGE_CACHE_SIZE;
85
86 /* leave fsid little-endian, regardless of host endianness */
87 fsid = *(u64 *)(&monmap->fsid) ^ *((u64 *)&monmap->fsid + 1);
88 buf->f_fsid.val[0] = fsid & 0xffffffff;
89 buf->f_fsid.val[1] = fsid >> 32;
90
91 return 0;
92}
93
94
95static int ceph_syncfs(struct super_block *sb, int wait)
96{
97 dout("sync_fs %d\n", wait);
98 ceph_osdc_sync(&ceph_client(sb)->osdc);
99 ceph_mdsc_sync(&ceph_client(sb)->mdsc);
Sage Weilf2cf4182009-10-14 14:09:07 -0700100 dout("sync_fs %d done\n", wait);
Sage Weil16725b92009-10-06 11:31:07 -0700101 return 0;
102}
103
104
105/**
106 * ceph_show_options - Show mount options in /proc/mounts
107 * @m: seq_file to write to
108 * @mnt: mount descriptor
109 */
110static int ceph_show_options(struct seq_file *m, struct vfsmount *mnt)
111{
112 struct ceph_client *client = ceph_sb_to_client(mnt->mnt_sb);
Sage Weil6b805182009-10-27 11:50:50 -0700113 struct ceph_mount_args *args = client->mount_args;
Sage Weil16725b92009-10-06 11:31:07 -0700114
115 if (args->flags & CEPH_OPT_FSID)
116 seq_printf(m, ",fsidmajor=%llu,fsidminor%llu",
117 le64_to_cpu(*(__le64 *)&args->fsid.fsid[0]),
118 le64_to_cpu(*(__le64 *)&args->fsid.fsid[8]));
119 if (args->flags & CEPH_OPT_NOSHARE)
120 seq_puts(m, ",noshare");
121 if (args->flags & CEPH_OPT_DIRSTAT)
122 seq_puts(m, ",dirstat");
123 if ((args->flags & CEPH_OPT_RBYTES) == 0)
124 seq_puts(m, ",norbytes");
125 if (args->flags & CEPH_OPT_NOCRC)
126 seq_puts(m, ",nocrc");
127 if (args->flags & CEPH_OPT_NOASYNCREADDIR)
128 seq_puts(m, ",noasyncreaddir");
129 if (strcmp(args->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
130 seq_printf(m, ",snapdirname=%s", args->snapdir_name);
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800131 if (args->name)
132 seq_printf(m, ",name=%s", args->name);
Sage Weil16725b92009-10-06 11:31:07 -0700133 if (args->secret)
134 seq_puts(m, ",secret=<hidden>");
135 return 0;
136}
137
138/*
139 * caches
140 */
141struct kmem_cache *ceph_inode_cachep;
142struct kmem_cache *ceph_cap_cachep;
143struct kmem_cache *ceph_dentry_cachep;
144struct kmem_cache *ceph_file_cachep;
145
146static void ceph_inode_init_once(void *foo)
147{
148 struct ceph_inode_info *ci = foo;
149 inode_init_once(&ci->vfs_inode);
150}
151
152static int __init init_caches(void)
153{
154 ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
155 sizeof(struct ceph_inode_info),
156 __alignof__(struct ceph_inode_info),
157 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
158 ceph_inode_init_once);
159 if (ceph_inode_cachep == NULL)
160 return -ENOMEM;
161
162 ceph_cap_cachep = KMEM_CACHE(ceph_cap,
163 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
164 if (ceph_cap_cachep == NULL)
165 goto bad_cap;
166
167 ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
168 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
169 if (ceph_dentry_cachep == NULL)
170 goto bad_dentry;
171
172 ceph_file_cachep = KMEM_CACHE(ceph_file_info,
173 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
174 if (ceph_file_cachep == NULL)
175 goto bad_file;
176
177 return 0;
178
179bad_file:
180 kmem_cache_destroy(ceph_dentry_cachep);
181bad_dentry:
182 kmem_cache_destroy(ceph_cap_cachep);
183bad_cap:
184 kmem_cache_destroy(ceph_inode_cachep);
185 return -ENOMEM;
186}
187
188static void destroy_caches(void)
189{
190 kmem_cache_destroy(ceph_inode_cachep);
191 kmem_cache_destroy(ceph_cap_cachep);
192 kmem_cache_destroy(ceph_dentry_cachep);
193 kmem_cache_destroy(ceph_file_cachep);
194}
195
196
197/*
198 * ceph_umount_begin - initiate forced umount. Tear down down the
199 * mount, skipping steps that may hang while waiting for server(s).
200 */
201static void ceph_umount_begin(struct super_block *sb)
202{
203 struct ceph_client *client = ceph_sb_to_client(sb);
204
205 dout("ceph_umount_begin - starting forced umount\n");
206 if (!client)
207 return;
208 client->mount_state = CEPH_MOUNT_SHUTDOWN;
209 return;
210}
211
212static const struct super_operations ceph_super_ops = {
213 .alloc_inode = ceph_alloc_inode,
214 .destroy_inode = ceph_destroy_inode,
215 .write_inode = ceph_write_inode,
216 .sync_fs = ceph_syncfs,
217 .put_super = ceph_put_super,
218 .show_options = ceph_show_options,
219 .statfs = ceph_statfs,
220 .umount_begin = ceph_umount_begin,
221};
222
223
224const char *ceph_msg_type_name(int type)
225{
226 switch (type) {
227 case CEPH_MSG_SHUTDOWN: return "shutdown";
228 case CEPH_MSG_PING: return "ping";
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800229 case CEPH_MSG_AUTH: return "auth";
230 case CEPH_MSG_AUTH_REPLY: return "auth_reply";
Sage Weil16725b92009-10-06 11:31:07 -0700231 case CEPH_MSG_MON_MAP: return "mon_map";
232 case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
233 case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
234 case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
Sage Weil16725b92009-10-06 11:31:07 -0700235 case CEPH_MSG_STATFS: return "statfs";
236 case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
Sage Weil16725b92009-10-06 11:31:07 -0700237 case CEPH_MSG_MDS_MAP: return "mds_map";
238 case CEPH_MSG_CLIENT_SESSION: return "client_session";
239 case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
240 case CEPH_MSG_CLIENT_REQUEST: return "client_request";
241 case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
242 case CEPH_MSG_CLIENT_REPLY: return "client_reply";
243 case CEPH_MSG_CLIENT_CAPS: return "client_caps";
244 case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
245 case CEPH_MSG_CLIENT_SNAP: return "client_snap";
246 case CEPH_MSG_CLIENT_LEASE: return "client_lease";
Sage Weil16725b92009-10-06 11:31:07 -0700247 case CEPH_MSG_OSD_MAP: return "osd_map";
248 case CEPH_MSG_OSD_OP: return "osd_op";
249 case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
250 default: return "unknown";
251 }
252}
253
254
255/*
256 * mount options
257 */
258enum {
259 Opt_fsidmajor,
260 Opt_fsidminor,
261 Opt_monport,
262 Opt_wsize,
263 Opt_rsize,
264 Opt_osdtimeout,
265 Opt_mount_timeout,
266 Opt_caps_wanted_delay_min,
267 Opt_caps_wanted_delay_max,
268 Opt_readdir_max_entries,
Sage Weile53c2fe2009-10-27 10:19:28 -0700269 Opt_last_int,
Sage Weil16725b92009-10-06 11:31:07 -0700270 /* int args above */
271 Opt_snapdirname,
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800272 Opt_name,
Sage Weil16725b92009-10-06 11:31:07 -0700273 Opt_secret,
Sage Weile53c2fe2009-10-27 10:19:28 -0700274 Opt_last_string,
Sage Weil16725b92009-10-06 11:31:07 -0700275 /* string args above */
276 Opt_ip,
277 Opt_noshare,
278 Opt_dirstat,
279 Opt_nodirstat,
280 Opt_rbytes,
281 Opt_norbytes,
282 Opt_nocrc,
283 Opt_noasyncreaddir,
284};
285
286static match_table_t arg_tokens = {
287 {Opt_fsidmajor, "fsidmajor=%ld"},
288 {Opt_fsidminor, "fsidminor=%ld"},
289 {Opt_monport, "monport=%d"},
290 {Opt_wsize, "wsize=%d"},
291 {Opt_rsize, "rsize=%d"},
292 {Opt_osdtimeout, "osdtimeout=%d"},
293 {Opt_mount_timeout, "mount_timeout=%d"},
294 {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
295 {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
296 {Opt_readdir_max_entries, "readdir_max_entries=%d"},
297 /* int args above */
298 {Opt_snapdirname, "snapdirname=%s"},
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800299 {Opt_name, "name=%s"},
Sage Weil16725b92009-10-06 11:31:07 -0700300 {Opt_secret, "secret=%s"},
301 /* string args above */
302 {Opt_ip, "ip=%s"},
303 {Opt_noshare, "noshare"},
304 {Opt_dirstat, "dirstat"},
305 {Opt_nodirstat, "nodirstat"},
306 {Opt_rbytes, "rbytes"},
307 {Opt_norbytes, "norbytes"},
308 {Opt_nocrc, "nocrc"},
309 {Opt_noasyncreaddir, "noasyncreaddir"},
310 {-1, NULL}
311};
312
313
Sage Weil6b805182009-10-27 11:50:50 -0700314static struct ceph_mount_args *parse_mount_args(int flags, char *options,
315 const char *dev_name,
316 const char **path)
Sage Weil16725b92009-10-06 11:31:07 -0700317{
Sage Weil6b805182009-10-27 11:50:50 -0700318 struct ceph_mount_args *args;
Sage Weil16725b92009-10-06 11:31:07 -0700319 const char *c;
Sage Weil6b805182009-10-27 11:50:50 -0700320 int err = -ENOMEM;
Sage Weil16725b92009-10-06 11:31:07 -0700321 substring_t argstr[MAX_OPT_ARGS];
Sage Weil16725b92009-10-06 11:31:07 -0700322
Sage Weil6b805182009-10-27 11:50:50 -0700323 args = kzalloc(sizeof(*args), GFP_KERNEL);
324 if (!args)
325 return ERR_PTR(-ENOMEM);
326 args->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*args->mon_addr),
327 GFP_KERNEL);
328 if (!args->mon_addr)
329 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700330
Sage Weil6b805182009-10-27 11:50:50 -0700331 dout("parse_mount_args %p, dev_name '%s'\n", args, dev_name);
Sage Weil7b813c462009-10-26 22:07:53 -0700332
Sage Weil16725b92009-10-06 11:31:07 -0700333 /* start with defaults */
334 args->sb_flags = flags;
335 args->flags = CEPH_OPT_DEFAULT;
336 args->osd_timeout = 5; /* seconds */
337 args->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT; /* seconds */
338 args->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
339 args->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
Sage Weil8fa97652009-10-16 14:44:35 -0700340 args->rsize = CEPH_MOUNT_RSIZE_DEFAULT;
Sage Weil16725b92009-10-06 11:31:07 -0700341 args->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
342 args->cap_release_safety = CEPH_CAPS_PER_RELEASE * 4;
343 args->max_readdir = 1024;
344
345 /* ip1[:port1][,ip2[:port2]...]:/subdir/in/fs */
Sage Weil7b813c462009-10-26 22:07:53 -0700346 err = -EINVAL;
Sage Weil16725b92009-10-06 11:31:07 -0700347 if (!dev_name)
Sage Weil7b813c462009-10-26 22:07:53 -0700348 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700349 *path = strstr(dev_name, ":/");
350 if (*path == NULL) {
351 pr_err("device name is missing path (no :/ in %s)\n",
352 dev_name);
Sage Weil7b813c462009-10-26 22:07:53 -0700353 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700354 }
355
356 /* get mon ip(s) */
Sage Weil6b805182009-10-27 11:50:50 -0700357 err = ceph_parse_ips(dev_name, *path, args->mon_addr,
358 CEPH_MAX_MON, &args->num_mon);
Sage Weil16725b92009-10-06 11:31:07 -0700359 if (err < 0)
Sage Weil7b813c462009-10-26 22:07:53 -0700360 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700361
Sage Weil16725b92009-10-06 11:31:07 -0700362 /* path on server */
363 *path += 2;
364 dout("server path '%s'\n", *path);
365
366 /* parse mount options */
367 while ((c = strsep(&options, ",")) != NULL) {
368 int token, intval, ret;
369 if (!*c)
370 continue;
Sage Weil7b813c462009-10-26 22:07:53 -0700371 err = -EINVAL;
Sage Weil16725b92009-10-06 11:31:07 -0700372 token = match_token((char *)c, arg_tokens, argstr);
373 if (token < 0) {
374 pr_err("bad mount option at '%s'\n", c);
Sage Weil7b813c462009-10-26 22:07:53 -0700375 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700376 }
Sage Weile53c2fe2009-10-27 10:19:28 -0700377 if (token < Opt_last_int) {
Sage Weil16725b92009-10-06 11:31:07 -0700378 ret = match_int(&argstr[0], &intval);
379 if (ret < 0) {
380 pr_err("bad mount option arg (not int) "
381 "at '%s'\n", c);
382 continue;
383 }
Sage Weile53c2fe2009-10-27 10:19:28 -0700384 dout("got int token %d val %d\n", token, intval);
385 } else if (token > Opt_last_int && token < Opt_last_string) {
386 dout("got string token %d val %s\n", token,
387 argstr[0].from);
388 } else {
389 dout("got token %d\n", token);
Sage Weil16725b92009-10-06 11:31:07 -0700390 }
391 switch (token) {
392 case Opt_fsidmajor:
393 *(__le64 *)&args->fsid.fsid[0] = cpu_to_le64(intval);
394 break;
395 case Opt_fsidminor:
396 *(__le64 *)&args->fsid.fsid[8] = cpu_to_le64(intval);
397 break;
398 case Opt_ip:
399 err = ceph_parse_ips(argstr[0].from,
400 argstr[0].to,
401 &args->my_addr,
402 1, NULL);
403 if (err < 0)
Sage Weil6b805182009-10-27 11:50:50 -0700404 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700405 args->flags |= CEPH_OPT_MYIP;
406 break;
407
408 case Opt_snapdirname:
409 kfree(args->snapdir_name);
410 args->snapdir_name = kstrndup(argstr[0].from,
411 argstr[0].to-argstr[0].from,
412 GFP_KERNEL);
413 break;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800414 case Opt_name:
415 args->name = kstrndup(argstr[0].from,
416 argstr[0].to-argstr[0].from,
417 GFP_KERNEL);
418 break;
Sage Weil16725b92009-10-06 11:31:07 -0700419 case Opt_secret:
420 args->secret = kstrndup(argstr[0].from,
421 argstr[0].to-argstr[0].from,
422 GFP_KERNEL);
423 break;
424
425 /* misc */
426 case Opt_wsize:
427 args->wsize = intval;
428 break;
429 case Opt_rsize:
430 args->rsize = intval;
431 break;
432 case Opt_osdtimeout:
433 args->osd_timeout = intval;
434 break;
435 case Opt_mount_timeout:
436 args->mount_timeout = intval;
437 break;
438 case Opt_caps_wanted_delay_min:
439 args->caps_wanted_delay_min = intval;
440 break;
441 case Opt_caps_wanted_delay_max:
442 args->caps_wanted_delay_max = intval;
443 break;
444 case Opt_readdir_max_entries:
445 args->max_readdir = intval;
446 break;
447
448 case Opt_noshare:
449 args->flags |= CEPH_OPT_NOSHARE;
450 break;
451
452 case Opt_dirstat:
453 args->flags |= CEPH_OPT_DIRSTAT;
454 break;
455 case Opt_nodirstat:
456 args->flags &= ~CEPH_OPT_DIRSTAT;
457 break;
458 case Opt_rbytes:
459 args->flags |= CEPH_OPT_RBYTES;
460 break;
461 case Opt_norbytes:
462 args->flags &= ~CEPH_OPT_RBYTES;
463 break;
464 case Opt_nocrc:
465 args->flags |= CEPH_OPT_NOCRC;
466 break;
467 case Opt_noasyncreaddir:
468 args->flags |= CEPH_OPT_NOASYNCREADDIR;
469 break;
470
471 default:
472 BUG_ON(token);
473 }
474 }
Sage Weil6b805182009-10-27 11:50:50 -0700475 return args;
Sage Weil16725b92009-10-06 11:31:07 -0700476
Sage Weil7b813c462009-10-26 22:07:53 -0700477out:
Sage Weil6b805182009-10-27 11:50:50 -0700478 kfree(args->mon_addr);
479 kfree(args);
480 return ERR_PTR(err);
Sage Weil16725b92009-10-06 11:31:07 -0700481}
482
Sage Weil6b805182009-10-27 11:50:50 -0700483static void destroy_mount_args(struct ceph_mount_args *args)
Sage Weil16725b92009-10-06 11:31:07 -0700484{
Sage Weil6b805182009-10-27 11:50:50 -0700485 dout("destroy_mount_args %p\n", args);
Sage Weil16725b92009-10-06 11:31:07 -0700486 kfree(args->snapdir_name);
487 args->snapdir_name = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800488 kfree(args->name);
489 args->name = NULL;
Sage Weil16725b92009-10-06 11:31:07 -0700490 kfree(args->secret);
491 args->secret = NULL;
Sage Weil6b805182009-10-27 11:50:50 -0700492 kfree(args);
Sage Weil16725b92009-10-06 11:31:07 -0700493}
494
495/*
496 * create a fresh client instance
497 */
Sage Weil6b805182009-10-27 11:50:50 -0700498static struct ceph_client *ceph_create_client(struct ceph_mount_args *args)
Sage Weil16725b92009-10-06 11:31:07 -0700499{
500 struct ceph_client *client;
501 int err = -ENOMEM;
502
503 client = kzalloc(sizeof(*client), GFP_KERNEL);
504 if (client == NULL)
505 return ERR_PTR(-ENOMEM);
506
507 mutex_init(&client->mount_mutex);
508
509 init_waitqueue_head(&client->mount_wq);
510
511 client->sb = NULL;
512 client->mount_state = CEPH_MOUNT_MOUNTING;
513 client->whoami = -1;
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;
519 client->signed_ticket = NULL;
520 client->signed_ticket_len = 0;
521
Sage Weil859e7b12009-11-02 09:32:47 -0800522 err = bdi_init(&client->backing_dev_info);
523 if (err < 0)
524 goto fail;
525
Sage Weil16725b92009-10-06 11:31:07 -0700526 err = -ENOMEM;
527 client->wb_wq = create_workqueue("ceph-writeback");
528 if (client->wb_wq == NULL)
Sage Weil859e7b12009-11-02 09:32:47 -0800529 goto fail_bdi;
Sage Weil16725b92009-10-06 11:31:07 -0700530 client->pg_inv_wq = create_singlethread_workqueue("ceph-pg-invalid");
531 if (client->pg_inv_wq == NULL)
532 goto fail_wb_wq;
533 client->trunc_wq = create_singlethread_workqueue("ceph-trunc");
534 if (client->trunc_wq == NULL)
535 goto fail_pg_inv_wq;
536
Sage Weilb9bfb932009-11-18 15:08:44 -0800537 /* set up mempools */
538 err = -ENOMEM;
539 client->wb_pagevec_pool = mempool_create_kmalloc_pool(10,
540 client->mount_args->wsize >> PAGE_CACHE_SHIFT);
541 if (!client->wb_pagevec_pool)
542 goto fail_trunc_wq;
543
544
Sage Weil16725b92009-10-06 11:31:07 -0700545 /* subsystems */
546 err = ceph_monc_init(&client->monc, client);
547 if (err < 0)
Sage Weilb9bfb932009-11-18 15:08:44 -0800548 goto fail_mempool;
Sage Weil16725b92009-10-06 11:31:07 -0700549 err = ceph_osdc_init(&client->osdc, client);
550 if (err < 0)
551 goto fail_monc;
Sage Weil5f44f142009-11-18 14:52:18 -0800552 err = ceph_mdsc_init(&client->mdsc, client);
553 if (err < 0)
554 goto fail_osdc;
Sage Weil16725b92009-10-06 11:31:07 -0700555 return client;
556
Sage Weil5f44f142009-11-18 14:52:18 -0800557fail_osdc:
558 ceph_osdc_stop(&client->osdc);
Sage Weil16725b92009-10-06 11:31:07 -0700559fail_monc:
560 ceph_monc_stop(&client->monc);
Sage Weilb9bfb932009-11-18 15:08:44 -0800561fail_mempool:
562 mempool_destroy(client->wb_pagevec_pool);
Sage Weil16725b92009-10-06 11:31:07 -0700563fail_trunc_wq:
564 destroy_workqueue(client->trunc_wq);
565fail_pg_inv_wq:
566 destroy_workqueue(client->pg_inv_wq);
567fail_wb_wq:
568 destroy_workqueue(client->wb_wq);
Sage Weil859e7b12009-11-02 09:32:47 -0800569fail_bdi:
570 bdi_destroy(&client->backing_dev_info);
Sage Weil16725b92009-10-06 11:31:07 -0700571fail:
572 kfree(client);
573 return ERR_PTR(err);
574}
575
576static void ceph_destroy_client(struct ceph_client *client)
577{
578 dout("destroy_client %p\n", client);
579
580 /* unmount */
581 ceph_mdsc_stop(&client->mdsc);
582 ceph_monc_stop(&client->monc);
583 ceph_osdc_stop(&client->osdc);
584
585 kfree(client->signed_ticket);
586
587 ceph_debugfs_client_cleanup(client);
588 destroy_workqueue(client->wb_wq);
589 destroy_workqueue(client->pg_inv_wq);
590 destroy_workqueue(client->trunc_wq);
591
592 if (client->msgr)
593 ceph_messenger_destroy(client->msgr);
Sage Weilb9bfb932009-11-18 15:08:44 -0800594 mempool_destroy(client->wb_pagevec_pool);
Sage Weil16725b92009-10-06 11:31:07 -0700595
Sage Weil6b805182009-10-27 11:50:50 -0700596 destroy_mount_args(client->mount_args);
Sage Weil16725b92009-10-06 11:31:07 -0700597
598 kfree(client);
599 dout("destroy_client %p done\n", client);
600}
601
602/*
603 * true if we have the mon map (and have thus joined the cluster)
604 */
605static int have_mon_map(struct ceph_client *client)
606{
607 return client->monc.monmap && client->monc.monmap->epoch;
608}
609
610/*
611 * Bootstrap mount by opening the root directory. Note the mount
612 * @started time from caller, and time out if this takes too long.
613 */
614static struct dentry *open_root_dentry(struct ceph_client *client,
615 const char *path,
616 unsigned long started)
617{
618 struct ceph_mds_client *mdsc = &client->mdsc;
619 struct ceph_mds_request *req = NULL;
620 int err;
621 struct dentry *root;
622
623 /* open dir */
624 dout("open_root_inode opening '%s'\n", path);
625 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
626 if (IS_ERR(req))
627 return ERR_PTR(PTR_ERR(req));
628 req->r_path1 = kstrdup(path, GFP_NOFS);
629 req->r_ino1.ino = CEPH_INO_ROOT;
630 req->r_ino1.snap = CEPH_NOSNAP;
631 req->r_started = started;
Sage Weil6b805182009-10-27 11:50:50 -0700632 req->r_timeout = client->mount_args->mount_timeout * HZ;
Sage Weil16725b92009-10-06 11:31:07 -0700633 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
634 req->r_num_caps = 2;
635 err = ceph_mdsc_do_request(mdsc, NULL, req);
636 if (err == 0) {
637 dout("open_root_inode success\n");
638 if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
639 client->sb->s_root == NULL)
640 root = d_alloc_root(req->r_target_inode);
641 else
642 root = d_obtain_alias(req->r_target_inode);
643 req->r_target_inode = NULL;
644 dout("open_root_inode success, root dentry is %p\n", root);
645 } else {
646 root = ERR_PTR(err);
647 }
648 ceph_mdsc_put_request(req);
649 return root;
650}
651
652/*
653 * mount: join the ceph cluster, and open root directory.
654 */
655static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt,
656 const char *path)
657{
658 struct ceph_entity_addr *myaddr = NULL;
659 int err;
Sage Weil6b805182009-10-27 11:50:50 -0700660 unsigned long timeout = client->mount_args->mount_timeout * HZ;
Sage Weil16725b92009-10-06 11:31:07 -0700661 unsigned long started = jiffies; /* note the start time */
662 struct dentry *root;
663
664 dout("mount start\n");
665 mutex_lock(&client->mount_mutex);
666
667 /* initialize the messenger */
668 if (client->msgr == NULL) {
669 if (ceph_test_opt(client, MYIP))
Sage Weil6b805182009-10-27 11:50:50 -0700670 myaddr = &client->mount_args->my_addr;
Sage Weil16725b92009-10-06 11:31:07 -0700671 client->msgr = ceph_messenger_create(myaddr);
672 if (IS_ERR(client->msgr)) {
673 err = PTR_ERR(client->msgr);
674 client->msgr = NULL;
675 goto out;
676 }
677 client->msgr->nocrc = ceph_test_opt(client, NOCRC);
678 }
679
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800680 /* open session, and wait for mon, mds, and osd maps */
681 err = ceph_monc_open_session(&client->monc);
Sage Weil16725b92009-10-06 11:31:07 -0700682 if (err < 0)
683 goto out;
684
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800685 while (!have_mon_map(client)) {
Sage Weil16725b92009-10-06 11:31:07 -0700686 err = -EIO;
687 if (timeout && time_after_eq(jiffies, started + timeout))
688 goto out;
689
690 /* wait */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800691 dout("mount waiting for mon_map\n");
692 err = wait_event_interruptible_timeout(client->mount_wq, /* FIXME */
693 have_mon_map(client),
Sage Weil16725b92009-10-06 11:31:07 -0700694 timeout);
695 if (err == -EINTR || err == -ERESTARTSYS)
696 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700697 }
698
699 dout("mount opening root\n");
700 root = open_root_dentry(client, "", started);
701 if (IS_ERR(root)) {
702 err = PTR_ERR(root);
703 goto out;
704 }
705 if (client->sb->s_root)
706 dput(root);
707 else
708 client->sb->s_root = root;
709
710 if (path[0] == 0) {
711 dget(root);
712 } else {
713 dout("mount opening base mountpoint\n");
714 root = open_root_dentry(client, path, started);
715 if (IS_ERR(root)) {
716 err = PTR_ERR(root);
717 dput(client->sb->s_root);
718 client->sb->s_root = NULL;
719 goto out;
720 }
721 }
722
723 mnt->mnt_root = root;
724 mnt->mnt_sb = client->sb;
725
726 client->mount_state = CEPH_MOUNT_MOUNTED;
727 dout("mount success\n");
728 err = 0;
729
730out:
731 mutex_unlock(&client->mount_mutex);
732 return err;
733}
734
735static int ceph_set_super(struct super_block *s, void *data)
736{
737 struct ceph_client *client = data;
738 int ret;
739
740 dout("set_super %p data %p\n", s, data);
741
Sage Weil6b805182009-10-27 11:50:50 -0700742 s->s_flags = client->mount_args->sb_flags;
Sage Weil16725b92009-10-06 11:31:07 -0700743 s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */
744
745 s->s_fs_info = client;
746 client->sb = s;
747
748 s->s_op = &ceph_super_ops;
749 s->s_export_op = &ceph_export_ops;
750
751 s->s_time_gran = 1000; /* 1000 ns == 1 us */
752
753 ret = set_anon_super(s, NULL); /* what is that second arg for? */
754 if (ret != 0)
755 goto fail;
756
757 return ret;
758
759fail:
760 s->s_fs_info = NULL;
761 client->sb = NULL;
762 return ret;
763}
764
765/*
766 * share superblock if same fs AND options
767 */
768static int ceph_compare_super(struct super_block *sb, void *data)
769{
770 struct ceph_client *new = data;
Sage Weil6b805182009-10-27 11:50:50 -0700771 struct ceph_mount_args *args = new->mount_args;
Sage Weil16725b92009-10-06 11:31:07 -0700772 struct ceph_client *other = ceph_sb_to_client(sb);
773 int i;
774
775 dout("ceph_compare_super %p\n", sb);
776 if (args->flags & CEPH_OPT_FSID) {
777 if (ceph_fsid_compare(&args->fsid, &other->fsid)) {
778 dout("fsid doesn't match\n");
779 return 0;
780 }
781 } else {
782 /* do we share (a) monitor? */
783 for (i = 0; i < new->monc.monmap->num_mon; i++)
784 if (ceph_monmap_contains(other->monc.monmap,
785 &new->monc.monmap->mon_inst[i].addr))
786 break;
787 if (i == new->monc.monmap->num_mon) {
788 dout("mon ip not part of monmap\n");
789 return 0;
790 }
791 dout("mon ip matches existing sb %p\n", sb);
792 }
Sage Weil6b805182009-10-27 11:50:50 -0700793 if (args->sb_flags != other->mount_args->sb_flags) {
Sage Weil16725b92009-10-06 11:31:07 -0700794 dout("flags differ\n");
795 return 0;
796 }
797 return 1;
798}
799
800/*
801 * construct our own bdi so we can control readahead, etc.
802 */
Sage Weil859e7b12009-11-02 09:32:47 -0800803static int ceph_register_bdi(struct super_block *sb, struct ceph_client *client)
Sage Weil16725b92009-10-06 11:31:07 -0700804{
805 int err;
806
Sage Weilf2cf4182009-10-14 14:09:07 -0700807 sb->s_bdi = &client->backing_dev_info;
Sage Weil16725b92009-10-06 11:31:07 -0700808
809 /* set ra_pages based on rsize mount option? */
Sage Weil6b805182009-10-27 11:50:50 -0700810 if (client->mount_args->rsize >= PAGE_CACHE_SIZE)
Sage Weil16725b92009-10-06 11:31:07 -0700811 client->backing_dev_info.ra_pages =
Sage Weil6b805182009-10-27 11:50:50 -0700812 (client->mount_args->rsize + PAGE_CACHE_SIZE - 1)
Sage Weil16725b92009-10-06 11:31:07 -0700813 >> PAGE_SHIFT;
Sage Weil16725b92009-10-06 11:31:07 -0700814 err = bdi_register_dev(&client->backing_dev_info, sb->s_dev);
815 return err;
816}
817
818static int ceph_get_sb(struct file_system_type *fs_type,
819 int flags, const char *dev_name, void *data,
820 struct vfsmount *mnt)
821{
822 struct super_block *sb;
823 struct ceph_client *client;
824 int err;
825 int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
Sage Weil6a18be12009-11-04 11:40:05 -0800826 const char *path = NULL;
Sage Weil6b805182009-10-27 11:50:50 -0700827 struct ceph_mount_args *args;
Sage Weil16725b92009-10-06 11:31:07 -0700828
829 dout("ceph_get_sb\n");
Sage Weil6b805182009-10-27 11:50:50 -0700830 args = parse_mount_args(flags, data, dev_name, &path);
831 if (IS_ERR(args)) {
832 err = PTR_ERR(args);
833 goto out_final;
834 }
Sage Weil16725b92009-10-06 11:31:07 -0700835
836 /* create client (which we may/may not use) */
Sage Weil6b805182009-10-27 11:50:50 -0700837 client = ceph_create_client(args);
838 if (IS_ERR(client)) {
839 err = PTR_ERR(client);
840 goto out_final;
841 }
Sage Weil16725b92009-10-06 11:31:07 -0700842
Sage Weil6b805182009-10-27 11:50:50 -0700843 if (client->mount_args->flags & CEPH_OPT_NOSHARE)
Sage Weil16725b92009-10-06 11:31:07 -0700844 compare_super = NULL;
845 sb = sget(fs_type, compare_super, ceph_set_super, client);
846 if (IS_ERR(sb)) {
847 err = PTR_ERR(sb);
848 goto out;
849 }
850
851 if (ceph_client(sb) != client) {
852 ceph_destroy_client(client);
853 client = ceph_client(sb);
854 dout("get_sb got existing client %p\n", client);
855 } else {
856 dout("get_sb using new client %p\n", client);
Sage Weil859e7b12009-11-02 09:32:47 -0800857 err = ceph_register_bdi(sb, client);
Sage Weil16725b92009-10-06 11:31:07 -0700858 if (err < 0)
859 goto out_splat;
860 }
861
862 err = ceph_mount(client, mnt, path);
863 if (err < 0)
864 goto out_splat;
865 dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root,
866 mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode));
867 return 0;
868
869out_splat:
870 ceph_mdsc_close_sessions(&client->mdsc);
871 up_write(&sb->s_umount);
872 deactivate_super(sb);
873 goto out_final;
874
875out:
876 ceph_destroy_client(client);
877out_final:
878 dout("ceph_get_sb fail %d\n", err);
879 return err;
880}
881
882static void ceph_kill_sb(struct super_block *s)
883{
884 struct ceph_client *client = ceph_sb_to_client(s);
885 dout("kill_sb %p\n", s);
886 ceph_mdsc_pre_umount(&client->mdsc);
Sage Weil16725b92009-10-06 11:31:07 -0700887 kill_anon_super(s); /* will call put_super after sb is r/o */
Sage Weilf2cf4182009-10-14 14:09:07 -0700888 bdi_unregister(&client->backing_dev_info);
Sage Weil16725b92009-10-06 11:31:07 -0700889 bdi_destroy(&client->backing_dev_info);
890 ceph_destroy_client(client);
891}
892
893static struct file_system_type ceph_fs_type = {
894 .owner = THIS_MODULE,
895 .name = "ceph",
896 .get_sb = ceph_get_sb,
897 .kill_sb = ceph_kill_sb,
898 .fs_flags = FS_RENAME_DOES_D_MOVE,
899};
900
901#define _STRINGIFY(x) #x
902#define STRINGIFY(x) _STRINGIFY(x)
903
904static int __init init_ceph(void)
905{
906 int ret = 0;
907
908 ret = ceph_debugfs_init();
909 if (ret < 0)
910 goto out;
911
912 ret = ceph_msgr_init();
913 if (ret < 0)
914 goto out_debugfs;
915
916 ret = init_caches();
917 if (ret)
918 goto out_msgr;
919
920 ceph_caps_init();
921
922 ret = register_filesystem(&ceph_fs_type);
923 if (ret)
924 goto out_icache;
925
Sage Weilfa0b72e2009-10-07 10:59:10 -0700926 pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n",
927 CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH,
928 CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL);
Sage Weil16725b92009-10-06 11:31:07 -0700929 return 0;
930
931out_icache:
932 destroy_caches();
933out_msgr:
934 ceph_msgr_exit();
935out_debugfs:
936 ceph_debugfs_cleanup();
937out:
938 return ret;
939}
940
941static void __exit exit_ceph(void)
942{
943 dout("exit_ceph\n");
944 unregister_filesystem(&ceph_fs_type);
945 ceph_caps_finalize();
946 destroy_caches();
947 ceph_msgr_exit();
948 ceph_debugfs_cleanup();
949}
950
951module_init(init_ceph);
952module_exit(exit_ceph);
953
954MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
955MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
956MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
957MODULE_DESCRIPTION("Ceph filesystem for Linux");
958MODULE_LICENSE("GPL");