3 * Copyright (C) 2011 Novell Inc.
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.
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"
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");
27 static int ovl_set_redirect(struct dentry
*dentry
, bool samedir
);
29 int ovl_cleanup(struct inode
*wdir
, struct dentry
*wdentry
)
34 if (d_is_dir(wdentry
))
35 err
= ovl_do_rmdir(wdir
, wdentry
);
37 err
= ovl_do_unlink(wdir
, wdentry
);
41 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
48 static struct dentry
*ovl_lookup_temp(struct dentry
*workdir
)
52 static atomic_t temp_id
= ATOMIC_INIT(0);
54 /* counter is allowed to wrap, since temp dentries are ephemeral */
55 snprintf(name
, sizeof(name
), "#%x", atomic_inc_return(&temp_id
));
57 temp
= lookup_one_len(name
, workdir
, strlen(name
));
58 if (!IS_ERR(temp
) && temp
->d_inode
) {
59 pr_err("overlayfs: workdir/%s already exists\n", name
);
67 /* caller holds i_mutex on workdir */
68 static struct dentry
*ovl_whiteout(struct dentry
*workdir
)
71 struct dentry
*whiteout
;
72 struct inode
*wdir
= workdir
->d_inode
;
74 whiteout
= ovl_lookup_temp(workdir
);
78 err
= ovl_do_whiteout(wdir
, whiteout
);
81 whiteout
= ERR_PTR(err
);
87 /* Caller must hold i_mutex on both workdir and dir */
88 int ovl_cleanup_and_whiteout(struct dentry
*workdir
, struct inode
*dir
,
89 struct dentry
*dentry
)
91 struct inode
*wdir
= workdir
->d_inode
;
92 struct dentry
*whiteout
;
96 whiteout
= ovl_whiteout(workdir
);
97 err
= PTR_ERR(whiteout
);
101 if (d_is_dir(dentry
))
102 flags
= RENAME_EXCHANGE
;
104 err
= ovl_do_rename(wdir
, whiteout
, dir
, dentry
, flags
);
108 ovl_cleanup(wdir
, dentry
);
115 ovl_cleanup(wdir
, whiteout
);
119 static int ovl_mkdir_real(struct inode
*dir
, struct dentry
**newdentry
,
123 struct dentry
*d
, *dentry
= *newdentry
;
125 err
= ovl_do_mkdir(dir
, dentry
, mode
);
129 if (likely(!d_unhashed(dentry
)))
133 * vfs_mkdir() may succeed and leave the dentry passed
134 * to it unhashed and negative. If that happens, try to
135 * lookup a new hashed and positive dentry.
137 d
= lookup_one_len(dentry
->d_name
.name
, dentry
->d_parent
,
140 pr_warn("overlayfs: failed lookup after mkdir (%pd2, err=%i).\n",
150 struct dentry
*ovl_create_real(struct inode
*dir
, struct dentry
*newdentry
,
151 struct ovl_cattr
*attr
)
155 if (IS_ERR(newdentry
))
159 if (newdentry
->d_inode
)
162 if (attr
->hardlink
) {
163 err
= ovl_do_link(attr
->hardlink
, dir
, newdentry
);
165 switch (attr
->mode
& S_IFMT
) {
167 err
= ovl_do_create(dir
, newdentry
, attr
->mode
);
171 /* mkdir is special... */
172 err
= ovl_mkdir_real(dir
, &newdentry
, attr
->mode
);
179 err
= ovl_do_mknod(dir
, newdentry
, attr
->mode
,
184 err
= ovl_do_symlink(dir
, newdentry
, attr
->link
);
191 if (!err
&& WARN_ON(!newdentry
->d_inode
)) {
193 * Not quite sure if non-instantiated dentry is legal or not.
194 * VFS doesn't seem to care so check and warn here.
206 struct dentry
*ovl_create_temp(struct dentry
*workdir
, struct ovl_cattr
*attr
)
208 return ovl_create_real(d_inode(workdir
), ovl_lookup_temp(workdir
),
212 static int ovl_set_opaque_xerr(struct dentry
*dentry
, struct dentry
*upper
,
217 err
= ovl_check_setxattr(dentry
, upper
, OVL_XATTR_OPAQUE
, "y", 1, xerr
);
219 ovl_dentry_set_opaque(dentry
);
224 static int ovl_set_opaque(struct dentry
*dentry
, struct dentry
*upperdentry
)
227 * Fail with -EIO when trying to create opaque dir and upper doesn't
228 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
229 * return a specific error for noxattr case.
231 return ovl_set_opaque_xerr(dentry
, upperdentry
, -EIO
);
235 * Common operations required to be done after creation of file on upper.
236 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
237 * may not use to instantiate the new dentry.
239 static int ovl_instantiate(struct dentry
*dentry
, struct inode
*inode
,
240 struct dentry
*newdentry
, bool hardlink
)
242 struct ovl_inode_params oip
= {
243 .upperdentry
= newdentry
,
247 ovl_dir_modified(dentry
->d_parent
, false);
248 ovl_dentry_set_upper_alias(dentry
);
251 * ovl_obtain_alias() can be called after ovl_create_real()
252 * and before we get here, so we may get an inode from cache
253 * with the same real upperdentry that is not the inode we
254 * pre-allocated. In this case we will use the cached inode
255 * to instantiate the new dentry.
257 * XXX: if we ever use ovl_obtain_alias() to decode directory
258 * file handles, need to use ovl_get_inode_locked() and
259 * d_instantiate_new() here to prevent from creating two
260 * hashed directory inode aliases.
262 inode
= ovl_get_inode(dentry
->d_sb
, &oip
);
263 if (WARN_ON(IS_ERR(inode
)))
264 return PTR_ERR(inode
);
266 WARN_ON(ovl_inode_real(inode
) != d_inode(newdentry
));
271 d_instantiate(dentry
, inode
);
272 if (inode
!= oip
.newinode
) {
273 pr_warn_ratelimited("overlayfs: newly created inode found in cache (%pd2)\n",
277 /* Force lookup of new upper hardlink to find its lower */
284 static bool ovl_type_merge(struct dentry
*dentry
)
286 return OVL_TYPE_MERGE(ovl_path_type(dentry
));
289 static bool ovl_type_origin(struct dentry
*dentry
)
291 return OVL_TYPE_ORIGIN(ovl_path_type(dentry
));
294 static int ovl_create_upper(struct dentry
*dentry
, struct inode
*inode
,
295 struct ovl_cattr
*attr
)
297 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
298 struct inode
*udir
= upperdir
->d_inode
;
299 struct dentry
*newdentry
;
302 if (!attr
->hardlink
&& !IS_POSIXACL(udir
))
303 attr
->mode
&= ~current_umask();
305 inode_lock_nested(udir
, I_MUTEX_PARENT
);
306 newdentry
= ovl_create_real(udir
,
307 lookup_one_len(dentry
->d_name
.name
,
311 err
= PTR_ERR(newdentry
);
312 if (IS_ERR(newdentry
))
315 if (ovl_type_merge(dentry
->d_parent
) && d_is_dir(newdentry
)) {
316 /* Setting opaque here is just an optimization, allow to fail */
317 ovl_set_opaque(dentry
, newdentry
);
320 err
= ovl_instantiate(dentry
, inode
, newdentry
, !!attr
->hardlink
);
328 ovl_cleanup(udir
, newdentry
);
333 static struct dentry
*ovl_clear_empty(struct dentry
*dentry
,
334 struct list_head
*list
)
336 struct dentry
*workdir
= ovl_workdir(dentry
);
337 struct inode
*wdir
= workdir
->d_inode
;
338 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
339 struct inode
*udir
= upperdir
->d_inode
;
340 struct path upperpath
;
341 struct dentry
*upper
;
342 struct dentry
*opaquedir
;
346 if (WARN_ON(!workdir
))
347 return ERR_PTR(-EROFS
);
349 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
353 ovl_path_upper(dentry
, &upperpath
);
354 err
= vfs_getattr(&upperpath
, &stat
,
355 STATX_BASIC_STATS
, AT_STATX_SYNC_AS_STAT
);
360 if (!S_ISDIR(stat
.mode
))
362 upper
= upperpath
.dentry
;
363 if (upper
->d_parent
->d_inode
!= udir
)
366 opaquedir
= ovl_create_temp(workdir
, OVL_CATTR(stat
.mode
));
367 err
= PTR_ERR(opaquedir
);
368 if (IS_ERR(opaquedir
))
371 err
= ovl_copy_xattr(upper
, opaquedir
);
375 err
= ovl_set_opaque(dentry
, opaquedir
);
379 inode_lock(opaquedir
->d_inode
);
380 err
= ovl_set_attr(opaquedir
, &stat
);
381 inode_unlock(opaquedir
->d_inode
);
385 err
= ovl_do_rename(wdir
, opaquedir
, udir
, upper
, RENAME_EXCHANGE
);
389 ovl_cleanup_whiteouts(upper
, list
);
390 ovl_cleanup(wdir
, upper
);
391 unlock_rename(workdir
, upperdir
);
393 /* dentry's upper doesn't match now, get rid of it */
399 ovl_cleanup(wdir
, opaquedir
);
402 unlock_rename(workdir
, upperdir
);
407 static int ovl_set_upper_acl(struct dentry
*upperdentry
, const char *name
,
408 const struct posix_acl
*acl
)
414 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !acl
)
417 size
= posix_acl_to_xattr(NULL
, acl
, NULL
, 0);
418 buffer
= kmalloc(size
, GFP_KERNEL
);
422 size
= posix_acl_to_xattr(&init_user_ns
, acl
, buffer
, size
);
427 err
= vfs_setxattr(upperdentry
, name
, buffer
, size
, XATTR_CREATE
);
433 static int ovl_create_over_whiteout(struct dentry
*dentry
, struct inode
*inode
,
434 struct ovl_cattr
*cattr
)
436 struct dentry
*workdir
= ovl_workdir(dentry
);
437 struct inode
*wdir
= workdir
->d_inode
;
438 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
439 struct inode
*udir
= upperdir
->d_inode
;
440 struct dentry
*upper
;
441 struct dentry
*newdentry
;
443 struct posix_acl
*acl
, *default_acl
;
444 bool hardlink
= !!cattr
->hardlink
;
446 if (WARN_ON(!workdir
))
450 err
= posix_acl_create(dentry
->d_parent
->d_inode
,
451 &cattr
->mode
, &default_acl
, &acl
);
456 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
460 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
462 err
= PTR_ERR(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_link(struct dentry
*old
, struct inode
*newdir
,
658 err
= ovl_want_write(old
);
662 err
= ovl_copy_up(old
);
666 if (ovl_is_metacopy_dentry(old
)) {
667 err
= ovl_set_redirect(old
, false);
672 err
= ovl_nlink_start(old
, &locked
);
676 inode
= d_inode(old
);
679 err
= ovl_create_or_link(new, inode
,
680 &(struct ovl_cattr
) {.hardlink
= ovl_dentry_upper(old
)},
681 ovl_type_origin(old
));
685 ovl_nlink_end(old
, locked
);
692 static bool ovl_matches_upper(struct dentry
*dentry
, struct dentry
*upper
)
694 return d_inode(ovl_dentry_upper(dentry
)) == d_inode(upper
);
697 static int ovl_remove_and_whiteout(struct dentry
*dentry
,
698 struct list_head
*list
)
700 struct dentry
*workdir
= ovl_workdir(dentry
);
701 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
702 struct dentry
*upper
;
703 struct dentry
*opaquedir
= NULL
;
706 if (WARN_ON(!workdir
))
709 if (!list_empty(list
)) {
710 opaquedir
= ovl_clear_empty(dentry
, list
);
711 err
= PTR_ERR(opaquedir
);
712 if (IS_ERR(opaquedir
))
716 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
720 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
722 err
= PTR_ERR(upper
);
727 if ((opaquedir
&& upper
!= opaquedir
) ||
728 (!opaquedir
&& ovl_dentry_upper(dentry
) &&
729 !ovl_matches_upper(dentry
, upper
))) {
733 err
= ovl_cleanup_and_whiteout(workdir
, d_inode(upperdir
), upper
);
737 ovl_dir_modified(dentry
->d_parent
, true);
743 unlock_rename(workdir
, upperdir
);
750 static int ovl_remove_upper(struct dentry
*dentry
, bool is_dir
,
751 struct list_head
*list
)
753 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
754 struct inode
*dir
= upperdir
->d_inode
;
755 struct dentry
*upper
;
756 struct dentry
*opaquedir
= NULL
;
759 if (!list_empty(list
)) {
760 opaquedir
= ovl_clear_empty(dentry
, list
);
761 err
= PTR_ERR(opaquedir
);
762 if (IS_ERR(opaquedir
))
766 inode_lock_nested(dir
, I_MUTEX_PARENT
);
767 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
769 err
= PTR_ERR(upper
);
774 if ((opaquedir
&& upper
!= opaquedir
) ||
775 (!opaquedir
&& !ovl_matches_upper(dentry
, upper
)))
779 err
= vfs_rmdir(dir
, upper
);
781 err
= vfs_unlink(dir
, upper
, NULL
);
782 ovl_dir_modified(dentry
->d_parent
, ovl_type_origin(dentry
));
785 * Keeping this dentry hashed would mean having to release
786 * upperpath/lowerpath, which could only be done if we are the
787 * sole user of this dentry. Too tricky... Just unhash for
801 static bool ovl_pure_upper(struct dentry
*dentry
)
803 return !ovl_dentry_lower(dentry
) &&
804 !ovl_test_flag(OVL_WHITEOUTS
, d_inode(dentry
));
807 static int ovl_do_remove(struct dentry
*dentry
, bool is_dir
)
811 const struct cred
*old_cred
;
812 struct dentry
*upperdentry
;
813 bool lower_positive
= ovl_lower_positive(dentry
);
816 /* No need to clean pure upper removed by vfs_rmdir() */
817 if (is_dir
&& (lower_positive
|| !ovl_pure_upper(dentry
))) {
818 err
= ovl_check_empty_dir(dentry
, &list
);
823 err
= ovl_want_write(dentry
);
827 err
= ovl_copy_up(dentry
->d_parent
);
831 err
= ovl_nlink_start(dentry
, &locked
);
835 old_cred
= ovl_override_creds(dentry
->d_sb
);
837 err
= ovl_remove_upper(dentry
, is_dir
, &list
);
839 err
= ovl_remove_and_whiteout(dentry
, &list
);
840 revert_creds(old_cred
);
843 clear_nlink(dentry
->d_inode
);
845 drop_nlink(dentry
->d_inode
);
847 ovl_nlink_end(dentry
, locked
);
852 * Note: we fail to update ctime if there was no copy-up, only a
855 upperdentry
= ovl_dentry_upper(dentry
);
857 ovl_copyattr(d_inode(upperdentry
), d_inode(dentry
));
860 ovl_drop_write(dentry
);
862 ovl_cache_free(&list
);
866 static int ovl_unlink(struct inode
*dir
, struct dentry
*dentry
)
868 return ovl_do_remove(dentry
, false);
871 static int ovl_rmdir(struct inode
*dir
, struct dentry
*dentry
)
873 return ovl_do_remove(dentry
, true);
876 static bool ovl_type_merge_or_lower(struct dentry
*dentry
)
878 enum ovl_path_type type
= ovl_path_type(dentry
);
880 return OVL_TYPE_MERGE(type
) || !OVL_TYPE_UPPER(type
);
883 static bool ovl_can_move(struct dentry
*dentry
)
885 return ovl_redirect_dir(dentry
->d_sb
) ||
886 !d_is_dir(dentry
) || !ovl_type_merge_or_lower(dentry
);
889 static char *ovl_get_redirect(struct dentry
*dentry
, bool abs_redirect
)
892 struct dentry
*d
, *tmp
;
893 int buflen
= ovl_redirect_max
+ 1;
896 ret
= kstrndup(dentry
->d_name
.name
, dentry
->d_name
.len
,
901 buf
= ret
= kmalloc(buflen
, GFP_KERNEL
);
907 for (d
= dget(dentry
); !IS_ROOT(d
);) {
911 spin_lock(&d
->d_lock
);
912 name
= ovl_dentry_get_redirect(d
);
914 thislen
= strlen(name
);
916 name
= d
->d_name
.name
;
917 thislen
= d
->d_name
.len
;
920 /* If path is too long, fall back to userspace move */
921 if (thislen
+ (name
[0] != '/') > buflen
) {
922 ret
= ERR_PTR(-EXDEV
);
923 spin_unlock(&d
->d_lock
);
928 memcpy(&buf
[buflen
], name
, thislen
);
929 tmp
= dget_dlock(d
->d_parent
);
930 spin_unlock(&d
->d_lock
);
935 /* Absolute redirect: finished */
936 if (buf
[buflen
] == '/')
941 ret
= kstrdup(&buf
[buflen
], GFP_KERNEL
);
946 return ret
? ret
: ERR_PTR(-ENOMEM
);
949 static bool ovl_need_absolute_redirect(struct dentry
*dentry
, bool samedir
)
951 struct dentry
*lowerdentry
;
956 if (d_is_dir(dentry
))
960 * For non-dir hardlinked files, we need absolute redirects
961 * in general as two upper hardlinks could be in different
962 * dirs. We could put a relative redirect now and convert
963 * it to absolute redirect later. But when nlink > 1 and
964 * indexing is on, that means relative redirect needs to be
965 * converted to absolute during copy up of another lower
968 * So without optimizing too much, just check if lower is
969 * a hard link or not. If lower is hard link, put absolute
972 lowerdentry
= ovl_dentry_lower(dentry
);
973 return (d_inode(lowerdentry
)->i_nlink
> 1);
976 static int ovl_set_redirect(struct dentry
*dentry
, bool samedir
)
979 const char *redirect
= ovl_dentry_get_redirect(dentry
);
980 bool absolute_redirect
= ovl_need_absolute_redirect(dentry
, samedir
);
982 if (redirect
&& (!absolute_redirect
|| redirect
[0] == '/'))
985 redirect
= ovl_get_redirect(dentry
, absolute_redirect
);
986 if (IS_ERR(redirect
))
987 return PTR_ERR(redirect
);
989 err
= ovl_check_setxattr(dentry
, ovl_dentry_upper(dentry
),
991 redirect
, strlen(redirect
), -EXDEV
);
993 spin_lock(&dentry
->d_lock
);
994 ovl_dentry_set_redirect(dentry
, redirect
);
995 spin_unlock(&dentry
->d_lock
);
998 pr_warn_ratelimited("overlayfs: failed to set redirect (%i)\n",
1000 /* Fall back to userspace copy-up */
1006 static int ovl_rename(struct inode
*olddir
, struct dentry
*old
,
1007 struct inode
*newdir
, struct dentry
*new,
1011 bool locked
= false;
1012 struct dentry
*old_upperdir
;
1013 struct dentry
*new_upperdir
;
1014 struct dentry
*olddentry
;
1015 struct dentry
*newdentry
;
1016 struct dentry
*trap
;
1019 bool cleanup_whiteout
= false;
1020 bool overwrite
= !(flags
& RENAME_EXCHANGE
);
1021 bool is_dir
= d_is_dir(old
);
1022 bool new_is_dir
= d_is_dir(new);
1023 bool samedir
= olddir
== newdir
;
1024 struct dentry
*opaquedir
= NULL
;
1025 const struct cred
*old_cred
= NULL
;
1029 if (flags
& ~(RENAME_EXCHANGE
| RENAME_NOREPLACE
))
1032 flags
&= ~RENAME_NOREPLACE
;
1034 /* Don't copy up directory trees */
1036 if (!ovl_can_move(old
))
1038 if (!overwrite
&& !ovl_can_move(new))
1041 if (overwrite
&& new_is_dir
&& !ovl_pure_upper(new)) {
1042 err
= ovl_check_empty_dir(new, &list
);
1048 if (ovl_lower_positive(old
)) {
1049 if (!ovl_dentry_is_whiteout(new)) {
1050 /* Whiteout source */
1051 flags
|= RENAME_WHITEOUT
;
1053 /* Switch whiteouts */
1054 flags
|= RENAME_EXCHANGE
;
1056 } else if (is_dir
&& ovl_dentry_is_whiteout(new)) {
1057 flags
|= RENAME_EXCHANGE
;
1058 cleanup_whiteout
= true;
1062 err
= ovl_want_write(old
);
1066 err
= ovl_copy_up(old
);
1068 goto out_drop_write
;
1070 err
= ovl_copy_up(new->d_parent
);
1072 goto out_drop_write
;
1074 err
= ovl_copy_up(new);
1076 goto out_drop_write
;
1078 err
= ovl_nlink_start(new, &locked
);
1080 goto out_drop_write
;
1083 old_cred
= ovl_override_creds(old
->d_sb
);
1085 if (!list_empty(&list
)) {
1086 opaquedir
= ovl_clear_empty(new, &list
);
1087 err
= PTR_ERR(opaquedir
);
1088 if (IS_ERR(opaquedir
)) {
1090 goto out_revert_creds
;
1094 old_upperdir
= ovl_dentry_upper(old
->d_parent
);
1095 new_upperdir
= ovl_dentry_upper(new->d_parent
);
1099 * When moving a merge dir or non-dir with copy up origin into
1100 * a new parent, we are marking the new parent dir "impure".
1101 * When ovl_iterate() iterates an "impure" upper dir, it will
1102 * lookup the origin inodes of the entries to fill d_ino.
1104 if (ovl_type_origin(old
)) {
1105 err
= ovl_set_impure(new->d_parent
, new_upperdir
);
1107 goto out_revert_creds
;
1109 if (!overwrite
&& ovl_type_origin(new)) {
1110 err
= ovl_set_impure(old
->d_parent
, old_upperdir
);
1112 goto out_revert_creds
;
1116 trap
= lock_rename(new_upperdir
, old_upperdir
);
1118 olddentry
= lookup_one_len(old
->d_name
.name
, old_upperdir
,
1120 err
= PTR_ERR(olddentry
);
1121 if (IS_ERR(olddentry
))
1125 if (!ovl_matches_upper(old
, olddentry
))
1128 newdentry
= lookup_one_len(new->d_name
.name
, new_upperdir
,
1130 err
= PTR_ERR(newdentry
);
1131 if (IS_ERR(newdentry
))
1134 old_opaque
= ovl_dentry_is_opaque(old
);
1135 new_opaque
= ovl_dentry_is_opaque(new);
1138 if (d_inode(new) && ovl_dentry_upper(new)) {
1140 if (newdentry
!= opaquedir
)
1143 if (!ovl_matches_upper(new, newdentry
))
1147 if (!d_is_negative(newdentry
) &&
1148 (!new_opaque
|| !ovl_is_whiteout(newdentry
)))
1152 if (olddentry
== trap
)
1154 if (newdentry
== trap
)
1157 if (WARN_ON(olddentry
->d_inode
== newdentry
->d_inode
))
1161 if (ovl_type_merge_or_lower(old
))
1162 err
= ovl_set_redirect(old
, samedir
);
1163 else if (is_dir
&& !old_opaque
&& ovl_type_merge(new->d_parent
))
1164 err
= ovl_set_opaque_xerr(old
, olddentry
, -EXDEV
);
1168 if (!overwrite
&& ovl_type_merge_or_lower(new))
1169 err
= ovl_set_redirect(new, samedir
);
1170 else if (!overwrite
&& new_is_dir
&& !new_opaque
&&
1171 ovl_type_merge(old
->d_parent
))
1172 err
= ovl_set_opaque_xerr(new, newdentry
, -EXDEV
);
1176 err
= ovl_do_rename(old_upperdir
->d_inode
, olddentry
,
1177 new_upperdir
->d_inode
, newdentry
, flags
);
1181 if (cleanup_whiteout
)
1182 ovl_cleanup(old_upperdir
->d_inode
, newdentry
);
1184 if (overwrite
&& d_inode(new)) {
1186 clear_nlink(d_inode(new));
1188 drop_nlink(d_inode(new));
1191 ovl_dir_modified(old
->d_parent
, ovl_type_origin(old
) ||
1192 (!overwrite
&& ovl_type_origin(new)));
1193 ovl_dir_modified(new->d_parent
, ovl_type_origin(old
) ||
1194 (d_inode(new) && ovl_type_origin(new)));
1197 ovl_copyattr(d_inode(olddentry
), d_inode(old
));
1198 if (d_inode(new) && ovl_dentry_upper(new))
1199 ovl_copyattr(d_inode(newdentry
), d_inode(new));
1206 unlock_rename(new_upperdir
, old_upperdir
);
1208 revert_creds(old_cred
);
1209 ovl_nlink_end(new, locked
);
1211 ovl_drop_write(old
);
1214 ovl_cache_free(&list
);
1218 const struct inode_operations ovl_dir_inode_operations
= {
1219 .lookup
= ovl_lookup
,
1221 .symlink
= ovl_symlink
,
1222 .unlink
= ovl_unlink
,
1224 .rename
= ovl_rename
,
1226 .setattr
= ovl_setattr
,
1227 .create
= ovl_create
,
1229 .permission
= ovl_permission
,
1230 .getattr
= ovl_getattr
,
1231 .listxattr
= ovl_listxattr
,
1232 .get_acl
= ovl_get_acl
,
1233 .update_time
= ovl_update_time
,