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