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