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