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 int ovl_create_upper(struct dentry
*dentry
, struct inode
*inode
,
185 struct cattr
*attr
, struct dentry
*hardlink
)
187 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
188 struct inode
*udir
= upperdir
->d_inode
;
189 struct dentry
*newdentry
;
192 if (!hardlink
&& !IS_POSIXACL(udir
))
193 attr
->mode
&= ~current_umask();
195 inode_lock_nested(udir
, I_MUTEX_PARENT
);
196 newdentry
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
198 err
= PTR_ERR(newdentry
);
199 if (IS_ERR(newdentry
))
201 err
= ovl_create_real(udir
, newdentry
, attr
, hardlink
, false);
205 if (ovl_type_merge(dentry
->d_parent
) && d_is_dir(newdentry
)) {
206 /* Setting opaque here is just an optimization, allow to fail */
207 ovl_set_opaque(dentry
, newdentry
);
210 ovl_instantiate(dentry
, inode
, newdentry
, !!hardlink
);
219 static struct dentry
*ovl_clear_empty(struct dentry
*dentry
,
220 struct list_head
*list
)
222 struct dentry
*workdir
= ovl_workdir(dentry
);
223 struct inode
*wdir
= workdir
->d_inode
;
224 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
225 struct inode
*udir
= upperdir
->d_inode
;
226 struct path upperpath
;
227 struct dentry
*upper
;
228 struct dentry
*opaquedir
;
232 if (WARN_ON(!workdir
))
233 return ERR_PTR(-EROFS
);
235 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
239 ovl_path_upper(dentry
, &upperpath
);
240 err
= vfs_getattr(&upperpath
, &stat
,
241 STATX_BASIC_STATS
, AT_STATX_SYNC_AS_STAT
);
246 if (!S_ISDIR(stat
.mode
))
248 upper
= upperpath
.dentry
;
249 if (upper
->d_parent
->d_inode
!= udir
)
252 opaquedir
= ovl_lookup_temp(workdir
);
253 err
= PTR_ERR(opaquedir
);
254 if (IS_ERR(opaquedir
))
257 err
= ovl_create_real(wdir
, opaquedir
,
258 &(struct cattr
){.mode
= stat
.mode
}, NULL
, true);
262 err
= ovl_copy_xattr(upper
, opaquedir
);
266 err
= ovl_set_opaque(dentry
, opaquedir
);
270 inode_lock(opaquedir
->d_inode
);
271 err
= ovl_set_attr(opaquedir
, &stat
);
272 inode_unlock(opaquedir
->d_inode
);
276 err
= ovl_do_rename(wdir
, opaquedir
, udir
, upper
, RENAME_EXCHANGE
);
280 ovl_cleanup_whiteouts(upper
, list
);
281 ovl_cleanup(wdir
, upper
);
282 unlock_rename(workdir
, upperdir
);
284 /* dentry's upper doesn't match now, get rid of it */
290 ovl_cleanup(wdir
, opaquedir
);
294 unlock_rename(workdir
, upperdir
);
299 static struct dentry
*ovl_check_empty_and_clear(struct dentry
*dentry
)
302 struct dentry
*ret
= NULL
;
303 enum ovl_path_type type
= ovl_path_type(dentry
);
306 err
= ovl_check_empty_dir(dentry
, &list
);
313 * When removing an empty opaque directory, then it makes no sense to
314 * replace it with an exact replica of itself.
316 * If no upperdentry then skip clearing whiteouts.
318 * Can race with copy-up, since we don't hold the upperdir mutex.
319 * Doesn't matter, since copy-up can't create a non-empty directory
322 if (OVL_TYPE_UPPER(type
) && OVL_TYPE_MERGE(type
))
323 ret
= ovl_clear_empty(dentry
, &list
);
326 ovl_cache_free(&list
);
331 static int ovl_set_upper_acl(struct dentry
*upperdentry
, const char *name
,
332 const struct posix_acl
*acl
)
338 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !acl
)
341 size
= posix_acl_to_xattr(NULL
, acl
, NULL
, 0);
342 buffer
= kmalloc(size
, GFP_KERNEL
);
346 size
= posix_acl_to_xattr(&init_user_ns
, acl
, buffer
, size
);
351 err
= vfs_setxattr(upperdentry
, name
, buffer
, size
, XATTR_CREATE
);
357 static int ovl_create_over_whiteout(struct dentry
*dentry
, struct inode
*inode
,
359 struct dentry
*hardlink
)
361 struct dentry
*workdir
= ovl_workdir(dentry
);
362 struct inode
*wdir
= workdir
->d_inode
;
363 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
364 struct inode
*udir
= upperdir
->d_inode
;
365 struct dentry
*upper
;
366 struct dentry
*newdentry
;
368 struct posix_acl
*acl
, *default_acl
;
370 if (WARN_ON(!workdir
))
374 err
= posix_acl_create(dentry
->d_parent
->d_inode
,
375 &cattr
->mode
, &default_acl
, &acl
);
380 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
384 newdentry
= ovl_lookup_temp(workdir
);
385 err
= PTR_ERR(newdentry
);
386 if (IS_ERR(newdentry
))
389 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
391 err
= PTR_ERR(upper
);
396 if (d_is_negative(upper
) || !IS_WHITEOUT(d_inode(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_copy_up(new->d_parent
);
607 err
= ovl_nlink_start(old
, &locked
);
611 inode
= d_inode(old
);
614 err
= ovl_create_or_link(new, inode
, NULL
, ovl_dentry_upper(old
),
615 ovl_type_origin(old
));
619 ovl_nlink_end(old
, locked
);
626 static bool ovl_matches_upper(struct dentry
*dentry
, struct dentry
*upper
)
628 return d_inode(ovl_dentry_upper(dentry
)) == d_inode(upper
);
631 static int ovl_remove_and_whiteout(struct dentry
*dentry
, bool is_dir
)
633 struct dentry
*workdir
= ovl_workdir(dentry
);
634 struct inode
*wdir
= workdir
->d_inode
;
635 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
636 struct inode
*udir
= upperdir
->d_inode
;
637 struct dentry
*whiteout
;
638 struct dentry
*upper
;
639 struct dentry
*opaquedir
= NULL
;
643 if (WARN_ON(!workdir
))
647 opaquedir
= ovl_check_empty_and_clear(dentry
);
648 err
= PTR_ERR(opaquedir
);
649 if (IS_ERR(opaquedir
))
653 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
657 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
659 err
= PTR_ERR(upper
);
664 if ((opaquedir
&& upper
!= opaquedir
) ||
665 (!opaquedir
&& ovl_dentry_upper(dentry
) &&
666 !ovl_matches_upper(dentry
, upper
))) {
670 whiteout
= ovl_whiteout(workdir
, dentry
);
671 err
= PTR_ERR(whiteout
);
672 if (IS_ERR(whiteout
))
676 flags
= RENAME_EXCHANGE
;
678 err
= ovl_do_rename(wdir
, whiteout
, udir
, upper
, flags
);
682 ovl_cleanup(wdir
, upper
);
684 ovl_dentry_version_inc(dentry
->d_parent
, true);
691 unlock_rename(workdir
, upperdir
);
698 ovl_cleanup(wdir
, whiteout
);
702 static int ovl_remove_upper(struct dentry
*dentry
, bool is_dir
)
704 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
705 struct inode
*dir
= upperdir
->d_inode
;
706 struct dentry
*upper
;
707 struct dentry
*opaquedir
= NULL
;
710 /* Redirect dir can be !ovl_lower_positive && OVL_TYPE_MERGE */
711 if (is_dir
&& ovl_dentry_get_redirect(dentry
)) {
712 opaquedir
= ovl_check_empty_and_clear(dentry
);
713 err
= PTR_ERR(opaquedir
);
714 if (IS_ERR(opaquedir
))
718 inode_lock_nested(dir
, I_MUTEX_PARENT
);
719 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
721 err
= PTR_ERR(upper
);
726 if ((opaquedir
&& upper
!= opaquedir
) ||
727 (!opaquedir
&& !ovl_matches_upper(dentry
, upper
)))
731 err
= vfs_rmdir(dir
, upper
);
733 err
= vfs_unlink(dir
, upper
, NULL
);
734 ovl_dentry_version_inc(dentry
->d_parent
, ovl_type_origin(dentry
));
737 * Keeping this dentry hashed would mean having to release
738 * upperpath/lowerpath, which could only be done if we are the
739 * sole user of this dentry. Too tricky... Just unhash for
753 static int ovl_do_remove(struct dentry
*dentry
, bool is_dir
)
757 const struct cred
*old_cred
;
759 err
= ovl_want_write(dentry
);
763 err
= ovl_copy_up(dentry
->d_parent
);
767 err
= ovl_nlink_start(dentry
, &locked
);
771 old_cred
= ovl_override_creds(dentry
->d_sb
);
772 if (!ovl_lower_positive(dentry
))
773 err
= ovl_remove_upper(dentry
, is_dir
);
775 err
= ovl_remove_and_whiteout(dentry
, is_dir
);
776 revert_creds(old_cred
);
779 clear_nlink(dentry
->d_inode
);
781 drop_nlink(dentry
->d_inode
);
783 ovl_nlink_end(dentry
, locked
);
785 ovl_drop_write(dentry
);
790 static int ovl_unlink(struct inode
*dir
, struct dentry
*dentry
)
792 return ovl_do_remove(dentry
, false);
795 static int ovl_rmdir(struct inode
*dir
, struct dentry
*dentry
)
797 return ovl_do_remove(dentry
, true);
800 static bool ovl_type_merge_or_lower(struct dentry
*dentry
)
802 enum ovl_path_type type
= ovl_path_type(dentry
);
804 return OVL_TYPE_MERGE(type
) || !OVL_TYPE_UPPER(type
);
807 static bool ovl_can_move(struct dentry
*dentry
)
809 return ovl_redirect_dir(dentry
->d_sb
) ||
810 !d_is_dir(dentry
) || !ovl_type_merge_or_lower(dentry
);
813 static char *ovl_get_redirect(struct dentry
*dentry
, bool samedir
)
816 struct dentry
*d
, *tmp
;
817 int buflen
= ovl_redirect_max
+ 1;
820 ret
= kstrndup(dentry
->d_name
.name
, dentry
->d_name
.len
,
825 buf
= ret
= kmalloc(buflen
, GFP_KERNEL
);
831 for (d
= dget(dentry
); !IS_ROOT(d
);) {
835 spin_lock(&d
->d_lock
);
836 name
= ovl_dentry_get_redirect(d
);
838 thislen
= strlen(name
);
840 name
= d
->d_name
.name
;
841 thislen
= d
->d_name
.len
;
844 /* If path is too long, fall back to userspace move */
845 if (thislen
+ (name
[0] != '/') > buflen
) {
846 ret
= ERR_PTR(-EXDEV
);
847 spin_unlock(&d
->d_lock
);
852 memcpy(&buf
[buflen
], name
, thislen
);
853 tmp
= dget_dlock(d
->d_parent
);
854 spin_unlock(&d
->d_lock
);
859 /* Absolute redirect: finished */
860 if (buf
[buflen
] == '/')
865 ret
= kstrdup(&buf
[buflen
], GFP_KERNEL
);
870 return ret
? ret
: ERR_PTR(-ENOMEM
);
873 static int ovl_set_redirect(struct dentry
*dentry
, bool samedir
)
876 const char *redirect
= ovl_dentry_get_redirect(dentry
);
878 if (redirect
&& (samedir
|| redirect
[0] == '/'))
881 redirect
= ovl_get_redirect(dentry
, samedir
);
882 if (IS_ERR(redirect
))
883 return PTR_ERR(redirect
);
885 err
= ovl_check_setxattr(dentry
, ovl_dentry_upper(dentry
),
887 redirect
, strlen(redirect
), -EXDEV
);
889 spin_lock(&dentry
->d_lock
);
890 ovl_dentry_set_redirect(dentry
, redirect
);
891 spin_unlock(&dentry
->d_lock
);
894 pr_warn_ratelimited("overlay: failed to set redirect (%i)\n", err
);
895 /* Fall back to userspace copy-up */
901 static int ovl_rename(struct inode
*olddir
, struct dentry
*old
,
902 struct inode
*newdir
, struct dentry
*new,
907 struct dentry
*old_upperdir
;
908 struct dentry
*new_upperdir
;
909 struct dentry
*olddentry
;
910 struct dentry
*newdentry
;
914 bool cleanup_whiteout
= false;
915 bool overwrite
= !(flags
& RENAME_EXCHANGE
);
916 bool is_dir
= d_is_dir(old
);
917 bool new_is_dir
= d_is_dir(new);
918 bool samedir
= olddir
== newdir
;
919 struct dentry
*opaquedir
= NULL
;
920 const struct cred
*old_cred
= NULL
;
923 if (flags
& ~(RENAME_EXCHANGE
| RENAME_NOREPLACE
))
926 flags
&= ~RENAME_NOREPLACE
;
928 /* Don't copy up directory trees */
930 if (!ovl_can_move(old
))
932 if (!overwrite
&& !ovl_can_move(new))
935 err
= ovl_want_write(old
);
939 err
= ovl_copy_up(old
);
943 err
= ovl_copy_up(new->d_parent
);
947 err
= ovl_copy_up(new);
951 err
= ovl_nlink_start(new, &locked
);
956 old_cred
= ovl_override_creds(old
->d_sb
);
958 if (overwrite
&& new_is_dir
&& ovl_type_merge_or_lower(new)) {
959 opaquedir
= ovl_check_empty_and_clear(new);
960 err
= PTR_ERR(opaquedir
);
961 if (IS_ERR(opaquedir
)) {
963 goto out_revert_creds
;
968 if (ovl_lower_positive(old
)) {
969 if (!ovl_dentry_is_whiteout(new)) {
970 /* Whiteout source */
971 flags
|= RENAME_WHITEOUT
;
973 /* Switch whiteouts */
974 flags
|= RENAME_EXCHANGE
;
976 } else if (is_dir
&& ovl_dentry_is_whiteout(new)) {
977 flags
|= RENAME_EXCHANGE
;
978 cleanup_whiteout
= true;
982 old_upperdir
= ovl_dentry_upper(old
->d_parent
);
983 new_upperdir
= ovl_dentry_upper(new->d_parent
);
987 * When moving a merge dir or non-dir with copy up origin into
988 * a new parent, we are marking the new parent dir "impure".
989 * When ovl_iterate() iterates an "impure" upper dir, it will
990 * lookup the origin inodes of the entries to fill d_ino.
992 if (ovl_type_origin(old
)) {
993 err
= ovl_set_impure(new->d_parent
, new_upperdir
);
995 goto out_revert_creds
;
997 if (!overwrite
&& ovl_type_origin(new)) {
998 err
= ovl_set_impure(old
->d_parent
, old_upperdir
);
1000 goto out_revert_creds
;
1004 trap
= lock_rename(new_upperdir
, old_upperdir
);
1006 olddentry
= lookup_one_len(old
->d_name
.name
, old_upperdir
,
1008 err
= PTR_ERR(olddentry
);
1009 if (IS_ERR(olddentry
))
1013 if (!ovl_matches_upper(old
, olddentry
))
1016 newdentry
= lookup_one_len(new->d_name
.name
, new_upperdir
,
1018 err
= PTR_ERR(newdentry
);
1019 if (IS_ERR(newdentry
))
1022 old_opaque
= ovl_dentry_is_opaque(old
);
1023 new_opaque
= ovl_dentry_is_opaque(new);
1026 if (d_inode(new) && ovl_dentry_upper(new)) {
1028 if (newdentry
!= opaquedir
)
1031 if (!ovl_matches_upper(new, newdentry
))
1035 if (!d_is_negative(newdentry
) &&
1036 (!new_opaque
|| !ovl_is_whiteout(newdentry
)))
1040 if (olddentry
== trap
)
1042 if (newdentry
== trap
)
1045 if (olddentry
->d_inode
== newdentry
->d_inode
)
1050 if (ovl_type_merge_or_lower(old
))
1051 err
= ovl_set_redirect(old
, samedir
);
1052 else if (!old_opaque
&& ovl_type_merge(new->d_parent
))
1053 err
= ovl_set_opaque_xerr(old
, olddentry
, -EXDEV
);
1057 if (!overwrite
&& new_is_dir
) {
1058 if (ovl_type_merge_or_lower(new))
1059 err
= ovl_set_redirect(new, samedir
);
1060 else if (!new_opaque
&& ovl_type_merge(old
->d_parent
))
1061 err
= ovl_set_opaque_xerr(new, newdentry
, -EXDEV
);
1066 err
= ovl_do_rename(old_upperdir
->d_inode
, olddentry
,
1067 new_upperdir
->d_inode
, newdentry
, flags
);
1071 if (cleanup_whiteout
)
1072 ovl_cleanup(old_upperdir
->d_inode
, newdentry
);
1074 if (overwrite
&& d_inode(new)) {
1076 clear_nlink(d_inode(new));
1078 drop_nlink(d_inode(new));
1081 ovl_dentry_version_inc(old
->d_parent
,
1082 !overwrite
&& ovl_type_origin(new));
1083 ovl_dentry_version_inc(new->d_parent
, ovl_type_origin(old
));
1090 unlock_rename(new_upperdir
, old_upperdir
);
1092 revert_creds(old_cred
);
1093 ovl_nlink_end(new, locked
);
1095 ovl_drop_write(old
);
1101 const struct inode_operations ovl_dir_inode_operations
= {
1102 .lookup
= ovl_lookup
,
1104 .symlink
= ovl_symlink
,
1105 .unlink
= ovl_unlink
,
1107 .rename
= ovl_rename
,
1109 .setattr
= ovl_setattr
,
1110 .create
= ovl_create
,
1112 .permission
= ovl_permission
,
1113 .getattr
= ovl_getattr
,
1114 .listxattr
= ovl_listxattr
,
1115 .get_acl
= ovl_get_acl
,
1116 .update_time
= ovl_update_time
,