xref: /openbmc/linux/fs/overlayfs/super.c (revision ac519625)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2011 Novell Inc.
5  */
6 
7 #include <uapi/linux/magic.h>
8 #include <linux/fs.h>
9 #include <linux/namei.h>
10 #include <linux/xattr.h>
11 #include <linux/mount.h>
12 #include <linux/parser.h>
13 #include <linux/module.h>
14 #include <linux/statfs.h>
15 #include <linux/seq_file.h>
16 #include <linux/posix_acl_xattr.h>
17 #include <linux/exportfs.h>
18 #include <linux/file.h>
19 #include <linux/fs_parser.h>
20 #include "overlayfs.h"
21 
22 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
23 MODULE_DESCRIPTION("Overlay filesystem");
24 MODULE_LICENSE("GPL");
25 
26 
27 struct ovl_dir_cache;
28 
29 #define OVL_MAX_STACK 500
30 
31 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
32 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
33 MODULE_PARM_DESC(redirect_dir,
34 		 "Default to on or off for the redirect_dir feature");
35 
36 static bool ovl_redirect_always_follow =
37 	IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
38 module_param_named(redirect_always_follow, ovl_redirect_always_follow,
39 		   bool, 0644);
40 MODULE_PARM_DESC(redirect_always_follow,
41 		 "Follow redirects even if redirect_dir feature is turned off");
42 
43 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
44 module_param_named(index, ovl_index_def, bool, 0644);
45 MODULE_PARM_DESC(index,
46 		 "Default to on or off for the inodes index feature");
47 
48 static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
49 module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
50 MODULE_PARM_DESC(nfs_export,
51 		 "Default to on or off for the NFS export feature");
52 
53 static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
54 module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
55 MODULE_PARM_DESC(xino_auto,
56 		 "Auto enable xino feature");
57 
58 static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
59 module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
60 MODULE_PARM_DESC(metacopy,
61 		 "Default to on or off for the metadata only copy up feature");
62 
63 static struct dentry *ovl_d_real(struct dentry *dentry,
64 				 const struct inode *inode)
65 {
66 	struct dentry *real = NULL, *lower;
67 
68 	/* It's an overlay file */
69 	if (inode && d_inode(dentry) == inode)
70 		return dentry;
71 
72 	if (!d_is_reg(dentry)) {
73 		if (!inode || inode == d_inode(dentry))
74 			return dentry;
75 		goto bug;
76 	}
77 
78 	real = ovl_dentry_upper(dentry);
79 	if (real && (inode == d_inode(real)))
80 		return real;
81 
82 	if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
83 		return real;
84 
85 	/*
86 	 * Best effort lazy lookup of lowerdata for !inode case to return
87 	 * the real lowerdata dentry.  The only current caller of d_real() with
88 	 * NULL inode is d_real_inode() from trace_uprobe and this caller is
89 	 * likely going to be followed reading from the file, before placing
90 	 * uprobes on offset within the file, so lowerdata should be available
91 	 * when setting the uprobe.
92 	 */
93 	ovl_maybe_lookup_lowerdata(dentry);
94 	lower = ovl_dentry_lowerdata(dentry);
95 	if (!lower)
96 		goto bug;
97 	real = lower;
98 
99 	/* Handle recursion */
100 	real = d_real(real, inode);
101 
102 	if (!inode || inode == d_inode(real))
103 		return real;
104 bug:
105 	WARN(1, "%s(%pd4, %s:%lu): real dentry (%p/%lu) not found\n",
106 	     __func__, dentry, inode ? inode->i_sb->s_id : "NULL",
107 	     inode ? inode->i_ino : 0, real,
108 	     real && d_inode(real) ? d_inode(real)->i_ino : 0);
109 	return dentry;
110 }
111 
112 static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
113 {
114 	int ret = 1;
115 
116 	if (!d)
117 		return 1;
118 
119 	if (weak) {
120 		if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE)
121 			ret =  d->d_op->d_weak_revalidate(d, flags);
122 	} else if (d->d_flags & DCACHE_OP_REVALIDATE) {
123 		ret = d->d_op->d_revalidate(d, flags);
124 		if (!ret) {
125 			if (!(flags & LOOKUP_RCU))
126 				d_invalidate(d);
127 			ret = -ESTALE;
128 		}
129 	}
130 	return ret;
131 }
132 
133 static int ovl_dentry_revalidate_common(struct dentry *dentry,
134 					unsigned int flags, bool weak)
135 {
136 	struct ovl_entry *oe = OVL_E(dentry);
137 	struct ovl_path *lowerstack = ovl_lowerstack(oe);
138 	struct inode *inode = d_inode_rcu(dentry);
139 	struct dentry *upper;
140 	unsigned int i;
141 	int ret = 1;
142 
143 	/* Careful in RCU mode */
144 	if (!inode)
145 		return -ECHILD;
146 
147 	upper = ovl_i_dentry_upper(inode);
148 	if (upper)
149 		ret = ovl_revalidate_real(upper, flags, weak);
150 
151 	for (i = 0; ret > 0 && i < ovl_numlower(oe); i++)
152 		ret = ovl_revalidate_real(lowerstack[i].dentry, flags, weak);
153 
154 	return ret;
155 }
156 
157 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
158 {
159 	return ovl_dentry_revalidate_common(dentry, flags, false);
160 }
161 
162 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
163 {
164 	return ovl_dentry_revalidate_common(dentry, flags, true);
165 }
166 
167 static const struct dentry_operations ovl_dentry_operations = {
168 	.d_real = ovl_d_real,
169 	.d_revalidate = ovl_dentry_revalidate,
170 	.d_weak_revalidate = ovl_dentry_weak_revalidate,
171 };
172 
173 static struct kmem_cache *ovl_inode_cachep;
174 
175 static struct inode *ovl_alloc_inode(struct super_block *sb)
176 {
177 	struct ovl_inode *oi = alloc_inode_sb(sb, ovl_inode_cachep, GFP_KERNEL);
178 
179 	if (!oi)
180 		return NULL;
181 
182 	oi->cache = NULL;
183 	oi->redirect = NULL;
184 	oi->version = 0;
185 	oi->flags = 0;
186 	oi->__upperdentry = NULL;
187 	oi->lowerdata_redirect = NULL;
188 	oi->oe = NULL;
189 	mutex_init(&oi->lock);
190 
191 	return &oi->vfs_inode;
192 }
193 
194 static void ovl_free_inode(struct inode *inode)
195 {
196 	struct ovl_inode *oi = OVL_I(inode);
197 
198 	kfree(oi->redirect);
199 	mutex_destroy(&oi->lock);
200 	kmem_cache_free(ovl_inode_cachep, oi);
201 }
202 
203 static void ovl_destroy_inode(struct inode *inode)
204 {
205 	struct ovl_inode *oi = OVL_I(inode);
206 
207 	dput(oi->__upperdentry);
208 	ovl_free_entry(oi->oe);
209 	if (S_ISDIR(inode->i_mode))
210 		ovl_dir_cache_free(inode);
211 	else
212 		kfree(oi->lowerdata_redirect);
213 }
214 
215 static void ovl_free_fs(struct ovl_fs *ofs)
216 {
217 	struct vfsmount **mounts;
218 	unsigned i;
219 
220 	iput(ofs->workbasedir_trap);
221 	iput(ofs->indexdir_trap);
222 	iput(ofs->workdir_trap);
223 	dput(ofs->whiteout);
224 	dput(ofs->indexdir);
225 	dput(ofs->workdir);
226 	if (ofs->workdir_locked)
227 		ovl_inuse_unlock(ofs->workbasedir);
228 	dput(ofs->workbasedir);
229 	if (ofs->upperdir_locked)
230 		ovl_inuse_unlock(ovl_upper_mnt(ofs)->mnt_root);
231 
232 	/* Hack!  Reuse ofs->layers as a vfsmount array before freeing it */
233 	mounts = (struct vfsmount **) ofs->layers;
234 	for (i = 0; i < ofs->numlayer; i++) {
235 		iput(ofs->layers[i].trap);
236 		mounts[i] = ofs->layers[i].mnt;
237 	}
238 	kern_unmount_array(mounts, ofs->numlayer);
239 	kfree(ofs->layers);
240 	for (i = 0; i < ofs->numfs; i++)
241 		free_anon_bdev(ofs->fs[i].pseudo_dev);
242 	kfree(ofs->fs);
243 
244 	kfree(ofs->config.lowerdir);
245 	kfree(ofs->config.upperdir);
246 	kfree(ofs->config.workdir);
247 	if (ofs->creator_cred)
248 		put_cred(ofs->creator_cred);
249 	kfree(ofs);
250 }
251 
252 static void ovl_put_super(struct super_block *sb)
253 {
254 	struct ovl_fs *ofs = sb->s_fs_info;
255 
256 	ovl_free_fs(ofs);
257 }
258 
259 /* Sync real dirty inodes in upper filesystem (if it exists) */
260 static int ovl_sync_fs(struct super_block *sb, int wait)
261 {
262 	struct ovl_fs *ofs = sb->s_fs_info;
263 	struct super_block *upper_sb;
264 	int ret;
265 
266 	ret = ovl_sync_status(ofs);
267 	/*
268 	 * We have to always set the err, because the return value isn't
269 	 * checked in syncfs, and instead indirectly return an error via
270 	 * the sb's writeback errseq, which VFS inspects after this call.
271 	 */
272 	if (ret < 0) {
273 		errseq_set(&sb->s_wb_err, -EIO);
274 		return -EIO;
275 	}
276 
277 	if (!ret)
278 		return ret;
279 
280 	/*
281 	 * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
282 	 * All the super blocks will be iterated, including upper_sb.
283 	 *
284 	 * If this is a syncfs(2) call, then we do need to call
285 	 * sync_filesystem() on upper_sb, but enough if we do it when being
286 	 * called with wait == 1.
287 	 */
288 	if (!wait)
289 		return 0;
290 
291 	upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
292 
293 	down_read(&upper_sb->s_umount);
294 	ret = sync_filesystem(upper_sb);
295 	up_read(&upper_sb->s_umount);
296 
297 	return ret;
298 }
299 
300 /**
301  * ovl_statfs
302  * @dentry: The dentry to query
303  * @buf: The struct kstatfs to fill in with stats
304  *
305  * Get the filesystem statistics.  As writes always target the upper layer
306  * filesystem pass the statfs to the upper filesystem (if it exists)
307  */
308 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
309 {
310 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
311 	struct dentry *root_dentry = dentry->d_sb->s_root;
312 	struct path path;
313 	int err;
314 
315 	ovl_path_real(root_dentry, &path);
316 
317 	err = vfs_statfs(&path, buf);
318 	if (!err) {
319 		buf->f_namelen = ofs->namelen;
320 		buf->f_type = OVERLAYFS_SUPER_MAGIC;
321 	}
322 
323 	return err;
324 }
325 
326 /* Will this overlay be forced to mount/remount ro? */
327 static bool ovl_force_readonly(struct ovl_fs *ofs)
328 {
329 	return (!ovl_upper_mnt(ofs) || !ofs->workdir);
330 }
331 
332 static const struct constant_table ovl_parameter_redirect_dir[] = {
333 	{ "off",	OVL_REDIRECT_OFF      },
334 	{ "follow",	OVL_REDIRECT_FOLLOW   },
335 	{ "nofollow",	OVL_REDIRECT_NOFOLLOW },
336 	{ "on",		OVL_REDIRECT_ON       },
337 	{}
338 };
339 
340 static const char *ovl_redirect_mode(struct ovl_config *config)
341 {
342 	return ovl_parameter_redirect_dir[config->redirect_mode].name;
343 }
344 
345 static int ovl_redirect_mode_def(void)
346 {
347 	return ovl_redirect_dir_def ? OVL_REDIRECT_ON :
348 		ovl_redirect_always_follow ? OVL_REDIRECT_FOLLOW :
349 					     OVL_REDIRECT_NOFOLLOW;
350 }
351 
352 static const struct constant_table ovl_parameter_xino[] = {
353 	{ "off",	OVL_XINO_OFF  },
354 	{ "auto",	OVL_XINO_AUTO },
355 	{ "on",		OVL_XINO_ON   },
356 	{}
357 };
358 
359 static const char *ovl_xino_mode(struct ovl_config *config)
360 {
361 	return ovl_parameter_xino[config->xino].name;
362 }
363 
364 static inline int ovl_xino_def(void)
365 {
366 	return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
367 }
368 
369 /**
370  * ovl_show_options
371  * @m: the seq_file handle
372  * @dentry: The dentry to query
373  *
374  * Prints the mount options for a given superblock.
375  * Returns zero; does not fail.
376  */
377 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
378 {
379 	struct super_block *sb = dentry->d_sb;
380 	struct ovl_fs *ofs = sb->s_fs_info;
381 
382 	seq_show_option(m, "lowerdir", ofs->config.lowerdir);
383 	if (ofs->config.upperdir) {
384 		seq_show_option(m, "upperdir", ofs->config.upperdir);
385 		seq_show_option(m, "workdir", ofs->config.workdir);
386 	}
387 	if (ofs->config.default_permissions)
388 		seq_puts(m, ",default_permissions");
389 	if (ofs->config.redirect_mode != ovl_redirect_mode_def())
390 		seq_printf(m, ",redirect_dir=%s",
391 			   ovl_redirect_mode(&ofs->config));
392 	if (ofs->config.index != ovl_index_def)
393 		seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
394 	if (!ofs->config.uuid)
395 		seq_puts(m, ",uuid=off");
396 	if (ofs->config.nfs_export != ovl_nfs_export_def)
397 		seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
398 						"on" : "off");
399 	if (ofs->config.xino != ovl_xino_def() && !ovl_same_fs(ofs))
400 		seq_printf(m, ",xino=%s", ovl_xino_mode(&ofs->config));
401 	if (ofs->config.metacopy != ovl_metacopy_def)
402 		seq_printf(m, ",metacopy=%s",
403 			   ofs->config.metacopy ? "on" : "off");
404 	if (ofs->config.ovl_volatile)
405 		seq_puts(m, ",volatile");
406 	if (ofs->config.userxattr)
407 		seq_puts(m, ",userxattr");
408 	return 0;
409 }
410 
411 static int ovl_remount(struct super_block *sb, int *flags, char *data)
412 {
413 	struct ovl_fs *ofs = sb->s_fs_info;
414 	struct super_block *upper_sb;
415 	int ret = 0;
416 
417 	if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
418 		return -EROFS;
419 
420 	if (*flags & SB_RDONLY && !sb_rdonly(sb)) {
421 		upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
422 		if (ovl_should_sync(ofs)) {
423 			down_read(&upper_sb->s_umount);
424 			ret = sync_filesystem(upper_sb);
425 			up_read(&upper_sb->s_umount);
426 		}
427 	}
428 
429 	return ret;
430 }
431 
432 static const struct super_operations ovl_super_operations = {
433 	.alloc_inode	= ovl_alloc_inode,
434 	.free_inode	= ovl_free_inode,
435 	.destroy_inode	= ovl_destroy_inode,
436 	.drop_inode	= generic_delete_inode,
437 	.put_super	= ovl_put_super,
438 	.sync_fs	= ovl_sync_fs,
439 	.statfs		= ovl_statfs,
440 	.show_options	= ovl_show_options,
441 	.remount_fs	= ovl_remount,
442 };
443 
444 enum {
445 	OPT_LOWERDIR,
446 	OPT_UPPERDIR,
447 	OPT_WORKDIR,
448 	OPT_DEFAULT_PERMISSIONS,
449 	OPT_REDIRECT_DIR_ON,
450 	OPT_REDIRECT_DIR_OFF,
451 	OPT_REDIRECT_DIR_FOLLOW,
452 	OPT_REDIRECT_DIR_NOFOLLOW,
453 	OPT_INDEX_ON,
454 	OPT_INDEX_OFF,
455 	OPT_UUID_ON,
456 	OPT_UUID_OFF,
457 	OPT_NFS_EXPORT_ON,
458 	OPT_USERXATTR,
459 	OPT_NFS_EXPORT_OFF,
460 	OPT_XINO_ON,
461 	OPT_XINO_OFF,
462 	OPT_XINO_AUTO,
463 	OPT_METACOPY_ON,
464 	OPT_METACOPY_OFF,
465 	OPT_VOLATILE,
466 	OPT_ERR,
467 };
468 
469 static const match_table_t ovl_tokens = {
470 	{OPT_LOWERDIR,			"lowerdir=%s"},
471 	{OPT_UPPERDIR,			"upperdir=%s"},
472 	{OPT_WORKDIR,			"workdir=%s"},
473 	{OPT_DEFAULT_PERMISSIONS,	"default_permissions"},
474 	{OPT_REDIRECT_DIR_ON,		"redirect_dir=on"},
475 	{OPT_REDIRECT_DIR_OFF,		"redirect_dir=off"},
476 	{OPT_REDIRECT_DIR_FOLLOW,	"redirect_dir=follow"},
477 	{OPT_REDIRECT_DIR_NOFOLLOW,	"redirect_dir=nofollow"},
478 	{OPT_INDEX_ON,			"index=on"},
479 	{OPT_INDEX_OFF,			"index=off"},
480 	{OPT_USERXATTR,			"userxattr"},
481 	{OPT_UUID_ON,			"uuid=on"},
482 	{OPT_UUID_OFF,			"uuid=off"},
483 	{OPT_NFS_EXPORT_ON,		"nfs_export=on"},
484 	{OPT_NFS_EXPORT_OFF,		"nfs_export=off"},
485 	{OPT_XINO_ON,			"xino=on"},
486 	{OPT_XINO_OFF,			"xino=off"},
487 	{OPT_XINO_AUTO,			"xino=auto"},
488 	{OPT_METACOPY_ON,		"metacopy=on"},
489 	{OPT_METACOPY_OFF,		"metacopy=off"},
490 	{OPT_VOLATILE,			"volatile"},
491 	{OPT_ERR,			NULL}
492 };
493 
494 static char *ovl_next_opt(char **s)
495 {
496 	char *sbegin = *s;
497 	char *p;
498 
499 	if (sbegin == NULL)
500 		return NULL;
501 
502 	for (p = sbegin; *p; p++) {
503 		if (*p == '\\') {
504 			p++;
505 			if (!*p)
506 				break;
507 		} else if (*p == ',') {
508 			*p = '\0';
509 			*s = p + 1;
510 			return sbegin;
511 		}
512 	}
513 	*s = NULL;
514 	return sbegin;
515 }
516 
517 static int ovl_parse_opt(char *opt, struct ovl_config *config,
518 			 struct ovl_opt_set *set)
519 {
520 	int err = 0;
521 	int token;
522 	substring_t args[MAX_OPT_ARGS];
523 
524 	if (!*opt)
525 		return 0;
526 
527 	token = match_token(opt, ovl_tokens, args);
528 	switch (token) {
529 	case OPT_UPPERDIR:
530 		kfree(config->upperdir);
531 		config->upperdir = match_strdup(&args[0]);
532 		if (!config->upperdir)
533 			return -ENOMEM;
534 		break;
535 
536 	case OPT_LOWERDIR:
537 		kfree(config->lowerdir);
538 		config->lowerdir = match_strdup(&args[0]);
539 		if (!config->lowerdir)
540 			return -ENOMEM;
541 		break;
542 
543 	case OPT_WORKDIR:
544 		kfree(config->workdir);
545 		config->workdir = match_strdup(&args[0]);
546 		if (!config->workdir)
547 			return -ENOMEM;
548 		break;
549 
550 	case OPT_DEFAULT_PERMISSIONS:
551 		config->default_permissions = true;
552 		break;
553 
554 	case OPT_REDIRECT_DIR_ON:
555 		config->redirect_mode = OVL_REDIRECT_ON;
556 		set->redirect = true;
557 		break;
558 
559 	case OPT_REDIRECT_DIR_OFF:
560 		config->redirect_mode = ovl_redirect_always_follow ?
561 			OVL_REDIRECT_FOLLOW :
562 			OVL_REDIRECT_NOFOLLOW;
563 		set->redirect = true;
564 		break;
565 
566 	case OPT_REDIRECT_DIR_FOLLOW:
567 		config->redirect_mode = OVL_REDIRECT_FOLLOW;
568 		set->redirect = true;
569 		break;
570 
571 	case OPT_REDIRECT_DIR_NOFOLLOW:
572 		config->redirect_mode = OVL_REDIRECT_NOFOLLOW;
573 		set->redirect = true;
574 		break;
575 
576 	case OPT_INDEX_ON:
577 		config->index = true;
578 		set->index = true;
579 		break;
580 
581 	case OPT_INDEX_OFF:
582 		config->index = false;
583 		set->index = true;
584 		break;
585 
586 	case OPT_UUID_ON:
587 		config->uuid = true;
588 		break;
589 
590 	case OPT_UUID_OFF:
591 		config->uuid = false;
592 		break;
593 
594 	case OPT_NFS_EXPORT_ON:
595 		config->nfs_export = true;
596 		set->nfs_export = true;
597 		break;
598 
599 	case OPT_NFS_EXPORT_OFF:
600 		config->nfs_export = false;
601 		set->nfs_export = true;
602 		break;
603 
604 	case OPT_XINO_ON:
605 		config->xino = OVL_XINO_ON;
606 		break;
607 
608 	case OPT_XINO_OFF:
609 		config->xino = OVL_XINO_OFF;
610 		break;
611 
612 	case OPT_XINO_AUTO:
613 		config->xino = OVL_XINO_AUTO;
614 		break;
615 
616 	case OPT_METACOPY_ON:
617 		config->metacopy = true;
618 		set->metacopy = true;
619 		break;
620 
621 	case OPT_METACOPY_OFF:
622 		config->metacopy = false;
623 		set->metacopy = true;
624 		break;
625 
626 	case OPT_VOLATILE:
627 		config->ovl_volatile = true;
628 		break;
629 
630 	case OPT_USERXATTR:
631 		config->userxattr = true;
632 		break;
633 
634 	default:
635 		pr_err("unrecognized mount option \"%s\" or missing value\n",
636 		       opt);
637 		return -EINVAL;
638 	}
639 
640 	return err;
641 }
642 
643 static int ovl_parse_options(char *opt, struct ovl_config *config)
644 {
645 	char *p;
646 	int err;
647 	struct ovl_opt_set set = {};
648 
649 	while ((p = ovl_next_opt(&opt)) != NULL) {
650 		err = ovl_parse_opt(p, config, &set);
651 		if (err)
652 			return err;
653 	}
654 
655 	/* Workdir/index are useless in non-upper mount */
656 	if (!config->upperdir) {
657 		if (config->workdir) {
658 			pr_info("option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
659 				config->workdir);
660 			kfree(config->workdir);
661 			config->workdir = NULL;
662 		}
663 		if (config->index && set.index) {
664 			pr_info("option \"index=on\" is useless in a non-upper mount, ignore\n");
665 			set.index = false;
666 		}
667 		config->index = false;
668 	}
669 
670 	if (!config->upperdir && config->ovl_volatile) {
671 		pr_info("option \"volatile\" is meaningless in a non-upper mount, ignoring it.\n");
672 		config->ovl_volatile = false;
673 	}
674 
675 	/*
676 	 * This is to make the logic below simpler.  It doesn't make any other
677 	 * difference, since redirect_dir=on is only used for upper.
678 	 */
679 	if (!config->upperdir && config->redirect_mode == OVL_REDIRECT_FOLLOW)
680 		config->redirect_mode = OVL_REDIRECT_ON;
681 
682 	/* Resolve metacopy -> redirect_dir dependency */
683 	if (config->metacopy && config->redirect_mode != OVL_REDIRECT_ON) {
684 		if (set.metacopy && set.redirect) {
685 			pr_err("conflicting options: metacopy=on,redirect_dir=%s\n",
686 			       ovl_redirect_mode(config));
687 			return -EINVAL;
688 		}
689 		if (set.redirect) {
690 			/*
691 			 * There was an explicit redirect_dir=... that resulted
692 			 * in this conflict.
693 			 */
694 			pr_info("disabling metacopy due to redirect_dir=%s\n",
695 				ovl_redirect_mode(config));
696 			config->metacopy = false;
697 		} else {
698 			/* Automatically enable redirect otherwise. */
699 			config->redirect_mode = OVL_REDIRECT_ON;
700 		}
701 	}
702 
703 	/* Resolve nfs_export -> index dependency */
704 	if (config->nfs_export && !config->index) {
705 		if (!config->upperdir &&
706 		    config->redirect_mode != OVL_REDIRECT_NOFOLLOW) {
707 			pr_info("NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
708 			config->nfs_export = false;
709 		} else if (set.nfs_export && set.index) {
710 			pr_err("conflicting options: nfs_export=on,index=off\n");
711 			return -EINVAL;
712 		} else if (set.index) {
713 			/*
714 			 * There was an explicit index=off that resulted
715 			 * in this conflict.
716 			 */
717 			pr_info("disabling nfs_export due to index=off\n");
718 			config->nfs_export = false;
719 		} else {
720 			/* Automatically enable index otherwise. */
721 			config->index = true;
722 		}
723 	}
724 
725 	/* Resolve nfs_export -> !metacopy dependency */
726 	if (config->nfs_export && config->metacopy) {
727 		if (set.nfs_export && set.metacopy) {
728 			pr_err("conflicting options: nfs_export=on,metacopy=on\n");
729 			return -EINVAL;
730 		}
731 		if (set.metacopy) {
732 			/*
733 			 * There was an explicit metacopy=on that resulted
734 			 * in this conflict.
735 			 */
736 			pr_info("disabling nfs_export due to metacopy=on\n");
737 			config->nfs_export = false;
738 		} else {
739 			/*
740 			 * There was an explicit nfs_export=on that resulted
741 			 * in this conflict.
742 			 */
743 			pr_info("disabling metacopy due to nfs_export=on\n");
744 			config->metacopy = false;
745 		}
746 	}
747 
748 
749 	/* Resolve userxattr -> !redirect && !metacopy dependency */
750 	if (config->userxattr) {
751 		if (set.redirect &&
752 		    config->redirect_mode != OVL_REDIRECT_NOFOLLOW) {
753 			pr_err("conflicting options: userxattr,redirect_dir=%s\n",
754 			       ovl_redirect_mode(config));
755 			return -EINVAL;
756 		}
757 		if (config->metacopy && set.metacopy) {
758 			pr_err("conflicting options: userxattr,metacopy=on\n");
759 			return -EINVAL;
760 		}
761 		/*
762 		 * Silently disable default setting of redirect and metacopy.
763 		 * This shall be the default in the future as well: these
764 		 * options must be explicitly enabled if used together with
765 		 * userxattr.
766 		 */
767 		config->redirect_mode = OVL_REDIRECT_NOFOLLOW;
768 		config->metacopy = false;
769 	}
770 
771 	return 0;
772 }
773 
774 #define OVL_WORKDIR_NAME "work"
775 #define OVL_INDEXDIR_NAME "index"
776 
777 static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
778 					 const char *name, bool persist)
779 {
780 	struct inode *dir =  ofs->workbasedir->d_inode;
781 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
782 	struct dentry *work;
783 	int err;
784 	bool retried = false;
785 
786 	inode_lock_nested(dir, I_MUTEX_PARENT);
787 retry:
788 	work = ovl_lookup_upper(ofs, name, ofs->workbasedir, strlen(name));
789 
790 	if (!IS_ERR(work)) {
791 		struct iattr attr = {
792 			.ia_valid = ATTR_MODE,
793 			.ia_mode = S_IFDIR | 0,
794 		};
795 
796 		if (work->d_inode) {
797 			err = -EEXIST;
798 			if (retried)
799 				goto out_dput;
800 
801 			if (persist)
802 				goto out_unlock;
803 
804 			retried = true;
805 			err = ovl_workdir_cleanup(ofs, dir, mnt, work, 0);
806 			dput(work);
807 			if (err == -EINVAL) {
808 				work = ERR_PTR(err);
809 				goto out_unlock;
810 			}
811 			goto retry;
812 		}
813 
814 		err = ovl_mkdir_real(ofs, dir, &work, attr.ia_mode);
815 		if (err)
816 			goto out_dput;
817 
818 		/* Weird filesystem returning with hashed negative (kernfs)? */
819 		err = -EINVAL;
820 		if (d_really_is_negative(work))
821 			goto out_dput;
822 
823 		/*
824 		 * Try to remove POSIX ACL xattrs from workdir.  We are good if:
825 		 *
826 		 * a) success (there was a POSIX ACL xattr and was removed)
827 		 * b) -ENODATA (there was no POSIX ACL xattr)
828 		 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
829 		 *
830 		 * There are various other error values that could effectively
831 		 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
832 		 * if the xattr name is too long), but the set of filesystems
833 		 * allowed as upper are limited to "normal" ones, where checking
834 		 * for the above two errors is sufficient.
835 		 */
836 		err = ovl_do_remove_acl(ofs, work, XATTR_NAME_POSIX_ACL_DEFAULT);
837 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
838 			goto out_dput;
839 
840 		err = ovl_do_remove_acl(ofs, work, XATTR_NAME_POSIX_ACL_ACCESS);
841 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
842 			goto out_dput;
843 
844 		/* Clear any inherited mode bits */
845 		inode_lock(work->d_inode);
846 		err = ovl_do_notify_change(ofs, work, &attr);
847 		inode_unlock(work->d_inode);
848 		if (err)
849 			goto out_dput;
850 	} else {
851 		err = PTR_ERR(work);
852 		goto out_err;
853 	}
854 out_unlock:
855 	inode_unlock(dir);
856 	return work;
857 
858 out_dput:
859 	dput(work);
860 out_err:
861 	pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
862 		ofs->config.workdir, name, -err);
863 	work = NULL;
864 	goto out_unlock;
865 }
866 
867 static void ovl_unescape(char *s)
868 {
869 	char *d = s;
870 
871 	for (;; s++, d++) {
872 		if (*s == '\\')
873 			s++;
874 		*d = *s;
875 		if (!*s)
876 			break;
877 	}
878 }
879 
880 static int ovl_mount_dir_noesc(const char *name, struct path *path)
881 {
882 	int err = -EINVAL;
883 
884 	if (!*name) {
885 		pr_err("empty lowerdir\n");
886 		goto out;
887 	}
888 	err = kern_path(name, LOOKUP_FOLLOW, path);
889 	if (err) {
890 		pr_err("failed to resolve '%s': %i\n", name, err);
891 		goto out;
892 	}
893 	err = -EINVAL;
894 	if (ovl_dentry_weird(path->dentry)) {
895 		pr_err("filesystem on '%s' not supported\n", name);
896 		goto out_put;
897 	}
898 	if (!d_is_dir(path->dentry)) {
899 		pr_err("'%s' not a directory\n", name);
900 		goto out_put;
901 	}
902 	return 0;
903 
904 out_put:
905 	path_put_init(path);
906 out:
907 	return err;
908 }
909 
910 static int ovl_mount_dir(const char *name, struct path *path)
911 {
912 	int err = -ENOMEM;
913 	char *tmp = kstrdup(name, GFP_KERNEL);
914 
915 	if (tmp) {
916 		ovl_unescape(tmp);
917 		err = ovl_mount_dir_noesc(tmp, path);
918 
919 		if (!err && path->dentry->d_flags & DCACHE_OP_REAL) {
920 			pr_err("filesystem on '%s' not supported as upperdir\n",
921 			       tmp);
922 			path_put_init(path);
923 			err = -EINVAL;
924 		}
925 		kfree(tmp);
926 	}
927 	return err;
928 }
929 
930 static int ovl_check_namelen(const struct path *path, struct ovl_fs *ofs,
931 			     const char *name)
932 {
933 	struct kstatfs statfs;
934 	int err = vfs_statfs(path, &statfs);
935 
936 	if (err)
937 		pr_err("statfs failed on '%s'\n", name);
938 	else
939 		ofs->namelen = max(ofs->namelen, statfs.f_namelen);
940 
941 	return err;
942 }
943 
944 static int ovl_lower_dir(const char *name, struct path *path,
945 			 struct ovl_fs *ofs, int *stack_depth)
946 {
947 	int fh_type;
948 	int err;
949 
950 	err = ovl_mount_dir_noesc(name, path);
951 	if (err)
952 		return err;
953 
954 	err = ovl_check_namelen(path, ofs, name);
955 	if (err)
956 		return err;
957 
958 	*stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
959 
960 	/*
961 	 * The inodes index feature and NFS export need to encode and decode
962 	 * file handles, so they require that all layers support them.
963 	 */
964 	fh_type = ovl_can_decode_fh(path->dentry->d_sb);
965 	if ((ofs->config.nfs_export ||
966 	     (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
967 		ofs->config.index = false;
968 		ofs->config.nfs_export = false;
969 		pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
970 			name);
971 	}
972 	/*
973 	 * Decoding origin file handle is required for persistent st_ino.
974 	 * Without persistent st_ino, xino=auto falls back to xino=off.
975 	 */
976 	if (ofs->config.xino == OVL_XINO_AUTO &&
977 	    ofs->config.upperdir && !fh_type) {
978 		ofs->config.xino = OVL_XINO_OFF;
979 		pr_warn("fs on '%s' does not support file handles, falling back to xino=off.\n",
980 			name);
981 	}
982 
983 	/* Check if lower fs has 32bit inode numbers */
984 	if (fh_type != FILEID_INO32_GEN)
985 		ofs->xino_mode = -1;
986 
987 	return 0;
988 }
989 
990 /* Workdir should not be subdir of upperdir and vice versa */
991 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
992 {
993 	bool ok = false;
994 
995 	if (workdir != upperdir) {
996 		ok = (lock_rename(workdir, upperdir) == NULL);
997 		unlock_rename(workdir, upperdir);
998 	}
999 	return ok;
1000 }
1001 
1002 static unsigned int ovl_split_lowerdirs(char *str)
1003 {
1004 	unsigned int ctr = 1;
1005 	char *s, *d;
1006 
1007 	for (s = d = str;; s++, d++) {
1008 		if (*s == '\\') {
1009 			s++;
1010 		} else if (*s == ':') {
1011 			*d = '\0';
1012 			ctr++;
1013 			continue;
1014 		}
1015 		*d = *s;
1016 		if (!*s)
1017 			break;
1018 	}
1019 	return ctr;
1020 }
1021 
1022 static int ovl_own_xattr_get(const struct xattr_handler *handler,
1023 			     struct dentry *dentry, struct inode *inode,
1024 			     const char *name, void *buffer, size_t size)
1025 {
1026 	return -EOPNOTSUPP;
1027 }
1028 
1029 static int ovl_own_xattr_set(const struct xattr_handler *handler,
1030 			     struct mnt_idmap *idmap,
1031 			     struct dentry *dentry, struct inode *inode,
1032 			     const char *name, const void *value,
1033 			     size_t size, int flags)
1034 {
1035 	return -EOPNOTSUPP;
1036 }
1037 
1038 static int ovl_other_xattr_get(const struct xattr_handler *handler,
1039 			       struct dentry *dentry, struct inode *inode,
1040 			       const char *name, void *buffer, size_t size)
1041 {
1042 	return ovl_xattr_get(dentry, inode, name, buffer, size);
1043 }
1044 
1045 static int ovl_other_xattr_set(const struct xattr_handler *handler,
1046 			       struct mnt_idmap *idmap,
1047 			       struct dentry *dentry, struct inode *inode,
1048 			       const char *name, const void *value,
1049 			       size_t size, int flags)
1050 {
1051 	return ovl_xattr_set(dentry, inode, name, value, size, flags);
1052 }
1053 
1054 static const struct xattr_handler ovl_own_trusted_xattr_handler = {
1055 	.prefix	= OVL_XATTR_TRUSTED_PREFIX,
1056 	.get = ovl_own_xattr_get,
1057 	.set = ovl_own_xattr_set,
1058 };
1059 
1060 static const struct xattr_handler ovl_own_user_xattr_handler = {
1061 	.prefix	= OVL_XATTR_USER_PREFIX,
1062 	.get = ovl_own_xattr_get,
1063 	.set = ovl_own_xattr_set,
1064 };
1065 
1066 static const struct xattr_handler ovl_other_xattr_handler = {
1067 	.prefix	= "", /* catch all */
1068 	.get = ovl_other_xattr_get,
1069 	.set = ovl_other_xattr_set,
1070 };
1071 
1072 static const struct xattr_handler *ovl_trusted_xattr_handlers[] = {
1073 	&ovl_own_trusted_xattr_handler,
1074 	&ovl_other_xattr_handler,
1075 	NULL
1076 };
1077 
1078 static const struct xattr_handler *ovl_user_xattr_handlers[] = {
1079 	&ovl_own_user_xattr_handler,
1080 	&ovl_other_xattr_handler,
1081 	NULL
1082 };
1083 
1084 static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
1085 			  struct inode **ptrap, const char *name)
1086 {
1087 	struct inode *trap;
1088 	int err;
1089 
1090 	trap = ovl_get_trap_inode(sb, dir);
1091 	err = PTR_ERR_OR_ZERO(trap);
1092 	if (err) {
1093 		if (err == -ELOOP)
1094 			pr_err("conflicting %s path\n", name);
1095 		return err;
1096 	}
1097 
1098 	*ptrap = trap;
1099 	return 0;
1100 }
1101 
1102 /*
1103  * Determine how we treat concurrent use of upperdir/workdir based on the
1104  * index feature. This is papering over mount leaks of container runtimes,
1105  * for example, an old overlay mount is leaked and now its upperdir is
1106  * attempted to be used as a lower layer in a new overlay mount.
1107  */
1108 static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
1109 {
1110 	if (ofs->config.index) {
1111 		pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
1112 		       name);
1113 		return -EBUSY;
1114 	} else {
1115 		pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
1116 			name);
1117 		return 0;
1118 	}
1119 }
1120 
1121 static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
1122 			 struct ovl_layer *upper_layer, struct path *upperpath)
1123 {
1124 	struct vfsmount *upper_mnt;
1125 	int err;
1126 
1127 	err = ovl_mount_dir(ofs->config.upperdir, upperpath);
1128 	if (err)
1129 		goto out;
1130 
1131 	/* Upperdir path should not be r/o */
1132 	if (__mnt_is_readonly(upperpath->mnt)) {
1133 		pr_err("upper fs is r/o, try multi-lower layers mount\n");
1134 		err = -EINVAL;
1135 		goto out;
1136 	}
1137 
1138 	err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
1139 	if (err)
1140 		goto out;
1141 
1142 	err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap,
1143 			     "upperdir");
1144 	if (err)
1145 		goto out;
1146 
1147 	upper_mnt = clone_private_mount(upperpath);
1148 	err = PTR_ERR(upper_mnt);
1149 	if (IS_ERR(upper_mnt)) {
1150 		pr_err("failed to clone upperpath\n");
1151 		goto out;
1152 	}
1153 
1154 	/* Don't inherit atime flags */
1155 	upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1156 	upper_layer->mnt = upper_mnt;
1157 	upper_layer->idx = 0;
1158 	upper_layer->fsid = 0;
1159 
1160 	/*
1161 	 * Inherit SB_NOSEC flag from upperdir.
1162 	 *
1163 	 * This optimization changes behavior when a security related attribute
1164 	 * (suid/sgid/security.*) is changed on an underlying layer.  This is
1165 	 * okay because we don't yet have guarantees in that case, but it will
1166 	 * need careful treatment once we want to honour changes to underlying
1167 	 * filesystems.
1168 	 */
1169 	if (upper_mnt->mnt_sb->s_flags & SB_NOSEC)
1170 		sb->s_flags |= SB_NOSEC;
1171 
1172 	if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) {
1173 		ofs->upperdir_locked = true;
1174 	} else {
1175 		err = ovl_report_in_use(ofs, "upperdir");
1176 		if (err)
1177 			goto out;
1178 	}
1179 
1180 	err = 0;
1181 out:
1182 	return err;
1183 }
1184 
1185 /*
1186  * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and
1187  * negative values if error is encountered.
1188  */
1189 static int ovl_check_rename_whiteout(struct ovl_fs *ofs)
1190 {
1191 	struct dentry *workdir = ofs->workdir;
1192 	struct inode *dir = d_inode(workdir);
1193 	struct dentry *temp;
1194 	struct dentry *dest;
1195 	struct dentry *whiteout;
1196 	struct name_snapshot name;
1197 	int err;
1198 
1199 	inode_lock_nested(dir, I_MUTEX_PARENT);
1200 
1201 	temp = ovl_create_temp(ofs, workdir, OVL_CATTR(S_IFREG | 0));
1202 	err = PTR_ERR(temp);
1203 	if (IS_ERR(temp))
1204 		goto out_unlock;
1205 
1206 	dest = ovl_lookup_temp(ofs, workdir);
1207 	err = PTR_ERR(dest);
1208 	if (IS_ERR(dest)) {
1209 		dput(temp);
1210 		goto out_unlock;
1211 	}
1212 
1213 	/* Name is inline and stable - using snapshot as a copy helper */
1214 	take_dentry_name_snapshot(&name, temp);
1215 	err = ovl_do_rename(ofs, dir, temp, dir, dest, RENAME_WHITEOUT);
1216 	if (err) {
1217 		if (err == -EINVAL)
1218 			err = 0;
1219 		goto cleanup_temp;
1220 	}
1221 
1222 	whiteout = ovl_lookup_upper(ofs, name.name.name, workdir, name.name.len);
1223 	err = PTR_ERR(whiteout);
1224 	if (IS_ERR(whiteout))
1225 		goto cleanup_temp;
1226 
1227 	err = ovl_is_whiteout(whiteout);
1228 
1229 	/* Best effort cleanup of whiteout and temp file */
1230 	if (err)
1231 		ovl_cleanup(ofs, dir, whiteout);
1232 	dput(whiteout);
1233 
1234 cleanup_temp:
1235 	ovl_cleanup(ofs, dir, temp);
1236 	release_dentry_name_snapshot(&name);
1237 	dput(temp);
1238 	dput(dest);
1239 
1240 out_unlock:
1241 	inode_unlock(dir);
1242 
1243 	return err;
1244 }
1245 
1246 static struct dentry *ovl_lookup_or_create(struct ovl_fs *ofs,
1247 					   struct dentry *parent,
1248 					   const char *name, umode_t mode)
1249 {
1250 	size_t len = strlen(name);
1251 	struct dentry *child;
1252 
1253 	inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
1254 	child = ovl_lookup_upper(ofs, name, parent, len);
1255 	if (!IS_ERR(child) && !child->d_inode)
1256 		child = ovl_create_real(ofs, parent->d_inode, child,
1257 					OVL_CATTR(mode));
1258 	inode_unlock(parent->d_inode);
1259 	dput(parent);
1260 
1261 	return child;
1262 }
1263 
1264 /*
1265  * Creates $workdir/work/incompat/volatile/dirty file if it is not already
1266  * present.
1267  */
1268 static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
1269 {
1270 	unsigned int ctr;
1271 	struct dentry *d = dget(ofs->workbasedir);
1272 	static const char *const volatile_path[] = {
1273 		OVL_WORKDIR_NAME, "incompat", "volatile", "dirty"
1274 	};
1275 	const char *const *name = volatile_path;
1276 
1277 	for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
1278 		d = ovl_lookup_or_create(ofs, d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
1279 		if (IS_ERR(d))
1280 			return PTR_ERR(d);
1281 	}
1282 	dput(d);
1283 	return 0;
1284 }
1285 
1286 static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
1287 			    const struct path *workpath)
1288 {
1289 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
1290 	struct dentry *workdir;
1291 	struct file *tmpfile;
1292 	bool rename_whiteout;
1293 	bool d_type;
1294 	int fh_type;
1295 	int err;
1296 
1297 	err = mnt_want_write(mnt);
1298 	if (err)
1299 		return err;
1300 
1301 	workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1302 	err = PTR_ERR(workdir);
1303 	if (IS_ERR_OR_NULL(workdir))
1304 		goto out;
1305 
1306 	ofs->workdir = workdir;
1307 
1308 	err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
1309 	if (err)
1310 		goto out;
1311 
1312 	/*
1313 	 * Upper should support d_type, else whiteouts are visible.  Given
1314 	 * workdir and upper are on same fs, we can do iterate_dir() on
1315 	 * workdir. This check requires successful creation of workdir in
1316 	 * previous step.
1317 	 */
1318 	err = ovl_check_d_type_supported(workpath);
1319 	if (err < 0)
1320 		goto out;
1321 
1322 	d_type = err;
1323 	if (!d_type)
1324 		pr_warn("upper fs needs to support d_type.\n");
1325 
1326 	/* Check if upper/work fs supports O_TMPFILE */
1327 	tmpfile = ovl_do_tmpfile(ofs, ofs->workdir, S_IFREG | 0);
1328 	ofs->tmpfile = !IS_ERR(tmpfile);
1329 	if (ofs->tmpfile)
1330 		fput(tmpfile);
1331 	else
1332 		pr_warn("upper fs does not support tmpfile.\n");
1333 
1334 
1335 	/* Check if upper/work fs supports RENAME_WHITEOUT */
1336 	err = ovl_check_rename_whiteout(ofs);
1337 	if (err < 0)
1338 		goto out;
1339 
1340 	rename_whiteout = err;
1341 	if (!rename_whiteout)
1342 		pr_warn("upper fs does not support RENAME_WHITEOUT.\n");
1343 
1344 	/*
1345 	 * Check if upper/work fs supports (trusted|user).overlay.* xattr
1346 	 */
1347 	err = ovl_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1);
1348 	if (err) {
1349 		pr_warn("failed to set xattr on upper\n");
1350 		ofs->noxattr = true;
1351 		if (ovl_redirect_follow(ofs)) {
1352 			ofs->config.redirect_mode = OVL_REDIRECT_NOFOLLOW;
1353 			pr_warn("...falling back to redirect_dir=nofollow.\n");
1354 		}
1355 		if (ofs->config.metacopy) {
1356 			ofs->config.metacopy = false;
1357 			pr_warn("...falling back to metacopy=off.\n");
1358 		}
1359 		if (ofs->config.index) {
1360 			ofs->config.index = false;
1361 			pr_warn("...falling back to index=off.\n");
1362 		}
1363 		/*
1364 		 * xattr support is required for persistent st_ino.
1365 		 * Without persistent st_ino, xino=auto falls back to xino=off.
1366 		 */
1367 		if (ofs->config.xino == OVL_XINO_AUTO) {
1368 			ofs->config.xino = OVL_XINO_OFF;
1369 			pr_warn("...falling back to xino=off.\n");
1370 		}
1371 		if (err == -EPERM && !ofs->config.userxattr)
1372 			pr_info("try mounting with 'userxattr' option\n");
1373 		err = 0;
1374 	} else {
1375 		ovl_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE);
1376 	}
1377 
1378 	/*
1379 	 * We allowed sub-optimal upper fs configuration and don't want to break
1380 	 * users over kernel upgrade, but we never allowed remote upper fs, so
1381 	 * we can enforce strict requirements for remote upper fs.
1382 	 */
1383 	if (ovl_dentry_remote(ofs->workdir) &&
1384 	    (!d_type || !rename_whiteout || ofs->noxattr)) {
1385 		pr_err("upper fs missing required features.\n");
1386 		err = -EINVAL;
1387 		goto out;
1388 	}
1389 
1390 	/*
1391 	 * For volatile mount, create a incompat/volatile/dirty file to keep
1392 	 * track of it.
1393 	 */
1394 	if (ofs->config.ovl_volatile) {
1395 		err = ovl_create_volatile_dirty(ofs);
1396 		if (err < 0) {
1397 			pr_err("Failed to create volatile/dirty file.\n");
1398 			goto out;
1399 		}
1400 	}
1401 
1402 	/* Check if upper/work fs supports file handles */
1403 	fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1404 	if (ofs->config.index && !fh_type) {
1405 		ofs->config.index = false;
1406 		pr_warn("upper fs does not support file handles, falling back to index=off.\n");
1407 	}
1408 
1409 	/* Check if upper fs has 32bit inode numbers */
1410 	if (fh_type != FILEID_INO32_GEN)
1411 		ofs->xino_mode = -1;
1412 
1413 	/* NFS export of r/w mount depends on index */
1414 	if (ofs->config.nfs_export && !ofs->config.index) {
1415 		pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1416 		ofs->config.nfs_export = false;
1417 	}
1418 out:
1419 	mnt_drop_write(mnt);
1420 	return err;
1421 }
1422 
1423 static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
1424 			   const struct path *upperpath)
1425 {
1426 	int err;
1427 	struct path workpath = { };
1428 
1429 	err = ovl_mount_dir(ofs->config.workdir, &workpath);
1430 	if (err)
1431 		goto out;
1432 
1433 	err = -EINVAL;
1434 	if (upperpath->mnt != workpath.mnt) {
1435 		pr_err("workdir and upperdir must reside under the same mount\n");
1436 		goto out;
1437 	}
1438 	if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
1439 		pr_err("workdir and upperdir must be separate subtrees\n");
1440 		goto out;
1441 	}
1442 
1443 	ofs->workbasedir = dget(workpath.dentry);
1444 
1445 	if (ovl_inuse_trylock(ofs->workbasedir)) {
1446 		ofs->workdir_locked = true;
1447 	} else {
1448 		err = ovl_report_in_use(ofs, "workdir");
1449 		if (err)
1450 			goto out;
1451 	}
1452 
1453 	err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
1454 			     "workdir");
1455 	if (err)
1456 		goto out;
1457 
1458 	err = ovl_make_workdir(sb, ofs, &workpath);
1459 
1460 out:
1461 	path_put(&workpath);
1462 
1463 	return err;
1464 }
1465 
1466 static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
1467 			    struct ovl_entry *oe, const struct path *upperpath)
1468 {
1469 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
1470 	struct dentry *indexdir;
1471 	int err;
1472 
1473 	err = mnt_want_write(mnt);
1474 	if (err)
1475 		return err;
1476 
1477 	/* Verify lower root is upper root origin */
1478 	err = ovl_verify_origin(ofs, upperpath->dentry,
1479 				ovl_lowerstack(oe)->dentry, true);
1480 	if (err) {
1481 		pr_err("failed to verify upper root origin\n");
1482 		goto out;
1483 	}
1484 
1485 	/* index dir will act also as workdir */
1486 	iput(ofs->workdir_trap);
1487 	ofs->workdir_trap = NULL;
1488 	dput(ofs->workdir);
1489 	ofs->workdir = NULL;
1490 	indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1491 	if (IS_ERR(indexdir)) {
1492 		err = PTR_ERR(indexdir);
1493 	} else if (indexdir) {
1494 		ofs->indexdir = indexdir;
1495 		ofs->workdir = dget(indexdir);
1496 
1497 		err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
1498 				     "indexdir");
1499 		if (err)
1500 			goto out;
1501 
1502 		/*
1503 		 * Verify upper root is exclusively associated with index dir.
1504 		 * Older kernels stored upper fh in ".overlay.origin"
1505 		 * xattr. If that xattr exists, verify that it is a match to
1506 		 * upper dir file handle. In any case, verify or set xattr
1507 		 * ".overlay.upper" to indicate that index may have
1508 		 * directory entries.
1509 		 */
1510 		if (ovl_check_origin_xattr(ofs, ofs->indexdir)) {
1511 			err = ovl_verify_set_fh(ofs, ofs->indexdir,
1512 						OVL_XATTR_ORIGIN,
1513 						upperpath->dentry, true, false);
1514 			if (err)
1515 				pr_err("failed to verify index dir 'origin' xattr\n");
1516 		}
1517 		err = ovl_verify_upper(ofs, ofs->indexdir, upperpath->dentry,
1518 				       true);
1519 		if (err)
1520 			pr_err("failed to verify index dir 'upper' xattr\n");
1521 
1522 		/* Cleanup bad/stale/orphan index entries */
1523 		if (!err)
1524 			err = ovl_indexdir_cleanup(ofs);
1525 	}
1526 	if (err || !ofs->indexdir)
1527 		pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1528 
1529 out:
1530 	mnt_drop_write(mnt);
1531 	return err;
1532 }
1533 
1534 static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
1535 {
1536 	unsigned int i;
1537 
1538 	if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs))
1539 		return true;
1540 
1541 	/*
1542 	 * We allow using single lower with null uuid for index and nfs_export
1543 	 * for example to support those features with single lower squashfs.
1544 	 * To avoid regressions in setups of overlay with re-formatted lower
1545 	 * squashfs, do not allow decoding origin with lower null uuid unless
1546 	 * user opted-in to one of the new features that require following the
1547 	 * lower inode of non-dir upper.
1548 	 */
1549 	if (ovl_allow_offline_changes(ofs) && uuid_is_null(uuid))
1550 		return false;
1551 
1552 	for (i = 0; i < ofs->numfs; i++) {
1553 		/*
1554 		 * We use uuid to associate an overlay lower file handle with a
1555 		 * lower layer, so we can accept lower fs with null uuid as long
1556 		 * as all lower layers with null uuid are on the same fs.
1557 		 * if we detect multiple lower fs with the same uuid, we
1558 		 * disable lower file handle decoding on all of them.
1559 		 */
1560 		if (ofs->fs[i].is_lower &&
1561 		    uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
1562 			ofs->fs[i].bad_uuid = true;
1563 			return false;
1564 		}
1565 	}
1566 	return true;
1567 }
1568 
1569 /* Get a unique fsid for the layer */
1570 static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1571 {
1572 	struct super_block *sb = path->mnt->mnt_sb;
1573 	unsigned int i;
1574 	dev_t dev;
1575 	int err;
1576 	bool bad_uuid = false;
1577 	bool warn = false;
1578 
1579 	for (i = 0; i < ofs->numfs; i++) {
1580 		if (ofs->fs[i].sb == sb)
1581 			return i;
1582 	}
1583 
1584 	if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
1585 		bad_uuid = true;
1586 		if (ofs->config.xino == OVL_XINO_AUTO) {
1587 			ofs->config.xino = OVL_XINO_OFF;
1588 			warn = true;
1589 		}
1590 		if (ofs->config.index || ofs->config.nfs_export) {
1591 			ofs->config.index = false;
1592 			ofs->config.nfs_export = false;
1593 			warn = true;
1594 		}
1595 		if (warn) {
1596 			pr_warn("%s uuid detected in lower fs '%pd2', falling back to xino=%s,index=off,nfs_export=off.\n",
1597 				uuid_is_null(&sb->s_uuid) ? "null" :
1598 							    "conflicting",
1599 				path->dentry, ovl_xino_mode(&ofs->config));
1600 		}
1601 	}
1602 
1603 	err = get_anon_bdev(&dev);
1604 	if (err) {
1605 		pr_err("failed to get anonymous bdev for lowerpath\n");
1606 		return err;
1607 	}
1608 
1609 	ofs->fs[ofs->numfs].sb = sb;
1610 	ofs->fs[ofs->numfs].pseudo_dev = dev;
1611 	ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
1612 
1613 	return ofs->numfs++;
1614 }
1615 
1616 /*
1617  * The fsid after the last lower fsid is used for the data layers.
1618  * It is a "null fs" with a null sb, null uuid, and no pseudo dev.
1619  */
1620 static int ovl_get_data_fsid(struct ovl_fs *ofs)
1621 {
1622 	return ofs->numfs;
1623 }
1624 
1625 
1626 static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
1627 			  struct path *stack, unsigned int numlower,
1628 			  struct ovl_layer *layers)
1629 {
1630 	int err;
1631 	unsigned int i;
1632 
1633 	ofs->fs = kcalloc(numlower + 2, sizeof(struct ovl_sb), GFP_KERNEL);
1634 	if (ofs->fs == NULL)
1635 		return -ENOMEM;
1636 
1637 	/*
1638 	 * idx/fsid 0 are reserved for upper fs even with lower only overlay
1639 	 * and the last fsid is reserved for "null fs" of the data layers.
1640 	 */
1641 	ofs->numfs++;
1642 
1643 	/*
1644 	 * All lower layers that share the same fs as upper layer, use the same
1645 	 * pseudo_dev as upper layer.  Allocate fs[0].pseudo_dev even for lower
1646 	 * only overlay to simplify ovl_fs_free().
1647 	 * is_lower will be set if upper fs is shared with a lower layer.
1648 	 */
1649 	err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
1650 	if (err) {
1651 		pr_err("failed to get anonymous bdev for upper fs\n");
1652 		return err;
1653 	}
1654 
1655 	if (ovl_upper_mnt(ofs)) {
1656 		ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb;
1657 		ofs->fs[0].is_lower = false;
1658 	}
1659 
1660 	for (i = 0; i < numlower; i++) {
1661 		struct vfsmount *mnt;
1662 		struct inode *trap;
1663 		int fsid;
1664 
1665 		if (i < numlower - ofs->numdatalayer)
1666 			fsid = ovl_get_fsid(ofs, &stack[i]);
1667 		else
1668 			fsid = ovl_get_data_fsid(ofs);
1669 		if (fsid < 0)
1670 			return fsid;
1671 
1672 		/*
1673 		 * Check if lower root conflicts with this overlay layers before
1674 		 * checking if it is in-use as upperdir/workdir of "another"
1675 		 * mount, because we do not bother to check in ovl_is_inuse() if
1676 		 * the upperdir/workdir is in fact in-use by our
1677 		 * upperdir/workdir.
1678 		 */
1679 		err = ovl_setup_trap(sb, stack[i].dentry, &trap, "lowerdir");
1680 		if (err)
1681 			return err;
1682 
1683 		if (ovl_is_inuse(stack[i].dentry)) {
1684 			err = ovl_report_in_use(ofs, "lowerdir");
1685 			if (err) {
1686 				iput(trap);
1687 				return err;
1688 			}
1689 		}
1690 
1691 		mnt = clone_private_mount(&stack[i]);
1692 		err = PTR_ERR(mnt);
1693 		if (IS_ERR(mnt)) {
1694 			pr_err("failed to clone lowerpath\n");
1695 			iput(trap);
1696 			return err;
1697 		}
1698 
1699 		/*
1700 		 * Make lower layers R/O.  That way fchmod/fchown on lower file
1701 		 * will fail instead of modifying lower fs.
1702 		 */
1703 		mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1704 
1705 		layers[ofs->numlayer].trap = trap;
1706 		layers[ofs->numlayer].mnt = mnt;
1707 		layers[ofs->numlayer].idx = ofs->numlayer;
1708 		layers[ofs->numlayer].fsid = fsid;
1709 		layers[ofs->numlayer].fs = &ofs->fs[fsid];
1710 		ofs->numlayer++;
1711 		ofs->fs[fsid].is_lower = true;
1712 	}
1713 
1714 	/*
1715 	 * When all layers on same fs, overlay can use real inode numbers.
1716 	 * With mount option "xino=<on|auto>", mounter declares that there are
1717 	 * enough free high bits in underlying fs to hold the unique fsid.
1718 	 * If overlayfs does encounter underlying inodes using the high xino
1719 	 * bits reserved for fsid, it emits a warning and uses the original
1720 	 * inode number or a non persistent inode number allocated from a
1721 	 * dedicated range.
1722 	 */
1723 	if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) {
1724 		if (ofs->config.xino == OVL_XINO_ON)
1725 			pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
1726 		ofs->xino_mode = 0;
1727 	} else if (ofs->config.xino == OVL_XINO_OFF) {
1728 		ofs->xino_mode = -1;
1729 	} else if (ofs->xino_mode < 0) {
1730 		/*
1731 		 * This is a roundup of number of bits needed for encoding
1732 		 * fsid, where fsid 0 is reserved for upper fs (even with
1733 		 * lower only overlay) +1 extra bit is reserved for the non
1734 		 * persistent inode number range that is used for resolving
1735 		 * xino lower bits overflow.
1736 		 */
1737 		BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30);
1738 		ofs->xino_mode = ilog2(ofs->numfs - 1) + 2;
1739 	}
1740 
1741 	if (ofs->xino_mode > 0) {
1742 		pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
1743 			ofs->xino_mode);
1744 	}
1745 
1746 	return 0;
1747 }
1748 
1749 static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1750 				const char *lower, unsigned int numlower,
1751 				struct ovl_fs *ofs, struct ovl_layer *layers)
1752 {
1753 	int err;
1754 	struct path *stack = NULL;
1755 	struct ovl_path *lowerstack;
1756 	unsigned int numlowerdata = 0;
1757 	unsigned int i;
1758 	struct ovl_entry *oe;
1759 
1760 	if (!ofs->config.upperdir && numlower == 1) {
1761 		pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
1762 		return ERR_PTR(-EINVAL);
1763 	}
1764 
1765 	stack = kcalloc(numlower, sizeof(struct path), GFP_KERNEL);
1766 	if (!stack)
1767 		return ERR_PTR(-ENOMEM);
1768 
1769 	for (i = 0; i < numlower;) {
1770 		err = ovl_lower_dir(lower, &stack[i], ofs, &sb->s_stack_depth);
1771 		if (err)
1772 			goto out_err;
1773 
1774 		lower = strchr(lower, '\0') + 1;
1775 
1776 		i++;
1777 		if (i == numlower)
1778 			break;
1779 
1780 		err = -EINVAL;
1781 		/*
1782 		 * Empty lower layer path could mean :: separator that indicates
1783 		 * a data-only lower data.
1784 		 * Several data-only layers are allowed, but they all need to be
1785 		 * at the bottom of the stack.
1786 		 */
1787 		if (*lower) {
1788 			/* normal lower dir */
1789 			if (numlowerdata) {
1790 				pr_err("lower data-only dirs must be at the bottom of the stack.\n");
1791 				goto out_err;
1792 			}
1793 		} else {
1794 			/* data-only lower dir */
1795 			if (!ofs->config.metacopy) {
1796 				pr_err("lower data-only dirs require metacopy support.\n");
1797 				goto out_err;
1798 			}
1799 			if (i == numlower - 1) {
1800 				pr_err("lowerdir argument must not end with double colon.\n");
1801 				goto out_err;
1802 			}
1803 			lower++;
1804 			numlower--;
1805 			numlowerdata++;
1806 		}
1807 	}
1808 
1809 	if (numlowerdata) {
1810 		ofs->numdatalayer = numlowerdata;
1811 		pr_info("using the lowest %d of %d lowerdirs as data layers\n",
1812 			numlowerdata, numlower);
1813 	}
1814 
1815 	err = -EINVAL;
1816 	sb->s_stack_depth++;
1817 	if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1818 		pr_err("maximum fs stacking depth exceeded\n");
1819 		goto out_err;
1820 	}
1821 
1822 	err = ovl_get_layers(sb, ofs, stack, numlower, layers);
1823 	if (err)
1824 		goto out_err;
1825 
1826 	err = -ENOMEM;
1827 	/* Data-only layers are not merged in root directory */
1828 	oe = ovl_alloc_entry(numlower - numlowerdata);
1829 	if (!oe)
1830 		goto out_err;
1831 
1832 	lowerstack = ovl_lowerstack(oe);
1833 	for (i = 0; i < numlower - numlowerdata; i++) {
1834 		lowerstack[i].dentry = dget(stack[i].dentry);
1835 		lowerstack[i].layer = &ofs->layers[i+1];
1836 	}
1837 
1838 out:
1839 	for (i = 0; i < numlower; i++)
1840 		path_put(&stack[i]);
1841 	kfree(stack);
1842 
1843 	return oe;
1844 
1845 out_err:
1846 	oe = ERR_PTR(err);
1847 	goto out;
1848 }
1849 
1850 /*
1851  * Check if this layer root is a descendant of:
1852  * - another layer of this overlayfs instance
1853  * - upper/work dir of any overlayfs instance
1854  */
1855 static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
1856 			   struct dentry *dentry, const char *name,
1857 			   bool is_lower)
1858 {
1859 	struct dentry *next = dentry, *parent;
1860 	int err = 0;
1861 
1862 	if (!dentry)
1863 		return 0;
1864 
1865 	parent = dget_parent(next);
1866 
1867 	/* Walk back ancestors to root (inclusive) looking for traps */
1868 	while (!err && parent != next) {
1869 		if (is_lower && ovl_lookup_trap_inode(sb, parent)) {
1870 			err = -ELOOP;
1871 			pr_err("overlapping %s path\n", name);
1872 		} else if (ovl_is_inuse(parent)) {
1873 			err = ovl_report_in_use(ofs, name);
1874 		}
1875 		next = parent;
1876 		parent = dget_parent(next);
1877 		dput(next);
1878 	}
1879 
1880 	dput(parent);
1881 
1882 	return err;
1883 }
1884 
1885 /*
1886  * Check if any of the layers or work dirs overlap.
1887  */
1888 static int ovl_check_overlapping_layers(struct super_block *sb,
1889 					struct ovl_fs *ofs)
1890 {
1891 	int i, err;
1892 
1893 	if (ovl_upper_mnt(ofs)) {
1894 		err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root,
1895 				      "upperdir", false);
1896 		if (err)
1897 			return err;
1898 
1899 		/*
1900 		 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1901 		 * this instance and covers overlapping work and index dirs,
1902 		 * unless work or index dir have been moved since created inside
1903 		 * workbasedir.  In that case, we already have their traps in
1904 		 * inode cache and we will catch that case on lookup.
1905 		 */
1906 		err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir",
1907 				      false);
1908 		if (err)
1909 			return err;
1910 	}
1911 
1912 	for (i = 1; i < ofs->numlayer; i++) {
1913 		err = ovl_check_layer(sb, ofs,
1914 				      ofs->layers[i].mnt->mnt_root,
1915 				      "lowerdir", true);
1916 		if (err)
1917 			return err;
1918 	}
1919 
1920 	return 0;
1921 }
1922 
1923 static struct dentry *ovl_get_root(struct super_block *sb,
1924 				   struct dentry *upperdentry,
1925 				   struct ovl_entry *oe)
1926 {
1927 	struct dentry *root;
1928 	struct ovl_path *lowerpath = ovl_lowerstack(oe);
1929 	unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
1930 	int fsid = lowerpath->layer->fsid;
1931 	struct ovl_inode_params oip = {
1932 		.upperdentry = upperdentry,
1933 		.oe = oe,
1934 	};
1935 
1936 	root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1937 	if (!root)
1938 		return NULL;
1939 
1940 	if (upperdentry) {
1941 		/* Root inode uses upper st_ino/i_ino */
1942 		ino = d_inode(upperdentry)->i_ino;
1943 		fsid = 0;
1944 		ovl_dentry_set_upper_alias(root);
1945 		if (ovl_is_impuredir(sb, upperdentry))
1946 			ovl_set_flag(OVL_IMPURE, d_inode(root));
1947 	}
1948 
1949 	/* Root is always merge -> can have whiteouts */
1950 	ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
1951 	ovl_dentry_set_flag(OVL_E_CONNECTED, root);
1952 	ovl_set_upperdata(d_inode(root));
1953 	ovl_inode_init(d_inode(root), &oip, ino, fsid);
1954 	ovl_dentry_init_flags(root, upperdentry, oe, DCACHE_OP_WEAK_REVALIDATE);
1955 	/* root keeps a reference of upperdentry */
1956 	dget(upperdentry);
1957 
1958 	return root;
1959 }
1960 
1961 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1962 {
1963 	struct path upperpath = { };
1964 	struct dentry *root_dentry;
1965 	struct ovl_entry *oe;
1966 	struct ovl_fs *ofs;
1967 	struct ovl_layer *layers;
1968 	struct cred *cred;
1969 	char *splitlower = NULL;
1970 	unsigned int numlower;
1971 	int err;
1972 
1973 	err = -EIO;
1974 	if (WARN_ON(sb->s_user_ns != current_user_ns()))
1975 		goto out;
1976 
1977 	sb->s_d_op = &ovl_dentry_operations;
1978 
1979 	err = -ENOMEM;
1980 	ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1981 	if (!ofs)
1982 		goto out;
1983 
1984 	err = -ENOMEM;
1985 	ofs->creator_cred = cred = prepare_creds();
1986 	if (!cred)
1987 		goto out_err;
1988 
1989 	ofs->config.redirect_mode = ovl_redirect_mode_def();
1990 	ofs->config.index = ovl_index_def;
1991 	ofs->config.uuid = true;
1992 	ofs->config.nfs_export = ovl_nfs_export_def;
1993 	ofs->config.xino = ovl_xino_def();
1994 	ofs->config.metacopy = ovl_metacopy_def;
1995 	err = ovl_parse_options((char *) data, &ofs->config);
1996 	if (err)
1997 		goto out_err;
1998 
1999 	err = -EINVAL;
2000 	if (!ofs->config.lowerdir) {
2001 		if (!silent)
2002 			pr_err("missing 'lowerdir'\n");
2003 		goto out_err;
2004 	}
2005 
2006 	err = -ENOMEM;
2007 	splitlower = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
2008 	if (!splitlower)
2009 		goto out_err;
2010 
2011 	err = -EINVAL;
2012 	numlower = ovl_split_lowerdirs(splitlower);
2013 	if (numlower > OVL_MAX_STACK) {
2014 		pr_err("too many lower directories, limit is %d\n",
2015 		       OVL_MAX_STACK);
2016 		goto out_err;
2017 	}
2018 
2019 	err = -ENOMEM;
2020 	layers = kcalloc(numlower + 1, sizeof(struct ovl_layer), GFP_KERNEL);
2021 	if (!layers)
2022 		goto out_err;
2023 
2024 	ofs->layers = layers;
2025 	/* Layer 0 is reserved for upper even if there's no upper */
2026 	ofs->numlayer = 1;
2027 
2028 	sb->s_stack_depth = 0;
2029 	sb->s_maxbytes = MAX_LFS_FILESIZE;
2030 	atomic_long_set(&ofs->last_ino, 1);
2031 	/* Assume underlying fs uses 32bit inodes unless proven otherwise */
2032 	if (ofs->config.xino != OVL_XINO_OFF) {
2033 		ofs->xino_mode = BITS_PER_LONG - 32;
2034 		if (!ofs->xino_mode) {
2035 			pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
2036 			ofs->config.xino = OVL_XINO_OFF;
2037 		}
2038 	}
2039 
2040 	/* alloc/destroy_inode needed for setting up traps in inode cache */
2041 	sb->s_op = &ovl_super_operations;
2042 
2043 	if (ofs->config.upperdir) {
2044 		struct super_block *upper_sb;
2045 
2046 		err = -EINVAL;
2047 		if (!ofs->config.workdir) {
2048 			pr_err("missing 'workdir'\n");
2049 			goto out_err;
2050 		}
2051 
2052 		err = ovl_get_upper(sb, ofs, &layers[0], &upperpath);
2053 		if (err)
2054 			goto out_err;
2055 
2056 		upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
2057 		if (!ovl_should_sync(ofs)) {
2058 			ofs->errseq = errseq_sample(&upper_sb->s_wb_err);
2059 			if (errseq_check(&upper_sb->s_wb_err, ofs->errseq)) {
2060 				err = -EIO;
2061 				pr_err("Cannot mount volatile when upperdir has an unseen error. Sync upperdir fs to clear state.\n");
2062 				goto out_err;
2063 			}
2064 		}
2065 
2066 		err = ovl_get_workdir(sb, ofs, &upperpath);
2067 		if (err)
2068 			goto out_err;
2069 
2070 		if (!ofs->workdir)
2071 			sb->s_flags |= SB_RDONLY;
2072 
2073 		sb->s_stack_depth = upper_sb->s_stack_depth;
2074 		sb->s_time_gran = upper_sb->s_time_gran;
2075 	}
2076 	oe = ovl_get_lowerstack(sb, splitlower, numlower, ofs, layers);
2077 	err = PTR_ERR(oe);
2078 	if (IS_ERR(oe))
2079 		goto out_err;
2080 
2081 	/* If the upper fs is nonexistent, we mark overlayfs r/o too */
2082 	if (!ovl_upper_mnt(ofs))
2083 		sb->s_flags |= SB_RDONLY;
2084 
2085 	if (!ofs->config.uuid && ofs->numfs > 1) {
2086 		pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=on.\n");
2087 		ofs->config.uuid = true;
2088 	}
2089 
2090 	if (!ovl_force_readonly(ofs) && ofs->config.index) {
2091 		err = ovl_get_indexdir(sb, ofs, oe, &upperpath);
2092 		if (err)
2093 			goto out_free_oe;
2094 
2095 		/* Force r/o mount with no index dir */
2096 		if (!ofs->indexdir)
2097 			sb->s_flags |= SB_RDONLY;
2098 	}
2099 
2100 	err = ovl_check_overlapping_layers(sb, ofs);
2101 	if (err)
2102 		goto out_free_oe;
2103 
2104 	/* Show index=off in /proc/mounts for forced r/o mount */
2105 	if (!ofs->indexdir) {
2106 		ofs->config.index = false;
2107 		if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) {
2108 			pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
2109 			ofs->config.nfs_export = false;
2110 		}
2111 	}
2112 
2113 	if (ofs->config.metacopy && ofs->config.nfs_export) {
2114 		pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
2115 		ofs->config.nfs_export = false;
2116 	}
2117 
2118 	if (ofs->config.nfs_export)
2119 		sb->s_export_op = &ovl_export_operations;
2120 
2121 	/* Never override disk quota limits or use reserved space */
2122 	cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
2123 
2124 	sb->s_magic = OVERLAYFS_SUPER_MAGIC;
2125 	sb->s_xattr = ofs->config.userxattr ? ovl_user_xattr_handlers :
2126 		ovl_trusted_xattr_handlers;
2127 	sb->s_fs_info = ofs;
2128 	sb->s_flags |= SB_POSIXACL;
2129 	sb->s_iflags |= SB_I_SKIP_SYNC;
2130 
2131 	err = -ENOMEM;
2132 	root_dentry = ovl_get_root(sb, upperpath.dentry, oe);
2133 	if (!root_dentry)
2134 		goto out_free_oe;
2135 
2136 	path_put(&upperpath);
2137 	kfree(splitlower);
2138 
2139 	sb->s_root = root_dentry;
2140 
2141 	return 0;
2142 
2143 out_free_oe:
2144 	ovl_free_entry(oe);
2145 out_err:
2146 	kfree(splitlower);
2147 	path_put(&upperpath);
2148 	ovl_free_fs(ofs);
2149 out:
2150 	return err;
2151 }
2152 
2153 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
2154 				const char *dev_name, void *raw_data)
2155 {
2156 	return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
2157 }
2158 
2159 static struct file_system_type ovl_fs_type = {
2160 	.owner		= THIS_MODULE,
2161 	.name		= "overlay",
2162 	.fs_flags	= FS_USERNS_MOUNT,
2163 	.mount		= ovl_mount,
2164 	.kill_sb	= kill_anon_super,
2165 };
2166 MODULE_ALIAS_FS("overlay");
2167 
2168 static void ovl_inode_init_once(void *foo)
2169 {
2170 	struct ovl_inode *oi = foo;
2171 
2172 	inode_init_once(&oi->vfs_inode);
2173 }
2174 
2175 static int __init ovl_init(void)
2176 {
2177 	int err;
2178 
2179 	ovl_inode_cachep = kmem_cache_create("ovl_inode",
2180 					     sizeof(struct ovl_inode), 0,
2181 					     (SLAB_RECLAIM_ACCOUNT|
2182 					      SLAB_MEM_SPREAD|SLAB_ACCOUNT),
2183 					     ovl_inode_init_once);
2184 	if (ovl_inode_cachep == NULL)
2185 		return -ENOMEM;
2186 
2187 	err = ovl_aio_request_cache_init();
2188 	if (!err) {
2189 		err = register_filesystem(&ovl_fs_type);
2190 		if (!err)
2191 			return 0;
2192 
2193 		ovl_aio_request_cache_destroy();
2194 	}
2195 	kmem_cache_destroy(ovl_inode_cachep);
2196 
2197 	return err;
2198 }
2199 
2200 static void __exit ovl_exit(void)
2201 {
2202 	unregister_filesystem(&ovl_fs_type);
2203 
2204 	/*
2205 	 * Make sure all delayed rcu free inodes are flushed before we
2206 	 * destroy cache.
2207 	 */
2208 	rcu_barrier();
2209 	kmem_cache_destroy(ovl_inode_cachep);
2210 	ovl_aio_request_cache_destroy();
2211 }
2212 
2213 module_init(ovl_init);
2214 module_exit(ovl_exit);
2215