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