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