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