xref: /linux/fs/overlayfs/dir.c (revision b6ebbac51bedf9e98e837688bc838f400196da5e)
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 <linux/fs.h>
11 #include <linux/namei.h>
12 #include <linux/xattr.h>
13 #include <linux/security.h>
14 #include <linux/cred.h>
15 #include "overlayfs.h"
16 
17 void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
18 {
19 	int err;
20 
21 	dget(wdentry);
22 	if (d_is_dir(wdentry))
23 		err = ovl_do_rmdir(wdir, wdentry);
24 	else
25 		err = ovl_do_unlink(wdir, wdentry);
26 	dput(wdentry);
27 
28 	if (err) {
29 		pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
30 		       wdentry, err);
31 	}
32 }
33 
34 struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
35 {
36 	struct dentry *temp;
37 	char name[20];
38 
39 	snprintf(name, sizeof(name), "#%lx", (unsigned long) dentry);
40 
41 	temp = lookup_one_len(name, workdir, strlen(name));
42 	if (!IS_ERR(temp) && temp->d_inode) {
43 		pr_err("overlayfs: workdir/%s already exists\n", name);
44 		dput(temp);
45 		temp = ERR_PTR(-EIO);
46 	}
47 
48 	return temp;
49 }
50 
51 /* caller holds i_mutex on workdir */
52 static struct dentry *ovl_whiteout(struct dentry *workdir,
53 				   struct dentry *dentry)
54 {
55 	int err;
56 	struct dentry *whiteout;
57 	struct inode *wdir = workdir->d_inode;
58 
59 	whiteout = ovl_lookup_temp(workdir, dentry);
60 	if (IS_ERR(whiteout))
61 		return whiteout;
62 
63 	err = ovl_do_whiteout(wdir, whiteout);
64 	if (err) {
65 		dput(whiteout);
66 		whiteout = ERR_PTR(err);
67 	}
68 
69 	return whiteout;
70 }
71 
72 int ovl_create_real(struct inode *dir, struct dentry *newdentry,
73 		    struct kstat *stat, const char *link,
74 		    struct dentry *hardlink, bool debug)
75 {
76 	int err;
77 
78 	if (newdentry->d_inode)
79 		return -ESTALE;
80 
81 	if (hardlink) {
82 		err = ovl_do_link(hardlink, dir, newdentry, debug);
83 	} else {
84 		switch (stat->mode & S_IFMT) {
85 		case S_IFREG:
86 			err = ovl_do_create(dir, newdentry, stat->mode, debug);
87 			break;
88 
89 		case S_IFDIR:
90 			err = ovl_do_mkdir(dir, newdentry, stat->mode, debug);
91 			break;
92 
93 		case S_IFCHR:
94 		case S_IFBLK:
95 		case S_IFIFO:
96 		case S_IFSOCK:
97 			err = ovl_do_mknod(dir, newdentry,
98 					   stat->mode, stat->rdev, debug);
99 			break;
100 
101 		case S_IFLNK:
102 			err = ovl_do_symlink(dir, newdentry, link, debug);
103 			break;
104 
105 		default:
106 			err = -EPERM;
107 		}
108 	}
109 	if (!err && WARN_ON(!newdentry->d_inode)) {
110 		/*
111 		 * Not quite sure if non-instantiated dentry is legal or not.
112 		 * VFS doesn't seem to care so check and warn here.
113 		 */
114 		err = -ENOENT;
115 	}
116 	return err;
117 }
118 
119 static int ovl_set_opaque(struct dentry *upperdentry)
120 {
121 	return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
122 }
123 
124 static void ovl_remove_opaque(struct dentry *upperdentry)
125 {
126 	int err;
127 
128 	err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE);
129 	if (err) {
130 		pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n",
131 			upperdentry->d_name.name, err);
132 	}
133 }
134 
135 static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
136 			 struct kstat *stat)
137 {
138 	int err;
139 	enum ovl_path_type type;
140 	struct path realpath;
141 	const struct cred *old_cred;
142 
143 	type = ovl_path_real(dentry, &realpath);
144 	old_cred = ovl_override_creds(dentry->d_sb);
145 	err = vfs_getattr(&realpath, stat);
146 	revert_creds(old_cred);
147 	if (err)
148 		return err;
149 
150 	stat->dev = dentry->d_sb->s_dev;
151 	stat->ino = dentry->d_inode->i_ino;
152 
153 	/*
154 	 * It's probably not worth it to count subdirs to get the
155 	 * correct link count.  nlink=1 seems to pacify 'find' and
156 	 * other utilities.
157 	 */
158 	if (OVL_TYPE_MERGE(type))
159 		stat->nlink = 1;
160 
161 	return 0;
162 }
163 
164 /* Common operations required to be done after creation of file on upper */
165 static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
166 			    struct dentry *newdentry, bool hardlink)
167 {
168 	ovl_dentry_version_inc(dentry->d_parent);
169 	ovl_dentry_update(dentry, newdentry);
170 	if (!hardlink) {
171 		ovl_inode_update(inode, d_inode(newdentry));
172 		ovl_copyattr(newdentry->d_inode, inode);
173 	} else {
174 		WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
175 		inc_nlink(inode);
176 	}
177 	d_instantiate(dentry, inode);
178 }
179 
180 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
181 			    struct kstat *stat, const char *link,
182 			    struct dentry *hardlink)
183 {
184 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
185 	struct inode *udir = upperdir->d_inode;
186 	struct dentry *newdentry;
187 	int err;
188 
189 	inode_lock_nested(udir, I_MUTEX_PARENT);
190 	newdentry = lookup_one_len(dentry->d_name.name, upperdir,
191 				   dentry->d_name.len);
192 	err = PTR_ERR(newdentry);
193 	if (IS_ERR(newdentry))
194 		goto out_unlock;
195 	err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
196 	if (err)
197 		goto out_dput;
198 
199 	ovl_instantiate(dentry, inode, newdentry, !!hardlink);
200 	newdentry = NULL;
201 out_dput:
202 	dput(newdentry);
203 out_unlock:
204 	inode_unlock(udir);
205 	return err;
206 }
207 
208 static int ovl_lock_rename_workdir(struct dentry *workdir,
209 				   struct dentry *upperdir)
210 {
211 	/* Workdir should not be the same as upperdir */
212 	if (workdir == upperdir)
213 		goto err;
214 
215 	/* Workdir should not be subdir of upperdir and vice versa */
216 	if (lock_rename(workdir, upperdir) != NULL)
217 		goto err_unlock;
218 
219 	return 0;
220 
221 err_unlock:
222 	unlock_rename(workdir, upperdir);
223 err:
224 	pr_err("overlayfs: failed to lock workdir+upperdir\n");
225 	return -EIO;
226 }
227 
228 static struct dentry *ovl_clear_empty(struct dentry *dentry,
229 				      struct list_head *list)
230 {
231 	struct dentry *workdir = ovl_workdir(dentry);
232 	struct inode *wdir = workdir->d_inode;
233 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
234 	struct inode *udir = upperdir->d_inode;
235 	struct path upperpath;
236 	struct dentry *upper;
237 	struct dentry *opaquedir;
238 	struct kstat stat;
239 	int err;
240 
241 	if (WARN_ON(!workdir))
242 		return ERR_PTR(-EROFS);
243 
244 	err = ovl_lock_rename_workdir(workdir, upperdir);
245 	if (err)
246 		goto out;
247 
248 	ovl_path_upper(dentry, &upperpath);
249 	err = vfs_getattr(&upperpath, &stat);
250 	if (err)
251 		goto out_unlock;
252 
253 	err = -ESTALE;
254 	if (!S_ISDIR(stat.mode))
255 		goto out_unlock;
256 	upper = upperpath.dentry;
257 	if (upper->d_parent->d_inode != udir)
258 		goto out_unlock;
259 
260 	opaquedir = ovl_lookup_temp(workdir, dentry);
261 	err = PTR_ERR(opaquedir);
262 	if (IS_ERR(opaquedir))
263 		goto out_unlock;
264 
265 	err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
266 	if (err)
267 		goto out_dput;
268 
269 	err = ovl_copy_xattr(upper, opaquedir);
270 	if (err)
271 		goto out_cleanup;
272 
273 	err = ovl_set_opaque(opaquedir);
274 	if (err)
275 		goto out_cleanup;
276 
277 	inode_lock(opaquedir->d_inode);
278 	err = ovl_set_attr(opaquedir, &stat);
279 	inode_unlock(opaquedir->d_inode);
280 	if (err)
281 		goto out_cleanup;
282 
283 	err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
284 	if (err)
285 		goto out_cleanup;
286 
287 	ovl_cleanup_whiteouts(upper, list);
288 	ovl_cleanup(wdir, upper);
289 	unlock_rename(workdir, upperdir);
290 
291 	/* dentry's upper doesn't match now, get rid of it */
292 	d_drop(dentry);
293 
294 	return opaquedir;
295 
296 out_cleanup:
297 	ovl_cleanup(wdir, opaquedir);
298 out_dput:
299 	dput(opaquedir);
300 out_unlock:
301 	unlock_rename(workdir, upperdir);
302 out:
303 	return ERR_PTR(err);
304 }
305 
306 static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
307 {
308 	int err;
309 	struct dentry *ret = NULL;
310 	enum ovl_path_type type = ovl_path_type(dentry);
311 	LIST_HEAD(list);
312 
313 	err = ovl_check_empty_dir(dentry, &list);
314 	if (err) {
315 		ret = ERR_PTR(err);
316 		goto out_free;
317 	}
318 
319 	/*
320 	 * When removing an empty opaque directory, then it makes no sense to
321 	 * replace it with an exact replica of itself.
322 	 *
323 	 * If no upperdentry then skip clearing whiteouts.
324 	 *
325 	 * Can race with copy-up, since we don't hold the upperdir mutex.
326 	 * Doesn't matter, since copy-up can't create a non-empty directory
327 	 * from an empty one.
328 	 */
329 	if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
330 		ret = ovl_clear_empty(dentry, &list);
331 
332 out_free:
333 	ovl_cache_free(&list);
334 
335 	return ret;
336 }
337 
338 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
339 				    struct kstat *stat, const char *link,
340 				    struct dentry *hardlink)
341 {
342 	struct dentry *workdir = ovl_workdir(dentry);
343 	struct inode *wdir = workdir->d_inode;
344 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
345 	struct inode *udir = upperdir->d_inode;
346 	struct dentry *upper;
347 	struct dentry *newdentry;
348 	int err;
349 
350 	if (WARN_ON(!workdir))
351 		return -EROFS;
352 
353 	err = ovl_lock_rename_workdir(workdir, upperdir);
354 	if (err)
355 		goto out;
356 
357 	newdentry = ovl_lookup_temp(workdir, dentry);
358 	err = PTR_ERR(newdentry);
359 	if (IS_ERR(newdentry))
360 		goto out_unlock;
361 
362 	upper = lookup_one_len(dentry->d_name.name, upperdir,
363 			       dentry->d_name.len);
364 	err = PTR_ERR(upper);
365 	if (IS_ERR(upper))
366 		goto out_dput;
367 
368 	err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
369 	if (err)
370 		goto out_dput2;
371 
372 	/*
373 	 * mode could have been mutilated due to umask (e.g. sgid directory)
374 	 */
375 	if (!hardlink &&
376 	    !S_ISLNK(stat->mode) && newdentry->d_inode->i_mode != stat->mode) {
377 		struct iattr attr = {
378 			.ia_valid = ATTR_MODE,
379 			.ia_mode = stat->mode,
380 		};
381 		inode_lock(newdentry->d_inode);
382 		err = notify_change(newdentry, &attr, NULL);
383 		inode_unlock(newdentry->d_inode);
384 		if (err)
385 			goto out_cleanup;
386 	}
387 
388 	if (!hardlink && S_ISDIR(stat->mode)) {
389 		err = ovl_set_opaque(newdentry);
390 		if (err)
391 			goto out_cleanup;
392 
393 		err = ovl_do_rename(wdir, newdentry, udir, upper,
394 				    RENAME_EXCHANGE);
395 		if (err)
396 			goto out_cleanup;
397 
398 		ovl_cleanup(wdir, upper);
399 	} else {
400 		err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
401 		if (err)
402 			goto out_cleanup;
403 	}
404 	ovl_instantiate(dentry, inode, newdentry, !!hardlink);
405 	newdentry = NULL;
406 out_dput2:
407 	dput(upper);
408 out_dput:
409 	dput(newdentry);
410 out_unlock:
411 	unlock_rename(workdir, upperdir);
412 out:
413 	return err;
414 
415 out_cleanup:
416 	ovl_cleanup(wdir, newdentry);
417 	goto out_dput2;
418 }
419 
420 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
421 			      struct kstat *stat, const char *link,
422 			      struct dentry *hardlink)
423 {
424 	int err;
425 	const struct cred *old_cred;
426 	struct cred *override_cred;
427 
428 	err = ovl_copy_up(dentry->d_parent);
429 	if (err)
430 		return err;
431 
432 	old_cred = ovl_override_creds(dentry->d_sb);
433 	err = -ENOMEM;
434 	override_cred = prepare_creds();
435 	if (override_cred) {
436 		override_cred->fsuid = inode->i_uid;
437 		override_cred->fsgid = inode->i_gid;
438 		put_cred(override_creds(override_cred));
439 		put_cred(override_cred);
440 
441 		if (!ovl_dentry_is_opaque(dentry))
442 			err = ovl_create_upper(dentry, inode, stat, link,
443 						hardlink);
444 		else
445 			err = ovl_create_over_whiteout(dentry, inode, stat,
446 							link, hardlink);
447 	}
448 	revert_creds(old_cred);
449 	if (!err) {
450 		struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
451 
452 		WARN_ON(inode->i_mode != realinode->i_mode);
453 		WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
454 		WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
455 	}
456 	return err;
457 }
458 
459 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
460 			     const char *link)
461 {
462 	int err;
463 	struct inode *inode;
464 	struct kstat stat = {
465 		.rdev = rdev,
466 	};
467 
468 	err = ovl_want_write(dentry);
469 	if (err)
470 		goto out;
471 
472 	err = -ENOMEM;
473 	inode = ovl_new_inode(dentry->d_sb, mode);
474 	if (!inode)
475 		goto out_drop_write;
476 
477 	inode_init_owner(inode, dentry->d_parent->d_inode, mode);
478 	stat.mode = inode->i_mode;
479 
480 	err = ovl_create_or_link(dentry, inode, &stat, link, NULL);
481 	if (err)
482 		iput(inode);
483 
484 out_drop_write:
485 	ovl_drop_write(dentry);
486 out:
487 	return err;
488 }
489 
490 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
491 		      bool excl)
492 {
493 	return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
494 }
495 
496 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
497 {
498 	return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
499 }
500 
501 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
502 		     dev_t rdev)
503 {
504 	/* Don't allow creation of "whiteout" on overlay */
505 	if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
506 		return -EPERM;
507 
508 	return ovl_create_object(dentry, mode, rdev, NULL);
509 }
510 
511 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
512 		       const char *link)
513 {
514 	return ovl_create_object(dentry, S_IFLNK, 0, link);
515 }
516 
517 static int ovl_link(struct dentry *old, struct inode *newdir,
518 		    struct dentry *new)
519 {
520 	int err;
521 	struct inode *inode;
522 
523 	err = ovl_want_write(old);
524 	if (err)
525 		goto out;
526 
527 	err = ovl_copy_up(old);
528 	if (err)
529 		goto out_drop_write;
530 
531 	inode = d_inode(old);
532 	ihold(inode);
533 
534 	err = ovl_create_or_link(new, inode, NULL, NULL, ovl_dentry_upper(old));
535 	if (err)
536 		iput(inode);
537 
538 out_drop_write:
539 	ovl_drop_write(old);
540 out:
541 	return err;
542 }
543 
544 static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
545 {
546 	struct dentry *workdir = ovl_workdir(dentry);
547 	struct inode *wdir = workdir->d_inode;
548 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
549 	struct inode *udir = upperdir->d_inode;
550 	struct dentry *whiteout;
551 	struct dentry *upper;
552 	struct dentry *opaquedir = NULL;
553 	int err;
554 	int flags = 0;
555 
556 	if (WARN_ON(!workdir))
557 		return -EROFS;
558 
559 	if (is_dir) {
560 		opaquedir = ovl_check_empty_and_clear(dentry);
561 		err = PTR_ERR(opaquedir);
562 		if (IS_ERR(opaquedir))
563 			goto out;
564 	}
565 
566 	err = ovl_lock_rename_workdir(workdir, upperdir);
567 	if (err)
568 		goto out_dput;
569 
570 	upper = lookup_one_len(dentry->d_name.name, upperdir,
571 			       dentry->d_name.len);
572 	err = PTR_ERR(upper);
573 	if (IS_ERR(upper))
574 		goto out_unlock;
575 
576 	err = -ESTALE;
577 	if ((opaquedir && upper != opaquedir) ||
578 	    (!opaquedir && ovl_dentry_upper(dentry) &&
579 	     upper != ovl_dentry_upper(dentry))) {
580 		goto out_dput_upper;
581 	}
582 
583 	whiteout = ovl_whiteout(workdir, dentry);
584 	err = PTR_ERR(whiteout);
585 	if (IS_ERR(whiteout))
586 		goto out_dput_upper;
587 
588 	if (d_is_dir(upper))
589 		flags = RENAME_EXCHANGE;
590 
591 	err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
592 	if (err)
593 		goto kill_whiteout;
594 	if (flags)
595 		ovl_cleanup(wdir, upper);
596 
597 	ovl_dentry_version_inc(dentry->d_parent);
598 out_d_drop:
599 	d_drop(dentry);
600 	dput(whiteout);
601 out_dput_upper:
602 	dput(upper);
603 out_unlock:
604 	unlock_rename(workdir, upperdir);
605 out_dput:
606 	dput(opaquedir);
607 out:
608 	return err;
609 
610 kill_whiteout:
611 	ovl_cleanup(wdir, whiteout);
612 	goto out_d_drop;
613 }
614 
615 static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
616 {
617 	struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
618 	struct inode *dir = upperdir->d_inode;
619 	struct dentry *upper;
620 	int err;
621 
622 	inode_lock_nested(dir, I_MUTEX_PARENT);
623 	upper = lookup_one_len(dentry->d_name.name, upperdir,
624 			       dentry->d_name.len);
625 	err = PTR_ERR(upper);
626 	if (IS_ERR(upper))
627 		goto out_unlock;
628 
629 	err = -ESTALE;
630 	if (upper == ovl_dentry_upper(dentry)) {
631 		if (is_dir)
632 			err = vfs_rmdir(dir, upper);
633 		else
634 			err = vfs_unlink(dir, upper, NULL);
635 		ovl_dentry_version_inc(dentry->d_parent);
636 	}
637 	dput(upper);
638 
639 	/*
640 	 * Keeping this dentry hashed would mean having to release
641 	 * upperpath/lowerpath, which could only be done if we are the
642 	 * sole user of this dentry.  Too tricky...  Just unhash for
643 	 * now.
644 	 */
645 	if (!err)
646 		d_drop(dentry);
647 out_unlock:
648 	inode_unlock(dir);
649 
650 	return err;
651 }
652 
653 static inline int ovl_check_sticky(struct dentry *dentry)
654 {
655 	struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode;
656 	struct inode *inode = ovl_dentry_real(dentry)->d_inode;
657 
658 	if (check_sticky(dir, inode))
659 		return -EPERM;
660 
661 	return 0;
662 }
663 
664 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
665 {
666 	enum ovl_path_type type;
667 	int err;
668 	const struct cred *old_cred;
669 
670 
671 	err = ovl_check_sticky(dentry);
672 	if (err)
673 		goto out;
674 
675 	err = ovl_want_write(dentry);
676 	if (err)
677 		goto out;
678 
679 	err = ovl_copy_up(dentry->d_parent);
680 	if (err)
681 		goto out_drop_write;
682 
683 	type = ovl_path_type(dentry);
684 
685 	old_cred = ovl_override_creds(dentry->d_sb);
686 	if (OVL_TYPE_PURE_UPPER(type))
687 		err = ovl_remove_upper(dentry, is_dir);
688 	else
689 		err = ovl_remove_and_whiteout(dentry, is_dir);
690 	revert_creds(old_cred);
691 	if (!err) {
692 		if (is_dir)
693 			clear_nlink(dentry->d_inode);
694 		else
695 			drop_nlink(dentry->d_inode);
696 	}
697 out_drop_write:
698 	ovl_drop_write(dentry);
699 out:
700 	return err;
701 }
702 
703 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
704 {
705 	return ovl_do_remove(dentry, false);
706 }
707 
708 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
709 {
710 	return ovl_do_remove(dentry, true);
711 }
712 
713 static int ovl_rename2(struct inode *olddir, struct dentry *old,
714 		       struct inode *newdir, struct dentry *new,
715 		       unsigned int flags)
716 {
717 	int err;
718 	enum ovl_path_type old_type;
719 	enum ovl_path_type new_type;
720 	struct dentry *old_upperdir;
721 	struct dentry *new_upperdir;
722 	struct dentry *olddentry;
723 	struct dentry *newdentry;
724 	struct dentry *trap;
725 	bool old_opaque;
726 	bool new_opaque;
727 	bool cleanup_whiteout = false;
728 	bool overwrite = !(flags & RENAME_EXCHANGE);
729 	bool is_dir = d_is_dir(old);
730 	bool new_is_dir = false;
731 	struct dentry *opaquedir = NULL;
732 	const struct cred *old_cred = NULL;
733 
734 	err = -EINVAL;
735 	if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
736 		goto out;
737 
738 	flags &= ~RENAME_NOREPLACE;
739 
740 	err = ovl_check_sticky(old);
741 	if (err)
742 		goto out;
743 
744 	/* Don't copy up directory trees */
745 	old_type = ovl_path_type(old);
746 	err = -EXDEV;
747 	if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
748 		goto out;
749 
750 	if (new->d_inode) {
751 		err = ovl_check_sticky(new);
752 		if (err)
753 			goto out;
754 
755 		if (d_is_dir(new))
756 			new_is_dir = true;
757 
758 		new_type = ovl_path_type(new);
759 		err = -EXDEV;
760 		if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
761 			goto out;
762 
763 		err = 0;
764 		if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
765 			if (ovl_dentry_lower(old)->d_inode ==
766 			    ovl_dentry_lower(new)->d_inode)
767 				goto out;
768 		}
769 		if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
770 			if (ovl_dentry_upper(old)->d_inode ==
771 			    ovl_dentry_upper(new)->d_inode)
772 				goto out;
773 		}
774 	} else {
775 		if (ovl_dentry_is_opaque(new))
776 			new_type = __OVL_PATH_UPPER;
777 		else
778 			new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
779 	}
780 
781 	err = ovl_want_write(old);
782 	if (err)
783 		goto out;
784 
785 	err = ovl_copy_up(old);
786 	if (err)
787 		goto out_drop_write;
788 
789 	err = ovl_copy_up(new->d_parent);
790 	if (err)
791 		goto out_drop_write;
792 	if (!overwrite) {
793 		err = ovl_copy_up(new);
794 		if (err)
795 			goto out_drop_write;
796 	}
797 
798 	old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
799 	new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
800 
801 	old_cred = ovl_override_creds(old->d_sb);
802 
803 	if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
804 		opaquedir = ovl_check_empty_and_clear(new);
805 		err = PTR_ERR(opaquedir);
806 		if (IS_ERR(opaquedir)) {
807 			opaquedir = NULL;
808 			goto out_revert_creds;
809 		}
810 	}
811 
812 	if (overwrite) {
813 		if (old_opaque) {
814 			if (new->d_inode || !new_opaque) {
815 				/* Whiteout source */
816 				flags |= RENAME_WHITEOUT;
817 			} else {
818 				/* Switch whiteouts */
819 				flags |= RENAME_EXCHANGE;
820 			}
821 		} else if (is_dir && !new->d_inode && new_opaque) {
822 			flags |= RENAME_EXCHANGE;
823 			cleanup_whiteout = true;
824 		}
825 	}
826 
827 	old_upperdir = ovl_dentry_upper(old->d_parent);
828 	new_upperdir = ovl_dentry_upper(new->d_parent);
829 
830 	trap = lock_rename(new_upperdir, old_upperdir);
831 
832 
833 	olddentry = lookup_one_len(old->d_name.name, old_upperdir,
834 				   old->d_name.len);
835 	err = PTR_ERR(olddentry);
836 	if (IS_ERR(olddentry))
837 		goto out_unlock;
838 
839 	err = -ESTALE;
840 	if (olddentry != ovl_dentry_upper(old))
841 		goto out_dput_old;
842 
843 	newdentry = lookup_one_len(new->d_name.name, new_upperdir,
844 				   new->d_name.len);
845 	err = PTR_ERR(newdentry);
846 	if (IS_ERR(newdentry))
847 		goto out_dput_old;
848 
849 	err = -ESTALE;
850 	if (ovl_dentry_upper(new)) {
851 		if (opaquedir) {
852 			if (newdentry != opaquedir)
853 				goto out_dput;
854 		} else {
855 			if (newdentry != ovl_dentry_upper(new))
856 				goto out_dput;
857 		}
858 	} else {
859 		if (!d_is_negative(newdentry) &&
860 		    (!new_opaque || !ovl_is_whiteout(newdentry)))
861 			goto out_dput;
862 	}
863 
864 	if (olddentry == trap)
865 		goto out_dput;
866 	if (newdentry == trap)
867 		goto out_dput;
868 
869 	if (is_dir && !old_opaque && new_opaque) {
870 		err = ovl_set_opaque(olddentry);
871 		if (err)
872 			goto out_dput;
873 	}
874 	if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
875 		err = ovl_set_opaque(newdentry);
876 		if (err)
877 			goto out_dput;
878 	}
879 
880 	if (old_opaque || new_opaque) {
881 		err = ovl_do_rename(old_upperdir->d_inode, olddentry,
882 				    new_upperdir->d_inode, newdentry,
883 				    flags);
884 	} else {
885 		/* No debug for the plain case */
886 		BUG_ON(flags & ~RENAME_EXCHANGE);
887 		err = vfs_rename(old_upperdir->d_inode, olddentry,
888 				 new_upperdir->d_inode, newdentry,
889 				 NULL, flags);
890 	}
891 
892 	if (err) {
893 		if (is_dir && !old_opaque && new_opaque)
894 			ovl_remove_opaque(olddentry);
895 		if (!overwrite && new_is_dir && old_opaque && !new_opaque)
896 			ovl_remove_opaque(newdentry);
897 		goto out_dput;
898 	}
899 
900 	if (is_dir && old_opaque && !new_opaque)
901 		ovl_remove_opaque(olddentry);
902 	if (!overwrite && new_is_dir && !old_opaque && new_opaque)
903 		ovl_remove_opaque(newdentry);
904 
905 	/*
906 	 * Old dentry now lives in different location. Dentries in
907 	 * lowerstack are stale. We cannot drop them here because
908 	 * access to them is lockless. This could be only pure upper
909 	 * or opaque directory - numlower is zero. Or upper non-dir
910 	 * entry - its pureness is tracked by flag opaque.
911 	 */
912 	if (old_opaque != new_opaque) {
913 		ovl_dentry_set_opaque(old, new_opaque);
914 		if (!overwrite)
915 			ovl_dentry_set_opaque(new, old_opaque);
916 	}
917 
918 	if (cleanup_whiteout)
919 		ovl_cleanup(old_upperdir->d_inode, newdentry);
920 
921 	ovl_dentry_version_inc(old->d_parent);
922 	ovl_dentry_version_inc(new->d_parent);
923 
924 out_dput:
925 	dput(newdentry);
926 out_dput_old:
927 	dput(olddentry);
928 out_unlock:
929 	unlock_rename(new_upperdir, old_upperdir);
930 out_revert_creds:
931 	revert_creds(old_cred);
932 out_drop_write:
933 	ovl_drop_write(old);
934 out:
935 	dput(opaquedir);
936 	return err;
937 }
938 
939 const struct inode_operations ovl_dir_inode_operations = {
940 	.lookup		= ovl_lookup,
941 	.mkdir		= ovl_mkdir,
942 	.symlink	= ovl_symlink,
943 	.unlink		= ovl_unlink,
944 	.rmdir		= ovl_rmdir,
945 	.rename2	= ovl_rename2,
946 	.link		= ovl_link,
947 	.setattr	= ovl_setattr,
948 	.create		= ovl_create,
949 	.mknod		= ovl_mknod,
950 	.permission	= ovl_permission,
951 	.getattr	= ovl_dir_getattr,
952 	.setxattr	= generic_setxattr,
953 	.getxattr	= ovl_getxattr,
954 	.listxattr	= ovl_listxattr,
955 	.removexattr	= ovl_removexattr,
956 	.get_acl	= ovl_get_acl,
957 	.update_time	= ovl_update_time,
958 };
959