SCSI: fix queue cleanup race before queue initialization is done
[linux/fpc-iii.git] / fs / overlayfs / dir.c
blobc6289147c7871f165b70f5ca8e13668f3010d4b0
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 (WARN_ON(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_xattr_size(acl->a_count);
418 buffer = kmalloc(size, GFP_KERNEL);
419 if (!buffer)
420 return -ENOMEM;
422 err = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
423 if (err < 0)
424 goto out_free;
426 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
427 out_free:
428 kfree(buffer);
429 return err;
432 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
433 struct ovl_cattr *cattr)
435 struct dentry *workdir = ovl_workdir(dentry);
436 struct inode *wdir = workdir->d_inode;
437 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
438 struct inode *udir = upperdir->d_inode;
439 struct dentry *upper;
440 struct dentry *newdentry;
441 int err;
442 struct posix_acl *acl, *default_acl;
443 bool hardlink = !!cattr->hardlink;
445 if (WARN_ON(!workdir))
446 return -EROFS;
448 if (!hardlink) {
449 err = posix_acl_create(dentry->d_parent->d_inode,
450 &cattr->mode, &default_acl, &acl);
451 if (err)
452 return err;
455 err = ovl_lock_rename_workdir(workdir, upperdir);
456 if (err)
457 goto out;
459 upper = lookup_one_len(dentry->d_name.name, upperdir,
460 dentry->d_name.len);
461 err = PTR_ERR(upper);
462 if (IS_ERR(upper))
463 goto out_unlock;
465 err = -ESTALE;
466 if (d_is_negative(upper) || !IS_WHITEOUT(d_inode(upper)))
467 goto out_dput;
469 newdentry = ovl_create_temp(workdir, cattr);
470 err = PTR_ERR(newdentry);
471 if (IS_ERR(newdentry))
472 goto out_dput;
475 * mode could have been mutilated due to umask (e.g. sgid directory)
477 if (!hardlink &&
478 !S_ISLNK(cattr->mode) &&
479 newdentry->d_inode->i_mode != cattr->mode) {
480 struct iattr attr = {
481 .ia_valid = ATTR_MODE,
482 .ia_mode = cattr->mode,
484 inode_lock(newdentry->d_inode);
485 err = notify_change(newdentry, &attr, NULL);
486 inode_unlock(newdentry->d_inode);
487 if (err)
488 goto out_cleanup;
490 if (!hardlink) {
491 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
492 acl);
493 if (err)
494 goto out_cleanup;
496 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
497 default_acl);
498 if (err)
499 goto out_cleanup;
502 if (!hardlink && S_ISDIR(cattr->mode)) {
503 err = ovl_set_opaque(dentry, newdentry);
504 if (err)
505 goto out_cleanup;
507 err = ovl_do_rename(wdir, newdentry, udir, upper,
508 RENAME_EXCHANGE);
509 if (err)
510 goto out_cleanup;
512 ovl_cleanup(wdir, upper);
513 } else {
514 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
515 if (err)
516 goto out_cleanup;
518 err = ovl_instantiate(dentry, inode, newdentry, hardlink);
519 if (err)
520 goto out_cleanup;
521 out_dput:
522 dput(upper);
523 out_unlock:
524 unlock_rename(workdir, upperdir);
525 out:
526 if (!hardlink) {
527 posix_acl_release(acl);
528 posix_acl_release(default_acl);
530 return err;
532 out_cleanup:
533 ovl_cleanup(wdir, newdentry);
534 dput(newdentry);
535 goto out_dput;
538 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
539 struct ovl_cattr *attr, bool origin)
541 int err;
542 const struct cred *old_cred;
543 struct cred *override_cred;
544 struct dentry *parent = dentry->d_parent;
546 err = ovl_copy_up(parent);
547 if (err)
548 return err;
550 old_cred = ovl_override_creds(dentry->d_sb);
553 * When linking a file with copy up origin into a new parent, mark the
554 * new parent dir "impure".
556 if (origin) {
557 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
558 if (err)
559 goto out_revert_creds;
562 err = -ENOMEM;
563 override_cred = prepare_creds();
564 if (override_cred) {
565 override_cred->fsuid = inode->i_uid;
566 override_cred->fsgid = inode->i_gid;
567 if (!attr->hardlink) {
568 err = security_dentry_create_files_as(dentry,
569 attr->mode, &dentry->d_name, old_cred,
570 override_cred);
571 if (err) {
572 put_cred(override_cred);
573 goto out_revert_creds;
576 put_cred(override_creds(override_cred));
577 put_cred(override_cred);
579 if (!ovl_dentry_is_whiteout(dentry))
580 err = ovl_create_upper(dentry, inode, attr);
581 else
582 err = ovl_create_over_whiteout(dentry, inode, attr);
584 out_revert_creds:
585 revert_creds(old_cred);
586 return err;
589 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
590 const char *link)
592 int err;
593 struct inode *inode;
594 struct ovl_cattr attr = {
595 .rdev = rdev,
596 .link = link,
599 err = ovl_want_write(dentry);
600 if (err)
601 goto out;
603 /* Preallocate inode to be used by ovl_get_inode() */
604 err = -ENOMEM;
605 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
606 if (!inode)
607 goto out_drop_write;
609 spin_lock(&inode->i_lock);
610 inode->i_state |= I_CREATING;
611 spin_unlock(&inode->i_lock);
613 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
614 attr.mode = inode->i_mode;
616 err = ovl_create_or_link(dentry, inode, &attr, false);
617 /* Did we end up using the preallocated inode? */
618 if (inode != d_inode(dentry))
619 iput(inode);
621 out_drop_write:
622 ovl_drop_write(dentry);
623 out:
624 return err;
627 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
628 bool excl)
630 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
633 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
635 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
638 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
639 dev_t rdev)
641 /* Don't allow creation of "whiteout" on overlay */
642 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
643 return -EPERM;
645 return ovl_create_object(dentry, mode, rdev, NULL);
648 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
649 const char *link)
651 return ovl_create_object(dentry, S_IFLNK, 0, link);
654 static int ovl_link(struct dentry *old, struct inode *newdir,
655 struct dentry *new)
657 int err;
658 struct inode *inode;
660 err = ovl_want_write(old);
661 if (err)
662 goto out;
664 err = ovl_copy_up(old);
665 if (err)
666 goto out_drop_write;
668 err = ovl_copy_up(new->d_parent);
669 if (err)
670 goto out_drop_write;
672 if (ovl_is_metacopy_dentry(old)) {
673 err = ovl_set_redirect(old, false);
674 if (err)
675 goto out_drop_write;
678 err = ovl_nlink_start(old);
679 if (err)
680 goto out_drop_write;
682 inode = d_inode(old);
683 ihold(inode);
685 err = ovl_create_or_link(new, inode,
686 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
687 ovl_type_origin(old));
688 if (err)
689 iput(inode);
691 ovl_nlink_end(old);
692 out_drop_write:
693 ovl_drop_write(old);
694 out:
695 return err;
698 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
700 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
703 static int ovl_remove_and_whiteout(struct dentry *dentry,
704 struct list_head *list)
706 struct dentry *workdir = ovl_workdir(dentry);
707 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
708 struct dentry *upper;
709 struct dentry *opaquedir = NULL;
710 int err;
712 if (WARN_ON(!workdir))
713 return -EROFS;
715 if (!list_empty(list)) {
716 opaquedir = ovl_clear_empty(dentry, list);
717 err = PTR_ERR(opaquedir);
718 if (IS_ERR(opaquedir))
719 goto out;
722 err = ovl_lock_rename_workdir(workdir, upperdir);
723 if (err)
724 goto out_dput;
726 upper = lookup_one_len(dentry->d_name.name, upperdir,
727 dentry->d_name.len);
728 err = PTR_ERR(upper);
729 if (IS_ERR(upper))
730 goto out_unlock;
732 err = -ESTALE;
733 if ((opaquedir && upper != opaquedir) ||
734 (!opaquedir && ovl_dentry_upper(dentry) &&
735 !ovl_matches_upper(dentry, upper))) {
736 goto out_dput_upper;
739 err = ovl_cleanup_and_whiteout(workdir, d_inode(upperdir), upper);
740 if (err)
741 goto out_d_drop;
743 ovl_dir_modified(dentry->d_parent, true);
744 out_d_drop:
745 d_drop(dentry);
746 out_dput_upper:
747 dput(upper);
748 out_unlock:
749 unlock_rename(workdir, upperdir);
750 out_dput:
751 dput(opaquedir);
752 out:
753 return err;
756 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
757 struct list_head *list)
759 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
760 struct inode *dir = upperdir->d_inode;
761 struct dentry *upper;
762 struct dentry *opaquedir = NULL;
763 int err;
765 if (!list_empty(list)) {
766 opaquedir = ovl_clear_empty(dentry, list);
767 err = PTR_ERR(opaquedir);
768 if (IS_ERR(opaquedir))
769 goto out;
772 inode_lock_nested(dir, I_MUTEX_PARENT);
773 upper = lookup_one_len(dentry->d_name.name, upperdir,
774 dentry->d_name.len);
775 err = PTR_ERR(upper);
776 if (IS_ERR(upper))
777 goto out_unlock;
779 err = -ESTALE;
780 if ((opaquedir && upper != opaquedir) ||
781 (!opaquedir && !ovl_matches_upper(dentry, upper)))
782 goto out_dput_upper;
784 if (is_dir)
785 err = vfs_rmdir(dir, upper);
786 else
787 err = vfs_unlink(dir, upper, NULL);
788 ovl_dir_modified(dentry->d_parent, ovl_type_origin(dentry));
791 * Keeping this dentry hashed would mean having to release
792 * upperpath/lowerpath, which could only be done if we are the
793 * sole user of this dentry. Too tricky... Just unhash for
794 * now.
796 if (!err)
797 d_drop(dentry);
798 out_dput_upper:
799 dput(upper);
800 out_unlock:
801 inode_unlock(dir);
802 dput(opaquedir);
803 out:
804 return err;
807 static bool ovl_pure_upper(struct dentry *dentry)
809 return !ovl_dentry_lower(dentry) &&
810 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
813 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
815 int err;
816 const struct cred *old_cred;
817 struct dentry *upperdentry;
818 bool lower_positive = ovl_lower_positive(dentry);
819 LIST_HEAD(list);
821 /* No need to clean pure upper removed by vfs_rmdir() */
822 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
823 err = ovl_check_empty_dir(dentry, &list);
824 if (err)
825 goto out;
828 err = ovl_want_write(dentry);
829 if (err)
830 goto out;
832 err = ovl_copy_up(dentry->d_parent);
833 if (err)
834 goto out_drop_write;
836 err = ovl_nlink_start(dentry);
837 if (err)
838 goto out_drop_write;
840 old_cred = ovl_override_creds(dentry->d_sb);
841 if (!lower_positive)
842 err = ovl_remove_upper(dentry, is_dir, &list);
843 else
844 err = ovl_remove_and_whiteout(dentry, &list);
845 revert_creds(old_cred);
846 if (!err) {
847 if (is_dir)
848 clear_nlink(dentry->d_inode);
849 else
850 drop_nlink(dentry->d_inode);
852 ovl_nlink_end(dentry);
855 * Copy ctime
857 * Note: we fail to update ctime if there was no copy-up, only a
858 * whiteout
860 upperdentry = ovl_dentry_upper(dentry);
861 if (upperdentry)
862 ovl_copyattr(d_inode(upperdentry), d_inode(dentry));
864 out_drop_write:
865 ovl_drop_write(dentry);
866 out:
867 ovl_cache_free(&list);
868 return err;
871 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
873 return ovl_do_remove(dentry, false);
876 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
878 return ovl_do_remove(dentry, true);
881 static bool ovl_type_merge_or_lower(struct dentry *dentry)
883 enum ovl_path_type type = ovl_path_type(dentry);
885 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
888 static bool ovl_can_move(struct dentry *dentry)
890 return ovl_redirect_dir(dentry->d_sb) ||
891 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
894 static char *ovl_get_redirect(struct dentry *dentry, bool abs_redirect)
896 char *buf, *ret;
897 struct dentry *d, *tmp;
898 int buflen = ovl_redirect_max + 1;
900 if (!abs_redirect) {
901 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
902 GFP_KERNEL);
903 goto out;
906 buf = ret = kmalloc(buflen, GFP_KERNEL);
907 if (!buf)
908 goto out;
910 buflen--;
911 buf[buflen] = '\0';
912 for (d = dget(dentry); !IS_ROOT(d);) {
913 const char *name;
914 int thislen;
916 spin_lock(&d->d_lock);
917 name = ovl_dentry_get_redirect(d);
918 if (name) {
919 thislen = strlen(name);
920 } else {
921 name = d->d_name.name;
922 thislen = d->d_name.len;
925 /* If path is too long, fall back to userspace move */
926 if (thislen + (name[0] != '/') > buflen) {
927 ret = ERR_PTR(-EXDEV);
928 spin_unlock(&d->d_lock);
929 goto out_put;
932 buflen -= thislen;
933 memcpy(&buf[buflen], name, thislen);
934 tmp = dget_dlock(d->d_parent);
935 spin_unlock(&d->d_lock);
937 dput(d);
938 d = tmp;
940 /* Absolute redirect: finished */
941 if (buf[buflen] == '/')
942 break;
943 buflen--;
944 buf[buflen] = '/';
946 ret = kstrdup(&buf[buflen], GFP_KERNEL);
947 out_put:
948 dput(d);
949 kfree(buf);
950 out:
951 return ret ? ret : ERR_PTR(-ENOMEM);
954 static bool ovl_need_absolute_redirect(struct dentry *dentry, bool samedir)
956 struct dentry *lowerdentry;
958 if (!samedir)
959 return true;
961 if (d_is_dir(dentry))
962 return false;
965 * For non-dir hardlinked files, we need absolute redirects
966 * in general as two upper hardlinks could be in different
967 * dirs. We could put a relative redirect now and convert
968 * it to absolute redirect later. But when nlink > 1 and
969 * indexing is on, that means relative redirect needs to be
970 * converted to absolute during copy up of another lower
971 * hardllink as well.
973 * So without optimizing too much, just check if lower is
974 * a hard link or not. If lower is hard link, put absolute
975 * redirect.
977 lowerdentry = ovl_dentry_lower(dentry);
978 return (d_inode(lowerdentry)->i_nlink > 1);
981 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
983 int err;
984 const char *redirect = ovl_dentry_get_redirect(dentry);
985 bool absolute_redirect = ovl_need_absolute_redirect(dentry, samedir);
987 if (redirect && (!absolute_redirect || redirect[0] == '/'))
988 return 0;
990 redirect = ovl_get_redirect(dentry, absolute_redirect);
991 if (IS_ERR(redirect))
992 return PTR_ERR(redirect);
994 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
995 OVL_XATTR_REDIRECT,
996 redirect, strlen(redirect), -EXDEV);
997 if (!err) {
998 spin_lock(&dentry->d_lock);
999 ovl_dentry_set_redirect(dentry, redirect);
1000 spin_unlock(&dentry->d_lock);
1001 } else {
1002 kfree(redirect);
1003 pr_warn_ratelimited("overlayfs: failed to set redirect (%i)\n",
1004 err);
1005 /* Fall back to userspace copy-up */
1006 err = -EXDEV;
1008 return err;
1011 static int ovl_rename(struct inode *olddir, struct dentry *old,
1012 struct inode *newdir, struct dentry *new,
1013 unsigned int flags)
1015 int err;
1016 struct dentry *old_upperdir;
1017 struct dentry *new_upperdir;
1018 struct dentry *olddentry;
1019 struct dentry *newdentry;
1020 struct dentry *trap;
1021 bool old_opaque;
1022 bool new_opaque;
1023 bool cleanup_whiteout = false;
1024 bool update_nlink = false;
1025 bool overwrite = !(flags & RENAME_EXCHANGE);
1026 bool is_dir = d_is_dir(old);
1027 bool new_is_dir = d_is_dir(new);
1028 bool samedir = olddir == newdir;
1029 struct dentry *opaquedir = NULL;
1030 const struct cred *old_cred = NULL;
1031 LIST_HEAD(list);
1033 err = -EINVAL;
1034 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
1035 goto out;
1037 flags &= ~RENAME_NOREPLACE;
1039 /* Don't copy up directory trees */
1040 err = -EXDEV;
1041 if (!ovl_can_move(old))
1042 goto out;
1043 if (!overwrite && !ovl_can_move(new))
1044 goto out;
1046 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
1047 err = ovl_check_empty_dir(new, &list);
1048 if (err)
1049 goto out;
1052 if (overwrite) {
1053 if (ovl_lower_positive(old)) {
1054 if (!ovl_dentry_is_whiteout(new)) {
1055 /* Whiteout source */
1056 flags |= RENAME_WHITEOUT;
1057 } else {
1058 /* Switch whiteouts */
1059 flags |= RENAME_EXCHANGE;
1061 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
1062 flags |= RENAME_EXCHANGE;
1063 cleanup_whiteout = true;
1067 err = ovl_want_write(old);
1068 if (err)
1069 goto out;
1071 err = ovl_copy_up(old);
1072 if (err)
1073 goto out_drop_write;
1075 err = ovl_copy_up(new->d_parent);
1076 if (err)
1077 goto out_drop_write;
1078 if (!overwrite) {
1079 err = ovl_copy_up(new);
1080 if (err)
1081 goto out_drop_write;
1082 } else if (d_inode(new)) {
1083 err = ovl_nlink_start(new);
1084 if (err)
1085 goto out_drop_write;
1087 update_nlink = true;
1090 old_cred = ovl_override_creds(old->d_sb);
1092 if (!list_empty(&list)) {
1093 opaquedir = ovl_clear_empty(new, &list);
1094 err = PTR_ERR(opaquedir);
1095 if (IS_ERR(opaquedir)) {
1096 opaquedir = NULL;
1097 goto out_revert_creds;
1101 old_upperdir = ovl_dentry_upper(old->d_parent);
1102 new_upperdir = ovl_dentry_upper(new->d_parent);
1104 if (!samedir) {
1106 * When moving a merge dir or non-dir with copy up origin into
1107 * a new parent, we are marking the new parent dir "impure".
1108 * When ovl_iterate() iterates an "impure" upper dir, it will
1109 * lookup the origin inodes of the entries to fill d_ino.
1111 if (ovl_type_origin(old)) {
1112 err = ovl_set_impure(new->d_parent, new_upperdir);
1113 if (err)
1114 goto out_revert_creds;
1116 if (!overwrite && ovl_type_origin(new)) {
1117 err = ovl_set_impure(old->d_parent, old_upperdir);
1118 if (err)
1119 goto out_revert_creds;
1123 trap = lock_rename(new_upperdir, old_upperdir);
1125 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
1126 old->d_name.len);
1127 err = PTR_ERR(olddentry);
1128 if (IS_ERR(olddentry))
1129 goto out_unlock;
1131 err = -ESTALE;
1132 if (!ovl_matches_upper(old, olddentry))
1133 goto out_dput_old;
1135 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1136 new->d_name.len);
1137 err = PTR_ERR(newdentry);
1138 if (IS_ERR(newdentry))
1139 goto out_dput_old;
1141 old_opaque = ovl_dentry_is_opaque(old);
1142 new_opaque = ovl_dentry_is_opaque(new);
1144 err = -ESTALE;
1145 if (d_inode(new) && ovl_dentry_upper(new)) {
1146 if (opaquedir) {
1147 if (newdentry != opaquedir)
1148 goto out_dput;
1149 } else {
1150 if (!ovl_matches_upper(new, newdentry))
1151 goto out_dput;
1153 } else {
1154 if (!d_is_negative(newdentry) &&
1155 (!new_opaque || !ovl_is_whiteout(newdentry)))
1156 goto out_dput;
1159 if (olddentry == trap)
1160 goto out_dput;
1161 if (newdentry == trap)
1162 goto out_dput;
1164 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
1165 goto out_dput;
1167 err = 0;
1168 if (ovl_type_merge_or_lower(old))
1169 err = ovl_set_redirect(old, samedir);
1170 else if (is_dir && !old_opaque && ovl_type_merge(new->d_parent))
1171 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1172 if (err)
1173 goto out_dput;
1175 if (!overwrite && ovl_type_merge_or_lower(new))
1176 err = ovl_set_redirect(new, samedir);
1177 else if (!overwrite && new_is_dir && !new_opaque &&
1178 ovl_type_merge(old->d_parent))
1179 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1180 if (err)
1181 goto out_dput;
1183 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1184 new_upperdir->d_inode, newdentry, flags);
1185 if (err)
1186 goto out_dput;
1188 if (cleanup_whiteout)
1189 ovl_cleanup(old_upperdir->d_inode, newdentry);
1191 if (overwrite && d_inode(new)) {
1192 if (new_is_dir)
1193 clear_nlink(d_inode(new));
1194 else
1195 drop_nlink(d_inode(new));
1198 ovl_dir_modified(old->d_parent, ovl_type_origin(old) ||
1199 (!overwrite && ovl_type_origin(new)));
1200 ovl_dir_modified(new->d_parent, ovl_type_origin(old) ||
1201 (d_inode(new) && ovl_type_origin(new)));
1203 /* copy ctime: */
1204 ovl_copyattr(d_inode(olddentry), d_inode(old));
1205 if (d_inode(new) && ovl_dentry_upper(new))
1206 ovl_copyattr(d_inode(newdentry), d_inode(new));
1208 out_dput:
1209 dput(newdentry);
1210 out_dput_old:
1211 dput(olddentry);
1212 out_unlock:
1213 unlock_rename(new_upperdir, old_upperdir);
1214 out_revert_creds:
1215 revert_creds(old_cred);
1216 if (update_nlink)
1217 ovl_nlink_end(new);
1218 out_drop_write:
1219 ovl_drop_write(old);
1220 out:
1221 dput(opaquedir);
1222 ovl_cache_free(&list);
1223 return err;
1226 const struct inode_operations ovl_dir_inode_operations = {
1227 .lookup = ovl_lookup,
1228 .mkdir = ovl_mkdir,
1229 .symlink = ovl_symlink,
1230 .unlink = ovl_unlink,
1231 .rmdir = ovl_rmdir,
1232 .rename = ovl_rename,
1233 .link = ovl_link,
1234 .setattr = ovl_setattr,
1235 .create = ovl_create,
1236 .mknod = ovl_mknod,
1237 .permission = ovl_permission,
1238 .getattr = ovl_getattr,
1239 .listxattr = ovl_listxattr,
1240 .get_acl = ovl_get_acl,
1241 .update_time = ovl_update_time,