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