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