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
,
238 err
= ovl_check_setxattr(dentry
, upper
, OVL_XATTR_OPAQUE
, "y", 1, xerr
);
240 ovl_dentry_set_opaque(dentry
);
245 static int ovl_set_opaque(struct dentry
*dentry
, struct dentry
*upperdentry
)
248 * Fail with -EIO when trying to create opaque dir and upper doesn't
249 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
250 * return a specific error for noxattr case.
252 return ovl_set_opaque_xerr(dentry
, upperdentry
, -EIO
);
256 * Common operations required to be done after creation of file on upper.
257 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
258 * may not use to instantiate the new dentry.
260 static int ovl_instantiate(struct dentry
*dentry
, struct inode
*inode
,
261 struct dentry
*newdentry
, bool hardlink
)
263 struct ovl_inode_params oip
= {
264 .upperdentry
= newdentry
,
268 ovl_dir_modified(dentry
->d_parent
, false);
269 ovl_dentry_set_upper_alias(dentry
);
270 ovl_dentry_update_reval(dentry
, newdentry
,
271 DCACHE_OP_REVALIDATE
| DCACHE_OP_WEAK_REVALIDATE
);
275 * ovl_obtain_alias() can be called after ovl_create_real()
276 * and before we get here, so we may get an inode from cache
277 * with the same real upperdentry that is not the inode we
278 * pre-allocated. In this case we will use the cached inode
279 * to instantiate the new dentry.
281 * XXX: if we ever use ovl_obtain_alias() to decode directory
282 * file handles, need to use ovl_get_inode_locked() and
283 * d_instantiate_new() here to prevent from creating two
284 * hashed directory inode aliases.
286 inode
= ovl_get_inode(dentry
->d_sb
, &oip
);
288 return PTR_ERR(inode
);
289 if (inode
== oip
.newinode
)
290 ovl_set_flag(OVL_UPPERDATA
, inode
);
292 WARN_ON(ovl_inode_real(inode
) != d_inode(newdentry
));
297 d_instantiate(dentry
, inode
);
298 if (inode
!= oip
.newinode
) {
299 pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
303 /* Force lookup of new upper hardlink to find its lower */
310 static bool ovl_type_merge(struct dentry
*dentry
)
312 return OVL_TYPE_MERGE(ovl_path_type(dentry
));
315 static bool ovl_type_origin(struct dentry
*dentry
)
317 return OVL_TYPE_ORIGIN(ovl_path_type(dentry
));
320 static int ovl_create_upper(struct dentry
*dentry
, struct inode
*inode
,
321 struct ovl_cattr
*attr
)
323 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
324 struct inode
*udir
= upperdir
->d_inode
;
325 struct dentry
*newdentry
;
328 if (!attr
->hardlink
&& !IS_POSIXACL(udir
))
329 attr
->mode
&= ~current_umask();
331 inode_lock_nested(udir
, I_MUTEX_PARENT
);
332 newdentry
= ovl_create_real(udir
,
333 lookup_one_len(dentry
->d_name
.name
,
337 err
= PTR_ERR(newdentry
);
338 if (IS_ERR(newdentry
))
341 if (ovl_type_merge(dentry
->d_parent
) && d_is_dir(newdentry
)) {
342 /* Setting opaque here is just an optimization, allow to fail */
343 ovl_set_opaque(dentry
, newdentry
);
346 err
= ovl_instantiate(dentry
, inode
, newdentry
, !!attr
->hardlink
);
354 ovl_cleanup(udir
, newdentry
);
359 static struct dentry
*ovl_clear_empty(struct dentry
*dentry
,
360 struct list_head
*list
)
362 struct dentry
*workdir
= ovl_workdir(dentry
);
363 struct inode
*wdir
= workdir
->d_inode
;
364 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
365 struct inode
*udir
= upperdir
->d_inode
;
366 struct path upperpath
;
367 struct dentry
*upper
;
368 struct dentry
*opaquedir
;
372 if (WARN_ON(!workdir
))
373 return ERR_PTR(-EROFS
);
375 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
379 ovl_path_upper(dentry
, &upperpath
);
380 err
= vfs_getattr(&upperpath
, &stat
,
381 STATX_BASIC_STATS
, AT_STATX_SYNC_AS_STAT
);
386 if (!S_ISDIR(stat
.mode
))
388 upper
= upperpath
.dentry
;
389 if (upper
->d_parent
->d_inode
!= udir
)
392 opaquedir
= ovl_create_temp(workdir
, OVL_CATTR(stat
.mode
));
393 err
= PTR_ERR(opaquedir
);
394 if (IS_ERR(opaquedir
))
397 err
= ovl_copy_xattr(dentry
->d_sb
, upper
, opaquedir
);
401 err
= ovl_set_opaque(dentry
, opaquedir
);
405 inode_lock(opaquedir
->d_inode
);
406 err
= ovl_set_attr(opaquedir
, &stat
);
407 inode_unlock(opaquedir
->d_inode
);
411 err
= ovl_do_rename(wdir
, opaquedir
, udir
, upper
, RENAME_EXCHANGE
);
415 ovl_cleanup_whiteouts(upper
, list
);
416 ovl_cleanup(wdir
, upper
);
417 unlock_rename(workdir
, upperdir
);
419 /* dentry's upper doesn't match now, get rid of it */
425 ovl_cleanup(wdir
, opaquedir
);
428 unlock_rename(workdir
, upperdir
);
433 static int ovl_set_upper_acl(struct dentry
*upperdentry
, const char *name
,
434 const struct posix_acl
*acl
)
440 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !acl
)
443 size
= posix_acl_xattr_size(acl
->a_count
);
444 buffer
= kmalloc(size
, GFP_KERNEL
);
448 err
= posix_acl_to_xattr(&init_user_ns
, acl
, buffer
, size
);
452 err
= vfs_setxattr(upperdentry
, name
, buffer
, size
, XATTR_CREATE
);
458 static int ovl_create_over_whiteout(struct dentry
*dentry
, struct inode
*inode
,
459 struct ovl_cattr
*cattr
)
461 struct dentry
*workdir
= ovl_workdir(dentry
);
462 struct inode
*wdir
= workdir
->d_inode
;
463 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
464 struct inode
*udir
= upperdir
->d_inode
;
465 struct dentry
*upper
;
466 struct dentry
*newdentry
;
468 struct posix_acl
*acl
, *default_acl
;
469 bool hardlink
= !!cattr
->hardlink
;
471 if (WARN_ON(!workdir
))
475 err
= posix_acl_create(dentry
->d_parent
->d_inode
,
476 &cattr
->mode
, &default_acl
, &acl
);
481 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
485 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
487 err
= PTR_ERR(upper
);
492 if (d_is_negative(upper
) || !IS_WHITEOUT(d_inode(upper
)))
495 newdentry
= ovl_create_temp(workdir
, cattr
);
496 err
= PTR_ERR(newdentry
);
497 if (IS_ERR(newdentry
))
501 * mode could have been mutilated due to umask (e.g. sgid directory)
504 !S_ISLNK(cattr
->mode
) &&
505 newdentry
->d_inode
->i_mode
!= cattr
->mode
) {
506 struct iattr attr
= {
507 .ia_valid
= ATTR_MODE
,
508 .ia_mode
= cattr
->mode
,
510 inode_lock(newdentry
->d_inode
);
511 err
= notify_change(newdentry
, &attr
, NULL
);
512 inode_unlock(newdentry
->d_inode
);
517 err
= ovl_set_upper_acl(newdentry
, XATTR_NAME_POSIX_ACL_ACCESS
,
522 err
= ovl_set_upper_acl(newdentry
, XATTR_NAME_POSIX_ACL_DEFAULT
,
528 if (!hardlink
&& S_ISDIR(cattr
->mode
)) {
529 err
= ovl_set_opaque(dentry
, newdentry
);
533 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
,
538 ovl_cleanup(wdir
, upper
);
540 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
, 0);
544 err
= ovl_instantiate(dentry
, inode
, newdentry
, hardlink
);
550 unlock_rename(workdir
, upperdir
);
553 posix_acl_release(acl
);
554 posix_acl_release(default_acl
);
559 ovl_cleanup(wdir
, newdentry
);
564 static int ovl_create_or_link(struct dentry
*dentry
, struct inode
*inode
,
565 struct ovl_cattr
*attr
, bool origin
)
568 const struct cred
*old_cred
;
569 struct cred
*override_cred
;
570 struct dentry
*parent
= dentry
->d_parent
;
572 err
= ovl_copy_up(parent
);
576 old_cred
= ovl_override_creds(dentry
->d_sb
);
579 * When linking a file with copy up origin into a new parent, mark the
580 * new parent dir "impure".
583 err
= ovl_set_impure(parent
, ovl_dentry_upper(parent
));
585 goto out_revert_creds
;
589 override_cred
= prepare_creds();
591 override_cred
->fsuid
= inode
->i_uid
;
592 override_cred
->fsgid
= inode
->i_gid
;
593 if (!attr
->hardlink
) {
594 err
= security_dentry_create_files_as(dentry
,
595 attr
->mode
, &dentry
->d_name
, old_cred
,
598 put_cred(override_cred
);
599 goto out_revert_creds
;
602 put_cred(override_creds(override_cred
));
603 put_cred(override_cred
);
605 if (!ovl_dentry_is_whiteout(dentry
))
606 err
= ovl_create_upper(dentry
, inode
, attr
);
608 err
= ovl_create_over_whiteout(dentry
, inode
, attr
);
611 revert_creds(old_cred
);
615 static int ovl_create_object(struct dentry
*dentry
, int mode
, dev_t rdev
,
620 struct ovl_cattr attr
= {
625 err
= ovl_want_write(dentry
);
629 /* Preallocate inode to be used by ovl_get_inode() */
631 inode
= ovl_new_inode(dentry
->d_sb
, mode
, rdev
);
635 spin_lock(&inode
->i_lock
);
636 inode
->i_state
|= I_CREATING
;
637 spin_unlock(&inode
->i_lock
);
639 inode_init_owner(inode
, dentry
->d_parent
->d_inode
, mode
);
640 attr
.mode
= inode
->i_mode
;
642 err
= ovl_create_or_link(dentry
, inode
, &attr
, false);
643 /* Did we end up using the preallocated inode? */
644 if (inode
!= d_inode(dentry
))
648 ovl_drop_write(dentry
);
653 static int ovl_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
656 return ovl_create_object(dentry
, (mode
& 07777) | S_IFREG
, 0, NULL
);
659 static int ovl_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
661 return ovl_create_object(dentry
, (mode
& 07777) | S_IFDIR
, 0, NULL
);
664 static int ovl_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
667 /* Don't allow creation of "whiteout" on overlay */
668 if (S_ISCHR(mode
) && rdev
== WHITEOUT_DEV
)
671 return ovl_create_object(dentry
, mode
, rdev
, NULL
);
674 static int ovl_symlink(struct inode
*dir
, struct dentry
*dentry
,
677 return ovl_create_object(dentry
, S_IFLNK
, 0, link
);
680 static int ovl_set_link_redirect(struct dentry
*dentry
)
682 const struct cred
*old_cred
;
685 old_cred
= ovl_override_creds(dentry
->d_sb
);
686 err
= ovl_set_redirect(dentry
, false);
687 revert_creds(old_cred
);
692 static int ovl_link(struct dentry
*old
, struct inode
*newdir
,
698 err
= ovl_want_write(old
);
702 err
= ovl_copy_up(old
);
706 err
= ovl_copy_up(new->d_parent
);
710 if (ovl_is_metacopy_dentry(old
)) {
711 err
= ovl_set_link_redirect(old
);
716 err
= ovl_nlink_start(old
);
720 inode
= d_inode(old
);
723 err
= ovl_create_or_link(new, inode
,
724 &(struct ovl_cattr
) {.hardlink
= ovl_dentry_upper(old
)},
725 ovl_type_origin(old
));
736 static bool ovl_matches_upper(struct dentry
*dentry
, struct dentry
*upper
)
738 return d_inode(ovl_dentry_upper(dentry
)) == d_inode(upper
);
741 static int ovl_remove_and_whiteout(struct dentry
*dentry
,
742 struct list_head
*list
)
744 struct ovl_fs
*ofs
= OVL_FS(dentry
->d_sb
);
745 struct dentry
*workdir
= ovl_workdir(dentry
);
746 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
747 struct dentry
*upper
;
748 struct dentry
*opaquedir
= NULL
;
751 if (WARN_ON(!workdir
))
754 if (!list_empty(list
)) {
755 opaquedir
= ovl_clear_empty(dentry
, list
);
756 err
= PTR_ERR(opaquedir
);
757 if (IS_ERR(opaquedir
))
761 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
765 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
767 err
= PTR_ERR(upper
);
772 if ((opaquedir
&& upper
!= opaquedir
) ||
773 (!opaquedir
&& ovl_dentry_upper(dentry
) &&
774 !ovl_matches_upper(dentry
, upper
))) {
778 err
= ovl_cleanup_and_whiteout(ofs
, d_inode(upperdir
), upper
);
782 ovl_dir_modified(dentry
->d_parent
, true);
788 unlock_rename(workdir
, upperdir
);
795 static int ovl_remove_upper(struct dentry
*dentry
, bool is_dir
,
796 struct list_head
*list
)
798 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
799 struct inode
*dir
= upperdir
->d_inode
;
800 struct dentry
*upper
;
801 struct dentry
*opaquedir
= NULL
;
804 if (!list_empty(list
)) {
805 opaquedir
= ovl_clear_empty(dentry
, list
);
806 err
= PTR_ERR(opaquedir
);
807 if (IS_ERR(opaquedir
))
811 inode_lock_nested(dir
, I_MUTEX_PARENT
);
812 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
814 err
= PTR_ERR(upper
);
819 if ((opaquedir
&& upper
!= opaquedir
) ||
820 (!opaquedir
&& !ovl_matches_upper(dentry
, upper
)))
824 err
= vfs_rmdir(dir
, upper
);
826 err
= vfs_unlink(dir
, upper
, NULL
);
827 ovl_dir_modified(dentry
->d_parent
, ovl_type_origin(dentry
));
830 * Keeping this dentry hashed would mean having to release
831 * upperpath/lowerpath, which could only be done if we are the
832 * sole user of this dentry. Too tricky... Just unhash for
846 static bool ovl_pure_upper(struct dentry
*dentry
)
848 return !ovl_dentry_lower(dentry
) &&
849 !ovl_test_flag(OVL_WHITEOUTS
, d_inode(dentry
));
852 static void ovl_drop_nlink(struct dentry
*dentry
)
854 struct inode
*inode
= d_inode(dentry
);
855 struct dentry
*alias
;
857 /* Try to find another, hashed alias */
858 spin_lock(&inode
->i_lock
);
859 hlist_for_each_entry(alias
, &inode
->i_dentry
, d_u
.d_alias
) {
860 if (alias
!= dentry
&& !d_unhashed(alias
))
863 spin_unlock(&inode
->i_lock
);
866 * Changes to underlying layers may cause i_nlink to lose sync with
867 * reality. In this case prevent the link count from going to zero
870 if (inode
->i_nlink
> !!alias
)
874 static int ovl_do_remove(struct dentry
*dentry
, bool is_dir
)
877 const struct cred
*old_cred
;
878 struct dentry
*upperdentry
;
879 bool lower_positive
= ovl_lower_positive(dentry
);
882 /* No need to clean pure upper removed by vfs_rmdir() */
883 if (is_dir
&& (lower_positive
|| !ovl_pure_upper(dentry
))) {
884 err
= ovl_check_empty_dir(dentry
, &list
);
889 err
= ovl_want_write(dentry
);
893 err
= ovl_copy_up(dentry
->d_parent
);
897 err
= ovl_nlink_start(dentry
);
901 old_cred
= ovl_override_creds(dentry
->d_sb
);
903 err
= ovl_remove_upper(dentry
, is_dir
, &list
);
905 err
= ovl_remove_and_whiteout(dentry
, &list
);
906 revert_creds(old_cred
);
909 clear_nlink(dentry
->d_inode
);
911 ovl_drop_nlink(dentry
);
913 ovl_nlink_end(dentry
);
918 * Note: we fail to update ctime if there was no copy-up, only a
921 upperdentry
= ovl_dentry_upper(dentry
);
923 ovl_copyattr(d_inode(upperdentry
), d_inode(dentry
));
926 ovl_drop_write(dentry
);
928 ovl_cache_free(&list
);
932 static int ovl_unlink(struct inode
*dir
, struct dentry
*dentry
)
934 return ovl_do_remove(dentry
, false);
937 static int ovl_rmdir(struct inode
*dir
, struct dentry
*dentry
)
939 return ovl_do_remove(dentry
, true);
942 static bool ovl_type_merge_or_lower(struct dentry
*dentry
)
944 enum ovl_path_type type
= ovl_path_type(dentry
);
946 return OVL_TYPE_MERGE(type
) || !OVL_TYPE_UPPER(type
);
949 static bool ovl_can_move(struct dentry
*dentry
)
951 return ovl_redirect_dir(dentry
->d_sb
) ||
952 !d_is_dir(dentry
) || !ovl_type_merge_or_lower(dentry
);
955 static char *ovl_get_redirect(struct dentry
*dentry
, bool abs_redirect
)
958 struct dentry
*d
, *tmp
;
959 int buflen
= ovl_redirect_max
+ 1;
962 ret
= kstrndup(dentry
->d_name
.name
, dentry
->d_name
.len
,
967 buf
= ret
= kmalloc(buflen
, GFP_KERNEL
);
973 for (d
= dget(dentry
); !IS_ROOT(d
);) {
977 spin_lock(&d
->d_lock
);
978 name
= ovl_dentry_get_redirect(d
);
980 thislen
= strlen(name
);
982 name
= d
->d_name
.name
;
983 thislen
= d
->d_name
.len
;
986 /* If path is too long, fall back to userspace move */
987 if (thislen
+ (name
[0] != '/') > buflen
) {
988 ret
= ERR_PTR(-EXDEV
);
989 spin_unlock(&d
->d_lock
);
994 memcpy(&buf
[buflen
], name
, thislen
);
995 tmp
= dget_dlock(d
->d_parent
);
996 spin_unlock(&d
->d_lock
);
1001 /* Absolute redirect: finished */
1002 if (buf
[buflen
] == '/')
1007 ret
= kstrdup(&buf
[buflen
], GFP_KERNEL
);
1012 return ret
? ret
: ERR_PTR(-ENOMEM
);
1015 static bool ovl_need_absolute_redirect(struct dentry
*dentry
, bool samedir
)
1017 struct dentry
*lowerdentry
;
1022 if (d_is_dir(dentry
))
1026 * For non-dir hardlinked files, we need absolute redirects
1027 * in general as two upper hardlinks could be in different
1028 * dirs. We could put a relative redirect now and convert
1029 * it to absolute redirect later. But when nlink > 1 and
1030 * indexing is on, that means relative redirect needs to be
1031 * converted to absolute during copy up of another lower
1032 * hardllink as well.
1034 * So without optimizing too much, just check if lower is
1035 * a hard link or not. If lower is hard link, put absolute
1038 lowerdentry
= ovl_dentry_lower(dentry
);
1039 return (d_inode(lowerdentry
)->i_nlink
> 1);
1042 static int ovl_set_redirect(struct dentry
*dentry
, bool samedir
)
1045 const char *redirect
= ovl_dentry_get_redirect(dentry
);
1046 bool absolute_redirect
= ovl_need_absolute_redirect(dentry
, samedir
);
1048 if (redirect
&& (!absolute_redirect
|| redirect
[0] == '/'))
1051 redirect
= ovl_get_redirect(dentry
, absolute_redirect
);
1052 if (IS_ERR(redirect
))
1053 return PTR_ERR(redirect
);
1055 err
= ovl_check_setxattr(dentry
, ovl_dentry_upper(dentry
),
1057 redirect
, strlen(redirect
), -EXDEV
);
1059 spin_lock(&dentry
->d_lock
);
1060 ovl_dentry_set_redirect(dentry
, redirect
);
1061 spin_unlock(&dentry
->d_lock
);
1064 pr_warn_ratelimited("failed to set redirect (%i)\n",
1066 /* Fall back to userspace copy-up */
1072 static int ovl_rename(struct inode
*olddir
, struct dentry
*old
,
1073 struct inode
*newdir
, struct dentry
*new,
1077 struct dentry
*old_upperdir
;
1078 struct dentry
*new_upperdir
;
1079 struct dentry
*olddentry
;
1080 struct dentry
*newdentry
;
1081 struct dentry
*trap
;
1084 bool cleanup_whiteout
= false;
1085 bool update_nlink
= false;
1086 bool overwrite
= !(flags
& RENAME_EXCHANGE
);
1087 bool is_dir
= d_is_dir(old
);
1088 bool new_is_dir
= d_is_dir(new);
1089 bool samedir
= olddir
== newdir
;
1090 struct dentry
*opaquedir
= NULL
;
1091 const struct cred
*old_cred
= NULL
;
1095 if (flags
& ~(RENAME_EXCHANGE
| RENAME_NOREPLACE
))
1098 flags
&= ~RENAME_NOREPLACE
;
1100 /* Don't copy up directory trees */
1102 if (!ovl_can_move(old
))
1104 if (!overwrite
&& !ovl_can_move(new))
1107 if (overwrite
&& new_is_dir
&& !ovl_pure_upper(new)) {
1108 err
= ovl_check_empty_dir(new, &list
);
1114 if (ovl_lower_positive(old
)) {
1115 if (!ovl_dentry_is_whiteout(new)) {
1116 /* Whiteout source */
1117 flags
|= RENAME_WHITEOUT
;
1119 /* Switch whiteouts */
1120 flags
|= RENAME_EXCHANGE
;
1122 } else if (is_dir
&& ovl_dentry_is_whiteout(new)) {
1123 flags
|= RENAME_EXCHANGE
;
1124 cleanup_whiteout
= true;
1128 err
= ovl_want_write(old
);
1132 err
= ovl_copy_up(old
);
1134 goto out_drop_write
;
1136 err
= ovl_copy_up(new->d_parent
);
1138 goto out_drop_write
;
1140 err
= ovl_copy_up(new);
1142 goto out_drop_write
;
1143 } else if (d_inode(new)) {
1144 err
= ovl_nlink_start(new);
1146 goto out_drop_write
;
1148 update_nlink
= true;
1151 old_cred
= ovl_override_creds(old
->d_sb
);
1153 if (!list_empty(&list
)) {
1154 opaquedir
= ovl_clear_empty(new, &list
);
1155 err
= PTR_ERR(opaquedir
);
1156 if (IS_ERR(opaquedir
)) {
1158 goto out_revert_creds
;
1162 old_upperdir
= ovl_dentry_upper(old
->d_parent
);
1163 new_upperdir
= ovl_dentry_upper(new->d_parent
);
1167 * When moving a merge dir or non-dir with copy up origin into
1168 * a new parent, we are marking the new parent dir "impure".
1169 * When ovl_iterate() iterates an "impure" upper dir, it will
1170 * lookup the origin inodes of the entries to fill d_ino.
1172 if (ovl_type_origin(old
)) {
1173 err
= ovl_set_impure(new->d_parent
, new_upperdir
);
1175 goto out_revert_creds
;
1177 if (!overwrite
&& ovl_type_origin(new)) {
1178 err
= ovl_set_impure(old
->d_parent
, old_upperdir
);
1180 goto out_revert_creds
;
1184 trap
= lock_rename(new_upperdir
, old_upperdir
);
1186 olddentry
= lookup_one_len(old
->d_name
.name
, old_upperdir
,
1188 err
= PTR_ERR(olddentry
);
1189 if (IS_ERR(olddentry
))
1193 if (!ovl_matches_upper(old
, olddentry
))
1196 newdentry
= lookup_one_len(new->d_name
.name
, new_upperdir
,
1198 err
= PTR_ERR(newdentry
);
1199 if (IS_ERR(newdentry
))
1202 old_opaque
= ovl_dentry_is_opaque(old
);
1203 new_opaque
= ovl_dentry_is_opaque(new);
1206 if (d_inode(new) && ovl_dentry_upper(new)) {
1208 if (newdentry
!= opaquedir
)
1211 if (!ovl_matches_upper(new, newdentry
))
1215 if (!d_is_negative(newdentry
) &&
1216 (!new_opaque
|| !ovl_is_whiteout(newdentry
)))
1220 if (olddentry
== trap
)
1222 if (newdentry
== trap
)
1225 if (olddentry
->d_inode
== newdentry
->d_inode
)
1229 if (ovl_type_merge_or_lower(old
))
1230 err
= ovl_set_redirect(old
, samedir
);
1231 else if (is_dir
&& !old_opaque
&& ovl_type_merge(new->d_parent
))
1232 err
= ovl_set_opaque_xerr(old
, olddentry
, -EXDEV
);
1236 if (!overwrite
&& ovl_type_merge_or_lower(new))
1237 err
= ovl_set_redirect(new, samedir
);
1238 else if (!overwrite
&& new_is_dir
&& !new_opaque
&&
1239 ovl_type_merge(old
->d_parent
))
1240 err
= ovl_set_opaque_xerr(new, newdentry
, -EXDEV
);
1244 err
= ovl_do_rename(old_upperdir
->d_inode
, olddentry
,
1245 new_upperdir
->d_inode
, newdentry
, flags
);
1249 if (cleanup_whiteout
)
1250 ovl_cleanup(old_upperdir
->d_inode
, newdentry
);
1252 if (overwrite
&& d_inode(new)) {
1254 clear_nlink(d_inode(new));
1256 ovl_drop_nlink(new);
1259 ovl_dir_modified(old
->d_parent
, ovl_type_origin(old
) ||
1260 (!overwrite
&& ovl_type_origin(new)));
1261 ovl_dir_modified(new->d_parent
, ovl_type_origin(old
) ||
1262 (d_inode(new) && ovl_type_origin(new)));
1265 ovl_copyattr(d_inode(olddentry
), d_inode(old
));
1266 if (d_inode(new) && ovl_dentry_upper(new))
1267 ovl_copyattr(d_inode(newdentry
), d_inode(new));
1274 unlock_rename(new_upperdir
, old_upperdir
);
1276 revert_creds(old_cred
);
1280 ovl_drop_write(old
);
1283 ovl_cache_free(&list
);
1287 const struct inode_operations ovl_dir_inode_operations
= {
1288 .lookup
= ovl_lookup
,
1290 .symlink
= ovl_symlink
,
1291 .unlink
= ovl_unlink
,
1293 .rename
= ovl_rename
,
1295 .setattr
= ovl_setattr
,
1296 .create
= ovl_create
,
1298 .permission
= ovl_permission
,
1299 .getattr
= ovl_getattr
,
1300 .listxattr
= ovl_listxattr
,
1301 .get_acl
= ovl_get_acl
,
1302 .update_time
= ovl_update_time
,