1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 Novell Inc.
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"
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");
24 static int ovl_set_redirect(struct dentry *dentry, bool samedir);
26 int ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
31 if (d_is_dir(wdentry))
32 err = ovl_do_rmdir(wdir, wdentry);
34 err = ovl_do_unlink(wdir, wdentry);
38 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
45 static struct dentry *ovl_lookup_temp(struct dentry *workdir)
49 static atomic_t temp_id = ATOMIC_INIT(0);
51 /* counter is allowed to wrap, since temp dentries are ephemeral */
52 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
54 temp = lookup_one_len(name, workdir, strlen(name));
55 if (!IS_ERR(temp) && temp->d_inode) {
56 pr_err("overlayfs: workdir/%s already exists\n", name);
64 /* caller holds i_mutex on workdir */
65 static struct dentry *ovl_whiteout(struct dentry *workdir)
68 struct dentry *whiteout;
69 struct inode *wdir = workdir->d_inode;
71 whiteout = ovl_lookup_temp(workdir);
75 err = ovl_do_whiteout(wdir, whiteout);
78 whiteout = ERR_PTR(err);
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)
88 struct inode *wdir = workdir->d_inode;
89 struct dentry *whiteout;
93 whiteout = ovl_whiteout(workdir);
94 err = PTR_ERR(whiteout);
99 flags = RENAME_EXCHANGE;
101 err = ovl_do_rename(wdir, whiteout, dir, dentry, flags);
105 ovl_cleanup(wdir, dentry);
112 ovl_cleanup(wdir, whiteout);
116 static int ovl_mkdir_real(struct inode *dir, struct dentry **newdentry,
120 struct dentry *d, *dentry = *newdentry;
122 err = ovl_do_mkdir(dir, dentry, mode);
126 if (likely(!d_unhashed(dentry)))
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.
134 d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
137 pr_warn("overlayfs: failed lookup after mkdir (%pd2, err=%i).\n",
147 struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
148 struct ovl_cattr *attr)
152 if (IS_ERR(newdentry))
156 if (newdentry->d_inode)
159 if (attr->hardlink) {
160 err = ovl_do_link(attr->hardlink, dir, newdentry);
162 switch (attr->mode & S_IFMT) {
164 err = ovl_do_create(dir, newdentry, attr->mode);
168 /* mkdir is special... */
169 err = ovl_mkdir_real(dir, &newdentry, attr->mode);
176 err = ovl_do_mknod(dir, newdentry, attr->mode,
181 err = ovl_do_symlink(dir, newdentry, attr->link);
188 if (!err && WARN_ON(!newdentry->d_inode)) {
190 * Not quite sure if non-instantiated dentry is legal or not.
191 * VFS doesn't seem to care so check and warn here.
203 struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr)
205 return ovl_create_real(d_inode(workdir), ovl_lookup_temp(workdir),
209 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
214 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
216 ovl_dentry_set_opaque(dentry);
221 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
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.
228 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
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.
236 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
237 struct dentry *newdentry, bool hardlink)
239 struct ovl_inode_params oip = {
240 .upperdentry = newdentry,
244 ovl_dir_modified(dentry->d_parent, false);
245 ovl_dentry_set_upper_alias(dentry);
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.
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.
259 inode = ovl_get_inode(dentry->d_sb, &oip);
261 return PTR_ERR(inode);
263 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
268 d_instantiate(dentry, inode);
269 if (inode != oip.newinode) {
270 pr_warn_ratelimited("overlayfs: newly created inode found in cache (%pd2)\n",
274 /* Force lookup of new upper hardlink to find its lower */
281 static bool ovl_type_merge(struct dentry *dentry)
283 return OVL_TYPE_MERGE(ovl_path_type(dentry));
286 static bool ovl_type_origin(struct dentry *dentry)
288 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
291 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
292 struct ovl_cattr *attr)
294 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
295 struct inode *udir = upperdir->d_inode;
296 struct dentry *newdentry;
299 if (!attr->hardlink && !IS_POSIXACL(udir))
300 attr->mode &= ~current_umask();
302 inode_lock_nested(udir, I_MUTEX_PARENT);
303 newdentry = ovl_create_real(udir,
304 lookup_one_len(dentry->d_name.name,
308 err = PTR_ERR(newdentry);
309 if (IS_ERR(newdentry))
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);
317 err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
325 ovl_cleanup(udir, newdentry);
330 static struct dentry *ovl_clear_empty(struct dentry *dentry,
331 struct list_head *list)
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;
343 if (WARN_ON(!workdir))
344 return ERR_PTR(-EROFS);
346 err = ovl_lock_rename_workdir(workdir, upperdir);
350 ovl_path_upper(dentry, &upperpath);
351 err = vfs_getattr(&upperpath, &stat,
352 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
357 if (!S_ISDIR(stat.mode))
359 upper = upperpath.dentry;
360 if (upper->d_parent->d_inode != udir)
363 opaquedir = ovl_create_temp(workdir, OVL_CATTR(stat.mode));
364 err = PTR_ERR(opaquedir);
365 if (IS_ERR(opaquedir))
368 err = ovl_copy_xattr(upper, opaquedir);
372 err = ovl_set_opaque(dentry, opaquedir);
376 inode_lock(opaquedir->d_inode);
377 err = ovl_set_attr(opaquedir, &stat);
378 inode_unlock(opaquedir->d_inode);
382 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
386 ovl_cleanup_whiteouts(upper, list);
387 ovl_cleanup(wdir, upper);
388 unlock_rename(workdir, upperdir);
390 /* dentry's upper doesn't match now, get rid of it */
396 ovl_cleanup(wdir, opaquedir);
399 unlock_rename(workdir, upperdir);
404 static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
405 const struct posix_acl *acl)
411 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
414 size = posix_acl_xattr_size(acl->a_count);
415 buffer = kmalloc(size, GFP_KERNEL);
419 err = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
423 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
429 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
430 struct ovl_cattr *cattr)
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;
439 struct posix_acl *acl, *default_acl;
440 bool hardlink = !!cattr->hardlink;
442 if (WARN_ON(!workdir))
446 err = posix_acl_create(dentry->d_parent->d_inode,
447 &cattr->mode, &default_acl, &acl);
452 err = ovl_lock_rename_workdir(workdir, upperdir);
456 upper = lookup_one_len(dentry->d_name.name, upperdir,
458 err = PTR_ERR(upper);
463 if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
466 newdentry = ovl_create_temp(workdir, cattr);
467 err = PTR_ERR(newdentry);
468 if (IS_ERR(newdentry))
472 * mode could have been mutilated due to umask (e.g. sgid directory)
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,
481 inode_lock(newdentry->d_inode);
482 err = notify_change(newdentry, &attr, NULL);
483 inode_unlock(newdentry->d_inode);
488 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
493 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
499 if (!hardlink && S_ISDIR(cattr->mode)) {
500 err = ovl_set_opaque(dentry, newdentry);
504 err = ovl_do_rename(wdir, newdentry, udir, upper,
509 ovl_cleanup(wdir, upper);
511 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
515 err = ovl_instantiate(dentry, inode, newdentry, hardlink);
521 unlock_rename(workdir, upperdir);
524 posix_acl_release(acl);
525 posix_acl_release(default_acl);
530 ovl_cleanup(wdir, newdentry);
535 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
536 struct ovl_cattr *attr, bool origin)
539 const struct cred *old_cred;
540 struct cred *override_cred;
541 struct dentry *parent = dentry->d_parent;
543 err = ovl_copy_up(parent);
547 old_cred = ovl_override_creds(dentry->d_sb);
550 * When linking a file with copy up origin into a new parent, mark the
551 * new parent dir "impure".
554 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
556 goto out_revert_creds;
560 override_cred = prepare_creds();
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,
569 put_cred(override_cred);
570 goto out_revert_creds;
573 put_cred(override_creds(override_cred));
574 put_cred(override_cred);
576 if (!ovl_dentry_is_whiteout(dentry))
577 err = ovl_create_upper(dentry, inode, attr);
579 err = ovl_create_over_whiteout(dentry, inode, attr);
582 revert_creds(old_cred);
586 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
591 struct ovl_cattr attr = {
596 err = ovl_want_write(dentry);
600 /* Preallocate inode to be used by ovl_get_inode() */
602 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
606 spin_lock(&inode->i_lock);
607 inode->i_state |= I_CREATING;
608 spin_unlock(&inode->i_lock);
610 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
611 attr.mode = inode->i_mode;
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))
619 ovl_drop_write(dentry);
624 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
627 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
630 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
632 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
635 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
638 /* Don't allow creation of "whiteout" on overlay */
639 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
642 return ovl_create_object(dentry, mode, rdev, NULL);
645 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
648 return ovl_create_object(dentry, S_IFLNK, 0, link);
651 static int ovl_set_link_redirect(struct dentry *dentry)
653 const struct cred *old_cred;
656 old_cred = ovl_override_creds(dentry->d_sb);
657 err = ovl_set_redirect(dentry, false);
658 revert_creds(old_cred);
663 static int ovl_link(struct dentry *old, struct inode *newdir,
669 err = ovl_want_write(old);
673 err = ovl_copy_up(old);
677 err = ovl_copy_up(new->d_parent);
681 if (ovl_is_metacopy_dentry(old)) {
682 err = ovl_set_link_redirect(old);
687 err = ovl_nlink_start(old);
691 inode = d_inode(old);
694 err = ovl_create_or_link(new, inode,
695 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
696 ovl_type_origin(old));
707 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
709 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
712 static int ovl_remove_and_whiteout(struct dentry *dentry,
713 struct list_head *list)
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;
721 if (WARN_ON(!workdir))
724 if (!list_empty(list)) {
725 opaquedir = ovl_clear_empty(dentry, list);
726 err = PTR_ERR(opaquedir);
727 if (IS_ERR(opaquedir))
731 err = ovl_lock_rename_workdir(workdir, upperdir);
735 upper = lookup_one_len(dentry->d_name.name, upperdir,
737 err = PTR_ERR(upper);
742 if ((opaquedir && upper != opaquedir) ||
743 (!opaquedir && ovl_dentry_upper(dentry) &&
744 !ovl_matches_upper(dentry, upper))) {
748 err = ovl_cleanup_and_whiteout(workdir, d_inode(upperdir), upper);
752 ovl_dir_modified(dentry->d_parent, true);
758 unlock_rename(workdir, upperdir);
765 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
766 struct list_head *list)
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;
774 if (!list_empty(list)) {
775 opaquedir = ovl_clear_empty(dentry, list);
776 err = PTR_ERR(opaquedir);
777 if (IS_ERR(opaquedir))
781 inode_lock_nested(dir, I_MUTEX_PARENT);
782 upper = lookup_one_len(dentry->d_name.name, upperdir,
784 err = PTR_ERR(upper);
789 if ((opaquedir && upper != opaquedir) ||
790 (!opaquedir && !ovl_matches_upper(dentry, upper)))
794 err = vfs_rmdir(dir, upper);
796 err = vfs_unlink(dir, upper, NULL);
797 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
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
816 static bool ovl_pure_upper(struct dentry *dentry)
818 return !ovl_dentry_lower(dentry) &&
819 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
822 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
825 const struct cred *old_cred;
826 struct dentry *upperdentry;
827 bool lower_positive = ovl_lower_positive(dentry);
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);
837 err = ovl_want_write(dentry);
841 err = ovl_copy_up(dentry->d_parent);
845 err = ovl_nlink_start(dentry);
849 old_cred = ovl_override_creds(dentry->d_sb);
851 err = ovl_remove_upper(dentry, is_dir, &list);
853 err = ovl_remove_and_whiteout(dentry, &list);
854 revert_creds(old_cred);
857 clear_nlink(dentry->d_inode);
859 drop_nlink(dentry->d_inode);
861 ovl_nlink_end(dentry);
866 * Note: we fail to update ctime if there was no copy-up, only a
869 upperdentry = ovl_dentry_upper(dentry);
871 ovl_copyattr(d_inode(upperdentry), d_inode(dentry));
874 ovl_drop_write(dentry);
876 ovl_cache_free(&list);
880 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
882 return ovl_do_remove(dentry, false);
885 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
887 return ovl_do_remove(dentry, true);
890 static bool ovl_type_merge_or_lower(struct dentry *dentry)
892 enum ovl_path_type type = ovl_path_type(dentry);
894 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
897 static bool ovl_can_move(struct dentry *dentry)
899 return ovl_redirect_dir(dentry->d_sb) ||
900 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
903 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
906 struct dentry *d, *tmp;
907 int buflen = ovl_redirect_max + 1;
910 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
915 buf = ret = kmalloc(buflen, GFP_KERNEL);
921 for (d = dget(dentry); !IS_ROOT(d);) {
925 spin_lock(&d->d_lock);
926 name = ovl_dentry_get_redirect(d);
928 thislen = strlen(name);
930 name = d->d_name.name;
931 thislen = d->d_name.len;
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);
942 memcpy(&buf[buflen], name, thislen);
943 tmp = dget_dlock(d->d_parent);
944 spin_unlock(&d->d_lock);
949 /* Absolute redirect: finished */
950 if (buf[buflen] == '/')
955 ret = kstrdup(&buf[buflen], GFP_KERNEL);
960 return ret ? ret : ERR_PTR(-ENOMEM);
963 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
965 struct dentry *lowerdentry;
970 if (d_is_dir(dentry))
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
982 * So without optimizing too much, just check if lower is
983 * a hard link or not. If lower is hard link, put absolute
986 lowerdentry = ovl_dentry_lower(dentry);
987 return (d_inode(lowerdentry)->i_nlink > 1);
990 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
993 const char *redirect = ovl_dentry_get_redirect(dentry);
994 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
996 if (redirect && (!absolute_redirect || redirect[0] == '/'))
999 redirect = ovl_get_redirect(dentry, absolute_redirect);
1000 if (IS_ERR(redirect))
1001 return PTR_ERR(redirect);
1003 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
1005 redirect, strlen(redirect), -EXDEV);
1007 spin_lock(&dentry->d_lock);
1008 ovl_dentry_set_redirect(dentry, redirect);
1009 spin_unlock(&dentry->d_lock);
1012 pr_warn_ratelimited("overlayfs: failed to set redirect (%i)\n",
1014 /* Fall back to userspace copy-up */
1020 static int ovl_rename(struct inode *olddir, struct dentry *old,
1021 struct inode *newdir, struct dentry *new,
1025 struct dentry *old_upperdir;
1026 struct dentry *new_upperdir;
1027 struct dentry *olddentry;
1028 struct dentry *newdentry;
1029 struct dentry *trap;
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;
1043 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1046 flags &= ~RENAME_NOREPLACE;
1048 /* Don't copy up directory trees */
1050 if (!ovl_can_move(old))
1052 if (!overwrite && !ovl_can_move(new))
1055 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1056 err = ovl_check_empty_dir(new, &list);
1062 if (ovl_lower_positive(old)) {
1063 if (!ovl_dentry_is_whiteout(new)) {
1064 /* Whiteout source */
1065 flags |= RENAME_WHITEOUT;
1067 /* Switch whiteouts */
1068 flags |= RENAME_EXCHANGE;
1070 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1071 flags |= RENAME_EXCHANGE;
1072 cleanup_whiteout = true;
1076 err = ovl_want_write(old);
1080 err = ovl_copy_up(old);
1082 goto out_drop_write;
1084 err = ovl_copy_up(new->d_parent);
1086 goto out_drop_write;
1088 err = ovl_copy_up(new);
1090 goto out_drop_write;
1091 } else if (d_inode(new)) {
1092 err = ovl_nlink_start(new);
1094 goto out_drop_write;
1096 update_nlink = true;
1099 old_cred = ovl_override_creds(old->d_sb);
1101 if (!list_empty(&list)) {
1102 opaquedir = ovl_clear_empty(new, &list);
1103 err = PTR_ERR(opaquedir);
1104 if (IS_ERR(opaquedir)) {
1106 goto out_revert_creds;
1110 old_upperdir = ovl_dentry_upper(old->d_parent);
1111 new_upperdir = ovl_dentry_upper(new->d_parent);
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.
1120 if (ovl_type_origin(old)) {
1121 err = ovl_set_impure(new->d_parent, new_upperdir);
1123 goto out_revert_creds;
1125 if (!overwrite && ovl_type_origin(new)) {
1126 err = ovl_set_impure(old->d_parent, old_upperdir);
1128 goto out_revert_creds;
1132 trap = lock_rename(new_upperdir, old_upperdir);
1134 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
1136 err = PTR_ERR(olddentry);
1137 if (IS_ERR(olddentry))
1141 if (!ovl_matches_upper(old, olddentry))
1144 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1146 err = PTR_ERR(newdentry);
1147 if (IS_ERR(newdentry))
1150 old_opaque = ovl_dentry_is_opaque(old);
1151 new_opaque = ovl_dentry_is_opaque(new);
1154 if (d_inode(new) && ovl_dentry_upper(new)) {
1156 if (newdentry != opaquedir)
1159 if (!ovl_matches_upper(new, newdentry))
1163 if (!d_is_negative(newdentry) &&
1164 (!new_opaque || !ovl_is_whiteout(newdentry)))
1168 if (olddentry == trap)
1170 if (newdentry == trap)
1173 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
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);
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);
1192 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1193 new_upperdir->d_inode, newdentry, flags);
1197 if (cleanup_whiteout)
1198 ovl_cleanup(old_upperdir->d_inode, newdentry);
1200 if (overwrite && d_inode(new)) {
1202 clear_nlink(d_inode(new));
1204 drop_nlink(d_inode(new));
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)));
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));
1222 unlock_rename(new_upperdir, old_upperdir);
1224 revert_creds(old_cred);
1228 ovl_drop_write(old);
1231 ovl_cache_free(&list);
1235 const struct inode_operations ovl_dir_inode_operations = {
1236 .lookup = ovl_lookup,
1238 .symlink = ovl_symlink,
1239 .unlink = ovl_unlink,
1241 .rename = ovl_rename,
1243 .setattr = ovl_setattr,
1244 .create = ovl_create,
1246 .permission = ovl_permission,
1247 .getattr = ovl_getattr,
1248 .listxattr = ovl_listxattr,
1249 .get_acl = ovl_get_acl,
1250 .update_time = ovl_update_time,