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