Linux 4.19.133
[linux/fpc-iii.git] / fs / overlayfs / dir.c
blob800bcad67325c69e008712b9ccdee4a383381478
1 /*
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.
8 */
10 #include <linux/fs.h>
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 static int ovl_set_redirect(struct dentry *dentry, bool samedir);
29 int ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
31 int err;
33 dget(wdentry);
34 if (d_is_dir(wdentry))
35 err = ovl_do_rmdir(wdir, wdentry);
36 else
37 err = ovl_do_unlink(wdir, wdentry);
38 dput(wdentry);
40 if (err) {
41 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
42 wdentry, err);
45 return err;
48 static struct dentry *ovl_lookup_temp(struct dentry *workdir)
50 struct dentry *temp;
51 char name[20];
52 static atomic_t temp_id = ATOMIC_INIT(0);
54 /* counter is allowed to wrap, since temp dentries are ephemeral */
55 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
57 temp = lookup_one_len(name, workdir, strlen(name));
58 if (!IS_ERR(temp) && temp->d_inode) {
59 pr_err("overlayfs: workdir/%s already exists\n", name);
60 dput(temp);
61 temp = ERR_PTR(-EIO);
64 return temp;
67 /* caller holds i_mutex on workdir */
68 static struct dentry *ovl_whiteout(struct dentry *workdir)
70 int err;
71 struct dentry *whiteout;
72 struct inode *wdir = workdir->d_inode;
74 whiteout = ovl_lookup_temp(workdir);
75 if (IS_ERR(whiteout))
76 return whiteout;
78 err = ovl_do_whiteout(wdir, whiteout);
79 if (err) {
80 dput(whiteout);
81 whiteout = ERR_PTR(err);
84 return whiteout;
87 /* Caller must hold i_mutex on both workdir and dir */
88 int ovl_cleanup_and_whiteout(struct dentry *workdir, struct inode *dir,
89 struct dentry *dentry)
91 struct inode *wdir = workdir->d_inode;
92 struct dentry *whiteout;
93 int err;
94 int flags = 0;
96 whiteout = ovl_whiteout(workdir);
97 err = PTR_ERR(whiteout);
98 if (IS_ERR(whiteout))
99 return err;
101 if (d_is_dir(dentry))
102 flags = RENAME_EXCHANGE;
104 err = ovl_do_rename(wdir, whiteout, dir, dentry, flags);
105 if (err)
106 goto kill_whiteout;
107 if (flags)
108 ovl_cleanup(wdir, dentry);
110 out:
111 dput(whiteout);
112 return err;
114 kill_whiteout:
115 ovl_cleanup(wdir, whiteout);
116 goto out;
119 static int ovl_mkdir_real(struct inode *dir, struct dentry **newdentry,
120 umode_t mode)
122 int err;
123 struct dentry *d, *dentry = *newdentry;
125 err = ovl_do_mkdir(dir, dentry, mode);
126 if (err)
127 return err;
129 if (likely(!d_unhashed(dentry)))
130 return 0;
133 * vfs_mkdir() may succeed and leave the dentry passed
134 * to it unhashed and negative. If that happens, try to
135 * lookup a new hashed and positive dentry.
137 d = lookup_one_len(dentry->d_name.name, dentry->d_parent,
138 dentry->d_name.len);
139 if (IS_ERR(d)) {
140 pr_warn("overlayfs: failed lookup after mkdir (%pd2, err=%i).\n",
141 dentry, err);
142 return PTR_ERR(d);
144 dput(dentry);
145 *newdentry = d;
147 return 0;
150 struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
151 struct ovl_cattr *attr)
153 int err;
155 if (IS_ERR(newdentry))
156 return newdentry;
158 err = -ESTALE;
159 if (newdentry->d_inode)
160 goto out;
162 if (attr->hardlink) {
163 err = ovl_do_link(attr->hardlink, dir, newdentry);
164 } else {
165 switch (attr->mode & S_IFMT) {
166 case S_IFREG:
167 err = ovl_do_create(dir, newdentry, attr->mode);
168 break;
170 case S_IFDIR:
171 /* mkdir is special... */
172 err = ovl_mkdir_real(dir, &newdentry, attr->mode);
173 break;
175 case S_IFCHR:
176 case S_IFBLK:
177 case S_IFIFO:
178 case S_IFSOCK:
179 err = ovl_do_mknod(dir, newdentry, attr->mode,
180 attr->rdev);
181 break;
183 case S_IFLNK:
184 err = ovl_do_symlink(dir, newdentry, attr->link);
185 break;
187 default:
188 err = -EPERM;
191 if (!err && WARN_ON(!newdentry->d_inode)) {
193 * Not quite sure if non-instantiated dentry is legal or not.
194 * VFS doesn't seem to care so check and warn here.
196 err = -EIO;
198 out:
199 if (err) {
200 dput(newdentry);
201 return ERR_PTR(err);
203 return newdentry;
206 struct dentry *ovl_create_temp(struct dentry *workdir, struct ovl_cattr *attr)
208 return ovl_create_real(d_inode(workdir), ovl_lookup_temp(workdir),
209 attr);
212 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
213 int xerr)
215 int err;
217 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
218 if (!err)
219 ovl_dentry_set_opaque(dentry);
221 return err;
224 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
227 * Fail with -EIO when trying to create opaque dir and upper doesn't
228 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
229 * return a specific error for noxattr case.
231 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
235 * Common operations required to be done after creation of file on upper.
236 * If @hardlink is false, then @inode is a pre-allocated inode, we may or
237 * may not use to instantiate the new dentry.
239 static int ovl_instantiate(struct dentry *dentry, struct inode *inode,
240 struct dentry *newdentry, bool hardlink)
242 struct ovl_inode_params oip = {
243 .upperdentry = newdentry,
244 .newinode = inode,
247 ovl_dir_modified(dentry->d_parent, false);
248 ovl_dentry_set_upper_alias(dentry);
249 if (!hardlink) {
251 * ovl_obtain_alias() can be called after ovl_create_real()
252 * and before we get here, so we may get an inode from cache
253 * with the same real upperdentry that is not the inode we
254 * pre-allocated. In this case we will use the cached inode
255 * to instantiate the new dentry.
257 * XXX: if we ever use ovl_obtain_alias() to decode directory
258 * file handles, need to use ovl_get_inode_locked() and
259 * d_instantiate_new() here to prevent from creating two
260 * hashed directory inode aliases.
262 inode = ovl_get_inode(dentry->d_sb, &oip);
263 if (IS_ERR(inode))
264 return PTR_ERR(inode);
265 } else {
266 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
267 dput(newdentry);
268 inc_nlink(inode);
271 d_instantiate(dentry, inode);
272 if (inode != oip.newinode) {
273 pr_warn_ratelimited("overlayfs: newly created inode found in cache (%pd2)\n",
274 dentry);
277 /* Force lookup of new upper hardlink to find its lower */
278 if (hardlink)
279 d_drop(dentry);
281 return 0;
284 static bool ovl_type_merge(struct dentry *dentry)
286 return OVL_TYPE_MERGE(ovl_path_type(dentry));
289 static bool ovl_type_origin(struct dentry *dentry)
291 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
294 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
295 struct ovl_cattr *attr)
297 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
298 struct inode *udir = upperdir->d_inode;
299 struct dentry *newdentry;
300 int err;
302 if (!attr->hardlink && !IS_POSIXACL(udir))
303 attr->mode &= ~current_umask();
305 inode_lock_nested(udir, I_MUTEX_PARENT);
306 newdentry = ovl_create_real(udir,
307 lookup_one_len(dentry->d_name.name,
308 upperdir,
309 dentry->d_name.len),
310 attr);
311 err = PTR_ERR(newdentry);
312 if (IS_ERR(newdentry))
313 goto out_unlock;
315 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
316 /* Setting opaque here is just an optimization, allow to fail */
317 ovl_set_opaque(dentry, newdentry);
320 err = ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
321 if (err)
322 goto out_cleanup;
323 out_unlock:
324 inode_unlock(udir);
325 return err;
327 out_cleanup:
328 ovl_cleanup(udir, newdentry);
329 dput(newdentry);
330 goto out_unlock;
333 static struct dentry *ovl_clear_empty(struct dentry *dentry,
334 struct list_head *list)
336 struct dentry *workdir = ovl_workdir(dentry);
337 struct inode *wdir = workdir->d_inode;
338 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
339 struct inode *udir = upperdir->d_inode;
340 struct path upperpath;
341 struct dentry *upper;
342 struct dentry *opaquedir;
343 struct kstat stat;
344 int err;
346 if (WARN_ON(!workdir))
347 return ERR_PTR(-EROFS);
349 err = ovl_lock_rename_workdir(workdir, upperdir);
350 if (err)
351 goto out;
353 ovl_path_upper(dentry, &upperpath);
354 err = vfs_getattr(&upperpath, &stat,
355 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
356 if (err)
357 goto out_unlock;
359 err = -ESTALE;
360 if (!S_ISDIR(stat.mode))
361 goto out_unlock;
362 upper = upperpath.dentry;
363 if (upper->d_parent->d_inode != udir)
364 goto out_unlock;
366 opaquedir = ovl_create_temp(workdir, OVL_CATTR(stat.mode));
367 err = PTR_ERR(opaquedir);
368 if (IS_ERR(opaquedir))
369 goto out_unlock;
371 err = ovl_copy_xattr(upper, opaquedir);
372 if (err)
373 goto out_cleanup;
375 err = ovl_set_opaque(dentry, opaquedir);
376 if (err)
377 goto out_cleanup;
379 inode_lock(opaquedir->d_inode);
380 err = ovl_set_attr(opaquedir, &stat);
381 inode_unlock(opaquedir->d_inode);
382 if (err)
383 goto out_cleanup;
385 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
386 if (err)
387 goto out_cleanup;
389 ovl_cleanup_whiteouts(upper, list);
390 ovl_cleanup(wdir, upper);
391 unlock_rename(workdir, upperdir);
393 /* dentry's upper doesn't match now, get rid of it */
394 d_drop(dentry);
396 return opaquedir;
398 out_cleanup:
399 ovl_cleanup(wdir, opaquedir);
400 dput(opaquedir);
401 out_unlock:
402 unlock_rename(workdir, upperdir);
403 out:
404 return ERR_PTR(err);
407 static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
408 const struct posix_acl *acl)
410 void *buffer;
411 size_t size;
412 int err;
414 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
415 return 0;
417 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
418 buffer = kmalloc(size, GFP_KERNEL);
419 if (!buffer)
420 return -ENOMEM;
422 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
423 err = size;
424 if (err < 0)
425 goto out_free;
427 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
428 out_free:
429 kfree(buffer);
430 return err;
433 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
434 struct ovl_cattr *cattr)
436 struct dentry *workdir = ovl_workdir(dentry);
437 struct inode *wdir = workdir->d_inode;
438 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
439 struct inode *udir = upperdir->d_inode;
440 struct dentry *upper;
441 struct dentry *newdentry;
442 int err;
443 struct posix_acl *acl, *default_acl;
444 bool hardlink = !!cattr->hardlink;
446 if (WARN_ON(!workdir))
447 return -EROFS;
449 if (!hardlink) {
450 err = posix_acl_create(dentry->d_parent->d_inode,
451 &cattr->mode, &default_acl, &acl);
452 if (err)
453 return err;
456 err = ovl_lock_rename_workdir(workdir, upperdir);
457 if (err)
458 goto out;
460 upper = lookup_one_len(dentry->d_name.name, upperdir,
461 dentry->d_name.len);
462 err = PTR_ERR(upper);
463 if (IS_ERR(upper))
464 goto out_unlock;
466 err = -ESTALE;
467 if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
468 goto out_dput;
470 newdentry = ovl_create_temp(workdir, cattr);
471 err = PTR_ERR(newdentry);
472 if (IS_ERR(newdentry))
473 goto out_dput;
476 * mode could have been mutilated due to umask (e.g. sgid directory)
478 if (!hardlink &&
479 !S_ISLNK(cattr->mode) &&
480 newdentry->d_inode->i_mode != cattr->mode) {
481 struct iattr attr = {
482 .ia_valid = ATTR_MODE,
483 .ia_mode = cattr->mode,
485 inode_lock(newdentry->d_inode);
486 err = notify_change(newdentry, &attr, NULL);
487 inode_unlock(newdentry->d_inode);
488 if (err)
489 goto out_cleanup;
491 if (!hardlink) {
492 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
493 acl);
494 if (err)
495 goto out_cleanup;
497 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
498 default_acl);
499 if (err)
500 goto out_cleanup;
503 if (!hardlink && S_ISDIR(cattr->mode)) {
504 err = ovl_set_opaque(dentry, newdentry);
505 if (err)
506 goto out_cleanup;
508 err = ovl_do_rename(wdir, newdentry, udir, upper,
509 RENAME_EXCHANGE);
510 if (err)
511 goto out_cleanup;
513 ovl_cleanup(wdir, upper);
514 } else {
515 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
516 if (err)
517 goto out_cleanup;
519 err = ovl_instantiate(dentry, inode, newdentry, hardlink);
520 if (err)
521 goto out_cleanup;
522 out_dput:
523 dput(upper);
524 out_unlock:
525 unlock_rename(workdir, upperdir);
526 out:
527 if (!hardlink) {
528 posix_acl_release(acl);
529 posix_acl_release(default_acl);
531 return err;
533 out_cleanup:
534 ovl_cleanup(wdir, newdentry);
535 dput(newdentry);
536 goto out_dput;
539 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
540 struct ovl_cattr *attr, bool origin)
542 int err;
543 const struct cred *old_cred;
544 struct cred *override_cred;
545 struct dentry *parent = dentry->d_parent;
547 err = ovl_copy_up(parent);
548 if (err)
549 return err;
551 old_cred = ovl_override_creds(dentry->d_sb);
554 * When linking a file with copy up origin into a new parent, mark the
555 * new parent dir "impure".
557 if (origin) {
558 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
559 if (err)
560 goto out_revert_creds;
563 err = -ENOMEM;
564 override_cred = prepare_creds();
565 if (override_cred) {
566 override_cred->fsuid = inode->i_uid;
567 override_cred->fsgid = inode->i_gid;
568 if (!attr->hardlink) {
569 err = security_dentry_create_files_as(dentry,
570 attr->mode, &dentry->d_name, old_cred,
571 override_cred);
572 if (err) {
573 put_cred(override_cred);
574 goto out_revert_creds;
577 put_cred(override_creds(override_cred));
578 put_cred(override_cred);
580 if (!ovl_dentry_is_whiteout(dentry))
581 err = ovl_create_upper(dentry, inode, attr);
582 else
583 err = ovl_create_over_whiteout(dentry, inode, attr);
585 out_revert_creds:
586 revert_creds(old_cred);
587 return err;
590 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
591 const char *link)
593 int err;
594 struct inode *inode;
595 struct ovl_cattr attr = {
596 .rdev = rdev,
597 .link = link,
600 err = ovl_want_write(dentry);
601 if (err)
602 goto out;
604 /* Preallocate inode to be used by ovl_get_inode() */
605 err = -ENOMEM;
606 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
607 if (!inode)
608 goto out_drop_write;
610 spin_lock(&inode->i_lock);
611 inode->i_state |= I_CREATING;
612 spin_unlock(&inode->i_lock);
614 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
615 attr.mode = inode->i_mode;
617 err = ovl_create_or_link(dentry, inode, &attr, false);
618 /* Did we end up using the preallocated inode? */
619 if (inode != d_inode(dentry))
620 iput(inode);
622 out_drop_write:
623 ovl_drop_write(dentry);
624 out:
625 return err;
628 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
629 bool excl)
631 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
634 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
636 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
639 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
640 dev_t rdev)
642 /* Don't allow creation of "whiteout" on overlay */
643 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
644 return -EPERM;
646 return ovl_create_object(dentry, mode, rdev, NULL);
649 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
650 const char *link)
652 return ovl_create_object(dentry, S_IFLNK, 0, link);
655 static int ovl_set_link_redirect(struct dentry *dentry)
657 const struct cred *old_cred;
658 int err;
660 old_cred = ovl_override_creds(dentry->d_sb);
661 err = ovl_set_redirect(dentry, false);
662 revert_creds(old_cred);
664 return err;
667 static int ovl_link(struct dentry *old, struct inode *newdir,
668 struct dentry *new)
670 int err;
671 bool locked = false;
672 struct inode *inode;
674 err = ovl_want_write(old);
675 if (err)
676 goto out;
678 err = ovl_copy_up(old);
679 if (err)
680 goto out_drop_write;
682 err = ovl_copy_up(new->d_parent);
683 if (err)
684 goto out_drop_write;
686 if (ovl_is_metacopy_dentry(old)) {
687 err = ovl_set_link_redirect(old);
688 if (err)
689 goto out_drop_write;
692 err = ovl_nlink_start(old, &locked);
693 if (err)
694 goto out_drop_write;
696 inode = d_inode(old);
697 ihold(inode);
699 err = ovl_create_or_link(new, inode,
700 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
701 ovl_type_origin(old));
702 if (err)
703 iput(inode);
705 ovl_nlink_end(old, locked);
706 out_drop_write:
707 ovl_drop_write(old);
708 out:
709 return err;
712 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
714 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
717 static int ovl_remove_and_whiteout(struct dentry *dentry,
718 struct list_head *list)
720 struct dentry *workdir = ovl_workdir(dentry);
721 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
722 struct dentry *upper;
723 struct dentry *opaquedir = NULL;
724 int err;
726 if (WARN_ON(!workdir))
727 return -EROFS;
729 if (!list_empty(list)) {
730 opaquedir = ovl_clear_empty(dentry, list);
731 err = PTR_ERR(opaquedir);
732 if (IS_ERR(opaquedir))
733 goto out;
736 err = ovl_lock_rename_workdir(workdir, upperdir);
737 if (err)
738 goto out_dput;
740 upper = lookup_one_len(dentry->d_name.name, upperdir,
741 dentry->d_name.len);
742 err = PTR_ERR(upper);
743 if (IS_ERR(upper))
744 goto out_unlock;
746 err = -ESTALE;
747 if ((opaquedir && upper != opaquedir) ||
748 (!opaquedir && ovl_dentry_upper(dentry) &&
749 !ovl_matches_upper(dentry, upper))) {
750 goto out_dput_upper;
753 err = ovl_cleanup_and_whiteout(workdir, d_inode(upperdir), upper);
754 if (err)
755 goto out_d_drop;
757 ovl_dir_modified(dentry->d_parent, true);
758 out_d_drop:
759 d_drop(dentry);
760 out_dput_upper:
761 dput(upper);
762 out_unlock:
763 unlock_rename(workdir, upperdir);
764 out_dput:
765 dput(opaquedir);
766 out:
767 return err;
770 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
771 struct list_head *list)
773 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
774 struct inode *dir = upperdir->d_inode;
775 struct dentry *upper;
776 struct dentry *opaquedir = NULL;
777 int err;
779 if (!list_empty(list)) {
780 opaquedir = ovl_clear_empty(dentry, list);
781 err = PTR_ERR(opaquedir);
782 if (IS_ERR(opaquedir))
783 goto out;
786 inode_lock_nested(dir, I_MUTEX_PARENT);
787 upper = lookup_one_len(dentry->d_name.name, upperdir,
788 dentry->d_name.len);
789 err = PTR_ERR(upper);
790 if (IS_ERR(upper))
791 goto out_unlock;
793 err = -ESTALE;
794 if ((opaquedir && upper != opaquedir) ||
795 (!opaquedir && !ovl_matches_upper(dentry, upper)))
796 goto out_dput_upper;
798 if (is_dir)
799 err = vfs_rmdir(dir, upper);
800 else
801 err = vfs_unlink(dir, upper, NULL);
802 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
805 * Keeping this dentry hashed would mean having to release
806 * upperpath/lowerpath, which could only be done if we are the
807 * sole user of this dentry. Too tricky... Just unhash for
808 * now.
810 if (!err)
811 d_drop(dentry);
812 out_dput_upper:
813 dput(upper);
814 out_unlock:
815 inode_unlock(dir);
816 dput(opaquedir);
817 out:
818 return err;
821 static bool ovl_pure_upper(struct dentry *dentry)
823 return !ovl_dentry_lower(dentry) &&
824 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
827 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
829 int err;
830 bool locked = false;
831 const struct cred *old_cred;
832 struct dentry *upperdentry;
833 bool lower_positive = ovl_lower_positive(dentry);
834 LIST_HEAD(list);
836 /* No need to clean pure upper removed by vfs_rmdir() */
837 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
838 err = ovl_check_empty_dir(dentry, &list);
839 if (err)
840 goto out;
843 err = ovl_want_write(dentry);
844 if (err)
845 goto out;
847 err = ovl_copy_up(dentry->d_parent);
848 if (err)
849 goto out_drop_write;
851 err = ovl_nlink_start(dentry, &locked);
852 if (err)
853 goto out_drop_write;
855 old_cred = ovl_override_creds(dentry->d_sb);
856 if (!lower_positive)
857 err = ovl_remove_upper(dentry, is_dir, &list);
858 else
859 err = ovl_remove_and_whiteout(dentry, &list);
860 revert_creds(old_cred);
861 if (!err) {
862 if (is_dir)
863 clear_nlink(dentry->d_inode);
864 else
865 drop_nlink(dentry->d_inode);
867 ovl_nlink_end(dentry, locked);
870 * Copy ctime
872 * Note: we fail to update ctime if there was no copy-up, only a
873 * whiteout
875 upperdentry = ovl_dentry_upper(dentry);
876 if (upperdentry)
877 ovl_copyattr(d_inode(upperdentry), d_inode(dentry));
879 out_drop_write:
880 ovl_drop_write(dentry);
881 out:
882 ovl_cache_free(&list);
883 return err;
886 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
888 return ovl_do_remove(dentry, false);
891 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
893 return ovl_do_remove(dentry, true);
896 static bool ovl_type_merge_or_lower(struct dentry *dentry)
898 enum ovl_path_type type = ovl_path_type(dentry);
900 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
903 static bool ovl_can_move(struct dentry *dentry)
905 return ovl_redirect_dir(dentry->d_sb) ||
906 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
909 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
911 char *buf, *ret;
912 struct dentry *d, *tmp;
913 int buflen = ovl_redirect_max + 1;
915 if (!abs_redirect) {
916 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
917 GFP_KERNEL);
918 goto out;
921 buf = ret = kmalloc(buflen, GFP_KERNEL);
922 if (!buf)
923 goto out;
925 buflen--;
926 buf[buflen] = '\0';
927 for (d = dget(dentry); !IS_ROOT(d);) {
928 const char *name;
929 int thislen;
931 spin_lock(&d->d_lock);
932 name = ovl_dentry_get_redirect(d);
933 if (name) {
934 thislen = strlen(name);
935 } else {
936 name = d->d_name.name;
937 thislen = d->d_name.len;
940 /* If path is too long, fall back to userspace move */
941 if (thislen + (name[0] != '/') > buflen) {
942 ret = ERR_PTR(-EXDEV);
943 spin_unlock(&d->d_lock);
944 goto out_put;
947 buflen -= thislen;
948 memcpy(&buf[buflen], name, thislen);
949 tmp = dget_dlock(d->d_parent);
950 spin_unlock(&d->d_lock);
952 dput(d);
953 d = tmp;
955 /* Absolute redirect: finished */
956 if (buf[buflen] == '/')
957 break;
958 buflen--;
959 buf[buflen] = '/';
961 ret = kstrdup(&buf[buflen], GFP_KERNEL);
962 out_put:
963 dput(d);
964 kfree(buf);
965 out:
966 return ret ? ret : ERR_PTR(-ENOMEM);
969 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
971 struct dentry *lowerdentry;
973 if (!samedir)
974 return true;
976 if (d_is_dir(dentry))
977 return false;
980 * For non-dir hardlinked files, we need absolute redirects
981 * in general as two upper hardlinks could be in different
982 * dirs. We could put a relative redirect now and convert
983 * it to absolute redirect later. But when nlink > 1 and
984 * indexing is on, that means relative redirect needs to be
985 * converted to absolute during copy up of another lower
986 * hardllink as well.
988 * So without optimizing too much, just check if lower is
989 * a hard link or not. If lower is hard link, put absolute
990 * redirect.
992 lowerdentry = ovl_dentry_lower(dentry);
993 return (d_inode(lowerdentry)->i_nlink > 1);
996 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
998 int err;
999 const char *redirect = ovl_dentry_get_redirect(dentry);
1000 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
1002 if (redirect && (!absolute_redirect || redirect[0] == '/'))
1003 return 0;
1005 redirect = ovl_get_redirect(dentry, absolute_redirect);
1006 if (IS_ERR(redirect))
1007 return PTR_ERR(redirect);
1009 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
1010 OVL_XATTR_REDIRECT,
1011 redirect, strlen(redirect), -EXDEV);
1012 if (!err) {
1013 spin_lock(&dentry->d_lock);
1014 ovl_dentry_set_redirect(dentry, redirect);
1015 spin_unlock(&dentry->d_lock);
1016 } else {
1017 kfree(redirect);
1018 pr_warn_ratelimited("overlayfs: failed to set redirect (%i)\n",
1019 err);
1020 /* Fall back to userspace copy-up */
1021 err = -EXDEV;
1023 return err;
1026 static int ovl_rename(struct inode *olddir, struct dentry *old,
1027 struct inode *newdir, struct dentry *new,
1028 unsigned int flags)
1030 int err;
1031 bool locked = false;
1032 struct dentry *old_upperdir;
1033 struct dentry *new_upperdir;
1034 struct dentry *olddentry;
1035 struct dentry *newdentry;
1036 struct dentry *trap;
1037 bool old_opaque;
1038 bool new_opaque;
1039 bool cleanup_whiteout = false;
1040 bool overwrite = !(flags & RENAME_EXCHANGE);
1041 bool is_dir = d_is_dir(old);
1042 bool new_is_dir = d_is_dir(new);
1043 bool samedir = olddir == newdir;
1044 struct dentry *opaquedir = NULL;
1045 const struct cred *old_cred = NULL;
1046 LIST_HEAD(list);
1048 err = -EINVAL;
1049 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1050 goto out;
1052 flags &= ~RENAME_NOREPLACE;
1054 /* Don't copy up directory trees */
1055 err = -EXDEV;
1056 if (!ovl_can_move(old))
1057 goto out;
1058 if (!overwrite && !ovl_can_move(new))
1059 goto out;
1061 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1062 err = ovl_check_empty_dir(new, &list);
1063 if (err)
1064 goto out;
1067 if (overwrite) {
1068 if (ovl_lower_positive(old)) {
1069 if (!ovl_dentry_is_whiteout(new)) {
1070 /* Whiteout source */
1071 flags |= RENAME_WHITEOUT;
1072 } else {
1073 /* Switch whiteouts */
1074 flags |= RENAME_EXCHANGE;
1076 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1077 flags |= RENAME_EXCHANGE;
1078 cleanup_whiteout = true;
1082 err = ovl_want_write(old);
1083 if (err)
1084 goto out;
1086 err = ovl_copy_up(old);
1087 if (err)
1088 goto out_drop_write;
1090 err = ovl_copy_up(new->d_parent);
1091 if (err)
1092 goto out_drop_write;
1093 if (!overwrite) {
1094 err = ovl_copy_up(new);
1095 if (err)
1096 goto out_drop_write;
1097 } else {
1098 err = ovl_nlink_start(new, &locked);
1099 if (err)
1100 goto out_drop_write;
1103 old_cred = ovl_override_creds(old->d_sb);
1105 if (!list_empty(&list)) {
1106 opaquedir = ovl_clear_empty(new, &list);
1107 err = PTR_ERR(opaquedir);
1108 if (IS_ERR(opaquedir)) {
1109 opaquedir = NULL;
1110 goto out_revert_creds;
1114 old_upperdir = ovl_dentry_upper(old->d_parent);
1115 new_upperdir = ovl_dentry_upper(new->d_parent);
1117 if (!samedir) {
1119 * When moving a merge dir or non-dir with copy up origin into
1120 * a new parent, we are marking the new parent dir "impure".
1121 * When ovl_iterate() iterates an "impure" upper dir, it will
1122 * lookup the origin inodes of the entries to fill d_ino.
1124 if (ovl_type_origin(old)) {
1125 err = ovl_set_impure(new->d_parent, new_upperdir);
1126 if (err)
1127 goto out_revert_creds;
1129 if (!overwrite && ovl_type_origin(new)) {
1130 err = ovl_set_impure(old->d_parent, old_upperdir);
1131 if (err)
1132 goto out_revert_creds;
1136 trap = lock_rename(new_upperdir, old_upperdir);
1138 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
1139 old->d_name.len);
1140 err = PTR_ERR(olddentry);
1141 if (IS_ERR(olddentry))
1142 goto out_unlock;
1144 err = -ESTALE;
1145 if (!ovl_matches_upper(old, olddentry))
1146 goto out_dput_old;
1148 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1149 new->d_name.len);
1150 err = PTR_ERR(newdentry);
1151 if (IS_ERR(newdentry))
1152 goto out_dput_old;
1154 old_opaque = ovl_dentry_is_opaque(old);
1155 new_opaque = ovl_dentry_is_opaque(new);
1157 err = -ESTALE;
1158 if (d_inode(new) && ovl_dentry_upper(new)) {
1159 if (opaquedir) {
1160 if (newdentry != opaquedir)
1161 goto out_dput;
1162 } else {
1163 if (!ovl_matches_upper(new, newdentry))
1164 goto out_dput;
1166 } else {
1167 if (!d_is_negative(newdentry) &&
1168 (!new_opaque || !ovl_is_whiteout(newdentry)))
1169 goto out_dput;
1172 if (olddentry == trap)
1173 goto out_dput;
1174 if (newdentry == trap)
1175 goto out_dput;
1177 if (olddentry->d_inode == newdentry->d_inode)
1178 goto out_dput;
1180 err = 0;
1181 if (ovl_type_merge_or_lower(old))
1182 err = ovl_set_redirect(old, samedir);
1183 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1184 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1185 if (err)
1186 goto out_dput;
1188 if (!overwrite && ovl_type_merge_or_lower(new))
1189 err = ovl_set_redirect(new, samedir);
1190 else if (!overwrite && new_is_dir && !new_opaque &&
1191 ovl_type_merge(old->d_parent))
1192 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1193 if (err)
1194 goto out_dput;
1196 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1197 new_upperdir->d_inode, newdentry, flags);
1198 if (err)
1199 goto out_dput;
1201 if (cleanup_whiteout)
1202 ovl_cleanup(old_upperdir->d_inode, newdentry);
1204 if (overwrite && d_inode(new)) {
1205 if (new_is_dir)
1206 clear_nlink(d_inode(new));
1207 else
1208 drop_nlink(d_inode(new));
1211 ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1212 (!overwrite && ovl_type_origin(new)));
1213 ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1214 (d_inode(new) && ovl_type_origin(new)));
1216 /* copy ctime: */
1217 ovl_copyattr(d_inode(olddentry), d_inode(old));
1218 if (d_inode(new) && ovl_dentry_upper(new))
1219 ovl_copyattr(d_inode(newdentry), d_inode(new));
1221 out_dput:
1222 dput(newdentry);
1223 out_dput_old:
1224 dput(olddentry);
1225 out_unlock:
1226 unlock_rename(new_upperdir, old_upperdir);
1227 out_revert_creds:
1228 revert_creds(old_cred);
1229 ovl_nlink_end(new, locked);
1230 out_drop_write:
1231 ovl_drop_write(old);
1232 out:
1233 dput(opaquedir);
1234 ovl_cache_free(&list);
1235 return err;
1238 const struct inode_operations ovl_dir_inode_operations = {
1239 .lookup = ovl_lookup,
1240 .mkdir = ovl_mkdir,
1241 .symlink = ovl_symlink,
1242 .unlink = ovl_unlink,
1243 .rmdir = ovl_rmdir,
1244 .rename = ovl_rename,
1245 .link = ovl_link,
1246 .setattr = ovl_setattr,
1247 .create = ovl_create,
1248 .mknod = ovl_mknod,
1249 .permission = ovl_permission,
1250 .getattr = ovl_getattr,
1251 .listxattr = ovl_listxattr,
1252 .get_acl = ovl_get_acl,
1253 .update_time = ovl_update_time,