ceph: allocate and parse mount args before client instance
[linux-3.10.git] / fs / ceph / super.c
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 "decode.h"
20 #include "super.h"
21 #include "mon_client.h"
22
23 /*
24  * Ceph superblock operations
25  *
26  * Handle the basics of mounting, unmounting.
27  */
28
29
30 /*
31  * find filename portion of a path (/foo/bar/baz -> baz)
32  */
33 const char *ceph_file_part(const char *s, int len)
34 {
35         const char *e = s + len;
36
37         while (e != s && *(e-1) != '/')
38                 e--;
39         return e;
40 }
41
42
43 /*
44  * super ops
45  */
46 static void ceph_put_super(struct super_block *s)
47 {
48         struct ceph_client *cl = ceph_client(s);
49
50         dout("put_super\n");
51         ceph_mdsc_close_sessions(&cl->mdsc);
52         return;
53 }
54
55 static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
56 {
57         struct ceph_client *client = ceph_inode_to_client(dentry->d_inode);
58         struct ceph_monmap *monmap = client->monc.monmap;
59         struct ceph_statfs st;
60         u64 fsid;
61         int err;
62
63         dout("statfs\n");
64         err = ceph_monc_do_statfs(&client->monc, &st);
65         if (err < 0)
66                 return err;
67
68         /* fill in kstatfs */
69         buf->f_type = CEPH_SUPER_MAGIC;  /* ?? */
70
71         /*
72          * express utilization in terms of large blocks to avoid
73          * overflow on 32-bit machines.
74          */
75         buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
76         buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
77         buf->f_bfree = (le64_to_cpu(st.kb) - le64_to_cpu(st.kb_used)) >>
78                 (CEPH_BLOCK_SHIFT-10);
79         buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
80
81         buf->f_files = le64_to_cpu(st.num_objects);
82         buf->f_ffree = -1;
83         buf->f_namelen = PATH_MAX;
84         buf->f_frsize = PAGE_CACHE_SIZE;
85
86         /* leave fsid little-endian, regardless of host endianness */
87         fsid = *(u64 *)(&monmap->fsid) ^ *((u64 *)&monmap->fsid + 1);
88         buf->f_fsid.val[0] = fsid & 0xffffffff;
89         buf->f_fsid.val[1] = fsid >> 32;
90
91         return 0;
92 }
93
94
95 static int ceph_syncfs(struct super_block *sb, int wait)
96 {
97         dout("sync_fs %d\n", wait);
98         ceph_osdc_sync(&ceph_client(sb)->osdc);
99         ceph_mdsc_sync(&ceph_client(sb)->mdsc);
100         dout("sync_fs %d done\n", wait);
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_MAP: return "mds_map";
236         case CEPH_MSG_CLIENT_SESSION: return "client_session";
237         case CEPH_MSG_CLIENT_RECONNECT: return "client_reconnect";
238         case CEPH_MSG_CLIENT_REQUEST: return "client_request";
239         case CEPH_MSG_CLIENT_REQUEST_FORWARD: return "client_request_forward";
240         case CEPH_MSG_CLIENT_REPLY: return "client_reply";
241         case CEPH_MSG_CLIENT_CAPS: return "client_caps";
242         case CEPH_MSG_CLIENT_CAPRELEASE: return "client_cap_release";
243         case CEPH_MSG_CLIENT_SNAP: return "client_snap";
244         case CEPH_MSG_CLIENT_LEASE: return "client_lease";
245         case CEPH_MSG_OSD_MAP: return "osd_map";
246         case CEPH_MSG_OSD_OP: return "osd_op";
247         case CEPH_MSG_OSD_OPREPLY: return "osd_opreply";
248         default: return "unknown";
249         }
250 }
251
252
253 /*
254  * mount options
255  */
256 enum {
257         Opt_fsidmajor,
258         Opt_fsidminor,
259         Opt_monport,
260         Opt_wsize,
261         Opt_rsize,
262         Opt_osdtimeout,
263         Opt_mount_timeout,
264         Opt_caps_wanted_delay_min,
265         Opt_caps_wanted_delay_max,
266         Opt_readdir_max_entries,
267         Opt_last_int,
268         /* int args above */
269         Opt_snapdirname,
270         Opt_secret,
271         Opt_last_string,
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 struct ceph_mount_args *parse_mount_args(int flags, char *options,
311                                                 const char *dev_name,
312                                                 const char **path)
313 {
314         struct ceph_mount_args *args;
315         const char *c;
316         int err = -ENOMEM;
317         substring_t argstr[MAX_OPT_ARGS];
318
319         args = kzalloc(sizeof(*args), GFP_KERNEL);
320         if (!args)
321                 return ERR_PTR(-ENOMEM);
322         args->mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*args->mon_addr),
323                                  GFP_KERNEL);
324         if (!args->mon_addr)
325                 goto out;
326
327         dout("parse_mount_args %p, dev_name '%s'\n", args, dev_name);
328
329         /* start with defaults */
330         args->sb_flags = flags;
331         args->flags = CEPH_OPT_DEFAULT;
332         args->osd_timeout = 5;    /* seconds */
333         args->mount_timeout = CEPH_MOUNT_TIMEOUT_DEFAULT; /* seconds */
334         args->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
335         args->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
336         args->rsize = CEPH_MOUNT_RSIZE_DEFAULT;
337         args->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
338         args->cap_release_safety = CEPH_CAPS_PER_RELEASE * 4;
339         args->max_readdir = 1024;
340
341         /* ip1[:port1][,ip2[:port2]...]:/subdir/in/fs */
342         err = -EINVAL;
343         if (!dev_name)
344                 goto out;
345         *path = strstr(dev_name, ":/");
346         if (*path == NULL) {
347                 pr_err("device name is missing path (no :/ in %s)\n",
348                        dev_name);
349                 goto out;
350         }
351
352         /* get mon ip(s) */
353         err = ceph_parse_ips(dev_name, *path, args->mon_addr,
354                              CEPH_MAX_MON, &args->num_mon);
355         if (err < 0)
356                 goto out;
357
358         /* path on server */
359         *path += 2;
360         dout("server path '%s'\n", *path);
361
362         /* parse mount options */
363         while ((c = strsep(&options, ",")) != NULL) {
364                 int token, intval, ret;
365                 if (!*c)
366                         continue;
367                 err = -EINVAL;
368                 token = match_token((char *)c, arg_tokens, argstr);
369                 if (token < 0) {
370                         pr_err("bad mount option at '%s'\n", c);
371                         goto out;
372                 }
373                 if (token < Opt_last_int) {
374                         ret = match_int(&argstr[0], &intval);
375                         if (ret < 0) {
376                                 pr_err("bad mount option arg (not int) "
377                                        "at '%s'\n", c);
378                                 continue;
379                         }
380                         dout("got int token %d val %d\n", token, intval);
381                 } else if (token > Opt_last_int && token < Opt_last_string) {
382                         dout("got string token %d val %s\n", token,
383                              argstr[0].from);
384                 } else {
385                         dout("got token %d\n", token);
386                 }
387                 switch (token) {
388                 case Opt_fsidmajor:
389                         *(__le64 *)&args->fsid.fsid[0] = cpu_to_le64(intval);
390                         break;
391                 case Opt_fsidminor:
392                         *(__le64 *)&args->fsid.fsid[8] = cpu_to_le64(intval);
393                         break;
394                 case Opt_ip:
395                         err = ceph_parse_ips(argstr[0].from,
396                                              argstr[0].to,
397                                              &args->my_addr,
398                                              1, NULL);
399                         if (err < 0)
400                                 goto out;
401                         args->flags |= CEPH_OPT_MYIP;
402                         break;
403
404                 case Opt_snapdirname:
405                         kfree(args->snapdir_name);
406                         args->snapdir_name = kstrndup(argstr[0].from,
407                                               argstr[0].to-argstr[0].from,
408                                               GFP_KERNEL);
409                         break;
410                 case Opt_secret:
411                         args->secret = kstrndup(argstr[0].from,
412                                                 argstr[0].to-argstr[0].from,
413                                                 GFP_KERNEL);
414                         break;
415
416                         /* misc */
417                 case Opt_wsize:
418                         args->wsize = intval;
419                         break;
420                 case Opt_rsize:
421                         args->rsize = intval;
422                         break;
423                 case Opt_osdtimeout:
424                         args->osd_timeout = intval;
425                         break;
426                 case Opt_mount_timeout:
427                         args->mount_timeout = intval;
428                         break;
429                 case Opt_caps_wanted_delay_min:
430                         args->caps_wanted_delay_min = intval;
431                         break;
432                 case Opt_caps_wanted_delay_max:
433                         args->caps_wanted_delay_max = intval;
434                         break;
435                 case Opt_readdir_max_entries:
436                         args->max_readdir = intval;
437                         break;
438
439                 case Opt_noshare:
440                         args->flags |= CEPH_OPT_NOSHARE;
441                         break;
442
443                 case Opt_dirstat:
444                         args->flags |= CEPH_OPT_DIRSTAT;
445                         break;
446                 case Opt_nodirstat:
447                         args->flags &= ~CEPH_OPT_DIRSTAT;
448                         break;
449                 case Opt_rbytes:
450                         args->flags |= CEPH_OPT_RBYTES;
451                         break;
452                 case Opt_norbytes:
453                         args->flags &= ~CEPH_OPT_RBYTES;
454                         break;
455                 case Opt_nocrc:
456                         args->flags |= CEPH_OPT_NOCRC;
457                         break;
458                 case Opt_noasyncreaddir:
459                         args->flags |= CEPH_OPT_NOASYNCREADDIR;
460                         break;
461
462                 default:
463                         BUG_ON(token);
464                 }
465         }
466         return args;
467
468 out:
469         kfree(args->mon_addr);
470         kfree(args);
471         return ERR_PTR(err);
472 }
473
474 static void destroy_mount_args(struct ceph_mount_args *args)
475 {
476         dout("destroy_mount_args %p\n", args);
477         kfree(args->snapdir_name);
478         args->snapdir_name = NULL;
479         kfree(args->secret);
480         args->secret = NULL;
481         kfree(args);
482 }
483
484 /*
485  * create a fresh client instance
486  */
487 static struct ceph_client *ceph_create_client(struct ceph_mount_args *args)
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         client->mount_args = args;
504
505         client->msgr = NULL;
506
507         client->mount_err = 0;
508         client->signed_ticket = NULL;
509         client->signed_ticket_len = 0;
510
511         err = -ENOMEM;
512         client->wb_wq = create_workqueue("ceph-writeback");
513         if (client->wb_wq == NULL)
514                 goto fail;
515         client->pg_inv_wq = create_singlethread_workqueue("ceph-pg-invalid");
516         if (client->pg_inv_wq == NULL)
517                 goto fail_wb_wq;
518         client->trunc_wq = create_singlethread_workqueue("ceph-trunc");
519         if (client->trunc_wq == NULL)
520                 goto fail_pg_inv_wq;
521
522         /* subsystems */
523         err = ceph_monc_init(&client->monc, client);
524         if (err < 0)
525                 goto fail_trunc_wq;
526         err = ceph_osdc_init(&client->osdc, client);
527         if (err < 0)
528                 goto fail_monc;
529         ceph_mdsc_init(&client->mdsc, client);
530         return client;
531
532 fail_monc:
533         ceph_monc_stop(&client->monc);
534 fail_trunc_wq:
535         destroy_workqueue(client->trunc_wq);
536 fail_pg_inv_wq:
537         destroy_workqueue(client->pg_inv_wq);
538 fail_wb_wq:
539         destroy_workqueue(client->wb_wq);
540 fail:
541         kfree(client);
542         return ERR_PTR(err);
543 }
544
545 static void ceph_destroy_client(struct ceph_client *client)
546 {
547         dout("destroy_client %p\n", client);
548
549         /* unmount */
550         ceph_mdsc_stop(&client->mdsc);
551         ceph_monc_stop(&client->monc);
552         ceph_osdc_stop(&client->osdc);
553
554         kfree(client->signed_ticket);
555
556         ceph_debugfs_client_cleanup(client);
557         destroy_workqueue(client->wb_wq);
558         destroy_workqueue(client->pg_inv_wq);
559         destroy_workqueue(client->trunc_wq);
560
561         if (client->msgr)
562                 ceph_messenger_destroy(client->msgr);
563         if (client->wb_pagevec_pool)
564                 mempool_destroy(client->wb_pagevec_pool);
565
566         destroy_mount_args(client->mount_args);
567
568         kfree(client);
569         dout("destroy_client %p done\n", client);
570 }
571
572 /*
573  * true if we have the mon map (and have thus joined the cluster)
574  */
575 static int have_mon_map(struct ceph_client *client)
576 {
577         return client->monc.monmap && client->monc.monmap->epoch;
578 }
579
580 /*
581  * Bootstrap mount by opening the root directory.  Note the mount
582  * @started time from caller, and time out if this takes too long.
583  */
584 static struct dentry *open_root_dentry(struct ceph_client *client,
585                                        const char *path,
586                                        unsigned long started)
587 {
588         struct ceph_mds_client *mdsc = &client->mdsc;
589         struct ceph_mds_request *req = NULL;
590         int err;
591         struct dentry *root;
592
593         /* open dir */
594         dout("open_root_inode opening '%s'\n", path);
595         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
596         if (IS_ERR(req))
597                 return ERR_PTR(PTR_ERR(req));
598         req->r_path1 = kstrdup(path, GFP_NOFS);
599         req->r_ino1.ino = CEPH_INO_ROOT;
600         req->r_ino1.snap = CEPH_NOSNAP;
601         req->r_started = started;
602         req->r_timeout = client->mount_args->mount_timeout * HZ;
603         req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
604         req->r_num_caps = 2;
605         err = ceph_mdsc_do_request(mdsc, NULL, req);
606         if (err == 0) {
607                 dout("open_root_inode success\n");
608                 if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
609                     client->sb->s_root == NULL)
610                         root = d_alloc_root(req->r_target_inode);
611                 else
612                         root = d_obtain_alias(req->r_target_inode);
613                 req->r_target_inode = NULL;
614                 dout("open_root_inode success, root dentry is %p\n", root);
615         } else {
616                 root = ERR_PTR(err);
617         }
618         ceph_mdsc_put_request(req);
619         return root;
620 }
621
622 /*
623  * mount: join the ceph cluster, and open root directory.
624  */
625 static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt,
626                       const char *path)
627 {
628         struct ceph_entity_addr *myaddr = NULL;
629         int err;
630         unsigned long timeout = client->mount_args->mount_timeout * HZ;
631         unsigned long started = jiffies;  /* note the start time */
632         struct dentry *root;
633
634         dout("mount start\n");
635         mutex_lock(&client->mount_mutex);
636
637         /* initialize the messenger */
638         if (client->msgr == NULL) {
639                 if (ceph_test_opt(client, MYIP))
640                         myaddr = &client->mount_args->my_addr;
641                 client->msgr = ceph_messenger_create(myaddr);
642                 if (IS_ERR(client->msgr)) {
643                         err = PTR_ERR(client->msgr);
644                         client->msgr = NULL;
645                         goto out;
646                 }
647                 client->msgr->nocrc = ceph_test_opt(client, NOCRC);
648         }
649
650         /* send mount request, and wait for mon, mds, and osd maps */
651         err = ceph_monc_request_mount(&client->monc);
652         if (err < 0)
653                 goto out;
654
655         while (!have_mon_map(client) && !client->mount_err) {
656                 err = -EIO;
657                 if (timeout && time_after_eq(jiffies, started + timeout))
658                         goto out;
659
660                 /* wait */
661                 dout("mount waiting for mount\n");
662                 err = wait_event_interruptible_timeout(client->mount_wq,
663                                client->mount_err || have_mon_map(client),
664                                timeout);
665                 if (err == -EINTR || err == -ERESTARTSYS)
666                         goto out;
667                 if (client->mount_err) {
668                         err = client->mount_err;
669                         goto out;
670                 }
671         }
672
673         dout("mount opening root\n");
674         root = open_root_dentry(client, "", started);
675         if (IS_ERR(root)) {
676                 err = PTR_ERR(root);
677                 goto out;
678         }
679         if (client->sb->s_root)
680                 dput(root);
681         else
682                 client->sb->s_root = root;
683
684         if (path[0] == 0) {
685                 dget(root);
686         } else {
687                 dout("mount opening base mountpoint\n");
688                 root = open_root_dentry(client, path, started);
689                 if (IS_ERR(root)) {
690                         err = PTR_ERR(root);
691                         dput(client->sb->s_root);
692                         client->sb->s_root = NULL;
693                         goto out;
694                 }
695         }
696
697         mnt->mnt_root = root;
698         mnt->mnt_sb = client->sb;
699
700         client->mount_state = CEPH_MOUNT_MOUNTED;
701         dout("mount success\n");
702         err = 0;
703
704 out:
705         mutex_unlock(&client->mount_mutex);
706         return err;
707 }
708
709 static int ceph_set_super(struct super_block *s, void *data)
710 {
711         struct ceph_client *client = data;
712         int ret;
713
714         dout("set_super %p data %p\n", s, data);
715
716         s->s_flags = client->mount_args->sb_flags;
717         s->s_maxbytes = 1ULL << 40;  /* temp value until we get mdsmap */
718
719         s->s_fs_info = client;
720         client->sb = s;
721
722         s->s_op = &ceph_super_ops;
723         s->s_export_op = &ceph_export_ops;
724
725         s->s_time_gran = 1000;  /* 1000 ns == 1 us */
726
727         ret = set_anon_super(s, NULL);  /* what is that second arg for? */
728         if (ret != 0)
729                 goto fail;
730
731         return ret;
732
733 fail:
734         s->s_fs_info = NULL;
735         client->sb = NULL;
736         return ret;
737 }
738
739 /*
740  * share superblock if same fs AND options
741  */
742 static int ceph_compare_super(struct super_block *sb, void *data)
743 {
744         struct ceph_client *new = data;
745         struct ceph_mount_args *args = new->mount_args;
746         struct ceph_client *other = ceph_sb_to_client(sb);
747         int i;
748
749         dout("ceph_compare_super %p\n", sb);
750         if (args->flags & CEPH_OPT_FSID) {
751                 if (ceph_fsid_compare(&args->fsid, &other->fsid)) {
752                         dout("fsid doesn't match\n");
753                         return 0;
754                 }
755         } else {
756                 /* do we share (a) monitor? */
757                 for (i = 0; i < new->monc.monmap->num_mon; i++)
758                         if (ceph_monmap_contains(other->monc.monmap,
759                                          &new->monc.monmap->mon_inst[i].addr))
760                                 break;
761                 if (i == new->monc.monmap->num_mon) {
762                         dout("mon ip not part of monmap\n");
763                         return 0;
764                 }
765                 dout("mon ip matches existing sb %p\n", sb);
766         }
767         if (args->sb_flags != other->mount_args->sb_flags) {
768                 dout("flags differ\n");
769                 return 0;
770         }
771         return 1;
772 }
773
774 /*
775  * construct our own bdi so we can control readahead, etc.
776  */
777 static int ceph_init_bdi(struct super_block *sb, struct ceph_client *client)
778 {
779         int err;
780
781         err = bdi_init(&client->backing_dev_info);
782         if (err < 0)
783                 return err;
784         sb->s_bdi = &client->backing_dev_info;
785
786         /* set ra_pages based on rsize mount option? */
787         if (client->mount_args->rsize >= PAGE_CACHE_SIZE)
788                 client->backing_dev_info.ra_pages =
789                         (client->mount_args->rsize + PAGE_CACHE_SIZE - 1)
790                         >> PAGE_SHIFT;
791
792         err = bdi_register_dev(&client->backing_dev_info, sb->s_dev);
793         return err;
794 }
795
796 static int ceph_get_sb(struct file_system_type *fs_type,
797                        int flags, const char *dev_name, void *data,
798                        struct vfsmount *mnt)
799 {
800         struct super_block *sb;
801         struct ceph_client *client;
802         int err;
803         int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
804         const char *path = 0;
805         struct ceph_mount_args *args;
806
807         dout("ceph_get_sb\n");
808         args = parse_mount_args(flags, data, dev_name, &path);
809         if (IS_ERR(args)) {
810                 err = PTR_ERR(args);
811                 goto out_final;
812         }
813
814         /* create client (which we may/may not use) */
815         client = ceph_create_client(args);
816         if (IS_ERR(client)) {
817                 err = PTR_ERR(client);
818                 goto out_final;
819         }
820
821         if (client->mount_args->flags & CEPH_OPT_NOSHARE)
822                 compare_super = NULL;
823         sb = sget(fs_type, compare_super, ceph_set_super, client);
824         if (IS_ERR(sb)) {
825                 err = PTR_ERR(sb);
826                 goto out;
827         }
828
829         if (ceph_client(sb) != client) {
830                 ceph_destroy_client(client);
831                 client = ceph_client(sb);
832                 dout("get_sb got existing client %p\n", client);
833         } else {
834                 dout("get_sb using new client %p\n", client);
835
836                 /* set up mempools */
837                 err = -ENOMEM;
838                 client->wb_pagevec_pool = mempool_create_kmalloc_pool(10,
839                               client->mount_args->wsize >> PAGE_CACHE_SHIFT);
840                 if (!client->wb_pagevec_pool)
841                         goto out_splat;
842
843                 err = ceph_init_bdi(sb, client);
844                 if (err < 0)
845                         goto out_splat;
846         }
847
848         err = ceph_mount(client, mnt, path);
849         if (err < 0)
850                 goto out_splat;
851         dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root,
852              mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode));
853         return 0;
854
855 out_splat:
856         ceph_mdsc_close_sessions(&client->mdsc);
857         up_write(&sb->s_umount);
858         deactivate_super(sb);
859         goto out_final;
860
861 out:
862         ceph_destroy_client(client);
863 out_final:
864         dout("ceph_get_sb fail %d\n", err);
865         return err;
866 }
867
868 static void ceph_kill_sb(struct super_block *s)
869 {
870         struct ceph_client *client = ceph_sb_to_client(s);
871         dout("kill_sb %p\n", s);
872         ceph_mdsc_pre_umount(&client->mdsc);
873         kill_anon_super(s);    /* will call put_super after sb is r/o */
874         bdi_unregister(&client->backing_dev_info);
875         bdi_destroy(&client->backing_dev_info);
876         ceph_destroy_client(client);
877 }
878
879 static struct file_system_type ceph_fs_type = {
880         .owner          = THIS_MODULE,
881         .name           = "ceph",
882         .get_sb         = ceph_get_sb,
883         .kill_sb        = ceph_kill_sb,
884         .fs_flags       = FS_RENAME_DOES_D_MOVE,
885 };
886
887 #define _STRINGIFY(x) #x
888 #define STRINGIFY(x) _STRINGIFY(x)
889
890 static int __init init_ceph(void)
891 {
892         int ret = 0;
893
894         ret = ceph_debugfs_init();
895         if (ret < 0)
896                 goto out;
897
898         ret = ceph_msgr_init();
899         if (ret < 0)
900                 goto out_debugfs;
901
902         ret = init_caches();
903         if (ret)
904                 goto out_msgr;
905
906         ceph_caps_init();
907
908         ret = register_filesystem(&ceph_fs_type);
909         if (ret)
910                 goto out_icache;
911
912         pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n",
913                 CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH,
914                 CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL);
915         return 0;
916
917 out_icache:
918         destroy_caches();
919 out_msgr:
920         ceph_msgr_exit();
921 out_debugfs:
922         ceph_debugfs_cleanup();
923 out:
924         return ret;
925 }
926
927 static void __exit exit_ceph(void)
928 {
929         dout("exit_ceph\n");
930         unregister_filesystem(&ceph_fs_type);
931         ceph_caps_finalize();
932         destroy_caches();
933         ceph_msgr_exit();
934         ceph_debugfs_cleanup();
935 }
936
937 module_init(init_ceph);
938 module_exit(exit_ceph);
939
940 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
941 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
942 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
943 MODULE_DESCRIPTION("Ceph filesystem for Linux");
944 MODULE_LICENSE("GPL");