]>
Commit | Line | Data |
---|---|---|
16725b9d SW |
1 | |
2 | #include "ceph_debug.h" | |
3 | ||
4 | #include <linux/backing-dev.h> | |
5 | #include <linux/fs.h> | |
6 | #include <linux/inet.h> | |
7 | #include <linux/in6.h> | |
8 | #include <linux/module.h> | |
9 | #include <linux/mount.h> | |
10 | #include <linux/parser.h> | |
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 | ||
16725b9d SW |
19 | #include "decode.h" |
20 | #include "super.h" | |
21 | #include "mon_client.h" | |
0743304d | 22 | #include "auth.h" |
16725b9d SW |
23 | |
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 | */ | |
34 | const 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 | */ | |
47 | static 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 | ||
56 | static 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 | ||
96 | static 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); | |
f2cf418c | 101 | dout("sync_fs %d done\n", wait); |
16725b9d SW |
102 | 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 | */ | |
111 | static int ceph_show_options(struct seq_file *m, struct vfsmount *mnt) | |
112 | { | |
113 | struct ceph_client *client = ceph_sb_to_client(mnt->mnt_sb); | |
6b805185 | 114 | struct ceph_mount_args *args = client->mount_args; |
16725b9d SW |
115 | |
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); | |
4e7a5dcd SW |
132 | if (args->name) |
133 | seq_printf(m, ",name=%s", args->name); | |
16725b9d SW |
134 | if (args->secret) |
135 | seq_puts(m, ",secret=<hidden>"); | |
136 | return 0; | |
137 | } | |
138 | ||
139 | /* | |
140 | * caches | |
141 | */ | |
142 | struct kmem_cache *ceph_inode_cachep; | |
143 | struct kmem_cache *ceph_cap_cachep; | |
144 | struct kmem_cache *ceph_dentry_cachep; | |
145 | struct kmem_cache *ceph_file_cachep; | |
146 | ||
147 | static void ceph_inode_init_once(void *foo) | |
148 | { | |
149 | struct ceph_inode_info *ci = foo; | |
150 | inode_init_once(&ci->vfs_inode); | |
151 | } | |
152 | ||
153 | static 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 | ||
180 | bad_file: | |
181 | kmem_cache_destroy(ceph_dentry_cachep); | |
182 | bad_dentry: | |
183 | kmem_cache_destroy(ceph_cap_cachep); | |
184 | bad_cap: | |
185 | kmem_cache_destroy(ceph_inode_cachep); | |
186 | return -ENOMEM; | |
187 | } | |
188 | ||
189 | static 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 | */ | |
202 | static 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 | ||
213 | static 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 | ||
225 | const 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"; | |
4e7a5dcd SW |
230 | case CEPH_MSG_AUTH: return "auth"; |
231 | case CEPH_MSG_AUTH_REPLY: return "auth_reply"; | |
16725b9d SW |
232 | 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"; | |
16725b9d SW |
236 | case CEPH_MSG_STATFS: return "statfs"; |
237 | case CEPH_MSG_STATFS_REPLY: return "statfs_reply"; | |
16725b9d SW |
238 | 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"; | |
16725b9d SW |
248 | 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 | */ | |
259 | enum { | |
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, | |
e53c2fe0 | 270 | Opt_last_int, |
16725b9d SW |
271 | /* int args above */ |
272 | Opt_snapdirname, | |
4e7a5dcd | 273 | Opt_name, |
16725b9d | 274 | Opt_secret, |
e53c2fe0 | 275 | Opt_last_string, |
16725b9d SW |
276 | /* 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 | ||
287 | static 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"}, | |
4e7a5dcd | 300 | {Opt_name, "name=%s"}, |
16725b9d SW |
301 | {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 | ||
6b805185 SW |
315 | static struct ceph_mount_args *parse_mount_args(int flags, char *options, |
316 | const char *dev_name, | |
317 | const char **path) | |
16725b9d | 318 | { |
6b805185 | 319 | struct ceph_mount_args *args; |
16725b9d | 320 | const char *c; |
6b805185 | 321 | int err = -ENOMEM; |
16725b9d | 322 | substring_t argstr[MAX_OPT_ARGS]; |
16725b9d | 323 | |
6b805185 SW |
324 | 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; | |
16725b9d | 331 | |
6b805185 | 332 | dout("parse_mount_args %p, dev_name '%s'\n", args, dev_name); |
7b813c46 | 333 | |
16725b9d SW |
334 | /* 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; | |
8fa97655 | 341 | args->rsize = CEPH_MOUNT_RSIZE_DEFAULT; |
16725b9d SW |
342 | 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 */ | |
7b813c46 | 347 | err = -EINVAL; |
16725b9d | 348 | if (!dev_name) |
7b813c46 | 349 | goto out; |
16725b9d SW |
350 | *path = strstr(dev_name, ":/"); |
351 | if (*path == NULL) { | |
352 | pr_err("device name is missing path (no :/ in %s)\n", | |
353 | dev_name); | |
7b813c46 | 354 | goto out; |
16725b9d SW |
355 | } |
356 | ||
357 | /* get mon ip(s) */ | |
6b805185 SW |
358 | err = ceph_parse_ips(dev_name, *path, args->mon_addr, |
359 | CEPH_MAX_MON, &args->num_mon); | |
16725b9d | 360 | if (err < 0) |
7b813c46 | 361 | goto out; |
16725b9d | 362 | |
16725b9d SW |
363 | /* 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; | |
7b813c46 | 372 | err = -EINVAL; |
16725b9d SW |
373 | token = match_token((char *)c, arg_tokens, argstr); |
374 | if (token < 0) { | |
375 | pr_err("bad mount option at '%s'\n", c); | |
7b813c46 | 376 | goto out; |
16725b9d | 377 | } |
e53c2fe0 | 378 | if (token < Opt_last_int) { |
16725b9d SW |
379 | 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 | } | |
e53c2fe0 SW |
385 | 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); | |
16725b9d SW |
391 | } |
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) | |
6b805185 | 405 | goto out; |
16725b9d SW |
406 | 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; | |
4e7a5dcd SW |
415 | case Opt_name: |
416 | args->name = kstrndup(argstr[0].from, | |
417 | argstr[0].to-argstr[0].from, | |
418 | GFP_KERNEL); | |
419 | break; | |
16725b9d SW |
420 | 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 | } | |
6b805185 | 476 | return args; |
16725b9d | 477 | |
7b813c46 | 478 | out: |
6b805185 SW |
479 | kfree(args->mon_addr); |
480 | kfree(args); | |
481 | return ERR_PTR(err); | |
16725b9d SW |
482 | } |
483 | ||
6b805185 | 484 | static void destroy_mount_args(struct ceph_mount_args *args) |
16725b9d | 485 | { |
6b805185 | 486 | dout("destroy_mount_args %p\n", args); |
16725b9d SW |
487 | kfree(args->snapdir_name); |
488 | args->snapdir_name = NULL; | |
4e7a5dcd SW |
489 | kfree(args->name); |
490 | args->name = NULL; | |
16725b9d SW |
491 | kfree(args->secret); |
492 | args->secret = NULL; | |
6b805185 | 493 | kfree(args); |
16725b9d SW |
494 | } |
495 | ||
496 | /* | |
497 | * create a fresh client instance | |
498 | */ | |
6b805185 | 499 | static struct ceph_client *ceph_create_client(struct ceph_mount_args *args) |
16725b9d SW |
500 | { |
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; | |
6b805185 | 514 | client->mount_args = args; |
16725b9d SW |
515 | |
516 | client->msgr = NULL; | |
517 | ||
518 | client->mount_err = 0; | |
16725b9d | 519 | |
859e7b14 SW |
520 | err = bdi_init(&client->backing_dev_info); |
521 | if (err < 0) | |
522 | goto fail; | |
523 | ||
16725b9d SW |
524 | err = -ENOMEM; |
525 | client->wb_wq = create_workqueue("ceph-writeback"); | |
526 | if (client->wb_wq == NULL) | |
859e7b14 | 527 | goto fail_bdi; |
16725b9d SW |
528 | 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 | ||
b9bfb93c SW |
535 | /* 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 | ||
16725b9d SW |
543 | /* subsystems */ |
544 | err = ceph_monc_init(&client->monc, client); | |
545 | if (err < 0) | |
b9bfb93c | 546 | goto fail_mempool; |
16725b9d SW |
547 | err = ceph_osdc_init(&client->osdc, client); |
548 | if (err < 0) | |
549 | goto fail_monc; | |
5f44f142 SW |
550 | err = ceph_mdsc_init(&client->mdsc, client); |
551 | if (err < 0) | |
552 | goto fail_osdc; | |
16725b9d SW |
553 | return client; |
554 | ||
5f44f142 SW |
555 | fail_osdc: |
556 | ceph_osdc_stop(&client->osdc); | |
16725b9d SW |
557 | fail_monc: |
558 | ceph_monc_stop(&client->monc); | |
b9bfb93c SW |
559 | fail_mempool: |
560 | mempool_destroy(client->wb_pagevec_pool); | |
16725b9d SW |
561 | fail_trunc_wq: |
562 | destroy_workqueue(client->trunc_wq); | |
563 | fail_pg_inv_wq: | |
564 | destroy_workqueue(client->pg_inv_wq); | |
565 | fail_wb_wq: | |
566 | destroy_workqueue(client->wb_wq); | |
859e7b14 SW |
567 | fail_bdi: |
568 | bdi_destroy(&client->backing_dev_info); | |
16725b9d SW |
569 | fail: |
570 | kfree(client); | |
571 | return ERR_PTR(err); | |
572 | } | |
573 | ||
574 | static 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 | ||
16725b9d SW |
583 | 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); | |
b9bfb93c | 590 | mempool_destroy(client->wb_pagevec_pool); |
16725b9d | 591 | |
6b805185 | 592 | destroy_mount_args(client->mount_args); |
16725b9d SW |
593 | |
594 | kfree(client); | |
595 | dout("destroy_client %p done\n", client); | |
596 | } | |
597 | ||
0743304d SW |
598 | /* |
599 | * Initially learn our fsid, or verify an fsid matches. | |
600 | */ | |
601 | int 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)) { | |
605 | print_hex_dump(KERN_ERR, "this fsid: ", | |
606 | DUMP_PREFIX_NONE, 16, 1, | |
607 | (void *)fsid, 16, 0); | |
608 | print_hex_dump(KERN_ERR, " old fsid: ", | |
609 | DUMP_PREFIX_NONE, 16, 1, | |
610 | (void *)&client->fsid, 16, 0); | |
611 | pr_err("fsid mismatch\n"); | |
612 | return -1; | |
613 | } | |
614 | } else { | |
615 | pr_info("client%lld fsid " FSID_FORMAT "\n", | |
616 | client->monc.auth->global_id, PR_FSID(fsid)); | |
617 | memcpy(&client->fsid, fsid, sizeof(*fsid)); | |
618 | ceph_debugfs_client_init(client); | |
619 | client->have_fsid = true; | |
620 | } | |
621 | return 0; | |
622 | } | |
623 | ||
16725b9d SW |
624 | /* |
625 | * true if we have the mon map (and have thus joined the cluster) | |
626 | */ | |
627 | static int have_mon_map(struct ceph_client *client) | |
628 | { | |
629 | return client->monc.monmap && client->monc.monmap->epoch; | |
630 | } | |
631 | ||
632 | /* | |
633 | * Bootstrap mount by opening the root directory. Note the mount | |
634 | * @started time from caller, and time out if this takes too long. | |
635 | */ | |
636 | static struct dentry *open_root_dentry(struct ceph_client *client, | |
637 | const char *path, | |
638 | unsigned long started) | |
639 | { | |
640 | struct ceph_mds_client *mdsc = &client->mdsc; | |
641 | struct ceph_mds_request *req = NULL; | |
642 | int err; | |
643 | struct dentry *root; | |
644 | ||
645 | /* open dir */ | |
646 | dout("open_root_inode opening '%s'\n", path); | |
647 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS); | |
648 | if (IS_ERR(req)) | |
649 | return ERR_PTR(PTR_ERR(req)); | |
650 | req->r_path1 = kstrdup(path, GFP_NOFS); | |
651 | req->r_ino1.ino = CEPH_INO_ROOT; | |
652 | req->r_ino1.snap = CEPH_NOSNAP; | |
653 | req->r_started = started; | |
6b805185 | 654 | req->r_timeout = client->mount_args->mount_timeout * HZ; |
16725b9d SW |
655 | req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE); |
656 | req->r_num_caps = 2; | |
657 | err = ceph_mdsc_do_request(mdsc, NULL, req); | |
658 | if (err == 0) { | |
659 | dout("open_root_inode success\n"); | |
660 | if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT && | |
661 | client->sb->s_root == NULL) | |
662 | root = d_alloc_root(req->r_target_inode); | |
663 | else | |
664 | root = d_obtain_alias(req->r_target_inode); | |
665 | req->r_target_inode = NULL; | |
666 | dout("open_root_inode success, root dentry is %p\n", root); | |
667 | } else { | |
668 | root = ERR_PTR(err); | |
669 | } | |
670 | ceph_mdsc_put_request(req); | |
671 | return root; | |
672 | } | |
673 | ||
674 | /* | |
675 | * mount: join the ceph cluster, and open root directory. | |
676 | */ | |
677 | static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt, | |
678 | const char *path) | |
679 | { | |
680 | struct ceph_entity_addr *myaddr = NULL; | |
681 | int err; | |
6b805185 | 682 | unsigned long timeout = client->mount_args->mount_timeout * HZ; |
16725b9d SW |
683 | unsigned long started = jiffies; /* note the start time */ |
684 | struct dentry *root; | |
685 | ||
686 | dout("mount start\n"); | |
687 | mutex_lock(&client->mount_mutex); | |
688 | ||
689 | /* initialize the messenger */ | |
690 | if (client->msgr == NULL) { | |
691 | if (ceph_test_opt(client, MYIP)) | |
6b805185 | 692 | myaddr = &client->mount_args->my_addr; |
16725b9d SW |
693 | client->msgr = ceph_messenger_create(myaddr); |
694 | if (IS_ERR(client->msgr)) { | |
695 | err = PTR_ERR(client->msgr); | |
696 | client->msgr = NULL; | |
697 | goto out; | |
698 | } | |
699 | client->msgr->nocrc = ceph_test_opt(client, NOCRC); | |
700 | } | |
701 | ||
4e7a5dcd SW |
702 | /* open session, and wait for mon, mds, and osd maps */ |
703 | err = ceph_monc_open_session(&client->monc); | |
16725b9d SW |
704 | if (err < 0) |
705 | goto out; | |
706 | ||
4e7a5dcd | 707 | while (!have_mon_map(client)) { |
16725b9d SW |
708 | err = -EIO; |
709 | if (timeout && time_after_eq(jiffies, started + timeout)) | |
710 | goto out; | |
711 | ||
712 | /* wait */ | |
4e7a5dcd SW |
713 | dout("mount waiting for mon_map\n"); |
714 | err = wait_event_interruptible_timeout(client->mount_wq, /* FIXME */ | |
dc14657c | 715 | have_mon_map(client) || (client->mount_err < 0), |
16725b9d SW |
716 | timeout); |
717 | if (err == -EINTR || err == -ERESTARTSYS) | |
718 | goto out; | |
dc14657c YS |
719 | if (client->mount_err < 0) { |
720 | err = client->mount_err; | |
721 | goto out; | |
722 | } | |
16725b9d SW |
723 | } |
724 | ||
725 | dout("mount opening root\n"); | |
726 | root = open_root_dentry(client, "", started); | |
727 | if (IS_ERR(root)) { | |
728 | err = PTR_ERR(root); | |
729 | goto out; | |
730 | } | |
731 | if (client->sb->s_root) | |
732 | dput(root); | |
733 | else | |
734 | client->sb->s_root = root; | |
735 | ||
736 | if (path[0] == 0) { | |
737 | dget(root); | |
738 | } else { | |
739 | dout("mount opening base mountpoint\n"); | |
740 | root = open_root_dentry(client, path, started); | |
741 | if (IS_ERR(root)) { | |
742 | err = PTR_ERR(root); | |
743 | dput(client->sb->s_root); | |
744 | client->sb->s_root = NULL; | |
745 | goto out; | |
746 | } | |
747 | } | |
748 | ||
749 | mnt->mnt_root = root; | |
750 | mnt->mnt_sb = client->sb; | |
751 | ||
752 | client->mount_state = CEPH_MOUNT_MOUNTED; | |
753 | dout("mount success\n"); | |
754 | err = 0; | |
755 | ||
756 | out: | |
757 | mutex_unlock(&client->mount_mutex); | |
758 | return err; | |
759 | } | |
760 | ||
761 | static int ceph_set_super(struct super_block *s, void *data) | |
762 | { | |
763 | struct ceph_client *client = data; | |
764 | int ret; | |
765 | ||
766 | dout("set_super %p data %p\n", s, data); | |
767 | ||
6b805185 | 768 | s->s_flags = client->mount_args->sb_flags; |
16725b9d SW |
769 | s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */ |
770 | ||
771 | s->s_fs_info = client; | |
772 | client->sb = s; | |
773 | ||
774 | s->s_op = &ceph_super_ops; | |
775 | s->s_export_op = &ceph_export_ops; | |
776 | ||
777 | s->s_time_gran = 1000; /* 1000 ns == 1 us */ | |
778 | ||
779 | ret = set_anon_super(s, NULL); /* what is that second arg for? */ | |
780 | if (ret != 0) | |
781 | goto fail; | |
782 | ||
783 | return ret; | |
784 | ||
785 | fail: | |
786 | s->s_fs_info = NULL; | |
787 | client->sb = NULL; | |
788 | return ret; | |
789 | } | |
790 | ||
791 | /* | |
792 | * share superblock if same fs AND options | |
793 | */ | |
794 | static int ceph_compare_super(struct super_block *sb, void *data) | |
795 | { | |
796 | struct ceph_client *new = data; | |
6b805185 | 797 | struct ceph_mount_args *args = new->mount_args; |
16725b9d SW |
798 | struct ceph_client *other = ceph_sb_to_client(sb); |
799 | int i; | |
800 | ||
801 | dout("ceph_compare_super %p\n", sb); | |
802 | if (args->flags & CEPH_OPT_FSID) { | |
803 | if (ceph_fsid_compare(&args->fsid, &other->fsid)) { | |
804 | dout("fsid doesn't match\n"); | |
805 | return 0; | |
806 | } | |
807 | } else { | |
808 | /* do we share (a) monitor? */ | |
809 | for (i = 0; i < new->monc.monmap->num_mon; i++) | |
810 | if (ceph_monmap_contains(other->monc.monmap, | |
811 | &new->monc.monmap->mon_inst[i].addr)) | |
812 | break; | |
813 | if (i == new->monc.monmap->num_mon) { | |
814 | dout("mon ip not part of monmap\n"); | |
815 | return 0; | |
816 | } | |
817 | dout("mon ip matches existing sb %p\n", sb); | |
818 | } | |
6b805185 | 819 | if (args->sb_flags != other->mount_args->sb_flags) { |
16725b9d SW |
820 | dout("flags differ\n"); |
821 | return 0; | |
822 | } | |
823 | return 1; | |
824 | } | |
825 | ||
826 | /* | |
827 | * construct our own bdi so we can control readahead, etc. | |
828 | */ | |
859e7b14 | 829 | static int ceph_register_bdi(struct super_block *sb, struct ceph_client *client) |
16725b9d SW |
830 | { |
831 | int err; | |
832 | ||
f2cf418c | 833 | sb->s_bdi = &client->backing_dev_info; |
16725b9d SW |
834 | |
835 | /* set ra_pages based on rsize mount option? */ | |
6b805185 | 836 | if (client->mount_args->rsize >= PAGE_CACHE_SIZE) |
16725b9d | 837 | client->backing_dev_info.ra_pages = |
6b805185 | 838 | (client->mount_args->rsize + PAGE_CACHE_SIZE - 1) |
16725b9d | 839 | >> PAGE_SHIFT; |
16725b9d SW |
840 | err = bdi_register_dev(&client->backing_dev_info, sb->s_dev); |
841 | return err; | |
842 | } | |
843 | ||
844 | static int ceph_get_sb(struct file_system_type *fs_type, | |
845 | int flags, const char *dev_name, void *data, | |
846 | struct vfsmount *mnt) | |
847 | { | |
848 | struct super_block *sb; | |
849 | struct ceph_client *client; | |
850 | int err; | |
851 | int (*compare_super)(struct super_block *, void *) = ceph_compare_super; | |
6a18be16 | 852 | const char *path = NULL; |
6b805185 | 853 | struct ceph_mount_args *args; |
16725b9d SW |
854 | |
855 | dout("ceph_get_sb\n"); | |
6b805185 SW |
856 | args = parse_mount_args(flags, data, dev_name, &path); |
857 | if (IS_ERR(args)) { | |
858 | err = PTR_ERR(args); | |
859 | goto out_final; | |
860 | } | |
16725b9d SW |
861 | |
862 | /* create client (which we may/may not use) */ | |
6b805185 SW |
863 | client = ceph_create_client(args); |
864 | if (IS_ERR(client)) { | |
865 | err = PTR_ERR(client); | |
866 | goto out_final; | |
867 | } | |
16725b9d | 868 | |
6b805185 | 869 | if (client->mount_args->flags & CEPH_OPT_NOSHARE) |
16725b9d SW |
870 | compare_super = NULL; |
871 | sb = sget(fs_type, compare_super, ceph_set_super, client); | |
872 | if (IS_ERR(sb)) { | |
873 | err = PTR_ERR(sb); | |
874 | goto out; | |
875 | } | |
876 | ||
877 | if (ceph_client(sb) != client) { | |
878 | ceph_destroy_client(client); | |
879 | client = ceph_client(sb); | |
880 | dout("get_sb got existing client %p\n", client); | |
881 | } else { | |
882 | dout("get_sb using new client %p\n", client); | |
859e7b14 | 883 | err = ceph_register_bdi(sb, client); |
16725b9d SW |
884 | if (err < 0) |
885 | goto out_splat; | |
886 | } | |
887 | ||
888 | err = ceph_mount(client, mnt, path); | |
889 | if (err < 0) | |
890 | goto out_splat; | |
891 | dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root, | |
892 | mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode)); | |
893 | return 0; | |
894 | ||
895 | out_splat: | |
896 | ceph_mdsc_close_sessions(&client->mdsc); | |
897 | up_write(&sb->s_umount); | |
898 | deactivate_super(sb); | |
899 | goto out_final; | |
900 | ||
901 | out: | |
902 | ceph_destroy_client(client); | |
903 | out_final: | |
904 | dout("ceph_get_sb fail %d\n", err); | |
905 | return err; | |
906 | } | |
907 | ||
908 | static void ceph_kill_sb(struct super_block *s) | |
909 | { | |
910 | struct ceph_client *client = ceph_sb_to_client(s); | |
911 | dout("kill_sb %p\n", s); | |
912 | ceph_mdsc_pre_umount(&client->mdsc); | |
16725b9d | 913 | kill_anon_super(s); /* will call put_super after sb is r/o */ |
f2cf418c | 914 | bdi_unregister(&client->backing_dev_info); |
16725b9d SW |
915 | bdi_destroy(&client->backing_dev_info); |
916 | ceph_destroy_client(client); | |
917 | } | |
918 | ||
919 | static struct file_system_type ceph_fs_type = { | |
920 | .owner = THIS_MODULE, | |
921 | .name = "ceph", | |
922 | .get_sb = ceph_get_sb, | |
923 | .kill_sb = ceph_kill_sb, | |
924 | .fs_flags = FS_RENAME_DOES_D_MOVE, | |
925 | }; | |
926 | ||
927 | #define _STRINGIFY(x) #x | |
928 | #define STRINGIFY(x) _STRINGIFY(x) | |
929 | ||
930 | static int __init init_ceph(void) | |
931 | { | |
932 | int ret = 0; | |
933 | ||
934 | ret = ceph_debugfs_init(); | |
935 | if (ret < 0) | |
936 | goto out; | |
937 | ||
938 | ret = ceph_msgr_init(); | |
939 | if (ret < 0) | |
940 | goto out_debugfs; | |
941 | ||
942 | ret = init_caches(); | |
943 | if (ret) | |
944 | goto out_msgr; | |
945 | ||
946 | ceph_caps_init(); | |
947 | ||
948 | ret = register_filesystem(&ceph_fs_type); | |
949 | if (ret) | |
950 | goto out_icache; | |
951 | ||
fa0b72e9 SW |
952 | pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n", |
953 | CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH, | |
954 | CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL); | |
16725b9d SW |
955 | return 0; |
956 | ||
957 | out_icache: | |
958 | destroy_caches(); | |
959 | out_msgr: | |
960 | ceph_msgr_exit(); | |
961 | out_debugfs: | |
962 | ceph_debugfs_cleanup(); | |
963 | out: | |
964 | return ret; | |
965 | } | |
966 | ||
967 | static void __exit exit_ceph(void) | |
968 | { | |
969 | dout("exit_ceph\n"); | |
970 | unregister_filesystem(&ceph_fs_type); | |
971 | ceph_caps_finalize(); | |
972 | destroy_caches(); | |
973 | ceph_msgr_exit(); | |
974 | ceph_debugfs_cleanup(); | |
975 | } | |
976 | ||
977 | module_init(init_ceph); | |
978 | module_exit(exit_ceph); | |
979 | ||
980 | MODULE_AUTHOR("Sage Weil <[email protected]>"); | |
981 | MODULE_AUTHOR("Yehuda Sadeh <[email protected]>"); | |
982 | MODULE_AUTHOR("Patience Warnick <[email protected]>"); | |
983 | MODULE_DESCRIPTION("Ceph filesystem for Linux"); | |
984 | MODULE_LICENSE("GPL"); |