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