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