ceph: new device mount syntax
[platform/kernel/linux-rpi.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/fs_context.h>
13 #include <linux/fs_parser.h>
14 #include <linux/sched.h>
15 #include <linux/seq_file.h>
16 #include <linux/slab.h>
17 #include <linux/statfs.h>
18 #include <linux/string.h>
19
20 #include "super.h"
21 #include "mds_client.h"
22 #include "cache.h"
23
24 #include <linux/ceph/ceph_features.h>
25 #include <linux/ceph/decode.h>
26 #include <linux/ceph/mon_client.h>
27 #include <linux/ceph/auth.h>
28 #include <linux/ceph/debugfs.h>
29
30 static DEFINE_SPINLOCK(ceph_fsc_lock);
31 static LIST_HEAD(ceph_fsc_list);
32
33 /*
34  * Ceph superblock operations
35  *
36  * Handle the basics of mounting, unmounting.
37  */
38
39 /*
40  * super ops
41  */
42 static void ceph_put_super(struct super_block *s)
43 {
44         struct ceph_fs_client *fsc = ceph_sb_to_client(s);
45
46         dout("put_super\n");
47         ceph_mdsc_close_sessions(fsc->mdsc);
48 }
49
50 static int ceph_statfs(struct dentry *dentry, struct kstatfs *buf)
51 {
52         struct ceph_fs_client *fsc = ceph_inode_to_client(d_inode(dentry));
53         struct ceph_mon_client *monc = &fsc->client->monc;
54         struct ceph_statfs st;
55         int i, err;
56         u64 data_pool;
57
58         if (fsc->mdsc->mdsmap->m_num_data_pg_pools == 1) {
59                 data_pool = fsc->mdsc->mdsmap->m_data_pg_pools[0];
60         } else {
61                 data_pool = CEPH_NOPOOL;
62         }
63
64         dout("statfs\n");
65         err = ceph_monc_do_statfs(monc, data_pool, &st);
66         if (err < 0)
67                 return err;
68
69         /* fill in kstatfs */
70         buf->f_type = CEPH_SUPER_MAGIC;  /* ?? */
71
72         /*
73          * express utilization in terms of large blocks to avoid
74          * overflow on 32-bit machines.
75          *
76          * NOTE: for the time being, we make bsize == frsize to humor
77          * not-yet-ancient versions of glibc that are broken.
78          * Someday, we will probably want to report a real block
79          * size...  whatever that may mean for a network file system!
80          */
81         buf->f_bsize = 1 << CEPH_BLOCK_SHIFT;
82         buf->f_frsize = 1 << CEPH_BLOCK_SHIFT;
83
84         /*
85          * By default use root quota for stats; fallback to overall filesystem
86          * usage if using 'noquotadf' mount option or if the root dir doesn't
87          * have max_bytes quota set.
88          */
89         if (ceph_test_mount_opt(fsc, NOQUOTADF) ||
90             !ceph_quota_update_statfs(fsc, buf)) {
91                 buf->f_blocks = le64_to_cpu(st.kb) >> (CEPH_BLOCK_SHIFT-10);
92                 buf->f_bfree = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
93                 buf->f_bavail = le64_to_cpu(st.kb_avail) >> (CEPH_BLOCK_SHIFT-10);
94         }
95
96         buf->f_files = le64_to_cpu(st.num_objects);
97         buf->f_ffree = -1;
98         buf->f_namelen = NAME_MAX;
99
100         /* Must convert the fsid, for consistent values across arches */
101         buf->f_fsid.val[0] = 0;
102         mutex_lock(&monc->mutex);
103         for (i = 0 ; i < sizeof(monc->monmap->fsid) / sizeof(__le32) ; ++i)
104                 buf->f_fsid.val[0] ^= le32_to_cpu(((__le32 *)&monc->monmap->fsid)[i]);
105         mutex_unlock(&monc->mutex);
106
107         /* fold the fs_cluster_id into the upper bits */
108         buf->f_fsid.val[1] = monc->fs_cluster_id;
109
110         return 0;
111 }
112
113 static int ceph_sync_fs(struct super_block *sb, int wait)
114 {
115         struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
116
117         if (!wait) {
118                 dout("sync_fs (non-blocking)\n");
119                 ceph_flush_dirty_caps(fsc->mdsc);
120                 dout("sync_fs (non-blocking) done\n");
121                 return 0;
122         }
123
124         dout("sync_fs (blocking)\n");
125         ceph_osdc_sync(&fsc->client->osdc);
126         ceph_mdsc_sync(fsc->mdsc);
127         dout("sync_fs (blocking) done\n");
128         return 0;
129 }
130
131 /*
132  * mount options
133  */
134 enum {
135         Opt_wsize,
136         Opt_rsize,
137         Opt_rasize,
138         Opt_caps_wanted_delay_min,
139         Opt_caps_wanted_delay_max,
140         Opt_caps_max,
141         Opt_readdir_max_entries,
142         Opt_readdir_max_bytes,
143         Opt_congestion_kb,
144         /* int args above */
145         Opt_snapdirname,
146         Opt_mds_namespace,
147         Opt_recover_session,
148         Opt_source,
149         Opt_mon_addr,
150         /* string args above */
151         Opt_dirstat,
152         Opt_rbytes,
153         Opt_asyncreaddir,
154         Opt_dcache,
155         Opt_ino32,
156         Opt_fscache,
157         Opt_poolperm,
158         Opt_require_active_mds,
159         Opt_acl,
160         Opt_quotadf,
161         Opt_copyfrom,
162         Opt_wsync,
163 };
164
165 enum ceph_recover_session_mode {
166         ceph_recover_session_no,
167         ceph_recover_session_clean
168 };
169
170 static const struct constant_table ceph_param_recover[] = {
171         { "no",         ceph_recover_session_no },
172         { "clean",      ceph_recover_session_clean },
173         {}
174 };
175
176 static const struct fs_parameter_spec ceph_mount_parameters[] = {
177         fsparam_flag_no ("acl",                         Opt_acl),
178         fsparam_flag_no ("asyncreaddir",                Opt_asyncreaddir),
179         fsparam_s32     ("caps_max",                    Opt_caps_max),
180         fsparam_u32     ("caps_wanted_delay_max",       Opt_caps_wanted_delay_max),
181         fsparam_u32     ("caps_wanted_delay_min",       Opt_caps_wanted_delay_min),
182         fsparam_u32     ("write_congestion_kb",         Opt_congestion_kb),
183         fsparam_flag_no ("copyfrom",                    Opt_copyfrom),
184         fsparam_flag_no ("dcache",                      Opt_dcache),
185         fsparam_flag_no ("dirstat",                     Opt_dirstat),
186         fsparam_flag_no ("fsc",                         Opt_fscache), // fsc|nofsc
187         fsparam_string  ("fsc",                         Opt_fscache), // fsc=...
188         fsparam_flag_no ("ino32",                       Opt_ino32),
189         fsparam_string  ("mds_namespace",               Opt_mds_namespace),
190         fsparam_flag_no ("poolperm",                    Opt_poolperm),
191         fsparam_flag_no ("quotadf",                     Opt_quotadf),
192         fsparam_u32     ("rasize",                      Opt_rasize),
193         fsparam_flag_no ("rbytes",                      Opt_rbytes),
194         fsparam_u32     ("readdir_max_bytes",           Opt_readdir_max_bytes),
195         fsparam_u32     ("readdir_max_entries",         Opt_readdir_max_entries),
196         fsparam_enum    ("recover_session",             Opt_recover_session, ceph_param_recover),
197         fsparam_flag_no ("require_active_mds",          Opt_require_active_mds),
198         fsparam_u32     ("rsize",                       Opt_rsize),
199         fsparam_string  ("snapdirname",                 Opt_snapdirname),
200         fsparam_string  ("source",                      Opt_source),
201         fsparam_string  ("mon_addr",                    Opt_mon_addr),
202         fsparam_u32     ("wsize",                       Opt_wsize),
203         fsparam_flag_no ("wsync",                       Opt_wsync),
204         {}
205 };
206
207 struct ceph_parse_opts_ctx {
208         struct ceph_options             *copts;
209         struct ceph_mount_options       *opts;
210 };
211
212 /*
213  * Remove adjacent slashes and then the trailing slash, unless it is
214  * the only remaining character.
215  *
216  * E.g. "//dir1////dir2///" --> "/dir1/dir2", "///" --> "/".
217  */
218 static void canonicalize_path(char *path)
219 {
220         int i, j = 0;
221
222         for (i = 0; path[i] != '\0'; i++) {
223                 if (path[i] != '/' || j < 1 || path[j - 1] != '/')
224                         path[j++] = path[i];
225         }
226
227         if (j > 1 && path[j - 1] == '/')
228                 j--;
229         path[j] = '\0';
230 }
231
232 /*
233  * Check if the mds namespace in ceph_mount_options matches
234  * the passed in namespace string. First time match (when
235  * ->mds_namespace is NULL) is treated specially, since
236  * ->mds_namespace needs to be initialized by the caller.
237  */
238 static int namespace_equals(struct ceph_mount_options *fsopt,
239                             const char *namespace, size_t len)
240 {
241         return !(fsopt->mds_namespace &&
242                  (strlen(fsopt->mds_namespace) != len ||
243                   strncmp(fsopt->mds_namespace, namespace, len)));
244 }
245
246 static int ceph_parse_old_source(const char *dev_name, const char *dev_name_end,
247                                  struct fs_context *fc)
248 {
249         int r;
250         struct ceph_parse_opts_ctx *pctx = fc->fs_private;
251         struct ceph_mount_options *fsopt = pctx->opts;
252
253         if (*dev_name_end != ':')
254                 return invalfc(fc, "separator ':' missing in source");
255
256         r = ceph_parse_mon_ips(dev_name, dev_name_end - dev_name,
257                                pctx->copts, fc->log.log, ',');
258         if (r)
259                 return r;
260
261         fsopt->new_dev_syntax = false;
262         return 0;
263 }
264
265 static int ceph_parse_new_source(const char *dev_name, const char *dev_name_end,
266                                  struct fs_context *fc)
267 {
268         size_t len;
269         struct ceph_fsid fsid;
270         struct ceph_parse_opts_ctx *pctx = fc->fs_private;
271         struct ceph_mount_options *fsopt = pctx->opts;
272         char *fsid_start, *fs_name_start;
273
274         if (*dev_name_end != '=') {
275                 dout("separator '=' missing in source");
276                 return -EINVAL;
277         }
278
279         fsid_start = strchr(dev_name, '@');
280         if (!fsid_start)
281                 return invalfc(fc, "missing cluster fsid");
282         ++fsid_start; /* start of cluster fsid */
283
284         fs_name_start = strchr(fsid_start, '.');
285         if (!fs_name_start)
286                 return invalfc(fc, "missing file system name");
287
288         if (ceph_parse_fsid(fsid_start, &fsid))
289                 return invalfc(fc, "Invalid FSID");
290
291         ++fs_name_start; /* start of file system name */
292         len = dev_name_end - fs_name_start;
293
294         if (!namespace_equals(fsopt, fs_name_start, len))
295                 return invalfc(fc, "Mismatching mds_namespace");
296         kfree(fsopt->mds_namespace);
297         fsopt->mds_namespace = kstrndup(fs_name_start, len, GFP_KERNEL);
298         if (!fsopt->mds_namespace)
299                 return -ENOMEM;
300         dout("file system (mds namespace) '%s'\n", fsopt->mds_namespace);
301
302         fsopt->new_dev_syntax = true;
303         return 0;
304 }
305
306 /*
307  * Parse the source parameter for new device format. Distinguish the device
308  * spec from the path. Try parsing new device format and fallback to old
309  * format if needed.
310  *
311  * New device syntax will looks like:
312  *     <device_spec>=/<path>
313  * where
314  *     <device_spec> is name@fsid.fsname
315  *     <path> is optional, but if present must begin with '/'
316  * (monitor addresses are passed via mount option)
317  *
318  * Old device syntax is:
319  *     <server_spec>[,<server_spec>...]:[<path>]
320  * where
321  *     <server_spec> is <ip>[:<port>]
322  *     <path> is optional, but if present must begin with '/'
323  */
324 static int ceph_parse_source(struct fs_parameter *param, struct fs_context *fc)
325 {
326         struct ceph_parse_opts_ctx *pctx = fc->fs_private;
327         struct ceph_mount_options *fsopt = pctx->opts;
328         char *dev_name = param->string, *dev_name_end;
329         int ret;
330
331         dout("%s '%s'\n", __func__, dev_name);
332         if (!dev_name || !*dev_name)
333                 return invalfc(fc, "Empty source");
334
335         dev_name_end = strchr(dev_name, '/');
336         if (dev_name_end) {
337                 /*
338                  * The server_path will include the whole chars from userland
339                  * including the leading '/'.
340                  */
341                 kfree(fsopt->server_path);
342                 fsopt->server_path = kstrdup(dev_name_end, GFP_KERNEL);
343                 if (!fsopt->server_path)
344                         return -ENOMEM;
345
346                 canonicalize_path(fsopt->server_path);
347         } else {
348                 dev_name_end = dev_name + strlen(dev_name);
349         }
350
351         dev_name_end--;         /* back up to separator */
352         if (dev_name_end < dev_name)
353                 return invalfc(fc, "Path missing in source");
354
355         dout("device name '%.*s'\n", (int)(dev_name_end - dev_name), dev_name);
356         if (fsopt->server_path)
357                 dout("server path '%s'\n", fsopt->server_path);
358
359         dout("trying new device syntax");
360         ret = ceph_parse_new_source(dev_name, dev_name_end, fc);
361         if (ret) {
362                 if (ret != -EINVAL)
363                         return ret;
364                 dout("trying old device syntax");
365                 ret = ceph_parse_old_source(dev_name, dev_name_end, fc);
366                 if (ret)
367                         return ret;
368         }
369
370         fc->source = param->string;
371         param->string = NULL;
372         return 0;
373 }
374
375 static int ceph_parse_mon_addr(struct fs_parameter *param,
376                                struct fs_context *fc)
377 {
378         struct ceph_parse_opts_ctx *pctx = fc->fs_private;
379         struct ceph_mount_options *fsopt = pctx->opts;
380
381         kfree(fsopt->mon_addr);
382         fsopt->mon_addr = param->string;
383         param->string = NULL;
384
385         return ceph_parse_mon_ips(fsopt->mon_addr, strlen(fsopt->mon_addr),
386                                   pctx->copts, fc->log.log, '/');
387 }
388
389 static int ceph_parse_mount_param(struct fs_context *fc,
390                                   struct fs_parameter *param)
391 {
392         struct ceph_parse_opts_ctx *pctx = fc->fs_private;
393         struct ceph_mount_options *fsopt = pctx->opts;
394         struct fs_parse_result result;
395         unsigned int mode;
396         int token, ret;
397
398         ret = ceph_parse_param(param, pctx->copts, fc->log.log);
399         if (ret != -ENOPARAM)
400                 return ret;
401
402         token = fs_parse(fc, ceph_mount_parameters, param, &result);
403         dout("%s fs_parse '%s' token %d\n", __func__, param->key, token);
404         if (token < 0)
405                 return token;
406
407         switch (token) {
408         case Opt_snapdirname:
409                 kfree(fsopt->snapdir_name);
410                 fsopt->snapdir_name = param->string;
411                 param->string = NULL;
412                 break;
413         case Opt_mds_namespace:
414                 if (!namespace_equals(fsopt, param->string, strlen(param->string)))
415                         return invalfc(fc, "Mismatching mds_namespace");
416                 kfree(fsopt->mds_namespace);
417                 fsopt->mds_namespace = param->string;
418                 param->string = NULL;
419                 break;
420         case Opt_recover_session:
421                 mode = result.uint_32;
422                 if (mode == ceph_recover_session_no)
423                         fsopt->flags &= ~CEPH_MOUNT_OPT_CLEANRECOVER;
424                 else if (mode == ceph_recover_session_clean)
425                         fsopt->flags |= CEPH_MOUNT_OPT_CLEANRECOVER;
426                 else
427                         BUG();
428                 break;
429         case Opt_source:
430                 if (fc->source)
431                         return invalfc(fc, "Multiple sources specified");
432                 return ceph_parse_source(param, fc);
433         case Opt_mon_addr:
434                 return ceph_parse_mon_addr(param, fc);
435         case Opt_wsize:
436                 if (result.uint_32 < PAGE_SIZE ||
437                     result.uint_32 > CEPH_MAX_WRITE_SIZE)
438                         goto out_of_range;
439                 fsopt->wsize = ALIGN(result.uint_32, PAGE_SIZE);
440                 break;
441         case Opt_rsize:
442                 if (result.uint_32 < PAGE_SIZE ||
443                     result.uint_32 > CEPH_MAX_READ_SIZE)
444                         goto out_of_range;
445                 fsopt->rsize = ALIGN(result.uint_32, PAGE_SIZE);
446                 break;
447         case Opt_rasize:
448                 fsopt->rasize = ALIGN(result.uint_32, PAGE_SIZE);
449                 break;
450         case Opt_caps_wanted_delay_min:
451                 if (result.uint_32 < 1)
452                         goto out_of_range;
453                 fsopt->caps_wanted_delay_min = result.uint_32;
454                 break;
455         case Opt_caps_wanted_delay_max:
456                 if (result.uint_32 < 1)
457                         goto out_of_range;
458                 fsopt->caps_wanted_delay_max = result.uint_32;
459                 break;
460         case Opt_caps_max:
461                 if (result.int_32 < 0)
462                         goto out_of_range;
463                 fsopt->caps_max = result.int_32;
464                 break;
465         case Opt_readdir_max_entries:
466                 if (result.uint_32 < 1)
467                         goto out_of_range;
468                 fsopt->max_readdir = result.uint_32;
469                 break;
470         case Opt_readdir_max_bytes:
471                 if (result.uint_32 < PAGE_SIZE && result.uint_32 != 0)
472                         goto out_of_range;
473                 fsopt->max_readdir_bytes = result.uint_32;
474                 break;
475         case Opt_congestion_kb:
476                 if (result.uint_32 < 1024) /* at least 1M */
477                         goto out_of_range;
478                 fsopt->congestion_kb = result.uint_32;
479                 break;
480         case Opt_dirstat:
481                 if (!result.negated)
482                         fsopt->flags |= CEPH_MOUNT_OPT_DIRSTAT;
483                 else
484                         fsopt->flags &= ~CEPH_MOUNT_OPT_DIRSTAT;
485                 break;
486         case Opt_rbytes:
487                 if (!result.negated)
488                         fsopt->flags |= CEPH_MOUNT_OPT_RBYTES;
489                 else
490                         fsopt->flags &= ~CEPH_MOUNT_OPT_RBYTES;
491                 break;
492         case Opt_asyncreaddir:
493                 if (!result.negated)
494                         fsopt->flags &= ~CEPH_MOUNT_OPT_NOASYNCREADDIR;
495                 else
496                         fsopt->flags |= CEPH_MOUNT_OPT_NOASYNCREADDIR;
497                 break;
498         case Opt_dcache:
499                 if (!result.negated)
500                         fsopt->flags |= CEPH_MOUNT_OPT_DCACHE;
501                 else
502                         fsopt->flags &= ~CEPH_MOUNT_OPT_DCACHE;
503                 break;
504         case Opt_ino32:
505                 if (!result.negated)
506                         fsopt->flags |= CEPH_MOUNT_OPT_INO32;
507                 else
508                         fsopt->flags &= ~CEPH_MOUNT_OPT_INO32;
509                 break;
510
511         case Opt_fscache:
512 #ifdef CONFIG_CEPH_FSCACHE
513                 kfree(fsopt->fscache_uniq);
514                 fsopt->fscache_uniq = NULL;
515                 if (result.negated) {
516                         fsopt->flags &= ~CEPH_MOUNT_OPT_FSCACHE;
517                 } else {
518                         fsopt->flags |= CEPH_MOUNT_OPT_FSCACHE;
519                         fsopt->fscache_uniq = param->string;
520                         param->string = NULL;
521                 }
522                 break;
523 #else
524                 return invalfc(fc, "fscache support is disabled");
525 #endif
526         case Opt_poolperm:
527                 if (!result.negated)
528                         fsopt->flags &= ~CEPH_MOUNT_OPT_NOPOOLPERM;
529                 else
530                         fsopt->flags |= CEPH_MOUNT_OPT_NOPOOLPERM;
531                 break;
532         case Opt_require_active_mds:
533                 if (!result.negated)
534                         fsopt->flags &= ~CEPH_MOUNT_OPT_MOUNTWAIT;
535                 else
536                         fsopt->flags |= CEPH_MOUNT_OPT_MOUNTWAIT;
537                 break;
538         case Opt_quotadf:
539                 if (!result.negated)
540                         fsopt->flags &= ~CEPH_MOUNT_OPT_NOQUOTADF;
541                 else
542                         fsopt->flags |= CEPH_MOUNT_OPT_NOQUOTADF;
543                 break;
544         case Opt_copyfrom:
545                 if (!result.negated)
546                         fsopt->flags &= ~CEPH_MOUNT_OPT_NOCOPYFROM;
547                 else
548                         fsopt->flags |= CEPH_MOUNT_OPT_NOCOPYFROM;
549                 break;
550         case Opt_acl:
551                 if (!result.negated) {
552 #ifdef CONFIG_CEPH_FS_POSIX_ACL
553                         fc->sb_flags |= SB_POSIXACL;
554 #else
555                         return invalfc(fc, "POSIX ACL support is disabled");
556 #endif
557                 } else {
558                         fc->sb_flags &= ~SB_POSIXACL;
559                 }
560                 break;
561         case Opt_wsync:
562                 if (!result.negated)
563                         fsopt->flags &= ~CEPH_MOUNT_OPT_ASYNC_DIROPS;
564                 else
565                         fsopt->flags |= CEPH_MOUNT_OPT_ASYNC_DIROPS;
566                 break;
567         default:
568                 BUG();
569         }
570         return 0;
571
572 out_of_range:
573         return invalfc(fc, "%s out of range", param->key);
574 }
575
576 static void destroy_mount_options(struct ceph_mount_options *args)
577 {
578         dout("destroy_mount_options %p\n", args);
579         if (!args)
580                 return;
581
582         kfree(args->snapdir_name);
583         kfree(args->mds_namespace);
584         kfree(args->server_path);
585         kfree(args->fscache_uniq);
586         kfree(args->mon_addr);
587         kfree(args);
588 }
589
590 static int strcmp_null(const char *s1, const char *s2)
591 {
592         if (!s1 && !s2)
593                 return 0;
594         if (s1 && !s2)
595                 return -1;
596         if (!s1 && s2)
597                 return 1;
598         return strcmp(s1, s2);
599 }
600
601 static int compare_mount_options(struct ceph_mount_options *new_fsopt,
602                                  struct ceph_options *new_opt,
603                                  struct ceph_fs_client *fsc)
604 {
605         struct ceph_mount_options *fsopt1 = new_fsopt;
606         struct ceph_mount_options *fsopt2 = fsc->mount_options;
607         int ofs = offsetof(struct ceph_mount_options, snapdir_name);
608         int ret;
609
610         ret = memcmp(fsopt1, fsopt2, ofs);
611         if (ret)
612                 return ret;
613
614         ret = strcmp_null(fsopt1->snapdir_name, fsopt2->snapdir_name);
615         if (ret)
616                 return ret;
617
618         ret = strcmp_null(fsopt1->mds_namespace, fsopt2->mds_namespace);
619         if (ret)
620                 return ret;
621
622         ret = strcmp_null(fsopt1->server_path, fsopt2->server_path);
623         if (ret)
624                 return ret;
625
626         ret = strcmp_null(fsopt1->fscache_uniq, fsopt2->fscache_uniq);
627         if (ret)
628                 return ret;
629
630         ret = strcmp_null(fsopt1->mon_addr, fsopt2->mon_addr);
631         if (ret)
632                 return ret;
633
634         return ceph_compare_options(new_opt, fsc->client);
635 }
636
637 /**
638  * ceph_show_options - Show mount options in /proc/mounts
639  * @m: seq_file to write to
640  * @root: root of that (sub)tree
641  */
642 static int ceph_show_options(struct seq_file *m, struct dentry *root)
643 {
644         struct ceph_fs_client *fsc = ceph_sb_to_client(root->d_sb);
645         struct ceph_mount_options *fsopt = fsc->mount_options;
646         size_t pos;
647         int ret;
648
649         /* a comma between MNT/MS and client options */
650         seq_putc(m, ',');
651         pos = m->count;
652
653         ret = ceph_print_client_options(m, fsc->client, false);
654         if (ret)
655                 return ret;
656
657         /* retract our comma if no client options */
658         if (m->count == pos)
659                 m->count--;
660
661         if (fsopt->flags & CEPH_MOUNT_OPT_DIRSTAT)
662                 seq_puts(m, ",dirstat");
663         if ((fsopt->flags & CEPH_MOUNT_OPT_RBYTES))
664                 seq_puts(m, ",rbytes");
665         if (fsopt->flags & CEPH_MOUNT_OPT_NOASYNCREADDIR)
666                 seq_puts(m, ",noasyncreaddir");
667         if ((fsopt->flags & CEPH_MOUNT_OPT_DCACHE) == 0)
668                 seq_puts(m, ",nodcache");
669         if (fsopt->flags & CEPH_MOUNT_OPT_INO32)
670                 seq_puts(m, ",ino32");
671         if (fsopt->flags & CEPH_MOUNT_OPT_FSCACHE) {
672                 seq_show_option(m, "fsc", fsopt->fscache_uniq);
673         }
674         if (fsopt->flags & CEPH_MOUNT_OPT_NOPOOLPERM)
675                 seq_puts(m, ",nopoolperm");
676         if (fsopt->flags & CEPH_MOUNT_OPT_NOQUOTADF)
677                 seq_puts(m, ",noquotadf");
678
679 #ifdef CONFIG_CEPH_FS_POSIX_ACL
680         if (root->d_sb->s_flags & SB_POSIXACL)
681                 seq_puts(m, ",acl");
682         else
683                 seq_puts(m, ",noacl");
684 #endif
685
686         if ((fsopt->flags & CEPH_MOUNT_OPT_NOCOPYFROM) == 0)
687                 seq_puts(m, ",copyfrom");
688
689         /* dump mds_namespace when old device syntax is in use */
690         if (fsopt->mds_namespace && !fsopt->new_dev_syntax)
691                 seq_show_option(m, "mds_namespace", fsopt->mds_namespace);
692
693         if (fsopt->mon_addr)
694                 seq_printf(m, ",mon_addr=%s", fsopt->mon_addr);
695
696         if (fsopt->flags & CEPH_MOUNT_OPT_CLEANRECOVER)
697                 seq_show_option(m, "recover_session", "clean");
698
699         if (!(fsopt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS))
700                 seq_puts(m, ",wsync");
701
702         if (fsopt->wsize != CEPH_MAX_WRITE_SIZE)
703                 seq_printf(m, ",wsize=%u", fsopt->wsize);
704         if (fsopt->rsize != CEPH_MAX_READ_SIZE)
705                 seq_printf(m, ",rsize=%u", fsopt->rsize);
706         if (fsopt->rasize != CEPH_RASIZE_DEFAULT)
707                 seq_printf(m, ",rasize=%u", fsopt->rasize);
708         if (fsopt->congestion_kb != default_congestion_kb())
709                 seq_printf(m, ",write_congestion_kb=%u", fsopt->congestion_kb);
710         if (fsopt->caps_max)
711                 seq_printf(m, ",caps_max=%d", fsopt->caps_max);
712         if (fsopt->caps_wanted_delay_min != CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT)
713                 seq_printf(m, ",caps_wanted_delay_min=%u",
714                          fsopt->caps_wanted_delay_min);
715         if (fsopt->caps_wanted_delay_max != CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT)
716                 seq_printf(m, ",caps_wanted_delay_max=%u",
717                            fsopt->caps_wanted_delay_max);
718         if (fsopt->max_readdir != CEPH_MAX_READDIR_DEFAULT)
719                 seq_printf(m, ",readdir_max_entries=%u", fsopt->max_readdir);
720         if (fsopt->max_readdir_bytes != CEPH_MAX_READDIR_BYTES_DEFAULT)
721                 seq_printf(m, ",readdir_max_bytes=%u", fsopt->max_readdir_bytes);
722         if (strcmp(fsopt->snapdir_name, CEPH_SNAPDIRNAME_DEFAULT))
723                 seq_show_option(m, "snapdirname", fsopt->snapdir_name);
724
725         return 0;
726 }
727
728 /*
729  * handle any mon messages the standard library doesn't understand.
730  * return error if we don't either.
731  */
732 static int extra_mon_dispatch(struct ceph_client *client, struct ceph_msg *msg)
733 {
734         struct ceph_fs_client *fsc = client->private;
735         int type = le16_to_cpu(msg->hdr.type);
736
737         switch (type) {
738         case CEPH_MSG_MDS_MAP:
739                 ceph_mdsc_handle_mdsmap(fsc->mdsc, msg);
740                 return 0;
741         case CEPH_MSG_FS_MAP_USER:
742                 ceph_mdsc_handle_fsmap(fsc->mdsc, msg);
743                 return 0;
744         default:
745                 return -1;
746         }
747 }
748
749 /*
750  * create a new fs client
751  *
752  * Success or not, this function consumes @fsopt and @opt.
753  */
754 static struct ceph_fs_client *create_fs_client(struct ceph_mount_options *fsopt,
755                                         struct ceph_options *opt)
756 {
757         struct ceph_fs_client *fsc;
758         int err;
759
760         fsc = kzalloc(sizeof(*fsc), GFP_KERNEL);
761         if (!fsc) {
762                 err = -ENOMEM;
763                 goto fail;
764         }
765
766         fsc->client = ceph_create_client(opt, fsc);
767         if (IS_ERR(fsc->client)) {
768                 err = PTR_ERR(fsc->client);
769                 goto fail;
770         }
771         opt = NULL; /* fsc->client now owns this */
772
773         fsc->client->extra_mon_dispatch = extra_mon_dispatch;
774         ceph_set_opt(fsc->client, ABORT_ON_FULL);
775
776         if (!fsopt->mds_namespace) {
777                 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_MDSMAP,
778                                    0, true);
779         } else {
780                 ceph_monc_want_map(&fsc->client->monc, CEPH_SUB_FSMAP,
781                                    0, false);
782         }
783
784         fsc->mount_options = fsopt;
785
786         fsc->sb = NULL;
787         fsc->mount_state = CEPH_MOUNT_MOUNTING;
788         fsc->filp_gen = 1;
789         fsc->have_copy_from2 = true;
790
791         atomic_long_set(&fsc->writeback_count, 0);
792
793         err = -ENOMEM;
794         /*
795          * The number of concurrent works can be high but they don't need
796          * to be processed in parallel, limit concurrency.
797          */
798         fsc->inode_wq = alloc_workqueue("ceph-inode", WQ_UNBOUND, 0);
799         if (!fsc->inode_wq)
800                 goto fail_client;
801         fsc->cap_wq = alloc_workqueue("ceph-cap", 0, 1);
802         if (!fsc->cap_wq)
803                 goto fail_inode_wq;
804
805         spin_lock(&ceph_fsc_lock);
806         list_add_tail(&fsc->metric_wakeup, &ceph_fsc_list);
807         spin_unlock(&ceph_fsc_lock);
808
809         return fsc;
810
811 fail_inode_wq:
812         destroy_workqueue(fsc->inode_wq);
813 fail_client:
814         ceph_destroy_client(fsc->client);
815 fail:
816         kfree(fsc);
817         if (opt)
818                 ceph_destroy_options(opt);
819         destroy_mount_options(fsopt);
820         return ERR_PTR(err);
821 }
822
823 static void flush_fs_workqueues(struct ceph_fs_client *fsc)
824 {
825         flush_workqueue(fsc->inode_wq);
826         flush_workqueue(fsc->cap_wq);
827 }
828
829 static void destroy_fs_client(struct ceph_fs_client *fsc)
830 {
831         dout("destroy_fs_client %p\n", fsc);
832
833         spin_lock(&ceph_fsc_lock);
834         list_del(&fsc->metric_wakeup);
835         spin_unlock(&ceph_fsc_lock);
836
837         ceph_mdsc_destroy(fsc);
838         destroy_workqueue(fsc->inode_wq);
839         destroy_workqueue(fsc->cap_wq);
840
841         destroy_mount_options(fsc->mount_options);
842
843         ceph_destroy_client(fsc->client);
844
845         kfree(fsc);
846         dout("destroy_fs_client %p done\n", fsc);
847 }
848
849 /*
850  * caches
851  */
852 struct kmem_cache *ceph_inode_cachep;
853 struct kmem_cache *ceph_cap_cachep;
854 struct kmem_cache *ceph_cap_flush_cachep;
855 struct kmem_cache *ceph_dentry_cachep;
856 struct kmem_cache *ceph_file_cachep;
857 struct kmem_cache *ceph_dir_file_cachep;
858 struct kmem_cache *ceph_mds_request_cachep;
859 mempool_t *ceph_wb_pagevec_pool;
860
861 static void ceph_inode_init_once(void *foo)
862 {
863         struct ceph_inode_info *ci = foo;
864         inode_init_once(&ci->vfs_inode);
865 }
866
867 static int __init init_caches(void)
868 {
869         int error = -ENOMEM;
870
871         ceph_inode_cachep = kmem_cache_create("ceph_inode_info",
872                                       sizeof(struct ceph_inode_info),
873                                       __alignof__(struct ceph_inode_info),
874                                       SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD|
875                                       SLAB_ACCOUNT, ceph_inode_init_once);
876         if (!ceph_inode_cachep)
877                 return -ENOMEM;
878
879         ceph_cap_cachep = KMEM_CACHE(ceph_cap, SLAB_MEM_SPREAD);
880         if (!ceph_cap_cachep)
881                 goto bad_cap;
882         ceph_cap_flush_cachep = KMEM_CACHE(ceph_cap_flush,
883                                            SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
884         if (!ceph_cap_flush_cachep)
885                 goto bad_cap_flush;
886
887         ceph_dentry_cachep = KMEM_CACHE(ceph_dentry_info,
888                                         SLAB_RECLAIM_ACCOUNT|SLAB_MEM_SPREAD);
889         if (!ceph_dentry_cachep)
890                 goto bad_dentry;
891
892         ceph_file_cachep = KMEM_CACHE(ceph_file_info, SLAB_MEM_SPREAD);
893         if (!ceph_file_cachep)
894                 goto bad_file;
895
896         ceph_dir_file_cachep = KMEM_CACHE(ceph_dir_file_info, SLAB_MEM_SPREAD);
897         if (!ceph_dir_file_cachep)
898                 goto bad_dir_file;
899
900         ceph_mds_request_cachep = KMEM_CACHE(ceph_mds_request, SLAB_MEM_SPREAD);
901         if (!ceph_mds_request_cachep)
902                 goto bad_mds_req;
903
904         ceph_wb_pagevec_pool = mempool_create_kmalloc_pool(10, CEPH_MAX_WRITE_SIZE >> PAGE_SHIFT);
905         if (!ceph_wb_pagevec_pool)
906                 goto bad_pagevec_pool;
907
908         error = ceph_fscache_register();
909         if (error)
910                 goto bad_fscache;
911
912         return 0;
913
914 bad_fscache:
915         kmem_cache_destroy(ceph_mds_request_cachep);
916 bad_pagevec_pool:
917         mempool_destroy(ceph_wb_pagevec_pool);
918 bad_mds_req:
919         kmem_cache_destroy(ceph_dir_file_cachep);
920 bad_dir_file:
921         kmem_cache_destroy(ceph_file_cachep);
922 bad_file:
923         kmem_cache_destroy(ceph_dentry_cachep);
924 bad_dentry:
925         kmem_cache_destroy(ceph_cap_flush_cachep);
926 bad_cap_flush:
927         kmem_cache_destroy(ceph_cap_cachep);
928 bad_cap:
929         kmem_cache_destroy(ceph_inode_cachep);
930         return error;
931 }
932
933 static void destroy_caches(void)
934 {
935         /*
936          * Make sure all delayed rcu free inodes are flushed before we
937          * destroy cache.
938          */
939         rcu_barrier();
940
941         kmem_cache_destroy(ceph_inode_cachep);
942         kmem_cache_destroy(ceph_cap_cachep);
943         kmem_cache_destroy(ceph_cap_flush_cachep);
944         kmem_cache_destroy(ceph_dentry_cachep);
945         kmem_cache_destroy(ceph_file_cachep);
946         kmem_cache_destroy(ceph_dir_file_cachep);
947         kmem_cache_destroy(ceph_mds_request_cachep);
948         mempool_destroy(ceph_wb_pagevec_pool);
949
950         ceph_fscache_unregister();
951 }
952
953 static void __ceph_umount_begin(struct ceph_fs_client *fsc)
954 {
955         ceph_osdc_abort_requests(&fsc->client->osdc, -EIO);
956         ceph_mdsc_force_umount(fsc->mdsc);
957         fsc->filp_gen++; // invalidate open files
958 }
959
960 /*
961  * ceph_umount_begin - initiate forced umount.  Tear down the
962  * mount, skipping steps that may hang while waiting for server(s).
963  */
964 void ceph_umount_begin(struct super_block *sb)
965 {
966         struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
967
968         dout("ceph_umount_begin - starting forced umount\n");
969         if (!fsc)
970                 return;
971         fsc->mount_state = CEPH_MOUNT_SHUTDOWN;
972         __ceph_umount_begin(fsc);
973 }
974
975 static const struct super_operations ceph_super_ops = {
976         .alloc_inode    = ceph_alloc_inode,
977         .free_inode     = ceph_free_inode,
978         .write_inode    = ceph_write_inode,
979         .drop_inode     = generic_delete_inode,
980         .evict_inode    = ceph_evict_inode,
981         .sync_fs        = ceph_sync_fs,
982         .put_super      = ceph_put_super,
983         .show_options   = ceph_show_options,
984         .statfs         = ceph_statfs,
985         .umount_begin   = ceph_umount_begin,
986 };
987
988 /*
989  * Bootstrap mount by opening the root directory.  Note the mount
990  * @started time from caller, and time out if this takes too long.
991  */
992 static struct dentry *open_root_dentry(struct ceph_fs_client *fsc,
993                                        const char *path,
994                                        unsigned long started)
995 {
996         struct ceph_mds_client *mdsc = fsc->mdsc;
997         struct ceph_mds_request *req = NULL;
998         int err;
999         struct dentry *root;
1000
1001         /* open dir */
1002         dout("open_root_inode opening '%s'\n", path);
1003         req = ceph_mdsc_create_request(mdsc, CEPH_MDS_OP_GETATTR, USE_ANY_MDS);
1004         if (IS_ERR(req))
1005                 return ERR_CAST(req);
1006         req->r_path1 = kstrdup(path, GFP_NOFS);
1007         if (!req->r_path1) {
1008                 root = ERR_PTR(-ENOMEM);
1009                 goto out;
1010         }
1011
1012         req->r_ino1.ino = CEPH_INO_ROOT;
1013         req->r_ino1.snap = CEPH_NOSNAP;
1014         req->r_started = started;
1015         req->r_timeout = fsc->client->options->mount_timeout;
1016         req->r_args.getattr.mask = cpu_to_le32(CEPH_STAT_CAP_INODE);
1017         req->r_num_caps = 2;
1018         err = ceph_mdsc_do_request(mdsc, NULL, req);
1019         if (err == 0) {
1020                 struct inode *inode = req->r_target_inode;
1021                 req->r_target_inode = NULL;
1022                 dout("open_root_inode success\n");
1023                 root = d_make_root(inode);
1024                 if (!root) {
1025                         root = ERR_PTR(-ENOMEM);
1026                         goto out;
1027                 }
1028                 dout("open_root_inode success, root dentry is %p\n", root);
1029         } else {
1030                 root = ERR_PTR(err);
1031         }
1032 out:
1033         ceph_mdsc_put_request(req);
1034         return root;
1035 }
1036
1037 /*
1038  * mount: join the ceph cluster, and open root directory.
1039  */
1040 static struct dentry *ceph_real_mount(struct ceph_fs_client *fsc,
1041                                       struct fs_context *fc)
1042 {
1043         int err;
1044         unsigned long started = jiffies;  /* note the start time */
1045         struct dentry *root;
1046
1047         dout("mount start %p\n", fsc);
1048         mutex_lock(&fsc->client->mount_mutex);
1049
1050         if (!fsc->sb->s_root) {
1051                 const char *path = fsc->mount_options->server_path ?
1052                                      fsc->mount_options->server_path + 1 : "";
1053
1054                 err = __ceph_open_session(fsc->client, started);
1055                 if (err < 0)
1056                         goto out;
1057
1058                 /* setup fscache */
1059                 if (fsc->mount_options->flags & CEPH_MOUNT_OPT_FSCACHE) {
1060                         err = ceph_fscache_register_fs(fsc, fc);
1061                         if (err < 0)
1062                                 goto out;
1063                 }
1064
1065                 dout("mount opening path '%s'\n", path);
1066
1067                 ceph_fs_debugfs_init(fsc);
1068
1069                 root = open_root_dentry(fsc, path, started);
1070                 if (IS_ERR(root)) {
1071                         err = PTR_ERR(root);
1072                         goto out;
1073                 }
1074                 fsc->sb->s_root = dget(root);
1075         } else {
1076                 root = dget(fsc->sb->s_root);
1077         }
1078
1079         fsc->mount_state = CEPH_MOUNT_MOUNTED;
1080         dout("mount success\n");
1081         mutex_unlock(&fsc->client->mount_mutex);
1082         return root;
1083
1084 out:
1085         mutex_unlock(&fsc->client->mount_mutex);
1086         return ERR_PTR(err);
1087 }
1088
1089 static int ceph_set_super(struct super_block *s, struct fs_context *fc)
1090 {
1091         struct ceph_fs_client *fsc = s->s_fs_info;
1092         int ret;
1093
1094         dout("set_super %p\n", s);
1095
1096         s->s_maxbytes = MAX_LFS_FILESIZE;
1097
1098         s->s_xattr = ceph_xattr_handlers;
1099         fsc->sb = s;
1100         fsc->max_file_size = 1ULL << 40; /* temp value until we get mdsmap */
1101
1102         s->s_op = &ceph_super_ops;
1103         s->s_d_op = &ceph_dentry_ops;
1104         s->s_export_op = &ceph_export_ops;
1105
1106         s->s_time_gran = 1;
1107         s->s_time_min = 0;
1108         s->s_time_max = U32_MAX;
1109
1110         ret = set_anon_super_fc(s, fc);
1111         if (ret != 0)
1112                 fsc->sb = NULL;
1113         return ret;
1114 }
1115
1116 /*
1117  * share superblock if same fs AND options
1118  */
1119 static int ceph_compare_super(struct super_block *sb, struct fs_context *fc)
1120 {
1121         struct ceph_fs_client *new = fc->s_fs_info;
1122         struct ceph_mount_options *fsopt = new->mount_options;
1123         struct ceph_options *opt = new->client->options;
1124         struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1125
1126         dout("ceph_compare_super %p\n", sb);
1127
1128         if (compare_mount_options(fsopt, opt, fsc)) {
1129                 dout("monitor(s)/mount options don't match\n");
1130                 return 0;
1131         }
1132         if ((opt->flags & CEPH_OPT_FSID) &&
1133             ceph_fsid_compare(&opt->fsid, &fsc->client->fsid)) {
1134                 dout("fsid doesn't match\n");
1135                 return 0;
1136         }
1137         if (fc->sb_flags != (sb->s_flags & ~SB_BORN)) {
1138                 dout("flags differ\n");
1139                 return 0;
1140         }
1141
1142         if (fsc->blocklisted && !ceph_test_mount_opt(fsc, CLEANRECOVER)) {
1143                 dout("client is blocklisted (and CLEANRECOVER is not set)\n");
1144                 return 0;
1145         }
1146
1147         if (fsc->mount_state == CEPH_MOUNT_SHUTDOWN) {
1148                 dout("client has been forcibly unmounted\n");
1149                 return 0;
1150         }
1151
1152         return 1;
1153 }
1154
1155 /*
1156  * construct our own bdi so we can control readahead, etc.
1157  */
1158 static atomic_long_t bdi_seq = ATOMIC_LONG_INIT(0);
1159
1160 static int ceph_setup_bdi(struct super_block *sb, struct ceph_fs_client *fsc)
1161 {
1162         int err;
1163
1164         err = super_setup_bdi_name(sb, "ceph-%ld",
1165                                    atomic_long_inc_return(&bdi_seq));
1166         if (err)
1167                 return err;
1168
1169         /* set ra_pages based on rasize mount option? */
1170         sb->s_bdi->ra_pages = fsc->mount_options->rasize >> PAGE_SHIFT;
1171
1172         /* set io_pages based on max osd read size */
1173         sb->s_bdi->io_pages = fsc->mount_options->rsize >> PAGE_SHIFT;
1174
1175         return 0;
1176 }
1177
1178 static int ceph_get_tree(struct fs_context *fc)
1179 {
1180         struct ceph_parse_opts_ctx *pctx = fc->fs_private;
1181         struct ceph_mount_options *fsopt = pctx->opts;
1182         struct super_block *sb;
1183         struct ceph_fs_client *fsc;
1184         struct dentry *res;
1185         int (*compare_super)(struct super_block *, struct fs_context *) =
1186                 ceph_compare_super;
1187         int err;
1188
1189         dout("ceph_get_tree\n");
1190
1191         if (!fc->source)
1192                 return invalfc(fc, "No source");
1193         if (fsopt->new_dev_syntax && !fsopt->mon_addr)
1194                 return invalfc(fc, "No monitor address");
1195
1196         /* create client (which we may/may not use) */
1197         fsc = create_fs_client(pctx->opts, pctx->copts);
1198         pctx->opts = NULL;
1199         pctx->copts = NULL;
1200         if (IS_ERR(fsc)) {
1201                 err = PTR_ERR(fsc);
1202                 goto out_final;
1203         }
1204
1205         err = ceph_mdsc_init(fsc);
1206         if (err < 0)
1207                 goto out;
1208
1209         if (ceph_test_opt(fsc->client, NOSHARE))
1210                 compare_super = NULL;
1211
1212         fc->s_fs_info = fsc;
1213         sb = sget_fc(fc, compare_super, ceph_set_super);
1214         fc->s_fs_info = NULL;
1215         if (IS_ERR(sb)) {
1216                 err = PTR_ERR(sb);
1217                 goto out;
1218         }
1219
1220         if (ceph_sb_to_client(sb) != fsc) {
1221                 destroy_fs_client(fsc);
1222                 fsc = ceph_sb_to_client(sb);
1223                 dout("get_sb got existing client %p\n", fsc);
1224         } else {
1225                 dout("get_sb using new client %p\n", fsc);
1226                 err = ceph_setup_bdi(sb, fsc);
1227                 if (err < 0)
1228                         goto out_splat;
1229         }
1230
1231         res = ceph_real_mount(fsc, fc);
1232         if (IS_ERR(res)) {
1233                 err = PTR_ERR(res);
1234                 goto out_splat;
1235         }
1236         dout("root %p inode %p ino %llx.%llx\n", res,
1237              d_inode(res), ceph_vinop(d_inode(res)));
1238         fc->root = fsc->sb->s_root;
1239         return 0;
1240
1241 out_splat:
1242         if (!ceph_mdsmap_is_cluster_available(fsc->mdsc->mdsmap)) {
1243                 pr_info("No mds server is up or the cluster is laggy\n");
1244                 err = -EHOSTUNREACH;
1245         }
1246
1247         ceph_mdsc_close_sessions(fsc->mdsc);
1248         deactivate_locked_super(sb);
1249         goto out_final;
1250
1251 out:
1252         destroy_fs_client(fsc);
1253 out_final:
1254         dout("ceph_get_tree fail %d\n", err);
1255         return err;
1256 }
1257
1258 static void ceph_free_fc(struct fs_context *fc)
1259 {
1260         struct ceph_parse_opts_ctx *pctx = fc->fs_private;
1261
1262         if (pctx) {
1263                 destroy_mount_options(pctx->opts);
1264                 ceph_destroy_options(pctx->copts);
1265                 kfree(pctx);
1266         }
1267 }
1268
1269 static int ceph_reconfigure_fc(struct fs_context *fc)
1270 {
1271         struct ceph_parse_opts_ctx *pctx = fc->fs_private;
1272         struct ceph_mount_options *fsopt = pctx->opts;
1273         struct ceph_fs_client *fsc = ceph_sb_to_client(fc->root->d_sb);
1274
1275         if (fsopt->flags & CEPH_MOUNT_OPT_ASYNC_DIROPS)
1276                 ceph_set_mount_opt(fsc, ASYNC_DIROPS);
1277         else
1278                 ceph_clear_mount_opt(fsc, ASYNC_DIROPS);
1279
1280         sync_filesystem(fc->root->d_sb);
1281         return 0;
1282 }
1283
1284 static const struct fs_context_operations ceph_context_ops = {
1285         .free           = ceph_free_fc,
1286         .parse_param    = ceph_parse_mount_param,
1287         .get_tree       = ceph_get_tree,
1288         .reconfigure    = ceph_reconfigure_fc,
1289 };
1290
1291 /*
1292  * Set up the filesystem mount context.
1293  */
1294 static int ceph_init_fs_context(struct fs_context *fc)
1295 {
1296         struct ceph_parse_opts_ctx *pctx;
1297         struct ceph_mount_options *fsopt;
1298
1299         pctx = kzalloc(sizeof(*pctx), GFP_KERNEL);
1300         if (!pctx)
1301                 return -ENOMEM;
1302
1303         pctx->copts = ceph_alloc_options();
1304         if (!pctx->copts)
1305                 goto nomem;
1306
1307         pctx->opts = kzalloc(sizeof(*pctx->opts), GFP_KERNEL);
1308         if (!pctx->opts)
1309                 goto nomem;
1310
1311         fsopt = pctx->opts;
1312         fsopt->flags = CEPH_MOUNT_OPT_DEFAULT;
1313
1314         fsopt->wsize = CEPH_MAX_WRITE_SIZE;
1315         fsopt->rsize = CEPH_MAX_READ_SIZE;
1316         fsopt->rasize = CEPH_RASIZE_DEFAULT;
1317         fsopt->snapdir_name = kstrdup(CEPH_SNAPDIRNAME_DEFAULT, GFP_KERNEL);
1318         if (!fsopt->snapdir_name)
1319                 goto nomem;
1320
1321         fsopt->caps_wanted_delay_min = CEPH_CAPS_WANTED_DELAY_MIN_DEFAULT;
1322         fsopt->caps_wanted_delay_max = CEPH_CAPS_WANTED_DELAY_MAX_DEFAULT;
1323         fsopt->max_readdir = CEPH_MAX_READDIR_DEFAULT;
1324         fsopt->max_readdir_bytes = CEPH_MAX_READDIR_BYTES_DEFAULT;
1325         fsopt->congestion_kb = default_congestion_kb();
1326
1327 #ifdef CONFIG_CEPH_FS_POSIX_ACL
1328         fc->sb_flags |= SB_POSIXACL;
1329 #endif
1330
1331         fc->fs_private = pctx;
1332         fc->ops = &ceph_context_ops;
1333         return 0;
1334
1335 nomem:
1336         destroy_mount_options(pctx->opts);
1337         ceph_destroy_options(pctx->copts);
1338         kfree(pctx);
1339         return -ENOMEM;
1340 }
1341
1342 static void ceph_kill_sb(struct super_block *s)
1343 {
1344         struct ceph_fs_client *fsc = ceph_sb_to_client(s);
1345
1346         dout("kill_sb %p\n", s);
1347
1348         ceph_mdsc_pre_umount(fsc->mdsc);
1349         flush_fs_workqueues(fsc);
1350
1351         kill_anon_super(s);
1352
1353         fsc->client->extra_mon_dispatch = NULL;
1354         ceph_fs_debugfs_cleanup(fsc);
1355
1356         ceph_fscache_unregister_fs(fsc);
1357
1358         destroy_fs_client(fsc);
1359 }
1360
1361 static struct file_system_type ceph_fs_type = {
1362         .owner          = THIS_MODULE,
1363         .name           = "ceph",
1364         .init_fs_context = ceph_init_fs_context,
1365         .kill_sb        = ceph_kill_sb,
1366         .fs_flags       = FS_RENAME_DOES_D_MOVE,
1367 };
1368 MODULE_ALIAS_FS("ceph");
1369
1370 int ceph_force_reconnect(struct super_block *sb)
1371 {
1372         struct ceph_fs_client *fsc = ceph_sb_to_client(sb);
1373         int err = 0;
1374
1375         fsc->mount_state = CEPH_MOUNT_RECOVER;
1376         __ceph_umount_begin(fsc);
1377
1378         /* Make sure all page caches get invalidated.
1379          * see remove_session_caps_cb() */
1380         flush_workqueue(fsc->inode_wq);
1381
1382         /* In case that we were blocklisted. This also reset
1383          * all mon/osd connections */
1384         ceph_reset_client_addr(fsc->client);
1385
1386         ceph_osdc_clear_abort_err(&fsc->client->osdc);
1387
1388         fsc->blocklisted = false;
1389         fsc->mount_state = CEPH_MOUNT_MOUNTED;
1390
1391         if (sb->s_root) {
1392                 err = __ceph_do_getattr(d_inode(sb->s_root), NULL,
1393                                         CEPH_STAT_CAP_INODE, true);
1394         }
1395         return err;
1396 }
1397
1398 static int __init init_ceph(void)
1399 {
1400         int ret = init_caches();
1401         if (ret)
1402                 goto out;
1403
1404         ceph_flock_init();
1405         ret = register_filesystem(&ceph_fs_type);
1406         if (ret)
1407                 goto out_caches;
1408
1409         pr_info("loaded (mds proto %d)\n", CEPH_MDSC_PROTOCOL);
1410
1411         return 0;
1412
1413 out_caches:
1414         destroy_caches();
1415 out:
1416         return ret;
1417 }
1418
1419 static void __exit exit_ceph(void)
1420 {
1421         dout("exit_ceph\n");
1422         unregister_filesystem(&ceph_fs_type);
1423         destroy_caches();
1424 }
1425
1426 static int param_set_metrics(const char *val, const struct kernel_param *kp)
1427 {
1428         struct ceph_fs_client *fsc;
1429         int ret;
1430
1431         ret = param_set_bool(val, kp);
1432         if (ret) {
1433                 pr_err("Failed to parse sending metrics switch value '%s'\n",
1434                        val);
1435                 return ret;
1436         } else if (!disable_send_metrics) {
1437                 // wake up all the mds clients
1438                 spin_lock(&ceph_fsc_lock);
1439                 list_for_each_entry(fsc, &ceph_fsc_list, metric_wakeup) {
1440                         metric_schedule_delayed(&fsc->mdsc->metric);
1441                 }
1442                 spin_unlock(&ceph_fsc_lock);
1443         }
1444
1445         return 0;
1446 }
1447
1448 static const struct kernel_param_ops param_ops_metrics = {
1449         .set = param_set_metrics,
1450         .get = param_get_bool,
1451 };
1452
1453 bool disable_send_metrics = false;
1454 module_param_cb(disable_send_metrics, &param_ops_metrics, &disable_send_metrics, 0644);
1455 MODULE_PARM_DESC(disable_send_metrics, "Enable sending perf metrics to ceph cluster (default: on)");
1456
1457 module_init(init_ceph);
1458 module_exit(exit_ceph);
1459
1460 MODULE_AUTHOR("Sage Weil <sage@newdream.net>");
1461 MODULE_AUTHOR("Yehuda Sadeh <yehuda@hq.newdream.net>");
1462 MODULE_AUTHOR("Patience Warnick <patience@newdream.net>");
1463 MODULE_DESCRIPTION("Ceph filesystem for Linux");
1464 MODULE_LICENSE("GPL");