ceph: fix, clean up string mount arg parsing
[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 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;
320         int i;
321
322         dout("parse_mount_args dev_name '%s'\n", dev_name);
323         memset(args, 0, sizeof(*args));
324
325         mon_addr = kcalloc(CEPH_MAX_MON, sizeof(*mon_addr), GFP_KERNEL);
326         if (!mon_addr)
327                 return -ENOMEM;
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, mon_addr,
354                              CEPH_MAX_MON, &num_mon);
355         if (err < 0)
356                 goto out;
357
358         /* build initial monmap */
359         err = -ENOMEM;
360         client->monc.monmap = kzalloc(sizeof(*client->monc.monmap) +
361                                num_mon*sizeof(client->monc.monmap->mon_inst[0]),
362                                GFP_KERNEL);
363         if (!client->monc.monmap)
364                 goto out;
365         for (i = 0; i < num_mon; i++) {
366                 client->monc.monmap->mon_inst[i].addr = mon_addr[i];
367                 client->monc.monmap->mon_inst[i].addr.erank = 0;
368                 client->monc.monmap->mon_inst[i].addr.nonce = 0;
369                 client->monc.monmap->mon_inst[i].name.type =
370                         CEPH_ENTITY_TYPE_MON;
371                 client->monc.monmap->mon_inst[i].name.num = cpu_to_le64(i);
372         }
373         client->monc.monmap->num_mon = num_mon;
374         memset(&args->my_addr.in_addr, 0, sizeof(args->my_addr.in_addr));
375
376         /* path on server */
377         *path += 2;
378         dout("server path '%s'\n", *path);
379
380         /* parse mount options */
381         while ((c = strsep(&options, ",")) != NULL) {
382                 int token, intval, ret;
383                 if (!*c)
384                         continue;
385                 err = -EINVAL;
386                 token = match_token((char *)c, arg_tokens, argstr);
387                 if (token < 0) {
388                         pr_err("bad mount option at '%s'\n", c);
389                         goto out;
390                 }
391                 if (token < Opt_last_int) {
392                         ret = match_int(&argstr[0], &intval);
393                         if (ret < 0) {
394                                 pr_err("bad mount option arg (not int) "
395                                        "at '%s'\n", c);
396                                 continue;
397                         }
398                         dout("got int token %d val %d\n", token, intval);
399                 } else if (token > Opt_last_int && token < Opt_last_string) {
400                         dout("got string token %d val %s\n", token,
401                              argstr[0].from);
402                 } else {
403                         dout("got token %d\n", token);
404                 }
405                 switch (token) {
406                 case Opt_fsidmajor:
407                         *(__le64 *)&args->fsid.fsid[0] = cpu_to_le64(intval);
408                         break;
409                 case Opt_fsidminor:
410                         *(__le64 *)&args->fsid.fsid[8] = cpu_to_le64(intval);
411                         break;
412                 case Opt_ip:
413                         err = ceph_parse_ips(argstr[0].from,
414                                              argstr[0].to,
415                                              &args->my_addr,
416                                              1, NULL);
417                         if (err < 0)
418                                 return err;
419                         args->flags |= CEPH_OPT_MYIP;
420                         break;
421
422                 case Opt_snapdirname:
423                         kfree(args->snapdir_name);
424                         args->snapdir_name = kstrndup(argstr[0].from,
425                                               argstr[0].to-argstr[0].from,
426                                               GFP_KERNEL);
427                         break;
428                 case Opt_secret:
429                         args->secret = kstrndup(argstr[0].from,
430                                                 argstr[0].to-argstr[0].from,
431                                                 GFP_KERNEL);
432                         break;
433
434                         /* misc */
435                 case Opt_wsize:
436                         args->wsize = intval;
437                         break;
438                 case Opt_rsize:
439                         args->rsize = intval;
440                         break;
441                 case Opt_osdtimeout:
442                         args->osd_timeout = intval;
443                         break;
444                 case Opt_mount_timeout:
445                         args->mount_timeout = intval;
446                         break;
447                 case Opt_caps_wanted_delay_min:
448                         args->caps_wanted_delay_min = intval;
449                         break;
450                 case Opt_caps_wanted_delay_max:
451                         args->caps_wanted_delay_max = intval;
452                         break;
453                 case Opt_readdir_max_entries:
454                         args->max_readdir = intval;
455                         break;
456
457                 case Opt_noshare:
458                         args->flags |= CEPH_OPT_NOSHARE;
459                         break;
460
461                 case Opt_dirstat:
462                         args->flags |= CEPH_OPT_DIRSTAT;
463                         break;
464                 case Opt_nodirstat:
465                         args->flags &= ~CEPH_OPT_DIRSTAT;
466                         break;
467                 case Opt_rbytes:
468                         args->flags |= CEPH_OPT_RBYTES;
469                         break;
470                 case Opt_norbytes:
471                         args->flags &= ~CEPH_OPT_RBYTES;
472                         break;
473                 case Opt_nocrc:
474                         args->flags |= CEPH_OPT_NOCRC;
475                         break;
476                 case Opt_noasyncreaddir:
477                         args->flags |= CEPH_OPT_NOASYNCREADDIR;
478                         break;
479
480                 default:
481                         BUG_ON(token);
482                 }
483         }
484         err = 0;
485
486 out:
487         kfree(mon_addr);
488         return err;
489 }
490
491 static void release_mount_args(struct ceph_mount_args *args)
492 {
493         kfree(args->snapdir_name);
494         args->snapdir_name = NULL;
495         kfree(args->secret);
496         args->secret = NULL;
497 }
498
499 /*
500  * create a fresh client instance
501  */
502 static struct ceph_client *ceph_create_client(void)
503 {
504         struct ceph_client *client;
505         int err = -ENOMEM;
506
507         client = kzalloc(sizeof(*client), GFP_KERNEL);
508         if (client == NULL)
509                 return ERR_PTR(-ENOMEM);
510
511         mutex_init(&client->mount_mutex);
512
513         init_waitqueue_head(&client->mount_wq);
514
515         client->sb = NULL;
516         client->mount_state = CEPH_MOUNT_MOUNTING;
517         client->whoami = -1;
518
519         client->msgr = NULL;
520
521         client->mount_err = 0;
522         client->signed_ticket = NULL;
523         client->signed_ticket_len = 0;
524
525         err = -ENOMEM;
526         client->wb_wq = create_workqueue("ceph-writeback");
527         if (client->wb_wq == NULL)
528                 goto fail;
529         client->pg_inv_wq = create_singlethread_workqueue("ceph-pg-invalid");
530         if (client->pg_inv_wq == NULL)
531                 goto fail_wb_wq;
532         client->trunc_wq = create_singlethread_workqueue("ceph-trunc");
533         if (client->trunc_wq == NULL)
534                 goto fail_pg_inv_wq;
535
536         /* subsystems */
537         err = ceph_monc_init(&client->monc, client);
538         if (err < 0)
539                 goto fail_trunc_wq;
540         err = ceph_osdc_init(&client->osdc, client);
541         if (err < 0)
542                 goto fail_monc;
543         ceph_mdsc_init(&client->mdsc, client);
544         return client;
545
546 fail_monc:
547         ceph_monc_stop(&client->monc);
548 fail_trunc_wq:
549         destroy_workqueue(client->trunc_wq);
550 fail_pg_inv_wq:
551         destroy_workqueue(client->pg_inv_wq);
552 fail_wb_wq:
553         destroy_workqueue(client->wb_wq);
554 fail:
555         kfree(client);
556         return ERR_PTR(err);
557 }
558
559 static void ceph_destroy_client(struct ceph_client *client)
560 {
561         dout("destroy_client %p\n", client);
562
563         /* unmount */
564         ceph_mdsc_stop(&client->mdsc);
565         ceph_monc_stop(&client->monc);
566         ceph_osdc_stop(&client->osdc);
567
568         kfree(client->signed_ticket);
569
570         ceph_debugfs_client_cleanup(client);
571         destroy_workqueue(client->wb_wq);
572         destroy_workqueue(client->pg_inv_wq);
573         destroy_workqueue(client->trunc_wq);
574
575         if (client->msgr)
576                 ceph_messenger_destroy(client->msgr);
577         if (client->wb_pagevec_pool)
578                 mempool_destroy(client->wb_pagevec_pool);
579
580         release_mount_args(&client->mount_args);
581
582         kfree(client);
583         dout("destroy_client %p done\n", client);
584 }
585
586 /*
587  * true if we have the mon map (and have thus joined the cluster)
588  */
589 static int have_mon_map(struct ceph_client *client)
590 {
591         return client->monc.monmap && client->monc.monmap->epoch;
592 }
593
594 /*
595  * Bootstrap mount by opening the root directory.  Note the mount
596  * @started time from caller, and time out if this takes too long.
597  */
598 static struct dentry *open_root_dentry(struct ceph_client *client,
599                                        const char *path,
600                                        unsigned long started)
601 {
602         struct ceph_mds_client *mdsc = &client->mdsc;
603         struct ceph_mds_request *req = NULL;
604         int err;
605         struct dentry *root;
606
607         /* open dir */
608         dout("open_root_inode opening '%s'\n", path);
609         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
610         if (IS_ERR(req))
611                 return ERR_PTR(PTR_ERR(req));
612         req->r_path1 = kstrdup(path, GFP_NOFS);
613         req->r_ino1.ino = CEPH_INO_ROOT;
614         req->r_ino1.snap = CEPH_NOSNAP;
615         req->r_started = started;
616         req->r_timeout = client->mount_args.mount_timeout * HZ;
617         req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
618         req->r_num_caps = 2;
619         err = ceph_mdsc_do_request(mdsc, NULL, req);
620         if (err == 0) {
621                 dout("open_root_inode success\n");
622                 if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
623                     client->sb->s_root == NULL)
624                         root = d_alloc_root(req->r_target_inode);
625                 else
626                         root = d_obtain_alias(req->r_target_inode);
627                 req->r_target_inode = NULL;
628                 dout("open_root_inode success, root dentry is %p\n", root);
629         } else {
630                 root = ERR_PTR(err);
631         }
632         ceph_mdsc_put_request(req);
633         return root;
634 }
635
636 /*
637  * mount: join the ceph cluster, and open root directory.
638  */
639 static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt,
640                       const char *path)
641 {
642         struct ceph_entity_addr *myaddr = NULL;
643         int err;
644         unsigned long timeout = client->mount_args.mount_timeout * HZ;
645         unsigned long started = jiffies;  /* note the start time */
646         struct dentry *root;
647
648         dout("mount start\n");
649         mutex_lock(&client->mount_mutex);
650
651         /* initialize the messenger */
652         if (client->msgr == NULL) {
653                 if (ceph_test_opt(client, MYIP))
654                         myaddr = &client->mount_args.my_addr;
655                 client->msgr = ceph_messenger_create(myaddr);
656                 if (IS_ERR(client->msgr)) {
657                         err = PTR_ERR(client->msgr);
658                         client->msgr = NULL;
659                         goto out;
660                 }
661                 client->msgr->nocrc = ceph_test_opt(client, NOCRC);
662         }
663
664         /* send mount request, and wait for mon, mds, and osd maps */
665         err = ceph_monc_request_mount(&client->monc);
666         if (err < 0)
667                 goto out;
668
669         while (!have_mon_map(client) && !client->mount_err) {
670                 err = -EIO;
671                 if (timeout && time_after_eq(jiffies, started + timeout))
672                         goto out;
673
674                 /* wait */
675                 dout("mount waiting for mount\n");
676                 err = wait_event_interruptible_timeout(client->mount_wq,
677                                client->mount_err || have_mon_map(client),
678                                timeout);
679                 if (err == -EINTR || err == -ERESTARTSYS)
680                         goto out;
681                 if (client->mount_err) {
682                         err = client->mount_err;
683                         goto out;
684                 }
685         }
686
687         dout("mount opening root\n");
688         root = open_root_dentry(client, "", started);
689         if (IS_ERR(root)) {
690                 err = PTR_ERR(root);
691                 goto out;
692         }
693         if (client->sb->s_root)
694                 dput(root);
695         else
696                 client->sb->s_root = root;
697
698         if (path[0] == 0) {
699                 dget(root);
700         } else {
701                 dout("mount opening base mountpoint\n");
702                 root = open_root_dentry(client, path, started);
703                 if (IS_ERR(root)) {
704                         err = PTR_ERR(root);
705                         dput(client->sb->s_root);
706                         client->sb->s_root = NULL;
707                         goto out;
708                 }
709         }
710
711         mnt->mnt_root = root;
712         mnt->mnt_sb = client->sb;
713
714         client->mount_state = CEPH_MOUNT_MOUNTED;
715         dout("mount success\n");
716         err = 0;
717
718 out:
719         mutex_unlock(&client->mount_mutex);
720         return err;
721 }
722
723 static int ceph_set_super(struct super_block *s, void *data)
724 {
725         struct ceph_client *client = data;
726         int ret;
727
728         dout("set_super %p data %p\n", s, data);
729
730         s->s_flags = client->mount_args.sb_flags;
731         s->s_maxbytes = 1ULL << 40;  /* temp value until we get mdsmap */
732
733         s->s_fs_info = client;
734         client->sb = s;
735
736         s->s_op = &ceph_super_ops;
737         s->s_export_op = &ceph_export_ops;
738
739         s->s_time_gran = 1000;  /* 1000 ns == 1 us */
740
741         ret = set_anon_super(s, NULL);  /* what is that second arg for? */
742         if (ret != 0)
743                 goto fail;
744
745         return ret;
746
747 fail:
748         s->s_fs_info = NULL;
749         client->sb = NULL;
750         return ret;
751 }
752
753 /*
754  * share superblock if same fs AND options
755  */
756 static int ceph_compare_super(struct super_block *sb, void *data)
757 {
758         struct ceph_client *new = data;
759         struct ceph_mount_args *args = &new->mount_args;
760         struct ceph_client *other = ceph_sb_to_client(sb);
761         int i;
762
763         dout("ceph_compare_super %p\n", sb);
764         if (args->flags & CEPH_OPT_FSID) {
765                 if (ceph_fsid_compare(&args->fsid, &other->fsid)) {
766                         dout("fsid doesn't match\n");
767                         return 0;
768                 }
769         } else {
770                 /* do we share (a) monitor? */
771                 for (i = 0; i < new->monc.monmap->num_mon; i++)
772                         if (ceph_monmap_contains(other->monc.monmap,
773                                          &new->monc.monmap->mon_inst[i].addr))
774                                 break;
775                 if (i == new->monc.monmap->num_mon) {
776                         dout("mon ip not part of monmap\n");
777                         return 0;
778                 }
779                 dout("mon ip matches existing sb %p\n", sb);
780         }
781         if (args->sb_flags != other->mount_args.sb_flags) {
782                 dout("flags differ\n");
783                 return 0;
784         }
785         return 1;
786 }
787
788 /*
789  * construct our own bdi so we can control readahead, etc.
790  */
791 static int ceph_init_bdi(struct super_block *sb, struct ceph_client *client)
792 {
793         int err;
794
795         err = bdi_init(&client->backing_dev_info);
796         if (err < 0)
797                 return err;
798         sb->s_bdi = &client->backing_dev_info;
799
800         /* set ra_pages based on rsize mount option? */
801         if (client->mount_args.rsize >= PAGE_CACHE_SIZE)
802                 client->backing_dev_info.ra_pages =
803                         (client->mount_args.rsize + PAGE_CACHE_SIZE - 1)
804                         >> PAGE_SHIFT;
805
806         err = bdi_register_dev(&client->backing_dev_info, sb->s_dev);
807         return err;
808 }
809
810 static int ceph_get_sb(struct file_system_type *fs_type,
811                        int flags, const char *dev_name, void *data,
812                        struct vfsmount *mnt)
813 {
814         struct super_block *sb;
815         struct ceph_client *client;
816         int err;
817         int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
818         const char *path = 0;
819
820         dout("ceph_get_sb\n");
821
822         /* create client (which we may/may not use) */
823         client = ceph_create_client();
824         if (IS_ERR(client))
825                 return PTR_ERR(client);
826
827         err = parse_mount_args(client, flags, data, dev_name, &path);
828         if (err < 0)
829                 goto out;
830
831         if (client->mount_args.flags & CEPH_OPT_NOSHARE)
832                 compare_super = NULL;
833         sb = sget(fs_type, compare_super, ceph_set_super, client);
834         if (IS_ERR(sb)) {
835                 err = PTR_ERR(sb);
836                 goto out;
837         }
838
839         if (ceph_client(sb) != client) {
840                 ceph_destroy_client(client);
841                 client = ceph_client(sb);
842                 dout("get_sb got existing client %p\n", client);
843         } else {
844                 dout("get_sb using new client %p\n", client);
845
846                 /* set up mempools */
847                 err = -ENOMEM;
848                 client->wb_pagevec_pool = mempool_create_kmalloc_pool(10,
849                               client->mount_args.wsize >> PAGE_CACHE_SHIFT);
850                 if (!client->wb_pagevec_pool)
851                         goto out_splat;
852
853                 err = ceph_init_bdi(sb, client);
854                 if (err < 0)
855                         goto out_splat;
856         }
857
858         err = ceph_mount(client, mnt, path);
859         if (err < 0)
860                 goto out_splat;
861         dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root,
862              mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode));
863         return 0;
864
865 out_splat:
866         ceph_mdsc_close_sessions(&client->mdsc);
867         up_write(&sb->s_umount);
868         deactivate_super(sb);
869         goto out_final;
870
871 out:
872         ceph_destroy_client(client);
873 out_final:
874         dout("ceph_get_sb fail %d\n", err);
875         return err;
876 }
877
878 static void ceph_kill_sb(struct super_block *s)
879 {
880         struct ceph_client *client = ceph_sb_to_client(s);
881         dout("kill_sb %p\n", s);
882         ceph_mdsc_pre_umount(&client->mdsc);
883         kill_anon_super(s);    /* will call put_super after sb is r/o */
884         bdi_unregister(&client->backing_dev_info);
885         bdi_destroy(&client->backing_dev_info);
886         ceph_destroy_client(client);
887 }
888
889 static struct file_system_type ceph_fs_type = {
890         .owner          = THIS_MODULE,
891         .name           = "ceph",
892         .get_sb         = ceph_get_sb,
893         .kill_sb        = ceph_kill_sb,
894         .fs_flags       = FS_RENAME_DOES_D_MOVE,
895 };
896
897 #define _STRINGIFY(x) #x
898 #define STRINGIFY(x) _STRINGIFY(x)
899
900 static int __init init_ceph(void)
901 {
902         int ret = 0;
903
904         ret = ceph_debugfs_init();
905         if (ret < 0)
906                 goto out;
907
908         ret = ceph_msgr_init();
909         if (ret < 0)
910                 goto out_debugfs;
911
912         ret = init_caches();
913         if (ret)
914                 goto out_msgr;
915
916         ceph_caps_init();
917
918         ret = register_filesystem(&ceph_fs_type);
919         if (ret)
920                 goto out_icache;
921
922         pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n",
923                 CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH,
924                 CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL);
925         return 0;
926
927 out_icache:
928         destroy_caches();
929 out_msgr:
930         ceph_msgr_exit();
931 out_debugfs:
932         ceph_debugfs_cleanup();
933 out:
934         return ret;
935 }
936
937 static void __exit exit_ceph(void)
938 {
939         dout("exit_ceph\n");
940         unregister_filesystem(&ceph_fs_type);
941         ceph_caps_finalize();
942         destroy_caches();
943         ceph_msgr_exit();
944         ceph_debugfs_cleanup();
945 }
946
947 module_init(init_ceph);
948 module_exit(exit_ceph);
949
950 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
951 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
952 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
953 MODULE_DESCRIPTION("Ceph filesystem for Linux");
954 MODULE_LICENSE("GPL");