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 ovl_fs *ofs, struct inode *wdir, struct dentry *wdentry)
31 if (d_is_dir(wdentry))
32 err = ovl_do_rmdir(ofs, wdir, wdentry);
34 err = ovl_do_unlink(ofs, wdir, wdentry);
38 pr_err("cleanup of '%pd2' failed (%i)\n",
45 struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, 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 = ovl_lookup_upper(ofs, 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(ofs, workdir);
77 err = ovl_do_whiteout(ofs, wdir, whiteout);
80 whiteout = ERR_PTR(err);
83 ofs->whiteout = whiteout;
86 if (ofs->share_whiteout) {
87 whiteout = ovl_lookup_temp(ofs, workdir);
91 err = ovl_do_link(ofs, 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(ofs, wdir, whiteout, dir, dentry, flags);
129 ovl_cleanup(ofs, wdir, dentry);
136 ovl_cleanup(ofs, wdir, whiteout);
140 int ovl_mkdir_real(struct ovl_fs *ofs, struct inode *dir,
141 struct dentry **newdentry, umode_t mode)
144 struct dentry *d, *dentry = *newdentry;
146 err = ovl_do_mkdir(ofs, 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 = ovl_lookup_upper(ofs, 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 ovl_fs *ofs, struct inode *dir,
172 struct dentry *newdentry, struct ovl_cattr *attr)
176 if (IS_ERR(newdentry))
180 if (newdentry->d_inode)
183 if (attr->hardlink) {
184 err = ovl_do_link(ofs, attr->hardlink, dir, newdentry);
186 switch (attr->mode & S_IFMT) {
188 err = ovl_do_create(ofs, dir, newdentry, attr->mode);
192 /* mkdir is special... */
193 err = ovl_mkdir_real(ofs, dir, &newdentry, attr->mode);
200 err = ovl_do_mknod(ofs, dir, newdentry, attr->mode,
205 err = ovl_do_symlink(ofs, 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 ovl_fs *ofs, struct dentry *workdir,
228 struct ovl_cattr *attr)
230 return ovl_create_real(ofs, d_inode(workdir),
231 ovl_lookup_temp(ofs, workdir), attr);
234 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
237 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
240 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
242 ovl_dentry_set_opaque(dentry);
247 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
250 * Fail with -EIO when trying to create opaque dir and upper doesn't
251 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
252 * return a specific error for noxattr case.
254 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
258 * Common operations required to be done after creation of file on upper.
259 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
260 * may not use to instantiate the new dentry.
262 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
263 struct dentry *newdentry, bool hardlink)
265 struct ovl_inode_params oip = {
266 .upperdentry = newdentry,
270 ovl_dir_modified(dentry->d_parent, false);
271 ovl_dentry_set_upper_alias(dentry);
272 ovl_dentry_update_reval(dentry, newdentry,
273 DCACHE_OP_REVALIDATE | DCACHE_OP_WEAK_REVALIDATE);
277 * ovl_obtain_alias() can be called after ovl_create_real()
278 * and before we get here, so we may get an inode from cache
279 * with the same real upperdentry that is not the inode we
280 * pre-allocated. In this case we will use the cached inode
281 * to instantiate the new dentry.
283 * XXX: if we ever use ovl_obtain_alias() to decode directory
284 * file handles, need to use ovl_get_inode_locked() and
285 * d_instantiate_new() here to prevent from creating two
286 * hashed directory inode aliases.
288 inode = ovl_get_inode(dentry->d_sb, &oip);
290 return PTR_ERR(inode);
291 if (inode == oip.newinode)
292 ovl_set_flag(OVL_UPPERDATA, inode);
294 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
299 d_instantiate(dentry, inode);
300 if (inode != oip.newinode) {
301 pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
305 /* Force lookup of new upper hardlink to find its lower */
312 static bool ovl_type_merge(struct dentry *dentry)
314 return OVL_TYPE_MERGE(ovl_path_type(dentry));
317 static bool ovl_type_origin(struct dentry *dentry)
319 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
322 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
323 struct ovl_cattr *attr)
325 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
326 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
327 struct inode *udir = upperdir->d_inode;
328 struct dentry *newdentry;
331 if (!attr->hardlink && !IS_POSIXACL(udir))
332 attr->mode &= ~current_umask();
334 inode_lock_nested(udir, I_MUTEX_PARENT);
335 newdentry = ovl_create_real(ofs, udir,
336 ovl_lookup_upper(ofs, dentry->d_name.name,
337 upperdir, dentry->d_name.len),
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(ofs, udir, newdentry);
362 static struct dentry *ovl_clear_empty(struct dentry *dentry,
363 struct list_head *list)
365 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
366 struct dentry *workdir = ovl_workdir(dentry);
367 struct inode *wdir = workdir->d_inode;
368 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
369 struct inode *udir = upperdir->d_inode;
370 struct path upperpath;
371 struct dentry *upper;
372 struct dentry *opaquedir;
376 if (WARN_ON(!workdir))
377 return ERR_PTR(-EROFS);
379 err = ovl_lock_rename_workdir(workdir, upperdir);
383 ovl_path_upper(dentry, &upperpath);
384 err = vfs_getattr(&upperpath, &stat,
385 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
390 if (!S_ISDIR(stat.mode))
392 upper = upperpath.dentry;
393 if (upper->d_parent->d_inode != udir)
396 opaquedir = ovl_create_temp(ofs, workdir, OVL_CATTR(stat.mode));
397 err = PTR_ERR(opaquedir);
398 if (IS_ERR(opaquedir))
401 err = ovl_copy_xattr(dentry->d_sb, &upperpath, opaquedir);
405 err = ovl_set_opaque(dentry, opaquedir);
409 inode_lock(opaquedir->d_inode);
410 err = ovl_set_attr(ofs, opaquedir, &stat);
411 inode_unlock(opaquedir->d_inode);
415 err = ovl_do_rename(ofs, wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
419 ovl_cleanup_whiteouts(ofs, upper, list);
420 ovl_cleanup(ofs, wdir, upper);
421 unlock_rename(workdir, upperdir);
423 /* dentry's upper doesn't match now, get rid of it */
429 ovl_cleanup(ofs, wdir, opaquedir);
432 unlock_rename(workdir, upperdir);
437 static int ovl_set_upper_acl(struct ovl_fs *ofs, struct dentry *upperdentry,
438 const char *name, const struct posix_acl *acl)
444 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
447 size = posix_acl_xattr_size(acl->a_count);
448 buffer = kmalloc(size, GFP_KERNEL);
452 err = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
456 err = ovl_do_setxattr(ofs, upperdentry, name, buffer, size, XATTR_CREATE);
462 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
463 struct ovl_cattr *cattr)
465 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
466 struct dentry *workdir = ovl_workdir(dentry);
467 struct inode *wdir = workdir->d_inode;
468 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
469 struct inode *udir = upperdir->d_inode;
470 struct dentry *upper;
471 struct dentry *newdentry;
473 struct posix_acl *acl, *default_acl;
474 bool hardlink = !!cattr->hardlink;
476 if (WARN_ON(!workdir))
480 err = posix_acl_create(dentry->d_parent->d_inode,
481 &cattr->mode, &default_acl, &acl);
486 err = ovl_lock_rename_workdir(workdir, upperdir);
490 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
492 err = PTR_ERR(upper);
497 if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
500 newdentry = ovl_create_temp(ofs, workdir, cattr);
501 err = PTR_ERR(newdentry);
502 if (IS_ERR(newdentry))
506 * mode could have been mutilated due to umask (e.g. sgid directory)
509 !S_ISLNK(cattr->mode) &&
510 newdentry->d_inode->i_mode != cattr->mode) {
511 struct iattr attr = {
512 .ia_valid = ATTR_MODE,
513 .ia_mode = cattr->mode,
515 inode_lock(newdentry->d_inode);
516 err = ovl_do_notify_change(ofs, newdentry, &attr);
517 inode_unlock(newdentry->d_inode);
522 err = ovl_set_upper_acl(ofs, newdentry,
523 XATTR_NAME_POSIX_ACL_ACCESS, acl);
527 err = ovl_set_upper_acl(ofs, newdentry,
528 XATTR_NAME_POSIX_ACL_DEFAULT, default_acl);
533 if (!hardlink && S_ISDIR(cattr->mode)) {
534 err = ovl_set_opaque(dentry, newdentry);
538 err = ovl_do_rename(ofs, wdir, newdentry, udir, upper,
543 ovl_cleanup(ofs, wdir, upper);
545 err = ovl_do_rename(ofs, wdir, newdentry, udir, upper, 0);
549 err = ovl_instantiate(dentry, inode, newdentry, hardlink);
551 ovl_cleanup(ofs, udir, newdentry);
557 unlock_rename(workdir, upperdir);
560 posix_acl_release(acl);
561 posix_acl_release(default_acl);
566 ovl_cleanup(ofs, wdir, newdentry);
571 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
572 struct ovl_cattr *attr, bool origin)
575 const struct cred *old_cred;
576 struct cred *override_cred;
577 struct dentry *parent = dentry->d_parent;
579 err = ovl_copy_up(parent);
583 old_cred = ovl_override_creds(dentry->d_sb);
586 * When linking a file with copy up origin into a new parent, mark the
587 * new parent dir "impure".
590 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
592 goto out_revert_creds;
596 override_cred = prepare_creds();
598 override_cred->fsuid = inode->i_uid;
599 override_cred->fsgid = inode->i_gid;
600 if (!attr->hardlink) {
601 err = security_dentry_create_files_as(dentry,
602 attr->mode, &dentry->d_name, old_cred,
605 put_cred(override_cred);
606 goto out_revert_creds;
609 put_cred(override_creds(override_cred));
610 put_cred(override_cred);
612 if (!ovl_dentry_is_whiteout(dentry))
613 err = ovl_create_upper(dentry, inode, attr);
615 err = ovl_create_over_whiteout(dentry, inode, attr);
618 revert_creds(old_cred);
622 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
627 struct ovl_cattr attr = {
632 err = ovl_want_write(dentry);
636 /* Preallocate inode to be used by ovl_get_inode() */
638 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
642 spin_lock(&inode->i_lock);
643 inode->i_state |= I_CREATING;
644 spin_unlock(&inode->i_lock);
646 inode_init_owner(&init_user_ns, inode, dentry->d_parent->d_inode, mode);
647 attr.mode = inode->i_mode;
649 err = ovl_create_or_link(dentry, inode, &attr, false);
650 /* Did we end up using the preallocated inode? */
651 if (inode != d_inode(dentry))
655 ovl_drop_write(dentry);
660 static int ovl_create(struct user_namespace *mnt_userns, struct inode *dir,
661 struct dentry *dentry, umode_t mode, bool excl)
663 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
666 static int ovl_mkdir(struct user_namespace *mnt_userns, struct inode *dir,
667 struct dentry *dentry, umode_t mode)
669 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
672 static int ovl_mknod(struct user_namespace *mnt_userns, struct inode *dir,
673 struct dentry *dentry, umode_t mode, dev_t rdev)
675 /* Don't allow creation of "whiteout" on overlay */
676 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
679 return ovl_create_object(dentry, mode, rdev, NULL);
682 static int ovl_symlink(struct user_namespace *mnt_userns, struct inode *dir,
683 struct dentry *dentry, const char *link)
685 return ovl_create_object(dentry, S_IFLNK, 0, link);
688 static int ovl_set_link_redirect(struct dentry *dentry)
690 const struct cred *old_cred;
693 old_cred = ovl_override_creds(dentry->d_sb);
694 err = ovl_set_redirect(dentry, false);
695 revert_creds(old_cred);
700 static int ovl_link(struct dentry *old, struct inode *newdir,
706 err = ovl_want_write(old);
710 err = ovl_copy_up(old);
714 err = ovl_copy_up(new->d_parent);
718 if (ovl_is_metacopy_dentry(old)) {
719 err = ovl_set_link_redirect(old);
724 err = ovl_nlink_start(old);
728 inode = d_inode(old);
731 err = ovl_create_or_link(new, inode,
732 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
733 ovl_type_origin(old));
744 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
746 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
749 static int ovl_remove_and_whiteout(struct dentry *dentry,
750 struct list_head *list)
752 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
753 struct dentry *workdir = ovl_workdir(dentry);
754 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
755 struct dentry *upper;
756 struct dentry *opaquedir = NULL;
759 if (WARN_ON(!workdir))
762 if (!list_empty(list)) {
763 opaquedir = ovl_clear_empty(dentry, list);
764 err = PTR_ERR(opaquedir);
765 if (IS_ERR(opaquedir))
769 err = ovl_lock_rename_workdir(workdir, upperdir);
773 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
775 err = PTR_ERR(upper);
780 if ((opaquedir && upper != opaquedir) ||
781 (!opaquedir && ovl_dentry_upper(dentry) &&
782 !ovl_matches_upper(dentry, upper))) {
786 err = ovl_cleanup_and_whiteout(ofs, d_inode(upperdir), upper);
790 ovl_dir_modified(dentry->d_parent, true);
796 unlock_rename(workdir, upperdir);
803 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
804 struct list_head *list)
806 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
807 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
808 struct inode *dir = upperdir->d_inode;
809 struct dentry *upper;
810 struct dentry *opaquedir = NULL;
813 if (!list_empty(list)) {
814 opaquedir = ovl_clear_empty(dentry, list);
815 err = PTR_ERR(opaquedir);
816 if (IS_ERR(opaquedir))
820 inode_lock_nested(dir, I_MUTEX_PARENT);
821 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
823 err = PTR_ERR(upper);
828 if ((opaquedir && upper != opaquedir) ||
829 (!opaquedir && !ovl_matches_upper(dentry, upper)))
833 err = ovl_do_rmdir(ofs, dir, upper);
835 err = ovl_do_unlink(ofs, dir, upper);
836 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
839 * Keeping this dentry hashed would mean having to release
840 * upperpath/lowerpath, which could only be done if we are the
841 * sole user of this dentry. Too tricky... Just unhash for
855 static bool ovl_pure_upper(struct dentry *dentry)
857 return !ovl_dentry_lower(dentry) &&
858 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
861 static void ovl_drop_nlink(struct dentry *dentry)
863 struct inode *inode = d_inode(dentry);
864 struct dentry *alias;
866 /* Try to find another, hashed alias */
867 spin_lock(&inode->i_lock);
868 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
869 if (alias != dentry && !d_unhashed(alias))
872 spin_unlock(&inode->i_lock);
875 * Changes to underlying layers may cause i_nlink to lose sync with
876 * reality. In this case prevent the link count from going to zero
879 if (inode->i_nlink > !!alias)
883 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
886 const struct cred *old_cred;
887 bool lower_positive = ovl_lower_positive(dentry);
890 /* No need to clean pure upper removed by vfs_rmdir() */
891 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
892 err = ovl_check_empty_dir(dentry, &list);
897 err = ovl_want_write(dentry);
901 err = ovl_copy_up(dentry->d_parent);
905 err = ovl_nlink_start(dentry);
909 old_cred = ovl_override_creds(dentry->d_sb);
911 err = ovl_remove_upper(dentry, is_dir, &list);
913 err = ovl_remove_and_whiteout(dentry, &list);
914 revert_creds(old_cred);
917 clear_nlink(dentry->d_inode);
919 ovl_drop_nlink(dentry);
921 ovl_nlink_end(dentry);
926 * Note: we fail to update ctime if there was no copy-up, only a
929 if (ovl_dentry_upper(dentry))
930 ovl_copyattr(d_inode(dentry));
933 ovl_drop_write(dentry);
935 ovl_cache_free(&list);
939 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
941 return ovl_do_remove(dentry, false);
944 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
946 return ovl_do_remove(dentry, true);
949 static bool ovl_type_merge_or_lower(struct dentry *dentry)
951 enum ovl_path_type type = ovl_path_type(dentry);
953 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
956 static bool ovl_can_move(struct dentry *dentry)
958 return ovl_redirect_dir(dentry->d_sb) ||
959 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
962 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
965 struct dentry *d, *tmp;
966 int buflen = ovl_redirect_max + 1;
969 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
974 buf = ret = kmalloc(buflen, GFP_KERNEL);
980 for (d = dget(dentry); !IS_ROOT(d);) {
984 spin_lock(&d->d_lock);
985 name = ovl_dentry_get_redirect(d);
987 thislen = strlen(name);
989 name = d->d_name.name;
990 thislen = d->d_name.len;
993 /* If path is too long, fall back to userspace move */
994 if (thislen + (name[0] != '/') > buflen) {
995 ret = ERR_PTR(-EXDEV);
996 spin_unlock(&d->d_lock);
1001 memcpy(&buf[buflen], name, thislen);
1002 spin_unlock(&d->d_lock);
1003 tmp = dget_parent(d);
1008 /* Absolute redirect: finished */
1009 if (buf[buflen] == '/')
1014 ret = kstrdup(&buf[buflen], GFP_KERNEL);
1019 return ret ? ret : ERR_PTR(-ENOMEM);
1022 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
1024 struct dentry *lowerdentry;
1029 if (d_is_dir(dentry))
1033 * For non-dir hardlinked files, we need absolute redirects
1034 * in general as two upper hardlinks could be in different
1035 * dirs. We could put a relative redirect now and convert
1036 * it to absolute redirect later. But when nlink > 1 and
1037 * indexing is on, that means relative redirect needs to be
1038 * converted to absolute during copy up of another lower
1039 * hardllink as well.
1041 * So without optimizing too much, just check if lower is
1042 * a hard link or not. If lower is hard link, put absolute
1045 lowerdentry = ovl_dentry_lower(dentry);
1046 return (d_inode(lowerdentry)->i_nlink > 1);
1049 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
1052 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1053 const char *redirect = ovl_dentry_get_redirect(dentry);
1054 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1056 if (redirect && (!absolute_redirect || redirect[0] == '/'))
1059 redirect = ovl_get_redirect(dentry, absolute_redirect);
1060 if (IS_ERR(redirect))
1061 return PTR_ERR(redirect);
1063 err = ovl_check_setxattr(ofs, ovl_dentry_upper(dentry),
1065 redirect, strlen(redirect), -EXDEV);
1067 spin_lock(&dentry->d_lock);
1068 ovl_dentry_set_redirect(dentry, redirect);
1069 spin_unlock(&dentry->d_lock);
1072 pr_warn_ratelimited("failed to set redirect (%i)\n",
1074 /* Fall back to userspace copy-up */
1080 static int ovl_rename(struct user_namespace *mnt_userns, struct inode *olddir,
1081 struct dentry *old, struct inode *newdir,
1082 struct dentry *new, unsigned int flags)
1085 struct dentry *old_upperdir;
1086 struct dentry *new_upperdir;
1087 struct dentry *olddentry;
1088 struct dentry *newdentry;
1089 struct dentry *trap;
1092 bool cleanup_whiteout = false;
1093 bool update_nlink = false;
1094 bool overwrite = !(flags & RENAME_EXCHANGE);
1095 bool is_dir = d_is_dir(old);
1096 bool new_is_dir = d_is_dir(new);
1097 bool samedir = olddir == newdir;
1098 struct dentry *opaquedir = NULL;
1099 const struct cred *old_cred = NULL;
1100 struct ovl_fs *ofs = OVL_FS(old->d_sb);
1104 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1107 flags &= ~RENAME_NOREPLACE;
1109 /* Don't copy up directory trees */
1111 if (!ovl_can_move(old))
1113 if (!overwrite && !ovl_can_move(new))
1116 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1117 err = ovl_check_empty_dir(new, &list);
1123 if (ovl_lower_positive(old)) {
1124 if (!ovl_dentry_is_whiteout(new)) {
1125 /* Whiteout source */
1126 flags |= RENAME_WHITEOUT;
1128 /* Switch whiteouts */
1129 flags |= RENAME_EXCHANGE;
1131 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1132 flags |= RENAME_EXCHANGE;
1133 cleanup_whiteout = true;
1137 err = ovl_want_write(old);
1141 err = ovl_copy_up(old);
1143 goto out_drop_write;
1145 err = ovl_copy_up(new->d_parent);
1147 goto out_drop_write;
1149 err = ovl_copy_up(new);
1151 goto out_drop_write;
1152 } else if (d_inode(new)) {
1153 err = ovl_nlink_start(new);
1155 goto out_drop_write;
1157 update_nlink = true;
1160 old_cred = ovl_override_creds(old->d_sb);
1162 if (!list_empty(&list)) {
1163 opaquedir = ovl_clear_empty(new, &list);
1164 err = PTR_ERR(opaquedir);
1165 if (IS_ERR(opaquedir)) {
1167 goto out_revert_creds;
1171 old_upperdir = ovl_dentry_upper(old->d_parent);
1172 new_upperdir = ovl_dentry_upper(new->d_parent);
1176 * When moving a merge dir or non-dir with copy up origin into
1177 * a new parent, we are marking the new parent dir "impure".
1178 * When ovl_iterate() iterates an "impure" upper dir, it will
1179 * lookup the origin inodes of the entries to fill d_ino.
1181 if (ovl_type_origin(old)) {
1182 err = ovl_set_impure(new->d_parent, new_upperdir);
1184 goto out_revert_creds;
1186 if (!overwrite && ovl_type_origin(new)) {
1187 err = ovl_set_impure(old->d_parent, old_upperdir);
1189 goto out_revert_creds;
1193 trap = lock_rename(new_upperdir, old_upperdir);
1195 olddentry = ovl_lookup_upper(ofs, old->d_name.name, old_upperdir,
1197 err = PTR_ERR(olddentry);
1198 if (IS_ERR(olddentry))
1202 if (!ovl_matches_upper(old, olddentry))
1205 newdentry = ovl_lookup_upper(ofs, new->d_name.name, new_upperdir,
1207 err = PTR_ERR(newdentry);
1208 if (IS_ERR(newdentry))
1211 old_opaque = ovl_dentry_is_opaque(old);
1212 new_opaque = ovl_dentry_is_opaque(new);
1215 if (d_inode(new) && ovl_dentry_upper(new)) {
1217 if (newdentry != opaquedir)
1220 if (!ovl_matches_upper(new, newdentry))
1224 if (!d_is_negative(newdentry)) {
1225 if (!new_opaque || !ovl_is_whiteout(newdentry))
1228 if (flags & RENAME_EXCHANGE)
1233 if (olddentry == trap)
1235 if (newdentry == trap)
1238 if (olddentry->d_inode == newdentry->d_inode)
1242 if (ovl_type_merge_or_lower(old))
1243 err = ovl_set_redirect(old, samedir);
1244 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1245 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1249 if (!overwrite && ovl_type_merge_or_lower(new))
1250 err = ovl_set_redirect(new, samedir);
1251 else if (!overwrite && new_is_dir && !new_opaque &&
1252 ovl_type_merge(old->d_parent))
1253 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1257 err = ovl_do_rename(ofs, old_upperdir->d_inode, olddentry,
1258 new_upperdir->d_inode, newdentry, flags);
1262 if (cleanup_whiteout)
1263 ovl_cleanup(ofs, old_upperdir->d_inode, newdentry);
1265 if (overwrite && d_inode(new)) {
1267 clear_nlink(d_inode(new));
1269 ovl_drop_nlink(new);
1272 ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1273 (!overwrite && ovl_type_origin(new)));
1274 ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1275 (d_inode(new) && ovl_type_origin(new)));
1278 ovl_copyattr(d_inode(old));
1279 if (d_inode(new) && ovl_dentry_upper(new))
1280 ovl_copyattr(d_inode(new));
1287 unlock_rename(new_upperdir, old_upperdir);
1289 revert_creds(old_cred);
1293 ovl_drop_write(old);
1296 ovl_cache_free(&list);
1300 const struct inode_operations ovl_dir_inode_operations = {
1301 .lookup = ovl_lookup,
1303 .symlink = ovl_symlink,
1304 .unlink = ovl_unlink,
1306 .rename = ovl_rename,
1308 .setattr = ovl_setattr,
1309 .create = ovl_create,
1311 .permission = ovl_permission,
1312 .getattr = ovl_getattr,
1313 .listxattr = ovl_listxattr,
1314 .get_acl = ovl_get_acl,
1315 .update_time = ovl_update_time,
1316 .fileattr_get = ovl_fileattr_get,
1317 .fileattr_set = ovl_fileattr_set,