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 int ovl_cleanup(struct inode
*wdir
, struct dentry
*wdentry
)
32 if (d_is_dir(wdentry
))
33 err
= ovl_do_rmdir(wdir
, wdentry
);
35 err
= ovl_do_unlink(wdir
, wdentry
);
39 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
46 struct dentry
*ovl_lookup_temp(struct dentry
*workdir
)
50 static atomic_t temp_id
= ATOMIC_INIT(0);
52 /* counter is allowed to wrap, since temp dentries are ephemeral */
53 snprintf(name
, sizeof(name
), "#%x", atomic_inc_return(&temp_id
));
55 temp
= lookup_one_len(name
, workdir
, strlen(name
));
56 if (!IS_ERR(temp
) && temp
->d_inode
) {
57 pr_err("overlayfs: workdir/%s already exists\n", name
);
65 /* caller holds i_mutex on workdir */
66 static struct dentry
*ovl_whiteout(struct dentry
*workdir
,
67 struct dentry
*dentry
)
70 struct dentry
*whiteout
;
71 struct inode
*wdir
= workdir
->d_inode
;
73 whiteout
= ovl_lookup_temp(workdir
);
77 err
= ovl_do_whiteout(wdir
, whiteout
);
80 whiteout
= ERR_PTR(err
);
86 int ovl_create_real(struct inode
*dir
, struct dentry
*newdentry
,
87 struct cattr
*attr
, struct dentry
*hardlink
, bool debug
)
91 if (newdentry
->d_inode
)
95 err
= ovl_do_link(hardlink
, dir
, newdentry
, debug
);
97 switch (attr
->mode
& S_IFMT
) {
99 err
= ovl_do_create(dir
, newdentry
, attr
->mode
, debug
);
103 err
= ovl_do_mkdir(dir
, newdentry
, attr
->mode
, debug
);
110 err
= ovl_do_mknod(dir
, newdentry
,
111 attr
->mode
, attr
->rdev
, debug
);
115 err
= ovl_do_symlink(dir
, newdentry
, attr
->link
, debug
);
122 if (!err
&& WARN_ON(!newdentry
->d_inode
)) {
124 * Not quite sure if non-instantiated dentry is legal or not.
125 * VFS doesn't seem to care so check and warn here.
132 static int ovl_set_opaque_xerr(struct dentry
*dentry
, struct dentry
*upper
,
137 err
= ovl_check_setxattr(dentry
, upper
, OVL_XATTR_OPAQUE
, "y", 1, xerr
);
139 ovl_dentry_set_opaque(dentry
);
144 static int ovl_set_opaque(struct dentry
*dentry
, struct dentry
*upperdentry
)
147 * Fail with -EIO when trying to create opaque dir and upper doesn't
148 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
149 * return a specific error for noxattr case.
151 return ovl_set_opaque_xerr(dentry
, upperdentry
, -EIO
);
154 /* Common operations required to be done after creation of file on upper */
155 static void ovl_instantiate(struct dentry
*dentry
, struct inode
*inode
,
156 struct dentry
*newdentry
, bool hardlink
)
158 ovl_dentry_version_inc(dentry
->d_parent
, false);
159 ovl_dentry_set_upper_alias(dentry
);
161 ovl_inode_update(inode
, newdentry
);
162 ovl_copyattr(newdentry
->d_inode
, inode
);
164 WARN_ON(ovl_inode_real(inode
) != d_inode(newdentry
));
168 d_instantiate(dentry
, inode
);
169 /* Force lookup of new upper hardlink to find its lower */
174 static bool ovl_type_merge(struct dentry
*dentry
)
176 return OVL_TYPE_MERGE(ovl_path_type(dentry
));
179 static bool ovl_type_origin(struct dentry
*dentry
)
181 return OVL_TYPE_ORIGIN(ovl_path_type(dentry
));
184 static bool ovl_may_have_whiteouts(struct dentry
*dentry
)
186 return ovl_test_flag(OVL_WHITEOUTS
, d_inode(dentry
));
189 static int ovl_create_upper(struct dentry
*dentry
, struct inode
*inode
,
190 struct cattr
*attr
, struct dentry
*hardlink
)
192 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
193 struct inode
*udir
= upperdir
->d_inode
;
194 struct dentry
*newdentry
;
197 if (!hardlink
&& !IS_POSIXACL(udir
))
198 attr
->mode
&= ~current_umask();
200 inode_lock_nested(udir
, I_MUTEX_PARENT
);
201 newdentry
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
203 err
= PTR_ERR(newdentry
);
204 if (IS_ERR(newdentry
))
206 err
= ovl_create_real(udir
, newdentry
, attr
, hardlink
, false);
210 if (ovl_type_merge(dentry
->d_parent
) && d_is_dir(newdentry
)) {
211 /* Setting opaque here is just an optimization, allow to fail */
212 ovl_set_opaque(dentry
, newdentry
);
215 ovl_instantiate(dentry
, inode
, newdentry
, !!hardlink
);
224 static struct dentry
*ovl_clear_empty(struct dentry
*dentry
,
225 struct list_head
*list
)
227 struct dentry
*workdir
= ovl_workdir(dentry
);
228 struct inode
*wdir
= workdir
->d_inode
;
229 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
230 struct inode
*udir
= upperdir
->d_inode
;
231 struct path upperpath
;
232 struct dentry
*upper
;
233 struct dentry
*opaquedir
;
237 if (WARN_ON(!workdir
))
238 return ERR_PTR(-EROFS
);
240 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
244 ovl_path_upper(dentry
, &upperpath
);
245 err
= vfs_getattr(&upperpath
, &stat
,
246 STATX_BASIC_STATS
, AT_STATX_SYNC_AS_STAT
);
251 if (!S_ISDIR(stat
.mode
))
253 upper
= upperpath
.dentry
;
254 if (upper
->d_parent
->d_inode
!= udir
)
257 opaquedir
= ovl_lookup_temp(workdir
);
258 err
= PTR_ERR(opaquedir
);
259 if (IS_ERR(opaquedir
))
262 err
= ovl_create_real(wdir
, opaquedir
,
263 &(struct cattr
){.mode
= stat
.mode
}, NULL
, true);
267 err
= ovl_copy_xattr(upper
, opaquedir
);
271 err
= ovl_set_opaque(dentry
, opaquedir
);
275 inode_lock(opaquedir
->d_inode
);
276 err
= ovl_set_attr(opaquedir
, &stat
);
277 inode_unlock(opaquedir
->d_inode
);
281 err
= ovl_do_rename(wdir
, opaquedir
, udir
, upper
, RENAME_EXCHANGE
);
285 ovl_cleanup_whiteouts(upper
, list
);
286 ovl_cleanup(wdir
, upper
);
287 unlock_rename(workdir
, upperdir
);
289 /* dentry's upper doesn't match now, get rid of it */
295 ovl_cleanup(wdir
, opaquedir
);
299 unlock_rename(workdir
, upperdir
);
304 static struct dentry
*ovl_check_empty_and_clear(struct dentry
*dentry
)
307 struct dentry
*ret
= NULL
;
310 err
= ovl_check_empty_dir(dentry
, &list
);
317 * When removing an empty opaque directory, then it makes no sense to
318 * replace it with an exact replica of itself.
320 * If upperdentry has whiteouts, clear them.
322 * Can race with copy-up, since we don't hold the upperdir mutex.
323 * Doesn't matter, since copy-up can't create a non-empty directory
326 if (!list_empty(&list
))
327 ret
= ovl_clear_empty(dentry
, &list
);
330 ovl_cache_free(&list
);
335 static int ovl_set_upper_acl(struct dentry
*upperdentry
, const char *name
,
336 const struct posix_acl
*acl
)
342 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !acl
)
345 size
= posix_acl_to_xattr(NULL
, acl
, NULL
, 0);
346 buffer
= kmalloc(size
, GFP_KERNEL
);
350 size
= posix_acl_to_xattr(&init_user_ns
, acl
, buffer
, size
);
355 err
= vfs_setxattr(upperdentry
, name
, buffer
, size
, XATTR_CREATE
);
361 static int ovl_create_over_whiteout(struct dentry
*dentry
, struct inode
*inode
,
363 struct dentry
*hardlink
)
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 dentry
*upper
;
370 struct dentry
*newdentry
;
372 struct posix_acl
*acl
, *default_acl
;
374 if (WARN_ON(!workdir
))
378 err
= posix_acl_create(dentry
->d_parent
->d_inode
,
379 &cattr
->mode
, &default_acl
, &acl
);
384 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
388 newdentry
= ovl_lookup_temp(workdir
);
389 err
= PTR_ERR(newdentry
);
390 if (IS_ERR(newdentry
))
393 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
395 err
= PTR_ERR(upper
);
399 err
= ovl_create_real(wdir
, newdentry
, cattr
, hardlink
, true);
404 * mode could have been mutilated due to umask (e.g. sgid directory)
407 !S_ISLNK(cattr
->mode
) &&
408 newdentry
->d_inode
->i_mode
!= cattr
->mode
) {
409 struct iattr attr
= {
410 .ia_valid
= ATTR_MODE
,
411 .ia_mode
= cattr
->mode
,
413 inode_lock(newdentry
->d_inode
);
414 err
= notify_change(newdentry
, &attr
, NULL
);
415 inode_unlock(newdentry
->d_inode
);
420 err
= ovl_set_upper_acl(newdentry
, XATTR_NAME_POSIX_ACL_ACCESS
,
425 err
= ovl_set_upper_acl(newdentry
, XATTR_NAME_POSIX_ACL_DEFAULT
,
431 if (!hardlink
&& S_ISDIR(cattr
->mode
)) {
432 err
= ovl_set_opaque(dentry
, newdentry
);
436 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
,
441 ovl_cleanup(wdir
, upper
);
443 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
, 0);
447 ovl_instantiate(dentry
, inode
, newdentry
, !!hardlink
);
454 unlock_rename(workdir
, upperdir
);
457 posix_acl_release(acl
);
458 posix_acl_release(default_acl
);
463 ovl_cleanup(wdir
, newdentry
);
467 static int ovl_create_or_link(struct dentry
*dentry
, struct inode
*inode
,
468 struct cattr
*attr
, struct dentry
*hardlink
,
472 const struct cred
*old_cred
;
473 struct cred
*override_cred
;
474 struct dentry
*parent
= dentry
->d_parent
;
476 err
= ovl_copy_up(parent
);
480 old_cred
= ovl_override_creds(dentry
->d_sb
);
483 * When linking a file with copy up origin into a new parent, mark the
484 * new parent dir "impure".
487 err
= ovl_set_impure(parent
, ovl_dentry_upper(parent
));
489 goto out_revert_creds
;
493 override_cred
= prepare_creds();
495 override_cred
->fsuid
= inode
->i_uid
;
496 override_cred
->fsgid
= inode
->i_gid
;
498 err
= security_dentry_create_files_as(dentry
,
499 attr
->mode
, &dentry
->d_name
, old_cred
,
502 put_cred(override_cred
);
503 goto out_revert_creds
;
506 put_cred(override_creds(override_cred
));
507 put_cred(override_cred
);
509 if (!ovl_dentry_is_whiteout(dentry
))
510 err
= ovl_create_upper(dentry
, inode
, attr
,
513 err
= ovl_create_over_whiteout(dentry
, inode
, attr
,
517 revert_creds(old_cred
);
519 struct inode
*realinode
= d_inode(ovl_dentry_upper(dentry
));
521 WARN_ON(inode
->i_mode
!= realinode
->i_mode
);
522 WARN_ON(!uid_eq(inode
->i_uid
, realinode
->i_uid
));
523 WARN_ON(!gid_eq(inode
->i_gid
, realinode
->i_gid
));
528 static int ovl_create_object(struct dentry
*dentry
, int mode
, dev_t rdev
,
533 struct cattr attr
= {
538 err
= ovl_want_write(dentry
);
543 inode
= ovl_new_inode(dentry
->d_sb
, mode
, rdev
);
547 inode_init_owner(inode
, dentry
->d_parent
->d_inode
, mode
);
548 attr
.mode
= inode
->i_mode
;
550 err
= ovl_create_or_link(dentry
, inode
, &attr
, NULL
, false);
555 ovl_drop_write(dentry
);
560 static int ovl_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
563 return ovl_create_object(dentry
, (mode
& 07777) | S_IFREG
, 0, NULL
);
566 static int ovl_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
568 return ovl_create_object(dentry
, (mode
& 07777) | S_IFDIR
, 0, NULL
);
571 static int ovl_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
574 /* Don't allow creation of "whiteout" on overlay */
575 if (S_ISCHR(mode
) && rdev
== WHITEOUT_DEV
)
578 return ovl_create_object(dentry
, mode
, rdev
, NULL
);
581 static int ovl_symlink(struct inode
*dir
, struct dentry
*dentry
,
584 return ovl_create_object(dentry
, S_IFLNK
, 0, link
);
587 static int ovl_link(struct dentry
*old
, struct inode
*newdir
,
594 err
= ovl_want_write(old
);
598 err
= ovl_copy_up(old
);
602 err
= ovl_nlink_start(old
, &locked
);
606 inode
= d_inode(old
);
609 err
= ovl_create_or_link(new, inode
, NULL
, ovl_dentry_upper(old
),
610 ovl_type_origin(old
));
614 ovl_nlink_end(old
, locked
);
621 static bool ovl_matches_upper(struct dentry
*dentry
, struct dentry
*upper
)
623 return d_inode(ovl_dentry_upper(dentry
)) == d_inode(upper
);
626 static int ovl_remove_and_whiteout(struct dentry
*dentry
, bool is_dir
)
628 struct dentry
*workdir
= ovl_workdir(dentry
);
629 struct inode
*wdir
= workdir
->d_inode
;
630 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
631 struct inode
*udir
= upperdir
->d_inode
;
632 struct dentry
*whiteout
;
633 struct dentry
*upper
;
634 struct dentry
*opaquedir
= NULL
;
638 if (WARN_ON(!workdir
))
642 opaquedir
= ovl_check_empty_and_clear(dentry
);
643 err
= PTR_ERR(opaquedir
);
644 if (IS_ERR(opaquedir
))
648 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
652 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
654 err
= PTR_ERR(upper
);
659 if ((opaquedir
&& upper
!= opaquedir
) ||
660 (!opaquedir
&& ovl_dentry_upper(dentry
) &&
661 !ovl_matches_upper(dentry
, upper
))) {
665 whiteout
= ovl_whiteout(workdir
, dentry
);
666 err
= PTR_ERR(whiteout
);
667 if (IS_ERR(whiteout
))
671 flags
= RENAME_EXCHANGE
;
673 err
= ovl_do_rename(wdir
, whiteout
, udir
, upper
, flags
);
677 ovl_cleanup(wdir
, upper
);
679 ovl_dentry_version_inc(dentry
->d_parent
, true);
686 unlock_rename(workdir
, upperdir
);
693 ovl_cleanup(wdir
, whiteout
);
697 static int ovl_remove_upper(struct dentry
*dentry
, bool is_dir
)
699 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
700 struct inode
*dir
= upperdir
->d_inode
;
701 struct dentry
*upper
;
702 struct dentry
*opaquedir
= NULL
;
705 /* Redirect/origin dir can be !ovl_lower_positive && not clean */
706 if (is_dir
&& (ovl_dentry_get_redirect(dentry
) ||
707 ovl_may_have_whiteouts(dentry
))) {
708 opaquedir
= ovl_check_empty_and_clear(dentry
);
709 err
= PTR_ERR(opaquedir
);
710 if (IS_ERR(opaquedir
))
714 inode_lock_nested(dir
, I_MUTEX_PARENT
);
715 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
717 err
= PTR_ERR(upper
);
722 if ((opaquedir
&& upper
!= opaquedir
) ||
723 (!opaquedir
&& !ovl_matches_upper(dentry
, upper
)))
727 err
= vfs_rmdir(dir
, upper
);
729 err
= vfs_unlink(dir
, upper
, NULL
);
730 ovl_dentry_version_inc(dentry
->d_parent
, ovl_type_origin(dentry
));
733 * Keeping this dentry hashed would mean having to release
734 * upperpath/lowerpath, which could only be done if we are the
735 * sole user of this dentry. Too tricky... Just unhash for
749 static int ovl_do_remove(struct dentry
*dentry
, bool is_dir
)
753 const struct cred
*old_cred
;
755 err
= ovl_want_write(dentry
);
759 err
= ovl_copy_up(dentry
->d_parent
);
763 err
= ovl_nlink_start(dentry
, &locked
);
767 old_cred
= ovl_override_creds(dentry
->d_sb
);
768 if (!ovl_lower_positive(dentry
))
769 err
= ovl_remove_upper(dentry
, is_dir
);
771 err
= ovl_remove_and_whiteout(dentry
, is_dir
);
772 revert_creds(old_cred
);
775 clear_nlink(dentry
->d_inode
);
777 drop_nlink(dentry
->d_inode
);
779 ovl_nlink_end(dentry
, locked
);
781 ovl_drop_write(dentry
);
786 static int ovl_unlink(struct inode
*dir
, struct dentry
*dentry
)
788 return ovl_do_remove(dentry
, false);
791 static int ovl_rmdir(struct inode
*dir
, struct dentry
*dentry
)
793 return ovl_do_remove(dentry
, true);
796 static bool ovl_type_merge_or_lower(struct dentry
*dentry
)
798 enum ovl_path_type type
= ovl_path_type(dentry
);
800 return OVL_TYPE_MERGE(type
) || !OVL_TYPE_UPPER(type
);
803 static bool ovl_can_move(struct dentry
*dentry
)
805 return ovl_redirect_dir(dentry
->d_sb
) ||
806 !d_is_dir(dentry
) || !ovl_type_merge_or_lower(dentry
);
809 static char *ovl_get_redirect(struct dentry
*dentry
, bool samedir
)
812 struct dentry
*d
, *tmp
;
813 int buflen
= ovl_redirect_max
+ 1;
816 ret
= kstrndup(dentry
->d_name
.name
, dentry
->d_name
.len
,
821 buf
= ret
= kmalloc(buflen
, GFP_KERNEL
);
827 for (d
= dget(dentry
); !IS_ROOT(d
);) {
831 spin_lock(&d
->d_lock
);
832 name
= ovl_dentry_get_redirect(d
);
834 thislen
= strlen(name
);
836 name
= d
->d_name
.name
;
837 thislen
= d
->d_name
.len
;
840 /* If path is too long, fall back to userspace move */
841 if (thislen
+ (name
[0] != '/') > buflen
) {
842 ret
= ERR_PTR(-EXDEV
);
843 spin_unlock(&d
->d_lock
);
848 memcpy(&buf
[buflen
], name
, thislen
);
849 tmp
= dget_dlock(d
->d_parent
);
850 spin_unlock(&d
->d_lock
);
855 /* Absolute redirect: finished */
856 if (buf
[buflen
] == '/')
861 ret
= kstrdup(&buf
[buflen
], GFP_KERNEL
);
866 return ret
? ret
: ERR_PTR(-ENOMEM
);
869 static int ovl_set_redirect(struct dentry
*dentry
, bool samedir
)
872 const char *redirect
= ovl_dentry_get_redirect(dentry
);
874 if (redirect
&& (samedir
|| redirect
[0] == '/'))
877 redirect
= ovl_get_redirect(dentry
, samedir
);
878 if (IS_ERR(redirect
))
879 return PTR_ERR(redirect
);
881 err
= ovl_check_setxattr(dentry
, ovl_dentry_upper(dentry
),
883 redirect
, strlen(redirect
), -EXDEV
);
885 spin_lock(&dentry
->d_lock
);
886 ovl_dentry_set_redirect(dentry
, redirect
);
887 spin_unlock(&dentry
->d_lock
);
890 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err
);
891 /* Fall back to userspace copy-up */
897 static int ovl_rename(struct inode
*olddir
, struct dentry
*old
,
898 struct inode
*newdir
, struct dentry
*new,
903 struct dentry
*old_upperdir
;
904 struct dentry
*new_upperdir
;
905 struct dentry
*olddentry
;
906 struct dentry
*newdentry
;
910 bool cleanup_whiteout
= false;
911 bool overwrite
= !(flags
& RENAME_EXCHANGE
);
912 bool is_dir
= d_is_dir(old
);
913 bool new_is_dir
= d_is_dir(new);
914 bool samedir
= olddir
== newdir
;
915 struct dentry
*opaquedir
= NULL
;
916 const struct cred
*old_cred
= NULL
;
919 if (flags
& ~(RENAME_EXCHANGE
| RENAME_NOREPLACE
))
922 flags
&= ~RENAME_NOREPLACE
;
924 /* Don't copy up directory trees */
926 if (!ovl_can_move(old
))
928 if (!overwrite
&& !ovl_can_move(new))
931 err
= ovl_want_write(old
);
935 err
= ovl_copy_up(old
);
939 err
= ovl_copy_up(new->d_parent
);
943 err
= ovl_copy_up(new);
947 err
= ovl_nlink_start(new, &locked
);
952 old_cred
= ovl_override_creds(old
->d_sb
);
954 if (overwrite
&& new_is_dir
&& (ovl_type_merge_or_lower(new) ||
955 ovl_may_have_whiteouts(new))) {
956 opaquedir
= ovl_check_empty_and_clear(new);
957 err
= PTR_ERR(opaquedir
);
958 if (IS_ERR(opaquedir
)) {
960 goto out_revert_creds
;
965 if (ovl_lower_positive(old
)) {
966 if (!ovl_dentry_is_whiteout(new)) {
967 /* Whiteout source */
968 flags
|= RENAME_WHITEOUT
;
970 /* Switch whiteouts */
971 flags
|= RENAME_EXCHANGE
;
973 } else if (is_dir
&& ovl_dentry_is_whiteout(new)) {
974 flags
|= RENAME_EXCHANGE
;
975 cleanup_whiteout
= true;
979 old_upperdir
= ovl_dentry_upper(old
->d_parent
);
980 new_upperdir
= ovl_dentry_upper(new->d_parent
);
984 * When moving a merge dir or non-dir with copy up origin into
985 * a new parent, we are marking the new parent dir "impure".
986 * When ovl_iterate() iterates an "impure" upper dir, it will
987 * lookup the origin inodes of the entries to fill d_ino.
989 if (ovl_type_origin(old
)) {
990 err
= ovl_set_impure(new->d_parent
, new_upperdir
);
992 goto out_revert_creds
;
994 if (!overwrite
&& ovl_type_origin(new)) {
995 err
= ovl_set_impure(old
->d_parent
, old_upperdir
);
997 goto out_revert_creds
;
1001 trap
= lock_rename(new_upperdir
, old_upperdir
);
1003 olddentry
= lookup_one_len(old
->d_name
.name
, old_upperdir
,
1005 err
= PTR_ERR(olddentry
);
1006 if (IS_ERR(olddentry
))
1010 if (!ovl_matches_upper(old
, olddentry
))
1013 newdentry
= lookup_one_len(new->d_name
.name
, new_upperdir
,
1015 err
= PTR_ERR(newdentry
);
1016 if (IS_ERR(newdentry
))
1019 old_opaque
= ovl_dentry_is_opaque(old
);
1020 new_opaque
= ovl_dentry_is_opaque(new);
1023 if (d_inode(new) && ovl_dentry_upper(new)) {
1025 if (newdentry
!= opaquedir
)
1028 if (!ovl_matches_upper(new, newdentry
))
1032 if (!d_is_negative(newdentry
) &&
1033 (!new_opaque
|| !ovl_is_whiteout(newdentry
)))
1037 if (olddentry
== trap
)
1039 if (newdentry
== trap
)
1042 if (WARN_ON(olddentry
->d_inode
== newdentry
->d_inode
))
1047 if (ovl_type_merge_or_lower(old
))
1048 err
= ovl_set_redirect(old
, samedir
);
1049 else if (!old_opaque
&& ovl_type_merge(new->d_parent
))
1050 err
= ovl_set_opaque_xerr(old
, olddentry
, -EXDEV
);
1054 if (!overwrite
&& new_is_dir
) {
1055 if (ovl_type_merge_or_lower(new))
1056 err
= ovl_set_redirect(new, samedir
);
1057 else if (!new_opaque
&& ovl_type_merge(old
->d_parent
))
1058 err
= ovl_set_opaque_xerr(new, newdentry
, -EXDEV
);
1063 err
= ovl_do_rename(old_upperdir
->d_inode
, olddentry
,
1064 new_upperdir
->d_inode
, newdentry
, flags
);
1068 if (cleanup_whiteout
)
1069 ovl_cleanup(old_upperdir
->d_inode
, newdentry
);
1071 if (overwrite
&& d_inode(new)) {
1073 clear_nlink(d_inode(new));
1075 drop_nlink(d_inode(new));
1078 ovl_dentry_version_inc(old
->d_parent
, ovl_type_origin(old
) ||
1079 (!overwrite
&& ovl_type_origin(new)));
1080 ovl_dentry_version_inc(new->d_parent
, ovl_type_origin(old
) ||
1081 (d_inode(new) && ovl_type_origin(new)));
1088 unlock_rename(new_upperdir
, old_upperdir
);
1090 revert_creds(old_cred
);
1091 ovl_nlink_end(new, locked
);
1093 ovl_drop_write(old
);
1099 const struct inode_operations ovl_dir_inode_operations
= {
1100 .lookup
= ovl_lookup
,
1102 .symlink
= ovl_symlink
,
1103 .unlink
= ovl_unlink
,
1105 .rename
= ovl_rename
,
1107 .setattr
= ovl_setattr
,
1108 .create
= ovl_create
,
1110 .permission
= ovl_permission
,
1111 .getattr
= ovl_getattr
,
1112 .listxattr
= ovl_listxattr
,
1113 .get_acl
= ovl_get_acl
,
1114 .update_time
= ovl_update_time
,