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