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
)
69 struct dentry
*whiteout
;
70 struct inode
*wdir
= workdir
->d_inode
;
72 whiteout
= ovl_lookup_temp(workdir
);
76 err
= ovl_do_whiteout(wdir
, whiteout
);
79 whiteout
= ERR_PTR(err
);
85 /* Caller must hold i_mutex on both workdir and dir */
86 int ovl_cleanup_and_whiteout(struct dentry
*workdir
, struct inode
*dir
,
87 struct dentry
*dentry
)
89 struct inode
*wdir
= workdir
->d_inode
;
90 struct dentry
*whiteout
;
94 whiteout
= ovl_whiteout(workdir
);
95 err
= PTR_ERR(whiteout
);
100 flags
= RENAME_EXCHANGE
;
102 err
= ovl_do_rename(wdir
, whiteout
, dir
, dentry
, flags
);
106 ovl_cleanup(wdir
, dentry
);
113 ovl_cleanup(wdir
, whiteout
);
117 int ovl_create_real(struct inode
*dir
, struct dentry
*newdentry
,
118 struct cattr
*attr
, struct dentry
*hardlink
, bool debug
)
122 if (newdentry
->d_inode
)
126 err
= ovl_do_link(hardlink
, dir
, newdentry
, debug
);
128 switch (attr
->mode
& S_IFMT
) {
130 err
= ovl_do_create(dir
, newdentry
, attr
->mode
, debug
);
134 err
= ovl_do_mkdir(dir
, newdentry
, attr
->mode
, debug
);
141 err
= ovl_do_mknod(dir
, newdentry
,
142 attr
->mode
, attr
->rdev
, debug
);
146 err
= ovl_do_symlink(dir
, newdentry
, attr
->link
, debug
);
153 if (!err
&& WARN_ON(!newdentry
->d_inode
)) {
155 * Not quite sure if non-instantiated dentry is legal or not.
156 * VFS doesn't seem to care so check and warn here.
163 static int ovl_set_opaque_xerr(struct dentry
*dentry
, struct dentry
*upper
,
168 err
= ovl_check_setxattr(dentry
, upper
, OVL_XATTR_OPAQUE
, "y", 1, xerr
);
170 ovl_dentry_set_opaque(dentry
);
175 static int ovl_set_opaque(struct dentry
*dentry
, struct dentry
*upperdentry
)
178 * Fail with -EIO when trying to create opaque dir and upper doesn't
179 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
180 * return a specific error for noxattr case.
182 return ovl_set_opaque_xerr(dentry
, upperdentry
, -EIO
);
185 /* Common operations required to be done after creation of file on upper */
186 static void ovl_instantiate(struct dentry
*dentry
, struct inode
*inode
,
187 struct dentry
*newdentry
, bool hardlink
)
189 ovl_dentry_version_inc(dentry
->d_parent
, false);
190 ovl_dentry_set_upper_alias(dentry
);
192 ovl_inode_update(inode
, newdentry
);
193 ovl_copyattr(newdentry
->d_inode
, inode
);
195 WARN_ON(ovl_inode_real(inode
) != d_inode(newdentry
));
199 d_instantiate(dentry
, inode
);
200 /* Force lookup of new upper hardlink to find its lower */
205 static bool ovl_type_merge(struct dentry
*dentry
)
207 return OVL_TYPE_MERGE(ovl_path_type(dentry
));
210 static bool ovl_type_origin(struct dentry
*dentry
)
212 return OVL_TYPE_ORIGIN(ovl_path_type(dentry
));
215 static int ovl_create_upper(struct dentry
*dentry
, struct inode
*inode
,
216 struct cattr
*attr
, struct dentry
*hardlink
)
218 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
219 struct inode
*udir
= upperdir
->d_inode
;
220 struct dentry
*newdentry
;
223 if (!hardlink
&& !IS_POSIXACL(udir
))
224 attr
->mode
&= ~current_umask();
226 inode_lock_nested(udir
, I_MUTEX_PARENT
);
227 newdentry
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
229 err
= PTR_ERR(newdentry
);
230 if (IS_ERR(newdentry
))
232 err
= ovl_create_real(udir
, newdentry
, attr
, hardlink
, false);
236 if (ovl_type_merge(dentry
->d_parent
) && d_is_dir(newdentry
)) {
237 /* Setting opaque here is just an optimization, allow to fail */
238 ovl_set_opaque(dentry
, newdentry
);
241 ovl_instantiate(dentry
, inode
, newdentry
, !!hardlink
);
250 static struct dentry
*ovl_clear_empty(struct dentry
*dentry
,
251 struct list_head
*list
)
253 struct dentry
*workdir
= ovl_workdir(dentry
);
254 struct inode
*wdir
= workdir
->d_inode
;
255 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
256 struct inode
*udir
= upperdir
->d_inode
;
257 struct path upperpath
;
258 struct dentry
*upper
;
259 struct dentry
*opaquedir
;
263 if (WARN_ON(!workdir
))
264 return ERR_PTR(-EROFS
);
266 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
270 ovl_path_upper(dentry
, &upperpath
);
271 err
= vfs_getattr(&upperpath
, &stat
,
272 STATX_BASIC_STATS
, AT_STATX_SYNC_AS_STAT
);
277 if (!S_ISDIR(stat
.mode
))
279 upper
= upperpath
.dentry
;
280 if (upper
->d_parent
->d_inode
!= udir
)
283 opaquedir
= ovl_lookup_temp(workdir
);
284 err
= PTR_ERR(opaquedir
);
285 if (IS_ERR(opaquedir
))
288 err
= ovl_create_real(wdir
, opaquedir
,
289 &(struct cattr
){.mode
= stat
.mode
}, NULL
, true);
293 err
= ovl_copy_xattr(upper
, opaquedir
);
297 err
= ovl_set_opaque(dentry
, opaquedir
);
301 inode_lock(opaquedir
->d_inode
);
302 err
= ovl_set_attr(opaquedir
, &stat
);
303 inode_unlock(opaquedir
->d_inode
);
307 err
= ovl_do_rename(wdir
, opaquedir
, udir
, upper
, RENAME_EXCHANGE
);
311 ovl_cleanup_whiteouts(upper
, list
);
312 ovl_cleanup(wdir
, upper
);
313 unlock_rename(workdir
, upperdir
);
315 /* dentry's upper doesn't match now, get rid of it */
321 ovl_cleanup(wdir
, opaquedir
);
325 unlock_rename(workdir
, upperdir
);
330 static int ovl_set_upper_acl(struct dentry
*upperdentry
, const char *name
,
331 const struct posix_acl
*acl
)
337 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !acl
)
340 size
= posix_acl_to_xattr(NULL
, acl
, NULL
, 0);
341 buffer
= kmalloc(size
, GFP_KERNEL
);
345 size
= posix_acl_to_xattr(&init_user_ns
, acl
, buffer
, size
);
350 err
= vfs_setxattr(upperdentry
, name
, buffer
, size
, XATTR_CREATE
);
356 static int ovl_create_over_whiteout(struct dentry
*dentry
, struct inode
*inode
,
358 struct dentry
*hardlink
)
360 struct dentry
*workdir
= ovl_workdir(dentry
);
361 struct inode
*wdir
= workdir
->d_inode
;
362 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
363 struct inode
*udir
= upperdir
->d_inode
;
364 struct dentry
*upper
;
365 struct dentry
*newdentry
;
367 struct posix_acl
*acl
, *default_acl
;
369 if (WARN_ON(!workdir
))
373 err
= posix_acl_create(dentry
->d_parent
->d_inode
,
374 &cattr
->mode
, &default_acl
, &acl
);
379 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
383 newdentry
= ovl_lookup_temp(workdir
);
384 err
= PTR_ERR(newdentry
);
385 if (IS_ERR(newdentry
))
388 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
390 err
= PTR_ERR(upper
);
394 err
= ovl_create_real(wdir
, newdentry
, cattr
, hardlink
, true);
399 * mode could have been mutilated due to umask (e.g. sgid directory)
402 !S_ISLNK(cattr
->mode
) &&
403 newdentry
->d_inode
->i_mode
!= cattr
->mode
) {
404 struct iattr attr
= {
405 .ia_valid
= ATTR_MODE
,
406 .ia_mode
= cattr
->mode
,
408 inode_lock(newdentry
->d_inode
);
409 err
= notify_change(newdentry
, &attr
, NULL
);
410 inode_unlock(newdentry
->d_inode
);
415 err
= ovl_set_upper_acl(newdentry
, XATTR_NAME_POSIX_ACL_ACCESS
,
420 err
= ovl_set_upper_acl(newdentry
, XATTR_NAME_POSIX_ACL_DEFAULT
,
426 if (!hardlink
&& S_ISDIR(cattr
->mode
)) {
427 err
= ovl_set_opaque(dentry
, newdentry
);
431 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
,
436 ovl_cleanup(wdir
, upper
);
438 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
, 0);
442 ovl_instantiate(dentry
, inode
, newdentry
, !!hardlink
);
449 unlock_rename(workdir
, upperdir
);
452 posix_acl_release(acl
);
453 posix_acl_release(default_acl
);
458 ovl_cleanup(wdir
, newdentry
);
462 static int ovl_create_or_link(struct dentry
*dentry
, struct inode
*inode
,
463 struct cattr
*attr
, struct dentry
*hardlink
,
467 const struct cred
*old_cred
;
468 struct cred
*override_cred
;
469 struct dentry
*parent
= dentry
->d_parent
;
471 err
= ovl_copy_up(parent
);
475 old_cred
= ovl_override_creds(dentry
->d_sb
);
478 * When linking a file with copy up origin into a new parent, mark the
479 * new parent dir "impure".
482 err
= ovl_set_impure(parent
, ovl_dentry_upper(parent
));
484 goto out_revert_creds
;
488 override_cred
= prepare_creds();
490 override_cred
->fsuid
= inode
->i_uid
;
491 override_cred
->fsgid
= inode
->i_gid
;
493 err
= security_dentry_create_files_as(dentry
,
494 attr
->mode
, &dentry
->d_name
, old_cred
,
497 put_cred(override_cred
);
498 goto out_revert_creds
;
501 put_cred(override_creds(override_cred
));
502 put_cred(override_cred
);
504 if (!ovl_dentry_is_whiteout(dentry
))
505 err
= ovl_create_upper(dentry
, inode
, attr
,
508 err
= ovl_create_over_whiteout(dentry
, inode
, attr
,
512 revert_creds(old_cred
);
514 struct inode
*realinode
= d_inode(ovl_dentry_upper(dentry
));
516 WARN_ON(inode
->i_mode
!= realinode
->i_mode
);
517 WARN_ON(!uid_eq(inode
->i_uid
, realinode
->i_uid
));
518 WARN_ON(!gid_eq(inode
->i_gid
, realinode
->i_gid
));
523 static int ovl_create_object(struct dentry
*dentry
, int mode
, dev_t rdev
,
528 struct cattr attr
= {
533 err
= ovl_want_write(dentry
);
538 inode
= ovl_new_inode(dentry
->d_sb
, mode
, rdev
);
542 inode_init_owner(inode
, dentry
->d_parent
->d_inode
, mode
);
543 attr
.mode
= inode
->i_mode
;
545 err
= ovl_create_or_link(dentry
, inode
, &attr
, NULL
, false);
550 ovl_drop_write(dentry
);
555 static int ovl_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
558 return ovl_create_object(dentry
, (mode
& 07777) | S_IFREG
, 0, NULL
);
561 static int ovl_mkdir(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
)
563 return ovl_create_object(dentry
, (mode
& 07777) | S_IFDIR
, 0, NULL
);
566 static int ovl_mknod(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
569 /* Don't allow creation of "whiteout" on overlay */
570 if (S_ISCHR(mode
) && rdev
== WHITEOUT_DEV
)
573 return ovl_create_object(dentry
, mode
, rdev
, NULL
);
576 static int ovl_symlink(struct inode
*dir
, struct dentry
*dentry
,
579 return ovl_create_object(dentry
, S_IFLNK
, 0, link
);
582 static int ovl_link(struct dentry
*old
, struct inode
*newdir
,
589 err
= ovl_want_write(old
);
593 err
= ovl_copy_up(old
);
597 err
= ovl_nlink_start(old
, &locked
);
601 inode
= d_inode(old
);
604 err
= ovl_create_or_link(new, inode
, NULL
, ovl_dentry_upper(old
),
605 ovl_type_origin(old
));
609 ovl_nlink_end(old
, locked
);
616 static bool ovl_matches_upper(struct dentry
*dentry
, struct dentry
*upper
)
618 return d_inode(ovl_dentry_upper(dentry
)) == d_inode(upper
);
621 static int ovl_remove_and_whiteout(struct dentry
*dentry
,
622 struct list_head
*list
)
624 struct dentry
*workdir
= ovl_workdir(dentry
);
625 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
626 struct dentry
*upper
;
627 struct dentry
*opaquedir
= NULL
;
630 if (WARN_ON(!workdir
))
633 if (!list_empty(list
)) {
634 opaquedir
= ovl_clear_empty(dentry
, list
);
635 err
= PTR_ERR(opaquedir
);
636 if (IS_ERR(opaquedir
))
640 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
644 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
646 err
= PTR_ERR(upper
);
651 if ((opaquedir
&& upper
!= opaquedir
) ||
652 (!opaquedir
&& ovl_dentry_upper(dentry
) &&
653 !ovl_matches_upper(dentry
, upper
))) {
657 err
= ovl_cleanup_and_whiteout(workdir
, d_inode(upperdir
), upper
);
661 ovl_dentry_version_inc(dentry
->d_parent
, true);
667 unlock_rename(workdir
, upperdir
);
674 static int ovl_remove_upper(struct dentry
*dentry
, bool is_dir
,
675 struct list_head
*list
)
677 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
678 struct inode
*dir
= upperdir
->d_inode
;
679 struct dentry
*upper
;
680 struct dentry
*opaquedir
= NULL
;
683 if (!list_empty(list
)) {
684 opaquedir
= ovl_clear_empty(dentry
, list
);
685 err
= PTR_ERR(opaquedir
);
686 if (IS_ERR(opaquedir
))
690 inode_lock_nested(dir
, I_MUTEX_PARENT
);
691 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
693 err
= PTR_ERR(upper
);
698 if ((opaquedir
&& upper
!= opaquedir
) ||
699 (!opaquedir
&& !ovl_matches_upper(dentry
, upper
)))
703 err
= vfs_rmdir(dir
, upper
);
705 err
= vfs_unlink(dir
, upper
, NULL
);
706 ovl_dentry_version_inc(dentry
->d_parent
, ovl_type_origin(dentry
));
709 * Keeping this dentry hashed would mean having to release
710 * upperpath/lowerpath, which could only be done if we are the
711 * sole user of this dentry. Too tricky... Just unhash for
725 static bool ovl_pure_upper(struct dentry
*dentry
)
727 return !ovl_dentry_lower(dentry
) &&
728 !ovl_test_flag(OVL_WHITEOUTS
, d_inode(dentry
));
731 static int ovl_do_remove(struct dentry
*dentry
, bool is_dir
)
735 const struct cred
*old_cred
;
736 bool lower_positive
= ovl_lower_positive(dentry
);
739 /* No need to clean pure upper removed by vfs_rmdir() */
740 if (is_dir
&& (lower_positive
|| !ovl_pure_upper(dentry
))) {
741 err
= ovl_check_empty_dir(dentry
, &list
);
746 err
= ovl_want_write(dentry
);
750 err
= ovl_copy_up(dentry
->d_parent
);
754 err
= ovl_nlink_start(dentry
, &locked
);
758 old_cred
= ovl_override_creds(dentry
->d_sb
);
760 err
= ovl_remove_upper(dentry
, is_dir
, &list
);
762 err
= ovl_remove_and_whiteout(dentry
, &list
);
763 revert_creds(old_cred
);
766 clear_nlink(dentry
->d_inode
);
768 drop_nlink(dentry
->d_inode
);
770 ovl_nlink_end(dentry
, locked
);
772 ovl_drop_write(dentry
);
774 ovl_cache_free(&list
);
778 static int ovl_unlink(struct inode
*dir
, struct dentry
*dentry
)
780 return ovl_do_remove(dentry
, false);
783 static int ovl_rmdir(struct inode
*dir
, struct dentry
*dentry
)
785 return ovl_do_remove(dentry
, true);
788 static bool ovl_type_merge_or_lower(struct dentry
*dentry
)
790 enum ovl_path_type type
= ovl_path_type(dentry
);
792 return OVL_TYPE_MERGE(type
) || !OVL_TYPE_UPPER(type
);
795 static bool ovl_can_move(struct dentry
*dentry
)
797 return ovl_redirect_dir(dentry
->d_sb
) ||
798 !d_is_dir(dentry
) || !ovl_type_merge_or_lower(dentry
);
801 static char *ovl_get_redirect(struct dentry
*dentry
, bool samedir
)
804 struct dentry
*d
, *tmp
;
805 int buflen
= ovl_redirect_max
+ 1;
808 ret
= kstrndup(dentry
->d_name
.name
, dentry
->d_name
.len
,
813 buf
= ret
= kmalloc(buflen
, GFP_KERNEL
);
819 for (d
= dget(dentry
); !IS_ROOT(d
);) {
823 spin_lock(&d
->d_lock
);
824 name
= ovl_dentry_get_redirect(d
);
826 thislen
= strlen(name
);
828 name
= d
->d_name
.name
;
829 thislen
= d
->d_name
.len
;
832 /* If path is too long, fall back to userspace move */
833 if (thislen
+ (name
[0] != '/') > buflen
) {
834 ret
= ERR_PTR(-EXDEV
);
835 spin_unlock(&d
->d_lock
);
840 memcpy(&buf
[buflen
], name
, thislen
);
841 tmp
= dget_dlock(d
->d_parent
);
842 spin_unlock(&d
->d_lock
);
847 /* Absolute redirect: finished */
848 if (buf
[buflen
] == '/')
853 ret
= kstrdup(&buf
[buflen
], GFP_KERNEL
);
858 return ret
? ret
: ERR_PTR(-ENOMEM
);
861 static int ovl_set_redirect(struct dentry
*dentry
, bool samedir
)
864 const char *redirect
= ovl_dentry_get_redirect(dentry
);
866 if (redirect
&& (samedir
|| redirect
[0] == '/'))
869 redirect
= ovl_get_redirect(dentry
, samedir
);
870 if (IS_ERR(redirect
))
871 return PTR_ERR(redirect
);
873 err
= ovl_check_setxattr(dentry
, ovl_dentry_upper(dentry
),
875 redirect
, strlen(redirect
), -EXDEV
);
877 spin_lock(&dentry
->d_lock
);
878 ovl_dentry_set_redirect(dentry
, redirect
);
879 spin_unlock(&dentry
->d_lock
);
882 pr_warn_ratelimited("overlayfs: failed to set redirect (%i)\n",
884 /* Fall back to userspace copy-up */
890 static int ovl_rename(struct inode
*olddir
, struct dentry
*old
,
891 struct inode
*newdir
, struct dentry
*new,
896 struct dentry
*old_upperdir
;
897 struct dentry
*new_upperdir
;
898 struct dentry
*olddentry
;
899 struct dentry
*newdentry
;
903 bool cleanup_whiteout
= false;
904 bool overwrite
= !(flags
& RENAME_EXCHANGE
);
905 bool is_dir
= d_is_dir(old
);
906 bool new_is_dir
= d_is_dir(new);
907 bool samedir
= olddir
== newdir
;
908 struct dentry
*opaquedir
= NULL
;
909 const struct cred
*old_cred
= NULL
;
913 if (flags
& ~(RENAME_EXCHANGE
| RENAME_NOREPLACE
))
916 flags
&= ~RENAME_NOREPLACE
;
918 /* Don't copy up directory trees */
920 if (!ovl_can_move(old
))
922 if (!overwrite
&& !ovl_can_move(new))
925 if (overwrite
&& new_is_dir
&& !ovl_pure_upper(new)) {
926 err
= ovl_check_empty_dir(new, &list
);
932 if (ovl_lower_positive(old
)) {
933 if (!ovl_dentry_is_whiteout(new)) {
934 /* Whiteout source */
935 flags
|= RENAME_WHITEOUT
;
937 /* Switch whiteouts */
938 flags
|= RENAME_EXCHANGE
;
940 } else if (is_dir
&& ovl_dentry_is_whiteout(new)) {
941 flags
|= RENAME_EXCHANGE
;
942 cleanup_whiteout
= true;
946 err
= ovl_want_write(old
);
950 err
= ovl_copy_up(old
);
954 err
= ovl_copy_up(new->d_parent
);
958 err
= ovl_copy_up(new);
962 err
= ovl_nlink_start(new, &locked
);
967 old_cred
= ovl_override_creds(old
->d_sb
);
969 if (!list_empty(&list
)) {
970 opaquedir
= ovl_clear_empty(new, &list
);
971 err
= PTR_ERR(opaquedir
);
972 if (IS_ERR(opaquedir
)) {
974 goto out_revert_creds
;
978 old_upperdir
= ovl_dentry_upper(old
->d_parent
);
979 new_upperdir
= ovl_dentry_upper(new->d_parent
);
983 * When moving a merge dir or non-dir with copy up origin into
984 * a new parent, we are marking the new parent dir "impure".
985 * When ovl_iterate() iterates an "impure" upper dir, it will
986 * lookup the origin inodes of the entries to fill d_ino.
988 if (ovl_type_origin(old
)) {
989 err
= ovl_set_impure(new->d_parent
, new_upperdir
);
991 goto out_revert_creds
;
993 if (!overwrite
&& ovl_type_origin(new)) {
994 err
= ovl_set_impure(old
->d_parent
, old_upperdir
);
996 goto out_revert_creds
;
1000 trap
= lock_rename(new_upperdir
, old_upperdir
);
1002 olddentry
= lookup_one_len(old
->d_name
.name
, old_upperdir
,
1004 err
= PTR_ERR(olddentry
);
1005 if (IS_ERR(olddentry
))
1009 if (!ovl_matches_upper(old
, olddentry
))
1012 newdentry
= lookup_one_len(new->d_name
.name
, new_upperdir
,
1014 err
= PTR_ERR(newdentry
);
1015 if (IS_ERR(newdentry
))
1018 old_opaque
= ovl_dentry_is_opaque(old
);
1019 new_opaque
= ovl_dentry_is_opaque(new);
1022 if (d_inode(new) && ovl_dentry_upper(new)) {
1024 if (newdentry
!= opaquedir
)
1027 if (!ovl_matches_upper(new, newdentry
))
1031 if (!d_is_negative(newdentry
) &&
1032 (!new_opaque
|| !ovl_is_whiteout(newdentry
)))
1036 if (olddentry
== trap
)
1038 if (newdentry
== trap
)
1041 if (WARN_ON(olddentry
->d_inode
== newdentry
->d_inode
))
1046 if (ovl_type_merge_or_lower(old
))
1047 err
= ovl_set_redirect(old
, samedir
);
1048 else if (!old_opaque
&& ovl_type_merge(new->d_parent
))
1049 err
= ovl_set_opaque_xerr(old
, olddentry
, -EXDEV
);
1053 if (!overwrite
&& new_is_dir
) {
1054 if (ovl_type_merge_or_lower(new))
1055 err
= ovl_set_redirect(new, samedir
);
1056 else if (!new_opaque
&& ovl_type_merge(old
->d_parent
))
1057 err
= ovl_set_opaque_xerr(new, newdentry
, -EXDEV
);
1062 err
= ovl_do_rename(old_upperdir
->d_inode
, olddentry
,
1063 new_upperdir
->d_inode
, newdentry
, flags
);
1067 if (cleanup_whiteout
)
1068 ovl_cleanup(old_upperdir
->d_inode
, newdentry
);
1070 if (overwrite
&& d_inode(new)) {
1072 clear_nlink(d_inode(new));
1074 drop_nlink(d_inode(new));
1077 ovl_dentry_version_inc(old
->d_parent
, ovl_type_origin(old
) ||
1078 (!overwrite
&& ovl_type_origin(new)));
1079 ovl_dentry_version_inc(new->d_parent
, ovl_type_origin(old
) ||
1080 (d_inode(new) && ovl_type_origin(new)));
1087 unlock_rename(new_upperdir
, old_upperdir
);
1089 revert_creds(old_cred
);
1090 ovl_nlink_end(new, locked
);
1092 ovl_drop_write(old
);
1095 ovl_cache_free(&list
);
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
,