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 <linux/backing-file.h>
18 #include "overlayfs.h"
20 static unsigned short ovl_redirect_max
= 256;
21 module_param_named(redirect_max
, ovl_redirect_max
, ushort
, 0644);
22 MODULE_PARM_DESC(redirect_max
,
23 "Maximum length of absolute redirect xattr value");
25 static int ovl_set_redirect(struct dentry
*dentry
, bool samedir
);
27 int ovl_cleanup(struct ovl_fs
*ofs
, struct inode
*wdir
, struct dentry
*wdentry
)
32 if (d_is_dir(wdentry
))
33 err
= ovl_do_rmdir(ofs
, wdir
, wdentry
);
35 err
= ovl_do_unlink(ofs
, wdir
, wdentry
);
39 pr_err("cleanup of '%pd2' failed (%i)\n",
46 struct dentry
*ovl_lookup_temp(struct ovl_fs
*ofs
, 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
= ovl_lookup_upper(ofs
, name
, workdir
, strlen(name
));
56 if (!IS_ERR(temp
) && temp
->d_inode
) {
57 pr_err("workdir/%s already exists\n", name
);
65 /* caller holds i_mutex on workdir */
66 static struct dentry
*ovl_whiteout(struct ovl_fs
*ofs
)
69 struct dentry
*whiteout
;
70 struct dentry
*workdir
= ofs
->workdir
;
71 struct inode
*wdir
= workdir
->d_inode
;
74 whiteout
= ovl_lookup_temp(ofs
, workdir
);
78 err
= ovl_do_whiteout(ofs
, wdir
, whiteout
);
81 whiteout
= ERR_PTR(err
);
84 ofs
->whiteout
= whiteout
;
87 if (!ofs
->no_shared_whiteout
) {
88 whiteout
= ovl_lookup_temp(ofs
, workdir
);
92 err
= ovl_do_link(ofs
, ofs
->whiteout
, wdir
, whiteout
);
97 pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%i)\n",
98 ofs
->whiteout
->d_inode
->i_nlink
, err
);
99 ofs
->no_shared_whiteout
= true;
103 whiteout
= ofs
->whiteout
;
104 ofs
->whiteout
= NULL
;
109 /* Caller must hold i_mutex on both workdir and dir */
110 int ovl_cleanup_and_whiteout(struct ovl_fs
*ofs
, struct inode
*dir
,
111 struct dentry
*dentry
)
113 struct inode
*wdir
= ofs
->workdir
->d_inode
;
114 struct dentry
*whiteout
;
118 whiteout
= ovl_whiteout(ofs
);
119 err
= PTR_ERR(whiteout
);
120 if (IS_ERR(whiteout
))
123 if (d_is_dir(dentry
))
124 flags
= RENAME_EXCHANGE
;
126 err
= ovl_do_rename(ofs
, wdir
, whiteout
, dir
, dentry
, flags
);
130 ovl_cleanup(ofs
, wdir
, dentry
);
137 ovl_cleanup(ofs
, wdir
, whiteout
);
141 int ovl_mkdir_real(struct ovl_fs
*ofs
, struct inode
*dir
,
142 struct dentry
**newdentry
, umode_t mode
)
145 struct dentry
*d
, *dentry
= *newdentry
;
147 err
= ovl_do_mkdir(ofs
, dir
, dentry
, mode
);
151 if (likely(!d_unhashed(dentry
)))
155 * vfs_mkdir() may succeed and leave the dentry passed
156 * to it unhashed and negative. If that happens, try to
157 * lookup a new hashed and positive dentry.
159 d
= ovl_lookup_upper(ofs
, dentry
->d_name
.name
, dentry
->d_parent
,
162 pr_warn("failed lookup after mkdir (%pd2, err=%i).\n",
172 struct dentry
*ovl_create_real(struct ovl_fs
*ofs
, struct inode
*dir
,
173 struct dentry
*newdentry
, struct ovl_cattr
*attr
)
177 if (IS_ERR(newdentry
))
181 if (newdentry
->d_inode
)
184 if (attr
->hardlink
) {
185 err
= ovl_do_link(ofs
, attr
->hardlink
, dir
, newdentry
);
187 switch (attr
->mode
& S_IFMT
) {
189 err
= ovl_do_create(ofs
, dir
, newdentry
, attr
->mode
);
193 /* mkdir is special... */
194 err
= ovl_mkdir_real(ofs
, dir
, &newdentry
, attr
->mode
);
201 err
= ovl_do_mknod(ofs
, dir
, newdentry
, attr
->mode
,
206 err
= ovl_do_symlink(ofs
, dir
, newdentry
, attr
->link
);
213 if (!err
&& WARN_ON(!newdentry
->d_inode
)) {
215 * Not quite sure if non-instantiated dentry is legal or not.
216 * VFS doesn't seem to care so check and warn here.
228 struct dentry
*ovl_create_temp(struct ovl_fs
*ofs
, struct dentry
*workdir
,
229 struct ovl_cattr
*attr
)
231 return ovl_create_real(ofs
, d_inode(workdir
),
232 ovl_lookup_temp(ofs
, workdir
), attr
);
235 static int ovl_set_opaque_xerr(struct dentry
*dentry
, struct dentry
*upper
,
238 struct ovl_fs
*ofs
= OVL_FS(dentry
->d_sb
);
241 err
= ovl_check_setxattr(ofs
, upper
, OVL_XATTR_OPAQUE
, "y", 1, xerr
);
243 ovl_dentry_set_opaque(dentry
);
248 static int ovl_set_opaque(struct dentry
*dentry
, struct dentry
*upperdentry
)
251 * Fail with -EIO when trying to create opaque dir and upper doesn't
252 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
253 * return a specific error for noxattr case.
255 return ovl_set_opaque_xerr(dentry
, upperdentry
, -EIO
);
259 * Common operations required to be done after creation of file on upper.
260 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
261 * may not use to instantiate the new dentry.
263 static int ovl_instantiate(struct dentry
*dentry
, struct inode
*inode
,
264 struct dentry
*newdentry
, bool hardlink
, struct file
*tmpfile
)
266 struct ovl_inode_params oip
= {
267 .upperdentry
= newdentry
,
271 ovl_dentry_set_upper_alias(dentry
);
272 ovl_dentry_init_reval(dentry
, newdentry
, NULL
);
276 * ovl_obtain_alias() can be called after ovl_create_real()
277 * and before we get here, so we may get an inode from cache
278 * with the same real upperdentry that is not the inode we
279 * pre-allocated. In this case we will use the cached inode
280 * to instantiate the new dentry.
282 * XXX: if we ever use ovl_obtain_alias() to decode directory
283 * file handles, need to use ovl_get_inode_locked() and
284 * d_instantiate_new() here to prevent from creating two
285 * hashed directory inode aliases.
287 inode
= ovl_get_inode(dentry
->d_sb
, &oip
);
289 return PTR_ERR(inode
);
290 if (inode
== oip
.newinode
)
291 ovl_set_flag(OVL_UPPERDATA
, inode
);
293 WARN_ON(ovl_inode_real(inode
) != d_inode(newdentry
));
299 d_mark_tmpfile(tmpfile
, inode
);
301 d_instantiate(dentry
, inode
);
302 if (inode
!= oip
.newinode
) {
303 pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
307 /* Force lookup of new upper hardlink to find its lower */
314 static bool ovl_type_merge(struct dentry
*dentry
)
316 return OVL_TYPE_MERGE(ovl_path_type(dentry
));
319 static bool ovl_type_origin(struct dentry
*dentry
)
321 return OVL_TYPE_ORIGIN(ovl_path_type(dentry
));
324 static int ovl_create_upper(struct dentry
*dentry
, struct inode
*inode
,
325 struct ovl_cattr
*attr
)
327 struct ovl_fs
*ofs
= OVL_FS(dentry
->d_sb
);
328 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
329 struct inode
*udir
= upperdir
->d_inode
;
330 struct dentry
*newdentry
;
333 inode_lock_nested(udir
, I_MUTEX_PARENT
);
334 newdentry
= ovl_create_real(ofs
, udir
,
335 ovl_lookup_upper(ofs
, dentry
->d_name
.name
,
336 upperdir
, dentry
->d_name
.len
),
338 err
= PTR_ERR(newdentry
);
339 if (IS_ERR(newdentry
))
342 if (ovl_type_merge(dentry
->d_parent
) && d_is_dir(newdentry
) &&
343 !ovl_allow_offline_changes(ofs
)) {
344 /* Setting opaque here is just an optimization, allow to fail */
345 ovl_set_opaque(dentry
, newdentry
);
348 ovl_dir_modified(dentry
->d_parent
, false);
349 err
= ovl_instantiate(dentry
, inode
, newdentry
, !!attr
->hardlink
, NULL
);
357 ovl_cleanup(ofs
, udir
, newdentry
);
362 static struct dentry
*ovl_clear_empty(struct dentry
*dentry
,
363 struct list_head
*list
)
365 struct ovl_fs
*ofs
= OVL_FS(dentry
->d_sb
);
366 struct dentry
*workdir
= ovl_workdir(dentry
);
367 struct inode
*wdir
= workdir
->d_inode
;
368 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
369 struct inode
*udir
= upperdir
->d_inode
;
370 struct path upperpath
;
371 struct dentry
*upper
;
372 struct dentry
*opaquedir
;
376 if (WARN_ON(!workdir
))
377 return ERR_PTR(-EROFS
);
379 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
383 ovl_path_upper(dentry
, &upperpath
);
384 err
= vfs_getattr(&upperpath
, &stat
,
385 STATX_BASIC_STATS
, AT_STATX_SYNC_AS_STAT
);
390 if (!S_ISDIR(stat
.mode
))
392 upper
= upperpath
.dentry
;
393 if (upper
->d_parent
->d_inode
!= udir
)
396 opaquedir
= ovl_create_temp(ofs
, workdir
, OVL_CATTR(stat
.mode
));
397 err
= PTR_ERR(opaquedir
);
398 if (IS_ERR(opaquedir
))
401 err
= ovl_copy_xattr(dentry
->d_sb
, &upperpath
, opaquedir
);
405 err
= ovl_set_opaque(dentry
, opaquedir
);
409 inode_lock(opaquedir
->d_inode
);
410 err
= ovl_set_attr(ofs
, opaquedir
, &stat
);
411 inode_unlock(opaquedir
->d_inode
);
415 err
= ovl_do_rename(ofs
, wdir
, opaquedir
, udir
, upper
, RENAME_EXCHANGE
);
419 ovl_cleanup_whiteouts(ofs
, upper
, list
);
420 ovl_cleanup(ofs
, wdir
, upper
);
421 unlock_rename(workdir
, upperdir
);
423 /* dentry's upper doesn't match now, get rid of it */
429 ovl_cleanup(ofs
, wdir
, opaquedir
);
432 unlock_rename(workdir
, upperdir
);
437 static int ovl_set_upper_acl(struct ovl_fs
*ofs
, struct dentry
*upperdentry
,
438 const char *acl_name
, struct posix_acl
*acl
)
440 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL
) || !acl
)
443 return ovl_do_set_acl(ofs
, upperdentry
, acl_name
, acl
);
446 static int ovl_create_over_whiteout(struct dentry
*dentry
, struct inode
*inode
,
447 struct ovl_cattr
*cattr
)
449 struct ovl_fs
*ofs
= OVL_FS(dentry
->d_sb
);
450 struct dentry
*workdir
= ovl_workdir(dentry
);
451 struct inode
*wdir
= workdir
->d_inode
;
452 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
453 struct inode
*udir
= upperdir
->d_inode
;
454 struct dentry
*upper
;
455 struct dentry
*newdentry
;
457 struct posix_acl
*acl
, *default_acl
;
458 bool hardlink
= !!cattr
->hardlink
;
460 if (WARN_ON(!workdir
))
464 err
= posix_acl_create(dentry
->d_parent
->d_inode
,
465 &cattr
->mode
, &default_acl
, &acl
);
470 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
474 upper
= ovl_lookup_upper(ofs
, dentry
->d_name
.name
, upperdir
,
476 err
= PTR_ERR(upper
);
481 if (d_is_negative(upper
) || !ovl_upper_is_whiteout(ofs
, upper
))
484 newdentry
= ovl_create_temp(ofs
, workdir
, cattr
);
485 err
= PTR_ERR(newdentry
);
486 if (IS_ERR(newdentry
))
490 * mode could have been mutilated due to umask (e.g. sgid directory)
493 !S_ISLNK(cattr
->mode
) &&
494 newdentry
->d_inode
->i_mode
!= cattr
->mode
) {
495 struct iattr attr
= {
496 .ia_valid
= ATTR_MODE
,
497 .ia_mode
= cattr
->mode
,
499 inode_lock(newdentry
->d_inode
);
500 err
= ovl_do_notify_change(ofs
, newdentry
, &attr
);
501 inode_unlock(newdentry
->d_inode
);
506 err
= ovl_set_upper_acl(ofs
, newdentry
,
507 XATTR_NAME_POSIX_ACL_ACCESS
, acl
);
511 err
= ovl_set_upper_acl(ofs
, newdentry
,
512 XATTR_NAME_POSIX_ACL_DEFAULT
, default_acl
);
517 if (!hardlink
&& S_ISDIR(cattr
->mode
)) {
518 err
= ovl_set_opaque(dentry
, newdentry
);
522 err
= ovl_do_rename(ofs
, wdir
, newdentry
, udir
, upper
,
527 ovl_cleanup(ofs
, wdir
, upper
);
529 err
= ovl_do_rename(ofs
, wdir
, newdentry
, udir
, upper
, 0);
533 ovl_dir_modified(dentry
->d_parent
, false);
534 err
= ovl_instantiate(dentry
, inode
, newdentry
, hardlink
, NULL
);
536 ovl_cleanup(ofs
, udir
, newdentry
);
542 unlock_rename(workdir
, upperdir
);
545 posix_acl_release(acl
);
546 posix_acl_release(default_acl
);
551 ovl_cleanup(ofs
, wdir
, newdentry
);
556 static const struct cred
*ovl_setup_cred_for_create(struct dentry
*dentry
,
559 const struct cred
*old_cred
)
562 struct cred
*override_cred
;
564 override_cred
= prepare_creds();
566 return ERR_PTR(-ENOMEM
);
568 override_cred
->fsuid
= inode
->i_uid
;
569 override_cred
->fsgid
= inode
->i_gid
;
570 err
= security_dentry_create_files_as(dentry
, mode
, &dentry
->d_name
,
571 old_cred
, override_cred
);
573 put_cred(override_cred
);
578 * Caller is going to match this with revert_creds_light() and drop
579 * referenec on the returned creds.
580 * We must be called with creator creds already, otherwise we risk
583 old_cred
= override_creds_light(override_cred
);
584 WARN_ON_ONCE(old_cred
!= ovl_creds(dentry
->d_sb
));
586 return override_cred
;
589 static int ovl_create_or_link(struct dentry
*dentry
, struct inode
*inode
,
590 struct ovl_cattr
*attr
, bool origin
)
593 const struct cred
*old_cred
, *new_cred
= NULL
;
594 struct dentry
*parent
= dentry
->d_parent
;
596 old_cred
= ovl_override_creds(dentry
->d_sb
);
599 * When linking a file with copy up origin into a new parent, mark the
600 * new parent dir "impure".
603 err
= ovl_set_impure(parent
, ovl_dentry_upper(parent
));
605 goto out_revert_creds
;
608 if (!attr
->hardlink
) {
610 * In the creation cases(create, mkdir, mknod, symlink),
611 * ovl should transfer current's fs{u,g}id to underlying
612 * fs. Because underlying fs want to initialize its new
613 * inode owner using current's fs{u,g}id. And in this
614 * case, the @inode is a new inode that is initialized
615 * in inode_init_owner() to current's fs{u,g}id. So use
616 * the inode's i_{u,g}id to override the cred's fs{u,g}id.
618 * But in the other hardlink case, ovl_link() does not
619 * create a new inode, so just use the ovl mounter's
622 new_cred
= ovl_setup_cred_for_create(dentry
, inode
, attr
->mode
,
624 err
= PTR_ERR(new_cred
);
625 if (IS_ERR(new_cred
)) {
627 goto out_revert_creds
;
631 if (!ovl_dentry_is_whiteout(dentry
))
632 err
= ovl_create_upper(dentry
, inode
, attr
);
634 err
= ovl_create_over_whiteout(dentry
, inode
, attr
);
637 ovl_revert_creds(old_cred
);
642 static int ovl_create_object(struct dentry
*dentry
, int mode
, dev_t rdev
,
647 struct ovl_cattr attr
= {
652 err
= ovl_copy_up(dentry
->d_parent
);
656 err
= ovl_want_write(dentry
);
660 /* Preallocate inode to be used by ovl_get_inode() */
662 inode
= ovl_new_inode(dentry
->d_sb
, mode
, rdev
);
666 spin_lock(&inode
->i_lock
);
667 inode
->i_state
|= I_CREATING
;
668 spin_unlock(&inode
->i_lock
);
670 inode_init_owner(&nop_mnt_idmap
, inode
, dentry
->d_parent
->d_inode
, mode
);
671 attr
.mode
= inode
->i_mode
;
673 err
= ovl_create_or_link(dentry
, inode
, &attr
, false);
674 /* Did we end up using the preallocated inode? */
675 if (inode
!= d_inode(dentry
))
679 ovl_drop_write(dentry
);
684 static int ovl_create(struct mnt_idmap
*idmap
, struct inode
*dir
,
685 struct dentry
*dentry
, umode_t mode
, bool excl
)
687 return ovl_create_object(dentry
, (mode
& 07777) | S_IFREG
, 0, NULL
);
690 static int ovl_mkdir(struct mnt_idmap
*idmap
, struct inode
*dir
,
691 struct dentry
*dentry
, umode_t mode
)
693 return ovl_create_object(dentry
, (mode
& 07777) | S_IFDIR
, 0, NULL
);
696 static int ovl_mknod(struct mnt_idmap
*idmap
, struct inode
*dir
,
697 struct dentry
*dentry
, umode_t mode
, dev_t rdev
)
699 /* Don't allow creation of "whiteout" on overlay */
700 if (S_ISCHR(mode
) && rdev
== WHITEOUT_DEV
)
703 return ovl_create_object(dentry
, mode
, rdev
, NULL
);
706 static int ovl_symlink(struct mnt_idmap
*idmap
, struct inode
*dir
,
707 struct dentry
*dentry
, const char *link
)
709 return ovl_create_object(dentry
, S_IFLNK
, 0, link
);
712 static int ovl_set_link_redirect(struct dentry
*dentry
)
714 const struct cred
*old_cred
;
717 old_cred
= ovl_override_creds(dentry
->d_sb
);
718 err
= ovl_set_redirect(dentry
, false);
719 ovl_revert_creds(old_cred
);
724 static int ovl_link(struct dentry
*old
, struct inode
*newdir
,
730 err
= ovl_copy_up(old
);
734 err
= ovl_copy_up(new->d_parent
);
738 err
= ovl_nlink_start(old
);
742 if (ovl_is_metacopy_dentry(old
)) {
743 err
= ovl_set_link_redirect(old
);
748 inode
= d_inode(old
);
751 err
= ovl_create_or_link(new, inode
,
752 &(struct ovl_cattr
) {.hardlink
= ovl_dentry_upper(old
)},
753 ovl_type_origin(old
));
763 static bool ovl_matches_upper(struct dentry
*dentry
, struct dentry
*upper
)
765 return d_inode(ovl_dentry_upper(dentry
)) == d_inode(upper
);
768 static int ovl_remove_and_whiteout(struct dentry
*dentry
,
769 struct list_head
*list
)
771 struct ovl_fs
*ofs
= OVL_FS(dentry
->d_sb
);
772 struct dentry
*workdir
= ovl_workdir(dentry
);
773 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
774 struct dentry
*upper
;
775 struct dentry
*opaquedir
= NULL
;
778 if (WARN_ON(!workdir
))
781 if (!list_empty(list
)) {
782 opaquedir
= ovl_clear_empty(dentry
, list
);
783 err
= PTR_ERR(opaquedir
);
784 if (IS_ERR(opaquedir
))
788 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
792 upper
= ovl_lookup_upper(ofs
, dentry
->d_name
.name
, upperdir
,
794 err
= PTR_ERR(upper
);
799 if ((opaquedir
&& upper
!= opaquedir
) ||
800 (!opaquedir
&& ovl_dentry_upper(dentry
) &&
801 !ovl_matches_upper(dentry
, upper
))) {
805 err
= ovl_cleanup_and_whiteout(ofs
, d_inode(upperdir
), upper
);
809 ovl_dir_modified(dentry
->d_parent
, true);
815 unlock_rename(workdir
, upperdir
);
822 static int ovl_remove_upper(struct dentry
*dentry
, bool is_dir
,
823 struct list_head
*list
)
825 struct ovl_fs
*ofs
= OVL_FS(dentry
->d_sb
);
826 struct dentry
*upperdir
= ovl_dentry_upper(dentry
->d_parent
);
827 struct inode
*dir
= upperdir
->d_inode
;
828 struct dentry
*upper
;
829 struct dentry
*opaquedir
= NULL
;
832 if (!list_empty(list
)) {
833 opaquedir
= ovl_clear_empty(dentry
, list
);
834 err
= PTR_ERR(opaquedir
);
835 if (IS_ERR(opaquedir
))
839 inode_lock_nested(dir
, I_MUTEX_PARENT
);
840 upper
= ovl_lookup_upper(ofs
, dentry
->d_name
.name
, upperdir
,
842 err
= PTR_ERR(upper
);
847 if ((opaquedir
&& upper
!= opaquedir
) ||
848 (!opaquedir
&& !ovl_matches_upper(dentry
, upper
)))
852 err
= ovl_do_rmdir(ofs
, dir
, upper
);
854 err
= ovl_do_unlink(ofs
, dir
, upper
);
855 ovl_dir_modified(dentry
->d_parent
, ovl_type_origin(dentry
));
858 * Keeping this dentry hashed would mean having to release
859 * upperpath/lowerpath, which could only be done if we are the
860 * sole user of this dentry. Too tricky... Just unhash for
874 static bool ovl_pure_upper(struct dentry
*dentry
)
876 return !ovl_dentry_lower(dentry
) &&
877 !ovl_test_flag(OVL_WHITEOUTS
, d_inode(dentry
));
880 static void ovl_drop_nlink(struct dentry
*dentry
)
882 struct inode
*inode
= d_inode(dentry
);
883 struct dentry
*alias
;
885 /* Try to find another, hashed alias */
886 spin_lock(&inode
->i_lock
);
887 hlist_for_each_entry(alias
, &inode
->i_dentry
, d_u
.d_alias
) {
888 if (alias
!= dentry
&& !d_unhashed(alias
))
891 spin_unlock(&inode
->i_lock
);
894 * Changes to underlying layers may cause i_nlink to lose sync with
895 * reality. In this case prevent the link count from going to zero
898 if (inode
->i_nlink
> !!alias
)
902 static int ovl_do_remove(struct dentry
*dentry
, bool is_dir
)
905 const struct cred
*old_cred
;
906 bool lower_positive
= ovl_lower_positive(dentry
);
909 /* No need to clean pure upper removed by vfs_rmdir() */
910 if (is_dir
&& (lower_positive
|| !ovl_pure_upper(dentry
))) {
911 err
= ovl_check_empty_dir(dentry
, &list
);
916 err
= ovl_copy_up(dentry
->d_parent
);
920 err
= ovl_nlink_start(dentry
);
924 old_cred
= ovl_override_creds(dentry
->d_sb
);
926 err
= ovl_remove_upper(dentry
, is_dir
, &list
);
928 err
= ovl_remove_and_whiteout(dentry
, &list
);
929 ovl_revert_creds(old_cred
);
932 clear_nlink(dentry
->d_inode
);
934 ovl_drop_nlink(dentry
);
936 ovl_nlink_end(dentry
);
941 * Note: we fail to update ctime if there was no copy-up, only a
944 if (ovl_dentry_upper(dentry
))
945 ovl_copyattr(d_inode(dentry
));
948 ovl_cache_free(&list
);
952 static int ovl_unlink(struct inode
*dir
, struct dentry
*dentry
)
954 return ovl_do_remove(dentry
, false);
957 static int ovl_rmdir(struct inode
*dir
, struct dentry
*dentry
)
959 return ovl_do_remove(dentry
, true);
962 static bool ovl_type_merge_or_lower(struct dentry
*dentry
)
964 enum ovl_path_type type
= ovl_path_type(dentry
);
966 return OVL_TYPE_MERGE(type
) || !OVL_TYPE_UPPER(type
);
969 static bool ovl_can_move(struct dentry
*dentry
)
971 return ovl_redirect_dir(OVL_FS(dentry
->d_sb
)) ||
972 !d_is_dir(dentry
) || !ovl_type_merge_or_lower(dentry
);
975 static char *ovl_get_redirect(struct dentry
*dentry
, bool abs_redirect
)
978 struct dentry
*d
, *tmp
;
979 int buflen
= ovl_redirect_max
+ 1;
982 ret
= kstrndup(dentry
->d_name
.name
, dentry
->d_name
.len
,
987 buf
= ret
= kmalloc(buflen
, GFP_KERNEL
);
993 for (d
= dget(dentry
); !IS_ROOT(d
);) {
997 spin_lock(&d
->d_lock
);
998 name
= ovl_dentry_get_redirect(d
);
1000 thislen
= strlen(name
);
1002 name
= d
->d_name
.name
;
1003 thislen
= d
->d_name
.len
;
1006 /* If path is too long, fall back to userspace move */
1007 if (thislen
+ (name
[0] != '/') > buflen
) {
1008 ret
= ERR_PTR(-EXDEV
);
1009 spin_unlock(&d
->d_lock
);
1014 memcpy(&buf
[buflen
], name
, thislen
);
1015 spin_unlock(&d
->d_lock
);
1016 tmp
= dget_parent(d
);
1021 /* Absolute redirect: finished */
1022 if (buf
[buflen
] == '/')
1027 ret
= kstrdup(&buf
[buflen
], GFP_KERNEL
);
1032 return ret
? ret
: ERR_PTR(-ENOMEM
);
1035 static bool ovl_need_absolute_redirect(struct dentry
*dentry
, bool samedir
)
1037 struct dentry
*lowerdentry
;
1042 if (d_is_dir(dentry
))
1046 * For non-dir hardlinked files, we need absolute redirects
1047 * in general as two upper hardlinks could be in different
1048 * dirs. We could put a relative redirect now and convert
1049 * it to absolute redirect later. But when nlink > 1 and
1050 * indexing is on, that means relative redirect needs to be
1051 * converted to absolute during copy up of another lower
1052 * hardllink as well.
1054 * So without optimizing too much, just check if lower is
1055 * a hard link or not. If lower is hard link, put absolute
1058 lowerdentry
= ovl_dentry_lower(dentry
);
1059 return (d_inode(lowerdentry
)->i_nlink
> 1);
1062 static int ovl_set_redirect(struct dentry
*dentry
, bool samedir
)
1065 struct ovl_fs
*ofs
= OVL_FS(dentry
->d_sb
);
1066 const char *redirect
= ovl_dentry_get_redirect(dentry
);
1067 bool absolute_redirect
= ovl_need_absolute_redirect(dentry
, samedir
);
1069 if (redirect
&& (!absolute_redirect
|| redirect
[0] == '/'))
1072 redirect
= ovl_get_redirect(dentry
, absolute_redirect
);
1073 if (IS_ERR(redirect
))
1074 return PTR_ERR(redirect
);
1076 err
= ovl_check_setxattr(ofs
, ovl_dentry_upper(dentry
),
1078 redirect
, strlen(redirect
), -EXDEV
);
1080 spin_lock(&dentry
->d_lock
);
1081 ovl_dentry_set_redirect(dentry
, redirect
);
1082 spin_unlock(&dentry
->d_lock
);
1085 pr_warn_ratelimited("failed to set redirect (%i)\n",
1087 /* Fall back to userspace copy-up */
1093 static int ovl_rename(struct mnt_idmap
*idmap
, struct inode
*olddir
,
1094 struct dentry
*old
, struct inode
*newdir
,
1095 struct dentry
*new, unsigned int flags
)
1098 struct dentry
*old_upperdir
;
1099 struct dentry
*new_upperdir
;
1100 struct dentry
*olddentry
;
1101 struct dentry
*newdentry
;
1102 struct dentry
*trap
;
1105 bool cleanup_whiteout
= false;
1106 bool update_nlink
= false;
1107 bool overwrite
= !(flags
& RENAME_EXCHANGE
);
1108 bool is_dir
= d_is_dir(old
);
1109 bool new_is_dir
= d_is_dir(new);
1110 bool samedir
= olddir
== newdir
;
1111 struct dentry
*opaquedir
= NULL
;
1112 const struct cred
*old_cred
= NULL
;
1113 struct ovl_fs
*ofs
= OVL_FS(old
->d_sb
);
1117 if (flags
& ~(RENAME_EXCHANGE
| RENAME_NOREPLACE
))
1120 flags
&= ~RENAME_NOREPLACE
;
1122 /* Don't copy up directory trees */
1124 if (!ovl_can_move(old
))
1126 if (!overwrite
&& !ovl_can_move(new))
1129 if (overwrite
&& new_is_dir
&& !ovl_pure_upper(new)) {
1130 err
= ovl_check_empty_dir(new, &list
);
1136 if (ovl_lower_positive(old
)) {
1137 if (!ovl_dentry_is_whiteout(new)) {
1138 /* Whiteout source */
1139 flags
|= RENAME_WHITEOUT
;
1141 /* Switch whiteouts */
1142 flags
|= RENAME_EXCHANGE
;
1144 } else if (is_dir
&& ovl_dentry_is_whiteout(new)) {
1145 flags
|= RENAME_EXCHANGE
;
1146 cleanup_whiteout
= true;
1150 err
= ovl_copy_up(old
);
1154 err
= ovl_copy_up(new->d_parent
);
1158 err
= ovl_copy_up(new);
1161 } else if (d_inode(new)) {
1162 err
= ovl_nlink_start(new);
1166 update_nlink
= true;
1169 if (!update_nlink
) {
1170 /* ovl_nlink_start() took ovl_want_write() */
1171 err
= ovl_want_write(old
);
1176 old_cred
= ovl_override_creds(old
->d_sb
);
1178 if (!list_empty(&list
)) {
1179 opaquedir
= ovl_clear_empty(new, &list
);
1180 err
= PTR_ERR(opaquedir
);
1181 if (IS_ERR(opaquedir
)) {
1183 goto out_revert_creds
;
1187 old_upperdir
= ovl_dentry_upper(old
->d_parent
);
1188 new_upperdir
= ovl_dentry_upper(new->d_parent
);
1192 * When moving a merge dir or non-dir with copy up origin into
1193 * a new parent, we are marking the new parent dir "impure".
1194 * When ovl_iterate() iterates an "impure" upper dir, it will
1195 * lookup the origin inodes of the entries to fill d_ino.
1197 if (ovl_type_origin(old
)) {
1198 err
= ovl_set_impure(new->d_parent
, new_upperdir
);
1200 goto out_revert_creds
;
1202 if (!overwrite
&& ovl_type_origin(new)) {
1203 err
= ovl_set_impure(old
->d_parent
, old_upperdir
);
1205 goto out_revert_creds
;
1209 trap
= lock_rename(new_upperdir
, old_upperdir
);
1211 err
= PTR_ERR(trap
);
1212 goto out_revert_creds
;
1215 olddentry
= ovl_lookup_upper(ofs
, old
->d_name
.name
, old_upperdir
,
1217 err
= PTR_ERR(olddentry
);
1218 if (IS_ERR(olddentry
))
1222 if (!ovl_matches_upper(old
, olddentry
))
1225 newdentry
= ovl_lookup_upper(ofs
, new->d_name
.name
, new_upperdir
,
1227 err
= PTR_ERR(newdentry
);
1228 if (IS_ERR(newdentry
))
1231 old_opaque
= ovl_dentry_is_opaque(old
);
1232 new_opaque
= ovl_dentry_is_opaque(new);
1235 if (d_inode(new) && ovl_dentry_upper(new)) {
1237 if (newdentry
!= opaquedir
)
1240 if (!ovl_matches_upper(new, newdentry
))
1244 if (!d_is_negative(newdentry
)) {
1245 if (!new_opaque
|| !ovl_upper_is_whiteout(ofs
, newdentry
))
1248 if (flags
& RENAME_EXCHANGE
)
1253 if (olddentry
== trap
)
1255 if (newdentry
== trap
)
1258 if (olddentry
->d_inode
== newdentry
->d_inode
)
1262 if (ovl_type_merge_or_lower(old
))
1263 err
= ovl_set_redirect(old
, samedir
);
1264 else if (is_dir
&& !old_opaque
&& ovl_type_merge(new->d_parent
))
1265 err
= ovl_set_opaque_xerr(old
, olddentry
, -EXDEV
);
1269 if (!overwrite
&& ovl_type_merge_or_lower(new))
1270 err
= ovl_set_redirect(new, samedir
);
1271 else if (!overwrite
&& new_is_dir
&& !new_opaque
&&
1272 ovl_type_merge(old
->d_parent
))
1273 err
= ovl_set_opaque_xerr(new, newdentry
, -EXDEV
);
1277 err
= ovl_do_rename(ofs
, old_upperdir
->d_inode
, olddentry
,
1278 new_upperdir
->d_inode
, newdentry
, flags
);
1282 if (cleanup_whiteout
)
1283 ovl_cleanup(ofs
, old_upperdir
->d_inode
, newdentry
);
1285 if (overwrite
&& d_inode(new)) {
1287 clear_nlink(d_inode(new));
1289 ovl_drop_nlink(new);
1292 ovl_dir_modified(old
->d_parent
, ovl_type_origin(old
) ||
1293 (!overwrite
&& ovl_type_origin(new)));
1294 ovl_dir_modified(new->d_parent
, ovl_type_origin(old
) ||
1295 (d_inode(new) && ovl_type_origin(new)));
1298 ovl_copyattr(d_inode(old
));
1299 if (d_inode(new) && ovl_dentry_upper(new))
1300 ovl_copyattr(d_inode(new));
1307 unlock_rename(new_upperdir
, old_upperdir
);
1309 ovl_revert_creds(old_cred
);
1313 ovl_drop_write(old
);
1316 ovl_cache_free(&list
);
1320 static int ovl_create_tmpfile(struct file
*file
, struct dentry
*dentry
,
1321 struct inode
*inode
, umode_t mode
)
1323 const struct cred
*old_cred
, *new_cred
= NULL
;
1324 struct path realparentpath
;
1325 struct file
*realfile
;
1326 struct ovl_file
*of
;
1327 struct dentry
*newdentry
;
1328 /* It's okay to set O_NOATIME, since the owner will be current fsuid */
1329 int flags
= file
->f_flags
| OVL_OPEN_FLAGS
;
1332 old_cred
= ovl_override_creds(dentry
->d_sb
);
1333 new_cred
= ovl_setup_cred_for_create(dentry
, inode
, mode
, old_cred
);
1334 err
= PTR_ERR(new_cred
);
1335 if (IS_ERR(new_cred
)) {
1337 goto out_revert_creds
;
1340 ovl_path_upper(dentry
->d_parent
, &realparentpath
);
1341 realfile
= backing_tmpfile_open(&file
->f_path
, flags
, &realparentpath
,
1342 mode
, current_cred());
1343 err
= PTR_ERR_OR_ZERO(realfile
);
1344 pr_debug("tmpfile/open(%pd2, 0%o) = %i\n", realparentpath
.dentry
, mode
, err
);
1346 goto out_revert_creds
;
1348 of
= ovl_file_alloc(realfile
);
1352 goto out_revert_creds
;
1355 /* ovl_instantiate() consumes the newdentry reference on success */
1356 newdentry
= dget(realfile
->f_path
.dentry
);
1357 err
= ovl_instantiate(dentry
, inode
, newdentry
, false, file
);
1359 file
->private_data
= of
;
1365 ovl_revert_creds(old_cred
);
1370 static int ovl_dummy_open(struct inode
*inode
, struct file
*file
)
1375 static int ovl_tmpfile(struct mnt_idmap
*idmap
, struct inode
*dir
,
1376 struct file
*file
, umode_t mode
)
1379 struct dentry
*dentry
= file
->f_path
.dentry
;
1380 struct inode
*inode
;
1382 if (!OVL_FS(dentry
->d_sb
)->tmpfile
)
1385 err
= ovl_copy_up(dentry
->d_parent
);
1389 err
= ovl_want_write(dentry
);
1394 inode
= ovl_new_inode(dentry
->d_sb
, mode
, 0);
1398 inode_init_owner(&nop_mnt_idmap
, inode
, dir
, mode
);
1399 err
= ovl_create_tmpfile(file
, dentry
, inode
, inode
->i_mode
);
1404 * Check if the preallocated inode was actually used. Having something
1405 * else assigned to the dentry shouldn't happen as that would indicate
1406 * that the backing tmpfile "leaked" out of overlayfs.
1409 if (WARN_ON(inode
!= d_inode(dentry
)))
1412 /* inode reference was transferred to dentry */
1414 err
= finish_open(file
, dentry
, ovl_dummy_open
);
1416 /* Without FMODE_OPENED ->release() won't be called on @file */
1417 if (!(file
->f_mode
& FMODE_OPENED
))
1418 ovl_file_free(file
->private_data
);
1422 ovl_drop_write(dentry
);
1426 const struct inode_operations ovl_dir_inode_operations
= {
1427 .lookup
= ovl_lookup
,
1429 .symlink
= ovl_symlink
,
1430 .unlink
= ovl_unlink
,
1432 .rename
= ovl_rename
,
1434 .setattr
= ovl_setattr
,
1435 .create
= ovl_create
,
1437 .permission
= ovl_permission
,
1438 .getattr
= ovl_getattr
,
1439 .listxattr
= ovl_listxattr
,
1440 .get_inode_acl
= ovl_get_inode_acl
,
1441 .get_acl
= ovl_get_acl
,
1442 .set_acl
= ovl_set_acl
,
1443 .update_time
= ovl_update_time
,
1444 .fileattr_get
= ovl_fileattr_get
,
1445 .fileattr_set
= ovl_fileattr_set
,
1446 .tmpfile
= ovl_tmpfile
,