xref: /linux/fs/overlayfs/super.c (revision 2174181019e4273e583a0f0a9795e9db38984784)
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_context.h>
20 #include <linux/fs_parser.h>
21 #include "overlayfs.h"
22 #include "params.h"
23 
24 MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
25 MODULE_DESCRIPTION("Overlay filesystem");
26 MODULE_LICENSE("GPL");
27 
28 
29 struct ovl_dir_cache;
30 
31 static struct dentry *ovl_d_real(struct dentry *dentry,
32 				 const struct inode *inode)
33 {
34 	struct dentry *real = NULL, *lower;
35 
36 	/* It's an overlay file */
37 	if (inode && d_inode(dentry) == inode)
38 		return dentry;
39 
40 	if (!d_is_reg(dentry)) {
41 		if (!inode || inode == d_inode(dentry))
42 			return dentry;
43 		goto bug;
44 	}
45 
46 	real = ovl_dentry_upper(dentry);
47 	if (real && (inode == d_inode(real)))
48 		return real;
49 
50 	if (real && !inode && ovl_has_upperdata(d_inode(dentry)))
51 		return real;
52 
53 	/*
54 	 * Best effort lazy lookup of lowerdata for !inode case to return
55 	 * the real lowerdata dentry.  The only current caller of d_real() with
56 	 * NULL inode is d_real_inode() from trace_uprobe and this caller is
57 	 * likely going to be followed reading from the file, before placing
58 	 * uprobes on offset within the file, so lowerdata should be available
59 	 * when setting the uprobe.
60 	 */
61 	ovl_maybe_lookup_lowerdata(dentry);
62 	lower = ovl_dentry_lowerdata(dentry);
63 	if (!lower)
64 		goto bug;
65 	real = lower;
66 
67 	/* Handle recursion */
68 	real = d_real(real, inode);
69 
70 	if (!inode || inode == d_inode(real))
71 		return real;
72 bug:
73 	WARN(1, "%s(%pd4, %s:%lu): real dentry (%p/%lu) not found\n",
74 	     __func__, dentry, inode ? inode->i_sb->s_id : "NULL",
75 	     inode ? inode->i_ino : 0, real,
76 	     real && d_inode(real) ? d_inode(real)->i_ino : 0);
77 	return dentry;
78 }
79 
80 static int ovl_revalidate_real(struct dentry *d, unsigned int flags, bool weak)
81 {
82 	int ret = 1;
83 
84 	if (!d)
85 		return 1;
86 
87 	if (weak) {
88 		if (d->d_flags & DCACHE_OP_WEAK_REVALIDATE)
89 			ret =  d->d_op->d_weak_revalidate(d, flags);
90 	} else if (d->d_flags & DCACHE_OP_REVALIDATE) {
91 		ret = d->d_op->d_revalidate(d, flags);
92 		if (!ret) {
93 			if (!(flags & LOOKUP_RCU))
94 				d_invalidate(d);
95 			ret = -ESTALE;
96 		}
97 	}
98 	return ret;
99 }
100 
101 static int ovl_dentry_revalidate_common(struct dentry *dentry,
102 					unsigned int flags, bool weak)
103 {
104 	struct ovl_entry *oe = OVL_E(dentry);
105 	struct ovl_path *lowerstack = ovl_lowerstack(oe);
106 	struct inode *inode = d_inode_rcu(dentry);
107 	struct dentry *upper;
108 	unsigned int i;
109 	int ret = 1;
110 
111 	/* Careful in RCU mode */
112 	if (!inode)
113 		return -ECHILD;
114 
115 	upper = ovl_i_dentry_upper(inode);
116 	if (upper)
117 		ret = ovl_revalidate_real(upper, flags, weak);
118 
119 	for (i = 0; ret > 0 && i < ovl_numlower(oe); i++)
120 		ret = ovl_revalidate_real(lowerstack[i].dentry, flags, weak);
121 
122 	return ret;
123 }
124 
125 static int ovl_dentry_revalidate(struct dentry *dentry, unsigned int flags)
126 {
127 	return ovl_dentry_revalidate_common(dentry, flags, false);
128 }
129 
130 static int ovl_dentry_weak_revalidate(struct dentry *dentry, unsigned int flags)
131 {
132 	return ovl_dentry_revalidate_common(dentry, flags, true);
133 }
134 
135 static const struct dentry_operations ovl_dentry_operations = {
136 	.d_real = ovl_d_real,
137 	.d_revalidate = ovl_dentry_revalidate,
138 	.d_weak_revalidate = ovl_dentry_weak_revalidate,
139 };
140 
141 static struct kmem_cache *ovl_inode_cachep;
142 
143 static struct inode *ovl_alloc_inode(struct super_block *sb)
144 {
145 	struct ovl_inode *oi = alloc_inode_sb(sb, ovl_inode_cachep, GFP_KERNEL);
146 
147 	if (!oi)
148 		return NULL;
149 
150 	oi->cache = NULL;
151 	oi->redirect = NULL;
152 	oi->version = 0;
153 	oi->flags = 0;
154 	oi->__upperdentry = NULL;
155 	oi->lowerdata_redirect = NULL;
156 	oi->oe = NULL;
157 	mutex_init(&oi->lock);
158 
159 	return &oi->vfs_inode;
160 }
161 
162 static void ovl_free_inode(struct inode *inode)
163 {
164 	struct ovl_inode *oi = OVL_I(inode);
165 
166 	kfree(oi->redirect);
167 	mutex_destroy(&oi->lock);
168 	kmem_cache_free(ovl_inode_cachep, oi);
169 }
170 
171 static void ovl_destroy_inode(struct inode *inode)
172 {
173 	struct ovl_inode *oi = OVL_I(inode);
174 
175 	dput(oi->__upperdentry);
176 	ovl_free_entry(oi->oe);
177 	if (S_ISDIR(inode->i_mode))
178 		ovl_dir_cache_free(inode);
179 	else
180 		kfree(oi->lowerdata_redirect);
181 }
182 
183 static void ovl_put_super(struct super_block *sb)
184 {
185 	struct ovl_fs *ofs = sb->s_fs_info;
186 
187 	if (ofs)
188 		ovl_free_fs(ofs);
189 }
190 
191 /* Sync real dirty inodes in upper filesystem (if it exists) */
192 static int ovl_sync_fs(struct super_block *sb, int wait)
193 {
194 	struct ovl_fs *ofs = sb->s_fs_info;
195 	struct super_block *upper_sb;
196 	int ret;
197 
198 	ret = ovl_sync_status(ofs);
199 	/*
200 	 * We have to always set the err, because the return value isn't
201 	 * checked in syncfs, and instead indirectly return an error via
202 	 * the sb's writeback errseq, which VFS inspects after this call.
203 	 */
204 	if (ret < 0) {
205 		errseq_set(&sb->s_wb_err, -EIO);
206 		return -EIO;
207 	}
208 
209 	if (!ret)
210 		return ret;
211 
212 	/*
213 	 * Not called for sync(2) call or an emergency sync (SB_I_SKIP_SYNC).
214 	 * All the super blocks will be iterated, including upper_sb.
215 	 *
216 	 * If this is a syncfs(2) call, then we do need to call
217 	 * sync_filesystem() on upper_sb, but enough if we do it when being
218 	 * called with wait == 1.
219 	 */
220 	if (!wait)
221 		return 0;
222 
223 	upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
224 
225 	down_read(&upper_sb->s_umount);
226 	ret = sync_filesystem(upper_sb);
227 	up_read(&upper_sb->s_umount);
228 
229 	return ret;
230 }
231 
232 /**
233  * ovl_statfs
234  * @dentry: The dentry to query
235  * @buf: The struct kstatfs to fill in with stats
236  *
237  * Get the filesystem statistics.  As writes always target the upper layer
238  * filesystem pass the statfs to the upper filesystem (if it exists)
239  */
240 static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
241 {
242 	struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
243 	struct dentry *root_dentry = dentry->d_sb->s_root;
244 	struct path path;
245 	int err;
246 
247 	ovl_path_real(root_dentry, &path);
248 
249 	err = vfs_statfs(&path, buf);
250 	if (!err) {
251 		buf->f_namelen = ofs->namelen;
252 		buf->f_type = OVERLAYFS_SUPER_MAGIC;
253 	}
254 
255 	return err;
256 }
257 
258 static const struct super_operations ovl_super_operations = {
259 	.alloc_inode	= ovl_alloc_inode,
260 	.free_inode	= ovl_free_inode,
261 	.destroy_inode	= ovl_destroy_inode,
262 	.drop_inode	= generic_delete_inode,
263 	.put_super	= ovl_put_super,
264 	.sync_fs	= ovl_sync_fs,
265 	.statfs		= ovl_statfs,
266 	.show_options	= ovl_show_options,
267 };
268 
269 #define OVL_WORKDIR_NAME "work"
270 #define OVL_INDEXDIR_NAME "index"
271 
272 static struct dentry *ovl_workdir_create(struct ovl_fs *ofs,
273 					 const char *name, bool persist)
274 {
275 	struct inode *dir =  ofs->workbasedir->d_inode;
276 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
277 	struct dentry *work;
278 	int err;
279 	bool retried = false;
280 
281 	inode_lock_nested(dir, I_MUTEX_PARENT);
282 retry:
283 	work = ovl_lookup_upper(ofs, name, ofs->workbasedir, strlen(name));
284 
285 	if (!IS_ERR(work)) {
286 		struct iattr attr = {
287 			.ia_valid = ATTR_MODE,
288 			.ia_mode = S_IFDIR | 0,
289 		};
290 
291 		if (work->d_inode) {
292 			err = -EEXIST;
293 			if (retried)
294 				goto out_dput;
295 
296 			if (persist)
297 				goto out_unlock;
298 
299 			retried = true;
300 			err = ovl_workdir_cleanup(ofs, dir, mnt, work, 0);
301 			dput(work);
302 			if (err == -EINVAL) {
303 				work = ERR_PTR(err);
304 				goto out_unlock;
305 			}
306 			goto retry;
307 		}
308 
309 		err = ovl_mkdir_real(ofs, dir, &work, attr.ia_mode);
310 		if (err)
311 			goto out_dput;
312 
313 		/* Weird filesystem returning with hashed negative (kernfs)? */
314 		err = -EINVAL;
315 		if (d_really_is_negative(work))
316 			goto out_dput;
317 
318 		/*
319 		 * Try to remove POSIX ACL xattrs from workdir.  We are good if:
320 		 *
321 		 * a) success (there was a POSIX ACL xattr and was removed)
322 		 * b) -ENODATA (there was no POSIX ACL xattr)
323 		 * c) -EOPNOTSUPP (POSIX ACL xattrs are not supported)
324 		 *
325 		 * There are various other error values that could effectively
326 		 * mean that the xattr doesn't exist (e.g. -ERANGE is returned
327 		 * if the xattr name is too long), but the set of filesystems
328 		 * allowed as upper are limited to "normal" ones, where checking
329 		 * for the above two errors is sufficient.
330 		 */
331 		err = ovl_do_remove_acl(ofs, work, XATTR_NAME_POSIX_ACL_DEFAULT);
332 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
333 			goto out_dput;
334 
335 		err = ovl_do_remove_acl(ofs, work, XATTR_NAME_POSIX_ACL_ACCESS);
336 		if (err && err != -ENODATA && err != -EOPNOTSUPP)
337 			goto out_dput;
338 
339 		/* Clear any inherited mode bits */
340 		inode_lock(work->d_inode);
341 		err = ovl_do_notify_change(ofs, work, &attr);
342 		inode_unlock(work->d_inode);
343 		if (err)
344 			goto out_dput;
345 	} else {
346 		err = PTR_ERR(work);
347 		goto out_err;
348 	}
349 out_unlock:
350 	inode_unlock(dir);
351 	return work;
352 
353 out_dput:
354 	dput(work);
355 out_err:
356 	pr_warn("failed to create directory %s/%s (errno: %i); mounting read-only\n",
357 		ofs->config.workdir, name, -err);
358 	work = NULL;
359 	goto out_unlock;
360 }
361 
362 static int ovl_check_namelen(const struct path *path, struct ovl_fs *ofs,
363 			     const char *name)
364 {
365 	struct kstatfs statfs;
366 	int err = vfs_statfs(path, &statfs);
367 
368 	if (err)
369 		pr_err("statfs failed on '%s'\n", name);
370 	else
371 		ofs->namelen = max(ofs->namelen, statfs.f_namelen);
372 
373 	return err;
374 }
375 
376 static int ovl_lower_dir(const char *name, struct path *path,
377 			 struct ovl_fs *ofs, int *stack_depth)
378 {
379 	int fh_type;
380 	int err;
381 
382 	err = ovl_check_namelen(path, ofs, name);
383 	if (err)
384 		return err;
385 
386 	*stack_depth = max(*stack_depth, path->mnt->mnt_sb->s_stack_depth);
387 
388 	/*
389 	 * The inodes index feature and NFS export need to encode and decode
390 	 * file handles, so they require that all layers support them.
391 	 */
392 	fh_type = ovl_can_decode_fh(path->dentry->d_sb);
393 	if ((ofs->config.nfs_export ||
394 	     (ofs->config.index && ofs->config.upperdir)) && !fh_type) {
395 		ofs->config.index = false;
396 		ofs->config.nfs_export = false;
397 		pr_warn("fs on '%s' does not support file handles, falling back to index=off,nfs_export=off.\n",
398 			name);
399 	}
400 	/*
401 	 * Decoding origin file handle is required for persistent st_ino.
402 	 * Without persistent st_ino, xino=auto falls back to xino=off.
403 	 */
404 	if (ofs->config.xino == OVL_XINO_AUTO &&
405 	    ofs->config.upperdir && !fh_type) {
406 		ofs->config.xino = OVL_XINO_OFF;
407 		pr_warn("fs on '%s' does not support file handles, falling back to xino=off.\n",
408 			name);
409 	}
410 
411 	/* Check if lower fs has 32bit inode numbers */
412 	if (fh_type != FILEID_INO32_GEN)
413 		ofs->xino_mode = -1;
414 
415 	return 0;
416 }
417 
418 /* Workdir should not be subdir of upperdir and vice versa */
419 static bool ovl_workdir_ok(struct dentry *workdir, struct dentry *upperdir)
420 {
421 	bool ok = false;
422 
423 	if (workdir != upperdir) {
424 		ok = (lock_rename(workdir, upperdir) == NULL);
425 		unlock_rename(workdir, upperdir);
426 	}
427 	return ok;
428 }
429 
430 static int ovl_own_xattr_get(const struct xattr_handler *handler,
431 			     struct dentry *dentry, struct inode *inode,
432 			     const char *name, void *buffer, size_t size)
433 {
434 	return -EOPNOTSUPP;
435 }
436 
437 static int ovl_own_xattr_set(const struct xattr_handler *handler,
438 			     struct mnt_idmap *idmap,
439 			     struct dentry *dentry, struct inode *inode,
440 			     const char *name, const void *value,
441 			     size_t size, int flags)
442 {
443 	return -EOPNOTSUPP;
444 }
445 
446 static int ovl_other_xattr_get(const struct xattr_handler *handler,
447 			       struct dentry *dentry, struct inode *inode,
448 			       const char *name, void *buffer, size_t size)
449 {
450 	return ovl_xattr_get(dentry, inode, name, buffer, size);
451 }
452 
453 static int ovl_other_xattr_set(const struct xattr_handler *handler,
454 			       struct mnt_idmap *idmap,
455 			       struct dentry *dentry, struct inode *inode,
456 			       const char *name, const void *value,
457 			       size_t size, int flags)
458 {
459 	return ovl_xattr_set(dentry, inode, name, value, size, flags);
460 }
461 
462 static const struct xattr_handler ovl_own_trusted_xattr_handler = {
463 	.prefix	= OVL_XATTR_TRUSTED_PREFIX,
464 	.get = ovl_own_xattr_get,
465 	.set = ovl_own_xattr_set,
466 };
467 
468 static const struct xattr_handler ovl_own_user_xattr_handler = {
469 	.prefix	= OVL_XATTR_USER_PREFIX,
470 	.get = ovl_own_xattr_get,
471 	.set = ovl_own_xattr_set,
472 };
473 
474 static const struct xattr_handler ovl_other_xattr_handler = {
475 	.prefix	= "", /* catch all */
476 	.get = ovl_other_xattr_get,
477 	.set = ovl_other_xattr_set,
478 };
479 
480 static const struct xattr_handler *ovl_trusted_xattr_handlers[] = {
481 	&ovl_own_trusted_xattr_handler,
482 	&ovl_other_xattr_handler,
483 	NULL
484 };
485 
486 static const struct xattr_handler *ovl_user_xattr_handlers[] = {
487 	&ovl_own_user_xattr_handler,
488 	&ovl_other_xattr_handler,
489 	NULL
490 };
491 
492 static int ovl_setup_trap(struct super_block *sb, struct dentry *dir,
493 			  struct inode **ptrap, const char *name)
494 {
495 	struct inode *trap;
496 	int err;
497 
498 	trap = ovl_get_trap_inode(sb, dir);
499 	err = PTR_ERR_OR_ZERO(trap);
500 	if (err) {
501 		if (err == -ELOOP)
502 			pr_err("conflicting %s path\n", name);
503 		return err;
504 	}
505 
506 	*ptrap = trap;
507 	return 0;
508 }
509 
510 /*
511  * Determine how we treat concurrent use of upperdir/workdir based on the
512  * index feature. This is papering over mount leaks of container runtimes,
513  * for example, an old overlay mount is leaked and now its upperdir is
514  * attempted to be used as a lower layer in a new overlay mount.
515  */
516 static int ovl_report_in_use(struct ovl_fs *ofs, const char *name)
517 {
518 	if (ofs->config.index) {
519 		pr_err("%s is in-use as upperdir/workdir of another mount, mount with '-o index=off' to override exclusive upperdir protection.\n",
520 		       name);
521 		return -EBUSY;
522 	} else {
523 		pr_warn("%s is in-use as upperdir/workdir of another mount, accessing files from both mounts will result in undefined behavior.\n",
524 			name);
525 		return 0;
526 	}
527 }
528 
529 static int ovl_get_upper(struct super_block *sb, struct ovl_fs *ofs,
530 			 struct ovl_layer *upper_layer,
531 			 const struct path *upperpath)
532 {
533 	struct vfsmount *upper_mnt;
534 	int err;
535 
536 	/* Upperdir path should not be r/o */
537 	if (__mnt_is_readonly(upperpath->mnt)) {
538 		pr_err("upper fs is r/o, try multi-lower layers mount\n");
539 		err = -EINVAL;
540 		goto out;
541 	}
542 
543 	err = ovl_check_namelen(upperpath, ofs, ofs->config.upperdir);
544 	if (err)
545 		goto out;
546 
547 	err = ovl_setup_trap(sb, upperpath->dentry, &upper_layer->trap,
548 			     "upperdir");
549 	if (err)
550 		goto out;
551 
552 	upper_mnt = clone_private_mount(upperpath);
553 	err = PTR_ERR(upper_mnt);
554 	if (IS_ERR(upper_mnt)) {
555 		pr_err("failed to clone upperpath\n");
556 		goto out;
557 	}
558 
559 	/* Don't inherit atime flags */
560 	upper_mnt->mnt_flags &= ~(MNT_NOATIME | MNT_NODIRATIME | MNT_RELATIME);
561 	upper_layer->mnt = upper_mnt;
562 	upper_layer->idx = 0;
563 	upper_layer->fsid = 0;
564 
565 	err = -ENOMEM;
566 	upper_layer->name = kstrdup(ofs->config.upperdir, GFP_KERNEL);
567 	if (!upper_layer->name)
568 		goto out;
569 
570 	/*
571 	 * Inherit SB_NOSEC flag from upperdir.
572 	 *
573 	 * This optimization changes behavior when a security related attribute
574 	 * (suid/sgid/security.*) is changed on an underlying layer.  This is
575 	 * okay because we don't yet have guarantees in that case, but it will
576 	 * need careful treatment once we want to honour changes to underlying
577 	 * filesystems.
578 	 */
579 	if (upper_mnt->mnt_sb->s_flags & SB_NOSEC)
580 		sb->s_flags |= SB_NOSEC;
581 
582 	if (ovl_inuse_trylock(ovl_upper_mnt(ofs)->mnt_root)) {
583 		ofs->upperdir_locked = true;
584 	} else {
585 		err = ovl_report_in_use(ofs, "upperdir");
586 		if (err)
587 			goto out;
588 	}
589 
590 	err = 0;
591 out:
592 	return err;
593 }
594 
595 /*
596  * Returns 1 if RENAME_WHITEOUT is supported, 0 if not supported and
597  * negative values if error is encountered.
598  */
599 static int ovl_check_rename_whiteout(struct ovl_fs *ofs)
600 {
601 	struct dentry *workdir = ofs->workdir;
602 	struct inode *dir = d_inode(workdir);
603 	struct dentry *temp;
604 	struct dentry *dest;
605 	struct dentry *whiteout;
606 	struct name_snapshot name;
607 	int err;
608 
609 	inode_lock_nested(dir, I_MUTEX_PARENT);
610 
611 	temp = ovl_create_temp(ofs, workdir, OVL_CATTR(S_IFREG | 0));
612 	err = PTR_ERR(temp);
613 	if (IS_ERR(temp))
614 		goto out_unlock;
615 
616 	dest = ovl_lookup_temp(ofs, workdir);
617 	err = PTR_ERR(dest);
618 	if (IS_ERR(dest)) {
619 		dput(temp);
620 		goto out_unlock;
621 	}
622 
623 	/* Name is inline and stable - using snapshot as a copy helper */
624 	take_dentry_name_snapshot(&name, temp);
625 	err = ovl_do_rename(ofs, dir, temp, dir, dest, RENAME_WHITEOUT);
626 	if (err) {
627 		if (err == -EINVAL)
628 			err = 0;
629 		goto cleanup_temp;
630 	}
631 
632 	whiteout = ovl_lookup_upper(ofs, name.name.name, workdir, name.name.len);
633 	err = PTR_ERR(whiteout);
634 	if (IS_ERR(whiteout))
635 		goto cleanup_temp;
636 
637 	err = ovl_is_whiteout(whiteout);
638 
639 	/* Best effort cleanup of whiteout and temp file */
640 	if (err)
641 		ovl_cleanup(ofs, dir, whiteout);
642 	dput(whiteout);
643 
644 cleanup_temp:
645 	ovl_cleanup(ofs, dir, temp);
646 	release_dentry_name_snapshot(&name);
647 	dput(temp);
648 	dput(dest);
649 
650 out_unlock:
651 	inode_unlock(dir);
652 
653 	return err;
654 }
655 
656 static struct dentry *ovl_lookup_or_create(struct ovl_fs *ofs,
657 					   struct dentry *parent,
658 					   const char *name, umode_t mode)
659 {
660 	size_t len = strlen(name);
661 	struct dentry *child;
662 
663 	inode_lock_nested(parent->d_inode, I_MUTEX_PARENT);
664 	child = ovl_lookup_upper(ofs, name, parent, len);
665 	if (!IS_ERR(child) && !child->d_inode)
666 		child = ovl_create_real(ofs, parent->d_inode, child,
667 					OVL_CATTR(mode));
668 	inode_unlock(parent->d_inode);
669 	dput(parent);
670 
671 	return child;
672 }
673 
674 /*
675  * Creates $workdir/work/incompat/volatile/dirty file if it is not already
676  * present.
677  */
678 static int ovl_create_volatile_dirty(struct ovl_fs *ofs)
679 {
680 	unsigned int ctr;
681 	struct dentry *d = dget(ofs->workbasedir);
682 	static const char *const volatile_path[] = {
683 		OVL_WORKDIR_NAME, "incompat", "volatile", "dirty"
684 	};
685 	const char *const *name = volatile_path;
686 
687 	for (ctr = ARRAY_SIZE(volatile_path); ctr; ctr--, name++) {
688 		d = ovl_lookup_or_create(ofs, d, *name, ctr > 1 ? S_IFDIR : S_IFREG);
689 		if (IS_ERR(d))
690 			return PTR_ERR(d);
691 	}
692 	dput(d);
693 	return 0;
694 }
695 
696 static int ovl_make_workdir(struct super_block *sb, struct ovl_fs *ofs,
697 			    const struct path *workpath)
698 {
699 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
700 	struct dentry *workdir;
701 	struct file *tmpfile;
702 	bool rename_whiteout;
703 	bool d_type;
704 	int fh_type;
705 	int err;
706 
707 	err = mnt_want_write(mnt);
708 	if (err)
709 		return err;
710 
711 	workdir = ovl_workdir_create(ofs, OVL_WORKDIR_NAME, false);
712 	err = PTR_ERR(workdir);
713 	if (IS_ERR_OR_NULL(workdir))
714 		goto out;
715 
716 	ofs->workdir = workdir;
717 
718 	err = ovl_setup_trap(sb, ofs->workdir, &ofs->workdir_trap, "workdir");
719 	if (err)
720 		goto out;
721 
722 	/*
723 	 * Upper should support d_type, else whiteouts are visible.  Given
724 	 * workdir and upper are on same fs, we can do iterate_dir() on
725 	 * workdir. This check requires successful creation of workdir in
726 	 * previous step.
727 	 */
728 	err = ovl_check_d_type_supported(workpath);
729 	if (err < 0)
730 		goto out;
731 
732 	d_type = err;
733 	if (!d_type)
734 		pr_warn("upper fs needs to support d_type.\n");
735 
736 	/* Check if upper/work fs supports O_TMPFILE */
737 	tmpfile = ovl_do_tmpfile(ofs, ofs->workdir, S_IFREG | 0);
738 	ofs->tmpfile = !IS_ERR(tmpfile);
739 	if (ofs->tmpfile)
740 		fput(tmpfile);
741 	else
742 		pr_warn("upper fs does not support tmpfile.\n");
743 
744 
745 	/* Check if upper/work fs supports RENAME_WHITEOUT */
746 	err = ovl_check_rename_whiteout(ofs);
747 	if (err < 0)
748 		goto out;
749 
750 	rename_whiteout = err;
751 	if (!rename_whiteout)
752 		pr_warn("upper fs does not support RENAME_WHITEOUT.\n");
753 
754 	/*
755 	 * Check if upper/work fs supports (trusted|user).overlay.* xattr
756 	 */
757 	err = ovl_setxattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE, "0", 1);
758 	if (err) {
759 		pr_warn("failed to set xattr on upper\n");
760 		ofs->noxattr = true;
761 		if (ovl_redirect_follow(ofs)) {
762 			ofs->config.redirect_mode = OVL_REDIRECT_NOFOLLOW;
763 			pr_warn("...falling back to redirect_dir=nofollow.\n");
764 		}
765 		if (ofs->config.metacopy) {
766 			ofs->config.metacopy = false;
767 			pr_warn("...falling back to metacopy=off.\n");
768 		}
769 		if (ofs->config.index) {
770 			ofs->config.index = false;
771 			pr_warn("...falling back to index=off.\n");
772 		}
773 		/*
774 		 * xattr support is required for persistent st_ino.
775 		 * Without persistent st_ino, xino=auto falls back to xino=off.
776 		 */
777 		if (ofs->config.xino == OVL_XINO_AUTO) {
778 			ofs->config.xino = OVL_XINO_OFF;
779 			pr_warn("...falling back to xino=off.\n");
780 		}
781 		if (err == -EPERM && !ofs->config.userxattr)
782 			pr_info("try mounting with 'userxattr' option\n");
783 		err = 0;
784 	} else {
785 		ovl_removexattr(ofs, ofs->workdir, OVL_XATTR_OPAQUE);
786 	}
787 
788 	/*
789 	 * We allowed sub-optimal upper fs configuration and don't want to break
790 	 * users over kernel upgrade, but we never allowed remote upper fs, so
791 	 * we can enforce strict requirements for remote upper fs.
792 	 */
793 	if (ovl_dentry_remote(ofs->workdir) &&
794 	    (!d_type || !rename_whiteout || ofs->noxattr)) {
795 		pr_err("upper fs missing required features.\n");
796 		err = -EINVAL;
797 		goto out;
798 	}
799 
800 	/*
801 	 * For volatile mount, create a incompat/volatile/dirty file to keep
802 	 * track of it.
803 	 */
804 	if (ofs->config.ovl_volatile) {
805 		err = ovl_create_volatile_dirty(ofs);
806 		if (err < 0) {
807 			pr_err("Failed to create volatile/dirty file.\n");
808 			goto out;
809 		}
810 	}
811 
812 	/* Check if upper/work fs supports file handles */
813 	fh_type = ovl_can_decode_fh(ofs->workdir->d_sb);
814 	if (ofs->config.index && !fh_type) {
815 		ofs->config.index = false;
816 		pr_warn("upper fs does not support file handles, falling back to index=off.\n");
817 	}
818 
819 	/* Check if upper fs has 32bit inode numbers */
820 	if (fh_type != FILEID_INO32_GEN)
821 		ofs->xino_mode = -1;
822 
823 	/* NFS export of r/w mount depends on index */
824 	if (ofs->config.nfs_export && !ofs->config.index) {
825 		pr_warn("NFS export requires \"index=on\", falling back to nfs_export=off.\n");
826 		ofs->config.nfs_export = false;
827 	}
828 out:
829 	mnt_drop_write(mnt);
830 	return err;
831 }
832 
833 static int ovl_get_workdir(struct super_block *sb, struct ovl_fs *ofs,
834 			   const struct path *upperpath,
835 			   const struct path *workpath)
836 {
837 	int err;
838 
839 	err = -EINVAL;
840 	if (upperpath->mnt != workpath->mnt) {
841 		pr_err("workdir and upperdir must reside under the same mount\n");
842 		return err;
843 	}
844 	if (!ovl_workdir_ok(workpath->dentry, upperpath->dentry)) {
845 		pr_err("workdir and upperdir must be separate subtrees\n");
846 		return err;
847 	}
848 
849 	ofs->workbasedir = dget(workpath->dentry);
850 
851 	if (ovl_inuse_trylock(ofs->workbasedir)) {
852 		ofs->workdir_locked = true;
853 	} else {
854 		err = ovl_report_in_use(ofs, "workdir");
855 		if (err)
856 			return err;
857 	}
858 
859 	err = ovl_setup_trap(sb, ofs->workbasedir, &ofs->workbasedir_trap,
860 			     "workdir");
861 	if (err)
862 		return err;
863 
864 	return ovl_make_workdir(sb, ofs, workpath);
865 }
866 
867 static int ovl_get_indexdir(struct super_block *sb, struct ovl_fs *ofs,
868 			    struct ovl_entry *oe, const struct path *upperpath)
869 {
870 	struct vfsmount *mnt = ovl_upper_mnt(ofs);
871 	struct dentry *indexdir;
872 	int err;
873 
874 	err = mnt_want_write(mnt);
875 	if (err)
876 		return err;
877 
878 	/* Verify lower root is upper root origin */
879 	err = ovl_verify_origin(ofs, upperpath->dentry,
880 				ovl_lowerstack(oe)->dentry, true);
881 	if (err) {
882 		pr_err("failed to verify upper root origin\n");
883 		goto out;
884 	}
885 
886 	/* index dir will act also as workdir */
887 	iput(ofs->workdir_trap);
888 	ofs->workdir_trap = NULL;
889 	dput(ofs->workdir);
890 	ofs->workdir = NULL;
891 	indexdir = ovl_workdir_create(ofs, OVL_INDEXDIR_NAME, true);
892 	if (IS_ERR(indexdir)) {
893 		err = PTR_ERR(indexdir);
894 	} else if (indexdir) {
895 		ofs->indexdir = indexdir;
896 		ofs->workdir = dget(indexdir);
897 
898 		err = ovl_setup_trap(sb, ofs->indexdir, &ofs->indexdir_trap,
899 				     "indexdir");
900 		if (err)
901 			goto out;
902 
903 		/*
904 		 * Verify upper root is exclusively associated with index dir.
905 		 * Older kernels stored upper fh in ".overlay.origin"
906 		 * xattr. If that xattr exists, verify that it is a match to
907 		 * upper dir file handle. In any case, verify or set xattr
908 		 * ".overlay.upper" to indicate that index may have
909 		 * directory entries.
910 		 */
911 		if (ovl_check_origin_xattr(ofs, ofs->indexdir)) {
912 			err = ovl_verify_set_fh(ofs, ofs->indexdir,
913 						OVL_XATTR_ORIGIN,
914 						upperpath->dentry, true, false);
915 			if (err)
916 				pr_err("failed to verify index dir 'origin' xattr\n");
917 		}
918 		err = ovl_verify_upper(ofs, ofs->indexdir, upperpath->dentry,
919 				       true);
920 		if (err)
921 			pr_err("failed to verify index dir 'upper' xattr\n");
922 
923 		/* Cleanup bad/stale/orphan index entries */
924 		if (!err)
925 			err = ovl_indexdir_cleanup(ofs);
926 	}
927 	if (err || !ofs->indexdir)
928 		pr_warn("try deleting index dir or mounting with '-o index=off' to disable inodes index.\n");
929 
930 out:
931 	mnt_drop_write(mnt);
932 	return err;
933 }
934 
935 static bool ovl_lower_uuid_ok(struct ovl_fs *ofs, const uuid_t *uuid)
936 {
937 	unsigned int i;
938 
939 	if (!ofs->config.nfs_export && !ovl_upper_mnt(ofs))
940 		return true;
941 
942 	/*
943 	 * We allow using single lower with null uuid for index and nfs_export
944 	 * for example to support those features with single lower squashfs.
945 	 * To avoid regressions in setups of overlay with re-formatted lower
946 	 * squashfs, do not allow decoding origin with lower null uuid unless
947 	 * user opted-in to one of the new features that require following the
948 	 * lower inode of non-dir upper.
949 	 */
950 	if (ovl_allow_offline_changes(ofs) && uuid_is_null(uuid))
951 		return false;
952 
953 	for (i = 0; i < ofs->numfs; i++) {
954 		/*
955 		 * We use uuid to associate an overlay lower file handle with a
956 		 * lower layer, so we can accept lower fs with null uuid as long
957 		 * as all lower layers with null uuid are on the same fs.
958 		 * if we detect multiple lower fs with the same uuid, we
959 		 * disable lower file handle decoding on all of them.
960 		 */
961 		if (ofs->fs[i].is_lower &&
962 		    uuid_equal(&ofs->fs[i].sb->s_uuid, uuid)) {
963 			ofs->fs[i].bad_uuid = true;
964 			return false;
965 		}
966 	}
967 	return true;
968 }
969 
970 /* Get a unique fsid for the layer */
971 static int ovl_get_fsid(struct ovl_fs *ofs, const struct path *path)
972 {
973 	struct super_block *sb = path->mnt->mnt_sb;
974 	unsigned int i;
975 	dev_t dev;
976 	int err;
977 	bool bad_uuid = false;
978 	bool warn = false;
979 
980 	for (i = 0; i < ofs->numfs; i++) {
981 		if (ofs->fs[i].sb == sb)
982 			return i;
983 	}
984 
985 	if (!ovl_lower_uuid_ok(ofs, &sb->s_uuid)) {
986 		bad_uuid = true;
987 		if (ofs->config.xino == OVL_XINO_AUTO) {
988 			ofs->config.xino = OVL_XINO_OFF;
989 			warn = true;
990 		}
991 		if (ofs->config.index || ofs->config.nfs_export) {
992 			ofs->config.index = false;
993 			ofs->config.nfs_export = false;
994 			warn = true;
995 		}
996 		if (warn) {
997 			pr_warn("%s uuid detected in lower fs '%pd2', falling back to xino=%s,index=off,nfs_export=off.\n",
998 				uuid_is_null(&sb->s_uuid) ? "null" :
999 							    "conflicting",
1000 				path->dentry, ovl_xino_mode(&ofs->config));
1001 		}
1002 	}
1003 
1004 	err = get_anon_bdev(&dev);
1005 	if (err) {
1006 		pr_err("failed to get anonymous bdev for lowerpath\n");
1007 		return err;
1008 	}
1009 
1010 	ofs->fs[ofs->numfs].sb = sb;
1011 	ofs->fs[ofs->numfs].pseudo_dev = dev;
1012 	ofs->fs[ofs->numfs].bad_uuid = bad_uuid;
1013 
1014 	return ofs->numfs++;
1015 }
1016 
1017 /*
1018  * The fsid after the last lower fsid is used for the data layers.
1019  * It is a "null fs" with a null sb, null uuid, and no pseudo dev.
1020  */
1021 static int ovl_get_data_fsid(struct ovl_fs *ofs)
1022 {
1023 	return ofs->numfs;
1024 }
1025 
1026 
1027 static int ovl_get_layers(struct super_block *sb, struct ovl_fs *ofs,
1028 			  struct ovl_fs_context *ctx, struct ovl_layer *layers)
1029 {
1030 	int err;
1031 	unsigned int i;
1032 	size_t nr_merged_lower;
1033 
1034 	ofs->fs = kcalloc(ctx->nr + 2, sizeof(struct ovl_sb), GFP_KERNEL);
1035 	if (ofs->fs == NULL)
1036 		return -ENOMEM;
1037 
1038 	/*
1039 	 * idx/fsid 0 are reserved for upper fs even with lower only overlay
1040 	 * and the last fsid is reserved for "null fs" of the data layers.
1041 	 */
1042 	ofs->numfs++;
1043 
1044 	/*
1045 	 * All lower layers that share the same fs as upper layer, use the same
1046 	 * pseudo_dev as upper layer.  Allocate fs[0].pseudo_dev even for lower
1047 	 * only overlay to simplify ovl_fs_free().
1048 	 * is_lower will be set if upper fs is shared with a lower layer.
1049 	 */
1050 	err = get_anon_bdev(&ofs->fs[0].pseudo_dev);
1051 	if (err) {
1052 		pr_err("failed to get anonymous bdev for upper fs\n");
1053 		return err;
1054 	}
1055 
1056 	if (ovl_upper_mnt(ofs)) {
1057 		ofs->fs[0].sb = ovl_upper_mnt(ofs)->mnt_sb;
1058 		ofs->fs[0].is_lower = false;
1059 	}
1060 
1061 	nr_merged_lower = ctx->nr - ctx->nr_data;
1062 	for (i = 0; i < ctx->nr; i++) {
1063 		struct ovl_fs_context_layer *l = &ctx->lower[i];
1064 		struct vfsmount *mnt;
1065 		struct inode *trap;
1066 		int fsid;
1067 
1068 		if (i < nr_merged_lower)
1069 			fsid = ovl_get_fsid(ofs, &l->path);
1070 		else
1071 			fsid = ovl_get_data_fsid(ofs);
1072 		if (fsid < 0)
1073 			return fsid;
1074 
1075 		/*
1076 		 * Check if lower root conflicts with this overlay layers before
1077 		 * checking if it is in-use as upperdir/workdir of "another"
1078 		 * mount, because we do not bother to check in ovl_is_inuse() if
1079 		 * the upperdir/workdir is in fact in-use by our
1080 		 * upperdir/workdir.
1081 		 */
1082 		err = ovl_setup_trap(sb, l->path.dentry, &trap, "lowerdir");
1083 		if (err)
1084 			return err;
1085 
1086 		if (ovl_is_inuse(l->path.dentry)) {
1087 			err = ovl_report_in_use(ofs, "lowerdir");
1088 			if (err) {
1089 				iput(trap);
1090 				return err;
1091 			}
1092 		}
1093 
1094 		mnt = clone_private_mount(&l->path);
1095 		err = PTR_ERR(mnt);
1096 		if (IS_ERR(mnt)) {
1097 			pr_err("failed to clone lowerpath\n");
1098 			iput(trap);
1099 			return err;
1100 		}
1101 
1102 		/*
1103 		 * Make lower layers R/O.  That way fchmod/fchown on lower file
1104 		 * will fail instead of modifying lower fs.
1105 		 */
1106 		mnt->mnt_flags |= MNT_READONLY | MNT_NOATIME;
1107 
1108 		layers[ofs->numlayer].trap = trap;
1109 		layers[ofs->numlayer].mnt = mnt;
1110 		layers[ofs->numlayer].idx = ofs->numlayer;
1111 		layers[ofs->numlayer].fsid = fsid;
1112 		layers[ofs->numlayer].fs = &ofs->fs[fsid];
1113 		layers[ofs->numlayer].name = l->name;
1114 		l->name = NULL;
1115 		ofs->numlayer++;
1116 		ofs->fs[fsid].is_lower = true;
1117 	}
1118 
1119 	/*
1120 	 * When all layers on same fs, overlay can use real inode numbers.
1121 	 * With mount option "xino=<on|auto>", mounter declares that there are
1122 	 * enough free high bits in underlying fs to hold the unique fsid.
1123 	 * If overlayfs does encounter underlying inodes using the high xino
1124 	 * bits reserved for fsid, it emits a warning and uses the original
1125 	 * inode number or a non persistent inode number allocated from a
1126 	 * dedicated range.
1127 	 */
1128 	if (ofs->numfs - !ovl_upper_mnt(ofs) == 1) {
1129 		if (ofs->config.xino == OVL_XINO_ON)
1130 			pr_info("\"xino=on\" is useless with all layers on same fs, ignore.\n");
1131 		ofs->xino_mode = 0;
1132 	} else if (ofs->config.xino == OVL_XINO_OFF) {
1133 		ofs->xino_mode = -1;
1134 	} else if (ofs->xino_mode < 0) {
1135 		/*
1136 		 * This is a roundup of number of bits needed for encoding
1137 		 * fsid, where fsid 0 is reserved for upper fs (even with
1138 		 * lower only overlay) +1 extra bit is reserved for the non
1139 		 * persistent inode number range that is used for resolving
1140 		 * xino lower bits overflow.
1141 		 */
1142 		BUILD_BUG_ON(ilog2(OVL_MAX_STACK) > 30);
1143 		ofs->xino_mode = ilog2(ofs->numfs - 1) + 2;
1144 	}
1145 
1146 	if (ofs->xino_mode > 0) {
1147 		pr_info("\"xino\" feature enabled using %d upper inode bits.\n",
1148 			ofs->xino_mode);
1149 	}
1150 
1151 	return 0;
1152 }
1153 
1154 static struct ovl_entry *ovl_get_lowerstack(struct super_block *sb,
1155 					    struct ovl_fs_context *ctx,
1156 					    struct ovl_fs *ofs,
1157 					    struct ovl_layer *layers)
1158 {
1159 	int err;
1160 	unsigned int i;
1161 	size_t nr_merged_lower;
1162 	struct ovl_entry *oe;
1163 	struct ovl_path *lowerstack;
1164 
1165 	struct ovl_fs_context_layer *l;
1166 
1167 	if (!ofs->config.upperdir && ctx->nr == 1) {
1168 		pr_err("at least 2 lowerdir are needed while upperdir nonexistent\n");
1169 		return ERR_PTR(-EINVAL);
1170 	}
1171 
1172 	err = -EINVAL;
1173 	for (i = 0; i < ctx->nr; i++) {
1174 		l = &ctx->lower[i];
1175 
1176 		err = ovl_lower_dir(l->name, &l->path, ofs, &sb->s_stack_depth);
1177 		if (err)
1178 			return ERR_PTR(err);
1179 	}
1180 
1181 	err = -EINVAL;
1182 	sb->s_stack_depth++;
1183 	if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
1184 		pr_err("maximum fs stacking depth exceeded\n");
1185 		return ERR_PTR(err);
1186 	}
1187 
1188 	err = ovl_get_layers(sb, ofs, ctx, layers);
1189 	if (err)
1190 		return ERR_PTR(err);
1191 
1192 	err = -ENOMEM;
1193 	/* Data-only layers are not merged in root directory */
1194 	nr_merged_lower = ctx->nr - ctx->nr_data;
1195 	oe = ovl_alloc_entry(nr_merged_lower);
1196 	if (!oe)
1197 		return ERR_PTR(err);
1198 
1199 	lowerstack = ovl_lowerstack(oe);
1200 	for (i = 0; i < nr_merged_lower; i++) {
1201 		l = &ctx->lower[i];
1202 		lowerstack[i].dentry = dget(l->path.dentry);
1203 		lowerstack[i].layer = &ofs->layers[i + 1];
1204 	}
1205 	ofs->numdatalayer = ctx->nr_data;
1206 
1207 	return oe;
1208 }
1209 
1210 /*
1211  * Check if this layer root is a descendant of:
1212  * - another layer of this overlayfs instance
1213  * - upper/work dir of any overlayfs instance
1214  */
1215 static int ovl_check_layer(struct super_block *sb, struct ovl_fs *ofs,
1216 			   struct dentry *dentry, const char *name,
1217 			   bool is_lower)
1218 {
1219 	struct dentry *next = dentry, *parent;
1220 	int err = 0;
1221 
1222 	if (!dentry)
1223 		return 0;
1224 
1225 	parent = dget_parent(next);
1226 
1227 	/* Walk back ancestors to root (inclusive) looking for traps */
1228 	while (!err && parent != next) {
1229 		if (is_lower && ovl_lookup_trap_inode(sb, parent)) {
1230 			err = -ELOOP;
1231 			pr_err("overlapping %s path\n", name);
1232 		} else if (ovl_is_inuse(parent)) {
1233 			err = ovl_report_in_use(ofs, name);
1234 		}
1235 		next = parent;
1236 		parent = dget_parent(next);
1237 		dput(next);
1238 	}
1239 
1240 	dput(parent);
1241 
1242 	return err;
1243 }
1244 
1245 /*
1246  * Check if any of the layers or work dirs overlap.
1247  */
1248 static int ovl_check_overlapping_layers(struct super_block *sb,
1249 					struct ovl_fs *ofs)
1250 {
1251 	int i, err;
1252 
1253 	if (ovl_upper_mnt(ofs)) {
1254 		err = ovl_check_layer(sb, ofs, ovl_upper_mnt(ofs)->mnt_root,
1255 				      "upperdir", false);
1256 		if (err)
1257 			return err;
1258 
1259 		/*
1260 		 * Checking workbasedir avoids hitting ovl_is_inuse(parent) of
1261 		 * this instance and covers overlapping work and index dirs,
1262 		 * unless work or index dir have been moved since created inside
1263 		 * workbasedir.  In that case, we already have their traps in
1264 		 * inode cache and we will catch that case on lookup.
1265 		 */
1266 		err = ovl_check_layer(sb, ofs, ofs->workbasedir, "workdir",
1267 				      false);
1268 		if (err)
1269 			return err;
1270 	}
1271 
1272 	for (i = 1; i < ofs->numlayer; i++) {
1273 		err = ovl_check_layer(sb, ofs,
1274 				      ofs->layers[i].mnt->mnt_root,
1275 				      "lowerdir", true);
1276 		if (err)
1277 			return err;
1278 	}
1279 
1280 	return 0;
1281 }
1282 
1283 static struct dentry *ovl_get_root(struct super_block *sb,
1284 				   struct dentry *upperdentry,
1285 				   struct ovl_entry *oe)
1286 {
1287 	struct dentry *root;
1288 	struct ovl_path *lowerpath = ovl_lowerstack(oe);
1289 	unsigned long ino = d_inode(lowerpath->dentry)->i_ino;
1290 	int fsid = lowerpath->layer->fsid;
1291 	struct ovl_inode_params oip = {
1292 		.upperdentry = upperdentry,
1293 		.oe = oe,
1294 	};
1295 
1296 	root = d_make_root(ovl_new_inode(sb, S_IFDIR, 0));
1297 	if (!root)
1298 		return NULL;
1299 
1300 	if (upperdentry) {
1301 		/* Root inode uses upper st_ino/i_ino */
1302 		ino = d_inode(upperdentry)->i_ino;
1303 		fsid = 0;
1304 		ovl_dentry_set_upper_alias(root);
1305 		if (ovl_is_impuredir(sb, upperdentry))
1306 			ovl_set_flag(OVL_IMPURE, d_inode(root));
1307 	}
1308 
1309 	/* Root is always merge -> can have whiteouts */
1310 	ovl_set_flag(OVL_WHITEOUTS, d_inode(root));
1311 	ovl_dentry_set_flag(OVL_E_CONNECTED, root);
1312 	ovl_set_upperdata(d_inode(root));
1313 	ovl_inode_init(d_inode(root), &oip, ino, fsid);
1314 	ovl_dentry_init_flags(root, upperdentry, oe, DCACHE_OP_WEAK_REVALIDATE);
1315 	/* root keeps a reference of upperdentry */
1316 	dget(upperdentry);
1317 
1318 	return root;
1319 }
1320 
1321 int ovl_fill_super(struct super_block *sb, struct fs_context *fc)
1322 {
1323 	struct ovl_fs *ofs = sb->s_fs_info;
1324 	struct ovl_fs_context *ctx = fc->fs_private;
1325 	struct dentry *root_dentry;
1326 	struct ovl_entry *oe;
1327 	struct ovl_layer *layers;
1328 	struct cred *cred;
1329 	int err;
1330 
1331 	err = -EIO;
1332 	if (WARN_ON(fc->user_ns != current_user_ns()))
1333 		goto out_err;
1334 
1335 	sb->s_d_op = &ovl_dentry_operations;
1336 
1337 	err = -ENOMEM;
1338 	ofs->creator_cred = cred = prepare_creds();
1339 	if (!cred)
1340 		goto out_err;
1341 
1342 	err = ovl_fs_params_verify(ctx, &ofs->config);
1343 	if (err)
1344 		goto out_err;
1345 
1346 	err = -EINVAL;
1347 	if (ctx->nr == 0) {
1348 		if (!(fc->sb_flags & SB_SILENT))
1349 			pr_err("missing 'lowerdir'\n");
1350 		goto out_err;
1351 	}
1352 
1353 	err = -ENOMEM;
1354 	layers = kcalloc(ctx->nr + 1, sizeof(struct ovl_layer), GFP_KERNEL);
1355 	if (!layers)
1356 		goto out_err;
1357 
1358 	ofs->layers = layers;
1359 	/* Layer 0 is reserved for upper even if there's no upper */
1360 	ofs->numlayer = 1;
1361 
1362 	sb->s_stack_depth = 0;
1363 	sb->s_maxbytes = MAX_LFS_FILESIZE;
1364 	atomic_long_set(&ofs->last_ino, 1);
1365 	/* Assume underlying fs uses 32bit inodes unless proven otherwise */
1366 	if (ofs->config.xino != OVL_XINO_OFF) {
1367 		ofs->xino_mode = BITS_PER_LONG - 32;
1368 		if (!ofs->xino_mode) {
1369 			pr_warn("xino not supported on 32bit kernel, falling back to xino=off.\n");
1370 			ofs->config.xino = OVL_XINO_OFF;
1371 		}
1372 	}
1373 
1374 	/* alloc/destroy_inode needed for setting up traps in inode cache */
1375 	sb->s_op = &ovl_super_operations;
1376 
1377 	if (ofs->config.upperdir) {
1378 		struct super_block *upper_sb;
1379 
1380 		err = -EINVAL;
1381 		if (!ofs->config.workdir) {
1382 			pr_err("missing 'workdir'\n");
1383 			goto out_err;
1384 		}
1385 
1386 		err = ovl_get_upper(sb, ofs, &layers[0], &ctx->upper);
1387 		if (err)
1388 			goto out_err;
1389 
1390 		upper_sb = ovl_upper_mnt(ofs)->mnt_sb;
1391 		if (!ovl_should_sync(ofs)) {
1392 			ofs->errseq = errseq_sample(&upper_sb->s_wb_err);
1393 			if (errseq_check(&upper_sb->s_wb_err, ofs->errseq)) {
1394 				err = -EIO;
1395 				pr_err("Cannot mount volatile when upperdir has an unseen error. Sync upperdir fs to clear state.\n");
1396 				goto out_err;
1397 			}
1398 		}
1399 
1400 		err = ovl_get_workdir(sb, ofs, &ctx->upper, &ctx->work);
1401 		if (err)
1402 			goto out_err;
1403 
1404 		if (!ofs->workdir)
1405 			sb->s_flags |= SB_RDONLY;
1406 
1407 		sb->s_stack_depth = upper_sb->s_stack_depth;
1408 		sb->s_time_gran = upper_sb->s_time_gran;
1409 	}
1410 	oe = ovl_get_lowerstack(sb, ctx, ofs, layers);
1411 	err = PTR_ERR(oe);
1412 	if (IS_ERR(oe))
1413 		goto out_err;
1414 
1415 	/* If the upper fs is nonexistent, we mark overlayfs r/o too */
1416 	if (!ovl_upper_mnt(ofs))
1417 		sb->s_flags |= SB_RDONLY;
1418 
1419 	if (!ofs->config.uuid && ofs->numfs > 1) {
1420 		pr_warn("The uuid=off requires a single fs for lower and upper, falling back to uuid=on.\n");
1421 		ofs->config.uuid = true;
1422 	}
1423 
1424 	if (!ovl_force_readonly(ofs) && ofs->config.index) {
1425 		err = ovl_get_indexdir(sb, ofs, oe, &ctx->upper);
1426 		if (err)
1427 			goto out_free_oe;
1428 
1429 		/* Force r/o mount with no index dir */
1430 		if (!ofs->indexdir)
1431 			sb->s_flags |= SB_RDONLY;
1432 	}
1433 
1434 	err = ovl_check_overlapping_layers(sb, ofs);
1435 	if (err)
1436 		goto out_free_oe;
1437 
1438 	/* Show index=off in /proc/mounts for forced r/o mount */
1439 	if (!ofs->indexdir) {
1440 		ofs->config.index = false;
1441 		if (ovl_upper_mnt(ofs) && ofs->config.nfs_export) {
1442 			pr_warn("NFS export requires an index dir, falling back to nfs_export=off.\n");
1443 			ofs->config.nfs_export = false;
1444 		}
1445 	}
1446 
1447 	if (ofs->config.metacopy && ofs->config.nfs_export) {
1448 		pr_warn("NFS export is not supported with metadata only copy up, falling back to nfs_export=off.\n");
1449 		ofs->config.nfs_export = false;
1450 	}
1451 
1452 	if (ofs->config.nfs_export)
1453 		sb->s_export_op = &ovl_export_operations;
1454 
1455 	/* Never override disk quota limits or use reserved space */
1456 	cap_lower(cred->cap_effective, CAP_SYS_RESOURCE);
1457 
1458 	sb->s_magic = OVERLAYFS_SUPER_MAGIC;
1459 	sb->s_xattr = ofs->config.userxattr ? ovl_user_xattr_handlers :
1460 		ovl_trusted_xattr_handlers;
1461 	sb->s_fs_info = ofs;
1462 	sb->s_flags |= SB_POSIXACL;
1463 	sb->s_iflags |= SB_I_SKIP_SYNC | SB_I_IMA_UNVERIFIABLE_SIGNATURE;
1464 
1465 	err = -ENOMEM;
1466 	root_dentry = ovl_get_root(sb, ctx->upper.dentry, oe);
1467 	if (!root_dentry)
1468 		goto out_free_oe;
1469 
1470 	sb->s_root = root_dentry;
1471 
1472 	return 0;
1473 
1474 out_free_oe:
1475 	ovl_free_entry(oe);
1476 out_err:
1477 	ovl_free_fs(ofs);
1478 	sb->s_fs_info = NULL;
1479 	return err;
1480 }
1481 
1482 static struct file_system_type ovl_fs_type = {
1483 	.owner			= THIS_MODULE,
1484 	.name			= "overlay",
1485 	.init_fs_context	= ovl_init_fs_context,
1486 	.parameters		= ovl_parameter_spec,
1487 	.fs_flags		= FS_USERNS_MOUNT,
1488 	.kill_sb		= kill_anon_super,
1489 };
1490 MODULE_ALIAS_FS("overlay");
1491 
1492 static void ovl_inode_init_once(void *foo)
1493 {
1494 	struct ovl_inode *oi = foo;
1495 
1496 	inode_init_once(&oi->vfs_inode);
1497 }
1498 
1499 static int __init ovl_init(void)
1500 {
1501 	int err;
1502 
1503 	ovl_inode_cachep = kmem_cache_create("ovl_inode",
1504 					     sizeof(struct ovl_inode), 0,
1505 					     (SLAB_RECLAIM_ACCOUNT|
1506 					      SLAB_MEM_SPREAD|SLAB_ACCOUNT),
1507 					     ovl_inode_init_once);
1508 	if (ovl_inode_cachep == NULL)
1509 		return -ENOMEM;
1510 
1511 	err = ovl_aio_request_cache_init();
1512 	if (!err) {
1513 		err = register_filesystem(&ovl_fs_type);
1514 		if (!err)
1515 			return 0;
1516 
1517 		ovl_aio_request_cache_destroy();
1518 	}
1519 	kmem_cache_destroy(ovl_inode_cachep);
1520 
1521 	return err;
1522 }
1523 
1524 static void __exit ovl_exit(void)
1525 {
1526 	unregister_filesystem(&ovl_fs_type);
1527 
1528 	/*
1529 	 * Make sure all delayed rcu free inodes are flushed before we
1530 	 * destroy cache.
1531 	 */
1532 	rcu_barrier();
1533 	kmem_cache_destroy(ovl_inode_cachep);
1534 	ovl_aio_request_cache_destroy();
1535 }
1536 
1537 module_init(ovl_init);
1538 module_exit(ovl_exit);
1539