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