ceph: hex dump corrupt server data to KERN_DEBUG
[pandora-kernel.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 #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                         pr_err("bad fsid, had " FSID_FORMAT " got " FSID_FORMAT,
606                                PR_FSID(&client->fsid), PR_FSID(fsid));
607                         return -1;
608                 }
609         } else {
610                 pr_info("client%lld fsid " FSID_FORMAT "\n",
611                         client->monc.auth->global_id, PR_FSID(fsid));
612                 memcpy(&client->fsid, fsid, sizeof(*fsid));
613                 ceph_debugfs_client_init(client);
614                 client->have_fsid = true;
615         }
616         return 0;
617 }
618
619 /*
620  * true if we have the mon map (and have thus joined the cluster)
621  */
622 static int have_mon_map(struct ceph_client *client)
623 {
624         return client->monc.monmap && client->monc.monmap->epoch;
625 }
626
627 /*
628  * Bootstrap mount by opening the root directory.  Note the mount
629  * @started time from caller, and time out if this takes too long.
630  */
631 static struct dentry *open_root_dentry(struct ceph_client *client,
632                                        const char *path,
633                                        unsigned long started)
634 {
635         struct ceph_mds_client *mdsc = &client->mdsc;
636         struct ceph_mds_request *req = NULL;
637         int err;
638         struct dentry *root;
639
640         /* open dir */
641         dout("open_root_inode opening '%s'\n", path);
642         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
643         if (IS_ERR(req))
644                 return ERR_PTR(PTR_ERR(req));
645         req->r_path1 = kstrdup(path, GFP_NOFS);
646         req->r_ino1.ino = CEPH_INO_ROOT;
647         req->r_ino1.snap = CEPH_NOSNAP;
648         req->r_started = started;
649         req->r_timeout = client->mount_args->mount_timeout * HZ;
650         req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
651         req->r_num_caps = 2;
652         err = ceph_mdsc_do_request(mdsc, NULL, req);
653         if (err == 0) {
654                 dout("open_root_inode success\n");
655                 if (ceph_ino(req->r_target_inode) == CEPH_INO_ROOT &&
656                     client->sb->s_root == NULL)
657                         root = d_alloc_root(req->r_target_inode);
658                 else
659                         root = d_obtain_alias(req->r_target_inode);
660                 req->r_target_inode = NULL;
661                 dout("open_root_inode success, root dentry is %p\n", root);
662         } else {
663                 root = ERR_PTR(err);
664         }
665         ceph_mdsc_put_request(req);
666         return root;
667 }
668
669 /*
670  * mount: join the ceph cluster, and open root directory.
671  */
672 static int ceph_mount(struct ceph_client *client, struct vfsmount *mnt,
673                       const char *path)
674 {
675         struct ceph_entity_addr *myaddr = NULL;
676         int err;
677         unsigned long timeout = client->mount_args->mount_timeout * HZ;
678         unsigned long started = jiffies;  /* note the start time */
679         struct dentry *root;
680
681         dout("mount start\n");
682         mutex_lock(&client->mount_mutex);
683
684         /* initialize the messenger */
685         if (client->msgr == NULL) {
686                 if (ceph_test_opt(client, MYIP))
687                         myaddr = &client->mount_args->my_addr;
688                 client->msgr = ceph_messenger_create(myaddr);
689                 if (IS_ERR(client->msgr)) {
690                         err = PTR_ERR(client->msgr);
691                         client->msgr = NULL;
692                         goto out;
693                 }
694                 client->msgr->nocrc = ceph_test_opt(client, NOCRC);
695         }
696
697         /* open session, and wait for mon, mds, and osd maps */
698         err = ceph_monc_open_session(&client->monc);
699         if (err < 0)
700                 goto out;
701
702         while (!have_mon_map(client)) {
703                 err = -EIO;
704                 if (timeout && time_after_eq(jiffies, started + timeout))
705                         goto out;
706
707                 /* wait */
708                 dout("mount waiting for mon_map\n");
709                 err = wait_event_interruptible_timeout(client->mount_wq, /* FIXME */
710                                have_mon_map(client) || (client->mount_err < 0),
711                                timeout);
712                 if (err == -EINTR || err == -ERESTARTSYS)
713                         goto out;
714                 if (client->mount_err < 0) {
715                         err = client->mount_err;
716                         goto out;
717                 }
718         }
719
720         dout("mount opening root\n");
721         root = open_root_dentry(client, "", started);
722         if (IS_ERR(root)) {
723                 err = PTR_ERR(root);
724                 goto out;
725         }
726         if (client->sb->s_root)
727                 dput(root);
728         else
729                 client->sb->s_root = root;
730
731         if (path[0] == 0) {
732                 dget(root);
733         } else {
734                 dout("mount opening base mountpoint\n");
735                 root = open_root_dentry(client, path, started);
736                 if (IS_ERR(root)) {
737                         err = PTR_ERR(root);
738                         dput(client->sb->s_root);
739                         client->sb->s_root = NULL;
740                         goto out;
741                 }
742         }
743
744         mnt->mnt_root = root;
745         mnt->mnt_sb = client->sb;
746
747         client->mount_state = CEPH_MOUNT_MOUNTED;
748         dout("mount success\n");
749         err = 0;
750
751 out:
752         mutex_unlock(&client->mount_mutex);
753         return err;
754 }
755
756 static int ceph_set_super(struct super_block *s, void *data)
757 {
758         struct ceph_client *client = data;
759         int ret;
760
761         dout("set_super %p data %p\n", s, data);
762
763         s->s_flags = client->mount_args->sb_flags;
764         s->s_maxbytes = 1ULL << 40;  /* temp value until we get mdsmap */
765
766         s->s_fs_info = client;
767         client->sb = s;
768
769         s->s_op = &ceph_super_ops;
770         s->s_export_op = &ceph_export_ops;
771
772         s->s_time_gran = 1000;  /* 1000 ns == 1 us */
773
774         ret = set_anon_super(s, NULL);  /* what is that second arg for? */
775         if (ret != 0)
776                 goto fail;
777
778         return ret;
779
780 fail:
781         s->s_fs_info = NULL;
782         client->sb = NULL;
783         return ret;
784 }
785
786 /*
787  * share superblock if same fs AND options
788  */
789 static int ceph_compare_super(struct super_block *sb, void *data)
790 {
791         struct ceph_client *new = data;
792         struct ceph_mount_args *args = new->mount_args;
793         struct ceph_client *other = ceph_sb_to_client(sb);
794         int i;
795
796         dout("ceph_compare_super %p\n", sb);
797         if (args->flags & CEPH_OPT_FSID) {
798                 if (ceph_fsid_compare(&args->fsid, &other->fsid)) {
799                         dout("fsid doesn't match\n");
800                         return 0;
801                 }
802         } else {
803                 /* do we share (a) monitor? */
804                 for (i = 0; i < new->monc.monmap->num_mon; i++)
805                         if (ceph_monmap_contains(other->monc.monmap,
806                                          &new->monc.monmap->mon_inst[i].addr))
807                                 break;
808                 if (i == new->monc.monmap->num_mon) {
809                         dout("mon ip not part of monmap\n");
810                         return 0;
811                 }
812                 dout("mon ip matches existing sb %p\n", sb);
813         }
814         if (args->sb_flags != other->mount_args->sb_flags) {
815                 dout("flags differ\n");
816                 return 0;
817         }
818         return 1;
819 }
820
821 /*
822  * construct our own bdi so we can control readahead, etc.
823  */
824 static int ceph_register_bdi(struct super_block *sb, struct ceph_client *client)
825 {
826         int err;
827
828         sb->s_bdi = &client->backing_dev_info;
829
830         /* set ra_pages based on rsize mount option? */
831         if (client->mount_args->rsize >= PAGE_CACHE_SIZE)
832                 client->backing_dev_info.ra_pages =
833                         (client->mount_args->rsize + PAGE_CACHE_SIZE - 1)
834                         >> PAGE_SHIFT;
835         err = bdi_register_dev(&client->backing_dev_info, sb->s_dev);
836         return err;
837 }
838
839 static int ceph_get_sb(struct file_system_type *fs_type,
840                        int flags, const char *dev_name, void *data,
841                        struct vfsmount *mnt)
842 {
843         struct super_block *sb;
844         struct ceph_client *client;
845         int err;
846         int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
847         const char *path = NULL;
848         struct ceph_mount_args *args;
849
850         dout("ceph_get_sb\n");
851         args = parse_mount_args(flags, data, dev_name, &path);
852         if (IS_ERR(args)) {
853                 err = PTR_ERR(args);
854                 goto out_final;
855         }
856
857         /* create client (which we may/may not use) */
858         client = ceph_create_client(args);
859         if (IS_ERR(client)) {
860                 err = PTR_ERR(client);
861                 goto out_final;
862         }
863
864         if (client->mount_args->flags & CEPH_OPT_NOSHARE)
865                 compare_super = NULL;
866         sb = sget(fs_type, compare_super, ceph_set_super, client);
867         if (IS_ERR(sb)) {
868                 err = PTR_ERR(sb);
869                 goto out;
870         }
871
872         if (ceph_client(sb) != client) {
873                 ceph_destroy_client(client);
874                 client = ceph_client(sb);
875                 dout("get_sb got existing client %p\n", client);
876         } else {
877                 dout("get_sb using new client %p\n", client);
878                 err = ceph_register_bdi(sb, client);
879                 if (err < 0)
880                         goto out_splat;
881         }
882
883         err = ceph_mount(client, mnt, path);
884         if (err < 0)
885                 goto out_splat;
886         dout("root %p inode %p ino %llx.%llx\n", mnt->mnt_root,
887              mnt->mnt_root->d_inode, ceph_vinop(mnt->mnt_root->d_inode));
888         return 0;
889
890 out_splat:
891         ceph_mdsc_close_sessions(&client->mdsc);
892         up_write(&sb->s_umount);
893         deactivate_super(sb);
894         goto out_final;
895
896 out:
897         ceph_destroy_client(client);
898 out_final:
899         dout("ceph_get_sb fail %d\n", err);
900         return err;
901 }
902
903 static void ceph_kill_sb(struct super_block *s)
904 {
905         struct ceph_client *client = ceph_sb_to_client(s);
906         dout("kill_sb %p\n", s);
907         ceph_mdsc_pre_umount(&client->mdsc);
908         kill_anon_super(s);    /* will call put_super after sb is r/o */
909         bdi_unregister(&client->backing_dev_info);
910         bdi_destroy(&client->backing_dev_info);
911         ceph_destroy_client(client);
912 }
913
914 static struct file_system_type ceph_fs_type = {
915         .owner          = THIS_MODULE,
916         .name           = "ceph",
917         .get_sb         = ceph_get_sb,
918         .kill_sb        = ceph_kill_sb,
919         .fs_flags       = FS_RENAME_DOES_D_MOVE,
920 };
921
922 #define _STRINGIFY(x) #x
923 #define STRINGIFY(x) _STRINGIFY(x)
924
925 static int __init init_ceph(void)
926 {
927         int ret = 0;
928
929         ret = ceph_debugfs_init();
930         if (ret < 0)
931                 goto out;
932
933         ret = ceph_msgr_init();
934         if (ret < 0)
935                 goto out_debugfs;
936
937         ret = init_caches();
938         if (ret)
939                 goto out_msgr;
940
941         ceph_caps_init();
942
943         ret = register_filesystem(&ceph_fs_type);
944         if (ret)
945                 goto out_icache;
946
947         pr_info("loaded %d.%d.%d (mon/mds/osd proto %d/%d/%d)\n",
948                 CEPH_VERSION_MAJOR, CEPH_VERSION_MINOR, CEPH_VERSION_PATCH,
949                 CEPH_MONC_PROTOCOL, CEPH_MDSC_PROTOCOL, CEPH_OSDC_PROTOCOL);
950         return 0;
951
952 out_icache:
953         destroy_caches();
954 out_msgr:
955         ceph_msgr_exit();
956 out_debugfs:
957         ceph_debugfs_cleanup();
958 out:
959         return ret;
960 }
961
962 static void __exit exit_ceph(void)
963 {
964         dout("exit_ceph\n");
965         unregister_filesystem(&ceph_fs_type);
966         ceph_caps_finalize();
967         destroy_caches();
968         ceph_msgr_exit();
969         ceph_debugfs_cleanup();
970 }
971
972 module_init(init_ceph);
973 module_exit(exit_ceph);
974
975 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
976 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
977 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
978 MODULE_DESCRIPTION("Ceph filesystem for Linux");
979 MODULE_LICENSE("GPL");