blob: 3a2548951fe65d5a66736196102bf93898241c91 [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
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800153static int default_congestion_kb(void)
154{
155 int congestion_kb;
156
157 /*
158 * Copied from NFS
159 *
160 * congestion size, scale with available memory.
161 *
162 * 64MB: 8192k
163 * 128MB: 11585k
164 * 256MB: 16384k
165 * 512MB: 23170k
166 * 1GB: 32768k
167 * 2GB: 46340k
168 * 4GB: 65536k
169 * 8GB: 92681k
170 * 16GB: 131072k
171 *
172 * This allows larger machines to have larger/more transfers.
173 * Limit the default to 256M
174 */
175 congestion_kb = (16*int_sqrt(totalram_pages)) << (PAGE_SHIFT-10);
176 if (congestion_kb > 256*1024)
177 congestion_kb = 256*1024;
178
179 return congestion_kb;
180}
181
Sage Weil16725b92009-10-06 11:31:07 -0700182static int __init init_caches(void)
183{
184 ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
185 sizeof(struct ceph_inode_info),
186 __alignof__(struct ceph_inode_info),
187 (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD),
188 ceph_inode_init_once);
189 if (ceph_inode_cachep == NULL)
190 return -ENOMEM;
191
192 ceph_cap_cachep = KMEM_CACHE(ceph_cap,
193 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
194 if (ceph_cap_cachep == NULL)
195 goto bad_cap;
196
197 ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
198 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
199 if (ceph_dentry_cachep == NULL)
200 goto bad_dentry;
201
202 ceph_file_cachep = KMEM_CACHE(ceph_file_info,
203 SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
204 if (ceph_file_cachep == NULL)
205 goto bad_file;
206
207 return 0;
208
209bad_file:
210 kmem_cache_destroy(ceph_dentry_cachep);
211bad_dentry:
212 kmem_cache_destroy(ceph_cap_cachep);
213bad_cap:
214 kmem_cache_destroy(ceph_inode_cachep);
215 return -ENOMEM;
216}
217
218static void destroy_caches(void)
219{
220 kmem_cache_destroy(ceph_inode_cachep);
221 kmem_cache_destroy(ceph_cap_cachep);
222 kmem_cache_destroy(ceph_dentry_cachep);
223 kmem_cache_destroy(ceph_file_cachep);
224}
225
226
227/*
228 * ceph_umount_begin - initiate forced umount. Tear down down the
229 * mount, skipping steps that may hang while waiting for server(s).
230 */
231static void ceph_umount_begin(struct super_block *sb)
232{
233 struct ceph_client *client = ceph_sb_to_client(sb);
234
235 dout("ceph_umount_begin - starting forced umount\n");
236 if (!client)
237 return;
238 client->mount_state = CEPH_MOUNT_SHUTDOWN;
239 return;
240}
241
242static const struct super_operations ceph_super_ops = {
243 .alloc_inode = ceph_alloc_inode,
244 .destroy_inode = ceph_destroy_inode,
245 .write_inode = ceph_write_inode,
246 .sync_fs = ceph_syncfs,
247 .put_super = ceph_put_super,
248 .show_options = ceph_show_options,
249 .statfs = ceph_statfs,
250 .umount_begin = ceph_umount_begin,
251};
252
253
254const char *ceph_msg_type_name(int type)
255{
256 switch (type) {
257 case CEPH_MSG_SHUTDOWN: return "shutdown";
258 case CEPH_MSG_PING: return "ping";
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800259 case CEPH_MSG_AUTH: return "auth";
260 case CEPH_MSG_AUTH_REPLY: return "auth_reply";
Sage Weil16725b92009-10-06 11:31:07 -0700261 case CEPH_MSG_MON_MAP: return "mon_map";
262 case CEPH_MSG_MON_GET_MAP: return "mon_get_map";
263 case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe";
264 case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack";
Sage Weil16725b92009-10-06 11:31:07 -0700265 case CEPH_MSG_STATFS: return "statfs";
266 case CEPH_MSG_STATFS_REPLY: return "statfs_reply";
Sage Weil16725b92009-10-06 11:31:07 -0700267 case CEPH_MSG_MDS_MAP: return "mds_map";
268 case CEPH_MSG_CLIENT_SESSION: return "client_session";
269 case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
270 case CEPH_MSG_CLIENT_REQUEST: return "client_request";
271 case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
272 case CEPH_MSG_CLIENT_REPLY: return "client_reply";
273 case CEPH_MSG_CLIENT_CAPS: return "client_caps";
274 case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
275 case CEPH_MSG_CLIENT_SNAP: return "client_snap";
276 case CEPH_MSG_CLIENT_LEASE: return "client_lease";
Sage Weil16725b92009-10-06 11:31:07 -0700277 case CEPH_MSG_OSD_MAP: return "osd_map";
278 case CEPH_MSG_OSD_OP: return "osd_op";
279 case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
280 default: return "unknown";
281 }
282}
283
284
285/*
286 * mount options
287 */
288enum {
289 Opt_fsidmajor,
290 Opt_fsidminor,
291 Opt_monport,
292 Opt_wsize,
293 Opt_rsize,
294 Opt_osdtimeout,
295 Opt_mount_timeout,
296 Opt_caps_wanted_delay_min,
297 Opt_caps_wanted_delay_max,
298 Opt_readdir_max_entries,
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800299 Opt_congestion_kb,
Sage Weile53c2fe2009-10-27 10:19:28 -0700300 Opt_last_int,
Sage Weil16725b92009-10-06 11:31:07 -0700301 /* int args above */
302 Opt_snapdirname,
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800303 Opt_name,
Sage Weil16725b92009-10-06 11:31:07 -0700304 Opt_secret,
Sage Weile53c2fe2009-10-27 10:19:28 -0700305 Opt_last_string,
Sage Weil16725b92009-10-06 11:31:07 -0700306 /* string args above */
307 Opt_ip,
308 Opt_noshare,
309 Opt_dirstat,
310 Opt_nodirstat,
311 Opt_rbytes,
312 Opt_norbytes,
313 Opt_nocrc,
314 Opt_noasyncreaddir,
315};
316
317static match_table_t arg_tokens = {
318 {Opt_fsidmajor, "fsidmajor=%ld"},
319 {Opt_fsidminor, "fsidminor=%ld"},
320 {Opt_monport, "monport=%d"},
321 {Opt_wsize, "wsize=%d"},
322 {Opt_rsize, "rsize=%d"},
323 {Opt_osdtimeout, "osdtimeout=%d"},
324 {Opt_mount_timeout, "mount_timeout=%d"},
325 {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
326 {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
327 {Opt_readdir_max_entries, "readdir_max_entries=%d"},
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800328 {Opt_congestion_kb, "write_congestion_kb=%d"},
Sage Weil16725b92009-10-06 11:31:07 -0700329 /* int args above */
330 {Opt_snapdirname, "snapdirname=%s"},
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800331 {Opt_name, "name=%s"},
Sage Weil16725b92009-10-06 11:31:07 -0700332 {Opt_secret, "secret=%s"},
333 /* string args above */
334 {Opt_ip, "ip=%s"},
335 {Opt_noshare, "noshare"},
336 {Opt_dirstat, "dirstat"},
337 {Opt_nodirstat, "nodirstat"},
338 {Opt_rbytes, "rbytes"},
339 {Opt_norbytes, "norbytes"},
340 {Opt_nocrc, "nocrc"},
341 {Opt_noasyncreaddir, "noasyncreaddir"},
342 {-1, NULL}
343};
344
345
Sage Weil6b805182009-10-27 11:50:50 -0700346static struct ceph_mount_args *parse_mount_args(int flags, char *options,
347 const char *dev_name,
348 const char **path)
Sage Weil16725b92009-10-06 11:31:07 -0700349{
Sage Weil6b805182009-10-27 11:50:50 -0700350 struct ceph_mount_args *args;
Sage Weil16725b92009-10-06 11:31:07 -0700351 const char *c;
Sage Weil6b805182009-10-27 11:50:50 -0700352 int err = -ENOMEM;
Sage Weil16725b92009-10-06 11:31:07 -0700353 substring_t argstr[MAX_OPT_ARGS];
Sage Weil16725b92009-10-06 11:31:07 -0700354
Sage Weil6b805182009-10-27 11:50:50 -0700355 args = kzalloc(sizeof(*args), GFP_KERNEL);
356 if (!args)
357 return ERR_PTR(-ENOMEM);
358 args->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*args->mon_addr),
359 GFP_KERNEL);
360 if (!args->mon_addr)
361 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700362
Sage Weil6b805182009-10-27 11:50:50 -0700363 dout("parse_mount_args %p, dev_name '%s'\n", args, dev_name);
Sage Weil7b813c462009-10-26 22:07:53 -0700364
Sage Weil16725b92009-10-06 11:31:07 -0700365 /* start with defaults */
366 args->sb_flags = flags;
367 args->flags = CEPH_OPT_DEFAULT;
368 args->osd_timeout = 5; /* seconds */
369 args->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT; /* seconds */
370 args->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
371 args->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
Sage Weil8fa97652009-10-16 14:44:35 -0700372 args->rsize = CEPH_MOUNT_RSIZE_DEFAULT;
Sage Weil16725b92009-10-06 11:31:07 -0700373 args->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
374 args->cap_release_safety = CEPH_CAPS_PER_RELEASE * 4;
375 args->max_readdir = 1024;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800376 args->congestion_kb = default_congestion_kb();
Sage Weil16725b92009-10-06 11:31:07 -0700377
378 /* ip1[:port1][,ip2[:port2]...]:/subdir/in/fs */
Sage Weil7b813c462009-10-26 22:07:53 -0700379 err = -EINVAL;
Sage Weil16725b92009-10-06 11:31:07 -0700380 if (!dev_name)
Sage Weil7b813c462009-10-26 22:07:53 -0700381 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700382 *path = strstr(dev_name, ":/");
383 if (*path == NULL) {
384 pr_err("device name is missing path (no :/ in %s)\n",
385 dev_name);
Sage Weil7b813c462009-10-26 22:07:53 -0700386 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700387 }
388
389 /* get mon ip(s) */
Sage Weil6b805182009-10-27 11:50:50 -0700390 err = ceph_parse_ips(dev_name, *path, args->mon_addr,
391 CEPH_MAX_MON, &args->num_mon);
Sage Weil16725b92009-10-06 11:31:07 -0700392 if (err < 0)
Sage Weil7b813c462009-10-26 22:07:53 -0700393 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700394
Sage Weil16725b92009-10-06 11:31:07 -0700395 /* path on server */
396 *path += 2;
397 dout("server path '%s'\n", *path);
398
399 /* parse mount options */
400 while ((c = strsep(&options, ",")) != NULL) {
401 int token, intval, ret;
402 if (!*c)
403 continue;
Sage Weil7b813c462009-10-26 22:07:53 -0700404 err = -EINVAL;
Sage Weil16725b92009-10-06 11:31:07 -0700405 token = match_token((char *)c, arg_tokens, argstr);
406 if (token < 0) {
407 pr_err("bad mount option at '%s'\n", c);
Sage Weil7b813c462009-10-26 22:07:53 -0700408 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700409 }
Sage Weile53c2fe2009-10-27 10:19:28 -0700410 if (token < Opt_last_int) {
Sage Weil16725b92009-10-06 11:31:07 -0700411 ret = match_int(&argstr[0], &intval);
412 if (ret < 0) {
413 pr_err("bad mount option arg (not int) "
414 "at '%s'\n", c);
415 continue;
416 }
Sage Weile53c2fe2009-10-27 10:19:28 -0700417 dout("got int token %d val %d\n", token, intval);
418 } else if (token > Opt_last_int && token < Opt_last_string) {
419 dout("got string token %d val %s\n", token,
420 argstr[0].from);
421 } else {
422 dout("got token %d\n", token);
Sage Weil16725b92009-10-06 11:31:07 -0700423 }
424 switch (token) {
425 case Opt_fsidmajor:
426 *(__le64 *)&args->fsid.fsid[0] = cpu_to_le64(intval);
427 break;
428 case Opt_fsidminor:
429 *(__le64 *)&args->fsid.fsid[8] = cpu_to_le64(intval);
430 break;
431 case Opt_ip:
432 err = ceph_parse_ips(argstr[0].from,
433 argstr[0].to,
434 &args->my_addr,
435 1, NULL);
436 if (err < 0)
Sage Weil6b805182009-10-27 11:50:50 -0700437 goto out;
Sage Weil16725b92009-10-06 11:31:07 -0700438 args->flags |= CEPH_OPT_MYIP;
439 break;
440
441 case Opt_snapdirname:
442 kfree(args->snapdir_name);
443 args->snapdir_name = kstrndup(argstr[0].from,
444 argstr[0].to-argstr[0].from,
445 GFP_KERNEL);
446 break;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800447 case Opt_name:
448 args->name = kstrndup(argstr[0].from,
449 argstr[0].to-argstr[0].from,
450 GFP_KERNEL);
451 break;
Sage Weil16725b92009-10-06 11:31:07 -0700452 case Opt_secret:
453 args->secret = kstrndup(argstr[0].from,
454 argstr[0].to-argstr[0].from,
455 GFP_KERNEL);
456 break;
457
458 /* misc */
459 case Opt_wsize:
460 args->wsize = intval;
461 break;
462 case Opt_rsize:
463 args->rsize = intval;
464 break;
465 case Opt_osdtimeout:
466 args->osd_timeout = intval;
467 break;
468 case Opt_mount_timeout:
469 args->mount_timeout = intval;
470 break;
471 case Opt_caps_wanted_delay_min:
472 args->caps_wanted_delay_min = intval;
473 break;
474 case Opt_caps_wanted_delay_max:
475 args->caps_wanted_delay_max = intval;
476 break;
477 case Opt_readdir_max_entries:
478 args->max_readdir = intval;
479 break;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800480 case Opt_congestion_kb:
481 args->congestion_kb = intval;
482 break;
Sage Weil16725b92009-10-06 11:31:07 -0700483
484 case Opt_noshare:
485 args->flags |= CEPH_OPT_NOSHARE;
486 break;
487
488 case Opt_dirstat:
489 args->flags |= CEPH_OPT_DIRSTAT;
490 break;
491 case Opt_nodirstat:
492 args->flags &= ~CEPH_OPT_DIRSTAT;
493 break;
494 case Opt_rbytes:
495 args->flags |= CEPH_OPT_RBYTES;
496 break;
497 case Opt_norbytes:
498 args->flags &= ~CEPH_OPT_RBYTES;
499 break;
500 case Opt_nocrc:
501 args->flags |= CEPH_OPT_NOCRC;
502 break;
503 case Opt_noasyncreaddir:
504 args->flags |= CEPH_OPT_NOASYNCREADDIR;
505 break;
506
507 default:
508 BUG_ON(token);
509 }
510 }
Sage Weil6b805182009-10-27 11:50:50 -0700511 return args;
Sage Weil16725b92009-10-06 11:31:07 -0700512
Sage Weil7b813c462009-10-26 22:07:53 -0700513out:
Sage Weil6b805182009-10-27 11:50:50 -0700514 kfree(args->mon_addr);
515 kfree(args);
516 return ERR_PTR(err);
Sage Weil16725b92009-10-06 11:31:07 -0700517}
518
Sage Weil6b805182009-10-27 11:50:50 -0700519static void destroy_mount_args(struct ceph_mount_args *args)
Sage Weil16725b92009-10-06 11:31:07 -0700520{
Sage Weil6b805182009-10-27 11:50:50 -0700521 dout("destroy_mount_args %p\n", args);
Sage Weil16725b92009-10-06 11:31:07 -0700522 kfree(args->snapdir_name);
523 args->snapdir_name = NULL;
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800524 kfree(args->name);
525 args->name = NULL;
Sage Weil16725b92009-10-06 11:31:07 -0700526 kfree(args->secret);
527 args->secret = NULL;
Sage Weil6b805182009-10-27 11:50:50 -0700528 kfree(args);
Sage Weil16725b92009-10-06 11:31:07 -0700529}
530
531/*
532 * create a fresh client instance
533 */
Sage Weil6b805182009-10-27 11:50:50 -0700534static struct ceph_client *ceph_create_client(struct ceph_mount_args *args)
Sage Weil16725b92009-10-06 11:31:07 -0700535{
536 struct ceph_client *client;
537 int err = -ENOMEM;
538
539 client = kzalloc(sizeof(*client), GFP_KERNEL);
540 if (client == NULL)
541 return ERR_PTR(-ENOMEM);
542
543 mutex_init(&client->mount_mutex);
544
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800545 init_waitqueue_head(&client->auth_wq);
Sage Weil16725b92009-10-06 11:31:07 -0700546
547 client->sb = NULL;
548 client->mount_state = CEPH_MOUNT_MOUNTING;
Sage Weil6b805182009-10-27 11:50:50 -0700549 client->mount_args = args;
Sage Weil16725b92009-10-06 11:31:07 -0700550
551 client->msgr = NULL;
552
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800553 client->auth_err = 0;
Yehuda Sadeh2baba252009-12-18 13:51:57 -0800554 atomic_long_set(&client->writeback_count, 0);
Sage Weil16725b92009-10-06 11:31:07 -0700555
Sage Weil859e7b12009-11-02 09:32:47 -0800556 err = bdi_init(&client->backing_dev_info);
557 if (err < 0)
558 goto fail;
559
Sage Weil16725b92009-10-06 11:31:07 -0700560 err = -ENOMEM;
561 client->wb_wq = create_workqueue("ceph-writeback");
562 if (client->wb_wq == NULL)
Sage Weil859e7b12009-11-02 09:32:47 -0800563 goto fail_bdi;
Sage Weil16725b92009-10-06 11:31:07 -0700564 client->pg_inv_wq = create_singlethread_workqueue("ceph-pg-invalid");
565 if (client->pg_inv_wq == NULL)
566 goto fail_wb_wq;
567 client->trunc_wq = create_singlethread_workqueue("ceph-trunc");
568 if (client->trunc_wq == NULL)
569 goto fail_pg_inv_wq;
570
Sage Weilb9bfb932009-11-18 15:08:44 -0800571 /* set up mempools */
572 err = -ENOMEM;
573 client->wb_pagevec_pool = mempool_create_kmalloc_pool(10,
574 client->mount_args->wsize >> PAGE_CACHE_SHIFT);
575 if (!client->wb_pagevec_pool)
576 goto fail_trunc_wq;
577
578
Sage Weil16725b92009-10-06 11:31:07 -0700579 /* subsystems */
580 err = ceph_monc_init(&client->monc, client);
581 if (err < 0)
Sage Weilb9bfb932009-11-18 15:08:44 -0800582 goto fail_mempool;
Sage Weil16725b92009-10-06 11:31:07 -0700583 err = ceph_osdc_init(&client->osdc, client);
584 if (err < 0)
585 goto fail_monc;
Sage Weil5f44f142009-11-18 14:52:18 -0800586 err = ceph_mdsc_init(&client->mdsc, client);
587 if (err < 0)
588 goto fail_osdc;
Sage Weil16725b92009-10-06 11:31:07 -0700589 return client;
590
Sage Weil5f44f142009-11-18 14:52:18 -0800591fail_osdc:
592 ceph_osdc_stop(&client->osdc);
Sage Weil16725b92009-10-06 11:31:07 -0700593fail_monc:
594 ceph_monc_stop(&client->monc);
Sage Weilb9bfb932009-11-18 15:08:44 -0800595fail_mempool:
596 mempool_destroy(client->wb_pagevec_pool);
Sage Weil16725b92009-10-06 11:31:07 -0700597fail_trunc_wq:
598 destroy_workqueue(client->trunc_wq);
599fail_pg_inv_wq:
600 destroy_workqueue(client->pg_inv_wq);
601fail_wb_wq:
602 destroy_workqueue(client->wb_wq);
Sage Weil859e7b12009-11-02 09:32:47 -0800603fail_bdi:
604 bdi_destroy(&client->backing_dev_info);
Sage Weil16725b92009-10-06 11:31:07 -0700605fail:
606 kfree(client);
607 return ERR_PTR(err);
608}
609
610static void ceph_destroy_client(struct ceph_client *client)
611{
612 dout("destroy_client %p\n", client);
613
614 /* unmount */
615 ceph_mdsc_stop(&client->mdsc);
616 ceph_monc_stop(&client->monc);
617 ceph_osdc_stop(&client->osdc);
618
Sage Weil16725b92009-10-06 11:31:07 -0700619 ceph_debugfs_client_cleanup(client);
620 destroy_workqueue(client->wb_wq);
621 destroy_workqueue(client->pg_inv_wq);
622 destroy_workqueue(client->trunc_wq);
623
624 if (client->msgr)
625 ceph_messenger_destroy(client->msgr);
Sage Weilb9bfb932009-11-18 15:08:44 -0800626 mempool_destroy(client->wb_pagevec_pool);
Sage Weil16725b92009-10-06 11:31:07 -0700627
Sage Weil6b805182009-10-27 11:50:50 -0700628 destroy_mount_args(client->mount_args);
Sage Weil16725b92009-10-06 11:31:07 -0700629
630 kfree(client);
631 dout("destroy_client %p done\n", client);
632}
633
634/*
Sage Weil07433042009-11-18 16:50:41 -0800635 * Initially learn our fsid, or verify an fsid matches.
636 */
637int ceph_check_fsid(struct ceph_client *client, struct ceph_fsid *fsid)
638{
639 if (client->have_fsid) {
640 if (ceph_fsid_compare(&client->fsid, fsid)) {
Sage Weil9ec7cab2009-12-14 15:13:47 -0800641 pr_err("bad fsid, had " FSID_FORMAT " got " FSID_FORMAT,
642 PR_FSID(&client->fsid), PR_FSID(fsid));
Sage Weil07433042009-11-18 16:50:41 -0800643 return -1;
644 }
645 } else {
646 pr_info("client%lld fsid " FSID_FORMAT "\n",
647 client->monc.auth->global_id, PR_FSID(fsid));
648 memcpy(&client->fsid, fsid, sizeof(*fsid));
649 ceph_debugfs_client_init(client);
650 client->have_fsid = true;
651 }
652 return 0;
653}
654
655/*
Sage Weil16725b92009-10-06 11:31:07 -0700656 * true if we have the mon map (and have thus joined the cluster)
657 */
658static int have_mon_map(struct ceph_client *client)
659{
660 return client->monc.monmap && client->monc.monmap->epoch;
661}
662
663/*
664 * Bootstrap mount by opening the root directory. Note the mount
665 * @started time from caller, and time out if this takes too long.
666 */
667static struct dentry *open_root_dentry(struct ceph_client *client,
668 const char *path,
669 unsigned long started)
670{
671 struct ceph_mds_client *mdsc = &client->mdsc;
672 struct ceph_mds_request *req = NULL;
673 int err;
674 struct dentry *root;
675
676 /* open dir */
677 dout("open_root_inode opening '%s'\n", path);
678 req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
679 if (IS_ERR(req))
680 return ERR_PTR(PTR_ERR(req));
681 req->r_path1 = kstrdup(path, GFP_NOFS);
682 req->r_ino1.ino = CEPH_INO_ROOT;
683 req->r_ino1.snap = CEPH_NOSNAP;
684 req->r_started = started;
Sage Weil6b805182009-10-27 11:50:50 -0700685 req->r_timeout = client->mount_args->mount_timeout * HZ;
Sage Weil16725b92009-10-06 11:31:07 -0700686 req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
687 req->r_num_caps = 2;
688 err = ceph_mdsc_do_request(mdsc, NULL, req);
689 if (err == 0) {
690 dout("open_root_inode success\n");
691 if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
692 client->sb->s_root == NULL)
693 root = d_alloc_root(req->r_target_inode);
694 else
695 root = d_obtain_alias(req->r_target_inode);
696 req->r_target_inode = NULL;
697 dout("open_root_inode success, root dentry is %p\n", root);
698 } else {
699 root = ERR_PTR(err);
700 }
701 ceph_mdsc_put_request(req);
702 return root;
703}
704
705/*
706 * mount: join the ceph cluster, and open root directory.
707 */
708static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt,
709 const char *path)
710{
711 struct ceph_entity_addr *myaddr = NULL;
712 int err;
Sage Weil6b805182009-10-27 11:50:50 -0700713 unsigned long timeout = client->mount_args->mount_timeout * HZ;
Sage Weil16725b92009-10-06 11:31:07 -0700714 unsigned long started = jiffies; /* note the start time */
715 struct dentry *root;
716
717 dout("mount start\n");
718 mutex_lock(&client->mount_mutex);
719
720 /* initialize the messenger */
721 if (client->msgr == NULL) {
722 if (ceph_test_opt(client, MYIP))
Sage Weil6b805182009-10-27 11:50:50 -0700723 myaddr = &client->mount_args->my_addr;
Sage Weil16725b92009-10-06 11:31:07 -0700724 client->msgr = ceph_messenger_create(myaddr);
725 if (IS_ERR(client->msgr)) {
726 err = PTR_ERR(client->msgr);
727 client->msgr = NULL;
728 goto out;
729 }
730 client->msgr->nocrc = ceph_test_opt(client, NOCRC);
731 }
732
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800733 /* open session, and wait for mon, mds, and osd maps */
734 err = ceph_monc_open_session(&client->monc);
Sage Weil16725b92009-10-06 11:31:07 -0700735 if (err < 0)
736 goto out;
737
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800738 while (!have_mon_map(client)) {
Sage Weil16725b92009-10-06 11:31:07 -0700739 err = -EIO;
740 if (timeout && time_after_eq(jiffies, started + timeout))
741 goto out;
742
743 /* wait */
Sage Weil4e7a5dc2009-11-18 16:19:57 -0800744 dout("mount waiting for mon_map\n");
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800745 err = wait_event_interruptible_timeout(client->auth_wq,
746 have_mon_map(client) || (client->auth_err < 0),
Sage Weil16725b92009-10-06 11:31:07 -0700747 timeout);
748 if (err == -EINTR || err == -ERESTARTSYS)
749 goto out;
Sage Weil9bd2e6f2010-02-02 16:21:06 -0800750 if (client->auth_err < 0) {
751 err = client->auth_err;
Yehuda Sadehdc146572009-11-20 13:59:13 -0800752 goto out;
753 }
Sage Weil16725b92009-10-06 11:31:07 -0700754 }
755
756 dout("mount opening root\n");
757 root = open_root_dentry(client, "", started);
758 if (IS_ERR(root)) {
759 err = PTR_ERR(root);
760 goto out;
761 }
762 if (client->sb->s_root)
763 dput(root);
764 else
765 client->sb->s_root = root;
766
767 if (path[0] == 0) {
768 dget(root);
769 } else {
770 dout("mount opening base mountpoint\n");
771 root = open_root_dentry(client, path, started);
772 if (IS_ERR(root)) {
773 err = PTR_ERR(root);
774 dput(client->sb->s_root);
775 client->sb->s_root = NULL;
776 goto out;
777 }
778 }
779
780 mnt->mnt_root = root;
781 mnt->mnt_sb = client->sb;
782
783 client->mount_state = CEPH_MOUNT_MOUNTED;
784 dout("mount success\n");
785 err = 0;
786
787out:
788 mutex_unlock(&client->mount_mutex);
789 return err;
790}
791
792static int ceph_set_super(struct super_block *s, void *data)
793{
794 struct ceph_client *client = data;
795 int ret;
796
797 dout("set_super %p data %p\n", s, data);
798
Sage Weil6b805182009-10-27 11:50:50 -0700799 s->s_flags = client->mount_args->sb_flags;
Sage Weil16725b92009-10-06 11:31:07 -0700800 s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */
801
802 s->s_fs_info = client;
803 client->sb = s;
804
805 s->s_op = &ceph_super_ops;
806 s->s_export_op = &ceph_export_ops;
807
808 s->s_time_gran = 1000; /* 1000 ns == 1 us */
809
810 ret = set_anon_super(s, NULL); /* what is that second arg for? */
811 if (ret != 0)
812 goto fail;
813
814 return ret;
815
816fail:
817 s->s_fs_info = NULL;
818 client->sb = NULL;
819 return ret;
820}
821
822/*
823 * share superblock if same fs AND options
824 */
825static int ceph_compare_super(struct super_block *sb, void *data)
826{
827 struct ceph_client *new = data;
Sage Weil6b805182009-10-27 11:50:50 -0700828 struct ceph_mount_args *args = new->mount_args;
Sage Weil16725b92009-10-06 11:31:07 -0700829 struct ceph_client *other = ceph_sb_to_client(sb);
830 int i;
831
832 dout("ceph_compare_super %p\n", sb);
833 if (args->flags & CEPH_OPT_FSID) {
834 if (ceph_fsid_compare(&args->fsid, &other->fsid)) {
835 dout("fsid doesn't match\n");
836 return 0;
837 }
838 } else {
839 /* do we share (a) monitor? */
840 for (i = 0; i < new->monc.monmap->num_mon; i++)
841 if (ceph_monmap_contains(other->monc.monmap,
842 &new->monc.monmap->mon_inst[i].addr))
843 break;
844 if (i == new->monc.monmap->num_mon) {
845 dout("mon ip not part of monmap\n");
846 return 0;
847 }
848 dout("mon ip matches existing sb %p\n", sb);
849 }
Sage Weil6b805182009-10-27 11:50:50 -0700850 if (args->sb_flags != other->mount_args->sb_flags) {
Sage Weil16725b92009-10-06 11:31:07 -0700851 dout("flags differ\n");
852 return 0;
853 }
854 return 1;
855}
856
857/*
858 * construct our own bdi so we can control readahead, etc.
859 */
Sage Weil859e7b12009-11-02 09:32:47 -0800860static int ceph_register_bdi(struct super_block *sb, struct ceph_client *client)
Sage Weil16725b92009-10-06 11:31:07 -0700861{
862 int err;
863
Sage Weilf2cf4182009-10-14 14:09:07 -0700864 sb->s_bdi = &client->backing_dev_info;
Sage Weil16725b92009-10-06 11:31:07 -0700865
866 /* set ra_pages based on rsize mount option? */
Sage Weil6b805182009-10-27 11:50:50 -0700867 if (client->mount_args->rsize >= PAGE_CACHE_SIZE)
Sage Weil16725b92009-10-06 11:31:07 -0700868 client->backing_dev_info.ra_pages =
Sage Weil6b805182009-10-27 11:50:50 -0700869 (client->mount_args->rsize + PAGE_CACHE_SIZE - 1)
Sage Weil16725b92009-10-06 11:31:07 -0700870 >> PAGE_SHIFT;
Sage Weil16725b92009-10-06 11:31:07 -0700871 err = bdi_register_dev(&client->backing_dev_info, sb->s_dev);
872 return err;
873}
874
875static int ceph_get_sb(struct file_system_type *fs_type,
876 int flags, const char *dev_name, void *data,
877 struct vfsmount *mnt)
878{
879 struct super_block *sb;
880 struct ceph_client *client;
881 int err;
882 int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
Sage Weil6a18be12009-11-04 11:40:05 -0800883 const char *path = NULL;
Sage Weil6b805182009-10-27 11:50:50 -0700884 struct ceph_mount_args *args;
Sage Weil16725b92009-10-06 11:31:07 -0700885
886 dout("ceph_get_sb\n");
Sage Weil6b805182009-10-27 11:50:50 -0700887 args = parse_mount_args(flags, data, dev_name, &path);
888 if (IS_ERR(args)) {
889 err = PTR_ERR(args);
890 goto out_final;
891 }
Sage Weil16725b92009-10-06 11:31:07 -0700892
893 /* create client (which we may/may not use) */
Sage Weil6b805182009-10-27 11:50:50 -0700894 client = ceph_create_client(args);
895 if (IS_ERR(client)) {
896 err = PTR_ERR(client);
897 goto out_final;
898 }
Sage Weil16725b92009-10-06 11:31:07 -0700899
Sage Weil6b805182009-10-27 11:50:50 -0700900 if (client->mount_args->flags & CEPH_OPT_NOSHARE)
Sage Weil16725b92009-10-06 11:31:07 -0700901 compare_super = NULL;
902 sb = sget(fs_type, compare_super, ceph_set_super, client);
903 if (IS_ERR(sb)) {
904 err = PTR_ERR(sb);
905 goto out;
906 }
907
908 if (ceph_client(sb) != client) {
909 ceph_destroy_client(client);
910 client = ceph_client(sb);
911 dout("get_sb got existing client %p\n", client);
912 } else {
913 dout("get_sb using new client %p\n", client);
Sage Weil859e7b12009-11-02 09:32:47 -0800914 err = ceph_register_bdi(sb, client);
Sage Weil16725b92009-10-06 11:31:07 -0700915 if (err < 0)
916 goto out_splat;
917 }
918
919 err = ceph_mount(client, mnt, path);
920 if (err < 0)
921 goto out_splat;
922 dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root,
923 mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode));
924 return 0;
925
926out_splat:
927 ceph_mdsc_close_sessions(&client->mdsc);
928 up_write(&sb->s_umount);
929 deactivate_super(sb);
930 goto out_final;
931
932out:
933 ceph_destroy_client(client);
934out_final:
935 dout("ceph_get_sb fail %d\n", err);
936 return err;
937}
938
939static void ceph_kill_sb(struct super_block *s)
940{
941 struct ceph_client *client = ceph_sb_to_client(s);
942 dout("kill_sb %p\n", s);
943 ceph_mdsc_pre_umount(&client->mdsc);
Sage Weil16725b92009-10-06 11:31:07 -0700944 kill_anon_super(s); /* will call put_super after sb is r/o */
Sage Weile0e32712009-12-21 21:04:26 -0800945 if (s->s_bdi == &client->backing_dev_info)
946 bdi_unregister(&client->backing_dev_info);
Sage Weil16725b92009-10-06 11:31:07 -0700947 bdi_destroy(&client->backing_dev_info);
948 ceph_destroy_client(client);
949}
950
951static struct file_system_type ceph_fs_type = {
952 .owner = THIS_MODULE,
953 .name = "ceph",
954 .get_sb = ceph_get_sb,
955 .kill_sb = ceph_kill_sb,
956 .fs_flags = FS_RENAME_DOES_D_MOVE,
957};
958
959#define _STRINGIFY(x) #x
960#define STRINGIFY(x) _STRINGIFY(x)
961
962static int __init init_ceph(void)
963{
964 int ret = 0;
965
966 ret = ceph_debugfs_init();
967 if (ret < 0)
968 goto out;
969
970 ret = ceph_msgr_init();
971 if (ret < 0)
972 goto out_debugfs;
973
974 ret = init_caches();
975 if (ret)
976 goto out_msgr;
977
978 ceph_caps_init();
979
980 ret = register_filesystem(&ceph_fs_type);
981 if (ret)
982 goto out_icache;
983
Sage Weilfa0b72e2009-10-07 10:59:10 -0700984 pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n",
985 CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH,
986 CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL);
Sage Weil16725b92009-10-06 11:31:07 -0700987 return 0;
988
989out_icache:
990 destroy_caches();
991out_msgr:
992 ceph_msgr_exit();
993out_debugfs:
994 ceph_debugfs_cleanup();
995out:
996 return ret;
997}
998
999static void __exit exit_ceph(void)
1000{
1001 dout("exit_ceph\n");
1002 unregister_filesystem(&ceph_fs_type);
1003 ceph_caps_finalize();
1004 destroy_caches();
1005 ceph_msgr_exit();
1006 ceph_debugfs_cleanup();
1007}
1008
1009module_init(init_ceph);
1010module_exit(exit_ceph);
1011
1012MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
1013MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
1014MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
1015MODULE_DESCRIPTION("Ceph filesystem for Linux");
1016MODULE_LICENSE("GPL");