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("cleanup of '%pd2' failed (%i)\n",
45 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("workdir/%s already exists\n", name);
64 /* caller holds i_mutex on workdir */
65 static struct dentry *ovl_whiteout(struct ovl_fs *ofs)
68 struct dentry *whiteout;
69 struct dentry *workdir = ofs->workdir;
70 struct inode *wdir = workdir->d_inode;
73 whiteout = ovl_lookup_temp(workdir);
77 err = ovl_do_whiteout(wdir, whiteout);
80 whiteout = ERR_PTR(err);
83 ofs->whiteout = whiteout;
86 if (ofs->share_whiteout) {
87 whiteout = ovl_lookup_temp(workdir);
91 err = ovl_do_link(ofs->whiteout, wdir, whiteout);
96 pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%i)\n",
97 ofs->whiteout->d_inode->i_nlink, err);
98 ofs->share_whiteout = false;
102 whiteout = ofs->whiteout;
103 ofs->whiteout = NULL;
108 /* Caller must hold i_mutex on both workdir and dir */
109 int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
110 struct dentry *dentry)
112 struct inode *wdir = ofs->workdir->d_inode;
113 struct dentry *whiteout;
117 whiteout = ovl_whiteout(ofs);
118 err = PTR_ERR(whiteout);
119 if (IS_ERR(whiteout))
122 if (d_is_dir(dentry))
123 flags = RENAME_EXCHANGE;
125 err = ovl_do_rename(wdir, whiteout, dir, dentry, flags);
129 ovl_cleanup(wdir, dentry);
136 ovl_cleanup(wdir, whiteout);
140 static int ovl_mkdir_real(struct inode *dir, struct dentry **newdentry,
144 struct dentry *d, *dentry = *newdentry;
146 err = ovl_do_mkdir(dir, dentry, mode);
150 if (likely(!d_unhashed(dentry)))
154 * vfs_mkdir() may succeed and leave the dentry passed
155 * to it unhashed and negative. If that happens, try to
156 * lookup a new hashed and positive dentry.
158 d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
161 pr_warn("failed lookup after mkdir (%pd2, err=%i).\n",
171 struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
172 struct ovl_cattr *attr)
176 if (IS_ERR(newdentry))
180 if (newdentry->d_inode)
183 if (attr->hardlink) {
184 err = ovl_do_link(attr->hardlink, dir, newdentry);
186 switch (attr->mode & S_IFMT) {
188 err = ovl_do_create(dir, newdentry, attr->mode);
192 /* mkdir is special... */
193 err = ovl_mkdir_real(dir, &newdentry, attr->mode);
200 err = ovl_do_mknod(dir, newdentry, attr->mode,
205 err = ovl_do_symlink(dir, newdentry, attr->link);
212 if (!err && WARN_ON(!newdentry->d_inode)) {
214 * Not quite sure if non-instantiated dentry is legal or not.
215 * VFS doesn't seem to care so check and warn here.
227 struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr)
229 return ovl_create_real(d_inode(workdir), ovl_lookup_temp(workdir),
233 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
236 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
239 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
241 ovl_dentry_set_opaque(dentry);
246 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
249 * Fail with -EIO when trying to create opaque dir and upper doesn't
250 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
251 * return a specific error for noxattr case.
253 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
257 * Common operations required to be done after creation of file on upper.
258 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
259 * may not use to instantiate the new dentry.
261 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
262 struct dentry *newdentry, bool hardlink)
264 struct ovl_inode_params oip = {
265 .upperdentry = newdentry,
269 ovl_dir_modified(dentry->d_parent, false);
270 ovl_dentry_set_upper_alias(dentry);
271 ovl_dentry_update_reval(dentry, newdentry,
272 DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
276 * ovl_obtain_alias() can be called after ovl_create_real()
277 * and before we get here, so we may get an inode from cache
278 * with the same real upperdentry that is not the inode we
279 * pre-allocated. In this case we will use the cached inode
280 * to instantiate the new dentry.
282 * XXX: if we ever use ovl_obtain_alias() to decode directory
283 * file handles, need to use ovl_get_inode_locked() and
284 * d_instantiate_new() here to prevent from creating two
285 * hashed directory inode aliases.
287 inode = ovl_get_inode(dentry->d_sb, &oip);
289 return PTR_ERR(inode);
290 if (inode == oip.newinode)
291 ovl_set_flag(OVL_UPPERDATA, inode);
293 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
298 d_instantiate(dentry, inode);
299 if (inode != oip.newinode) {
300 pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
304 /* Force lookup of new upper hardlink to find its lower */
311 static bool ovl_type_merge(struct dentry *dentry)
313 return OVL_TYPE_MERGE(ovl_path_type(dentry));
316 static bool ovl_type_origin(struct dentry *dentry)
318 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
321 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
322 struct ovl_cattr *attr)
324 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
325 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
326 struct inode *udir = upperdir->d_inode;
327 struct dentry *newdentry;
330 if (!attr->hardlink && !IS_POSIXACL(udir))
331 attr->mode &= ~current_umask();
333 inode_lock_nested(udir, I_MUTEX_PARENT);
334 newdentry = ovl_create_real(udir,
335 lookup_one_len(dentry->d_name.name,
339 err = PTR_ERR(newdentry);
340 if (IS_ERR(newdentry))
343 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry) &&
344 !ovl_allow_offline_changes(ofs)) {
345 /* Setting opaque here is just an optimization, allow to fail */
346 ovl_set_opaque(dentry, newdentry);
349 err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
357 ovl_cleanup(udir, newdentry);
362 static struct dentry *ovl_clear_empty(struct dentry *dentry,
363 struct list_head *list)
365 struct dentry *workdir = ovl_workdir(dentry);
366 struct inode *wdir = workdir->d_inode;
367 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
368 struct inode *udir = upperdir->d_inode;
369 struct path upperpath;
370 struct dentry *upper;
371 struct dentry *opaquedir;
375 if (WARN_ON(!workdir))
376 return ERR_PTR(-EROFS);
378 err = ovl_lock_rename_workdir(workdir, upperdir);
382 ovl_path_upper(dentry, &upperpath);
383 err = vfs_getattr(&upperpath, &stat,
384 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
389 if (!S_ISDIR(stat.mode))
391 upper = upperpath.dentry;
392 if (upper->d_parent->d_inode != udir)
395 opaquedir = ovl_create_temp(workdir, OVL_CATTR(stat.mode));
396 err = PTR_ERR(opaquedir);
397 if (IS_ERR(opaquedir))
400 err = ovl_copy_xattr(dentry->d_sb, upper, opaquedir);
404 err = ovl_set_opaque(dentry, opaquedir);
408 inode_lock(opaquedir->d_inode);
409 err = ovl_set_attr(opaquedir, &stat);
410 inode_unlock(opaquedir->d_inode);
414 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
418 ovl_cleanup_whiteouts(upper, list);
419 ovl_cleanup(wdir, upper);
420 unlock_rename(workdir, upperdir);
422 /* dentry's upper doesn't match now, get rid of it */
428 ovl_cleanup(wdir, opaquedir);
431 unlock_rename(workdir, upperdir);
436 static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
437 const struct posix_acl *acl)
443 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
446 size = posix_acl_xattr_size(acl->a_count);
447 buffer = kmalloc(size, GFP_KERNEL);
451 err = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
455 err = vfs_setxattr(&init_user_ns, upperdentry, name, buffer, size, XATTR_CREATE);
461 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
462 struct ovl_cattr *cattr)
464 struct dentry *workdir = ovl_workdir(dentry);
465 struct inode *wdir = workdir->d_inode;
466 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
467 struct inode *udir = upperdir->d_inode;
468 struct dentry *upper;
469 struct dentry *newdentry;
471 struct posix_acl *acl, *default_acl;
472 bool hardlink = !!cattr->hardlink;
474 if (WARN_ON(!workdir))
478 err = posix_acl_create(dentry->d_parent->d_inode,
479 &cattr->mode, &default_acl, &acl);
484 err = ovl_lock_rename_workdir(workdir, upperdir);
488 upper = lookup_one_len(dentry->d_name.name, upperdir,
490 err = PTR_ERR(upper);
495 if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
498 newdentry = ovl_create_temp(workdir, cattr);
499 err = PTR_ERR(newdentry);
500 if (IS_ERR(newdentry))
504 * mode could have been mutilated due to umask (e.g. sgid directory)
507 !S_ISLNK(cattr->mode) &&
508 newdentry->d_inode->i_mode != cattr->mode) {
509 struct iattr attr = {
510 .ia_valid = ATTR_MODE,
511 .ia_mode = cattr->mode,
513 inode_lock(newdentry->d_inode);
514 err = notify_change(&init_user_ns, newdentry, &attr, NULL);
515 inode_unlock(newdentry->d_inode);
520 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
525 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
531 if (!hardlink && S_ISDIR(cattr->mode)) {
532 err = ovl_set_opaque(dentry, newdentry);
536 err = ovl_do_rename(wdir, newdentry, udir, upper,
541 ovl_cleanup(wdir, upper);
543 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
547 err = ovl_instantiate(dentry, inode, newdentry, hardlink);
549 ovl_cleanup(udir, newdentry);
555 unlock_rename(workdir, upperdir);
558 posix_acl_release(acl);
559 posix_acl_release(default_acl);
564 ovl_cleanup(wdir, newdentry);
569 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
570 struct ovl_cattr *attr, bool origin)
573 const struct cred *old_cred;
574 struct cred *override_cred;
575 struct dentry *parent = dentry->d_parent;
577 err = ovl_copy_up(parent);
581 old_cred = ovl_override_creds(dentry->d_sb);
584 * When linking a file with copy up origin into a new parent, mark the
585 * new parent dir "impure".
588 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
590 goto out_revert_creds;
594 override_cred = prepare_creds();
596 override_cred->fsuid = inode->i_uid;
597 override_cred->fsgid = inode->i_gid;
598 if (!attr->hardlink) {
599 err = security_dentry_create_files_as(dentry,
600 attr->mode, &dentry->d_name, old_cred,
603 put_cred(override_cred);
604 goto out_revert_creds;
607 put_cred(override_creds(override_cred));
608 put_cred(override_cred);
610 if (!ovl_dentry_is_whiteout(dentry))
611 err = ovl_create_upper(dentry, inode, attr);
613 err = ovl_create_over_whiteout(dentry, inode, attr);
616 revert_creds(old_cred);
620 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
625 struct ovl_cattr attr = {
630 err = ovl_want_write(dentry);
634 /* Preallocate inode to be used by ovl_get_inode() */
636 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
640 spin_lock(&inode->i_lock);
641 inode->i_state |= I_CREATING;
642 spin_unlock(&inode->i_lock);
644 inode_init_owner(&init_user_ns, inode, dentry->d_parent->d_inode, mode);
645 attr.mode = inode->i_mode;
647 err = ovl_create_or_link(dentry, inode, &attr, false);
648 /* Did we end up using the preallocated inode? */
649 if (inode != d_inode(dentry))
653 ovl_drop_write(dentry);
658 static int ovl_create(struct user_namespace *mnt_userns, struct inode *dir,
659 struct dentry *dentry, umode_t mode, bool excl)
661 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
664 static int ovl_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
665 struct dentry *dentry, umode_t mode)
667 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
670 static int ovl_mknod(struct user_namespace *mnt_userns, struct inode *dir,
671 struct dentry *dentry, umode_t mode, dev_t rdev)
673 /* Don't allow creation of "whiteout" on overlay */
674 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
677 return ovl_create_object(dentry, mode, rdev, NULL);
680 static int ovl_symlink(struct user_namespace *mnt_userns, struct inode *dir,
681 struct dentry *dentry, const char *link)
683 return ovl_create_object(dentry, S_IFLNK, 0, link);
686 static int ovl_set_link_redirect(struct dentry *dentry)
688 const struct cred *old_cred;
691 old_cred = ovl_override_creds(dentry->d_sb);
692 err = ovl_set_redirect(dentry, false);
693 revert_creds(old_cred);
698 static int ovl_link(struct dentry *old, struct inode *newdir,
704 err = ovl_want_write(old);
708 err = ovl_copy_up(old);
712 err = ovl_copy_up(new->d_parent);
716 if (ovl_is_metacopy_dentry(old)) {
717 err = ovl_set_link_redirect(old);
722 err = ovl_nlink_start(old);
726 inode = d_inode(old);
729 err = ovl_create_or_link(new, inode,
730 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
731 ovl_type_origin(old));
742 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
744 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
747 static int ovl_remove_and_whiteout(struct dentry *dentry,
748 struct list_head *list)
750 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
751 struct dentry *workdir = ovl_workdir(dentry);
752 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
753 struct dentry *upper;
754 struct dentry *opaquedir = NULL;
757 if (WARN_ON(!workdir))
760 if (!list_empty(list)) {
761 opaquedir = ovl_clear_empty(dentry, list);
762 err = PTR_ERR(opaquedir);
763 if (IS_ERR(opaquedir))
767 err = ovl_lock_rename_workdir(workdir, upperdir);
771 upper = lookup_one_len(dentry->d_name.name, upperdir,
773 err = PTR_ERR(upper);
778 if ((opaquedir && upper != opaquedir) ||
779 (!opaquedir && ovl_dentry_upper(dentry) &&
780 !ovl_matches_upper(dentry, upper))) {
784 err = ovl_cleanup_and_whiteout(ofs, d_inode(upperdir), upper);
788 ovl_dir_modified(dentry->d_parent, true);
794 unlock_rename(workdir, upperdir);
801 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
802 struct list_head *list)
804 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
805 struct inode *dir = upperdir->d_inode;
806 struct dentry *upper;
807 struct dentry *opaquedir = NULL;
810 if (!list_empty(list)) {
811 opaquedir = ovl_clear_empty(dentry, list);
812 err = PTR_ERR(opaquedir);
813 if (IS_ERR(opaquedir))
817 inode_lock_nested(dir, I_MUTEX_PARENT);
818 upper = lookup_one_len(dentry->d_name.name, upperdir,
820 err = PTR_ERR(upper);
825 if ((opaquedir && upper != opaquedir) ||
826 (!opaquedir && !ovl_matches_upper(dentry, upper)))
830 err = vfs_rmdir(&init_user_ns, dir, upper);
832 err = vfs_unlink(&init_user_ns, dir, upper, NULL);
833 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
836 * Keeping this dentry hashed would mean having to release
837 * upperpath/lowerpath, which could only be done if we are the
838 * sole user of this dentry. Too tricky... Just unhash for
852 static bool ovl_pure_upper(struct dentry *dentry)
854 return !ovl_dentry_lower(dentry) &&
855 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
858 static void ovl_drop_nlink(struct dentry *dentry)
860 struct inode *inode = d_inode(dentry);
861 struct dentry *alias;
863 /* Try to find another, hashed alias */
864 spin_lock(&inode->i_lock);
865 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
866 if (alias != dentry && !d_unhashed(alias))
869 spin_unlock(&inode->i_lock);
872 * Changes to underlying layers may cause i_nlink to lose sync with
873 * reality. In this case prevent the link count from going to zero
876 if (inode->i_nlink > !!alias)
880 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
883 const struct cred *old_cred;
884 struct dentry *upperdentry;
885 bool lower_positive = ovl_lower_positive(dentry);
888 /* No need to clean pure upper removed by vfs_rmdir() */
889 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
890 err = ovl_check_empty_dir(dentry, &list);
895 err = ovl_want_write(dentry);
899 err = ovl_copy_up(dentry->d_parent);
903 err = ovl_nlink_start(dentry);
907 old_cred = ovl_override_creds(dentry->d_sb);
909 err = ovl_remove_upper(dentry, is_dir, &list);
911 err = ovl_remove_and_whiteout(dentry, &list);
912 revert_creds(old_cred);
915 clear_nlink(dentry->d_inode);
917 ovl_drop_nlink(dentry);
919 ovl_nlink_end(dentry);
924 * Note: we fail to update ctime if there was no copy-up, only a
927 upperdentry = ovl_dentry_upper(dentry);
929 ovl_copyattr(d_inode(upperdentry), d_inode(dentry));
932 ovl_drop_write(dentry);
934 ovl_cache_free(&list);
938 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
940 return ovl_do_remove(dentry, false);
943 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
945 return ovl_do_remove(dentry, true);
948 static bool ovl_type_merge_or_lower(struct dentry *dentry)
950 enum ovl_path_type type = ovl_path_type(dentry);
952 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
955 static bool ovl_can_move(struct dentry *dentry)
957 return ovl_redirect_dir(dentry->d_sb) ||
958 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
961 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
964 struct dentry *d, *tmp;
965 int buflen = ovl_redirect_max + 1;
968 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
973 buf = ret = kmalloc(buflen, GFP_KERNEL);
979 for (d = dget(dentry); !IS_ROOT(d);) {
983 spin_lock(&d->d_lock);
984 name = ovl_dentry_get_redirect(d);
986 thislen = strlen(name);
988 name = d->d_name.name;
989 thislen = d->d_name.len;
992 /* If path is too long, fall back to userspace move */
993 if (thislen + (name[0] != '/') > buflen) {
994 ret = ERR_PTR(-EXDEV);
995 spin_unlock(&d->d_lock);
1000 memcpy(&buf[buflen], name, thislen);
1001 spin_unlock(&d->d_lock);
1002 tmp = dget_parent(d);
1007 /* Absolute redirect: finished */
1008 if (buf[buflen] == '/')
1013 ret = kstrdup(&buf[buflen], GFP_KERNEL);
1018 return ret ? ret : ERR_PTR(-ENOMEM);
1021 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
1023 struct dentry *lowerdentry;
1028 if (d_is_dir(dentry))
1032 * For non-dir hardlinked files, we need absolute redirects
1033 * in general as two upper hardlinks could be in different
1034 * dirs. We could put a relative redirect now and convert
1035 * it to absolute redirect later. But when nlink > 1 and
1036 * indexing is on, that means relative redirect needs to be
1037 * converted to absolute during copy up of another lower
1038 * hardllink as well.
1040 * So without optimizing too much, just check if lower is
1041 * a hard link or not. If lower is hard link, put absolute
1044 lowerdentry = ovl_dentry_lower(dentry);
1045 return (d_inode(lowerdentry)->i_nlink > 1);
1048 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
1051 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1052 const char *redirect = ovl_dentry_get_redirect(dentry);
1053 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1055 if (redirect && (!absolute_redirect || redirect[0] == '/'))
1058 redirect = ovl_get_redirect(dentry, absolute_redirect);
1059 if (IS_ERR(redirect))
1060 return PTR_ERR(redirect);
1062 err = ovl_check_setxattr(ofs, ovl_dentry_upper(dentry),
1064 redirect, strlen(redirect), -EXDEV);
1066 spin_lock(&dentry->d_lock);
1067 ovl_dentry_set_redirect(dentry, redirect);
1068 spin_unlock(&dentry->d_lock);
1071 pr_warn_ratelimited("failed to set redirect (%i)\n",
1073 /* Fall back to userspace copy-up */
1079 static int ovl_rename(struct user_namespace *mnt_userns, struct inode *olddir,
1080 struct dentry *old, struct inode *newdir,
1081 struct dentry *new, unsigned int flags)
1084 struct dentry *old_upperdir;
1085 struct dentry *new_upperdir;
1086 struct dentry *olddentry;
1087 struct dentry *newdentry;
1088 struct dentry *trap;
1091 bool cleanup_whiteout = false;
1092 bool update_nlink = false;
1093 bool overwrite = !(flags & RENAME_EXCHANGE);
1094 bool is_dir = d_is_dir(old);
1095 bool new_is_dir = d_is_dir(new);
1096 bool samedir = olddir == newdir;
1097 struct dentry *opaquedir = NULL;
1098 const struct cred *old_cred = NULL;
1102 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1105 flags &= ~RENAME_NOREPLACE;
1107 /* Don't copy up directory trees */
1109 if (!ovl_can_move(old))
1111 if (!overwrite && !ovl_can_move(new))
1114 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1115 err = ovl_check_empty_dir(new, &list);
1121 if (ovl_lower_positive(old)) {
1122 if (!ovl_dentry_is_whiteout(new)) {
1123 /* Whiteout source */
1124 flags |= RENAME_WHITEOUT;
1126 /* Switch whiteouts */
1127 flags |= RENAME_EXCHANGE;
1129 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1130 flags |= RENAME_EXCHANGE;
1131 cleanup_whiteout = true;
1135 err = ovl_want_write(old);
1139 err = ovl_copy_up(old);
1141 goto out_drop_write;
1143 err = ovl_copy_up(new->d_parent);
1145 goto out_drop_write;
1147 err = ovl_copy_up(new);
1149 goto out_drop_write;
1150 } else if (d_inode(new)) {
1151 err = ovl_nlink_start(new);
1153 goto out_drop_write;
1155 update_nlink = true;
1158 old_cred = ovl_override_creds(old->d_sb);
1160 if (!list_empty(&list)) {
1161 opaquedir = ovl_clear_empty(new, &list);
1162 err = PTR_ERR(opaquedir);
1163 if (IS_ERR(opaquedir)) {
1165 goto out_revert_creds;
1169 old_upperdir = ovl_dentry_upper(old->d_parent);
1170 new_upperdir = ovl_dentry_upper(new->d_parent);
1174 * When moving a merge dir or non-dir with copy up origin into
1175 * a new parent, we are marking the new parent dir "impure".
1176 * When ovl_iterate() iterates an "impure" upper dir, it will
1177 * lookup the origin inodes of the entries to fill d_ino.
1179 if (ovl_type_origin(old)) {
1180 err = ovl_set_impure(new->d_parent, new_upperdir);
1182 goto out_revert_creds;
1184 if (!overwrite && ovl_type_origin(new)) {
1185 err = ovl_set_impure(old->d_parent, old_upperdir);
1187 goto out_revert_creds;
1191 trap = lock_rename(new_upperdir, old_upperdir);
1193 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
1195 err = PTR_ERR(olddentry);
1196 if (IS_ERR(olddentry))
1200 if (!ovl_matches_upper(old, olddentry))
1203 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1205 err = PTR_ERR(newdentry);
1206 if (IS_ERR(newdentry))
1209 old_opaque = ovl_dentry_is_opaque(old);
1210 new_opaque = ovl_dentry_is_opaque(new);
1213 if (d_inode(new) && ovl_dentry_upper(new)) {
1215 if (newdentry != opaquedir)
1218 if (!ovl_matches_upper(new, newdentry))
1222 if (!d_is_negative(newdentry)) {
1223 if (!new_opaque || !ovl_is_whiteout(newdentry))
1226 if (flags & RENAME_EXCHANGE)
1231 if (olddentry == trap)
1233 if (newdentry == trap)
1236 if (olddentry->d_inode == newdentry->d_inode)
1240 if (ovl_type_merge_or_lower(old))
1241 err = ovl_set_redirect(old, samedir);
1242 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1243 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1247 if (!overwrite && ovl_type_merge_or_lower(new))
1248 err = ovl_set_redirect(new, samedir);
1249 else if (!overwrite && new_is_dir && !new_opaque &&
1250 ovl_type_merge(old->d_parent))
1251 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1255 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1256 new_upperdir->d_inode, newdentry, flags);
1260 if (cleanup_whiteout)
1261 ovl_cleanup(old_upperdir->d_inode, newdentry);
1263 if (overwrite && d_inode(new)) {
1265 clear_nlink(d_inode(new));
1267 ovl_drop_nlink(new);
1270 ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1271 (!overwrite && ovl_type_origin(new)));
1272 ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1273 (d_inode(new) && ovl_type_origin(new)));
1276 ovl_copyattr(d_inode(olddentry), d_inode(old));
1277 if (d_inode(new) && ovl_dentry_upper(new))
1278 ovl_copyattr(d_inode(newdentry), d_inode(new));
1285 unlock_rename(new_upperdir, old_upperdir);
1287 revert_creds(old_cred);
1291 ovl_drop_write(old);
1294 ovl_cache_free(&list);
1298 const struct inode_operations ovl_dir_inode_operations = {
1299 .lookup = ovl_lookup,
1301 .symlink = ovl_symlink,
1302 .unlink = ovl_unlink,
1304 .rename = ovl_rename,
1306 .setattr = ovl_setattr,
1307 .create = ovl_create,
1309 .permission = ovl_permission,
1310 .getattr = ovl_getattr,
1311 .listxattr = ovl_listxattr,
1312 .get_acl = ovl_get_acl,
1313 .update_time = ovl_update_time,
1314 .fileattr_get = ovl_fileattr_get,
1315 .fileattr_set = ovl_fileattr_set,