xref: /openbmc/linux/fs/overlayfs/super.c (revision ae213c44)
1 /*
2  *
3  * Copyright (C) 2011 Novell Inc.
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 as published by
7  * the Free Software Foundation.
8  */
9 
10 #include <uapi/linux/magic.h>
11 #include <linux/fs.h>
12 #include <linux/namei.h>
13 #include <linux/xattr.h>
14 #include <linux/mount.h>
15 #include <linux/parser.h>
16 #include <linux/module.h>
17 #include <linux/statfs.h>
18 #include <linux/seq_file.h>
19 #include <linux/posix_acl_xattr.h>
20 #include <linux/exportfs.h>
21 #include "overlayfs.h"
22 
23 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
24 MODULE_DESCRIPTION("Overlay filesystem");
25 MODULE_LICENSE("GPL");
26 
27 
28 struct ovl_dir_cache;
29 
30 #define OVL_MAX_STACK 500
31 
32 static bool ovl_redirect_dir_def = IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_DIR);
33 module_param_named(redirect_dir, ovl_redirect_dir_def, bool, 0644);
34 MODULE_PARM_DESC(ovl_redirect_dir_def,
35 		 "Default to on or off for the redirect_dir feature");
36 
37 static bool ovl_redirect_always_follow =
38 	IS_ENABLED(CONFIG_OVERLAY_FS_REDIRECT_ALWAYS_FOLLOW);
39 module_param_named(redirect_always_follow, ovl_redirect_always_follow,
40 		   bool, 0644);
41 MODULE_PARM_DESC(ovl_redirect_always_follow,
42 		 "Follow redirects even if redirect_dir feature is turned off");
43 
44 static bool ovl_index_def = IS_ENABLED(CONFIG_OVERLAY_FS_INDEX);
45 module_param_named(index, ovl_index_def, bool, 0644);
46 MODULE_PARM_DESC(ovl_index_def,
47 		 "Default to on or off for the inodes index feature");
48 
49 static bool ovl_nfs_export_def = IS_ENABLED(CONFIG_OVERLAY_FS_NFS_EXPORT);
50 module_param_named(nfs_export, ovl_nfs_export_def, bool, 0644);
51 MODULE_PARM_DESC(ovl_nfs_export_def,
52 		 "Default to on or off for the NFS export feature");
53 
54 static bool ovl_xino_auto_def = IS_ENABLED(CONFIG_OVERLAY_FS_XINO_AUTO);
55 module_param_named(xino_auto, ovl_xino_auto_def, bool, 0644);
56 MODULE_PARM_DESC(ovl_xino_auto_def,
57 		 "Auto enable xino feature");
58 
59 static void ovl_entry_stack_free(struct ovl_entry *oe)
60 {
61 	unsigned int i;
62 
63 	for (i = 0; i < oe->numlower; i++)
64 		dput(oe->lowerstack[i].dentry);
65 }
66 
67 static bool ovl_metacopy_def = IS_ENABLED(CONFIG_OVERLAY_FS_METACOPY);
68 module_param_named(metacopy, ovl_metacopy_def, bool, 0644);
69 MODULE_PARM_DESC(ovl_metacopy_def,
70 		 "Default to on or off for the metadata only copy up feature");
71 
72 static void ovl_dentry_release(struct dentry *dentry)
73 {
74 	struct ovl_entry *oe = dentry->d_fsdata;
75 
76 	if (oe) {
77 		ovl_entry_stack_free(oe);
78 		kfree_rcu(oe, rcu);
79 	}
80 }
81 
82 static struct dentry *ovl_d_real(struct dentry *dentry,
83 				 const struct inode *inode)
84 {
85 	struct dentry *real;
86 
87 	/* It's an overlay file */
88 	if (inode && d_inode(dentry) == inode)
89 		return dentry;
90 
91 	if (!d_is_reg(dentry)) {
92 		if (!inode || inode == d_inode(dentry))
93 			return dentry;
94 		goto bug;
95 	}
96 
97 	real = ovl_dentry_upper(dentry);
98 	if (real && (inode == d_inode(real)))
99 		return real;
100 
101 	if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
102 		return real;
103 
104 	real = ovl_dentry_lowerdata(dentry);
105 	if (!real)
106 		goto bug;
107 
108 	/* Handle recursion */
109 	real = d_real(real, inode);
110 
111 	if (!inode || inode == d_inode(real))
112 		return real;
113 bug:
114 	WARN(1, "ovl_d_real(%pd4, %s:%lu): real dentry not found\n", dentry,
115 	     inode ? inode->i_sb->s_id : "NULL", inode ? inode->i_ino : 0);
116 	return dentry;
117 }
118 
119 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
120 {
121 	struct ovl_entry *oe = dentry->d_fsdata;
122 	unsigned int i;
123 	int ret = 1;
124 
125 	for (i = 0; i < oe->numlower; i++) {
126 		struct dentry *d = oe->lowerstack[i].dentry;
127 
128 		if (d->d_flags & DCACHE_OP_REVALIDATE) {
129 			ret = d->d_op->d_revalidate(d, flags);
130 			if (ret < 0)
131 				return ret;
132 			if (!ret) {
133 				if (!(flags & LOOKUP_RCU))
134 					d_invalidate(d);
135 				return -ESTALE;
136 			}
137 		}
138 	}
139 	return 1;
140 }
141 
142 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
143 {
144 	struct ovl_entry *oe = dentry->d_fsdata;
145 	unsigned int i;
146 	int ret = 1;
147 
148 	for (i = 0; i < oe->numlower; i++) {
149 		struct dentry *d = oe->lowerstack[i].dentry;
150 
151 		if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE) {
152 			ret = d->d_op->d_weak_revalidate(d, flags);
153 			if (ret <= 0)
154 				break;
155 		}
156 	}
157 	return ret;
158 }
159 
160 static const struct dentry_operations ovl_dentry_operations = {
161 	.d_release = ovl_dentry_release,
162 	.d_real = ovl_d_real,
163 };
164 
165 static const struct dentry_operations ovl_reval_dentry_operations = {
166 	.d_release = ovl_dentry_release,
167 	.d_real = ovl_d_real,
168 	.d_revalidate = ovl_dentry_revalidate,
169 	.d_weak_revalidate = ovl_dentry_weak_revalidate,
170 };
171 
172 static struct kmem_cache *ovl_inode_cachep;
173 
174 static struct inode *ovl_alloc_inode(struct super_block *sb)
175 {
176 	struct ovl_inode *oi = kmem_cache_alloc(ovl_inode_cachep, GFP_KERNEL);
177 
178 	if (!oi)
179 		return NULL;
180 
181 	oi->cache = NULL;
182 	oi->redirect = NULL;
183 	oi->version = 0;
184 	oi->flags = 0;
185 	oi->__upperdentry = NULL;
186 	oi->lower = NULL;
187 	oi->lowerdata = NULL;
188 	mutex_init(&oi->lock);
189 
190 	return &oi->vfs_inode;
191 }
192 
193 static void ovl_free_inode(struct inode *inode)
194 {
195 	struct ovl_inode *oi = OVL_I(inode);
196 
197 	kfree(oi->redirect);
198 	mutex_destroy(&oi->lock);
199 	kmem_cache_free(ovl_inode_cachep, oi);
200 }
201 
202 static void ovl_destroy_inode(struct inode *inode)
203 {
204 	struct ovl_inode *oi = OVL_I(inode);
205 
206 	dput(oi->__upperdentry);
207 	iput(oi->lower);
208 	if (S_ISDIR(inode->i_mode))
209 		ovl_dir_cache_free(inode);
210 	else
211 		iput(oi->lowerdata);
212 }
213 
214 static void ovl_free_fs(struct ovl_fs *ofs)
215 {
216 	unsigned i;
217 
218 	dput(ofs->indexdir);
219 	dput(ofs->workdir);
220 	if (ofs->workdir_locked)
221 		ovl_inuse_unlock(ofs->workbasedir);
222 	dput(ofs->workbasedir);
223 	if (ofs->upperdir_locked)
224 		ovl_inuse_unlock(ofs->upper_mnt->mnt_root);
225 	mntput(ofs->upper_mnt);
226 	for (i = 0; i < ofs->numlower; i++)
227 		mntput(ofs->lower_layers[i].mnt);
228 	for (i = 0; i < ofs->numlowerfs; i++)
229 		free_anon_bdev(ofs->lower_fs[i].pseudo_dev);
230 	kfree(ofs->lower_layers);
231 	kfree(ofs->lower_fs);
232 
233 	kfree(ofs->config.lowerdir);
234 	kfree(ofs->config.upperdir);
235 	kfree(ofs->config.workdir);
236 	kfree(ofs->config.redirect_mode);
237 	if (ofs->creator_cred)
238 		put_cred(ofs->creator_cred);
239 	kfree(ofs);
240 }
241 
242 static void ovl_put_super(struct super_block *sb)
243 {
244 	struct ovl_fs *ofs = sb->s_fs_info;
245 
246 	ovl_free_fs(ofs);
247 }
248 
249 /* Sync real dirty inodes in upper filesystem (if it exists) */
250 static int ovl_sync_fs(struct super_block *sb, int wait)
251 {
252 	struct ovl_fs *ofs = sb->s_fs_info;
253 	struct super_block *upper_sb;
254 	int ret;
255 
256 	if (!ofs->upper_mnt)
257 		return 0;
258 
259 	/*
260 	 * If this is a sync(2) call or an emergency sync, all the super blocks
261 	 * will be iterated, including upper_sb, so no need to do anything.
262 	 *
263 	 * If this is a syncfs(2) call, then we do need to call
264 	 * sync_filesystem() on upper_sb, but enough if we do it when being
265 	 * called with wait == 1.
266 	 */
267 	if (!wait)
268 		return 0;
269 
270 	upper_sb = ofs->upper_mnt->mnt_sb;
271 
272 	down_read(&upper_sb->s_umount);
273 	ret = sync_filesystem(upper_sb);
274 	up_read(&upper_sb->s_umount);
275 
276 	return ret;
277 }
278 
279 /**
280  * ovl_statfs
281  * @sb: The overlayfs super block
282  * @buf: The struct kstatfs to fill in with stats
283  *
284  * Get the filesystem statistics.  As writes always target the upper layer
285  * filesystem pass the statfs to the upper filesystem (if it exists)
286  */
287 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
288 {
289 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
290 	struct dentry *root_dentry = dentry->d_sb->s_root;
291 	struct path path;
292 	int err;
293 
294 	ovl_path_real(root_dentry, &path);
295 
296 	err = vfs_statfs(&path, buf);
297 	if (!err) {
298 		buf->f_namelen = ofs->namelen;
299 		buf->f_type = OVERLAYFS_SUPER_MAGIC;
300 	}
301 
302 	return err;
303 }
304 
305 /* Will this overlay be forced to mount/remount ro? */
306 static bool ovl_force_readonly(struct ovl_fs *ofs)
307 {
308 	return (!ofs->upper_mnt || !ofs->workdir);
309 }
310 
311 static const char *ovl_redirect_mode_def(void)
312 {
313 	return ovl_redirect_dir_def ? "on" : "off";
314 }
315 
316 enum {
317 	OVL_XINO_OFF,
318 	OVL_XINO_AUTO,
319 	OVL_XINO_ON,
320 };
321 
322 static const char * const ovl_xino_str[] = {
323 	"off",
324 	"auto",
325 	"on",
326 };
327 
328 static inline int ovl_xino_def(void)
329 {
330 	return ovl_xino_auto_def ? OVL_XINO_AUTO : OVL_XINO_OFF;
331 }
332 
333 /**
334  * ovl_show_options
335  *
336  * Prints the mount options for a given superblock.
337  * Returns zero; does not fail.
338  */
339 static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
340 {
341 	struct super_block *sb = dentry->d_sb;
342 	struct ovl_fs *ofs = sb->s_fs_info;
343 
344 	seq_show_option(m, "lowerdir", ofs->config.lowerdir);
345 	if (ofs->config.upperdir) {
346 		seq_show_option(m, "upperdir", ofs->config.upperdir);
347 		seq_show_option(m, "workdir", ofs->config.workdir);
348 	}
349 	if (ofs->config.default_permissions)
350 		seq_puts(m, ",default_permissions");
351 	if (strcmp(ofs->config.redirect_mode, ovl_redirect_mode_def()) != 0)
352 		seq_printf(m, ",redirect_dir=%s", ofs->config.redirect_mode);
353 	if (ofs->config.index != ovl_index_def)
354 		seq_printf(m, ",index=%s", ofs->config.index ? "on" : "off");
355 	if (ofs->config.nfs_export != ovl_nfs_export_def)
356 		seq_printf(m, ",nfs_export=%s", ofs->config.nfs_export ?
357 						"on" : "off");
358 	if (ofs->config.xino != ovl_xino_def())
359 		seq_printf(m, ",xino=%s", ovl_xino_str[ofs->config.xino]);
360 	if (ofs->config.metacopy != ovl_metacopy_def)
361 		seq_printf(m, ",metacopy=%s",
362 			   ofs->config.metacopy ? "on" : "off");
363 	return 0;
364 }
365 
366 static int ovl_remount(struct super_block *sb, int *flags, char *data)
367 {
368 	struct ovl_fs *ofs = sb->s_fs_info;
369 
370 	if (!(*flags & SB_RDONLY) && ovl_force_readonly(ofs))
371 		return -EROFS;
372 
373 	return 0;
374 }
375 
376 static const struct super_operations ovl_super_operations = {
377 	.alloc_inode	= ovl_alloc_inode,
378 	.free_inode	= ovl_free_inode,
379 	.destroy_inode	= ovl_destroy_inode,
380 	.drop_inode	= generic_delete_inode,
381 	.put_super	= ovl_put_super,
382 	.sync_fs	= ovl_sync_fs,
383 	.statfs		= ovl_statfs,
384 	.show_options	= ovl_show_options,
385 	.remount_fs	= ovl_remount,
386 };
387 
388 enum {
389 	OPT_LOWERDIR,
390 	OPT_UPPERDIR,
391 	OPT_WORKDIR,
392 	OPT_DEFAULT_PERMISSIONS,
393 	OPT_REDIRECT_DIR,
394 	OPT_INDEX_ON,
395 	OPT_INDEX_OFF,
396 	OPT_NFS_EXPORT_ON,
397 	OPT_NFS_EXPORT_OFF,
398 	OPT_XINO_ON,
399 	OPT_XINO_OFF,
400 	OPT_XINO_AUTO,
401 	OPT_METACOPY_ON,
402 	OPT_METACOPY_OFF,
403 	OPT_ERR,
404 };
405 
406 static const match_table_t ovl_tokens = {
407 	{OPT_LOWERDIR,			"lowerdir=%s"},
408 	{OPT_UPPERDIR,			"upperdir=%s"},
409 	{OPT_WORKDIR,			"workdir=%s"},
410 	{OPT_DEFAULT_PERMISSIONS,	"default_permissions"},
411 	{OPT_REDIRECT_DIR,		"redirect_dir=%s"},
412 	{OPT_INDEX_ON,			"index=on"},
413 	{OPT_INDEX_OFF,			"index=off"},
414 	{OPT_NFS_EXPORT_ON,		"nfs_export=on"},
415 	{OPT_NFS_EXPORT_OFF,		"nfs_export=off"},
416 	{OPT_XINO_ON,			"xino=on"},
417 	{OPT_XINO_OFF,			"xino=off"},
418 	{OPT_XINO_AUTO,			"xino=auto"},
419 	{OPT_METACOPY_ON,		"metacopy=on"},
420 	{OPT_METACOPY_OFF,		"metacopy=off"},
421 	{OPT_ERR,			NULL}
422 };
423 
424 static char *ovl_next_opt(char **s)
425 {
426 	char *sbegin = *s;
427 	char *p;
428 
429 	if (sbegin == NULL)
430 		return NULL;
431 
432 	for (p = sbegin; *p; p++) {
433 		if (*p == '\\') {
434 			p++;
435 			if (!*p)
436 				break;
437 		} else if (*p == ',') {
438 			*p = '\0';
439 			*s = p + 1;
440 			return sbegin;
441 		}
442 	}
443 	*s = NULL;
444 	return sbegin;
445 }
446 
447 static int ovl_parse_redirect_mode(struct ovl_config *config, const char *mode)
448 {
449 	if (strcmp(mode, "on") == 0) {
450 		config->redirect_dir = true;
451 		/*
452 		 * Does not make sense to have redirect creation without
453 		 * redirect following.
454 		 */
455 		config->redirect_follow = true;
456 	} else if (strcmp(mode, "follow") == 0) {
457 		config->redirect_follow = true;
458 	} else if (strcmp(mode, "off") == 0) {
459 		if (ovl_redirect_always_follow)
460 			config->redirect_follow = true;
461 	} else if (strcmp(mode, "nofollow") != 0) {
462 		pr_err("overlayfs: bad mount option \"redirect_dir=%s\"\n",
463 		       mode);
464 		return -EINVAL;
465 	}
466 
467 	return 0;
468 }
469 
470 static int ovl_parse_opt(char *opt, struct ovl_config *config)
471 {
472 	char *p;
473 	int err;
474 	bool metacopy_opt = false, redirect_opt = false;
475 
476 	config->redirect_mode = kstrdup(ovl_redirect_mode_def(), GFP_KERNEL);
477 	if (!config->redirect_mode)
478 		return -ENOMEM;
479 
480 	while ((p = ovl_next_opt(&opt)) != NULL) {
481 		int token;
482 		substring_t args[MAX_OPT_ARGS];
483 
484 		if (!*p)
485 			continue;
486 
487 		token = match_token(p, ovl_tokens, args);
488 		switch (token) {
489 		case OPT_UPPERDIR:
490 			kfree(config->upperdir);
491 			config->upperdir = match_strdup(&args[0]);
492 			if (!config->upperdir)
493 				return -ENOMEM;
494 			break;
495 
496 		case OPT_LOWERDIR:
497 			kfree(config->lowerdir);
498 			config->lowerdir = match_strdup(&args[0]);
499 			if (!config->lowerdir)
500 				return -ENOMEM;
501 			break;
502 
503 		case OPT_WORKDIR:
504 			kfree(config->workdir);
505 			config->workdir = match_strdup(&args[0]);
506 			if (!config->workdir)
507 				return -ENOMEM;
508 			break;
509 
510 		case OPT_DEFAULT_PERMISSIONS:
511 			config->default_permissions = true;
512 			break;
513 
514 		case OPT_REDIRECT_DIR:
515 			kfree(config->redirect_mode);
516 			config->redirect_mode = match_strdup(&args[0]);
517 			if (!config->redirect_mode)
518 				return -ENOMEM;
519 			redirect_opt = true;
520 			break;
521 
522 		case OPT_INDEX_ON:
523 			config->index = true;
524 			break;
525 
526 		case OPT_INDEX_OFF:
527 			config->index = false;
528 			break;
529 
530 		case OPT_NFS_EXPORT_ON:
531 			config->nfs_export = true;
532 			break;
533 
534 		case OPT_NFS_EXPORT_OFF:
535 			config->nfs_export = false;
536 			break;
537 
538 		case OPT_XINO_ON:
539 			config->xino = OVL_XINO_ON;
540 			break;
541 
542 		case OPT_XINO_OFF:
543 			config->xino = OVL_XINO_OFF;
544 			break;
545 
546 		case OPT_XINO_AUTO:
547 			config->xino = OVL_XINO_AUTO;
548 			break;
549 
550 		case OPT_METACOPY_ON:
551 			config->metacopy = true;
552 			metacopy_opt = true;
553 			break;
554 
555 		case OPT_METACOPY_OFF:
556 			config->metacopy = false;
557 			break;
558 
559 		default:
560 			pr_err("overlayfs: unrecognized mount option \"%s\" or missing value\n", p);
561 			return -EINVAL;
562 		}
563 	}
564 
565 	/* Workdir is useless in non-upper mount */
566 	if (!config->upperdir && config->workdir) {
567 		pr_info("overlayfs: option \"workdir=%s\" is useless in a non-upper mount, ignore\n",
568 			config->workdir);
569 		kfree(config->workdir);
570 		config->workdir = NULL;
571 	}
572 
573 	err = ovl_parse_redirect_mode(config, config->redirect_mode);
574 	if (err)
575 		return err;
576 
577 	/*
578 	 * This is to make the logic below simpler.  It doesn't make any other
579 	 * difference, since config->redirect_dir is only used for upper.
580 	 */
581 	if (!config->upperdir && config->redirect_follow)
582 		config->redirect_dir = true;
583 
584 	/* Resolve metacopy -> redirect_dir dependency */
585 	if (config->metacopy && !config->redirect_dir) {
586 		if (metacopy_opt && redirect_opt) {
587 			pr_err("overlayfs: conflicting options: metacopy=on,redirect_dir=%s\n",
588 			       config->redirect_mode);
589 			return -EINVAL;
590 		}
591 		if (redirect_opt) {
592 			/*
593 			 * There was an explicit redirect_dir=... that resulted
594 			 * in this conflict.
595 			 */
596 			pr_info("overlayfs: disabling metacopy due to redirect_dir=%s\n",
597 				config->redirect_mode);
598 			config->metacopy = false;
599 		} else {
600 			/* Automatically enable redirect otherwise. */
601 			config->redirect_follow = config->redirect_dir = true;
602 		}
603 	}
604 
605 	return 0;
606 }
607 
608 #define OVL_WORKDIR_NAME "work"
609 #define OVL_INDEXDIR_NAME "index"
610 
611 static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
612 					 const char *name, bool persist)
613 {
614 	struct inode *dir =  ofs->workbasedir->d_inode;
615 	struct vfsmount *mnt = ofs->upper_mnt;
616 	struct dentry *work;
617 	int err;
618 	bool retried = false;
619 	bool locked = false;
620 
621 	inode_lock_nested(dir, I_MUTEX_PARENT);
622 	locked = true;
623 
624 retry:
625 	work = lookup_one_len(name, ofs->workbasedir, strlen(name));
626 
627 	if (!IS_ERR(work)) {
628 		struct iattr attr = {
629 			.ia_valid = ATTR_MODE,
630 			.ia_mode = S_IFDIR | 0,
631 		};
632 
633 		if (work->d_inode) {
634 			err = -EEXIST;
635 			if (retried)
636 				goto out_dput;
637 
638 			if (persist)
639 				goto out_unlock;
640 
641 			retried = true;
642 			ovl_workdir_cleanup(dir, mnt, work, 0);
643 			dput(work);
644 			goto retry;
645 		}
646 
647 		work = ovl_create_real(dir, work, OVL_CATTR(attr.ia_mode));
648 		err = PTR_ERR(work);
649 		if (IS_ERR(work))
650 			goto out_err;
651 
652 		/*
653 		 * Try to remove POSIX ACL xattrs from workdir.  We are good if:
654 		 *
655 		 * a) success (there was a POSIX ACL xattr and was removed)
656 		 * b) -ENODATA (there was no POSIX ACL xattr)
657 		 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
658 		 *
659 		 * There are various other error values that could effectively
660 		 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
661 		 * if the xattr name is too long), but the set of filesystems
662 		 * allowed as upper are limited to "normal" ones, where checking
663 		 * for the above two errors is sufficient.
664 		 */
665 		err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_DEFAULT);
666 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
667 			goto out_dput;
668 
669 		err = vfs_removexattr(work, XATTR_NAME_POSIX_ACL_ACCESS);
670 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
671 			goto out_dput;
672 
673 		/* Clear any inherited mode bits */
674 		inode_lock(work->d_inode);
675 		err = notify_change(work, &attr, NULL);
676 		inode_unlock(work->d_inode);
677 		if (err)
678 			goto out_dput;
679 	} else {
680 		err = PTR_ERR(work);
681 		goto out_err;
682 	}
683 out_unlock:
684 	if (locked)
685 		inode_unlock(dir);
686 
687 	return work;
688 
689 out_dput:
690 	dput(work);
691 out_err:
692 	pr_warn("overlayfs: failed to create directory %s/%s (errno: %i); mounting read-only\n",
693 		ofs->config.workdir, name, -err);
694 	work = NULL;
695 	goto out_unlock;
696 }
697 
698 static void ovl_unescape(char *s)
699 {
700 	char *d = s;
701 
702 	for (;; s++, d++) {
703 		if (*s == '\\')
704 			s++;
705 		*d = *s;
706 		if (!*s)
707 			break;
708 	}
709 }
710 
711 static int ovl_mount_dir_noesc(const char *name, struct path *path)
712 {
713 	int err = -EINVAL;
714 
715 	if (!*name) {
716 		pr_err("overlayfs: empty lowerdir\n");
717 		goto out;
718 	}
719 	err = kern_path(name, LOOKUP_FOLLOW, path);
720 	if (err) {
721 		pr_err("overlayfs: failed to resolve '%s': %i\n", name, err);
722 		goto out;
723 	}
724 	err = -EINVAL;
725 	if (ovl_dentry_weird(path->dentry)) {
726 		pr_err("overlayfs: filesystem on '%s' not supported\n", name);
727 		goto out_put;
728 	}
729 	if (!d_is_dir(path->dentry)) {
730 		pr_err("overlayfs: '%s' not a directory\n", name);
731 		goto out_put;
732 	}
733 	return 0;
734 
735 out_put:
736 	path_put_init(path);
737 out:
738 	return err;
739 }
740 
741 static int ovl_mount_dir(const char *name, struct path *path)
742 {
743 	int err = -ENOMEM;
744 	char *tmp = kstrdup(name, GFP_KERNEL);
745 
746 	if (tmp) {
747 		ovl_unescape(tmp);
748 		err = ovl_mount_dir_noesc(tmp, path);
749 
750 		if (!err)
751 			if (ovl_dentry_remote(path->dentry)) {
752 				pr_err("overlayfs: filesystem on '%s' not supported as upperdir\n",
753 				       tmp);
754 				path_put_init(path);
755 				err = -EINVAL;
756 			}
757 		kfree(tmp);
758 	}
759 	return err;
760 }
761 
762 static int ovl_check_namelen(struct path *path, struct ovl_fs *ofs,
763 			     const char *name)
764 {
765 	struct kstatfs statfs;
766 	int err = vfs_statfs(path, &statfs);
767 
768 	if (err)
769 		pr_err("overlayfs: statfs failed on '%s'\n", name);
770 	else
771 		ofs->namelen = max(ofs->namelen, statfs.f_namelen);
772 
773 	return err;
774 }
775 
776 static int ovl_lower_dir(const char *name, struct path *path,
777 			 struct ovl_fs *ofs, int *stack_depth, bool *remote)
778 {
779 	int fh_type;
780 	int err;
781 
782 	err = ovl_mount_dir_noesc(name, path);
783 	if (err)
784 		goto out;
785 
786 	err = ovl_check_namelen(path, ofs, name);
787 	if (err)
788 		goto out_put;
789 
790 	*stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
791 
792 	if (ovl_dentry_remote(path->dentry))
793 		*remote = true;
794 
795 	/*
796 	 * The inodes index feature and NFS export need to encode and decode
797 	 * file handles, so they require that all layers support them.
798 	 */
799 	fh_type = ovl_can_decode_fh(path->dentry->d_sb);
800 	if ((ofs->config.nfs_export ||
801 	     (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
802 		ofs->config.index = false;
803 		ofs->config.nfs_export = false;
804 		pr_warn("overlayfs: fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
805 			name);
806 	}
807 
808 	/* Check if lower fs has 32bit inode numbers */
809 	if (fh_type != FILEID_INO32_GEN)
810 		ofs->xino_bits = 0;
811 
812 	return 0;
813 
814 out_put:
815 	path_put_init(path);
816 out:
817 	return err;
818 }
819 
820 /* Workdir should not be subdir of upperdir and vice versa */
821 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
822 {
823 	bool ok = false;
824 
825 	if (workdir != upperdir) {
826 		ok = (lock_rename(workdir, upperdir) == NULL);
827 		unlock_rename(workdir, upperdir);
828 	}
829 	return ok;
830 }
831 
832 static unsigned int ovl_split_lowerdirs(char *str)
833 {
834 	unsigned int ctr = 1;
835 	char *s, *d;
836 
837 	for (s = d = str;; s++, d++) {
838 		if (*s == '\\') {
839 			s++;
840 		} else if (*s == ':') {
841 			*d = '\0';
842 			ctr++;
843 			continue;
844 		}
845 		*d = *s;
846 		if (!*s)
847 			break;
848 	}
849 	return ctr;
850 }
851 
852 static int __maybe_unused
853 ovl_posix_acl_xattr_get(const struct xattr_handler *handler,
854 			struct dentry *dentry, struct inode *inode,
855 			const char *name, void *buffer, size_t size)
856 {
857 	return ovl_xattr_get(dentry, inode, handler->name, buffer, size);
858 }
859 
860 static int __maybe_unused
861 ovl_posix_acl_xattr_set(const struct xattr_handler *handler,
862 			struct dentry *dentry, struct inode *inode,
863 			const char *name, const void *value,
864 			size_t size, int flags)
865 {
866 	struct dentry *workdir = ovl_workdir(dentry);
867 	struct inode *realinode = ovl_inode_real(inode);
868 	struct posix_acl *acl = NULL;
869 	int err;
870 
871 	/* Check that everything is OK before copy-up */
872 	if (value) {
873 		acl = posix_acl_from_xattr(&init_user_ns, value, size);
874 		if (IS_ERR(acl))
875 			return PTR_ERR(acl);
876 	}
877 	err = -EOPNOTSUPP;
878 	if (!IS_POSIXACL(d_inode(workdir)))
879 		goto out_acl_release;
880 	if (!realinode->i_op->set_acl)
881 		goto out_acl_release;
882 	if (handler->flags == ACL_TYPE_DEFAULT && !S_ISDIR(inode->i_mode)) {
883 		err = acl ? -EACCES : 0;
884 		goto out_acl_release;
885 	}
886 	err = -EPERM;
887 	if (!inode_owner_or_capable(inode))
888 		goto out_acl_release;
889 
890 	posix_acl_release(acl);
891 
892 	/*
893 	 * Check if sgid bit needs to be cleared (actual setacl operation will
894 	 * be done with mounter's capabilities and so that won't do it for us).
895 	 */
896 	if (unlikely(inode->i_mode & S_ISGID) &&
897 	    handler->flags == ACL_TYPE_ACCESS &&
898 	    !in_group_p(inode->i_gid) &&
899 	    !capable_wrt_inode_uidgid(inode, CAP_FSETID)) {
900 		struct iattr iattr = { .ia_valid = ATTR_KILL_SGID };
901 
902 		err = ovl_setattr(dentry, &iattr);
903 		if (err)
904 			return err;
905 	}
906 
907 	err = ovl_xattr_set(dentry, inode, handler->name, value, size, flags);
908 	if (!err)
909 		ovl_copyattr(ovl_inode_real(inode), inode);
910 
911 	return err;
912 
913 out_acl_release:
914 	posix_acl_release(acl);
915 	return err;
916 }
917 
918 static int ovl_own_xattr_get(const struct xattr_handler *handler,
919 			     struct dentry *dentry, struct inode *inode,
920 			     const char *name, void *buffer, size_t size)
921 {
922 	return -EOPNOTSUPP;
923 }
924 
925 static int ovl_own_xattr_set(const struct xattr_handler *handler,
926 			     struct dentry *dentry, struct inode *inode,
927 			     const char *name, const void *value,
928 			     size_t size, int flags)
929 {
930 	return -EOPNOTSUPP;
931 }
932 
933 static int ovl_other_xattr_get(const struct xattr_handler *handler,
934 			       struct dentry *dentry, struct inode *inode,
935 			       const char *name, void *buffer, size_t size)
936 {
937 	return ovl_xattr_get(dentry, inode, name, buffer, size);
938 }
939 
940 static int ovl_other_xattr_set(const struct xattr_handler *handler,
941 			       struct dentry *dentry, struct inode *inode,
942 			       const char *name, const void *value,
943 			       size_t size, int flags)
944 {
945 	return ovl_xattr_set(dentry, inode, name, value, size, flags);
946 }
947 
948 static const struct xattr_handler __maybe_unused
949 ovl_posix_acl_access_xattr_handler = {
950 	.name = XATTR_NAME_POSIX_ACL_ACCESS,
951 	.flags = ACL_TYPE_ACCESS,
952 	.get = ovl_posix_acl_xattr_get,
953 	.set = ovl_posix_acl_xattr_set,
954 };
955 
956 static const struct xattr_handler __maybe_unused
957 ovl_posix_acl_default_xattr_handler = {
958 	.name = XATTR_NAME_POSIX_ACL_DEFAULT,
959 	.flags = ACL_TYPE_DEFAULT,
960 	.get = ovl_posix_acl_xattr_get,
961 	.set = ovl_posix_acl_xattr_set,
962 };
963 
964 static const struct xattr_handler ovl_own_xattr_handler = {
965 	.prefix	= OVL_XATTR_PREFIX,
966 	.get = ovl_own_xattr_get,
967 	.set = ovl_own_xattr_set,
968 };
969 
970 static const struct xattr_handler ovl_other_xattr_handler = {
971 	.prefix	= "", /* catch all */
972 	.get = ovl_other_xattr_get,
973 	.set = ovl_other_xattr_set,
974 };
975 
976 static const struct xattr_handler *ovl_xattr_handlers[] = {
977 #ifdef CONFIG_FS_POSIX_ACL
978 	&ovl_posix_acl_access_xattr_handler,
979 	&ovl_posix_acl_default_xattr_handler,
980 #endif
981 	&ovl_own_xattr_handler,
982 	&ovl_other_xattr_handler,
983 	NULL
984 };
985 
986 static int ovl_get_upper(struct ovl_fs *ofs, struct path *upperpath)
987 {
988 	struct vfsmount *upper_mnt;
989 	int err;
990 
991 	err = ovl_mount_dir(ofs->config.upperdir, upperpath);
992 	if (err)
993 		goto out;
994 
995 	/* Upper fs should not be r/o */
996 	if (sb_rdonly(upperpath->mnt->mnt_sb)) {
997 		pr_err("overlayfs: upper fs is r/o, try multi-lower layers mount\n");
998 		err = -EINVAL;
999 		goto out;
1000 	}
1001 
1002 	err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
1003 	if (err)
1004 		goto out;
1005 
1006 	upper_mnt = clone_private_mount(upperpath);
1007 	err = PTR_ERR(upper_mnt);
1008 	if (IS_ERR(upper_mnt)) {
1009 		pr_err("overlayfs: failed to clone upperpath\n");
1010 		goto out;
1011 	}
1012 
1013 	/* Don't inherit atime flags */
1014 	upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
1015 	ofs->upper_mnt = upper_mnt;
1016 
1017 	err = -EBUSY;
1018 	if (ovl_inuse_trylock(ofs->upper_mnt->mnt_root)) {
1019 		ofs->upperdir_locked = true;
1020 	} else if (ofs->config.index) {
1021 		pr_err("overlayfs: upperdir is in-use by another mount, mount with '-o index=off' to override exclusive upperdir protection.\n");
1022 		goto out;
1023 	} else {
1024 		pr_warn("overlayfs: upperdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1025 	}
1026 
1027 	err = 0;
1028 out:
1029 	return err;
1030 }
1031 
1032 static int ovl_make_workdir(struct ovl_fs *ofs, struct path *workpath)
1033 {
1034 	struct vfsmount *mnt = ofs->upper_mnt;
1035 	struct dentry *temp;
1036 	int fh_type;
1037 	int err;
1038 
1039 	err = mnt_want_write(mnt);
1040 	if (err)
1041 		return err;
1042 
1043 	ofs->workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
1044 	if (!ofs->workdir)
1045 		goto out;
1046 
1047 	/*
1048 	 * Upper should support d_type, else whiteouts are visible.  Given
1049 	 * workdir and upper are on same fs, we can do iterate_dir() on
1050 	 * workdir. This check requires successful creation of workdir in
1051 	 * previous step.
1052 	 */
1053 	err = ovl_check_d_type_supported(workpath);
1054 	if (err < 0)
1055 		goto out;
1056 
1057 	/*
1058 	 * We allowed this configuration and don't want to break users over
1059 	 * kernel upgrade. So warn instead of erroring out.
1060 	 */
1061 	if (!err)
1062 		pr_warn("overlayfs: upper fs needs to support d_type.\n");
1063 
1064 	/* Check if upper/work fs supports O_TMPFILE */
1065 	temp = ovl_do_tmpfile(ofs->workdir, S_IFREG | 0);
1066 	ofs->tmpfile = !IS_ERR(temp);
1067 	if (ofs->tmpfile)
1068 		dput(temp);
1069 	else
1070 		pr_warn("overlayfs: upper fs does not support tmpfile.\n");
1071 
1072 	/*
1073 	 * Check if upper/work fs supports trusted.overlay.* xattr
1074 	 */
1075 	err = ovl_do_setxattr(ofs->workdir, OVL_XATTR_OPAQUE, "0", 1, 0);
1076 	if (err) {
1077 		ofs->noxattr = true;
1078 		ofs->config.index = false;
1079 		ofs->config.metacopy = false;
1080 		pr_warn("overlayfs: upper fs does not support xattr, falling back to index=off and metacopy=off.\n");
1081 		err = 0;
1082 	} else {
1083 		vfs_removexattr(ofs->workdir, OVL_XATTR_OPAQUE);
1084 	}
1085 
1086 	/* Check if upper/work fs supports file handles */
1087 	fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
1088 	if (ofs->config.index && !fh_type) {
1089 		ofs->config.index = false;
1090 		pr_warn("overlayfs: upper fs does not support file handles, falling back to index=off.\n");
1091 	}
1092 
1093 	/* Check if upper fs has 32bit inode numbers */
1094 	if (fh_type != FILEID_INO32_GEN)
1095 		ofs->xino_bits = 0;
1096 
1097 	/* NFS export of r/w mount depends on index */
1098 	if (ofs->config.nfs_export && !ofs->config.index) {
1099 		pr_warn("overlayfs: NFS export requires \"index=on\", falling back to nfs_export=off.\n");
1100 		ofs->config.nfs_export = false;
1101 	}
1102 out:
1103 	mnt_drop_write(mnt);
1104 	return err;
1105 }
1106 
1107 static int ovl_get_workdir(struct ovl_fs *ofs, struct path *upperpath)
1108 {
1109 	int err;
1110 	struct path workpath = { };
1111 
1112 	err = ovl_mount_dir(ofs->config.workdir, &workpath);
1113 	if (err)
1114 		goto out;
1115 
1116 	err = -EINVAL;
1117 	if (upperpath->mnt != workpath.mnt) {
1118 		pr_err("overlayfs: workdir and upperdir must reside under the same mount\n");
1119 		goto out;
1120 	}
1121 	if (!ovl_workdir_ok(workpath.dentry, upperpath->dentry)) {
1122 		pr_err("overlayfs: workdir and upperdir must be separate subtrees\n");
1123 		goto out;
1124 	}
1125 
1126 	ofs->workbasedir = dget(workpath.dentry);
1127 
1128 	err = -EBUSY;
1129 	if (ovl_inuse_trylock(ofs->workbasedir)) {
1130 		ofs->workdir_locked = true;
1131 	} else if (ofs->config.index) {
1132 		pr_err("overlayfs: workdir is in-use by another mount, mount with '-o index=off' to override exclusive workdir protection.\n");
1133 		goto out;
1134 	} else {
1135 		pr_warn("overlayfs: workdir is in-use by another mount, accessing files from both mounts will result in undefined behavior.\n");
1136 	}
1137 
1138 	err = ovl_make_workdir(ofs, &workpath);
1139 	if (err)
1140 		goto out;
1141 
1142 	err = 0;
1143 out:
1144 	path_put(&workpath);
1145 
1146 	return err;
1147 }
1148 
1149 static int ovl_get_indexdir(struct ovl_fs *ofs, struct ovl_entry *oe,
1150 			    struct path *upperpath)
1151 {
1152 	struct vfsmount *mnt = ofs->upper_mnt;
1153 	int err;
1154 
1155 	err = mnt_want_write(mnt);
1156 	if (err)
1157 		return err;
1158 
1159 	/* Verify lower root is upper root origin */
1160 	err = ovl_verify_origin(upperpath->dentry, oe->lowerstack[0].dentry,
1161 				true);
1162 	if (err) {
1163 		pr_err("overlayfs: failed to verify upper root origin\n");
1164 		goto out;
1165 	}
1166 
1167 	ofs->indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
1168 	if (ofs->indexdir) {
1169 		/*
1170 		 * Verify upper root is exclusively associated with index dir.
1171 		 * Older kernels stored upper fh in "trusted.overlay.origin"
1172 		 * xattr. If that xattr exists, verify that it is a match to
1173 		 * upper dir file handle. In any case, verify or set xattr
1174 		 * "trusted.overlay.upper" to indicate that index may have
1175 		 * directory entries.
1176 		 */
1177 		if (ovl_check_origin_xattr(ofs->indexdir)) {
1178 			err = ovl_verify_set_fh(ofs->indexdir, OVL_XATTR_ORIGIN,
1179 						upperpath->dentry, true, false);
1180 			if (err)
1181 				pr_err("overlayfs: failed to verify index dir 'origin' xattr\n");
1182 		}
1183 		err = ovl_verify_upper(ofs->indexdir, upperpath->dentry, true);
1184 		if (err)
1185 			pr_err("overlayfs: failed to verify index dir 'upper' xattr\n");
1186 
1187 		/* Cleanup bad/stale/orphan index entries */
1188 		if (!err)
1189 			err = ovl_indexdir_cleanup(ofs);
1190 	}
1191 	if (err || !ofs->indexdir)
1192 		pr_warn("overlayfs: try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
1193 
1194 out:
1195 	mnt_drop_write(mnt);
1196 	return err;
1197 }
1198 
1199 static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
1200 {
1201 	unsigned int i;
1202 
1203 	if (!ofs->config.nfs_export && !(ofs->config.index && ofs->upper_mnt))
1204 		return true;
1205 
1206 	for (i = 0; i < ofs->numlowerfs; i++) {
1207 		/*
1208 		 * We use uuid to associate an overlay lower file handle with a
1209 		 * lower layer, so we can accept lower fs with null uuid as long
1210 		 * as all lower layers with null uuid are on the same fs.
1211 		 */
1212 		if (uuid_equal(&ofs->lower_fs[i].sb->s_uuid, uuid))
1213 			return false;
1214 	}
1215 	return true;
1216 }
1217 
1218 /* Get a unique fsid for the layer */
1219 static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
1220 {
1221 	struct super_block *sb = path->mnt->mnt_sb;
1222 	unsigned int i;
1223 	dev_t dev;
1224 	int err;
1225 
1226 	/* fsid 0 is reserved for upper fs even with non upper overlay */
1227 	if (ofs->upper_mnt && ofs->upper_mnt->mnt_sb == sb)
1228 		return 0;
1229 
1230 	for (i = 0; i < ofs->numlowerfs; i++) {
1231 		if (ofs->lower_fs[i].sb == sb)
1232 			return i + 1;
1233 	}
1234 
1235 	if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
1236 		ofs->config.index = false;
1237 		ofs->config.nfs_export = false;
1238 		pr_warn("overlayfs: %s uuid detected in lower fs '%pd2', falling back to index=off,nfs_export=off.\n",
1239 			uuid_is_null(&sb->s_uuid) ? "null" : "conflicting",
1240 			path->dentry);
1241 	}
1242 
1243 	err = get_anon_bdev(&dev);
1244 	if (err) {
1245 		pr_err("overlayfs: failed to get anonymous bdev for lowerpath\n");
1246 		return err;
1247 	}
1248 
1249 	ofs->lower_fs[ofs->numlowerfs].sb = sb;
1250 	ofs->lower_fs[ofs->numlowerfs].pseudo_dev = dev;
1251 	ofs->numlowerfs++;
1252 
1253 	return ofs->numlowerfs;
1254 }
1255 
1256 static int ovl_get_lower_layers(struct ovl_fs *ofs, struct path *stack,
1257 				unsigned int numlower)
1258 {
1259 	int err;
1260 	unsigned int i;
1261 
1262 	err = -ENOMEM;
1263 	ofs->lower_layers = kcalloc(numlower, sizeof(struct ovl_layer),
1264 				    GFP_KERNEL);
1265 	if (ofs->lower_layers == NULL)
1266 		goto out;
1267 
1268 	ofs->lower_fs = kcalloc(numlower, sizeof(struct ovl_sb),
1269 				GFP_KERNEL);
1270 	if (ofs->lower_fs == NULL)
1271 		goto out;
1272 
1273 	for (i = 0; i < numlower; i++) {
1274 		struct vfsmount *mnt;
1275 		int fsid;
1276 
1277 		err = fsid = ovl_get_fsid(ofs, &stack[i]);
1278 		if (err < 0)
1279 			goto out;
1280 
1281 		mnt = clone_private_mount(&stack[i]);
1282 		err = PTR_ERR(mnt);
1283 		if (IS_ERR(mnt)) {
1284 			pr_err("overlayfs: failed to clone lowerpath\n");
1285 			goto out;
1286 		}
1287 
1288 		/*
1289 		 * Make lower layers R/O.  That way fchmod/fchown on lower file
1290 		 * will fail instead of modifying lower fs.
1291 		 */
1292 		mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1293 
1294 		ofs->lower_layers[ofs->numlower].mnt = mnt;
1295 		ofs->lower_layers[ofs->numlower].idx = i + 1;
1296 		ofs->lower_layers[ofs->numlower].fsid = fsid;
1297 		if (fsid) {
1298 			ofs->lower_layers[ofs->numlower].fs =
1299 				&ofs->lower_fs[fsid - 1];
1300 		}
1301 		ofs->numlower++;
1302 	}
1303 
1304 	/*
1305 	 * When all layers on same fs, overlay can use real inode numbers.
1306 	 * With mount option "xino=on", mounter declares that there are enough
1307 	 * free high bits in underlying fs to hold the unique fsid.
1308 	 * If overlayfs does encounter underlying inodes using the high xino
1309 	 * bits reserved for fsid, it emits a warning and uses the original
1310 	 * inode number.
1311 	 */
1312 	if (!ofs->numlowerfs || (ofs->numlowerfs == 1 && !ofs->upper_mnt)) {
1313 		ofs->xino_bits = 0;
1314 		ofs->config.xino = OVL_XINO_OFF;
1315 	} else if (ofs->config.xino == OVL_XINO_ON && !ofs->xino_bits) {
1316 		/*
1317 		 * This is a roundup of number of bits needed for numlowerfs+1
1318 		 * (i.e. ilog2(numlowerfs+1 - 1) + 1). fsid 0 is reserved for
1319 		 * upper fs even with non upper overlay.
1320 		 */
1321 		BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 31);
1322 		ofs->xino_bits = ilog2(ofs->numlowerfs) + 1;
1323 	}
1324 
1325 	if (ofs->xino_bits) {
1326 		pr_info("overlayfs: \"xino\" feature enabled using %d upper inode bits.\n",
1327 			ofs->xino_bits);
1328 	}
1329 
1330 	err = 0;
1331 out:
1332 	return err;
1333 }
1334 
1335 static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1336 					    struct ovl_fs *ofs)
1337 {
1338 	int err;
1339 	char *lowertmp, *lower;
1340 	struct path *stack = NULL;
1341 	unsigned int stacklen, numlower = 0, i;
1342 	bool remote = false;
1343 	struct ovl_entry *oe;
1344 
1345 	err = -ENOMEM;
1346 	lowertmp = kstrdup(ofs->config.lowerdir, GFP_KERNEL);
1347 	if (!lowertmp)
1348 		goto out_err;
1349 
1350 	err = -EINVAL;
1351 	stacklen = ovl_split_lowerdirs(lowertmp);
1352 	if (stacklen > OVL_MAX_STACK) {
1353 		pr_err("overlayfs: too many lower directories, limit is %d\n",
1354 		       OVL_MAX_STACK);
1355 		goto out_err;
1356 	} else if (!ofs->config.upperdir && stacklen == 1) {
1357 		pr_err("overlayfs: at least 2 lowerdir are needed while upperdir nonexistent\n");
1358 		goto out_err;
1359 	} else if (!ofs->config.upperdir && ofs->config.nfs_export &&
1360 		   ofs->config.redirect_follow) {
1361 		pr_warn("overlayfs: NFS export requires \"redirect_dir=nofollow\" on non-upper mount, falling back to nfs_export=off.\n");
1362 		ofs->config.nfs_export = false;
1363 	}
1364 
1365 	err = -ENOMEM;
1366 	stack = kcalloc(stacklen, sizeof(struct path), GFP_KERNEL);
1367 	if (!stack)
1368 		goto out_err;
1369 
1370 	err = -EINVAL;
1371 	lower = lowertmp;
1372 	for (numlower = 0; numlower < stacklen; numlower++) {
1373 		err = ovl_lower_dir(lower, &stack[numlower], ofs,
1374 				    &sb->s_stack_depth, &remote);
1375 		if (err)
1376 			goto out_err;
1377 
1378 		lower = strchr(lower, '\0') + 1;
1379 	}
1380 
1381 	err = -EINVAL;
1382 	sb->s_stack_depth++;
1383 	if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1384 		pr_err("overlayfs: maximum fs stacking depth exceeded\n");
1385 		goto out_err;
1386 	}
1387 
1388 	err = ovl_get_lower_layers(ofs, stack, numlower);
1389 	if (err)
1390 		goto out_err;
1391 
1392 	err = -ENOMEM;
1393 	oe = ovl_alloc_entry(numlower);
1394 	if (!oe)
1395 		goto out_err;
1396 
1397 	for (i = 0; i < numlower; i++) {
1398 		oe->lowerstack[i].dentry = dget(stack[i].dentry);
1399 		oe->lowerstack[i].layer = &ofs->lower_layers[i];
1400 	}
1401 
1402 	if (remote)
1403 		sb->s_d_op = &ovl_reval_dentry_operations;
1404 	else
1405 		sb->s_d_op = &ovl_dentry_operations;
1406 
1407 out:
1408 	for (i = 0; i < numlower; i++)
1409 		path_put(&stack[i]);
1410 	kfree(stack);
1411 	kfree(lowertmp);
1412 
1413 	return oe;
1414 
1415 out_err:
1416 	oe = ERR_PTR(err);
1417 	goto out;
1418 }
1419 
1420 static int ovl_fill_super(struct super_block *sb, void *data, int silent)
1421 {
1422 	struct path upperpath = { };
1423 	struct dentry *root_dentry;
1424 	struct ovl_entry *oe;
1425 	struct ovl_fs *ofs;
1426 	struct cred *cred;
1427 	int err;
1428 
1429 	err = -ENOMEM;
1430 	ofs = kzalloc(sizeof(struct ovl_fs), GFP_KERNEL);
1431 	if (!ofs)
1432 		goto out;
1433 
1434 	ofs->creator_cred = cred = prepare_creds();
1435 	if (!cred)
1436 		goto out_err;
1437 
1438 	ofs->config.index = ovl_index_def;
1439 	ofs->config.nfs_export = ovl_nfs_export_def;
1440 	ofs->config.xino = ovl_xino_def();
1441 	ofs->config.metacopy = ovl_metacopy_def;
1442 	err = ovl_parse_opt((char *) data, &ofs->config);
1443 	if (err)
1444 		goto out_err;
1445 
1446 	err = -EINVAL;
1447 	if (!ofs->config.lowerdir) {
1448 		if (!silent)
1449 			pr_err("overlayfs: missing 'lowerdir'\n");
1450 		goto out_err;
1451 	}
1452 
1453 	sb->s_stack_depth = 0;
1454 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1455 	/* Assume underlaying fs uses 32bit inodes unless proven otherwise */
1456 	if (ofs->config.xino != OVL_XINO_OFF)
1457 		ofs->xino_bits = BITS_PER_LONG - 32;
1458 
1459 	if (ofs->config.upperdir) {
1460 		if (!ofs->config.workdir) {
1461 			pr_err("overlayfs: missing 'workdir'\n");
1462 			goto out_err;
1463 		}
1464 
1465 		err = ovl_get_upper(ofs, &upperpath);
1466 		if (err)
1467 			goto out_err;
1468 
1469 		err = ovl_get_workdir(ofs, &upperpath);
1470 		if (err)
1471 			goto out_err;
1472 
1473 		if (!ofs->workdir)
1474 			sb->s_flags |= SB_RDONLY;
1475 
1476 		sb->s_stack_depth = ofs->upper_mnt->mnt_sb->s_stack_depth;
1477 		sb->s_time_gran = ofs->upper_mnt->mnt_sb->s_time_gran;
1478 
1479 	}
1480 	oe = ovl_get_lowerstack(sb, ofs);
1481 	err = PTR_ERR(oe);
1482 	if (IS_ERR(oe))
1483 		goto out_err;
1484 
1485 	/* If the upper fs is nonexistent, we mark overlayfs r/o too */
1486 	if (!ofs->upper_mnt)
1487 		sb->s_flags |= SB_RDONLY;
1488 
1489 	if (!(ovl_force_readonly(ofs)) && ofs->config.index) {
1490 		err = ovl_get_indexdir(ofs, oe, &upperpath);
1491 		if (err)
1492 			goto out_free_oe;
1493 
1494 		/* Force r/o mount with no index dir */
1495 		if (!ofs->indexdir) {
1496 			dput(ofs->workdir);
1497 			ofs->workdir = NULL;
1498 			sb->s_flags |= SB_RDONLY;
1499 		}
1500 
1501 	}
1502 
1503 	/* Show index=off in /proc/mounts for forced r/o mount */
1504 	if (!ofs->indexdir) {
1505 		ofs->config.index = false;
1506 		if (ofs->upper_mnt && ofs->config.nfs_export) {
1507 			pr_warn("overlayfs: NFS export requires an index dir, falling back to nfs_export=off.\n");
1508 			ofs->config.nfs_export = false;
1509 		}
1510 	}
1511 
1512 	if (ofs->config.metacopy && ofs->config.nfs_export) {
1513 		pr_warn("overlayfs: NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1514 		ofs->config.nfs_export = false;
1515 	}
1516 
1517 	if (ofs->config.nfs_export)
1518 		sb->s_export_op = &ovl_export_operations;
1519 
1520 	/* Never override disk quota limits or use reserved space */
1521 	cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1522 
1523 	sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1524 	sb->s_op = &ovl_super_operations;
1525 	sb->s_xattr = ovl_xattr_handlers;
1526 	sb->s_fs_info = ofs;
1527 	sb->s_flags |= SB_POSIXACL;
1528 
1529 	err = -ENOMEM;
1530 	root_dentry = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1531 	if (!root_dentry)
1532 		goto out_free_oe;
1533 
1534 	root_dentry->d_fsdata = oe;
1535 
1536 	mntput(upperpath.mnt);
1537 	if (upperpath.dentry) {
1538 		ovl_dentry_set_upper_alias(root_dentry);
1539 		if (ovl_is_impuredir(upperpath.dentry))
1540 			ovl_set_flag(OVL_IMPURE, d_inode(root_dentry));
1541 	}
1542 
1543 	/* Root is always merge -> can have whiteouts */
1544 	ovl_set_flag(OVL_WHITEOUTS, d_inode(root_dentry));
1545 	ovl_dentry_set_flag(OVL_E_CONNECTED, root_dentry);
1546 	ovl_set_upperdata(d_inode(root_dentry));
1547 	ovl_inode_init(d_inode(root_dentry), upperpath.dentry,
1548 		       ovl_dentry_lower(root_dentry), NULL);
1549 
1550 	sb->s_root = root_dentry;
1551 
1552 	return 0;
1553 
1554 out_free_oe:
1555 	ovl_entry_stack_free(oe);
1556 	kfree(oe);
1557 out_err:
1558 	path_put(&upperpath);
1559 	ovl_free_fs(ofs);
1560 out:
1561 	return err;
1562 }
1563 
1564 static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
1565 				const char *dev_name, void *raw_data)
1566 {
1567 	return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
1568 }
1569 
1570 static struct file_system_type ovl_fs_type = {
1571 	.owner		= THIS_MODULE,
1572 	.name		= "overlay",
1573 	.mount		= ovl_mount,
1574 	.kill_sb	= kill_anon_super,
1575 };
1576 MODULE_ALIAS_FS("overlay");
1577 
1578 static void ovl_inode_init_once(void *foo)
1579 {
1580 	struct ovl_inode *oi = foo;
1581 
1582 	inode_init_once(&oi->vfs_inode);
1583 }
1584 
1585 static int __init ovl_init(void)
1586 {
1587 	int err;
1588 
1589 	ovl_inode_cachep = kmem_cache_create("ovl_inode",
1590 					     sizeof(struct ovl_inode), 0,
1591 					     (SLAB_RECLAIM_ACCOUNT|
1592 					      SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1593 					     ovl_inode_init_once);
1594 	if (ovl_inode_cachep == NULL)
1595 		return -ENOMEM;
1596 
1597 	err = register_filesystem(&ovl_fs_type);
1598 	if (err)
1599 		kmem_cache_destroy(ovl_inode_cachep);
1600 
1601 	return err;
1602 }
1603 
1604 static void __exit ovl_exit(void)
1605 {
1606 	unregister_filesystem(&ovl_fs_type);
1607 
1608 	/*
1609 	 * Make sure all delayed rcu free inodes are flushed before we
1610 	 * destroy cache.
1611 	 */
1612 	rcu_barrier();
1613 	kmem_cache_destroy(ovl_inode_cachep);
1614 
1615 }
1616 
1617 module_init(ovl_init);
1618 module_exit(ovl_exit);
1619