xref: /linux/fs/overlayfs/dir.c (revision 987b741c52c7c6c68d46fbaeb95b8d1087f10b7f)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  *
4  * Copyright (C) 2011 Novell Inc.
5  */
6 
7 #include <linux/fs.h>
8 #include <linux/namei.h>
9 #include <linux/xattr.h>
10 #include <linux/security.h>
11 #include <linux/cred.h>
12 #include <linux/module.h>
13 #include <linux/posix_acl.h>
14 #include <linux/posix_acl_xattr.h>
15 #include <linux/atomic.h>
16 #include <linux/ratelimit.h>
17 #include "overlayfs.h"
18 
19 static unsigned short ovl_redirect_max = 256;
20 module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
21 MODULE_PARM_DESC(redirect_max,
22 		 "Maximum length of absolute redirect xattr value");
23 
24 static int ovl_set_redirect(struct dentry *dentry, bool samedir);
25 
26 int ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
27 {
28 	int err;
29 
30 	dget(wdentry);
31 	if (d_is_dir(wdentry))
32 		err = ovl_do_rmdir(wdir, wdentry);
33 	else
34 		err = ovl_do_unlink(wdir, wdentry);
35 	dput(wdentry);
36 
37 	if (err) {
38 		pr_err("cleanup of '%pd2' failed (%i)\n",
39 		       wdentry, err);
40 	}
41 
42 	return err;
43 }
44 
45 struct dentry *ovl_lookup_temp(struct dentry *workdir)
46 {
47 	struct dentry *temp;
48 	char name[20];
49 	static atomic_t temp_id = ATOMIC_INIT(0);
50 
51 	/* counter is allowed to wrap, since temp dentries are ephemeral */
52 	snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
53 
54 	temp = lookup_one_len(name, workdir, strlen(name));
55 	if (!IS_ERR(temp) && temp->d_inode) {
56 		pr_err("workdir/%s already exists\n", name);
57 		dput(temp);
58 		temp = ERR_PTR(-EIO);
59 	}
60 
61 	return temp;
62 }
63 
64 /* caller holds i_mutex on workdir */
65 static struct dentry *ovl_whiteout(struct ovl_fs *ofs)
66 {
67 	int err;
68 	struct dentry *whiteout;
69 	struct dentry *workdir = ofs->workdir;
70 	struct inode *wdir = workdir->d_inode;
71 
72 	if (!ofs->whiteout) {
73 		whiteout = ovl_lookup_temp(workdir);
74 		if (IS_ERR(whiteout))
75 			goto out;
76 
77 		err = ovl_do_whiteout(wdir, whiteout);
78 		if (err) {
79 			dput(whiteout);
80 			whiteout = ERR_PTR(err);
81 			goto out;
82 		}
83 		ofs->whiteout = whiteout;
84 	}
85 
86 	if (ofs->share_whiteout) {
87 		whiteout = ovl_lookup_temp(workdir);
88 		if (IS_ERR(whiteout))
89 			goto out;
90 
91 		err = ovl_do_link(ofs->whiteout, wdir, whiteout);
92 		if (!err)
93 			goto out;
94 
95 		if (err != -EMLINK) {
96 			pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%i)\n",
97 				ofs->whiteout->d_inode->i_nlink, err);
98 			ofs->share_whiteout = false;
99 		}
100 		dput(whiteout);
101 	}
102 	whiteout = ofs->whiteout;
103 	ofs->whiteout = NULL;
104 out:
105 	return whiteout;
106 }
107 
108 /* Caller must hold i_mutex on both workdir and dir */
109 int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
110 			     struct dentry *dentry)
111 {
112 	struct inode *wdir = ofs->workdir->d_inode;
113 	struct dentry *whiteout;
114 	int err;
115 	int flags = 0;
116 
117 	whiteout = ovl_whiteout(ofs);
118 	err = PTR_ERR(whiteout);
119 	if (IS_ERR(whiteout))
120 		return err;
121 
122 	if (d_is_dir(dentry))
123 		flags = RENAME_EXCHANGE;
124 
125 	err = ovl_do_rename(wdir, whiteout, dir, dentry, flags);
126 	if (err)
127 		goto kill_whiteout;
128 	if (flags)
129 		ovl_cleanup(wdir, dentry);
130 
131 out:
132 	dput(whiteout);
133 	return err;
134 
135 kill_whiteout:
136 	ovl_cleanup(wdir, whiteout);
137 	goto out;
138 }
139 
140 static int ovl_mkdir_real(struct inode *dir, struct dentry **newdentry,
141 			  umode_t mode)
142 {
143 	int err;
144 	struct dentry *d, *dentry = *newdentry;
145 
146 	err = ovl_do_mkdir(dir, dentry, mode);
147 	if (err)
148 		return err;
149 
150 	if (likely(!d_unhashed(dentry)))
151 		return 0;
152 
153 	/*
154 	 * vfs_mkdir() may succeed and leave the dentry passed
155 	 * to it unhashed and negative. If that happens, try to
156 	 * lookup a new hashed and positive dentry.
157 	 */
158 	d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
159 			   dentry->d_name.len);
160 	if (IS_ERR(d)) {
161 		pr_warn("failed lookup after mkdir (%pd2, err=%i).\n",
162 			dentry, err);
163 		return PTR_ERR(d);
164 	}
165 	dput(dentry);
166 	*newdentry = d;
167 
168 	return 0;
169 }
170 
171 struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
172 			       struct ovl_cattr *attr)
173 {
174 	int err;
175 
176 	if (IS_ERR(newdentry))
177 		return newdentry;
178 
179 	err = -ESTALE;
180 	if (newdentry->d_inode)
181 		goto out;
182 
183 	if (attr->hardlink) {
184 		err = ovl_do_link(attr->hardlink, dir, newdentry);
185 	} else {
186 		switch (attr->mode & S_IFMT) {
187 		case S_IFREG:
188 			err = ovl_do_create(dir, newdentry, attr->mode);
189 			break;
190 
191 		case S_IFDIR:
192 			/* mkdir is special... */
193 			err =  ovl_mkdir_real(dir, &newdentry, attr->mode);
194 			break;
195 
196 		case S_IFCHR:
197 		case S_IFBLK:
198 		case S_IFIFO:
199 		case S_IFSOCK:
200 			err = ovl_do_mknod(dir, newdentry, attr->mode,
201 					   attr->rdev);
202 			break;
203 
204 		case S_IFLNK:
205 			err = ovl_do_symlink(dir, newdentry, attr->link);
206 			break;
207 
208 		default:
209 			err = -EPERM;
210 		}
211 	}
212 	if (!err && WARN_ON(!newdentry->d_inode)) {
213 		/*
214 		 * Not quite sure if non-instantiated dentry is legal or not.
215 		 * VFS doesn't seem to care so check and warn here.
216 		 */
217 		err = -EIO;
218 	}
219 out:
220 	if (err) {
221 		dput(newdentry);
222 		return ERR_PTR(err);
223 	}
224 	return newdentry;
225 }
226 
227 struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr)
228 {
229 	return ovl_create_real(d_inode(workdir), ovl_lookup_temp(workdir),
230 			       attr);
231 }
232 
233 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
234 			       int xerr)
235 {
236 	int err;
237 
238 	err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
239 	if (!err)
240 		ovl_dentry_set_opaque(dentry);
241 
242 	return err;
243 }
244 
245 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
246 {
247 	/*
248 	 * Fail with -EIO when trying to create opaque dir and upper doesn't
249 	 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
250 	 * return a specific error for noxattr case.
251 	 */
252 	return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
253 }
254 
255 /*
256  * Common operations required to be done after creation of file on upper.
257  * If @hardlink is false, then @inode is a pre-allocated inode, we may or
258  * may not use to instantiate the new dentry.
259  */
260 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
261 			   struct dentry *newdentry, bool hardlink)
262 {
263 	struct ovl_inode_params oip = {
264 		.upperdentry = newdentry,
265 		.newinode = inode,
266 	};
267 
268 	ovl_dir_modified(dentry->d_parent, false);
269 	ovl_dentry_set_upper_alias(dentry);
270 	ovl_dentry_update_reval(dentry, newdentry,
271 			DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
272 
273 	if (!hardlink) {
274 		/*
275 		 * ovl_obtain_alias() can be called after ovl_create_real()
276 		 * and before we get here, so we may get an inode from cache
277 		 * with the same real upperdentry that is not the inode we
278 		 * pre-allocated.  In this case we will use the cached inode
279 		 * to instantiate the new dentry.
280 		 *
281 		 * XXX: if we ever use ovl_obtain_alias() to decode directory
282 		 * file handles, need to use ovl_get_inode_locked() and
283 		 * d_instantiate_new() here to prevent from creating two
284 		 * hashed directory inode aliases.
285 		 */
286 		inode = ovl_get_inode(dentry->d_sb, &oip);
287 		if (IS_ERR(inode))
288 			return PTR_ERR(inode);
289 		if (inode == oip.newinode)
290 			ovl_set_flag(OVL_UPPERDATA, inode);
291 	} else {
292 		WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
293 		dput(newdentry);
294 		inc_nlink(inode);
295 	}
296 
297 	d_instantiate(dentry, inode);
298 	if (inode != oip.newinode) {
299 		pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
300 				    dentry);
301 	}
302 
303 	/* Force lookup of new upper hardlink to find its lower */
304 	if (hardlink)
305 		d_drop(dentry);
306 
307 	return 0;
308 }
309 
310 static bool ovl_type_merge(struct dentry *dentry)
311 {
312 	return OVL_TYPE_MERGE(ovl_path_type(dentry));
313 }
314 
315 static bool ovl_type_origin(struct dentry *dentry)
316 {
317 	return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
318 }
319 
320 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
321 			    struct ovl_cattr *attr)
322 {
323 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
324 	struct inode *udir = upperdir->d_inode;
325 	struct dentry *newdentry;
326 	int err;
327 
328 	if (!attr->hardlink && !IS_POSIXACL(udir))
329 		attr->mode &= ~current_umask();
330 
331 	inode_lock_nested(udir, I_MUTEX_PARENT);
332 	newdentry = ovl_create_real(udir,
333 				    lookup_one_len(dentry->d_name.name,
334 						   upperdir,
335 						   dentry->d_name.len),
336 				    attr);
337 	err = PTR_ERR(newdentry);
338 	if (IS_ERR(newdentry))
339 		goto out_unlock;
340 
341 	if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
342 		/* Setting opaque here is just an optimization, allow to fail */
343 		ovl_set_opaque(dentry, newdentry);
344 	}
345 
346 	err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
347 	if (err)
348 		goto out_cleanup;
349 out_unlock:
350 	inode_unlock(udir);
351 	return err;
352 
353 out_cleanup:
354 	ovl_cleanup(udir, newdentry);
355 	dput(newdentry);
356 	goto out_unlock;
357 }
358 
359 static struct dentry *ovl_clear_empty(struct dentry *dentry,
360 				      struct list_head *list)
361 {
362 	struct dentry *workdir = ovl_workdir(dentry);
363 	struct inode *wdir = workdir->d_inode;
364 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
365 	struct inode *udir = upperdir->d_inode;
366 	struct path upperpath;
367 	struct dentry *upper;
368 	struct dentry *opaquedir;
369 	struct kstat stat;
370 	int err;
371 
372 	if (WARN_ON(!workdir))
373 		return ERR_PTR(-EROFS);
374 
375 	err = ovl_lock_rename_workdir(workdir, upperdir);
376 	if (err)
377 		goto out;
378 
379 	ovl_path_upper(dentry, &upperpath);
380 	err = vfs_getattr(&upperpath, &stat,
381 			  STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
382 	if (err)
383 		goto out_unlock;
384 
385 	err = -ESTALE;
386 	if (!S_ISDIR(stat.mode))
387 		goto out_unlock;
388 	upper = upperpath.dentry;
389 	if (upper->d_parent->d_inode != udir)
390 		goto out_unlock;
391 
392 	opaquedir = ovl_create_temp(workdir, OVL_CATTR(stat.mode));
393 	err = PTR_ERR(opaquedir);
394 	if (IS_ERR(opaquedir))
395 		goto out_unlock;
396 
397 	err = ovl_copy_xattr(dentry->d_sb, upper, opaquedir);
398 	if (err)
399 		goto out_cleanup;
400 
401 	err = ovl_set_opaque(dentry, opaquedir);
402 	if (err)
403 		goto out_cleanup;
404 
405 	inode_lock(opaquedir->d_inode);
406 	err = ovl_set_attr(opaquedir, &stat);
407 	inode_unlock(opaquedir->d_inode);
408 	if (err)
409 		goto out_cleanup;
410 
411 	err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
412 	if (err)
413 		goto out_cleanup;
414 
415 	ovl_cleanup_whiteouts(upper, list);
416 	ovl_cleanup(wdir, upper);
417 	unlock_rename(workdir, upperdir);
418 
419 	/* dentry's upper doesn't match now, get rid of it */
420 	d_drop(dentry);
421 
422 	return opaquedir;
423 
424 out_cleanup:
425 	ovl_cleanup(wdir, opaquedir);
426 	dput(opaquedir);
427 out_unlock:
428 	unlock_rename(workdir, upperdir);
429 out:
430 	return ERR_PTR(err);
431 }
432 
433 static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
434 			     const struct posix_acl *acl)
435 {
436 	void *buffer;
437 	size_t size;
438 	int err;
439 
440 	if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
441 		return 0;
442 
443 	size = posix_acl_xattr_size(acl->a_count);
444 	buffer = kmalloc(size, GFP_KERNEL);
445 	if (!buffer)
446 		return -ENOMEM;
447 
448 	err = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
449 	if (err < 0)
450 		goto out_free;
451 
452 	err = vfs_setxattr(&init_user_ns, upperdentry, name, buffer, size, XATTR_CREATE);
453 out_free:
454 	kfree(buffer);
455 	return err;
456 }
457 
458 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
459 				    struct ovl_cattr *cattr)
460 {
461 	struct dentry *workdir = ovl_workdir(dentry);
462 	struct inode *wdir = workdir->d_inode;
463 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
464 	struct inode *udir = upperdir->d_inode;
465 	struct dentry *upper;
466 	struct dentry *newdentry;
467 	int err;
468 	struct posix_acl *acl, *default_acl;
469 	bool hardlink = !!cattr->hardlink;
470 
471 	if (WARN_ON(!workdir))
472 		return -EROFS;
473 
474 	if (!hardlink) {
475 		err = posix_acl_create(dentry->d_parent->d_inode,
476 				       &cattr->mode, &default_acl, &acl);
477 		if (err)
478 			return err;
479 	}
480 
481 	err = ovl_lock_rename_workdir(workdir, upperdir);
482 	if (err)
483 		goto out;
484 
485 	upper = lookup_one_len(dentry->d_name.name, upperdir,
486 			       dentry->d_name.len);
487 	err = PTR_ERR(upper);
488 	if (IS_ERR(upper))
489 		goto out_unlock;
490 
491 	err = -ESTALE;
492 	if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
493 		goto out_dput;
494 
495 	newdentry = ovl_create_temp(workdir, cattr);
496 	err = PTR_ERR(newdentry);
497 	if (IS_ERR(newdentry))
498 		goto out_dput;
499 
500 	/*
501 	 * mode could have been mutilated due to umask (e.g. sgid directory)
502 	 */
503 	if (!hardlink &&
504 	    !S_ISLNK(cattr->mode) &&
505 	    newdentry->d_inode->i_mode != cattr->mode) {
506 		struct iattr attr = {
507 			.ia_valid = ATTR_MODE,
508 			.ia_mode = cattr->mode,
509 		};
510 		inode_lock(newdentry->d_inode);
511 		err = notify_change(&init_user_ns, newdentry, &attr, NULL);
512 		inode_unlock(newdentry->d_inode);
513 		if (err)
514 			goto out_cleanup;
515 	}
516 	if (!hardlink) {
517 		err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
518 					acl);
519 		if (err)
520 			goto out_cleanup;
521 
522 		err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
523 					default_acl);
524 		if (err)
525 			goto out_cleanup;
526 	}
527 
528 	if (!hardlink && S_ISDIR(cattr->mode)) {
529 		err = ovl_set_opaque(dentry, newdentry);
530 		if (err)
531 			goto out_cleanup;
532 
533 		err = ovl_do_rename(wdir, newdentry, udir, upper,
534 				    RENAME_EXCHANGE);
535 		if (err)
536 			goto out_cleanup;
537 
538 		ovl_cleanup(wdir, upper);
539 	} else {
540 		err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
541 		if (err)
542 			goto out_cleanup;
543 	}
544 	err = ovl_instantiate(dentry, inode, newdentry, hardlink);
545 	if (err)
546 		goto out_cleanup;
547 out_dput:
548 	dput(upper);
549 out_unlock:
550 	unlock_rename(workdir, upperdir);
551 out:
552 	if (!hardlink) {
553 		posix_acl_release(acl);
554 		posix_acl_release(default_acl);
555 	}
556 	return err;
557 
558 out_cleanup:
559 	ovl_cleanup(wdir, newdentry);
560 	dput(newdentry);
561 	goto out_dput;
562 }
563 
564 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
565 			      struct ovl_cattr *attr, bool origin)
566 {
567 	int err;
568 	const struct cred *old_cred;
569 	struct cred *override_cred;
570 	struct dentry *parent = dentry->d_parent;
571 
572 	err = ovl_copy_up(parent);
573 	if (err)
574 		return err;
575 
576 	old_cred = ovl_override_creds(dentry->d_sb);
577 
578 	/*
579 	 * When linking a file with copy up origin into a new parent, mark the
580 	 * new parent dir "impure".
581 	 */
582 	if (origin) {
583 		err = ovl_set_impure(parent, ovl_dentry_upper(parent));
584 		if (err)
585 			goto out_revert_creds;
586 	}
587 
588 	err = -ENOMEM;
589 	override_cred = prepare_creds();
590 	if (override_cred) {
591 		override_cred->fsuid = inode->i_uid;
592 		override_cred->fsgid = inode->i_gid;
593 		if (!attr->hardlink) {
594 			err = security_dentry_create_files_as(dentry,
595 					attr->mode, &dentry->d_name, old_cred,
596 					override_cred);
597 			if (err) {
598 				put_cred(override_cred);
599 				goto out_revert_creds;
600 			}
601 		}
602 		put_cred(override_creds(override_cred));
603 		put_cred(override_cred);
604 
605 		if (!ovl_dentry_is_whiteout(dentry))
606 			err = ovl_create_upper(dentry, inode, attr);
607 		else
608 			err = ovl_create_over_whiteout(dentry, inode, attr);
609 	}
610 out_revert_creds:
611 	revert_creds(old_cred);
612 	return err;
613 }
614 
615 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
616 			     const char *link)
617 {
618 	int err;
619 	struct inode *inode;
620 	struct ovl_cattr attr = {
621 		.rdev = rdev,
622 		.link = link,
623 	};
624 
625 	err = ovl_want_write(dentry);
626 	if (err)
627 		goto out;
628 
629 	/* Preallocate inode to be used by ovl_get_inode() */
630 	err = -ENOMEM;
631 	inode = ovl_new_inode(dentry->d_sb, mode, rdev);
632 	if (!inode)
633 		goto out_drop_write;
634 
635 	spin_lock(&inode->i_lock);
636 	inode->i_state |= I_CREATING;
637 	spin_unlock(&inode->i_lock);
638 
639 	inode_init_owner(&init_user_ns, inode, dentry->d_parent->d_inode, mode);
640 	attr.mode = inode->i_mode;
641 
642 	err = ovl_create_or_link(dentry, inode, &attr, false);
643 	/* Did we end up using the preallocated inode? */
644 	if (inode != d_inode(dentry))
645 		iput(inode);
646 
647 out_drop_write:
648 	ovl_drop_write(dentry);
649 out:
650 	return err;
651 }
652 
653 static int ovl_create(struct user_namespace *mnt_userns, struct inode *dir,
654 		      struct dentry *dentry, umode_t mode, bool excl)
655 {
656 	return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
657 }
658 
659 static int ovl_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
660 		     struct dentry *dentry, umode_t mode)
661 {
662 	return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
663 }
664 
665 static int ovl_mknod(struct user_namespace *mnt_userns, struct inode *dir,
666 		     struct dentry *dentry, umode_t mode, dev_t rdev)
667 {
668 	/* Don't allow creation of "whiteout" on overlay */
669 	if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
670 		return -EPERM;
671 
672 	return ovl_create_object(dentry, mode, rdev, NULL);
673 }
674 
675 static int ovl_symlink(struct user_namespace *mnt_userns, struct inode *dir,
676 		       struct dentry *dentry, const char *link)
677 {
678 	return ovl_create_object(dentry, S_IFLNK, 0, link);
679 }
680 
681 static int ovl_set_link_redirect(struct dentry *dentry)
682 {
683 	const struct cred *old_cred;
684 	int err;
685 
686 	old_cred = ovl_override_creds(dentry->d_sb);
687 	err = ovl_set_redirect(dentry, false);
688 	revert_creds(old_cred);
689 
690 	return err;
691 }
692 
693 static int ovl_link(struct dentry *old, struct inode *newdir,
694 		    struct dentry *new)
695 {
696 	int err;
697 	struct inode *inode;
698 
699 	err = ovl_want_write(old);
700 	if (err)
701 		goto out;
702 
703 	err = ovl_copy_up(old);
704 	if (err)
705 		goto out_drop_write;
706 
707 	err = ovl_copy_up(new->d_parent);
708 	if (err)
709 		goto out_drop_write;
710 
711 	if (ovl_is_metacopy_dentry(old)) {
712 		err = ovl_set_link_redirect(old);
713 		if (err)
714 			goto out_drop_write;
715 	}
716 
717 	err = ovl_nlink_start(old);
718 	if (err)
719 		goto out_drop_write;
720 
721 	inode = d_inode(old);
722 	ihold(inode);
723 
724 	err = ovl_create_or_link(new, inode,
725 			&(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
726 			ovl_type_origin(old));
727 	if (err)
728 		iput(inode);
729 
730 	ovl_nlink_end(old);
731 out_drop_write:
732 	ovl_drop_write(old);
733 out:
734 	return err;
735 }
736 
737 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
738 {
739 	return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
740 }
741 
742 static int ovl_remove_and_whiteout(struct dentry *dentry,
743 				   struct list_head *list)
744 {
745 	struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
746 	struct dentry *workdir = ovl_workdir(dentry);
747 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
748 	struct dentry *upper;
749 	struct dentry *opaquedir = NULL;
750 	int err;
751 
752 	if (WARN_ON(!workdir))
753 		return -EROFS;
754 
755 	if (!list_empty(list)) {
756 		opaquedir = ovl_clear_empty(dentry, list);
757 		err = PTR_ERR(opaquedir);
758 		if (IS_ERR(opaquedir))
759 			goto out;
760 	}
761 
762 	err = ovl_lock_rename_workdir(workdir, upperdir);
763 	if (err)
764 		goto out_dput;
765 
766 	upper = lookup_one_len(dentry->d_name.name, upperdir,
767 			       dentry->d_name.len);
768 	err = PTR_ERR(upper);
769 	if (IS_ERR(upper))
770 		goto out_unlock;
771 
772 	err = -ESTALE;
773 	if ((opaquedir && upper != opaquedir) ||
774 	    (!opaquedir && ovl_dentry_upper(dentry) &&
775 	     !ovl_matches_upper(dentry, upper))) {
776 		goto out_dput_upper;
777 	}
778 
779 	err = ovl_cleanup_and_whiteout(ofs, d_inode(upperdir), upper);
780 	if (err)
781 		goto out_d_drop;
782 
783 	ovl_dir_modified(dentry->d_parent, true);
784 out_d_drop:
785 	d_drop(dentry);
786 out_dput_upper:
787 	dput(upper);
788 out_unlock:
789 	unlock_rename(workdir, upperdir);
790 out_dput:
791 	dput(opaquedir);
792 out:
793 	return err;
794 }
795 
796 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
797 			    struct list_head *list)
798 {
799 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
800 	struct inode *dir = upperdir->d_inode;
801 	struct dentry *upper;
802 	struct dentry *opaquedir = NULL;
803 	int err;
804 
805 	if (!list_empty(list)) {
806 		opaquedir = ovl_clear_empty(dentry, list);
807 		err = PTR_ERR(opaquedir);
808 		if (IS_ERR(opaquedir))
809 			goto out;
810 	}
811 
812 	inode_lock_nested(dir, I_MUTEX_PARENT);
813 	upper = lookup_one_len(dentry->d_name.name, upperdir,
814 			       dentry->d_name.len);
815 	err = PTR_ERR(upper);
816 	if (IS_ERR(upper))
817 		goto out_unlock;
818 
819 	err = -ESTALE;
820 	if ((opaquedir && upper != opaquedir) ||
821 	    (!opaquedir && !ovl_matches_upper(dentry, upper)))
822 		goto out_dput_upper;
823 
824 	if (is_dir)
825 		err = vfs_rmdir(&init_user_ns, dir, upper);
826 	else
827 		err = vfs_unlink(&init_user_ns, dir, upper, NULL);
828 	ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
829 
830 	/*
831 	 * Keeping this dentry hashed would mean having to release
832 	 * upperpath/lowerpath, which could only be done if we are the
833 	 * sole user of this dentry.  Too tricky...  Just unhash for
834 	 * now.
835 	 */
836 	if (!err)
837 		d_drop(dentry);
838 out_dput_upper:
839 	dput(upper);
840 out_unlock:
841 	inode_unlock(dir);
842 	dput(opaquedir);
843 out:
844 	return err;
845 }
846 
847 static bool ovl_pure_upper(struct dentry *dentry)
848 {
849 	return !ovl_dentry_lower(dentry) &&
850 	       !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
851 }
852 
853 static void ovl_drop_nlink(struct dentry *dentry)
854 {
855 	struct inode *inode = d_inode(dentry);
856 	struct dentry *alias;
857 
858 	/* Try to find another, hashed alias */
859 	spin_lock(&inode->i_lock);
860 	hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
861 		if (alias != dentry && !d_unhashed(alias))
862 			break;
863 	}
864 	spin_unlock(&inode->i_lock);
865 
866 	/*
867 	 * Changes to underlying layers may cause i_nlink to lose sync with
868 	 * reality.  In this case prevent the link count from going to zero
869 	 * prematurely.
870 	 */
871 	if (inode->i_nlink > !!alias)
872 		drop_nlink(inode);
873 }
874 
875 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
876 {
877 	int err;
878 	const struct cred *old_cred;
879 	struct dentry *upperdentry;
880 	bool lower_positive = ovl_lower_positive(dentry);
881 	LIST_HEAD(list);
882 
883 	/* No need to clean pure upper removed by vfs_rmdir() */
884 	if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
885 		err = ovl_check_empty_dir(dentry, &list);
886 		if (err)
887 			goto out;
888 	}
889 
890 	err = ovl_want_write(dentry);
891 	if (err)
892 		goto out;
893 
894 	err = ovl_copy_up(dentry->d_parent);
895 	if (err)
896 		goto out_drop_write;
897 
898 	err = ovl_nlink_start(dentry);
899 	if (err)
900 		goto out_drop_write;
901 
902 	old_cred = ovl_override_creds(dentry->d_sb);
903 	if (!lower_positive)
904 		err = ovl_remove_upper(dentry, is_dir, &list);
905 	else
906 		err = ovl_remove_and_whiteout(dentry, &list);
907 	revert_creds(old_cred);
908 	if (!err) {
909 		if (is_dir)
910 			clear_nlink(dentry->d_inode);
911 		else
912 			ovl_drop_nlink(dentry);
913 	}
914 	ovl_nlink_end(dentry);
915 
916 	/*
917 	 * Copy ctime
918 	 *
919 	 * Note: we fail to update ctime if there was no copy-up, only a
920 	 * whiteout
921 	 */
922 	upperdentry = ovl_dentry_upper(dentry);
923 	if (upperdentry)
924 		ovl_copyattr(d_inode(upperdentry), d_inode(dentry));
925 
926 out_drop_write:
927 	ovl_drop_write(dentry);
928 out:
929 	ovl_cache_free(&list);
930 	return err;
931 }
932 
933 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
934 {
935 	return ovl_do_remove(dentry, false);
936 }
937 
938 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
939 {
940 	return ovl_do_remove(dentry, true);
941 }
942 
943 static bool ovl_type_merge_or_lower(struct dentry *dentry)
944 {
945 	enum ovl_path_type type = ovl_path_type(dentry);
946 
947 	return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
948 }
949 
950 static bool ovl_can_move(struct dentry *dentry)
951 {
952 	return ovl_redirect_dir(dentry->d_sb) ||
953 		!d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
954 }
955 
956 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
957 {
958 	char *buf, *ret;
959 	struct dentry *d, *tmp;
960 	int buflen = ovl_redirect_max + 1;
961 
962 	if (!abs_redirect) {
963 		ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
964 			       GFP_KERNEL);
965 		goto out;
966 	}
967 
968 	buf = ret = kmalloc(buflen, GFP_KERNEL);
969 	if (!buf)
970 		goto out;
971 
972 	buflen--;
973 	buf[buflen] = '\0';
974 	for (d = dget(dentry); !IS_ROOT(d);) {
975 		const char *name;
976 		int thislen;
977 
978 		spin_lock(&d->d_lock);
979 		name = ovl_dentry_get_redirect(d);
980 		if (name) {
981 			thislen = strlen(name);
982 		} else {
983 			name = d->d_name.name;
984 			thislen = d->d_name.len;
985 		}
986 
987 		/* If path is too long, fall back to userspace move */
988 		if (thislen + (name[0] != '/') > buflen) {
989 			ret = ERR_PTR(-EXDEV);
990 			spin_unlock(&d->d_lock);
991 			goto out_put;
992 		}
993 
994 		buflen -= thislen;
995 		memcpy(&buf[buflen], name, thislen);
996 		spin_unlock(&d->d_lock);
997 		tmp = dget_parent(d);
998 
999 		dput(d);
1000 		d = tmp;
1001 
1002 		/* Absolute redirect: finished */
1003 		if (buf[buflen] == '/')
1004 			break;
1005 		buflen--;
1006 		buf[buflen] = '/';
1007 	}
1008 	ret = kstrdup(&buf[buflen], GFP_KERNEL);
1009 out_put:
1010 	dput(d);
1011 	kfree(buf);
1012 out:
1013 	return ret ? ret : ERR_PTR(-ENOMEM);
1014 }
1015 
1016 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
1017 {
1018 	struct dentry *lowerdentry;
1019 
1020 	if (!samedir)
1021 		return true;
1022 
1023 	if (d_is_dir(dentry))
1024 		return false;
1025 
1026 	/*
1027 	 * For non-dir hardlinked files, we need absolute redirects
1028 	 * in general as two upper hardlinks could be in different
1029 	 * dirs. We could put a relative redirect now and convert
1030 	 * it to absolute redirect later. But when nlink > 1 and
1031 	 * indexing is on, that means relative redirect needs to be
1032 	 * converted to absolute during copy up of another lower
1033 	 * hardllink as well.
1034 	 *
1035 	 * So without optimizing too much, just check if lower is
1036 	 * a hard link or not. If lower is hard link, put absolute
1037 	 * redirect.
1038 	 */
1039 	lowerdentry = ovl_dentry_lower(dentry);
1040 	return (d_inode(lowerdentry)->i_nlink > 1);
1041 }
1042 
1043 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
1044 {
1045 	int err;
1046 	const char *redirect = ovl_dentry_get_redirect(dentry);
1047 	bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1048 
1049 	if (redirect && (!absolute_redirect || redirect[0] == '/'))
1050 		return 0;
1051 
1052 	redirect = ovl_get_redirect(dentry, absolute_redirect);
1053 	if (IS_ERR(redirect))
1054 		return PTR_ERR(redirect);
1055 
1056 	err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
1057 				 OVL_XATTR_REDIRECT,
1058 				 redirect, strlen(redirect), -EXDEV);
1059 	if (!err) {
1060 		spin_lock(&dentry->d_lock);
1061 		ovl_dentry_set_redirect(dentry, redirect);
1062 		spin_unlock(&dentry->d_lock);
1063 	} else {
1064 		kfree(redirect);
1065 		pr_warn_ratelimited("failed to set redirect (%i)\n",
1066 				    err);
1067 		/* Fall back to userspace copy-up */
1068 		err = -EXDEV;
1069 	}
1070 	return err;
1071 }
1072 
1073 static int ovl_rename(struct user_namespace *mnt_userns, struct inode *olddir,
1074 		      struct dentry *old, struct inode *newdir,
1075 		      struct dentry *new, unsigned int flags)
1076 {
1077 	int err;
1078 	struct dentry *old_upperdir;
1079 	struct dentry *new_upperdir;
1080 	struct dentry *olddentry;
1081 	struct dentry *newdentry;
1082 	struct dentry *trap;
1083 	bool old_opaque;
1084 	bool new_opaque;
1085 	bool cleanup_whiteout = false;
1086 	bool update_nlink = false;
1087 	bool overwrite = !(flags & RENAME_EXCHANGE);
1088 	bool is_dir = d_is_dir(old);
1089 	bool new_is_dir = d_is_dir(new);
1090 	bool samedir = olddir == newdir;
1091 	struct dentry *opaquedir = NULL;
1092 	const struct cred *old_cred = NULL;
1093 	LIST_HEAD(list);
1094 
1095 	err = -EINVAL;
1096 	if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1097 		goto out;
1098 
1099 	flags &= ~RENAME_NOREPLACE;
1100 
1101 	/* Don't copy up directory trees */
1102 	err = -EXDEV;
1103 	if (!ovl_can_move(old))
1104 		goto out;
1105 	if (!overwrite && !ovl_can_move(new))
1106 		goto out;
1107 
1108 	if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1109 		err = ovl_check_empty_dir(new, &list);
1110 		if (err)
1111 			goto out;
1112 	}
1113 
1114 	if (overwrite) {
1115 		if (ovl_lower_positive(old)) {
1116 			if (!ovl_dentry_is_whiteout(new)) {
1117 				/* Whiteout source */
1118 				flags |= RENAME_WHITEOUT;
1119 			} else {
1120 				/* Switch whiteouts */
1121 				flags |= RENAME_EXCHANGE;
1122 			}
1123 		} else if (is_dir && ovl_dentry_is_whiteout(new)) {
1124 			flags |= RENAME_EXCHANGE;
1125 			cleanup_whiteout = true;
1126 		}
1127 	}
1128 
1129 	err = ovl_want_write(old);
1130 	if (err)
1131 		goto out;
1132 
1133 	err = ovl_copy_up(old);
1134 	if (err)
1135 		goto out_drop_write;
1136 
1137 	err = ovl_copy_up(new->d_parent);
1138 	if (err)
1139 		goto out_drop_write;
1140 	if (!overwrite) {
1141 		err = ovl_copy_up(new);
1142 		if (err)
1143 			goto out_drop_write;
1144 	} else if (d_inode(new)) {
1145 		err = ovl_nlink_start(new);
1146 		if (err)
1147 			goto out_drop_write;
1148 
1149 		update_nlink = true;
1150 	}
1151 
1152 	old_cred = ovl_override_creds(old->d_sb);
1153 
1154 	if (!list_empty(&list)) {
1155 		opaquedir = ovl_clear_empty(new, &list);
1156 		err = PTR_ERR(opaquedir);
1157 		if (IS_ERR(opaquedir)) {
1158 			opaquedir = NULL;
1159 			goto out_revert_creds;
1160 		}
1161 	}
1162 
1163 	old_upperdir = ovl_dentry_upper(old->d_parent);
1164 	new_upperdir = ovl_dentry_upper(new->d_parent);
1165 
1166 	if (!samedir) {
1167 		/*
1168 		 * When moving a merge dir or non-dir with copy up origin into
1169 		 * a new parent, we are marking the new parent dir "impure".
1170 		 * When ovl_iterate() iterates an "impure" upper dir, it will
1171 		 * lookup the origin inodes of the entries to fill d_ino.
1172 		 */
1173 		if (ovl_type_origin(old)) {
1174 			err = ovl_set_impure(new->d_parent, new_upperdir);
1175 			if (err)
1176 				goto out_revert_creds;
1177 		}
1178 		if (!overwrite && ovl_type_origin(new)) {
1179 			err = ovl_set_impure(old->d_parent, old_upperdir);
1180 			if (err)
1181 				goto out_revert_creds;
1182 		}
1183 	}
1184 
1185 	trap = lock_rename(new_upperdir, old_upperdir);
1186 
1187 	olddentry = lookup_one_len(old->d_name.name, old_upperdir,
1188 				   old->d_name.len);
1189 	err = PTR_ERR(olddentry);
1190 	if (IS_ERR(olddentry))
1191 		goto out_unlock;
1192 
1193 	err = -ESTALE;
1194 	if (!ovl_matches_upper(old, olddentry))
1195 		goto out_dput_old;
1196 
1197 	newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1198 				   new->d_name.len);
1199 	err = PTR_ERR(newdentry);
1200 	if (IS_ERR(newdentry))
1201 		goto out_dput_old;
1202 
1203 	old_opaque = ovl_dentry_is_opaque(old);
1204 	new_opaque = ovl_dentry_is_opaque(new);
1205 
1206 	err = -ESTALE;
1207 	if (d_inode(new) && ovl_dentry_upper(new)) {
1208 		if (opaquedir) {
1209 			if (newdentry != opaquedir)
1210 				goto out_dput;
1211 		} else {
1212 			if (!ovl_matches_upper(new, newdentry))
1213 				goto out_dput;
1214 		}
1215 	} else {
1216 		if (!d_is_negative(newdentry) &&
1217 		    (!new_opaque || !ovl_is_whiteout(newdentry)))
1218 			goto out_dput;
1219 	}
1220 
1221 	if (olddentry == trap)
1222 		goto out_dput;
1223 	if (newdentry == trap)
1224 		goto out_dput;
1225 
1226 	if (olddentry->d_inode == newdentry->d_inode)
1227 		goto out_dput;
1228 
1229 	err = 0;
1230 	if (ovl_type_merge_or_lower(old))
1231 		err = ovl_set_redirect(old, samedir);
1232 	else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1233 		err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1234 	if (err)
1235 		goto out_dput;
1236 
1237 	if (!overwrite && ovl_type_merge_or_lower(new))
1238 		err = ovl_set_redirect(new, samedir);
1239 	else if (!overwrite && new_is_dir && !new_opaque &&
1240 		 ovl_type_merge(old->d_parent))
1241 		err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1242 	if (err)
1243 		goto out_dput;
1244 
1245 	err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1246 			    new_upperdir->d_inode, newdentry, flags);
1247 	if (err)
1248 		goto out_dput;
1249 
1250 	if (cleanup_whiteout)
1251 		ovl_cleanup(old_upperdir->d_inode, newdentry);
1252 
1253 	if (overwrite && d_inode(new)) {
1254 		if (new_is_dir)
1255 			clear_nlink(d_inode(new));
1256 		else
1257 			ovl_drop_nlink(new);
1258 	}
1259 
1260 	ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1261 			 (!overwrite && ovl_type_origin(new)));
1262 	ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1263 			 (d_inode(new) && ovl_type_origin(new)));
1264 
1265 	/* copy ctime: */
1266 	ovl_copyattr(d_inode(olddentry), d_inode(old));
1267 	if (d_inode(new) && ovl_dentry_upper(new))
1268 		ovl_copyattr(d_inode(newdentry), d_inode(new));
1269 
1270 out_dput:
1271 	dput(newdentry);
1272 out_dput_old:
1273 	dput(olddentry);
1274 out_unlock:
1275 	unlock_rename(new_upperdir, old_upperdir);
1276 out_revert_creds:
1277 	revert_creds(old_cred);
1278 	if (update_nlink)
1279 		ovl_nlink_end(new);
1280 out_drop_write:
1281 	ovl_drop_write(old);
1282 out:
1283 	dput(opaquedir);
1284 	ovl_cache_free(&list);
1285 	return err;
1286 }
1287 
1288 const struct inode_operations ovl_dir_inode_operations = {
1289 	.lookup		= ovl_lookup,
1290 	.mkdir		= ovl_mkdir,
1291 	.symlink	= ovl_symlink,
1292 	.unlink		= ovl_unlink,
1293 	.rmdir		= ovl_rmdir,
1294 	.rename		= ovl_rename,
1295 	.link		= ovl_link,
1296 	.setattr	= ovl_setattr,
1297 	.create		= ovl_create,
1298 	.mknod		= ovl_mknod,
1299 	.permission	= ovl_permission,
1300 	.getattr	= ovl_getattr,
1301 	.listxattr	= ovl_listxattr,
1302 	.get_acl	= ovl_get_acl,
1303 	.update_time	= ovl_update_time,
1304 	.fileattr_get	= ovl_fileattr_get,
1305 	.fileattr_set	= ovl_fileattr_set,
1306 };
1307