xref: /linux/fs/overlayfs/params.c (revision 8e3ed5440b0c305dcd1d5fa7419bd8066d22ef42)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/fs.h>
4 #include <linux/module.h>
5 #include <linux/namei.h>
6 #include <linux/fs_context.h>
7 #include <linux/fs_parser.h>
8 #include <linux/posix_acl_xattr.h>
9 #include <linux/seq_file.h>
10 #include <linux/xattr.h>
11 #include "overlayfs.h"
12 #include "params.h"
13 
14 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
15 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
16 MODULE_PARM_DESC(redirect_dir,
17 		 "Default to on or off for the redirect_dir feature");
18 
19 static bool ovl_redirect_always_follow =
20 	IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
21 module_param_named(redirect_always_follow, ovl_redirect_always_follow,
22 		   bool, 0644);
23 MODULE_PARM_DESC(redirect_always_follow,
24 		 "Follow redirects even if redirect_dir feature is turned off");
25 
26 static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
27 module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
28 MODULE_PARM_DESC(xino_auto,
29 		 "Auto enable xino feature");
30 
31 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
32 module_param_named(index, ovl_index_def, bool, 0644);
33 MODULE_PARM_DESC(index,
34 		 "Default to on or off for the inodes index feature");
35 
36 static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
37 module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
38 MODULE_PARM_DESC(nfs_export,
39 		 "Default to on or off for the NFS export feature");
40 
41 static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
42 module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
43 MODULE_PARM_DESC(metacopy,
44 		 "Default to on or off for the metadata only copy up feature");
45 
46 enum ovl_opt {
47 	Opt_lowerdir,
48 	Opt_lowerdir_add,
49 	Opt_datadir_add,
50 	Opt_upperdir,
51 	Opt_workdir,
52 	Opt_default_permissions,
53 	Opt_redirect_dir,
54 	Opt_index,
55 	Opt_uuid,
56 	Opt_nfs_export,
57 	Opt_userxattr,
58 	Opt_xino,
59 	Opt_metacopy,
60 	Opt_verity,
61 	Opt_volatile,
62 };
63 
64 static const struct constant_table ovl_parameter_bool[] = {
65 	{ "on",		true  },
66 	{ "off",	false },
67 	{}
68 };
69 
70 static const struct constant_table ovl_parameter_uuid[] = {
71 	{ "off",	OVL_UUID_OFF  },
72 	{ "null",	OVL_UUID_NULL },
73 	{ "auto",	OVL_UUID_AUTO },
74 	{ "on",		OVL_UUID_ON   },
75 	{}
76 };
77 
78 static const char *ovl_uuid_mode(struct ovl_config *config)
79 {
80 	return ovl_parameter_uuid[config->uuid].name;
81 }
82 
83 static int ovl_uuid_def(void)
84 {
85 	return OVL_UUID_AUTO;
86 }
87 
88 static const struct constant_table ovl_parameter_xino[] = {
89 	{ "off",	OVL_XINO_OFF  },
90 	{ "auto",	OVL_XINO_AUTO },
91 	{ "on",		OVL_XINO_ON   },
92 	{}
93 };
94 
95 const char *ovl_xino_mode(struct ovl_config *config)
96 {
97 	return ovl_parameter_xino[config->xino].name;
98 }
99 
100 static int ovl_xino_def(void)
101 {
102 	return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
103 }
104 
105 const struct constant_table ovl_parameter_redirect_dir[] = {
106 	{ "off",	OVL_REDIRECT_OFF      },
107 	{ "follow",	OVL_REDIRECT_FOLLOW   },
108 	{ "nofollow",	OVL_REDIRECT_NOFOLLOW },
109 	{ "on",		OVL_REDIRECT_ON       },
110 	{}
111 };
112 
113 static const char *ovl_redirect_mode(struct ovl_config *config)
114 {
115 	return ovl_parameter_redirect_dir[config->redirect_mode].name;
116 }
117 
118 static int ovl_redirect_mode_def(void)
119 {
120 	return ovl_redirect_dir_def	  ? OVL_REDIRECT_ON :
121 	       ovl_redirect_always_follow ? OVL_REDIRECT_FOLLOW :
122 					    OVL_REDIRECT_NOFOLLOW;
123 }
124 
125 static const struct constant_table ovl_parameter_verity[] = {
126 	{ "off",	OVL_VERITY_OFF     },
127 	{ "on",		OVL_VERITY_ON      },
128 	{ "require",	OVL_VERITY_REQUIRE },
129 	{}
130 };
131 
132 static const char *ovl_verity_mode(struct ovl_config *config)
133 {
134 	return ovl_parameter_verity[config->verity_mode].name;
135 }
136 
137 static int ovl_verity_mode_def(void)
138 {
139 	return OVL_VERITY_OFF;
140 }
141 
142 #define fsparam_string_empty(NAME, OPT) \
143 	__fsparam(fs_param_is_string, NAME, OPT, fs_param_can_be_empty, NULL)
144 
145 
146 const struct fs_parameter_spec ovl_parameter_spec[] = {
147 	fsparam_string_empty("lowerdir",    Opt_lowerdir),
148 	fsparam_string("lowerdir+",         Opt_lowerdir_add),
149 	fsparam_string("datadir+",          Opt_datadir_add),
150 	fsparam_string("upperdir",          Opt_upperdir),
151 	fsparam_string("workdir",           Opt_workdir),
152 	fsparam_flag("default_permissions", Opt_default_permissions),
153 	fsparam_enum("redirect_dir",        Opt_redirect_dir, ovl_parameter_redirect_dir),
154 	fsparam_enum("index",               Opt_index, ovl_parameter_bool),
155 	fsparam_enum("uuid",                Opt_uuid, ovl_parameter_uuid),
156 	fsparam_enum("nfs_export",          Opt_nfs_export, ovl_parameter_bool),
157 	fsparam_flag("userxattr",           Opt_userxattr),
158 	fsparam_enum("xino",                Opt_xino, ovl_parameter_xino),
159 	fsparam_enum("metacopy",            Opt_metacopy, ovl_parameter_bool),
160 	fsparam_enum("verity",              Opt_verity, ovl_parameter_verity),
161 	fsparam_flag("volatile",            Opt_volatile),
162 	{}
163 };
164 
165 static char *ovl_next_opt(char **s)
166 {
167 	char *sbegin = *s;
168 	char *p;
169 
170 	if (sbegin == NULL)
171 		return NULL;
172 
173 	for (p = sbegin; *p; p++) {
174 		if (*p == '\\') {
175 			p++;
176 			if (!*p)
177 				break;
178 		} else if (*p == ',') {
179 			*p = '\0';
180 			*s = p + 1;
181 			return sbegin;
182 		}
183 	}
184 	*s = NULL;
185 	return sbegin;
186 }
187 
188 static int ovl_parse_monolithic(struct fs_context *fc, void *data)
189 {
190 	return vfs_parse_monolithic_sep(fc, data, ovl_next_opt);
191 }
192 
193 static ssize_t ovl_parse_param_split_lowerdirs(char *str)
194 {
195 	ssize_t nr_layers = 1, nr_colons = 0;
196 	char *s, *d;
197 
198 	for (s = d = str;; s++, d++) {
199 		if (*s == '\\') {
200 			/* keep esc chars in split lowerdir */
201 			*d++ = *s++;
202 		} else if (*s == ':') {
203 			bool next_colon = (*(s + 1) == ':');
204 
205 			nr_colons++;
206 			if (nr_colons == 2 && next_colon) {
207 				pr_err("only single ':' or double '::' sequences of unescaped colons in lowerdir mount option allowed.\n");
208 				return -EINVAL;
209 			}
210 			/* count layers, not colons */
211 			if (!next_colon)
212 				nr_layers++;
213 
214 			*d = '\0';
215 			continue;
216 		}
217 
218 		*d = *s;
219 		if (!*s) {
220 			/* trailing colons */
221 			if (nr_colons) {
222 				pr_err("unescaped trailing colons in lowerdir mount option.\n");
223 				return -EINVAL;
224 			}
225 			break;
226 		}
227 		nr_colons = 0;
228 	}
229 
230 	return nr_layers;
231 }
232 
233 static int ovl_mount_dir_noesc(const char *name, struct path *path)
234 {
235 	int err = -EINVAL;
236 
237 	if (!*name) {
238 		pr_err("empty lowerdir\n");
239 		goto out;
240 	}
241 	err = kern_path(name, LOOKUP_FOLLOW, path);
242 	if (err) {
243 		pr_err("failed to resolve '%s': %i\n", name, err);
244 		goto out;
245 	}
246 	return 0;
247 
248 out:
249 	return err;
250 }
251 
252 static void ovl_unescape(char *s)
253 {
254 	char *d = s;
255 
256 	for (;; s++, d++) {
257 		if (*s == '\\')
258 			s++;
259 		*d = *s;
260 		if (!*s)
261 			break;
262 	}
263 }
264 
265 static int ovl_mount_dir(const char *name, struct path *path)
266 {
267 	int err = -ENOMEM;
268 	char *tmp = kstrdup(name, GFP_KERNEL);
269 
270 	if (tmp) {
271 		ovl_unescape(tmp);
272 		err = ovl_mount_dir_noesc(tmp, path);
273 		kfree(tmp);
274 	}
275 	return err;
276 }
277 
278 static int ovl_mount_dir_check(struct fs_context *fc, const struct path *path,
279 			       enum ovl_opt layer, const char *name, bool upper)
280 {
281 	struct ovl_fs_context *ctx = fc->fs_private;
282 
283 	if (ovl_dentry_weird(path->dentry))
284 		return invalfc(fc, "filesystem on %s not supported", name);
285 
286 	if (!d_is_dir(path->dentry))
287 		return invalfc(fc, "%s is not a directory", name);
288 
289 
290 	/*
291 	 * Check whether upper path is read-only here to report failures
292 	 * early. Don't forget to recheck when the superblock is created
293 	 * as the mount attributes could change.
294 	 */
295 	if (upper) {
296 		if (path->dentry->d_flags & DCACHE_OP_REAL)
297 			return invalfc(fc, "filesystem on %s not supported as upperdir", name);
298 		if (__mnt_is_readonly(path->mnt))
299 			return invalfc(fc, "filesystem on %s is read-only", name);
300 	} else {
301 		if (ctx->lowerdir_all && layer != Opt_lowerdir)
302 			return invalfc(fc, "lowerdir+ and datadir+ cannot follow lowerdir");
303 		if (ctx->nr_data && layer == Opt_lowerdir_add)
304 			return invalfc(fc, "regular lower layers cannot follow data layers");
305 		if (ctx->nr == OVL_MAX_STACK)
306 			return invalfc(fc, "too many lower directories, limit is %d",
307 				       OVL_MAX_STACK);
308 	}
309 	return 0;
310 }
311 
312 static int ovl_ctx_realloc_lower(struct fs_context *fc)
313 {
314 	struct ovl_fs_context *ctx = fc->fs_private;
315 	struct ovl_fs_context_layer *l;
316 	size_t nr;
317 
318 	if (ctx->nr < ctx->capacity)
319 		return 0;
320 
321 	nr = min_t(size_t, max(4096 / sizeof(*l), ctx->capacity * 2),
322 		   OVL_MAX_STACK);
323 	l = krealloc_array(ctx->lower, nr, sizeof(*l), GFP_KERNEL_ACCOUNT);
324 	if (!l)
325 		return -ENOMEM;
326 
327 	ctx->lower = l;
328 	ctx->capacity = nr;
329 	return 0;
330 }
331 
332 static void ovl_add_layer(struct fs_context *fc, enum ovl_opt layer,
333 			 struct path *path, char **pname)
334 {
335 	struct ovl_fs *ofs = fc->s_fs_info;
336 	struct ovl_config *config = &ofs->config;
337 	struct ovl_fs_context *ctx = fc->fs_private;
338 	struct ovl_fs_context_layer *l;
339 
340 	switch (layer) {
341 	case Opt_workdir:
342 		swap(config->workdir, *pname);
343 		swap(ctx->work, *path);
344 		break;
345 	case Opt_upperdir:
346 		swap(config->upperdir, *pname);
347 		swap(ctx->upper, *path);
348 		break;
349 	case Opt_datadir_add:
350 		ctx->nr_data++;
351 		fallthrough;
352 	case Opt_lowerdir_add:
353 		WARN_ON(ctx->nr >= ctx->capacity);
354 		l = &ctx->lower[ctx->nr++];
355 		memset(l, 0, sizeof(*l));
356 		swap(l->name, *pname);
357 		swap(l->path, *path);
358 		break;
359 	default:
360 		WARN_ON(1);
361 	}
362 }
363 
364 static int ovl_parse_layer(struct fs_context *fc, struct fs_parameter *param,
365 			   enum ovl_opt layer)
366 {
367 	char *name = kstrdup(param->string, GFP_KERNEL);
368 	bool upper = (layer == Opt_upperdir || layer == Opt_workdir);
369 	struct path path;
370 	int err;
371 
372 	if (!name)
373 		return -ENOMEM;
374 
375 	if (upper)
376 		err = ovl_mount_dir(name, &path);
377 	else
378 		err = ovl_mount_dir_noesc(name, &path);
379 	if (err)
380 		goto out_free;
381 
382 	err = ovl_mount_dir_check(fc, &path, layer, name, upper);
383 	if (err)
384 		goto out_put;
385 
386 	if (!upper) {
387 		err = ovl_ctx_realloc_lower(fc);
388 		if (err)
389 			goto out_put;
390 	}
391 
392 	/* Store the user provided path string in ctx to show in mountinfo */
393 	ovl_add_layer(fc, layer, &path, &name);
394 
395 out_put:
396 	path_put(&path);
397 out_free:
398 	kfree(name);
399 	return err;
400 }
401 
402 static void ovl_reset_lowerdirs(struct ovl_fs_context *ctx)
403 {
404 	struct ovl_fs_context_layer *l = ctx->lower;
405 
406 	// Reset old user provided lowerdir string
407 	kfree(ctx->lowerdir_all);
408 	ctx->lowerdir_all = NULL;
409 
410 	for (size_t nr = 0; nr < ctx->nr; nr++, l++) {
411 		path_put(&l->path);
412 		kfree(l->name);
413 		l->name = NULL;
414 	}
415 	ctx->nr = 0;
416 	ctx->nr_data = 0;
417 }
418 
419 /*
420  * Parse lowerdir= mount option:
421  *
422  * e.g.: lowerdir=/lower1:/lower2:/lower3::/data1::/data2
423  *     Set "/lower1", "/lower2", and "/lower3" as lower layers and
424  *     "/data1" and "/data2" as data lower layers. Any existing lower
425  *     layers are replaced.
426  */
427 static int ovl_parse_param_lowerdir(const char *name, struct fs_context *fc)
428 {
429 	int err;
430 	struct ovl_fs_context *ctx = fc->fs_private;
431 	struct ovl_fs_context_layer *l;
432 	char *dup = NULL, *iter;
433 	ssize_t nr_lower, nr;
434 	bool data_layer = false;
435 
436 	/*
437 	 * Ensure we're backwards compatible with mount(2)
438 	 * by allowing relative paths.
439 	 */
440 
441 	/* drop all existing lower layers */
442 	ovl_reset_lowerdirs(ctx);
443 
444 	if (!*name)
445 		return 0;
446 
447 	if (*name == ':') {
448 		pr_err("cannot append lower layer");
449 		return -EINVAL;
450 	}
451 
452 	// Store user provided lowerdir string to show in mount options
453 	ctx->lowerdir_all = kstrdup(name, GFP_KERNEL);
454 	if (!ctx->lowerdir_all)
455 		return -ENOMEM;
456 
457 	dup = kstrdup(name, GFP_KERNEL);
458 	if (!dup)
459 		return -ENOMEM;
460 
461 	err = -EINVAL;
462 	nr_lower = ovl_parse_param_split_lowerdirs(dup);
463 	if (nr_lower < 0)
464 		goto out_err;
465 
466 	if (nr_lower > OVL_MAX_STACK) {
467 		pr_err("too many lower directories, limit is %d\n", OVL_MAX_STACK);
468 		goto out_err;
469 	}
470 
471 	if (nr_lower > ctx->capacity) {
472 		err = -ENOMEM;
473 		l = krealloc_array(ctx->lower, nr_lower, sizeof(*ctx->lower),
474 				   GFP_KERNEL_ACCOUNT);
475 		if (!l)
476 			goto out_err;
477 
478 		ctx->lower = l;
479 		ctx->capacity = nr_lower;
480 	}
481 
482 	iter = dup;
483 	l = ctx->lower;
484 	for (nr = 0; nr < nr_lower; nr++, l++) {
485 		ctx->nr++;
486 		memset(l, 0, sizeof(*l));
487 
488 		err = ovl_mount_dir(iter, &l->path);
489 		if (err)
490 			goto out_put;
491 
492 		err = ovl_mount_dir_check(fc, &l->path, Opt_lowerdir, iter, false);
493 		if (err)
494 			goto out_put;
495 
496 		err = -ENOMEM;
497 		l->name = kstrdup(iter, GFP_KERNEL_ACCOUNT);
498 		if (!l->name)
499 			goto out_put;
500 
501 		if (data_layer)
502 			ctx->nr_data++;
503 
504 		/* Calling strchr() again would overrun. */
505 		if (ctx->nr == nr_lower)
506 			break;
507 
508 		err = -EINVAL;
509 		iter = strchr(iter, '\0') + 1;
510 		if (*iter) {
511 			/*
512 			 * This is a regular layer so we require that
513 			 * there are no data layers.
514 			 */
515 			if (ctx->nr_data > 0) {
516 				pr_err("regular lower layers cannot follow data lower layers");
517 				goto out_put;
518 			}
519 
520 			data_layer = false;
521 			continue;
522 		}
523 
524 		/* This is a data lower layer. */
525 		data_layer = true;
526 		iter++;
527 	}
528 	kfree(dup);
529 	return 0;
530 
531 out_put:
532 	ovl_reset_lowerdirs(ctx);
533 
534 out_err:
535 	kfree(dup);
536 
537 	/* Intentionally don't realloc to a smaller size. */
538 	return err;
539 }
540 
541 static int ovl_parse_param(struct fs_context *fc, struct fs_parameter *param)
542 {
543 	int err = 0;
544 	struct fs_parse_result result;
545 	struct ovl_fs *ofs = fc->s_fs_info;
546 	struct ovl_config *config = &ofs->config;
547 	struct ovl_fs_context *ctx = fc->fs_private;
548 	int opt;
549 
550 	if (fc->purpose == FS_CONTEXT_FOR_RECONFIGURE) {
551 		/*
552 		 * On remount overlayfs has always ignored all mount
553 		 * options no matter if malformed or not so for
554 		 * backwards compatibility we do the same here.
555 		 */
556 		if (fc->oldapi)
557 			return 0;
558 
559 		/*
560 		 * Give us the freedom to allow changing mount options
561 		 * with the new mount api in the future. So instead of
562 		 * silently ignoring everything we report a proper
563 		 * error. This is only visible for users of the new
564 		 * mount api.
565 		 */
566 		return invalfc(fc, "No changes allowed in reconfigure");
567 	}
568 
569 	opt = fs_parse(fc, ovl_parameter_spec, param, &result);
570 	if (opt < 0)
571 		return opt;
572 
573 	switch (opt) {
574 	case Opt_lowerdir:
575 		err = ovl_parse_param_lowerdir(param->string, fc);
576 		break;
577 	case Opt_lowerdir_add:
578 	case Opt_datadir_add:
579 	case Opt_upperdir:
580 	case Opt_workdir:
581 		err = ovl_parse_layer(fc, param, opt);
582 		break;
583 	case Opt_default_permissions:
584 		config->default_permissions = true;
585 		break;
586 	case Opt_redirect_dir:
587 		config->redirect_mode = result.uint_32;
588 		if (config->redirect_mode == OVL_REDIRECT_OFF) {
589 			config->redirect_mode = ovl_redirect_always_follow ?
590 						OVL_REDIRECT_FOLLOW :
591 						OVL_REDIRECT_NOFOLLOW;
592 		}
593 		ctx->set.redirect = true;
594 		break;
595 	case Opt_index:
596 		config->index = result.uint_32;
597 		ctx->set.index = true;
598 		break;
599 	case Opt_uuid:
600 		config->uuid = result.uint_32;
601 		break;
602 	case Opt_nfs_export:
603 		config->nfs_export = result.uint_32;
604 		ctx->set.nfs_export = true;
605 		break;
606 	case Opt_xino:
607 		config->xino = result.uint_32;
608 		break;
609 	case Opt_metacopy:
610 		config->metacopy = result.uint_32;
611 		ctx->set.metacopy = true;
612 		break;
613 	case Opt_verity:
614 		config->verity_mode = result.uint_32;
615 		break;
616 	case Opt_volatile:
617 		config->ovl_volatile = true;
618 		break;
619 	case Opt_userxattr:
620 		config->userxattr = true;
621 		break;
622 	default:
623 		pr_err("unrecognized mount option \"%s\" or missing value\n",
624 		       param->key);
625 		return -EINVAL;
626 	}
627 
628 	return err;
629 }
630 
631 static int ovl_get_tree(struct fs_context *fc)
632 {
633 	return get_tree_nodev(fc, ovl_fill_super);
634 }
635 
636 static inline void ovl_fs_context_free(struct ovl_fs_context *ctx)
637 {
638 	ovl_reset_lowerdirs(ctx);
639 	path_put(&ctx->upper);
640 	path_put(&ctx->work);
641 	kfree(ctx->lower);
642 	kfree(ctx);
643 }
644 
645 static void ovl_free(struct fs_context *fc)
646 {
647 	struct ovl_fs *ofs = fc->s_fs_info;
648 	struct ovl_fs_context *ctx = fc->fs_private;
649 
650 	/*
651 	 * ofs is stored in the fs_context when it is initialized.
652 	 * ofs is transferred to the superblock on a successful mount,
653 	 * but if an error occurs before the transfer we have to free
654 	 * it here.
655 	 */
656 	if (ofs)
657 		ovl_free_fs(ofs);
658 
659 	if (ctx)
660 		ovl_fs_context_free(ctx);
661 }
662 
663 static int ovl_reconfigure(struct fs_context *fc)
664 {
665 	struct super_block *sb = fc->root->d_sb;
666 	struct ovl_fs *ofs = OVL_FS(sb);
667 	struct super_block *upper_sb;
668 	int ret = 0;
669 
670 	if (!(fc->sb_flags & SB_RDONLY) && ovl_force_readonly(ofs))
671 		return -EROFS;
672 
673 	if (fc->sb_flags & SB_RDONLY && !sb_rdonly(sb)) {
674 		upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
675 		if (ovl_should_sync(ofs)) {
676 			down_read(&upper_sb->s_umount);
677 			ret = sync_filesystem(upper_sb);
678 			up_read(&upper_sb->s_umount);
679 		}
680 	}
681 
682 	return ret;
683 }
684 
685 static const struct fs_context_operations ovl_context_ops = {
686 	.parse_monolithic = ovl_parse_monolithic,
687 	.parse_param = ovl_parse_param,
688 	.get_tree    = ovl_get_tree,
689 	.reconfigure = ovl_reconfigure,
690 	.free        = ovl_free,
691 };
692 
693 /*
694  * This is called during fsopen() and will record the user namespace of
695  * the caller in fc->user_ns since we've raised FS_USERNS_MOUNT. We'll
696  * need it when we actually create the superblock to verify that the
697  * process creating the superblock is in the same user namespace as
698  * process that called fsopen().
699  */
700 int ovl_init_fs_context(struct fs_context *fc)
701 {
702 	struct ovl_fs_context *ctx;
703 	struct ovl_fs *ofs;
704 
705 	ctx = kzalloc(sizeof(*ctx), GFP_KERNEL_ACCOUNT);
706 	if (!ctx)
707 		return -ENOMEM;
708 
709 	/*
710 	 * By default we allocate for three lower layers. It's likely
711 	 * that it'll cover most users.
712 	 */
713 	ctx->lower = kmalloc_array(3, sizeof(*ctx->lower), GFP_KERNEL_ACCOUNT);
714 	if (!ctx->lower)
715 		goto out_err;
716 	ctx->capacity = 3;
717 
718 	ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
719 	if (!ofs)
720 		goto out_err;
721 
722 	ofs->config.redirect_mode	= ovl_redirect_mode_def();
723 	ofs->config.index		= ovl_index_def;
724 	ofs->config.uuid		= ovl_uuid_def();
725 	ofs->config.nfs_export		= ovl_nfs_export_def;
726 	ofs->config.xino		= ovl_xino_def();
727 	ofs->config.metacopy		= ovl_metacopy_def;
728 
729 	fc->s_fs_info		= ofs;
730 	fc->fs_private		= ctx;
731 	fc->ops			= &ovl_context_ops;
732 	return 0;
733 
734 out_err:
735 	ovl_fs_context_free(ctx);
736 	return -ENOMEM;
737 
738 }
739 
740 void ovl_free_fs(struct ovl_fs *ofs)
741 {
742 	struct vfsmount **mounts;
743 	unsigned i;
744 
745 	iput(ofs->workbasedir_trap);
746 	iput(ofs->workdir_trap);
747 	dput(ofs->whiteout);
748 	dput(ofs->workdir);
749 	if (ofs->workdir_locked)
750 		ovl_inuse_unlock(ofs->workbasedir);
751 	dput(ofs->workbasedir);
752 	if (ofs->upperdir_locked)
753 		ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root);
754 
755 	/* Reuse ofs->config.lowerdirs as a vfsmount array before freeing it */
756 	mounts = (struct vfsmount **) ofs->config.lowerdirs;
757 	for (i = 0; i < ofs->numlayer; i++) {
758 		iput(ofs->layers[i].trap);
759 		kfree(ofs->config.lowerdirs[i]);
760 		mounts[i] = ofs->layers[i].mnt;
761 	}
762 	kern_unmount_array(mounts, ofs->numlayer);
763 	kfree(ofs->layers);
764 	for (i = 0; i < ofs->numfs; i++)
765 		free_anon_bdev(ofs->fs[i].pseudo_dev);
766 	kfree(ofs->fs);
767 
768 	kfree(ofs->config.lowerdirs);
769 	kfree(ofs->config.upperdir);
770 	kfree(ofs->config.workdir);
771 	if (ofs->creator_cred)
772 		put_cred(ofs->creator_cred);
773 	kfree(ofs);
774 }
775 
776 int ovl_fs_params_verify(const struct ovl_fs_context *ctx,
777 			 struct ovl_config *config)
778 {
779 	struct ovl_opt_set set = ctx->set;
780 
781 	if (ctx->nr_data > 0 && !config->metacopy) {
782 		pr_err("lower data-only dirs require metacopy support.\n");
783 		return -EINVAL;
784 	}
785 
786 	/* Workdir/index are useless in non-upper mount */
787 	if (!config->upperdir) {
788 		if (config->workdir) {
789 			pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
790 				config->workdir);
791 			kfree(config->workdir);
792 			config->workdir = NULL;
793 		}
794 		if (config->index && set.index) {
795 			pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n");
796 			set.index = false;
797 		}
798 		config->index = false;
799 	}
800 
801 	if (!config->upperdir && config->ovl_volatile) {
802 		pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n");
803 		config->ovl_volatile = false;
804 	}
805 
806 	if (!config->upperdir && config->uuid == OVL_UUID_ON) {
807 		pr_info("option \"uuid=on\" requires an upper fs, falling back to uuid=null.\n");
808 		config->uuid = OVL_UUID_NULL;
809 	}
810 
811 	/* Resolve verity -> metacopy dependency */
812 	if (config->verity_mode && !config->metacopy) {
813 		/* Don't allow explicit specified conflicting combinations */
814 		if (set.metacopy) {
815 			pr_err("conflicting options: metacopy=off,verity=%s\n",
816 			       ovl_verity_mode(config));
817 			return -EINVAL;
818 		}
819 		/* Otherwise automatically enable metacopy. */
820 		config->metacopy = true;
821 	}
822 
823 	/*
824 	 * This is to make the logic below simpler.  It doesn't make any other
825 	 * difference, since redirect_dir=on is only used for upper.
826 	 */
827 	if (!config->upperdir && config->redirect_mode == OVL_REDIRECT_FOLLOW)
828 		config->redirect_mode = OVL_REDIRECT_ON;
829 
830 	/* Resolve verity -> metacopy -> redirect_dir dependency */
831 	if (config->metacopy && config->redirect_mode != OVL_REDIRECT_ON) {
832 		if (set.metacopy && set.redirect) {
833 			pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
834 			       ovl_redirect_mode(config));
835 			return -EINVAL;
836 		}
837 		if (config->verity_mode && set.redirect) {
838 			pr_err("conflicting options: verity=%s,redirect_dir=%s\n",
839 			       ovl_verity_mode(config), ovl_redirect_mode(config));
840 			return -EINVAL;
841 		}
842 		if (set.redirect) {
843 			/*
844 			 * There was an explicit redirect_dir=... that resulted
845 			 * in this conflict.
846 			 */
847 			pr_info("disabling metacopy due to redirect_dir=%s\n",
848 				ovl_redirect_mode(config));
849 			config->metacopy = false;
850 		} else {
851 			/* Automatically enable redirect otherwise. */
852 			config->redirect_mode = OVL_REDIRECT_ON;
853 		}
854 	}
855 
856 	/* Resolve nfs_export -> index dependency */
857 	if (config->nfs_export && !config->index) {
858 		if (!config->upperdir &&
859 		    config->redirect_mode != OVL_REDIRECT_NOFOLLOW) {
860 			pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
861 			config->nfs_export = false;
862 		} else if (set.nfs_export && set.index) {
863 			pr_err("conflicting options: nfs_export=on,index=off\n");
864 			return -EINVAL;
865 		} else if (set.index) {
866 			/*
867 			 * There was an explicit index=off that resulted
868 			 * in this conflict.
869 			 */
870 			pr_info("disabling nfs_export due to index=off\n");
871 			config->nfs_export = false;
872 		} else {
873 			/* Automatically enable index otherwise. */
874 			config->index = true;
875 		}
876 	}
877 
878 	/* Resolve nfs_export -> !metacopy && !verity dependency */
879 	if (config->nfs_export && config->metacopy) {
880 		if (set.nfs_export && set.metacopy) {
881 			pr_err("conflicting options: nfs_export=on,metacopy=on\n");
882 			return -EINVAL;
883 		}
884 		if (set.metacopy) {
885 			/*
886 			 * There was an explicit metacopy=on that resulted
887 			 * in this conflict.
888 			 */
889 			pr_info("disabling nfs_export due to metacopy=on\n");
890 			config->nfs_export = false;
891 		} else if (config->verity_mode) {
892 			/*
893 			 * There was an explicit verity=.. that resulted
894 			 * in this conflict.
895 			 */
896 			pr_info("disabling nfs_export due to verity=%s\n",
897 				ovl_verity_mode(config));
898 			config->nfs_export = false;
899 		} else {
900 			/*
901 			 * There was an explicit nfs_export=on that resulted
902 			 * in this conflict.
903 			 */
904 			pr_info("disabling metacopy due to nfs_export=on\n");
905 			config->metacopy = false;
906 		}
907 	}
908 
909 
910 	/* Resolve userxattr -> !redirect && !metacopy && !verity dependency */
911 	if (config->userxattr) {
912 		if (set.redirect &&
913 		    config->redirect_mode != OVL_REDIRECT_NOFOLLOW) {
914 			pr_err("conflicting options: userxattr,redirect_dir=%s\n",
915 			       ovl_redirect_mode(config));
916 			return -EINVAL;
917 		}
918 		if (config->metacopy && set.metacopy) {
919 			pr_err("conflicting options: userxattr,metacopy=on\n");
920 			return -EINVAL;
921 		}
922 		if (config->verity_mode) {
923 			pr_err("conflicting options: userxattr,verity=%s\n",
924 			       ovl_verity_mode(config));
925 			return -EINVAL;
926 		}
927 		/*
928 		 * Silently disable default setting of redirect and metacopy.
929 		 * This shall be the default in the future as well: these
930 		 * options must be explicitly enabled if used together with
931 		 * userxattr.
932 		 */
933 		config->redirect_mode = OVL_REDIRECT_NOFOLLOW;
934 		config->metacopy = false;
935 	}
936 
937 	return 0;
938 }
939 
940 /**
941  * ovl_show_options
942  * @m: the seq_file handle
943  * @dentry: The dentry to query
944  *
945  * Prints the mount options for a given superblock.
946  * Returns zero; does not fail.
947  */
948 int ovl_show_options(struct seq_file *m, struct dentry *dentry)
949 {
950 	struct super_block *sb = dentry->d_sb;
951 	struct ovl_fs *ofs = OVL_FS(sb);
952 	size_t nr, nr_merged_lower, nr_lower = 0;
953 	char **lowerdirs = ofs->config.lowerdirs;
954 
955 	/*
956 	 * lowerdirs[0] holds the colon separated list that user provided
957 	 * with lowerdir mount option.
958 	 * lowerdirs[1..numlayer] hold the lowerdir paths that were added
959 	 * using the lowerdir+ and datadir+ mount options.
960 	 * For now, we do not allow mixing the legacy lowerdir mount option
961 	 * with the new lowerdir+ and datadir+ mount options.
962 	 */
963 	if (lowerdirs[0]) {
964 		seq_show_option(m, "lowerdir", lowerdirs[0]);
965 	} else {
966 		nr_lower = ofs->numlayer;
967 		nr_merged_lower = nr_lower - ofs->numdatalayer;
968 	}
969 	for (nr = 1; nr < nr_lower; nr++) {
970 		if (nr < nr_merged_lower)
971 			seq_show_option(m, "lowerdir+", lowerdirs[nr]);
972 		else
973 			seq_show_option(m, "datadir+", lowerdirs[nr]);
974 	}
975 	if (ofs->config.upperdir) {
976 		seq_show_option(m, "upperdir", ofs->config.upperdir);
977 		seq_show_option(m, "workdir", ofs->config.workdir);
978 	}
979 	if (ofs->config.default_permissions)
980 		seq_puts(m, ",default_permissions");
981 	if (ofs->config.redirect_mode != ovl_redirect_mode_def())
982 		seq_printf(m, ",redirect_dir=%s",
983 			   ovl_redirect_mode(&ofs->config));
984 	if (ofs->config.index != ovl_index_def)
985 		seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
986 	if (ofs->config.uuid != ovl_uuid_def())
987 		seq_printf(m, ",uuid=%s", ovl_uuid_mode(&ofs->config));
988 	if (ofs->config.nfs_export != ovl_nfs_export_def)
989 		seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
990 						"on" : "off");
991 	if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(ofs))
992 		seq_printf(m, ",xino=%s", ovl_xino_mode(&ofs->config));
993 	if (ofs->config.metacopy != ovl_metacopy_def)
994 		seq_printf(m, ",metacopy=%s",
995 			   ofs->config.metacopy ? "on" : "off");
996 	if (ofs->config.ovl_volatile)
997 		seq_puts(m, ",volatile");
998 	if (ofs->config.userxattr)
999 		seq_puts(m, ",userxattr");
1000 	if (ofs->config.verity_mode != ovl_verity_mode_def())
1001 		seq_printf(m, ",verity=%s",
1002 			   ovl_verity_mode(&ofs->config));
1003 	return 0;
1004 }
1005