libata: Return correct status in sata_pmp_eh_recover_pm() when ATA_DFLAG_DETACH is set
[linux/fpc-iii.git] / fs / overlayfs / dir.c
blob29abdb1d3b5c6cf235e99e81948ee55b6d676636
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
4 * Copyright (C) 2011 Novell Inc.
5 */
7 #include <linux/fs.h>
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 inode *wdir, struct dentry *wdentry)
28 int err;
30 dget(wdentry);
31 if (d_is_dir(wdentry))
32 err = ovl_do_rmdir(wdir, wdentry);
33 else
34 err = ovl_do_unlink(wdir, wdentry);
35 dput(wdentry);
37 if (err) {
38 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
39 wdentry, err);
42 return err;
45 static struct dentry *ovl_lookup_temp(struct dentry *workdir)
47 struct dentry *temp;
48 char name[20];
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 = lookup_one_len(name, workdir, strlen(name));
55 if (!IS_ERR(temp) && temp->d_inode) {
56 pr_err("overlayfs: workdir/%s already exists\n", name);
57 dput(temp);
58 temp = ERR_PTR(-EIO);
61 return temp;
64 /* caller holds i_mutex on workdir */
65 static struct dentry *ovl_whiteout(struct dentry *workdir)
67 int err;
68 struct dentry *whiteout;
69 struct inode *wdir = workdir->d_inode;
71 whiteout = ovl_lookup_temp(workdir);
72 if (IS_ERR(whiteout))
73 return whiteout;
75 err = ovl_do_whiteout(wdir, whiteout);
76 if (err) {
77 dput(whiteout);
78 whiteout = ERR_PTR(err);
81 return whiteout;
84 /* Caller must hold i_mutex on both workdir and dir */
85 int ovl_cleanup_and_whiteout(struct dentry *workdir, struct inode *dir,
86 struct dentry *dentry)
88 struct inode *wdir = workdir->d_inode;
89 struct dentry *whiteout;
90 int err;
91 int flags = 0;
93 whiteout = ovl_whiteout(workdir);
94 err = PTR_ERR(whiteout);
95 if (IS_ERR(whiteout))
96 return err;
98 if (d_is_dir(dentry))
99 flags = RENAME_EXCHANGE;
101 err = ovl_do_rename(wdir, whiteout, dir, dentry, flags);
102 if (err)
103 goto kill_whiteout;
104 if (flags)
105 ovl_cleanup(wdir, dentry);
107 out:
108 dput(whiteout);
109 return err;
111 kill_whiteout:
112 ovl_cleanup(wdir, whiteout);
113 goto out;
116 static int ovl_mkdir_real(struct inode *dir, struct dentry **newdentry,
117 umode_t mode)
119 int err;
120 struct dentry *d, *dentry = *newdentry;
122 err = ovl_do_mkdir(dir, dentry, mode);
123 if (err)
124 return err;
126 if (likely(!d_unhashed(dentry)))
127 return 0;
130 * vfs_mkdir() may succeed and leave the dentry passed
131 * to it unhashed and negative. If that happens, try to
132 * lookup a new hashed and positive dentry.
134 d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
135 dentry->d_name.len);
136 if (IS_ERR(d)) {
137 pr_warn("overlayfs: failed lookup after mkdir (%pd2, err=%i).\n",
138 dentry, err);
139 return PTR_ERR(d);
141 dput(dentry);
142 *newdentry = d;
144 return 0;
147 struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
148 struct ovl_cattr *attr)
150 int err;
152 if (IS_ERR(newdentry))
153 return newdentry;
155 err = -ESTALE;
156 if (newdentry->d_inode)
157 goto out;
159 if (attr->hardlink) {
160 err = ovl_do_link(attr->hardlink, dir, newdentry);
161 } else {
162 switch (attr->mode & S_IFMT) {
163 case S_IFREG:
164 err = ovl_do_create(dir, newdentry, attr->mode);
165 break;
167 case S_IFDIR:
168 /* mkdir is special... */
169 err = ovl_mkdir_real(dir, &newdentry, attr->mode);
170 break;
172 case S_IFCHR:
173 case S_IFBLK:
174 case S_IFIFO:
175 case S_IFSOCK:
176 err = ovl_do_mknod(dir, newdentry, attr->mode,
177 attr->rdev);
178 break;
180 case S_IFLNK:
181 err = ovl_do_symlink(dir, newdentry, attr->link);
182 break;
184 default:
185 err = -EPERM;
188 if (!err && WARN_ON(!newdentry->d_inode)) {
190 * Not quite sure if non-instantiated dentry is legal or not.
191 * VFS doesn't seem to care so check and warn here.
193 err = -EIO;
195 out:
196 if (err) {
197 dput(newdentry);
198 return ERR_PTR(err);
200 return newdentry;
203 struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr)
205 return ovl_create_real(d_inode(workdir), ovl_lookup_temp(workdir),
206 attr);
209 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
210 int xerr)
212 int err;
214 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
215 if (!err)
216 ovl_dentry_set_opaque(dentry);
218 return err;
221 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
224 * Fail with -EIO when trying to create opaque dir and upper doesn't
225 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
226 * return a specific error for noxattr case.
228 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
232 * Common operations required to be done after creation of file on upper.
233 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
234 * may not use to instantiate the new dentry.
236 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
237 struct dentry *newdentry, bool hardlink)
239 struct ovl_inode_params oip = {
240 .upperdentry = newdentry,
241 .newinode = inode,
244 ovl_dir_modified(dentry->d_parent, false);
245 ovl_dentry_set_upper_alias(dentry);
246 if (!hardlink) {
248 * ovl_obtain_alias() can be called after ovl_create_real()
249 * and before we get here, so we may get an inode from cache
250 * with the same real upperdentry that is not the inode we
251 * pre-allocated. In this case we will use the cached inode
252 * to instantiate the new dentry.
254 * XXX: if we ever use ovl_obtain_alias() to decode directory
255 * file handles, need to use ovl_get_inode_locked() and
256 * d_instantiate_new() here to prevent from creating two
257 * hashed directory inode aliases.
259 inode = ovl_get_inode(dentry->d_sb, &oip);
260 if (IS_ERR(inode))
261 return PTR_ERR(inode);
262 } else {
263 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
264 dput(newdentry);
265 inc_nlink(inode);
268 d_instantiate(dentry, inode);
269 if (inode != oip.newinode) {
270 pr_warn_ratelimited("overlayfs: newly created inode found in cache (%pd2)\n",
271 dentry);
274 /* Force lookup of new upper hardlink to find its lower */
275 if (hardlink)
276 d_drop(dentry);
278 return 0;
281 static bool ovl_type_merge(struct dentry *dentry)
283 return OVL_TYPE_MERGE(ovl_path_type(dentry));
286 static bool ovl_type_origin(struct dentry *dentry)
288 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
291 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
292 struct ovl_cattr *attr)
294 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
295 struct inode *udir = upperdir->d_inode;
296 struct dentry *newdentry;
297 int err;
299 if (!attr->hardlink && !IS_POSIXACL(udir))
300 attr->mode &= ~current_umask();
302 inode_lock_nested(udir, I_MUTEX_PARENT);
303 newdentry = ovl_create_real(udir,
304 lookup_one_len(dentry->d_name.name,
305 upperdir,
306 dentry->d_name.len),
307 attr);
308 err = PTR_ERR(newdentry);
309 if (IS_ERR(newdentry))
310 goto out_unlock;
312 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
313 /* Setting opaque here is just an optimization, allow to fail */
314 ovl_set_opaque(dentry, newdentry);
317 err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
318 if (err)
319 goto out_cleanup;
320 out_unlock:
321 inode_unlock(udir);
322 return err;
324 out_cleanup:
325 ovl_cleanup(udir, newdentry);
326 dput(newdentry);
327 goto out_unlock;
330 static struct dentry *ovl_clear_empty(struct dentry *dentry,
331 struct list_head *list)
333 struct dentry *workdir = ovl_workdir(dentry);
334 struct inode *wdir = workdir->d_inode;
335 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
336 struct inode *udir = upperdir->d_inode;
337 struct path upperpath;
338 struct dentry *upper;
339 struct dentry *opaquedir;
340 struct kstat stat;
341 int err;
343 if (WARN_ON(!workdir))
344 return ERR_PTR(-EROFS);
346 err = ovl_lock_rename_workdir(workdir, upperdir);
347 if (err)
348 goto out;
350 ovl_path_upper(dentry, &upperpath);
351 err = vfs_getattr(&upperpath, &stat,
352 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
353 if (err)
354 goto out_unlock;
356 err = -ESTALE;
357 if (!S_ISDIR(stat.mode))
358 goto out_unlock;
359 upper = upperpath.dentry;
360 if (upper->d_parent->d_inode != udir)
361 goto out_unlock;
363 opaquedir = ovl_create_temp(workdir, OVL_CATTR(stat.mode));
364 err = PTR_ERR(opaquedir);
365 if (IS_ERR(opaquedir))
366 goto out_unlock;
368 err = ovl_copy_xattr(upper, opaquedir);
369 if (err)
370 goto out_cleanup;
372 err = ovl_set_opaque(dentry, opaquedir);
373 if (err)
374 goto out_cleanup;
376 inode_lock(opaquedir->d_inode);
377 err = ovl_set_attr(opaquedir, &stat);
378 inode_unlock(opaquedir->d_inode);
379 if (err)
380 goto out_cleanup;
382 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
383 if (err)
384 goto out_cleanup;
386 ovl_cleanup_whiteouts(upper, list);
387 ovl_cleanup(wdir, upper);
388 unlock_rename(workdir, upperdir);
390 /* dentry's upper doesn't match now, get rid of it */
391 d_drop(dentry);
393 return opaquedir;
395 out_cleanup:
396 ovl_cleanup(wdir, opaquedir);
397 dput(opaquedir);
398 out_unlock:
399 unlock_rename(workdir, upperdir);
400 out:
401 return ERR_PTR(err);
404 static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
405 const struct posix_acl *acl)
407 void *buffer;
408 size_t size;
409 int err;
411 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
412 return 0;
414 size = posix_acl_xattr_size(acl->a_count);
415 buffer = kmalloc(size, GFP_KERNEL);
416 if (!buffer)
417 return -ENOMEM;
419 err = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
420 if (err < 0)
421 goto out_free;
423 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
424 out_free:
425 kfree(buffer);
426 return err;
429 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
430 struct ovl_cattr *cattr)
432 struct dentry *workdir = ovl_workdir(dentry);
433 struct inode *wdir = workdir->d_inode;
434 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
435 struct inode *udir = upperdir->d_inode;
436 struct dentry *upper;
437 struct dentry *newdentry;
438 int err;
439 struct posix_acl *acl, *default_acl;
440 bool hardlink = !!cattr->hardlink;
442 if (WARN_ON(!workdir))
443 return -EROFS;
445 if (!hardlink) {
446 err = posix_acl_create(dentry->d_parent->d_inode,
447 &cattr->mode, &default_acl, &acl);
448 if (err)
449 return err;
452 err = ovl_lock_rename_workdir(workdir, upperdir);
453 if (err)
454 goto out;
456 upper = lookup_one_len(dentry->d_name.name, upperdir,
457 dentry->d_name.len);
458 err = PTR_ERR(upper);
459 if (IS_ERR(upper))
460 goto out_unlock;
462 err = -ESTALE;
463 if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
464 goto out_dput;
466 newdentry = ovl_create_temp(workdir, cattr);
467 err = PTR_ERR(newdentry);
468 if (IS_ERR(newdentry))
469 goto out_dput;
472 * mode could have been mutilated due to umask (e.g. sgid directory)
474 if (!hardlink &&
475 !S_ISLNK(cattr->mode) &&
476 newdentry->d_inode->i_mode != cattr->mode) {
477 struct iattr attr = {
478 .ia_valid = ATTR_MODE,
479 .ia_mode = cattr->mode,
481 inode_lock(newdentry->d_inode);
482 err = notify_change(newdentry, &attr, NULL);
483 inode_unlock(newdentry->d_inode);
484 if (err)
485 goto out_cleanup;
487 if (!hardlink) {
488 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
489 acl);
490 if (err)
491 goto out_cleanup;
493 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
494 default_acl);
495 if (err)
496 goto out_cleanup;
499 if (!hardlink && S_ISDIR(cattr->mode)) {
500 err = ovl_set_opaque(dentry, newdentry);
501 if (err)
502 goto out_cleanup;
504 err = ovl_do_rename(wdir, newdentry, udir, upper,
505 RENAME_EXCHANGE);
506 if (err)
507 goto out_cleanup;
509 ovl_cleanup(wdir, upper);
510 } else {
511 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
512 if (err)
513 goto out_cleanup;
515 err = ovl_instantiate(dentry, inode, newdentry, hardlink);
516 if (err)
517 goto out_cleanup;
518 out_dput:
519 dput(upper);
520 out_unlock:
521 unlock_rename(workdir, upperdir);
522 out:
523 if (!hardlink) {
524 posix_acl_release(acl);
525 posix_acl_release(default_acl);
527 return err;
529 out_cleanup:
530 ovl_cleanup(wdir, newdentry);
531 dput(newdentry);
532 goto out_dput;
535 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
536 struct ovl_cattr *attr, bool origin)
538 int err;
539 const struct cred *old_cred;
540 struct cred *override_cred;
541 struct dentry *parent = dentry->d_parent;
543 err = ovl_copy_up(parent);
544 if (err)
545 return err;
547 old_cred = ovl_override_creds(dentry->d_sb);
550 * When linking a file with copy up origin into a new parent, mark the
551 * new parent dir "impure".
553 if (origin) {
554 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
555 if (err)
556 goto out_revert_creds;
559 err = -ENOMEM;
560 override_cred = prepare_creds();
561 if (override_cred) {
562 override_cred->fsuid = inode->i_uid;
563 override_cred->fsgid = inode->i_gid;
564 if (!attr->hardlink) {
565 err = security_dentry_create_files_as(dentry,
566 attr->mode, &dentry->d_name, old_cred,
567 override_cred);
568 if (err) {
569 put_cred(override_cred);
570 goto out_revert_creds;
573 put_cred(override_creds(override_cred));
574 put_cred(override_cred);
576 if (!ovl_dentry_is_whiteout(dentry))
577 err = ovl_create_upper(dentry, inode, attr);
578 else
579 err = ovl_create_over_whiteout(dentry, inode, attr);
581 out_revert_creds:
582 revert_creds(old_cred);
583 return err;
586 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
587 const char *link)
589 int err;
590 struct inode *inode;
591 struct ovl_cattr attr = {
592 .rdev = rdev,
593 .link = link,
596 err = ovl_want_write(dentry);
597 if (err)
598 goto out;
600 /* Preallocate inode to be used by ovl_get_inode() */
601 err = -ENOMEM;
602 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
603 if (!inode)
604 goto out_drop_write;
606 spin_lock(&inode->i_lock);
607 inode->i_state |= I_CREATING;
608 spin_unlock(&inode->i_lock);
610 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
611 attr.mode = inode->i_mode;
613 err = ovl_create_or_link(dentry, inode, &attr, false);
614 /* Did we end up using the preallocated inode? */
615 if (inode != d_inode(dentry))
616 iput(inode);
618 out_drop_write:
619 ovl_drop_write(dentry);
620 out:
621 return err;
624 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
625 bool excl)
627 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
630 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
632 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
635 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
636 dev_t rdev)
638 /* Don't allow creation of "whiteout" on overlay */
639 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
640 return -EPERM;
642 return ovl_create_object(dentry, mode, rdev, NULL);
645 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
646 const char *link)
648 return ovl_create_object(dentry, S_IFLNK, 0, link);
651 static int ovl_set_link_redirect(struct dentry *dentry)
653 const struct cred *old_cred;
654 int err;
656 old_cred = ovl_override_creds(dentry->d_sb);
657 err = ovl_set_redirect(dentry, false);
658 revert_creds(old_cred);
660 return err;
663 static int ovl_link(struct dentry *old, struct inode *newdir,
664 struct dentry *new)
666 int err;
667 struct inode *inode;
669 err = ovl_want_write(old);
670 if (err)
671 goto out;
673 err = ovl_copy_up(old);
674 if (err)
675 goto out_drop_write;
677 err = ovl_copy_up(new->d_parent);
678 if (err)
679 goto out_drop_write;
681 if (ovl_is_metacopy_dentry(old)) {
682 err = ovl_set_link_redirect(old);
683 if (err)
684 goto out_drop_write;
687 err = ovl_nlink_start(old);
688 if (err)
689 goto out_drop_write;
691 inode = d_inode(old);
692 ihold(inode);
694 err = ovl_create_or_link(new, inode,
695 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
696 ovl_type_origin(old));
697 if (err)
698 iput(inode);
700 ovl_nlink_end(old);
701 out_drop_write:
702 ovl_drop_write(old);
703 out:
704 return err;
707 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
709 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
712 static int ovl_remove_and_whiteout(struct dentry *dentry,
713 struct list_head *list)
715 struct dentry *workdir = ovl_workdir(dentry);
716 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
717 struct dentry *upper;
718 struct dentry *opaquedir = NULL;
719 int err;
721 if (WARN_ON(!workdir))
722 return -EROFS;
724 if (!list_empty(list)) {
725 opaquedir = ovl_clear_empty(dentry, list);
726 err = PTR_ERR(opaquedir);
727 if (IS_ERR(opaquedir))
728 goto out;
731 err = ovl_lock_rename_workdir(workdir, upperdir);
732 if (err)
733 goto out_dput;
735 upper = lookup_one_len(dentry->d_name.name, upperdir,
736 dentry->d_name.len);
737 err = PTR_ERR(upper);
738 if (IS_ERR(upper))
739 goto out_unlock;
741 err = -ESTALE;
742 if ((opaquedir && upper != opaquedir) ||
743 (!opaquedir && ovl_dentry_upper(dentry) &&
744 !ovl_matches_upper(dentry, upper))) {
745 goto out_dput_upper;
748 err = ovl_cleanup_and_whiteout(workdir, d_inode(upperdir), upper);
749 if (err)
750 goto out_d_drop;
752 ovl_dir_modified(dentry->d_parent, true);
753 out_d_drop:
754 d_drop(dentry);
755 out_dput_upper:
756 dput(upper);
757 out_unlock:
758 unlock_rename(workdir, upperdir);
759 out_dput:
760 dput(opaquedir);
761 out:
762 return err;
765 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
766 struct list_head *list)
768 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
769 struct inode *dir = upperdir->d_inode;
770 struct dentry *upper;
771 struct dentry *opaquedir = NULL;
772 int err;
774 if (!list_empty(list)) {
775 opaquedir = ovl_clear_empty(dentry, list);
776 err = PTR_ERR(opaquedir);
777 if (IS_ERR(opaquedir))
778 goto out;
781 inode_lock_nested(dir, I_MUTEX_PARENT);
782 upper = lookup_one_len(dentry->d_name.name, upperdir,
783 dentry->d_name.len);
784 err = PTR_ERR(upper);
785 if (IS_ERR(upper))
786 goto out_unlock;
788 err = -ESTALE;
789 if ((opaquedir && upper != opaquedir) ||
790 (!opaquedir && !ovl_matches_upper(dentry, upper)))
791 goto out_dput_upper;
793 if (is_dir)
794 err = vfs_rmdir(dir, upper);
795 else
796 err = vfs_unlink(dir, upper, NULL);
797 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
800 * Keeping this dentry hashed would mean having to release
801 * upperpath/lowerpath, which could only be done if we are the
802 * sole user of this dentry. Too tricky... Just unhash for
803 * now.
805 if (!err)
806 d_drop(dentry);
807 out_dput_upper:
808 dput(upper);
809 out_unlock:
810 inode_unlock(dir);
811 dput(opaquedir);
812 out:
813 return err;
816 static bool ovl_pure_upper(struct dentry *dentry)
818 return !ovl_dentry_lower(dentry) &&
819 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
822 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
824 int err;
825 const struct cred *old_cred;
826 struct dentry *upperdentry;
827 bool lower_positive = ovl_lower_positive(dentry);
828 LIST_HEAD(list);
830 /* No need to clean pure upper removed by vfs_rmdir() */
831 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
832 err = ovl_check_empty_dir(dentry, &list);
833 if (err)
834 goto out;
837 err = ovl_want_write(dentry);
838 if (err)
839 goto out;
841 err = ovl_copy_up(dentry->d_parent);
842 if (err)
843 goto out_drop_write;
845 err = ovl_nlink_start(dentry);
846 if (err)
847 goto out_drop_write;
849 old_cred = ovl_override_creds(dentry->d_sb);
850 if (!lower_positive)
851 err = ovl_remove_upper(dentry, is_dir, &list);
852 else
853 err = ovl_remove_and_whiteout(dentry, &list);
854 revert_creds(old_cred);
855 if (!err) {
856 if (is_dir)
857 clear_nlink(dentry->d_inode);
858 else
859 drop_nlink(dentry->d_inode);
861 ovl_nlink_end(dentry);
864 * Copy ctime
866 * Note: we fail to update ctime if there was no copy-up, only a
867 * whiteout
869 upperdentry = ovl_dentry_upper(dentry);
870 if (upperdentry)
871 ovl_copyattr(d_inode(upperdentry), d_inode(dentry));
873 out_drop_write:
874 ovl_drop_write(dentry);
875 out:
876 ovl_cache_free(&list);
877 return err;
880 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
882 return ovl_do_remove(dentry, false);
885 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
887 return ovl_do_remove(dentry, true);
890 static bool ovl_type_merge_or_lower(struct dentry *dentry)
892 enum ovl_path_type type = ovl_path_type(dentry);
894 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
897 static bool ovl_can_move(struct dentry *dentry)
899 return ovl_redirect_dir(dentry->d_sb) ||
900 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
903 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
905 char *buf, *ret;
906 struct dentry *d, *tmp;
907 int buflen = ovl_redirect_max + 1;
909 if (!abs_redirect) {
910 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
911 GFP_KERNEL);
912 goto out;
915 buf = ret = kmalloc(buflen, GFP_KERNEL);
916 if (!buf)
917 goto out;
919 buflen--;
920 buf[buflen] = '\0';
921 for (d = dget(dentry); !IS_ROOT(d);) {
922 const char *name;
923 int thislen;
925 spin_lock(&d->d_lock);
926 name = ovl_dentry_get_redirect(d);
927 if (name) {
928 thislen = strlen(name);
929 } else {
930 name = d->d_name.name;
931 thislen = d->d_name.len;
934 /* If path is too long, fall back to userspace move */
935 if (thislen + (name[0] != '/') > buflen) {
936 ret = ERR_PTR(-EXDEV);
937 spin_unlock(&d->d_lock);
938 goto out_put;
941 buflen -= thislen;
942 memcpy(&buf[buflen], name, thislen);
943 tmp = dget_dlock(d->d_parent);
944 spin_unlock(&d->d_lock);
946 dput(d);
947 d = tmp;
949 /* Absolute redirect: finished */
950 if (buf[buflen] == '/')
951 break;
952 buflen--;
953 buf[buflen] = '/';
955 ret = kstrdup(&buf[buflen], GFP_KERNEL);
956 out_put:
957 dput(d);
958 kfree(buf);
959 out:
960 return ret ? ret : ERR_PTR(-ENOMEM);
963 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
965 struct dentry *lowerdentry;
967 if (!samedir)
968 return true;
970 if (d_is_dir(dentry))
971 return false;
974 * For non-dir hardlinked files, we need absolute redirects
975 * in general as two upper hardlinks could be in different
976 * dirs. We could put a relative redirect now and convert
977 * it to absolute redirect later. But when nlink > 1 and
978 * indexing is on, that means relative redirect needs to be
979 * converted to absolute during copy up of another lower
980 * hardllink as well.
982 * So without optimizing too much, just check if lower is
983 * a hard link or not. If lower is hard link, put absolute
984 * redirect.
986 lowerdentry = ovl_dentry_lower(dentry);
987 return (d_inode(lowerdentry)->i_nlink > 1);
990 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
992 int err;
993 const char *redirect = ovl_dentry_get_redirect(dentry);
994 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
996 if (redirect && (!absolute_redirect || redirect[0] == '/'))
997 return 0;
999 redirect = ovl_get_redirect(dentry, absolute_redirect);
1000 if (IS_ERR(redirect))
1001 return PTR_ERR(redirect);
1003 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
1004 OVL_XATTR_REDIRECT,
1005 redirect, strlen(redirect), -EXDEV);
1006 if (!err) {
1007 spin_lock(&dentry->d_lock);
1008 ovl_dentry_set_redirect(dentry, redirect);
1009 spin_unlock(&dentry->d_lock);
1010 } else {
1011 kfree(redirect);
1012 pr_warn_ratelimited("overlayfs: failed to set redirect (%i)\n",
1013 err);
1014 /* Fall back to userspace copy-up */
1015 err = -EXDEV;
1017 return err;
1020 static int ovl_rename(struct inode *olddir, struct dentry *old,
1021 struct inode *newdir, struct dentry *new,
1022 unsigned int flags)
1024 int err;
1025 struct dentry *old_upperdir;
1026 struct dentry *new_upperdir;
1027 struct dentry *olddentry;
1028 struct dentry *newdentry;
1029 struct dentry *trap;
1030 bool old_opaque;
1031 bool new_opaque;
1032 bool cleanup_whiteout = false;
1033 bool update_nlink = false;
1034 bool overwrite = !(flags & RENAME_EXCHANGE);
1035 bool is_dir = d_is_dir(old);
1036 bool new_is_dir = d_is_dir(new);
1037 bool samedir = olddir == newdir;
1038 struct dentry *opaquedir = NULL;
1039 const struct cred *old_cred = NULL;
1040 LIST_HEAD(list);
1042 err = -EINVAL;
1043 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1044 goto out;
1046 flags &= ~RENAME_NOREPLACE;
1048 /* Don't copy up directory trees */
1049 err = -EXDEV;
1050 if (!ovl_can_move(old))
1051 goto out;
1052 if (!overwrite && !ovl_can_move(new))
1053 goto out;
1055 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1056 err = ovl_check_empty_dir(new, &list);
1057 if (err)
1058 goto out;
1061 if (overwrite) {
1062 if (ovl_lower_positive(old)) {
1063 if (!ovl_dentry_is_whiteout(new)) {
1064 /* Whiteout source */
1065 flags |= RENAME_WHITEOUT;
1066 } else {
1067 /* Switch whiteouts */
1068 flags |= RENAME_EXCHANGE;
1070 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1071 flags |= RENAME_EXCHANGE;
1072 cleanup_whiteout = true;
1076 err = ovl_want_write(old);
1077 if (err)
1078 goto out;
1080 err = ovl_copy_up(old);
1081 if (err)
1082 goto out_drop_write;
1084 err = ovl_copy_up(new->d_parent);
1085 if (err)
1086 goto out_drop_write;
1087 if (!overwrite) {
1088 err = ovl_copy_up(new);
1089 if (err)
1090 goto out_drop_write;
1091 } else if (d_inode(new)) {
1092 err = ovl_nlink_start(new);
1093 if (err)
1094 goto out_drop_write;
1096 update_nlink = true;
1099 old_cred = ovl_override_creds(old->d_sb);
1101 if (!list_empty(&list)) {
1102 opaquedir = ovl_clear_empty(new, &list);
1103 err = PTR_ERR(opaquedir);
1104 if (IS_ERR(opaquedir)) {
1105 opaquedir = NULL;
1106 goto out_revert_creds;
1110 old_upperdir = ovl_dentry_upper(old->d_parent);
1111 new_upperdir = ovl_dentry_upper(new->d_parent);
1113 if (!samedir) {
1115 * When moving a merge dir or non-dir with copy up origin into
1116 * a new parent, we are marking the new parent dir "impure".
1117 * When ovl_iterate() iterates an "impure" upper dir, it will
1118 * lookup the origin inodes of the entries to fill d_ino.
1120 if (ovl_type_origin(old)) {
1121 err = ovl_set_impure(new->d_parent, new_upperdir);
1122 if (err)
1123 goto out_revert_creds;
1125 if (!overwrite && ovl_type_origin(new)) {
1126 err = ovl_set_impure(old->d_parent, old_upperdir);
1127 if (err)
1128 goto out_revert_creds;
1132 trap = lock_rename(new_upperdir, old_upperdir);
1134 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
1135 old->d_name.len);
1136 err = PTR_ERR(olddentry);
1137 if (IS_ERR(olddentry))
1138 goto out_unlock;
1140 err = -ESTALE;
1141 if (!ovl_matches_upper(old, olddentry))
1142 goto out_dput_old;
1144 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1145 new->d_name.len);
1146 err = PTR_ERR(newdentry);
1147 if (IS_ERR(newdentry))
1148 goto out_dput_old;
1150 old_opaque = ovl_dentry_is_opaque(old);
1151 new_opaque = ovl_dentry_is_opaque(new);
1153 err = -ESTALE;
1154 if (d_inode(new) && ovl_dentry_upper(new)) {
1155 if (opaquedir) {
1156 if (newdentry != opaquedir)
1157 goto out_dput;
1158 } else {
1159 if (!ovl_matches_upper(new, newdentry))
1160 goto out_dput;
1162 } else {
1163 if (!d_is_negative(newdentry) &&
1164 (!new_opaque || !ovl_is_whiteout(newdentry)))
1165 goto out_dput;
1168 if (olddentry == trap)
1169 goto out_dput;
1170 if (newdentry == trap)
1171 goto out_dput;
1173 if (olddentry->d_inode == newdentry->d_inode)
1174 goto out_dput;
1176 err = 0;
1177 if (ovl_type_merge_or_lower(old))
1178 err = ovl_set_redirect(old, samedir);
1179 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1180 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1181 if (err)
1182 goto out_dput;
1184 if (!overwrite && ovl_type_merge_or_lower(new))
1185 err = ovl_set_redirect(new, samedir);
1186 else if (!overwrite && new_is_dir && !new_opaque &&
1187 ovl_type_merge(old->d_parent))
1188 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1189 if (err)
1190 goto out_dput;
1192 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1193 new_upperdir->d_inode, newdentry, flags);
1194 if (err)
1195 goto out_dput;
1197 if (cleanup_whiteout)
1198 ovl_cleanup(old_upperdir->d_inode, newdentry);
1200 if (overwrite && d_inode(new)) {
1201 if (new_is_dir)
1202 clear_nlink(d_inode(new));
1203 else
1204 drop_nlink(d_inode(new));
1207 ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1208 (!overwrite && ovl_type_origin(new)));
1209 ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1210 (d_inode(new) && ovl_type_origin(new)));
1212 /* copy ctime: */
1213 ovl_copyattr(d_inode(olddentry), d_inode(old));
1214 if (d_inode(new) && ovl_dentry_upper(new))
1215 ovl_copyattr(d_inode(newdentry), d_inode(new));
1217 out_dput:
1218 dput(newdentry);
1219 out_dput_old:
1220 dput(olddentry);
1221 out_unlock:
1222 unlock_rename(new_upperdir, old_upperdir);
1223 out_revert_creds:
1224 revert_creds(old_cred);
1225 if (update_nlink)
1226 ovl_nlink_end(new);
1227 out_drop_write:
1228 ovl_drop_write(old);
1229 out:
1230 dput(opaquedir);
1231 ovl_cache_free(&list);
1232 return err;
1235 const struct inode_operations ovl_dir_inode_operations = {
1236 .lookup = ovl_lookup,
1237 .mkdir = ovl_mkdir,
1238 .symlink = ovl_symlink,
1239 .unlink = ovl_unlink,
1240 .rmdir = ovl_rmdir,
1241 .rename = ovl_rename,
1242 .link = ovl_link,
1243 .setattr = ovl_setattr,
1244 .create = ovl_create,
1245 .mknod = ovl_mknod,
1246 .permission = ovl_permission,
1247 .getattr = ovl_getattr,
1248 .listxattr = ovl_listxattr,
1249 .get_acl = ovl_get_acl,
1250 .update_time = ovl_update_time,