1 // SPDX-License-Identifier: GPL-2.0-only
4 * Copyright (C) 2011 Novell Inc.
8 #include <linux/namei.h>
9 #include <linux/xattr.h>
10 #include <linux/security.h>
11 #include <linux/cred.h>
12 #include <linux/module.h>
13 #include <linux/posix_acl.h>
14 #include <linux/posix_acl_xattr.h>
15 #include <linux/atomic.h>
16 #include <linux/ratelimit.h>
17 #include "overlayfs.h"
19 static unsigned short ovl_redirect_max = 256;
20 module_param_named(redirect_max, ovl_redirect_max, ushort, 0644);
21 MODULE_PARM_DESC(redirect_max,
22 "Maximum length of absolute redirect xattr value");
24 static int ovl_set_redirect(struct dentry *dentry, bool samedir);
26 int ovl_cleanup(struct ovl_fs *ofs, struct inode *wdir, struct dentry *wdentry)
31 if (d_is_dir(wdentry))
32 err = ovl_do_rmdir(ofs, wdir, wdentry);
34 err = ovl_do_unlink(ofs, wdir, wdentry);
38 pr_err("cleanup of '%pd2' failed (%i)\n",
45 struct dentry *ovl_lookup_temp(struct ovl_fs *ofs, struct dentry *workdir)
49 static atomic_t temp_id = ATOMIC_INIT(0);
51 /* counter is allowed to wrap, since temp dentries are ephemeral */
52 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
54 temp = ovl_lookup_upper(ofs, name, workdir, strlen(name));
55 if (!IS_ERR(temp) && temp->d_inode) {
56 pr_err("workdir/%s already exists\n", name);
64 /* caller holds i_mutex on workdir */
65 static struct dentry *ovl_whiteout(struct ovl_fs *ofs)
68 struct dentry *whiteout;
69 struct dentry *workdir = ofs->workdir;
70 struct inode *wdir = workdir->d_inode;
73 whiteout = ovl_lookup_temp(ofs, workdir);
77 err = ovl_do_whiteout(ofs, wdir, whiteout);
80 whiteout = ERR_PTR(err);
83 ofs->whiteout = whiteout;
86 if (!ofs->no_shared_whiteout) {
87 whiteout = ovl_lookup_temp(ofs, workdir);
91 err = ovl_do_link(ofs, ofs->whiteout, wdir, whiteout);
96 pr_warn("Failed to link whiteout - disabling whiteout inode sharing(nlink=%u, err=%i)\n",
97 ofs->whiteout->d_inode->i_nlink, err);
98 ofs->no_shared_whiteout = true;
102 whiteout = ofs->whiteout;
103 ofs->whiteout = NULL;
108 /* Caller must hold i_mutex on both workdir and dir */
109 int ovl_cleanup_and_whiteout(struct ovl_fs *ofs, struct inode *dir,
110 struct dentry *dentry)
112 struct inode *wdir = ofs->workdir->d_inode;
113 struct dentry *whiteout;
117 whiteout = ovl_whiteout(ofs);
118 err = PTR_ERR(whiteout);
119 if (IS_ERR(whiteout))
122 if (d_is_dir(dentry))
123 flags = RENAME_EXCHANGE;
125 err = ovl_do_rename(ofs, wdir, whiteout, dir, dentry, flags);
129 ovl_cleanup(ofs, wdir, dentry);
136 ovl_cleanup(ofs, wdir, whiteout);
140 int ovl_mkdir_real(struct ovl_fs *ofs, struct inode *dir,
141 struct dentry **newdentry, umode_t mode)
144 struct dentry *d, *dentry = *newdentry;
146 err = ovl_do_mkdir(ofs, dir, dentry, mode);
150 if (likely(!d_unhashed(dentry)))
154 * vfs_mkdir() may succeed and leave the dentry passed
155 * to it unhashed and negative. If that happens, try to
156 * lookup a new hashed and positive dentry.
158 d = ovl_lookup_upper(ofs, dentry->d_name.name, dentry->d_parent,
161 pr_warn("failed lookup after mkdir (%pd2, err=%i).\n",
171 struct dentry *ovl_create_real(struct ovl_fs *ofs, struct inode *dir,
172 struct dentry *newdentry, struct ovl_cattr *attr)
176 if (IS_ERR(newdentry))
180 if (newdentry->d_inode)
183 if (attr->hardlink) {
184 err = ovl_do_link(ofs, attr->hardlink, dir, newdentry);
186 switch (attr->mode & S_IFMT) {
188 err = ovl_do_create(ofs, dir, newdentry, attr->mode);
192 /* mkdir is special... */
193 err = ovl_mkdir_real(ofs, dir, &newdentry, attr->mode);
200 err = ovl_do_mknod(ofs, dir, newdentry, attr->mode,
205 err = ovl_do_symlink(ofs, dir, newdentry, attr->link);
212 if (!err && WARN_ON(!newdentry->d_inode)) {
214 * Not quite sure if non-instantiated dentry is legal or not.
215 * VFS doesn't seem to care so check and warn here.
227 struct dentry *ovl_create_temp(struct ovl_fs *ofs, struct dentry *workdir,
228 struct ovl_cattr *attr)
230 return ovl_create_real(ofs, d_inode(workdir),
231 ovl_lookup_temp(ofs, workdir), attr);
234 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
237 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
240 err = ovl_check_setxattr(ofs, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
242 ovl_dentry_set_opaque(dentry);
247 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
250 * Fail with -EIO when trying to create opaque dir and upper doesn't
251 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
252 * return a specific error for noxattr case.
254 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
258 * Common operations required to be done after creation of file on upper.
259 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
260 * may not use to instantiate the new dentry.
262 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
263 struct dentry *newdentry, bool hardlink)
265 struct ovl_inode_params oip = {
266 .upperdentry = newdentry,
270 ovl_dir_modified(dentry->d_parent, false);
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));
298 d_instantiate(dentry, inode);
299 if (inode != oip.newinode) {
300 pr_warn_ratelimited("newly created inode found in cache (%pd2)\n",
304 /* Force lookup of new upper hardlink to find its lower */
311 static bool ovl_type_merge(struct dentry *dentry)
313 return OVL_TYPE_MERGE(ovl_path_type(dentry));
316 static bool ovl_type_origin(struct dentry *dentry)
318 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
321 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
322 struct ovl_cattr *attr)
324 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
325 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
326 struct inode *udir = upperdir->d_inode;
327 struct dentry *newdentry;
330 if (!attr->hardlink && !IS_POSIXACL(udir))
331 attr->mode &= ~current_umask();
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 err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
356 ovl_cleanup(ofs, udir, newdentry);
361 static struct dentry *ovl_clear_empty(struct dentry *dentry,
362 struct list_head *list)
364 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
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 path upperpath;
370 struct dentry *upper;
371 struct dentry *opaquedir;
375 if (WARN_ON(!workdir))
376 return ERR_PTR(-EROFS);
378 err = ovl_lock_rename_workdir(workdir, upperdir);
382 ovl_path_upper(dentry, &upperpath);
383 err = vfs_getattr(&upperpath, &stat,
384 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
389 if (!S_ISDIR(stat.mode))
391 upper = upperpath.dentry;
392 if (upper->d_parent->d_inode != udir)
395 opaquedir = ovl_create_temp(ofs, workdir, OVL_CATTR(stat.mode));
396 err = PTR_ERR(opaquedir);
397 if (IS_ERR(opaquedir))
400 err = ovl_copy_xattr(dentry->d_sb, &upperpath, opaquedir);
404 err = ovl_set_opaque(dentry, opaquedir);
408 inode_lock(opaquedir->d_inode);
409 err = ovl_set_attr(ofs, opaquedir, &stat);
410 inode_unlock(opaquedir->d_inode);
414 err = ovl_do_rename(ofs, wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
418 ovl_cleanup_whiteouts(ofs, upper, list);
419 ovl_cleanup(ofs, wdir, upper);
420 unlock_rename(workdir, upperdir);
422 /* dentry's upper doesn't match now, get rid of it */
428 ovl_cleanup(ofs, wdir, opaquedir);
431 unlock_rename(workdir, upperdir);
436 static int ovl_set_upper_acl(struct ovl_fs *ofs, struct dentry *upperdentry,
437 const char *acl_name, struct posix_acl *acl)
439 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
442 return ovl_do_set_acl(ofs, upperdentry, acl_name, acl);
445 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
446 struct ovl_cattr *cattr)
448 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
449 struct dentry *workdir = ovl_workdir(dentry);
450 struct inode *wdir = workdir->d_inode;
451 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
452 struct inode *udir = upperdir->d_inode;
453 struct dentry *upper;
454 struct dentry *newdentry;
456 struct posix_acl *acl, *default_acl;
457 bool hardlink = !!cattr->hardlink;
459 if (WARN_ON(!workdir))
463 err = posix_acl_create(dentry->d_parent->d_inode,
464 &cattr->mode, &default_acl, &acl);
469 err = ovl_lock_rename_workdir(workdir, upperdir);
473 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
475 err = PTR_ERR(upper);
480 if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
483 newdentry = ovl_create_temp(ofs, workdir, cattr);
484 err = PTR_ERR(newdentry);
485 if (IS_ERR(newdentry))
489 * mode could have been mutilated due to umask (e.g. sgid directory)
492 !S_ISLNK(cattr->mode) &&
493 newdentry->d_inode->i_mode != cattr->mode) {
494 struct iattr attr = {
495 .ia_valid = ATTR_MODE,
496 .ia_mode = cattr->mode,
498 inode_lock(newdentry->d_inode);
499 err = ovl_do_notify_change(ofs, newdentry, &attr);
500 inode_unlock(newdentry->d_inode);
505 err = ovl_set_upper_acl(ofs, newdentry,
506 XATTR_NAME_POSIX_ACL_ACCESS, acl);
510 err = ovl_set_upper_acl(ofs, newdentry,
511 XATTR_NAME_POSIX_ACL_DEFAULT, default_acl);
516 if (!hardlink && S_ISDIR(cattr->mode)) {
517 err = ovl_set_opaque(dentry, newdentry);
521 err = ovl_do_rename(ofs, wdir, newdentry, udir, upper,
526 ovl_cleanup(ofs, wdir, upper);
528 err = ovl_do_rename(ofs, wdir, newdentry, udir, upper, 0);
532 err = ovl_instantiate(dentry, inode, newdentry, hardlink);
534 ovl_cleanup(ofs, udir, newdentry);
540 unlock_rename(workdir, upperdir);
543 posix_acl_release(acl);
544 posix_acl_release(default_acl);
549 ovl_cleanup(ofs, wdir, newdentry);
554 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
555 struct ovl_cattr *attr, bool origin)
558 const struct cred *old_cred;
559 struct cred *override_cred;
560 struct dentry *parent = dentry->d_parent;
562 err = ovl_copy_up(parent);
566 old_cred = ovl_override_creds(dentry->d_sb);
569 * When linking a file with copy up origin into a new parent, mark the
570 * new parent dir "impure".
573 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
575 goto out_revert_creds;
578 if (!attr->hardlink) {
580 override_cred = prepare_creds();
582 goto out_revert_creds;
584 * In the creation cases(create, mkdir, mknod, symlink),
585 * ovl should transfer current's fs{u,g}id to underlying
586 * fs. Because underlying fs want to initialize its new
587 * inode owner using current's fs{u,g}id. And in this
588 * case, the @inode is a new inode that is initialized
589 * in inode_init_owner() to current's fs{u,g}id. So use
590 * the inode's i_{u,g}id to override the cred's fs{u,g}id.
592 * But in the other hardlink case, ovl_link() does not
593 * create a new inode, so just use the ovl mounter's
596 override_cred->fsuid = inode->i_uid;
597 override_cred->fsgid = inode->i_gid;
598 err = security_dentry_create_files_as(dentry,
599 attr->mode, &dentry->d_name, old_cred,
602 put_cred(override_cred);
603 goto out_revert_creds;
605 put_cred(override_creds(override_cred));
606 put_cred(override_cred);
609 if (!ovl_dentry_is_whiteout(dentry))
610 err = ovl_create_upper(dentry, inode, attr);
612 err = ovl_create_over_whiteout(dentry, inode, attr);
615 revert_creds(old_cred);
619 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
624 struct ovl_cattr attr = {
629 err = ovl_want_write(dentry);
633 /* Preallocate inode to be used by ovl_get_inode() */
635 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
639 spin_lock(&inode->i_lock);
640 inode->i_state |= I_CREATING;
641 spin_unlock(&inode->i_lock);
643 inode_init_owner(&nop_mnt_idmap, inode, dentry->d_parent->d_inode, mode);
644 attr.mode = inode->i_mode;
646 err = ovl_create_or_link(dentry, inode, &attr, false);
647 /* Did we end up using the preallocated inode? */
648 if (inode != d_inode(dentry))
652 ovl_drop_write(dentry);
657 static int ovl_create(struct mnt_idmap *idmap, struct inode *dir,
658 struct dentry *dentry, umode_t mode, bool excl)
660 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
663 static int ovl_mkdir(struct mnt_idmap *idmap, struct inode *dir,
664 struct dentry *dentry, umode_t mode)
666 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
669 static int ovl_mknod(struct mnt_idmap *idmap, struct inode *dir,
670 struct dentry *dentry, umode_t mode, dev_t rdev)
672 /* Don't allow creation of "whiteout" on overlay */
673 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
676 return ovl_create_object(dentry, mode, rdev, NULL);
679 static int ovl_symlink(struct mnt_idmap *idmap, struct inode *dir,
680 struct dentry *dentry, const char *link)
682 return ovl_create_object(dentry, S_IFLNK, 0, link);
685 static int ovl_set_link_redirect(struct dentry *dentry)
687 const struct cred *old_cred;
690 old_cred = ovl_override_creds(dentry->d_sb);
691 err = ovl_set_redirect(dentry, false);
692 revert_creds(old_cred);
697 static int ovl_link(struct dentry *old, struct inode *newdir,
703 err = ovl_want_write(old);
707 err = ovl_copy_up(old);
711 err = ovl_copy_up(new->d_parent);
715 if (ovl_is_metacopy_dentry(old)) {
716 err = ovl_set_link_redirect(old);
721 err = ovl_nlink_start(old);
725 inode = d_inode(old);
728 err = ovl_create_or_link(new, inode,
729 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
730 ovl_type_origin(old));
741 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
743 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
746 static int ovl_remove_and_whiteout(struct dentry *dentry,
747 struct list_head *list)
749 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
750 struct dentry *workdir = ovl_workdir(dentry);
751 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
752 struct dentry *upper;
753 struct dentry *opaquedir = NULL;
756 if (WARN_ON(!workdir))
759 if (!list_empty(list)) {
760 opaquedir = ovl_clear_empty(dentry, list);
761 err = PTR_ERR(opaquedir);
762 if (IS_ERR(opaquedir))
766 err = ovl_lock_rename_workdir(workdir, upperdir);
770 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
772 err = PTR_ERR(upper);
777 if ((opaquedir && upper != opaquedir) ||
778 (!opaquedir && ovl_dentry_upper(dentry) &&
779 !ovl_matches_upper(dentry, upper))) {
783 err = ovl_cleanup_and_whiteout(ofs, d_inode(upperdir), upper);
787 ovl_dir_modified(dentry->d_parent, true);
793 unlock_rename(workdir, upperdir);
800 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
801 struct list_head *list)
803 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
804 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
805 struct inode *dir = upperdir->d_inode;
806 struct dentry *upper;
807 struct dentry *opaquedir = NULL;
810 if (!list_empty(list)) {
811 opaquedir = ovl_clear_empty(dentry, list);
812 err = PTR_ERR(opaquedir);
813 if (IS_ERR(opaquedir))
817 inode_lock_nested(dir, I_MUTEX_PARENT);
818 upper = ovl_lookup_upper(ofs, dentry->d_name.name, upperdir,
820 err = PTR_ERR(upper);
825 if ((opaquedir && upper != opaquedir) ||
826 (!opaquedir && !ovl_matches_upper(dentry, upper)))
830 err = ovl_do_rmdir(ofs, dir, upper);
832 err = ovl_do_unlink(ofs, dir, upper);
833 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
836 * Keeping this dentry hashed would mean having to release
837 * upperpath/lowerpath, which could only be done if we are the
838 * sole user of this dentry. Too tricky... Just unhash for
852 static bool ovl_pure_upper(struct dentry *dentry)
854 return !ovl_dentry_lower(dentry) &&
855 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
858 static void ovl_drop_nlink(struct dentry *dentry)
860 struct inode *inode = d_inode(dentry);
861 struct dentry *alias;
863 /* Try to find another, hashed alias */
864 spin_lock(&inode->i_lock);
865 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
866 if (alias != dentry && !d_unhashed(alias))
869 spin_unlock(&inode->i_lock);
872 * Changes to underlying layers may cause i_nlink to lose sync with
873 * reality. In this case prevent the link count from going to zero
876 if (inode->i_nlink > !!alias)
880 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
883 const struct cred *old_cred;
884 bool lower_positive = ovl_lower_positive(dentry);
887 /* No need to clean pure upper removed by vfs_rmdir() */
888 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
889 err = ovl_check_empty_dir(dentry, &list);
894 err = ovl_want_write(dentry);
898 err = ovl_copy_up(dentry->d_parent);
902 err = ovl_nlink_start(dentry);
906 old_cred = ovl_override_creds(dentry->d_sb);
908 err = ovl_remove_upper(dentry, is_dir, &list);
910 err = ovl_remove_and_whiteout(dentry, &list);
911 revert_creds(old_cred);
914 clear_nlink(dentry->d_inode);
916 ovl_drop_nlink(dentry);
918 ovl_nlink_end(dentry);
923 * Note: we fail to update ctime if there was no copy-up, only a
926 if (ovl_dentry_upper(dentry))
927 ovl_copyattr(d_inode(dentry));
930 ovl_drop_write(dentry);
932 ovl_cache_free(&list);
936 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
938 return ovl_do_remove(dentry, false);
941 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
943 return ovl_do_remove(dentry, true);
946 static bool ovl_type_merge_or_lower(struct dentry *dentry)
948 enum ovl_path_type type = ovl_path_type(dentry);
950 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
953 static bool ovl_can_move(struct dentry *dentry)
955 return ovl_redirect_dir(OVL_FS(dentry->d_sb)) ||
956 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
959 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
962 struct dentry *d, *tmp;
963 int buflen = ovl_redirect_max + 1;
966 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
971 buf = ret = kmalloc(buflen, GFP_KERNEL);
977 for (d = dget(dentry); !IS_ROOT(d);) {
981 spin_lock(&d->d_lock);
982 name = ovl_dentry_get_redirect(d);
984 thislen = strlen(name);
986 name = d->d_name.name;
987 thislen = d->d_name.len;
990 /* If path is too long, fall back to userspace move */
991 if (thislen + (name[0] != '/') > buflen) {
992 ret = ERR_PTR(-EXDEV);
993 spin_unlock(&d->d_lock);
998 memcpy(&buf[buflen], name, thislen);
999 spin_unlock(&d->d_lock);
1000 tmp = dget_parent(d);
1005 /* Absolute redirect: finished */
1006 if (buf[buflen] == '/')
1011 ret = kstrdup(&buf[buflen], GFP_KERNEL);
1016 return ret ? ret : ERR_PTR(-ENOMEM);
1019 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
1021 struct dentry *lowerdentry;
1026 if (d_is_dir(dentry))
1030 * For non-dir hardlinked files, we need absolute redirects
1031 * in general as two upper hardlinks could be in different
1032 * dirs. We could put a relative redirect now and convert
1033 * it to absolute redirect later. But when nlink > 1 and
1034 * indexing is on, that means relative redirect needs to be
1035 * converted to absolute during copy up of another lower
1036 * hardllink as well.
1038 * So without optimizing too much, just check if lower is
1039 * a hard link or not. If lower is hard link, put absolute
1042 lowerdentry = ovl_dentry_lower(dentry);
1043 return (d_inode(lowerdentry)->i_nlink > 1);
1046 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
1049 struct ovl_fs *ofs = OVL_FS(dentry->d_sb);
1050 const char *redirect = ovl_dentry_get_redirect(dentry);
1051 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1053 if (redirect && (!absolute_redirect || redirect[0] == '/'))
1056 redirect = ovl_get_redirect(dentry, absolute_redirect);
1057 if (IS_ERR(redirect))
1058 return PTR_ERR(redirect);
1060 err = ovl_check_setxattr(ofs, ovl_dentry_upper(dentry),
1062 redirect, strlen(redirect), -EXDEV);
1064 spin_lock(&dentry->d_lock);
1065 ovl_dentry_set_redirect(dentry, redirect);
1066 spin_unlock(&dentry->d_lock);
1069 pr_warn_ratelimited("failed to set redirect (%i)\n",
1071 /* Fall back to userspace copy-up */
1077 static int ovl_rename(struct mnt_idmap *idmap, struct inode *olddir,
1078 struct dentry *old, struct inode *newdir,
1079 struct dentry *new, unsigned int flags)
1082 struct dentry *old_upperdir;
1083 struct dentry *new_upperdir;
1084 struct dentry *olddentry;
1085 struct dentry *newdentry;
1086 struct dentry *trap;
1089 bool cleanup_whiteout = false;
1090 bool update_nlink = false;
1091 bool overwrite = !(flags & RENAME_EXCHANGE);
1092 bool is_dir = d_is_dir(old);
1093 bool new_is_dir = d_is_dir(new);
1094 bool samedir = olddir == newdir;
1095 struct dentry *opaquedir = NULL;
1096 const struct cred *old_cred = NULL;
1097 struct ovl_fs *ofs = OVL_FS(old->d_sb);
1101 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1104 flags &= ~RENAME_NOREPLACE;
1106 /* Don't copy up directory trees */
1108 if (!ovl_can_move(old))
1110 if (!overwrite && !ovl_can_move(new))
1113 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1114 err = ovl_check_empty_dir(new, &list);
1120 if (ovl_lower_positive(old)) {
1121 if (!ovl_dentry_is_whiteout(new)) {
1122 /* Whiteout source */
1123 flags |= RENAME_WHITEOUT;
1125 /* Switch whiteouts */
1126 flags |= RENAME_EXCHANGE;
1128 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1129 flags |= RENAME_EXCHANGE;
1130 cleanup_whiteout = true;
1134 err = ovl_want_write(old);
1138 err = ovl_copy_up(old);
1140 goto out_drop_write;
1142 err = ovl_copy_up(new->d_parent);
1144 goto out_drop_write;
1146 err = ovl_copy_up(new);
1148 goto out_drop_write;
1149 } else if (d_inode(new)) {
1150 err = ovl_nlink_start(new);
1152 goto out_drop_write;
1154 update_nlink = true;
1157 old_cred = ovl_override_creds(old->d_sb);
1159 if (!list_empty(&list)) {
1160 opaquedir = ovl_clear_empty(new, &list);
1161 err = PTR_ERR(opaquedir);
1162 if (IS_ERR(opaquedir)) {
1164 goto out_revert_creds;
1168 old_upperdir = ovl_dentry_upper(old->d_parent);
1169 new_upperdir = ovl_dentry_upper(new->d_parent);
1173 * When moving a merge dir or non-dir with copy up origin into
1174 * a new parent, we are marking the new parent dir "impure".
1175 * When ovl_iterate() iterates an "impure" upper dir, it will
1176 * lookup the origin inodes of the entries to fill d_ino.
1178 if (ovl_type_origin(old)) {
1179 err = ovl_set_impure(new->d_parent, new_upperdir);
1181 goto out_revert_creds;
1183 if (!overwrite && ovl_type_origin(new)) {
1184 err = ovl_set_impure(old->d_parent, old_upperdir);
1186 goto out_revert_creds;
1190 trap = lock_rename(new_upperdir, old_upperdir);
1192 olddentry = ovl_lookup_upper(ofs, old->d_name.name, old_upperdir,
1194 err = PTR_ERR(olddentry);
1195 if (IS_ERR(olddentry))
1199 if (!ovl_matches_upper(old, olddentry))
1202 newdentry = ovl_lookup_upper(ofs, new->d_name.name, new_upperdir,
1204 err = PTR_ERR(newdentry);
1205 if (IS_ERR(newdentry))
1208 old_opaque = ovl_dentry_is_opaque(old);
1209 new_opaque = ovl_dentry_is_opaque(new);
1212 if (d_inode(new) && ovl_dentry_upper(new)) {
1214 if (newdentry != opaquedir)
1217 if (!ovl_matches_upper(new, newdentry))
1221 if (!d_is_negative(newdentry)) {
1222 if (!new_opaque || !ovl_is_whiteout(newdentry))
1225 if (flags & RENAME_EXCHANGE)
1230 if (olddentry == trap)
1232 if (newdentry == trap)
1235 if (olddentry->d_inode == newdentry->d_inode)
1239 if (ovl_type_merge_or_lower(old))
1240 err = ovl_set_redirect(old, samedir);
1241 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1242 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1246 if (!overwrite && ovl_type_merge_or_lower(new))
1247 err = ovl_set_redirect(new, samedir);
1248 else if (!overwrite && new_is_dir && !new_opaque &&
1249 ovl_type_merge(old->d_parent))
1250 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1254 err = ovl_do_rename(ofs, old_upperdir->d_inode, olddentry,
1255 new_upperdir->d_inode, newdentry, flags);
1259 if (cleanup_whiteout)
1260 ovl_cleanup(ofs, old_upperdir->d_inode, newdentry);
1262 if (overwrite && d_inode(new)) {
1264 clear_nlink(d_inode(new));
1266 ovl_drop_nlink(new);
1269 ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1270 (!overwrite && ovl_type_origin(new)));
1271 ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1272 (d_inode(new) && ovl_type_origin(new)));
1275 ovl_copyattr(d_inode(old));
1276 if (d_inode(new) && ovl_dentry_upper(new))
1277 ovl_copyattr(d_inode(new));
1284 unlock_rename(new_upperdir, old_upperdir);
1286 revert_creds(old_cred);
1290 ovl_drop_write(old);
1293 ovl_cache_free(&list);
1297 const struct inode_operations ovl_dir_inode_operations = {
1298 .lookup = ovl_lookup,
1300 .symlink = ovl_symlink,
1301 .unlink = ovl_unlink,
1303 .rename = ovl_rename,
1305 .setattr = ovl_setattr,
1306 .create = ovl_create,
1308 .permission = ovl_permission,
1309 .getattr = ovl_getattr,
1310 .listxattr = ovl_listxattr,
1311 .get_inode_acl = ovl_get_inode_acl,
1312 .get_acl = ovl_get_acl,
1313 .set_acl = ovl_set_acl,
1314 .update_time = ovl_update_time,
1315 .fileattr_get = ovl_fileattr_get,
1316 .fileattr_set = ovl_fileattr_set,