Merge tag 'clk-fixes-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[linux-2.6-microblaze.git] / fs / ceph / super.c
1 // SPDX-License-Identifier: GPL-2.0-only
2
3 #include <linux/ceph/ceph_debug.h>
4
5 #include <linux/backing-dev.h>
6 #include <linux/ctype.h>
7 #include <linux/fs.h>
8 #include <linux/inet.h>
9 #include <linux/in6.h>
10 #include <linux/module.h>
11 #include <linux/mount.h>
12 #include <linux/parser.h>
13 #include <linux/sched.h>
14 #include <linux/seq_file.h>
15 #include <linux/slab.h>
16 #include <linux/statfs.h>
17 #include <linux/string.h>
18
19 #include "super.h"
20 #include "mds_client.h"
21 #include "cache.h"
22
23 #include <linux/ceph/ceph_features.h>
24 #include <linux/ceph/decode.h>
25 #include <linux/ceph/mon_client.h>
26 #include <linux/ceph/auth.h>
27 #include <linux/ceph/debugfs.h>
28
29 /*
30  * Ceph superblock operations
31  *
32  * Handle the basics of mounting, unmounting.
33  */
34
35 /*
36  * super ops
37  */
38 static void ceph_put_super(struct super_block *s)
39 {
40         struct ceph_fs_client *fsc = ceph_sb_to_client(s);
41
42         dout("put_super\n");
43         ceph_mdsc_close_sessions(fsc->mdsc);
44 }
45
46 static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
47 {
48         struct ceph_fs_client *fsc = ceph_inode_to_client(d_inode(dentry));
49         struct ceph_mon_client *monc = &fsc->client->monc;
50         struct ceph_statfs st;
51         u64 fsid;
52         int err;
53         u64 data_pool;
54
55         if (fsc->mdsc->mdsmap->m_num_data_pg_pools == 1) {
56                 data_pool = fsc->mdsc->mdsmap->m_data_pg_pools[0];
57         } else {
58                 data_pool = CEPH_NOPOOL;
59         }
60
61         dout("statfs\n");
62         err = ceph_monc_do_statfs(monc, data_pool, &st);
63         if (err < 0)
64                 return err;
65
66         /* fill in kstatfs */
67         buf->f_type = CEPH_SUPER_MAGIC;  /* ?? */
68
69         /*
70          * express utilization in terms of large blocks to avoid
71          * overflow on 32-bit machines.
72          *
73          * NOTE: for the time being, we make bsize == frsize to humor
74          * not-yet-ancient versions of glibc that are broken.
75          * Someday, we will probably want to report a real block
76          * size...  whatever that may mean for a network file system!
77          */
78         buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
79         buf->f_frsize = 1 << CEPH_BLOCK_SHIFT;
80
81         /*
82          * By default use root quota for stats; fallback to overall filesystem
83          * usage if using 'noquotadf' mount option or if the root dir doesn't
84          * have max_bytes quota set.
85          */
86         if (ceph_test_mount_opt(fsc, NOQUOTADF) ||
87             !ceph_quota_update_statfs(fsc, buf)) {
88                 buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
89                 buf->f_bfree = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
90                 buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
91         }
92
93         buf->f_files = le64_to_cpu(st.num_objects);
94         buf->f_ffree = -1;
95         buf->f_namelen = NAME_MAX;
96
97         /* Must convert the fsid, for consistent values across arches */
98         mutex_lock(&monc->mutex);
99         fsid = le64_to_cpu(*(__le64 *)(&monc->monmap->fsid)) ^
100                le64_to_cpu(*((__le64 *)&monc->monmap->fsid + 1));
101         mutex_unlock(&monc->mutex);
102
103         buf->f_fsid.val[0] = fsid & 0xffffffff;
104         buf->f_fsid.val[1] = fsid >> 32;
105
106         return 0;
107 }
108
109
110 static int ceph_sync_fs(struct super_block *sb, int wait)
111 {
112         struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
113
114         if (!wait) {
115                 dout("sync_fs (non-blocking)\n");
116                 ceph_flush_dirty_caps(fsc->mdsc);
117                 dout("sync_fs (non-blocking) done\n");
118                 return 0;
119         }
120
121         dout("sync_fs (blocking)\n");
122         ceph_osdc_sync(&fsc->client->osdc);
123         ceph_mdsc_sync(fsc->mdsc);
124         dout("sync_fs (blocking) done\n");
125         return 0;
126 }
127
128 /*
129  * mount options
130  */
131 enum {
132         Opt_wsize,
133         Opt_rsize,
134         Opt_rasize,
135         Opt_caps_wanted_delay_min,
136         Opt_caps_wanted_delay_max,
137         Opt_caps_max,
138         Opt_readdir_max_entries,
139         Opt_readdir_max_bytes,
140         Opt_congestion_kb,
141         Opt_last_int,
142         /* int args above */
143         Opt_snapdirname,
144         Opt_mds_namespace,
145         Opt_fscache_uniq,
146         Opt_recover_session,
147         Opt_last_string,
148         /* string args above */
149         Opt_dirstat,
150         Opt_nodirstat,
151         Opt_rbytes,
152         Opt_norbytes,
153         Opt_asyncreaddir,
154         Opt_noasyncreaddir,
155         Opt_dcache,
156         Opt_nodcache,
157         Opt_ino32,
158         Opt_noino32,
159         Opt_fscache,
160         Opt_nofscache,
161         Opt_poolperm,
162         Opt_nopoolperm,
163         Opt_require_active_mds,
164         Opt_norequire_active_mds,
165 #ifdef CONFIG_CEPH_FS_POSIX_ACL
166         Opt_acl,
167 #endif
168         Opt_noacl,
169         Opt_quotadf,
170         Opt_noquotadf,
171         Opt_copyfrom,
172         Opt_nocopyfrom,
173 };
174
175 static match_table_t fsopt_tokens = {
176         {Opt_wsize, "wsize=%d"},
177         {Opt_rsize, "rsize=%d"},
178         {Opt_rasize, "rasize=%d"},
179         {Opt_caps_wanted_delay_min, "caps_wanted_delay_min=%d"},
180         {Opt_caps_wanted_delay_max, "caps_wanted_delay_max=%d"},
181         {Opt_caps_max, "caps_max=%d"},
182         {Opt_readdir_max_entries, "readdir_max_entries=%d"},
183         {Opt_readdir_max_bytes, "readdir_max_bytes=%d"},
184         {Opt_congestion_kb, "write_congestion_kb=%d"},
185         /* int args above */
186         {Opt_snapdirname, "snapdirname=%s"},
187         {Opt_mds_namespace, "mds_namespace=%s"},
188         {Opt_recover_session, "recover_session=%s"},
189         {Opt_fscache_uniq, "fsc=%s"},
190         /* string args above */
191         {Opt_dirstat, "dirstat"},
192         {Opt_nodirstat, "nodirstat"},
193         {Opt_rbytes, "rbytes"},
194         {Opt_norbytes, "norbytes"},
195         {Opt_asyncreaddir, "asyncreaddir"},
196         {Opt_noasyncreaddir, "noasyncreaddir"},
197         {Opt_dcache, "dcache"},
198         {Opt_nodcache, "nodcache"},
199         {Opt_ino32, "ino32"},
200         {Opt_noino32, "noino32"},
201         {Opt_fscache, "fsc"},
202         {Opt_nofscache, "nofsc"},
203         {Opt_poolperm, "poolperm"},
204         {Opt_nopoolperm, "nopoolperm"},
205         {Opt_require_active_mds, "require_active_mds"},
206         {Opt_norequire_active_mds, "norequire_active_mds"},
207 #ifdef CONFIG_CEPH_FS_POSIX_ACL
208         {Opt_acl, "acl"},
209 #endif
210         {Opt_noacl, "noacl"},
211         {Opt_quotadf, "quotadf"},
212         {Opt_noquotadf, "noquotadf"},
213         {Opt_copyfrom, "copyfrom"},
214         {Opt_nocopyfrom, "nocopyfrom"},
215         {-1, NULL}
216 };
217
218 static int parse_fsopt_token(char *c, void *private)
219 {
220         struct ceph_mount_options *fsopt = private;
221         substring_t argstr[MAX_OPT_ARGS];
222         int token, intval, ret;
223
224         token = match_token((char *)c, fsopt_tokens, argstr);
225         if (token < 0)
226                 return -EINVAL;
227
228         if (token < Opt_last_int) {
229                 ret = match_int(&argstr[0], &intval);
230                 if (ret < 0) {
231                         pr_err("bad option arg (not int) at '%s'\n", c);
232                         return ret;
233                 }
234                 dout("got int token %d val %d\n", token, intval);
235         } else if (token > Opt_last_int && token < Opt_last_string) {
236                 dout("got string token %d val %s\n", token,
237                      argstr[0].from);
238         } else {
239                 dout("got token %d\n", token);
240         }
241
242         switch (token) {
243         case Opt_snapdirname:
244                 kfree(fsopt->snapdir_name);
245                 fsopt->snapdir_name = kstrndup(argstr[0].from,
246                                                argstr[0].to-argstr[0].from,
247                                                GFP_KERNEL);
248                 if (!fsopt->snapdir_name)
249                         return -ENOMEM;
250                 break;
251         case Opt_mds_namespace:
252                 kfree(fsopt->mds_namespace);
253                 fsopt->mds_namespace = kstrndup(argstr[0].from,
254                                                 argstr[0].to-argstr[0].from,
255                                                 GFP_KERNEL);
256                 if (!fsopt->mds_namespace)
257                         return -ENOMEM;
258                 break;
259         case Opt_recover_session:
260                 if (!strncmp(argstr[0].from, "no",
261                              argstr[0].to - argstr[0].from)) {
262                         fsopt->flags &= ~CEPH_MOUNT_OPT_CLEANRECOVER;
263                 } else if (!strncmp(argstr[0].from, "clean",
264                                     argstr[0].to - argstr[0].from)) {
265                         fsopt->flags |= CEPH_MOUNT_OPT_CLEANRECOVER;
266                 } else {
267                         return -EINVAL;
268                 }
269                 break;
270         case Opt_fscache_uniq:
271                 kfree(fsopt->fscache_uniq);
272                 fsopt->fscache_uniq = kstrndup(argstr[0].from,
273                                                argstr[0].to-argstr[0].from,
274                                                GFP_KERNEL);
275                 if (!fsopt->fscache_uniq)
276                         return -ENOMEM;
277                 fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
278                 break;
279                 /* misc */
280         case Opt_wsize:
281                 if (intval < (int)PAGE_SIZE || intval > CEPH_MAX_WRITE_SIZE)
282                         return -EINVAL;
283                 fsopt->wsize = ALIGN(intval, PAGE_SIZE);
284                 break;
285         case Opt_rsize:
286                 if (intval < (int)PAGE_SIZE || intval > CEPH_MAX_READ_SIZE)
287                         return -EINVAL;
288                 fsopt->rsize = ALIGN(intval, PAGE_SIZE);
289                 break;
290         case Opt_rasize:
291                 if (intval < 0)
292                         return -EINVAL;
293                 fsopt->rasize = ALIGN(intval, PAGE_SIZE);
294                 break;
295         case Opt_caps_wanted_delay_min:
296                 if (intval < 1)
297                         return -EINVAL;
298                 fsopt->caps_wanted_delay_min = intval;
299                 break;
300         case Opt_caps_wanted_delay_max:
301                 if (intval < 1)
302                         return -EINVAL;
303                 fsopt->caps_wanted_delay_max = intval;
304                 break;
305         case Opt_caps_max:
306                 if (intval < 0)
307                         return -EINVAL;
308                 fsopt->caps_max = intval;
309                 break;
310         case Opt_readdir_max_entries:
311                 if (intval < 1)
312                         return -EINVAL;
313                 fsopt->max_readdir = intval;
314                 break;
315         case Opt_readdir_max_bytes:
316                 if (intval < (int)PAGE_SIZE && intval != 0)
317                         return -EINVAL;
318                 fsopt->max_readdir_bytes = intval;
319                 break;
320         case Opt_congestion_kb:
321                 if (intval < 1024) /* at least 1M */
322                         return -EINVAL;
323                 fsopt->congestion_kb = intval;
324                 break;
325         case Opt_dirstat:
326                 fsopt->flags |= CEPH_MOUNT_OPT_DIRSTAT;
327                 break;
328         case Opt_nodirstat:
329                 fsopt->flags &= ~CEPH_MOUNT_OPT_DIRSTAT;
330                 break;
331         case Opt_rbytes:
332                 fsopt->flags |= CEPH_MOUNT_OPT_RBYTES;
333                 break;
334         case Opt_norbytes:
335                 fsopt->flags &= ~CEPH_MOUNT_OPT_RBYTES;
336                 break;
337         case Opt_asyncreaddir:
338                 fsopt->flags &= ~CEPH_MOUNT_OPT_NOASYNCREADDIR;
339                 break;
340         case Opt_noasyncreaddir:
341                 fsopt->flags |= CEPH_MOUNT_OPT_NOASYNCREADDIR;
342                 break;
343         case Opt_dcache:
344                 fsopt->flags |= CEPH_MOUNT_OPT_DCACHE;
345                 break;
346         case Opt_nodcache:
347                 fsopt->flags &= ~CEPH_MOUNT_OPT_DCACHE;
348                 break;
349         case Opt_ino32:
350                 fsopt->flags |= CEPH_MOUNT_OPT_INO32;
351                 break;
352         case Opt_noino32:
353                 fsopt->flags &= ~CEPH_MOUNT_OPT_INO32;
354                 break;
355         case Opt_fscache:
356                 fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
357                 kfree(fsopt->fscache_uniq);
358                 fsopt->fscache_uniq = NULL;
359                 break;
360         case Opt_nofscache:
361                 fsopt->flags &= ~CEPH_MOUNT_OPT_FSCACHE;
362                 kfree(fsopt->fscache_uniq);
363                 fsopt->fscache_uniq = NULL;
364                 break;
365         case Opt_poolperm:
366                 fsopt->flags &= ~CEPH_MOUNT_OPT_NOPOOLPERM;
367                 break;
368         case Opt_nopoolperm:
369                 fsopt->flags |= CEPH_MOUNT_OPT_NOPOOLPERM;
370                 break;
371         case Opt_require_active_mds:
372                 fsopt->flags &= ~CEPH_MOUNT_OPT_MOUNTWAIT;
373                 break;
374         case Opt_norequire_active_mds:
375                 fsopt->flags |= CEPH_MOUNT_OPT_MOUNTWAIT;
376                 break;
377         case Opt_quotadf:
378                 fsopt->flags &= ~CEPH_MOUNT_OPT_NOQUOTADF;
379                 break;
380         case Opt_noquotadf:
381                 fsopt->flags |= CEPH_MOUNT_OPT_NOQUOTADF;
382                 break;
383         case Opt_copyfrom:
384                 fsopt->flags &= ~CEPH_MOUNT_OPT_NOCOPYFROM;
385                 break;
386         case Opt_nocopyfrom:
387                 fsopt->flags |= CEPH_MOUNT_OPT_NOCOPYFROM;
388                 break;
389 #ifdef CONFIG_CEPH_FS_POSIX_ACL
390         case Opt_acl:
391                 fsopt->sb_flags |= SB_POSIXACL;
392                 break;
393 #endif
394         case Opt_noacl:
395                 fsopt->sb_flags &= ~SB_POSIXACL;
396                 break;
397         default:
398                 BUG_ON(token);
399         }
400         return 0;
401 }
402
403 static void destroy_mount_options(struct ceph_mount_options *args)
404 {
405         dout("destroy_mount_options %p\n", args);
406         kfree(args->snapdir_name);
407         kfree(args->mds_namespace);
408         kfree(args->server_path);
409         kfree(args->fscache_uniq);
410         kfree(args);
411 }
412
413 static int strcmp_null(const char *s1, const char *s2)
414 {
415         if (!s1 && !s2)
416                 return 0;
417         if (s1 && !s2)
418                 return -1;
419         if (!s1 && s2)
420                 return 1;
421         return strcmp(s1, s2);
422 }
423
424 static int compare_mount_options(struct ceph_mount_options *new_fsopt,
425                                  struct ceph_options *new_opt,
426                                  struct ceph_fs_client *fsc)
427 {
428         struct ceph_mount_options *fsopt1 = new_fsopt;
429         struct ceph_mount_options *fsopt2 = fsc->mount_options;
430         int ofs = offsetof(struct ceph_mount_options, snapdir_name);
431         int ret;
432
433         ret = memcmp(fsopt1, fsopt2, ofs);
434         if (ret)
435                 return ret;
436
437         ret = strcmp_null(fsopt1->snapdir_name, fsopt2->snapdir_name);
438         if (ret)
439                 return ret;
440         ret = strcmp_null(fsopt1->mds_namespace, fsopt2->mds_namespace);
441         if (ret)
442                 return ret;
443         ret = strcmp_null(fsopt1->server_path, fsopt2->server_path);
444         if (ret)
445                 return ret;
446         ret = strcmp_null(fsopt1->fscache_uniq, fsopt2->fscache_uniq);
447         if (ret)
448                 return ret;
449
450         return ceph_compare_options(new_opt, fsc->client);
451 }
452
453 static int parse_mount_options(struct ceph_mount_options **pfsopt,
454                                struct ceph_options **popt,
455                                int flags, char *options,
456                                const char *dev_name)
457 {
458         struct ceph_mount_options *fsopt;
459         const char *dev_name_end;
460         int err;
461
462         if (!dev_name || !*dev_name)
463                 return -EINVAL;
464
465         fsopt = kzalloc(sizeof(*fsopt), GFP_KERNEL);
466         if (!fsopt)
467                 return -ENOMEM;
468
469         dout("parse_mount_options %p, dev_name '%s'\n", fsopt, dev_name);
470
471         fsopt->sb_flags = flags;
472         fsopt->flags = CEPH_MOUNT_OPT_DEFAULT;
473
474         fsopt->wsize = CEPH_MAX_WRITE_SIZE;
475         fsopt->rsize = CEPH_MAX_READ_SIZE;
476         fsopt->rasize = CEPH_RASIZE_DEFAULT;
477         fsopt->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
478         if (!fsopt->snapdir_name) {
479                 err = -ENOMEM;
480                 goto out;
481         }
482
483         fsopt->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
484         fsopt->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
485         fsopt->max_readdir = CEPH_MAX_READDIR_DEFAULT;
486         fsopt->max_readdir_bytes = CEPH_MAX_READDIR_BYTES_DEFAULT;
487         fsopt->congestion_kb = default_congestion_kb();
488
489         /*
490          * Distinguish the server list from the path in "dev_name".
491          * Internally we do not include the leading '/' in the path.
492          *
493          * "dev_name" will look like:
494          *     <server_spec>[,<server_spec>...]:[<path>]
495          * where
496          *     <server_spec> is <ip>[:<port>]
497          *     <path> is optional, but if present must begin with '/'
498          */
499         dev_name_end = strchr(dev_name, '/');
500         if (dev_name_end) {
501                 if (strlen(dev_name_end) > 1) {
502                         fsopt->server_path = kstrdup(dev_name_end, GFP_KERNEL);
503                         if (!fsopt->server_path) {
504                                 err = -ENOMEM;
505                                 goto out;
506                         }
507                 }
508         } else {
509                 dev_name_end = dev_name + strlen(dev_name);
510         }
511         err = -EINVAL;
512         dev_name_end--;         /* back up to ':' separator */
513         if (dev_name_end < dev_name || *dev_name_end != ':') {
514                 pr_err("device name is missing path (no : separator in %s)\n",
515                                 dev_name);
516                 goto out;
517         }
518         dout("device name '%.*s'\n", (int)(dev_name_end - dev_name), dev_name);
519         if (fsopt->server_path)
520                 dout("server path '%s'\n", fsopt->server_path);
521
522         *popt = ceph_parse_options(options, dev_name, dev_name_end,
523                                  parse_fsopt_token, (void *)fsopt);
524         if (IS_ERR(*popt)) {
525                 err = PTR_ERR(*popt);
526                 goto out;
527         }
528
529         /* success */
530         *pfsopt = fsopt;
531         return 0;
532
533 out:
534         destroy_mount_options(fsopt);
535         return err;
536 }
537
538 /**
539  * ceph_show_options - Show mount options in /proc/mounts
540  * @m: seq_file to write to
541  * @root: root of that (sub)tree
542  */
543 static int ceph_show_options(struct seq_file *m, struct dentry *root)
544 {
545         struct ceph_fs_client *fsc = ceph_sb_to_client(root->d_sb);
546         struct ceph_mount_options *fsopt = fsc->mount_options;
547         size_t pos;
548         int ret;
549
550         /* a comma between MNT/MS and client options */
551         seq_putc(m, ',');
552         pos = m->count;
553
554         ret = ceph_print_client_options(m, fsc->client, false);
555         if (ret)
556                 return ret;
557
558         /* retract our comma if no client options */
559         if (m->count == pos)
560                 m->count--;
561
562         if (fsopt->flags & CEPH_MOUNT_OPT_DIRSTAT)
563                 seq_puts(m, ",dirstat");
564         if ((fsopt->flags & CEPH_MOUNT_OPT_RBYTES))
565                 seq_puts(m, ",rbytes");
566         if (fsopt->flags & CEPH_MOUNT_OPT_NOASYNCREADDIR)
567                 seq_puts(m, ",noasyncreaddir");
568         if ((fsopt->flags & CEPH_MOUNT_OPT_DCACHE) == 0)
569                 seq_puts(m, ",nodcache");
570         if (fsopt->flags & CEPH_MOUNT_OPT_INO32)
571                 seq_puts(m, ",ino32");
572         if (fsopt->flags & CEPH_MOUNT_OPT_FSCACHE) {
573                 seq_show_option(m, "fsc", fsopt->fscache_uniq);
574         }
575         if (fsopt->flags & CEPH_MOUNT_OPT_NOPOOLPERM)
576                 seq_puts(m, ",nopoolperm");
577         if (fsopt->flags & CEPH_MOUNT_OPT_NOQUOTADF)
578                 seq_puts(m, ",noquotadf");
579
580 #ifdef CONFIG_CEPH_FS_POSIX_ACL
581         if (fsopt->sb_flags & SB_POSIXACL)
582                 seq_puts(m, ",acl");
583         else
584                 seq_puts(m, ",noacl");
585 #endif
586
587         if ((fsopt->flags & CEPH_MOUNT_OPT_NOCOPYFROM) == 0)
588                 seq_puts(m, ",copyfrom");
589
590         if (fsopt->mds_namespace)
591                 seq_show_option(m, "mds_namespace", fsopt->mds_namespace);
592
593         if (fsopt->flags & CEPH_MOUNT_OPT_CLEANRECOVER)
594                 seq_show_option(m, "recover_session", "clean");
595
596         if (fsopt->wsize != CEPH_MAX_WRITE_SIZE)
597                 seq_printf(m, ",wsize=%d", fsopt->wsize);
598         if (fsopt->rsize != CEPH_MAX_READ_SIZE)
599                 seq_printf(m, ",rsize=%d", fsopt->rsize);
600         if (fsopt->rasize != CEPH_RASIZE_DEFAULT)
601                 seq_printf(m, ",rasize=%d", fsopt->rasize);
602         if (fsopt->congestion_kb != default_congestion_kb())
603                 seq_printf(m, ",write_congestion_kb=%d", fsopt->congestion_kb);
604         if (fsopt->caps_max)
605                 seq_printf(m, ",caps_max=%d", fsopt->caps_max);
606         if (fsopt->caps_wanted_delay_min != CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT)
607                 seq_printf(m, ",caps_wanted_delay_min=%d",
608                          fsopt->caps_wanted_delay_min);
609         if (fsopt->caps_wanted_delay_max != CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT)
610                 seq_printf(m, ",caps_wanted_delay_max=%d",
611                            fsopt->caps_wanted_delay_max);
612         if (fsopt->max_readdir != CEPH_MAX_READDIR_DEFAULT)
613                 seq_printf(m, ",readdir_max_entries=%d", fsopt->max_readdir);
614         if (fsopt->max_readdir_bytes != CEPH_MAX_READDIR_BYTES_DEFAULT)
615                 seq_printf(m, ",readdir_max_bytes=%d", fsopt->max_readdir_bytes);
616         if (strcmp(fsopt->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
617                 seq_show_option(m, "snapdirname", fsopt->snapdir_name);
618
619         return 0;
620 }
621
622 /*
623  * handle any mon messages the standard library doesn't understand.
624  * return error if we don't either.
625  */
626 static int extra_mon_dispatch(struct ceph_client *client, struct ceph_msg *msg)
627 {
628         struct ceph_fs_client *fsc = client->private;
629         int type = le16_to_cpu(msg->hdr.type);
630
631         switch (type) {
632         case CEPH_MSG_MDS_MAP:
633                 ceph_mdsc_handle_mdsmap(fsc->mdsc, msg);
634                 return 0;
635         case CEPH_MSG_FS_MAP_USER:
636                 ceph_mdsc_handle_fsmap(fsc->mdsc, msg);
637                 return 0;
638         default:
639                 return -1;
640         }
641 }
642
643 /*
644  * create a new fs client
645  *
646  * Success or not, this function consumes @fsopt and @opt.
647  */
648 static struct ceph_fs_client *create_fs_client(struct ceph_mount_options *fsopt,
649                                         struct ceph_options *opt)
650 {
651         struct ceph_fs_client *fsc;
652         int page_count;
653         size_t size;
654         int err;
655
656         fsc = kzalloc(sizeof(*fsc), GFP_KERNEL);
657         if (!fsc) {
658                 err = -ENOMEM;
659                 goto fail;
660         }
661
662         fsc->client = ceph_create_client(opt, fsc);
663         if (IS_ERR(fsc->client)) {
664                 err = PTR_ERR(fsc->client);
665                 goto fail;
666         }
667         opt = NULL; /* fsc->client now owns this */
668
669         fsc->client->extra_mon_dispatch = extra_mon_dispatch;
670         ceph_set_opt(fsc->client, ABORT_ON_FULL);
671
672         if (!fsopt->mds_namespace) {
673                 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
674                                    0, true);
675         } else {
676                 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_FSMAP,
677                                    0, false);
678         }
679
680         fsc->mount_options = fsopt;
681
682         fsc->sb = NULL;
683         fsc->mount_state = CEPH_MOUNT_MOUNTING;
684         fsc->filp_gen = 1;
685
686         atomic_long_set(&fsc->writeback_count, 0);
687
688         err = -ENOMEM;
689         /*
690          * The number of concurrent works can be high but they don't need
691          * to be processed in parallel, limit concurrency.
692          */
693         fsc->inode_wq = alloc_workqueue("ceph-inode", WQ_UNBOUND, 0);
694         if (!fsc->inode_wq)
695                 goto fail_client;
696         fsc->cap_wq = alloc_workqueue("ceph-cap", 0, 1);
697         if (!fsc->cap_wq)
698                 goto fail_inode_wq;
699
700         /* set up mempools */
701         err = -ENOMEM;
702         page_count = fsc->mount_options->wsize >> PAGE_SHIFT;
703         size = sizeof (struct page *) * (page_count ? page_count : 1);
704         fsc->wb_pagevec_pool = mempool_create_kmalloc_pool(10, size);
705         if (!fsc->wb_pagevec_pool)
706                 goto fail_cap_wq;
707
708         return fsc;
709
710 fail_cap_wq:
711         destroy_workqueue(fsc->cap_wq);
712 fail_inode_wq:
713         destroy_workqueue(fsc->inode_wq);
714 fail_client:
715         ceph_destroy_client(fsc->client);
716 fail:
717         kfree(fsc);
718         if (opt)
719                 ceph_destroy_options(opt);
720         destroy_mount_options(fsopt);
721         return ERR_PTR(err);
722 }
723
724 static void flush_fs_workqueues(struct ceph_fs_client *fsc)
725 {
726         flush_workqueue(fsc->inode_wq);
727         flush_workqueue(fsc->cap_wq);
728 }
729
730 static void destroy_fs_client(struct ceph_fs_client *fsc)
731 {
732         dout("destroy_fs_client %p\n", fsc);
733
734         ceph_mdsc_destroy(fsc);
735         destroy_workqueue(fsc->inode_wq);
736         destroy_workqueue(fsc->cap_wq);
737
738         mempool_destroy(fsc->wb_pagevec_pool);
739
740         destroy_mount_options(fsc->mount_options);
741
742         ceph_destroy_client(fsc->client);
743
744         kfree(fsc);
745         dout("destroy_fs_client %p done\n", fsc);
746 }
747
748 /*
749  * caches
750  */
751 struct kmem_cache *ceph_inode_cachep;
752 struct kmem_cache *ceph_cap_cachep;
753 struct kmem_cache *ceph_cap_flush_cachep;
754 struct kmem_cache *ceph_dentry_cachep;
755 struct kmem_cache *ceph_file_cachep;
756 struct kmem_cache *ceph_dir_file_cachep;
757
758 static void ceph_inode_init_once(void *foo)
759 {
760         struct ceph_inode_info *ci = foo;
761         inode_init_once(&ci->vfs_inode);
762 }
763
764 static int __init init_caches(void)
765 {
766         int error = -ENOMEM;
767
768         ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
769                                       sizeof(struct ceph_inode_info),
770                                       __alignof__(struct ceph_inode_info),
771                                       SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
772                                       SLAB_ACCOUNT, ceph_inode_init_once);
773         if (!ceph_inode_cachep)
774                 return -ENOMEM;
775
776         ceph_cap_cachep = KMEM_CACHE(ceph_cap, SLAB_MEM_SPREAD);
777         if (!ceph_cap_cachep)
778                 goto bad_cap;
779         ceph_cap_flush_cachep = KMEM_CACHE(ceph_cap_flush,
780                                            SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
781         if (!ceph_cap_flush_cachep)
782                 goto bad_cap_flush;
783
784         ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
785                                         SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
786         if (!ceph_dentry_cachep)
787                 goto bad_dentry;
788
789         ceph_file_cachep = KMEM_CACHE(ceph_file_info, SLAB_MEM_SPREAD);
790         if (!ceph_file_cachep)
791                 goto bad_file;
792
793         ceph_dir_file_cachep = KMEM_CACHE(ceph_dir_file_info, SLAB_MEM_SPREAD);
794         if (!ceph_dir_file_cachep)
795                 goto bad_dir_file;
796
797         error = ceph_fscache_register();
798         if (error)
799                 goto bad_fscache;
800
801         return 0;
802
803 bad_fscache:
804         kmem_cache_destroy(ceph_dir_file_cachep);
805 bad_dir_file:
806         kmem_cache_destroy(ceph_file_cachep);
807 bad_file:
808         kmem_cache_destroy(ceph_dentry_cachep);
809 bad_dentry:
810         kmem_cache_destroy(ceph_cap_flush_cachep);
811 bad_cap_flush:
812         kmem_cache_destroy(ceph_cap_cachep);
813 bad_cap:
814         kmem_cache_destroy(ceph_inode_cachep);
815         return error;
816 }
817
818 static void destroy_caches(void)
819 {
820         /*
821          * Make sure all delayed rcu free inodes are flushed before we
822          * destroy cache.
823          */
824         rcu_barrier();
825
826         kmem_cache_destroy(ceph_inode_cachep);
827         kmem_cache_destroy(ceph_cap_cachep);
828         kmem_cache_destroy(ceph_cap_flush_cachep);
829         kmem_cache_destroy(ceph_dentry_cachep);
830         kmem_cache_destroy(ceph_file_cachep);
831         kmem_cache_destroy(ceph_dir_file_cachep);
832
833         ceph_fscache_unregister();
834 }
835
836
837 /*
838  * ceph_umount_begin - initiate forced umount.  Tear down down the
839  * mount, skipping steps that may hang while waiting for server(s).
840  */
841 static void ceph_umount_begin(struct super_block *sb)
842 {
843         struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
844
845         dout("ceph_umount_begin - starting forced umount\n");
846         if (!fsc)
847                 return;
848         fsc->mount_state = CEPH_MOUNT_SHUTDOWN;
849         ceph_osdc_abort_requests(&fsc->client->osdc, -EIO);
850         ceph_mdsc_force_umount(fsc->mdsc);
851         fsc->filp_gen++; // invalidate open files
852 }
853
854 static int ceph_remount(struct super_block *sb, int *flags, char *data)
855 {
856         sync_filesystem(sb);
857         return 0;
858 }
859
860 static const struct super_operations ceph_super_ops = {
861         .alloc_inode    = ceph_alloc_inode,
862         .free_inode     = ceph_free_inode,
863         .write_inode    = ceph_write_inode,
864         .drop_inode     = generic_delete_inode,
865         .evict_inode    = ceph_evict_inode,
866         .sync_fs        = ceph_sync_fs,
867         .put_super      = ceph_put_super,
868         .remount_fs     = ceph_remount,
869         .show_options   = ceph_show_options,
870         .statfs         = ceph_statfs,
871         .umount_begin   = ceph_umount_begin,
872 };
873
874 /*
875  * Bootstrap mount by opening the root directory.  Note the mount
876  * @started time from caller, and time out if this takes too long.
877  */
878 static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
879                                        const char *path,
880                                        unsigned long started)
881 {
882         struct ceph_mds_client *mdsc = fsc->mdsc;
883         struct ceph_mds_request *req = NULL;
884         int err;
885         struct dentry *root;
886
887         /* open dir */
888         dout("open_root_inode opening '%s'\n", path);
889         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
890         if (IS_ERR(req))
891                 return ERR_CAST(req);
892         req->r_path1 = kstrdup(path, GFP_NOFS);
893         if (!req->r_path1) {
894                 root = ERR_PTR(-ENOMEM);
895                 goto out;
896         }
897
898         req->r_ino1.ino = CEPH_INO_ROOT;
899         req->r_ino1.snap = CEPH_NOSNAP;
900         req->r_started = started;
901         req->r_timeout = fsc->client->options->mount_timeout;
902         req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
903         req->r_num_caps = 2;
904         err = ceph_mdsc_do_request(mdsc, NULL, req);
905         if (err == 0) {
906                 struct inode *inode = req->r_target_inode;
907                 req->r_target_inode = NULL;
908                 dout("open_root_inode success\n");
909                 root = d_make_root(inode);
910                 if (!root) {
911                         root = ERR_PTR(-ENOMEM);
912                         goto out;
913                 }
914                 dout("open_root_inode success, root dentry is %p\n", root);
915         } else {
916                 root = ERR_PTR(err);
917         }
918 out:
919         ceph_mdsc_put_request(req);
920         return root;
921 }
922
923
924
925
926 /*
927  * mount: join the ceph cluster, and open root directory.
928  */
929 static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc)
930 {
931         int err;
932         unsigned long started = jiffies;  /* note the start time */
933         struct dentry *root;
934
935         dout("mount start %p\n", fsc);
936         mutex_lock(&fsc->client->mount_mutex);
937
938         if (!fsc->sb->s_root) {
939                 const char *path;
940                 err = __ceph_open_session(fsc->client, started);
941                 if (err < 0)
942                         goto out;
943
944                 /* setup fscache */
945                 if (fsc->mount_options->flags & CEPH_MOUNT_OPT_FSCACHE) {
946                         err = ceph_fscache_register_fs(fsc);
947                         if (err < 0)
948                                 goto out;
949                 }
950
951                 if (!fsc->mount_options->server_path) {
952                         path = "";
953                         dout("mount opening path \\t\n");
954                 } else {
955                         path = fsc->mount_options->server_path + 1;
956                         dout("mount opening path %s\n", path);
957                 }
958
959                 ceph_fs_debugfs_init(fsc);
960
961                 root = open_root_dentry(fsc, path, started);
962                 if (IS_ERR(root)) {
963                         err = PTR_ERR(root);
964                         goto out;
965                 }
966                 fsc->sb->s_root = dget(root);
967         } else {
968                 root = dget(fsc->sb->s_root);
969         }
970
971         fsc->mount_state = CEPH_MOUNT_MOUNTED;
972         dout("mount success\n");
973         mutex_unlock(&fsc->client->mount_mutex);
974         return root;
975
976 out:
977         mutex_unlock(&fsc->client->mount_mutex);
978         return ERR_PTR(err);
979 }
980
981 static int ceph_set_super(struct super_block *s, void *data)
982 {
983         struct ceph_fs_client *fsc = data;
984         int ret;
985
986         dout("set_super %p data %p\n", s, data);
987
988         s->s_flags = fsc->mount_options->sb_flags;
989         s->s_maxbytes = MAX_LFS_FILESIZE;
990
991         s->s_xattr = ceph_xattr_handlers;
992         s->s_fs_info = fsc;
993         fsc->sb = s;
994         fsc->max_file_size = 1ULL << 40; /* temp value until we get mdsmap */
995
996         s->s_op = &ceph_super_ops;
997         s->s_d_op = &ceph_dentry_ops;
998         s->s_export_op = &ceph_export_ops;
999
1000         s->s_time_gran = 1;
1001         s->s_time_min = 0;
1002         s->s_time_max = U32_MAX;
1003
1004         ret = set_anon_super(s, NULL);  /* what is that second arg for? */
1005         if (ret != 0)
1006                 goto fail;
1007
1008         return ret;
1009
1010 fail:
1011         s->s_fs_info = NULL;
1012         fsc->sb = NULL;
1013         return ret;
1014 }
1015
1016 /*
1017  * share superblock if same fs AND options
1018  */
1019 static int ceph_compare_super(struct super_block *sb, void *data)
1020 {
1021         struct ceph_fs_client *new = data;
1022         struct ceph_mount_options *fsopt = new->mount_options;
1023         struct ceph_options *opt = new->client->options;
1024         struct ceph_fs_client *other = ceph_sb_to_client(sb);
1025
1026         dout("ceph_compare_super %p\n", sb);
1027
1028         if (compare_mount_options(fsopt, opt, other)) {
1029                 dout("monitor(s)/mount options don't match\n");
1030                 return 0;
1031         }
1032         if ((opt->flags & CEPH_OPT_FSID) &&
1033             ceph_fsid_compare(&opt->fsid, &other->client->fsid)) {
1034                 dout("fsid doesn't match\n");
1035                 return 0;
1036         }
1037         if (fsopt->sb_flags != other->mount_options->sb_flags) {
1038                 dout("flags differ\n");
1039                 return 0;
1040         }
1041         return 1;
1042 }
1043
1044 /*
1045  * construct our own bdi so we can control readahead, etc.
1046  */
1047 static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
1048
1049 static int ceph_setup_bdi(struct super_block *sb, struct ceph_fs_client *fsc)
1050 {
1051         int err;
1052
1053         err = super_setup_bdi_name(sb, "ceph-%ld",
1054                                    atomic_long_inc_return(&bdi_seq));
1055         if (err)
1056                 return err;
1057
1058         /* set ra_pages based on rasize mount option? */
1059         sb->s_bdi->ra_pages = fsc->mount_options->rasize >> PAGE_SHIFT;
1060
1061         /* set io_pages based on max osd read size */
1062         sb->s_bdi->io_pages = fsc->mount_options->rsize >> PAGE_SHIFT;
1063
1064         return 0;
1065 }
1066
1067 static struct dentry *ceph_mount(struct file_system_type *fs_type,
1068                        int flags, const char *dev_name, void *data)
1069 {
1070         struct super_block *sb;
1071         struct ceph_fs_client *fsc;
1072         struct dentry *res;
1073         int err;
1074         int (*compare_super)(struct super_block *, void *) = ceph_compare_super;
1075         struct ceph_mount_options *fsopt = NULL;
1076         struct ceph_options *opt = NULL;
1077
1078         dout("ceph_mount\n");
1079
1080 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1081         flags |= SB_POSIXACL;
1082 #endif
1083         err = parse_mount_options(&fsopt, &opt, flags, data, dev_name);
1084         if (err < 0) {
1085                 res = ERR_PTR(err);
1086                 goto out_final;
1087         }
1088
1089         /* create client (which we may/may not use) */
1090         fsc = create_fs_client(fsopt, opt);
1091         if (IS_ERR(fsc)) {
1092                 res = ERR_CAST(fsc);
1093                 goto out_final;
1094         }
1095
1096         err = ceph_mdsc_init(fsc);
1097         if (err < 0) {
1098                 res = ERR_PTR(err);
1099                 goto out;
1100         }
1101
1102         if (ceph_test_opt(fsc->client, NOSHARE))
1103                 compare_super = NULL;
1104         sb = sget(fs_type, compare_super, ceph_set_super, flags, fsc);
1105         if (IS_ERR(sb)) {
1106                 res = ERR_CAST(sb);
1107                 goto out;
1108         }
1109
1110         if (ceph_sb_to_client(sb) != fsc) {
1111                 destroy_fs_client(fsc);
1112                 fsc = ceph_sb_to_client(sb);
1113                 dout("get_sb got existing client %p\n", fsc);
1114         } else {
1115                 dout("get_sb using new client %p\n", fsc);
1116                 err = ceph_setup_bdi(sb, fsc);
1117                 if (err < 0) {
1118                         res = ERR_PTR(err);
1119                         goto out_splat;
1120                 }
1121         }
1122
1123         res = ceph_real_mount(fsc);
1124         if (IS_ERR(res))
1125                 goto out_splat;
1126         dout("root %p inode %p ino %llx.%llx\n", res,
1127              d_inode(res), ceph_vinop(d_inode(res)));
1128         return res;
1129
1130 out_splat:
1131         ceph_mdsc_close_sessions(fsc->mdsc);
1132         deactivate_locked_super(sb);
1133         goto out_final;
1134
1135 out:
1136         destroy_fs_client(fsc);
1137 out_final:
1138         dout("ceph_mount fail %ld\n", PTR_ERR(res));
1139         return res;
1140 }
1141
1142 static void ceph_kill_sb(struct super_block *s)
1143 {
1144         struct ceph_fs_client *fsc = ceph_sb_to_client(s);
1145         dev_t dev = s->s_dev;
1146
1147         dout("kill_sb %p\n", s);
1148
1149         ceph_mdsc_pre_umount(fsc->mdsc);
1150         flush_fs_workqueues(fsc);
1151
1152         generic_shutdown_super(s);
1153
1154         fsc->client->extra_mon_dispatch = NULL;
1155         ceph_fs_debugfs_cleanup(fsc);
1156
1157         ceph_fscache_unregister_fs(fsc);
1158
1159         destroy_fs_client(fsc);
1160         free_anon_bdev(dev);
1161 }
1162
1163 static struct file_system_type ceph_fs_type = {
1164         .owner          = THIS_MODULE,
1165         .name           = "ceph",
1166         .mount          = ceph_mount,
1167         .kill_sb        = ceph_kill_sb,
1168         .fs_flags       = FS_RENAME_DOES_D_MOVE,
1169 };
1170 MODULE_ALIAS_FS("ceph");
1171
1172 int ceph_force_reconnect(struct super_block *sb)
1173 {
1174         struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1175         int err = 0;
1176
1177         ceph_umount_begin(sb);
1178
1179         /* Make sure all page caches get invalidated.
1180          * see remove_session_caps_cb() */
1181         flush_workqueue(fsc->inode_wq);
1182
1183         /* In case that we were blacklisted. This also reset
1184          * all mon/osd connections */
1185         ceph_reset_client_addr(fsc->client);
1186
1187         ceph_osdc_clear_abort_err(&fsc->client->osdc);
1188
1189         fsc->blacklisted = false;
1190         fsc->mount_state = CEPH_MOUNT_MOUNTED;
1191
1192         if (sb->s_root) {
1193                 err = __ceph_do_getattr(d_inode(sb->s_root), NULL,
1194                                         CEPH_STAT_CAP_INODE, true);
1195         }
1196         return err;
1197 }
1198
1199 static int __init init_ceph(void)
1200 {
1201         int ret = init_caches();
1202         if (ret)
1203                 goto out;
1204
1205         ceph_flock_init();
1206         ret = register_filesystem(&ceph_fs_type);
1207         if (ret)
1208                 goto out_caches;
1209
1210         pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
1211
1212         return 0;
1213
1214 out_caches:
1215         destroy_caches();
1216 out:
1217         return ret;
1218 }
1219
1220 static void __exit exit_ceph(void)
1221 {
1222         dout("exit_ceph\n");
1223         unregister_filesystem(&ceph_fs_type);
1224         destroy_caches();
1225 }
1226
1227 module_init(init_ceph);
1228 module_exit(exit_ceph);
1229
1230 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
1231 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
1232 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
1233 MODULE_DESCRIPTION("Ceph filesystem for Linux");
1234 MODULE_LICENSE("GPL");