diff options
author | Sage Weil <sage@newdream.net> | 2009-10-06 14:31:07 -0400 |
---|---|---|
committer | Sage Weil <sage@newdream.net> | 2009-10-06 14:31:07 -0400 |
commit | 16725b9d2a2e3d0fd2b0034482e2eb0a2d78050f (patch) | |
tree | ced2f4d3dbe13c7a9c64510d6ec235b703191a5c /fs/ceph/super.c | |
parent | c30dbb9cc7fc75ab1d0ee6fb084ba4684f7a665d (diff) |
ceph: super.c
Mount option parsing, client setup and teardown, and a few odds and
ends (e.g., statfs).
Signed-off-by: Sage Weil <sage@newdream.net>
Diffstat (limited to 'fs/ceph/super.c')
-rw-r--r-- | fs/ceph/super.c | 936 |
1 files changed, 936 insertions, 0 deletions
diff --git a/fs/ceph/super.c b/fs/ceph/super.c new file mode 100644 index 00000000000..0723fb6e96a --- /dev/null +++ b/fs/ceph/super.c | |||
@@ -0,0 +1,936 @@ | |||
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 | |||
19 | #include "ceph_ver.h" | ||
20 | #include "decode.h" | ||
21 | #include "super.h" | ||
22 | #include "mon_client.h" | ||
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); | ||
101 | 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 | */ | ||
110 | static int ceph_show_options(struct seq_file *m, struct vfsmount *mnt) | ||
111 | { | ||
112 | struct ceph_client *client = ceph_sb_to_client(mnt->mnt_sb); | ||
113 | struct ceph_mount_args *args = &client->mount_args; | ||
114 | |||
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); | ||
131 | if (args->secret) | ||
132 | seq_puts(m, ",secret=<hidden>"); | ||
133 | return 0; | ||
134 | } | ||
135 | |||
136 | /* | ||
137 | * caches | ||
138 | */ | ||
139 | struct kmem_cache *ceph_inode_cachep; | ||
140 | struct kmem_cache *ceph_cap_cachep; | ||
141 | struct kmem_cache *ceph_dentry_cachep; | ||
142 | struct kmem_cache *ceph_file_cachep; | ||
143 | |||
144 | static void ceph_inode_init_once(void *foo) | ||
145 | { | ||
146 | struct ceph_inode_info *ci = foo; | ||
147 | inode_init_once(&ci->vfs_inode); | ||
148 | } | ||
149 | |||
150 | static int __init init_caches(void) | ||
151 | { | ||
152 | ceph_inode_cachep = kmem_cache_create("ceph_inode_info", | ||
153 | sizeof(struct ceph_inode_info), | ||
154 | __alignof__(struct ceph_inode_info), | ||
155 | (SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD), | ||
156 | ceph_inode_init_once); | ||
157 | if (ceph_inode_cachep == NULL) | ||
158 | return -ENOMEM; | ||
159 | |||
160 | ceph_cap_cachep = KMEM_CACHE(ceph_cap, | ||
161 | SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD); | ||
162 | if (ceph_cap_cachep == NULL) | ||
163 | goto bad_cap; | ||
164 | |||
165 | ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info, | ||
166 | SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD); | ||
167 | if (ceph_dentry_cachep == NULL) | ||
168 | goto bad_dentry; | ||
169 | |||
170 | ceph_file_cachep = KMEM_CACHE(ceph_file_info, | ||
171 | SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD); | ||
172 | if (ceph_file_cachep == NULL) | ||
173 | goto bad_file; | ||
174 | |||
175 | return 0; | ||
176 | |||
177 | bad_file: | ||
178 | kmem_cache_destroy(ceph_dentry_cachep); | ||
179 | bad_dentry: | ||
180 | kmem_cache_destroy(ceph_cap_cachep); | ||
181 | bad_cap: | ||
182 | kmem_cache_destroy(ceph_inode_cachep); | ||
183 | return -ENOMEM; | ||
184 | } | ||
185 | |||
186 | static void destroy_caches(void) | ||
187 | { | ||
188 | kmem_cache_destroy(ceph_inode_cachep); | ||
189 | kmem_cache_destroy(ceph_cap_cachep); | ||
190 | kmem_cache_destroy(ceph_dentry_cachep); | ||
191 | kmem_cache_destroy(ceph_file_cachep); | ||
192 | } | ||
193 | |||
194 | |||
195 | /* | ||
196 | * ceph_umount_begin - initiate forced umount. Tear down down the | ||
197 | * mount, skipping steps that may hang while waiting for server(s). | ||
198 | */ | ||
199 | static void ceph_umount_begin(struct super_block *sb) | ||
200 | { | ||
201 | struct ceph_client *client = ceph_sb_to_client(sb); | ||
202 | |||
203 | dout("ceph_umount_begin - starting forced umount\n"); | ||
204 | if (!client) | ||
205 | return; | ||
206 | client->mount_state = CEPH_MOUNT_SHUTDOWN; | ||
207 | return; | ||
208 | } | ||
209 | |||
210 | static const struct super_operations ceph_super_ops = { | ||
211 | .alloc_inode = ceph_alloc_inode, | ||
212 | .destroy_inode = ceph_destroy_inode, | ||
213 | .write_inode = ceph_write_inode, | ||
214 | .sync_fs = ceph_syncfs, | ||
215 | .put_super = ceph_put_super, | ||
216 | .show_options = ceph_show_options, | ||
217 | .statfs = ceph_statfs, | ||
218 | .umount_begin = ceph_umount_begin, | ||
219 | }; | ||
220 | |||
221 | |||
222 | const char *ceph_msg_type_name(int type) | ||
223 | { | ||
224 | switch (type) { | ||
225 | case CEPH_MSG_SHUTDOWN: return "shutdown"; | ||
226 | case CEPH_MSG_PING: return "ping"; | ||
227 | case CEPH_MSG_MON_MAP: return "mon_map"; | ||
228 | case CEPH_MSG_MON_GET_MAP: return "mon_get_map"; | ||
229 | case CEPH_MSG_MON_SUBSCRIBE: return "mon_subscribe"; | ||
230 | case CEPH_MSG_MON_SUBSCRIBE_ACK: return "mon_subscribe_ack"; | ||
231 | case CEPH_MSG_CLIENT_MOUNT: return "client_mount"; | ||
232 | case CEPH_MSG_CLIENT_MOUNT_ACK: return "client_mount_ack"; | ||
233 | case CEPH_MSG_STATFS: return "statfs"; | ||
234 | case CEPH_MSG_STATFS_REPLY: return "statfs_reply"; | ||
235 | case CEPH_MSG_MDS_GETMAP: return "mds_getmap"; | ||
236 | case CEPH_MSG_MDS_MAP: return "mds_map"; | ||
237 | case CEPH_MSG_CLIENT_SESSION: return "client_session"; | ||
238 | case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect"; | ||
239 | case CEPH_MSG_CLIENT_REQUEST: return "client_request"; | ||
240 | case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward"; | ||
241 | case CEPH_MSG_CLIENT_REPLY: return "client_reply"; | ||
242 | case CEPH_MSG_CLIENT_CAPS: return "client_caps"; | ||
243 | case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release"; | ||
244 | case CEPH_MSG_CLIENT_SNAP: return "client_snap"; | ||
245 | case CEPH_MSG_CLIENT_LEASE: return "client_lease"; | ||
246 | case CEPH_MSG_OSD_GETMAP: return "osd_getmap"; | ||
247 | 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 | */ | ||
258 | enum { | ||
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, | ||
269 | /* int args above */ | ||
270 | Opt_snapdirname, | ||
271 | Opt_secret, | ||
272 | /* string args above */ | ||
273 | Opt_ip, | ||
274 | Opt_noshare, | ||
275 | Opt_dirstat, | ||
276 | Opt_nodirstat, | ||
277 | Opt_rbytes, | ||
278 | Opt_norbytes, | ||
279 | Opt_nocrc, | ||
280 | Opt_noasyncreaddir, | ||
281 | }; | ||
282 | |||
283 | static match_table_t arg_tokens = { | ||
284 | {Opt_fsidmajor, "fsidmajor=%ld"}, | ||
285 | {Opt_fsidminor, "fsidminor=%ld"}, | ||
286 | {Opt_monport, "monport=%d"}, | ||
287 | {Opt_wsize, "wsize=%d"}, | ||
288 | {Opt_rsize, "rsize=%d"}, | ||
289 | {Opt_osdtimeout, "osdtimeout=%d"}, | ||
290 | {Opt_mount_timeout, "mount_timeout=%d"}, | ||
291 | {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"}, | ||
292 | {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"}, | ||
293 | {Opt_readdir_max_entries, "readdir_max_entries=%d"}, | ||
294 | /* int args above */ | ||
295 | {Opt_snapdirname, "snapdirname=%s"}, | ||
296 | {Opt_secret, "secret=%s"}, | ||
297 | /* string args above */ | ||
298 | {Opt_ip, "ip=%s"}, | ||
299 | {Opt_noshare, "noshare"}, | ||
300 | {Opt_dirstat, "dirstat"}, | ||
301 | {Opt_nodirstat, "nodirstat"}, | ||
302 | {Opt_rbytes, "rbytes"}, | ||
303 | {Opt_norbytes, "norbytes"}, | ||
304 | {Opt_nocrc, "nocrc"}, | ||
305 | {Opt_noasyncreaddir, "noasyncreaddir"}, | ||
306 | {-1, NULL} | ||
307 | }; | ||
308 | |||
309 | |||
310 | static int parse_mount_args(struct ceph_client *client, | ||
311 | int flags, char *options, const char *dev_name, | ||
312 | const char **path) | ||
313 | { | ||
314 | struct ceph_mount_args *args = &client->mount_args; | ||
315 | const char *c; | ||
316 | int err; | ||
317 | substring_t argstr[MAX_OPT_ARGS]; | ||
318 | int num_mon; | ||
319 | struct ceph_entity_addr mon_addr[CEPH_MAX_MON_MOUNT_ADDR]; | ||
320 | int i; | ||
321 | |||
322 | dout("parse_mount_args dev_name '%s'\n", dev_name); | ||
323 | memset(args, 0, sizeof(*args)); | ||
324 | |||
325 | /* start with defaults */ | ||
326 | args->sb_flags = flags; | ||
327 | args->flags = CEPH_OPT_DEFAULT; | ||
328 | args->osd_timeout = 5; /* seconds */ | ||
329 | args->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT; /* seconds */ | ||
330 | args->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT; | ||
331 | args->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT; | ||
332 | args->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL); | ||
333 | args->cap_release_safety = CEPH_CAPS_PER_RELEASE * 4; | ||
334 | args->max_readdir = 1024; | ||
335 | |||
336 | /* ip1[:port1][,ip2[:port2]...]:/subdir/in/fs */ | ||
337 | if (!dev_name) | ||
338 | return -EINVAL; | ||
339 | *path = strstr(dev_name, ":/"); | ||
340 | if (*path == NULL) { | ||
341 | pr_err("device name is missing path (no :/ in %s)\n", | ||
342 | dev_name); | ||
343 | return -EINVAL; | ||
344 | } | ||
345 | |||
346 | /* get mon ip(s) */ | ||
347 | err = ceph_parse_ips(dev_name, *path, mon_addr, | ||
348 | CEPH_MAX_MON_MOUNT_ADDR, &num_mon); | ||
349 | if (err < 0) | ||
350 | return err; | ||
351 | |||
352 | /* build initial monmap */ | ||
353 | client->monc.monmap = kzalloc(sizeof(*client->monc.monmap) + | ||
354 | num_mon*sizeof(client->monc.monmap->mon_inst[0]), | ||
355 | GFP_KERNEL); | ||
356 | if (!client->monc.monmap) | ||
357 | return -ENOMEM; | ||
358 | for (i = 0; i < num_mon; i++) { | ||
359 | client->monc.monmap->mon_inst[i].addr = mon_addr[i]; | ||
360 | client->monc.monmap->mon_inst[i].addr.erank = 0; | ||
361 | client->monc.monmap->mon_inst[i].addr.nonce = 0; | ||
362 | client->monc.monmap->mon_inst[i].name.type = | ||
363 | CEPH_ENTITY_TYPE_MON; | ||
364 | client->monc.monmap->mon_inst[i].name.num = cpu_to_le64(i); | ||
365 | } | ||
366 | client->monc.monmap->num_mon = num_mon; | ||
367 | memset(&args->my_addr.in_addr, 0, sizeof(args->my_addr.in_addr)); | ||
368 | |||
369 | /* path on server */ | ||
370 | *path += 2; | ||
371 | dout("server path '%s'\n", *path); | ||
372 | |||
373 | /* parse mount options */ | ||
374 | while ((c = strsep(&options, ",")) != NULL) { | ||
375 | int token, intval, ret; | ||
376 | if (!*c) | ||
377 | continue; | ||
378 | token = match_token((char *)c, arg_tokens, argstr); | ||
379 | if (token < 0) { | ||
380 | pr_err("bad mount option at '%s'\n", c); | ||
381 | return -EINVAL; | ||
382 | |||
383 | } | ||
384 | if (token < Opt_ip) { | ||
385 | ret = match_int(&argstr[0], &intval); | ||
386 | if (ret < 0) { | ||
387 | pr_err("bad mount option arg (not int) " | ||
388 | "at '%s'\n", c); | ||
389 | continue; | ||
390 | } | ||
391 | dout("got token %d intval %d\n", token, intval); | ||
392 | } | ||
393 | switch (token) { | ||
394 | case Opt_fsidmajor: | ||
395 | *(__le64 *)&args->fsid.fsid[0] = cpu_to_le64(intval); | ||
396 | break; | ||
397 | case Opt_fsidminor: | ||
398 | *(__le64 *)&args->fsid.fsid[8] = cpu_to_le64(intval); | ||
399 | break; | ||
400 | case Opt_ip: | ||
401 | err = ceph_parse_ips(argstr[0].from, | ||
402 | argstr[0].to, | ||
403 | &args->my_addr, | ||
404 | 1, NULL); | ||
405 | if (err < 0) | ||
406 | return err; | ||
407 | args->flags |= CEPH_OPT_MYIP; | ||
408 | break; | ||
409 | |||
410 | case Opt_snapdirname: | ||
411 | kfree(args->snapdir_name); | ||
412 | args->snapdir_name = kstrndup(argstr[0].from, | ||
413 | argstr[0].to-argstr[0].from, | ||
414 | GFP_KERNEL); | ||
415 | break; | ||
416 | case Opt_secret: | ||
417 | args->secret = kstrndup(argstr[0].from, | ||
418 | argstr[0].to-argstr[0].from, | ||
419 | GFP_KERNEL); | ||
420 | break; | ||
421 | |||
422 | /* misc */ | ||
423 | case Opt_wsize: | ||
424 | args->wsize = intval; | ||
425 | break; | ||
426 | case Opt_rsize: | ||
427 | args->rsize = intval; | ||
428 | break; | ||
429 | case Opt_osdtimeout: | ||
430 | args->osd_timeout = intval; | ||
431 | break; | ||
432 | case Opt_mount_timeout: | ||
433 | args->mount_timeout = intval; | ||
434 | break; | ||
435 | case Opt_caps_wanted_delay_min: | ||
436 | args->caps_wanted_delay_min = intval; | ||
437 | break; | ||
438 | case Opt_caps_wanted_delay_max: | ||
439 | args->caps_wanted_delay_max = intval; | ||
440 | break; | ||
441 | case Opt_readdir_max_entries: | ||
442 | args->max_readdir = intval; | ||
443 | break; | ||
444 | |||
445 | case Opt_noshare: | ||
446 | args->flags |= CEPH_OPT_NOSHARE; | ||
447 | break; | ||
448 | |||
449 | case Opt_dirstat: | ||
450 | args->flags |= CEPH_OPT_DIRSTAT; | ||
451 | break; | ||
452 | case Opt_nodirstat: | ||
453 | args->flags &= ~CEPH_OPT_DIRSTAT; | ||
454 | break; | ||
455 | case Opt_rbytes: | ||
456 | args->flags |= CEPH_OPT_RBYTES; | ||
457 | break; | ||
458 | case Opt_norbytes: | ||
459 | args->flags &= ~CEPH_OPT_RBYTES; | ||
460 | break; | ||
461 | case Opt_nocrc: | ||
462 | args->flags |= CEPH_OPT_NOCRC; | ||
463 | break; | ||
464 | case Opt_noasyncreaddir: | ||
465 | args->flags |= CEPH_OPT_NOASYNCREADDIR; | ||
466 | break; | ||
467 | |||
468 | default: | ||
469 | BUG_ON(token); | ||
470 | } | ||
471 | } | ||
472 | |||
473 | return 0; | ||
474 | } | ||
475 | |||
476 | static void release_mount_args(struct ceph_mount_args *args) | ||
477 | { | ||
478 | kfree(args->snapdir_name); | ||
479 | args->snapdir_name = NULL; | ||
480 | kfree(args->secret); | ||
481 | args->secret = NULL; | ||
482 | } | ||
483 | |||
484 | /* | ||
485 | * create a fresh client instance | ||
486 | */ | ||
487 | static struct ceph_client *ceph_create_client(void) | ||
488 | { | ||
489 | struct ceph_client *client; | ||
490 | int err = -ENOMEM; | ||
491 | |||
492 | client = kzalloc(sizeof(*client), GFP_KERNEL); | ||
493 | if (client == NULL) | ||
494 | return ERR_PTR(-ENOMEM); | ||
495 | |||
496 | mutex_init(&client->mount_mutex); | ||
497 | |||
498 | init_waitqueue_head(&client->mount_wq); | ||
499 | |||
500 | client->sb = NULL; | ||
501 | client->mount_state = CEPH_MOUNT_MOUNTING; | ||
502 | client->whoami = -1; | ||
503 | |||
504 | client->msgr = NULL; | ||
505 | |||
506 | client->mount_err = 0; | ||
507 | client->signed_ticket = NULL; | ||
508 | client->signed_ticket_len = 0; | ||
509 | |||
510 | err = -ENOMEM; | ||
511 | client->wb_wq = create_workqueue("ceph-writeback"); | ||
512 | if (client->wb_wq == NULL) | ||
513 | goto fail; | ||
514 | client->pg_inv_wq = create_singlethread_workqueue("ceph-pg-invalid"); | ||
515 | if (client->pg_inv_wq == NULL) | ||
516 | goto fail_wb_wq; | ||
517 | client->trunc_wq = create_singlethread_workqueue("ceph-trunc"); | ||
518 | if (client->trunc_wq == NULL) | ||
519 | goto fail_pg_inv_wq; | ||
520 | |||
521 | /* subsystems */ | ||
522 | err = ceph_monc_init(&client->monc, client); | ||
523 | if (err < 0) | ||
524 | goto fail_trunc_wq; | ||
525 | err = ceph_osdc_init(&client->osdc, client); | ||
526 | if (err < 0) | ||
527 | goto fail_monc; | ||
528 | ceph_mdsc_init(&client->mdsc, client); | ||
529 | return client; | ||
530 | |||
531 | fail_monc: | ||
532 | ceph_monc_stop(&client->monc); | ||
533 | fail_trunc_wq: | ||
534 | destroy_workqueue(client->trunc_wq); | ||
535 | fail_pg_inv_wq: | ||
536 | destroy_workqueue(client->pg_inv_wq); | ||
537 | fail_wb_wq: | ||
538 | destroy_workqueue(client->wb_wq); | ||
539 | fail: | ||
540 | kfree(client); | ||
541 | return ERR_PTR(err); | ||
542 | } | ||
543 | |||
544 | static void ceph_destroy_client(struct ceph_client *client) | ||
545 | { | ||
546 | dout("destroy_client %p\n", client); | ||
547 | |||
548 | /* unmount */ | ||
549 | ceph_mdsc_stop(&client->mdsc); | ||
550 | ceph_monc_stop(&client->monc); | ||
551 | ceph_osdc_stop(&client->osdc); | ||
552 | |||
553 | kfree(client->signed_ticket); | ||
554 | |||
555 | ceph_debugfs_client_cleanup(client); | ||
556 | destroy_workqueue(client->wb_wq); | ||
557 | destroy_workqueue(client->pg_inv_wq); | ||
558 | destroy_workqueue(client->trunc_wq); | ||
559 | |||
560 | if (client->msgr) | ||
561 | ceph_messenger_destroy(client->msgr); | ||
562 | if (client->wb_pagevec_pool) | ||
563 | mempool_destroy(client->wb_pagevec_pool); | ||
564 | |||
565 | release_mount_args(&client->mount_args); | ||
566 | |||
567 | kfree(client); | ||
568 | dout("destroy_client %p done\n", client); | ||
569 | } | ||
570 | |||
571 | /* | ||
572 | * true if we have the mon map (and have thus joined the cluster) | ||
573 | */ | ||
574 | static int have_mon_map(struct ceph_client *client) | ||
575 | { | ||
576 | return client->monc.monmap && client->monc.monmap->epoch; | ||
577 | } | ||
578 | |||
579 | /* | ||
580 | * Bootstrap mount by opening the root directory. Note the mount | ||
581 | * @started time from caller, and time out if this takes too long. | ||
582 | */ | ||
583 | static struct dentry *open_root_dentry(struct ceph_client *client, | ||
584 | const char *path, | ||
585 | unsigned long started) | ||
586 | { | ||
587 | struct ceph_mds_client *mdsc = &client->mdsc; | ||
588 | struct ceph_mds_request *req = NULL; | ||
589 | int err; | ||
590 | struct dentry *root; | ||
591 | |||
592 | /* open dir */ | ||
593 | dout("open_root_inode opening '%s'\n", path); | ||
594 | req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS); | ||
595 | if (IS_ERR(req)) | ||
596 | return ERR_PTR(PTR_ERR(req)); | ||
597 | req->r_path1 = kstrdup(path, GFP_NOFS); | ||
598 | req->r_ino1.ino = CEPH_INO_ROOT; | ||
599 | req->r_ino1.snap = CEPH_NOSNAP; | ||
600 | req->r_started = started; | ||
601 | req->r_timeout = client->mount_args.mount_timeout * HZ; | ||
602 | req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE); | ||
603 | req->r_num_caps = 2; | ||
604 | err = ceph_mdsc_do_request(mdsc, NULL, req); | ||
605 | if (err == 0) { | ||
606 | dout("open_root_inode success\n"); | ||
607 | if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT && | ||
608 | client->sb->s_root == NULL) | ||
609 | root = d_alloc_root(req->r_target_inode); | ||
610 | else | ||
611 | root = d_obtain_alias(req->r_target_inode); | ||
612 | req->r_target_inode = NULL; | ||
613 | dout("open_root_inode success, root dentry is %p\n", root); | ||
614 | } else { | ||
615 | root = ERR_PTR(err); | ||
616 | } | ||
617 | ceph_mdsc_put_request(req); | ||
618 | return root; | ||
619 | } | ||
620 | |||
621 | /* | ||
622 | * mount: join the ceph cluster, and open root directory. | ||
623 | */ | ||
624 | static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt, | ||
625 | const char *path) | ||
626 | { | ||
627 | struct ceph_entity_addr *myaddr = NULL; | ||
628 | int err; | ||
629 | unsigned long timeout = client->mount_args.mount_timeout * HZ; | ||
630 | unsigned long started = jiffies; /* note the start time */ | ||
631 | struct dentry *root; | ||
632 | |||
633 | dout("mount start\n"); | ||
634 | mutex_lock(&client->mount_mutex); | ||
635 | |||
636 | /* initialize the messenger */ | ||
637 | if (client->msgr == NULL) { | ||
638 | if (ceph_test_opt(client, MYIP)) | ||
639 | myaddr = &client->mount_args.my_addr; | ||
640 | client->msgr = ceph_messenger_create(myaddr); | ||
641 | if (IS_ERR(client->msgr)) { | ||
642 | err = PTR_ERR(client->msgr); | ||
643 | client->msgr = NULL; | ||
644 | goto out; | ||
645 | } | ||
646 | client->msgr->nocrc = ceph_test_opt(client, NOCRC); | ||
647 | } | ||
648 | |||
649 | /* send mount request, and wait for mon, mds, and osd maps */ | ||
650 | err = ceph_monc_request_mount(&client->monc); | ||
651 | if (err < 0) | ||
652 | goto out; | ||
653 | |||
654 | while (!have_mon_map(client) && !client->mount_err) { | ||
655 | err = -EIO; | ||
656 | if (timeout && time_after_eq(jiffies, started + timeout)) | ||
657 | goto out; | ||
658 | |||
659 | /* wait */ | ||
660 | dout("mount waiting for mount\n"); | ||
661 | err = wait_event_interruptible_timeout(client->mount_wq, | ||
662 | client->mount_err || have_mon_map(client), | ||
663 | timeout); | ||
664 | if (err == -EINTR || err == -ERESTARTSYS) | ||
665 | goto out; | ||
666 | if (client->mount_err) { | ||
667 | err = client->mount_err; | ||
668 | goto out; | ||
669 | } | ||
670 | } | ||
671 | |||
672 | dout("mount opening root\n"); | ||
673 | root = open_root_dentry(client, "", started); | ||
674 | if (IS_ERR(root)) { | ||
675 | err = PTR_ERR(root); | ||
676 | goto out; | ||
677 | } | ||
678 | if (client->sb->s_root) | ||
679 | dput(root); | ||
680 | else | ||
681 | client->sb->s_root = root; | ||
682 | |||
683 | if (path[0] == 0) { | ||
684 | dget(root); | ||
685 | } else { | ||
686 | dout("mount opening base mountpoint\n"); | ||
687 | root = open_root_dentry(client, path, started); | ||
688 | if (IS_ERR(root)) { | ||
689 | err = PTR_ERR(root); | ||
690 | dput(client->sb->s_root); | ||
691 | client->sb->s_root = NULL; | ||
692 | goto out; | ||
693 | } | ||
694 | } | ||
695 | |||
696 | mnt->mnt_root = root; | ||
697 | mnt->mnt_sb = client->sb; | ||
698 | |||
699 | client->mount_state = CEPH_MOUNT_MOUNTED; | ||
700 | dout("mount success\n"); | ||
701 | err = 0; | ||
702 | |||
703 | out: | ||
704 | mutex_unlock(&client->mount_mutex); | ||
705 | return err; | ||
706 | } | ||
707 | |||
708 | static int ceph_set_super(struct super_block *s, void *data) | ||
709 | { | ||
710 | struct ceph_client *client = data; | ||
711 | int ret; | ||
712 | |||
713 | dout("set_super %p data %p\n", s, data); | ||
714 | |||
715 | s->s_flags = client->mount_args.sb_flags; | ||
716 | s->s_maxbytes = 1ULL << 40; /* temp value until we get mdsmap */ | ||
717 | |||
718 | s->s_fs_info = client; | ||
719 | client->sb = s; | ||
720 | |||
721 | s->s_op = &ceph_super_ops; | ||
722 | s->s_export_op = &ceph_export_ops; | ||
723 | |||
724 | s->s_time_gran = 1000; /* 1000 ns == 1 us */ | ||
725 | |||
726 | ret = set_anon_super(s, NULL); /* what is that second arg for? */ | ||
727 | if (ret != 0) | ||
728 | goto fail; | ||
729 | |||
730 | return ret; | ||
731 | |||
732 | fail: | ||
733 | s->s_fs_info = NULL; | ||
734 | client->sb = NULL; | ||
735 | return ret; | ||
736 | } | ||
737 | |||
738 | /* | ||
739 | * share superblock if same fs AND options | ||
740 | */ | ||
741 | static int ceph_compare_super(struct super_block *sb, void *data) | ||
742 | { | ||
743 | struct ceph_client *new = data; | ||
744 | struct ceph_mount_args *args = &new->mount_args; | ||
745 | struct ceph_client *other = ceph_sb_to_client(sb); | ||
746 | int i; | ||
747 | |||
748 | dout("ceph_compare_super %p\n", sb); | ||
749 | if (args->flags & CEPH_OPT_FSID) { | ||
750 | if (ceph_fsid_compare(&args->fsid, &other->fsid)) { | ||
751 | dout("fsid doesn't match\n"); | ||
752 | return 0; | ||
753 | } | ||
754 | } else { | ||
755 | /* do we share (a) monitor? */ | ||
756 | for (i = 0; i < new->monc.monmap->num_mon; i++) | ||
757 | if (ceph_monmap_contains(other->monc.monmap, | ||
758 | &new->monc.monmap->mon_inst[i].addr)) | ||
759 | break; | ||
760 | if (i == new->monc.monmap->num_mon) { | ||
761 | dout("mon ip not part of monmap\n"); | ||
762 | return 0; | ||
763 | } | ||
764 | dout("mon ip matches existing sb %p\n", sb); | ||
765 | } | ||
766 | if (args->sb_flags != other->mount_args.sb_flags) { | ||
767 | dout("flags differ\n"); | ||
768 | return 0; | ||
769 | } | ||
770 | return 1; | ||
771 | } | ||
772 | |||
773 | /* | ||
774 | * construct our own bdi so we can control readahead, etc. | ||
775 | */ | ||
776 | static int ceph_init_bdi(struct super_block *sb, struct ceph_client *client) | ||
777 | { | ||
778 | int err; | ||
779 | |||
780 | err = bdi_init(&client->backing_dev_info); | ||
781 | if (err < 0) | ||
782 | return err; | ||
783 | |||
784 | /* set ra_pages based on rsize mount option? */ | ||
785 | if (client->mount_args.rsize >= PAGE_CACHE_SIZE) | ||
786 | client->backing_dev_info.ra_pages = | ||
787 | (client->mount_args.rsize + PAGE_CACHE_SIZE - 1) | ||
788 | >> PAGE_SHIFT; | ||
789 | |||
790 | err = bdi_register_dev(&client->backing_dev_info, sb->s_dev); | ||
791 | return err; | ||
792 | } | ||
793 | |||
794 | static int ceph_get_sb(struct file_system_type *fs_type, | ||
795 | int flags, const char *dev_name, void *data, | ||
796 | struct vfsmount *mnt) | ||
797 | { | ||
798 | struct super_block *sb; | ||
799 | struct ceph_client *client; | ||
800 | int err; | ||
801 | int (*compare_super)(struct super_block *, void *) = ceph_compare_super; | ||
802 | const char *path; | ||
803 | |||
804 | dout("ceph_get_sb\n"); | ||
805 | |||
806 | /* create client (which we may/may not use) */ | ||
807 | client = ceph_create_client(); | ||
808 | if (IS_ERR(client)) | ||
809 | return PTR_ERR(client); | ||
810 | |||
811 | err = parse_mount_args(client, flags, data, dev_name, &path); | ||
812 | if (err < 0) | ||
813 | goto out; | ||
814 | |||
815 | if (client->mount_args.flags & CEPH_OPT_NOSHARE) | ||
816 | compare_super = NULL; | ||
817 | sb = sget(fs_type, compare_super, ceph_set_super, client); | ||
818 | if (IS_ERR(sb)) { | ||
819 | err = PTR_ERR(sb); | ||
820 | goto out; | ||
821 | } | ||
822 | |||
823 | if (ceph_client(sb) != client) { | ||
824 | ceph_destroy_client(client); | ||
825 | client = ceph_client(sb); | ||
826 | dout("get_sb got existing client %p\n", client); | ||
827 | } else { | ||
828 | dout("get_sb using new client %p\n", client); | ||
829 | |||
830 | /* set up mempools */ | ||
831 | err = -ENOMEM; | ||
832 | client->wb_pagevec_pool = mempool_create_kmalloc_pool(10, | ||
833 | client->mount_args.wsize >> PAGE_CACHE_SHIFT); | ||
834 | if (!client->wb_pagevec_pool) | ||
835 | goto out_splat; | ||
836 | |||
837 | err = ceph_init_bdi(sb, client); | ||
838 | if (err < 0) | ||
839 | goto out_splat; | ||
840 | } | ||
841 | |||
842 | err = ceph_mount(client, mnt, path); | ||
843 | if (err < 0) | ||
844 | goto out_splat; | ||
845 | dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root, | ||
846 | mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode)); | ||
847 | return 0; | ||
848 | |||
849 | out_splat: | ||
850 | ceph_mdsc_close_sessions(&client->mdsc); | ||
851 | up_write(&sb->s_umount); | ||
852 | deactivate_super(sb); | ||
853 | goto out_final; | ||
854 | |||
855 | out: | ||
856 | ceph_destroy_client(client); | ||
857 | out_final: | ||
858 | dout("ceph_get_sb fail %d\n", err); | ||
859 | return err; | ||
860 | } | ||
861 | |||
862 | static void ceph_kill_sb(struct super_block *s) | ||
863 | { | ||
864 | struct ceph_client *client = ceph_sb_to_client(s); | ||
865 | dout("kill_sb %p\n", s); | ||
866 | ceph_mdsc_pre_umount(&client->mdsc); | ||
867 | bdi_unregister(&client->backing_dev_info); | ||
868 | kill_anon_super(s); /* will call put_super after sb is r/o */ | ||
869 | bdi_destroy(&client->backing_dev_info); | ||
870 | ceph_destroy_client(client); | ||
871 | } | ||
872 | |||
873 | static struct file_system_type ceph_fs_type = { | ||
874 | .owner = THIS_MODULE, | ||
875 | .name = "ceph", | ||
876 | .get_sb = ceph_get_sb, | ||
877 | .kill_sb = ceph_kill_sb, | ||
878 | .fs_flags = FS_RENAME_DOES_D_MOVE, | ||
879 | }; | ||
880 | |||
881 | #define _STRINGIFY(x) #x | ||
882 | #define STRINGIFY(x) _STRINGIFY(x) | ||
883 | |||
884 | static int __init init_ceph(void) | ||
885 | { | ||
886 | int ret = 0; | ||
887 | |||
888 | ret = ceph_debugfs_init(); | ||
889 | if (ret < 0) | ||
890 | goto out; | ||
891 | |||
892 | ret = ceph_msgr_init(); | ||
893 | if (ret < 0) | ||
894 | goto out_debugfs; | ||
895 | |||
896 | ret = init_caches(); | ||
897 | if (ret) | ||
898 | goto out_msgr; | ||
899 | |||
900 | ceph_caps_init(); | ||
901 | |||
902 | ret = register_filesystem(&ceph_fs_type); | ||
903 | if (ret) | ||
904 | goto out_icache; | ||
905 | |||
906 | pr_info("loaded (%s)\n", STRINGIFY(CEPH_GIT_VER)); | ||
907 | return 0; | ||
908 | |||
909 | out_icache: | ||
910 | destroy_caches(); | ||
911 | out_msgr: | ||
912 | ceph_msgr_exit(); | ||
913 | out_debugfs: | ||
914 | ceph_debugfs_cleanup(); | ||
915 | out: | ||
916 | return ret; | ||
917 | } | ||
918 | |||
919 | static void __exit exit_ceph(void) | ||
920 | { | ||
921 | dout("exit_ceph\n"); | ||
922 | unregister_filesystem(&ceph_fs_type); | ||
923 | ceph_caps_finalize(); | ||
924 | destroy_caches(); | ||
925 | ceph_msgr_exit(); | ||
926 | ceph_debugfs_cleanup(); | ||
927 | } | ||
928 | |||
929 | module_init(init_ceph); | ||
930 | module_exit(exit_ceph); | ||
931 | |||
932 | MODULE_AUTHOR("Sage Weil <sage@newdream.net>"); | ||
933 | MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>"); | ||
934 | MODULE_AUTHOR("Patience Warnick <patience@newdream.net>"); | ||
935 | MODULE_DESCRIPTION("Ceph filesystem for Linux"); | ||
936 | MODULE_LICENSE("GPL"); | ||