ovl: return dentry from ovl_create_real()
[linux/fpc-iii.git] / fs / overlayfs / dir.c
blob425ddb098c4a7b6ceaecaab112722d6077db8fa5
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 int ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
29 int err;
31 dget(wdentry);
32 if (d_is_dir(wdentry))
33 err = ovl_do_rmdir(wdir, wdentry);
34 else
35 err = ovl_do_unlink(wdir, wdentry);
36 dput(wdentry);
38 if (err) {
39 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
40 wdentry, err);
43 return err;
46 struct dentry *ovl_lookup_temp(struct dentry *workdir)
48 struct dentry *temp;
49 char name[20];
50 static atomic_t temp_id = ATOMIC_INIT(0);
52 /* counter is allowed to wrap, since temp dentries are ephemeral */
53 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
55 temp = lookup_one_len(name, workdir, strlen(name));
56 if (!IS_ERR(temp) && temp->d_inode) {
57 pr_err("overlayfs: workdir/%s already exists\n", name);
58 dput(temp);
59 temp = ERR_PTR(-EIO);
62 return temp;
65 /* caller holds i_mutex on workdir */
66 static struct dentry *ovl_whiteout(struct dentry *workdir)
68 int err;
69 struct dentry *whiteout;
70 struct inode *wdir = workdir->d_inode;
72 whiteout = ovl_lookup_temp(workdir);
73 if (IS_ERR(whiteout))
74 return whiteout;
76 err = ovl_do_whiteout(wdir, whiteout);
77 if (err) {
78 dput(whiteout);
79 whiteout = ERR_PTR(err);
82 return whiteout;
85 /* Caller must hold i_mutex on both workdir and dir */
86 int ovl_cleanup_and_whiteout(struct dentry *workdir, struct inode *dir,
87 struct dentry *dentry)
89 struct inode *wdir = workdir->d_inode;
90 struct dentry *whiteout;
91 int err;
92 int flags = 0;
94 whiteout = ovl_whiteout(workdir);
95 err = PTR_ERR(whiteout);
96 if (IS_ERR(whiteout))
97 return err;
99 if (d_is_dir(dentry))
100 flags = RENAME_EXCHANGE;
102 err = ovl_do_rename(wdir, whiteout, dir, dentry, flags);
103 if (err)
104 goto kill_whiteout;
105 if (flags)
106 ovl_cleanup(wdir, dentry);
108 out:
109 dput(whiteout);
110 return err;
112 kill_whiteout:
113 ovl_cleanup(wdir, whiteout);
114 goto out;
117 struct dentry *ovl_create_real(struct inode *dir, struct dentry *newdentry,
118 struct ovl_cattr *attr)
120 int err;
122 if (IS_ERR(newdentry))
123 return newdentry;
125 err = -ESTALE;
126 if (newdentry->d_inode)
127 goto out;
129 if (attr->hardlink) {
130 err = ovl_do_link(attr->hardlink, dir, newdentry);
131 } else {
132 switch (attr->mode & S_IFMT) {
133 case S_IFREG:
134 err = ovl_do_create(dir, newdentry, attr->mode);
135 break;
137 case S_IFDIR:
138 err = ovl_do_mkdir(dir, newdentry, attr->mode);
139 break;
141 case S_IFCHR:
142 case S_IFBLK:
143 case S_IFIFO:
144 case S_IFSOCK:
145 err = ovl_do_mknod(dir, newdentry, attr->mode,
146 attr->rdev);
147 break;
149 case S_IFLNK:
150 err = ovl_do_symlink(dir, newdentry, attr->link);
151 break;
153 default:
154 err = -EPERM;
157 if (!err && WARN_ON(!newdentry->d_inode)) {
159 * Not quite sure if non-instantiated dentry is legal or not.
160 * VFS doesn't seem to care so check and warn here.
162 err = -ENOENT;
164 out:
165 if (err) {
166 dput(newdentry);
167 return ERR_PTR(err);
169 return newdentry;
172 static int ovl_set_opaque_xerr(struct dentry *dentry, struct dentry *upper,
173 int xerr)
175 int err;
177 err = ovl_check_setxattr(dentry, upper, OVL_XATTR_OPAQUE, "y", 1, xerr);
178 if (!err)
179 ovl_dentry_set_opaque(dentry);
181 return err;
184 static int ovl_set_opaque(struct dentry *dentry, struct dentry *upperdentry)
187 * Fail with -EIO when trying to create opaque dir and upper doesn't
188 * support xattrs. ovl_rename() calls ovl_set_opaque_xerr(-EXDEV) to
189 * return a specific error for noxattr case.
191 return ovl_set_opaque_xerr(dentry, upperdentry, -EIO);
194 /* Common operations required to be done after creation of file on upper */
195 static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
196 struct dentry *newdentry, bool hardlink)
198 ovl_dentry_version_inc(dentry->d_parent, false);
199 ovl_dentry_set_upper_alias(dentry);
200 if (!hardlink) {
201 ovl_inode_update(inode, newdentry);
202 ovl_copyattr(newdentry->d_inode, inode);
203 } else {
204 WARN_ON(ovl_inode_real(inode) != d_inode(newdentry));
205 dput(newdentry);
206 inc_nlink(inode);
208 d_instantiate(dentry, inode);
209 /* Force lookup of new upper hardlink to find its lower */
210 if (hardlink)
211 d_drop(dentry);
214 static bool ovl_type_merge(struct dentry *dentry)
216 return OVL_TYPE_MERGE(ovl_path_type(dentry));
219 static bool ovl_type_origin(struct dentry *dentry)
221 return OVL_TYPE_ORIGIN(ovl_path_type(dentry));
224 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
225 struct ovl_cattr *attr)
227 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
228 struct inode *udir = upperdir->d_inode;
229 struct dentry *newdentry;
230 int err;
232 if (!attr->hardlink && !IS_POSIXACL(udir))
233 attr->mode &= ~current_umask();
235 inode_lock_nested(udir, I_MUTEX_PARENT);
236 newdentry = ovl_create_real(udir,
237 lookup_one_len(dentry->d_name.name,
238 upperdir,
239 dentry->d_name.len),
240 attr);
241 err = PTR_ERR(newdentry);
242 if (IS_ERR(newdentry))
243 goto out_unlock;
245 if (ovl_type_merge(dentry->d_parent) && d_is_dir(newdentry)) {
246 /* Setting opaque here is just an optimization, allow to fail */
247 ovl_set_opaque(dentry, newdentry);
250 ovl_instantiate(dentry, inode, newdentry, !!attr->hardlink);
251 err = 0;
252 out_unlock:
253 inode_unlock(udir);
254 return err;
257 static struct dentry *ovl_clear_empty(struct dentry *dentry,
258 struct list_head *list)
260 struct dentry *workdir = ovl_workdir(dentry);
261 struct inode *wdir = workdir->d_inode;
262 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
263 struct inode *udir = upperdir->d_inode;
264 struct path upperpath;
265 struct dentry *upper;
266 struct dentry *opaquedir;
267 struct kstat stat;
268 int err;
270 if (WARN_ON(!workdir))
271 return ERR_PTR(-EROFS);
273 err = ovl_lock_rename_workdir(workdir, upperdir);
274 if (err)
275 goto out;
277 ovl_path_upper(dentry, &upperpath);
278 err = vfs_getattr(&upperpath, &stat,
279 STATX_BASIC_STATS, AT_STATX_SYNC_AS_STAT);
280 if (err)
281 goto out_unlock;
283 err = -ESTALE;
284 if (!S_ISDIR(stat.mode))
285 goto out_unlock;
286 upper = upperpath.dentry;
287 if (upper->d_parent->d_inode != udir)
288 goto out_unlock;
290 opaquedir = ovl_create_real(wdir, ovl_lookup_temp(workdir),
291 OVL_CATTR(stat.mode));
292 err = PTR_ERR(opaquedir);
293 if (IS_ERR(opaquedir))
294 goto out_unlock;
296 err = ovl_copy_xattr(upper, opaquedir);
297 if (err)
298 goto out_cleanup;
300 err = ovl_set_opaque(dentry, opaquedir);
301 if (err)
302 goto out_cleanup;
304 inode_lock(opaquedir->d_inode);
305 err = ovl_set_attr(opaquedir, &stat);
306 inode_unlock(opaquedir->d_inode);
307 if (err)
308 goto out_cleanup;
310 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
311 if (err)
312 goto out_cleanup;
314 ovl_cleanup_whiteouts(upper, list);
315 ovl_cleanup(wdir, upper);
316 unlock_rename(workdir, upperdir);
318 /* dentry's upper doesn't match now, get rid of it */
319 d_drop(dentry);
321 return opaquedir;
323 out_cleanup:
324 ovl_cleanup(wdir, opaquedir);
325 dput(opaquedir);
326 out_unlock:
327 unlock_rename(workdir, upperdir);
328 out:
329 return ERR_PTR(err);
332 static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
333 const struct posix_acl *acl)
335 void *buffer;
336 size_t size;
337 int err;
339 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
340 return 0;
342 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
343 buffer = kmalloc(size, GFP_KERNEL);
344 if (!buffer)
345 return -ENOMEM;
347 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
348 err = size;
349 if (err < 0)
350 goto out_free;
352 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
353 out_free:
354 kfree(buffer);
355 return err;
358 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
359 struct ovl_cattr *cattr)
361 struct dentry *workdir = ovl_workdir(dentry);
362 struct inode *wdir = workdir->d_inode;
363 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
364 struct inode *udir = upperdir->d_inode;
365 struct dentry *upper;
366 struct dentry *newdentry;
367 int err;
368 struct posix_acl *acl, *default_acl;
369 bool hardlink = !!cattr->hardlink;
371 if (WARN_ON(!workdir))
372 return -EROFS;
374 if (!hardlink) {
375 err = posix_acl_create(dentry->d_parent->d_inode,
376 &cattr->mode, &default_acl, &acl);
377 if (err)
378 return err;
381 err = ovl_lock_rename_workdir(workdir, upperdir);
382 if (err)
383 goto out;
385 upper = lookup_one_len(dentry->d_name.name, upperdir,
386 dentry->d_name.len);
387 err = PTR_ERR(upper);
388 if (IS_ERR(upper))
389 goto out_unlock;
391 newdentry = ovl_create_real(wdir, ovl_lookup_temp(workdir), cattr);
392 err = PTR_ERR(newdentry);
393 if (IS_ERR(newdentry))
394 goto out_dput;
397 * mode could have been mutilated due to umask (e.g. sgid directory)
399 if (!hardlink &&
400 !S_ISLNK(cattr->mode) &&
401 newdentry->d_inode->i_mode != cattr->mode) {
402 struct iattr attr = {
403 .ia_valid = ATTR_MODE,
404 .ia_mode = cattr->mode,
406 inode_lock(newdentry->d_inode);
407 err = notify_change(newdentry, &attr, NULL);
408 inode_unlock(newdentry->d_inode);
409 if (err)
410 goto out_cleanup;
412 if (!hardlink) {
413 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
414 acl);
415 if (err)
416 goto out_cleanup;
418 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
419 default_acl);
420 if (err)
421 goto out_cleanup;
424 if (!hardlink && S_ISDIR(cattr->mode)) {
425 err = ovl_set_opaque(dentry, newdentry);
426 if (err)
427 goto out_cleanup;
429 err = ovl_do_rename(wdir, newdentry, udir, upper,
430 RENAME_EXCHANGE);
431 if (err)
432 goto out_cleanup;
434 ovl_cleanup(wdir, upper);
435 } else {
436 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
437 if (err)
438 goto out_cleanup;
440 ovl_instantiate(dentry, inode, newdentry, hardlink);
441 err = 0;
442 out_dput:
443 dput(upper);
444 out_unlock:
445 unlock_rename(workdir, upperdir);
446 out:
447 if (!hardlink) {
448 posix_acl_release(acl);
449 posix_acl_release(default_acl);
451 return err;
453 out_cleanup:
454 ovl_cleanup(wdir, newdentry);
455 dput(newdentry);
456 goto out_dput;
459 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
460 struct ovl_cattr *attr, bool origin)
462 int err;
463 const struct cred *old_cred;
464 struct cred *override_cred;
465 struct dentry *parent = dentry->d_parent;
467 err = ovl_copy_up(parent);
468 if (err)
469 return err;
471 old_cred = ovl_override_creds(dentry->d_sb);
474 * When linking a file with copy up origin into a new parent, mark the
475 * new parent dir "impure".
477 if (origin) {
478 err = ovl_set_impure(parent, ovl_dentry_upper(parent));
479 if (err)
480 goto out_revert_creds;
483 err = -ENOMEM;
484 override_cred = prepare_creds();
485 if (override_cred) {
486 override_cred->fsuid = inode->i_uid;
487 override_cred->fsgid = inode->i_gid;
488 if (!attr->hardlink) {
489 err = security_dentry_create_files_as(dentry,
490 attr->mode, &dentry->d_name, old_cred,
491 override_cred);
492 if (err) {
493 put_cred(override_cred);
494 goto out_revert_creds;
497 put_cred(override_creds(override_cred));
498 put_cred(override_cred);
500 if (!ovl_dentry_is_whiteout(dentry))
501 err = ovl_create_upper(dentry, inode, attr);
502 else
503 err = ovl_create_over_whiteout(dentry, inode, attr);
505 out_revert_creds:
506 revert_creds(old_cred);
507 return err;
510 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
511 const char *link)
513 int err;
514 struct inode *inode;
515 struct ovl_cattr attr = {
516 .rdev = rdev,
517 .link = link,
520 err = ovl_want_write(dentry);
521 if (err)
522 goto out;
524 err = -ENOMEM;
525 inode = ovl_new_inode(dentry->d_sb, mode, rdev);
526 if (!inode)
527 goto out_drop_write;
529 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
530 attr.mode = inode->i_mode;
532 err = ovl_create_or_link(dentry, inode, &attr, false);
533 if (err)
534 iput(inode);
536 out_drop_write:
537 ovl_drop_write(dentry);
538 out:
539 return err;
542 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
543 bool excl)
545 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
548 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
550 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
553 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
554 dev_t rdev)
556 /* Don't allow creation of "whiteout" on overlay */
557 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
558 return -EPERM;
560 return ovl_create_object(dentry, mode, rdev, NULL);
563 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
564 const char *link)
566 return ovl_create_object(dentry, S_IFLNK, 0, link);
569 static int ovl_link(struct dentry *old, struct inode *newdir,
570 struct dentry *new)
572 int err;
573 bool locked = false;
574 struct inode *inode;
576 err = ovl_want_write(old);
577 if (err)
578 goto out;
580 err = ovl_copy_up(old);
581 if (err)
582 goto out_drop_write;
584 err = ovl_nlink_start(old, &locked);
585 if (err)
586 goto out_drop_write;
588 inode = d_inode(old);
589 ihold(inode);
591 err = ovl_create_or_link(new, inode,
592 &(struct ovl_cattr) {.hardlink = ovl_dentry_upper(old)},
593 ovl_type_origin(old));
594 if (err)
595 iput(inode);
597 ovl_nlink_end(old, locked);
598 out_drop_write:
599 ovl_drop_write(old);
600 out:
601 return err;
604 static bool ovl_matches_upper(struct dentry *dentry, struct dentry *upper)
606 return d_inode(ovl_dentry_upper(dentry)) == d_inode(upper);
609 static int ovl_remove_and_whiteout(struct dentry *dentry,
610 struct list_head *list)
612 struct dentry *workdir = ovl_workdir(dentry);
613 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
614 struct dentry *upper;
615 struct dentry *opaquedir = NULL;
616 int err;
618 if (WARN_ON(!workdir))
619 return -EROFS;
621 if (!list_empty(list)) {
622 opaquedir = ovl_clear_empty(dentry, list);
623 err = PTR_ERR(opaquedir);
624 if (IS_ERR(opaquedir))
625 goto out;
628 err = ovl_lock_rename_workdir(workdir, upperdir);
629 if (err)
630 goto out_dput;
632 upper = lookup_one_len(dentry->d_name.name, upperdir,
633 dentry->d_name.len);
634 err = PTR_ERR(upper);
635 if (IS_ERR(upper))
636 goto out_unlock;
638 err = -ESTALE;
639 if ((opaquedir && upper != opaquedir) ||
640 (!opaquedir && ovl_dentry_upper(dentry) &&
641 !ovl_matches_upper(dentry, upper))) {
642 goto out_dput_upper;
645 err = ovl_cleanup_and_whiteout(workdir, d_inode(upperdir), upper);
646 if (err)
647 goto out_d_drop;
649 ovl_dentry_version_inc(dentry->d_parent, true);
650 out_d_drop:
651 d_drop(dentry);
652 out_dput_upper:
653 dput(upper);
654 out_unlock:
655 unlock_rename(workdir, upperdir);
656 out_dput:
657 dput(opaquedir);
658 out:
659 return err;
662 static int ovl_remove_upper(struct dentry *dentry, bool is_dir,
663 struct list_head *list)
665 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
666 struct inode *dir = upperdir->d_inode;
667 struct dentry *upper;
668 struct dentry *opaquedir = NULL;
669 int err;
671 if (!list_empty(list)) {
672 opaquedir = ovl_clear_empty(dentry, list);
673 err = PTR_ERR(opaquedir);
674 if (IS_ERR(opaquedir))
675 goto out;
678 inode_lock_nested(dir, I_MUTEX_PARENT);
679 upper = lookup_one_len(dentry->d_name.name, upperdir,
680 dentry->d_name.len);
681 err = PTR_ERR(upper);
682 if (IS_ERR(upper))
683 goto out_unlock;
685 err = -ESTALE;
686 if ((opaquedir && upper != opaquedir) ||
687 (!opaquedir && !ovl_matches_upper(dentry, upper)))
688 goto out_dput_upper;
690 if (is_dir)
691 err = vfs_rmdir(dir, upper);
692 else
693 err = vfs_unlink(dir, upper, NULL);
694 ovl_dentry_version_inc(dentry->d_parent, ovl_type_origin(dentry));
697 * Keeping this dentry hashed would mean having to release
698 * upperpath/lowerpath, which could only be done if we are the
699 * sole user of this dentry. Too tricky... Just unhash for
700 * now.
702 if (!err)
703 d_drop(dentry);
704 out_dput_upper:
705 dput(upper);
706 out_unlock:
707 inode_unlock(dir);
708 dput(opaquedir);
709 out:
710 return err;
713 static bool ovl_pure_upper(struct dentry *dentry)
715 return !ovl_dentry_lower(dentry) &&
716 !ovl_test_flag(OVL_WHITEOUTS, d_inode(dentry));
719 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
721 int err;
722 bool locked = false;
723 const struct cred *old_cred;
724 bool lower_positive = ovl_lower_positive(dentry);
725 LIST_HEAD(list);
727 /* No need to clean pure upper removed by vfs_rmdir() */
728 if (is_dir && (lower_positive || !ovl_pure_upper(dentry))) {
729 err = ovl_check_empty_dir(dentry, &list);
730 if (err)
731 goto out;
734 err = ovl_want_write(dentry);
735 if (err)
736 goto out;
738 err = ovl_copy_up(dentry->d_parent);
739 if (err)
740 goto out_drop_write;
742 err = ovl_nlink_start(dentry, &locked);
743 if (err)
744 goto out_drop_write;
746 old_cred = ovl_override_creds(dentry->d_sb);
747 if (!lower_positive)
748 err = ovl_remove_upper(dentry, is_dir, &list);
749 else
750 err = ovl_remove_and_whiteout(dentry, &list);
751 revert_creds(old_cred);
752 if (!err) {
753 if (is_dir)
754 clear_nlink(dentry->d_inode);
755 else
756 drop_nlink(dentry->d_inode);
758 ovl_nlink_end(dentry, locked);
759 out_drop_write:
760 ovl_drop_write(dentry);
761 out:
762 ovl_cache_free(&list);
763 return err;
766 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
768 return ovl_do_remove(dentry, false);
771 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
773 return ovl_do_remove(dentry, true);
776 static bool ovl_type_merge_or_lower(struct dentry *dentry)
778 enum ovl_path_type type = ovl_path_type(dentry);
780 return OVL_TYPE_MERGE(type) || !OVL_TYPE_UPPER(type);
783 static bool ovl_can_move(struct dentry *dentry)
785 return ovl_redirect_dir(dentry->d_sb) ||
786 !d_is_dir(dentry) || !ovl_type_merge_or_lower(dentry);
789 static char *ovl_get_redirect(struct dentry *dentry, bool samedir)
791 char *buf, *ret;
792 struct dentry *d, *tmp;
793 int buflen = ovl_redirect_max + 1;
795 if (samedir) {
796 ret = kstrndup(dentry->d_name.name, dentry->d_name.len,
797 GFP_KERNEL);
798 goto out;
801 buf = ret = kmalloc(buflen, GFP_KERNEL);
802 if (!buf)
803 goto out;
805 buflen--;
806 buf[buflen] = '\0';
807 for (d = dget(dentry); !IS_ROOT(d);) {
808 const char *name;
809 int thislen;
811 spin_lock(&d->d_lock);
812 name = ovl_dentry_get_redirect(d);
813 if (name) {
814 thislen = strlen(name);
815 } else {
816 name = d->d_name.name;
817 thislen = d->d_name.len;
820 /* If path is too long, fall back to userspace move */
821 if (thislen + (name[0] != '/') > buflen) {
822 ret = ERR_PTR(-EXDEV);
823 spin_unlock(&d->d_lock);
824 goto out_put;
827 buflen -= thislen;
828 memcpy(&buf[buflen], name, thislen);
829 tmp = dget_dlock(d->d_parent);
830 spin_unlock(&d->d_lock);
832 dput(d);
833 d = tmp;
835 /* Absolute redirect: finished */
836 if (buf[buflen] == '/')
837 break;
838 buflen--;
839 buf[buflen] = '/';
841 ret = kstrdup(&buf[buflen], GFP_KERNEL);
842 out_put:
843 dput(d);
844 kfree(buf);
845 out:
846 return ret ? ret : ERR_PTR(-ENOMEM);
849 static int ovl_set_redirect(struct dentry *dentry, bool samedir)
851 int err;
852 const char *redirect = ovl_dentry_get_redirect(dentry);
854 if (redirect && (samedir || redirect[0] == '/'))
855 return 0;
857 redirect = ovl_get_redirect(dentry, samedir);
858 if (IS_ERR(redirect))
859 return PTR_ERR(redirect);
861 err = ovl_check_setxattr(dentry, ovl_dentry_upper(dentry),
862 OVL_XATTR_REDIRECT,
863 redirect, strlen(redirect), -EXDEV);
864 if (!err) {
865 spin_lock(&dentry->d_lock);
866 ovl_dentry_set_redirect(dentry, redirect);
867 spin_unlock(&dentry->d_lock);
868 } else {
869 kfree(redirect);
870 pr_warn_ratelimited("overlayfs: failed to set redirect (%i)\n",
871 err);
872 /* Fall back to userspace copy-up */
873 err = -EXDEV;
875 return err;
878 static int ovl_rename(struct inode *olddir, struct dentry *old,
879 struct inode *newdir, struct dentry *new,
880 unsigned int flags)
882 int err;
883 bool locked = false;
884 struct dentry *old_upperdir;
885 struct dentry *new_upperdir;
886 struct dentry *olddentry;
887 struct dentry *newdentry;
888 struct dentry *trap;
889 bool old_opaque;
890 bool new_opaque;
891 bool cleanup_whiteout = false;
892 bool overwrite = !(flags & RENAME_EXCHANGE);
893 bool is_dir = d_is_dir(old);
894 bool new_is_dir = d_is_dir(new);
895 bool samedir = olddir == newdir;
896 struct dentry *opaquedir = NULL;
897 const struct cred *old_cred = NULL;
898 LIST_HEAD(list);
900 err = -EINVAL;
901 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
902 goto out;
904 flags &= ~RENAME_NOREPLACE;
906 /* Don't copy up directory trees */
907 err = -EXDEV;
908 if (!ovl_can_move(old))
909 goto out;
910 if (!overwrite && !ovl_can_move(new))
911 goto out;
913 if (overwrite && new_is_dir && !ovl_pure_upper(new)) {
914 err = ovl_check_empty_dir(new, &list);
915 if (err)
916 goto out;
919 if (overwrite) {
920 if (ovl_lower_positive(old)) {
921 if (!ovl_dentry_is_whiteout(new)) {
922 /* Whiteout source */
923 flags |= RENAME_WHITEOUT;
924 } else {
925 /* Switch whiteouts */
926 flags |= RENAME_EXCHANGE;
928 } else if (is_dir && ovl_dentry_is_whiteout(new)) {
929 flags |= RENAME_EXCHANGE;
930 cleanup_whiteout = true;
934 err = ovl_want_write(old);
935 if (err)
936 goto out;
938 err = ovl_copy_up(old);
939 if (err)
940 goto out_drop_write;
942 err = ovl_copy_up(new->d_parent);
943 if (err)
944 goto out_drop_write;
945 if (!overwrite) {
946 err = ovl_copy_up(new);
947 if (err)
948 goto out_drop_write;
949 } else {
950 err = ovl_nlink_start(new, &locked);
951 if (err)
952 goto out_drop_write;
955 old_cred = ovl_override_creds(old->d_sb);
957 if (!list_empty(&list)) {
958 opaquedir = ovl_clear_empty(new, &list);
959 err = PTR_ERR(opaquedir);
960 if (IS_ERR(opaquedir)) {
961 opaquedir = NULL;
962 goto out_revert_creds;
966 old_upperdir = ovl_dentry_upper(old->d_parent);
967 new_upperdir = ovl_dentry_upper(new->d_parent);
969 if (!samedir) {
971 * When moving a merge dir or non-dir with copy up origin into
972 * a new parent, we are marking the new parent dir "impure".
973 * When ovl_iterate() iterates an "impure" upper dir, it will
974 * lookup the origin inodes of the entries to fill d_ino.
976 if (ovl_type_origin(old)) {
977 err = ovl_set_impure(new->d_parent, new_upperdir);
978 if (err)
979 goto out_revert_creds;
981 if (!overwrite && ovl_type_origin(new)) {
982 err = ovl_set_impure(old->d_parent, old_upperdir);
983 if (err)
984 goto out_revert_creds;
988 trap = lock_rename(new_upperdir, old_upperdir);
990 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
991 old->d_name.len);
992 err = PTR_ERR(olddentry);
993 if (IS_ERR(olddentry))
994 goto out_unlock;
996 err = -ESTALE;
997 if (!ovl_matches_upper(old, olddentry))
998 goto out_dput_old;
1000 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
1001 new->d_name.len);
1002 err = PTR_ERR(newdentry);
1003 if (IS_ERR(newdentry))
1004 goto out_dput_old;
1006 old_opaque = ovl_dentry_is_opaque(old);
1007 new_opaque = ovl_dentry_is_opaque(new);
1009 err = -ESTALE;
1010 if (d_inode(new) && ovl_dentry_upper(new)) {
1011 if (opaquedir) {
1012 if (newdentry != opaquedir)
1013 goto out_dput;
1014 } else {
1015 if (!ovl_matches_upper(new, newdentry))
1016 goto out_dput;
1018 } else {
1019 if (!d_is_negative(newdentry) &&
1020 (!new_opaque || !ovl_is_whiteout(newdentry)))
1021 goto out_dput;
1024 if (olddentry == trap)
1025 goto out_dput;
1026 if (newdentry == trap)
1027 goto out_dput;
1029 if (WARN_ON(olddentry->d_inode == newdentry->d_inode))
1030 goto out_dput;
1032 err = 0;
1033 if (is_dir) {
1034 if (ovl_type_merge_or_lower(old))
1035 err = ovl_set_redirect(old, samedir);
1036 else if (!old_opaque && ovl_type_merge(new->d_parent))
1037 err = ovl_set_opaque_xerr(old, olddentry, -EXDEV);
1038 if (err)
1039 goto out_dput;
1041 if (!overwrite && new_is_dir) {
1042 if (ovl_type_merge_or_lower(new))
1043 err = ovl_set_redirect(new, samedir);
1044 else if (!new_opaque && ovl_type_merge(old->d_parent))
1045 err = ovl_set_opaque_xerr(new, newdentry, -EXDEV);
1046 if (err)
1047 goto out_dput;
1050 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
1051 new_upperdir->d_inode, newdentry, flags);
1052 if (err)
1053 goto out_dput;
1055 if (cleanup_whiteout)
1056 ovl_cleanup(old_upperdir->d_inode, newdentry);
1058 if (overwrite && d_inode(new)) {
1059 if (new_is_dir)
1060 clear_nlink(d_inode(new));
1061 else
1062 drop_nlink(d_inode(new));
1065 ovl_dentry_version_inc(old->d_parent, ovl_type_origin(old) ||
1066 (!overwrite && ovl_type_origin(new)));
1067 ovl_dentry_version_inc(new->d_parent, ovl_type_origin(old) ||
1068 (d_inode(new) && ovl_type_origin(new)));
1070 out_dput:
1071 dput(newdentry);
1072 out_dput_old:
1073 dput(olddentry);
1074 out_unlock:
1075 unlock_rename(new_upperdir, old_upperdir);
1076 out_revert_creds:
1077 revert_creds(old_cred);
1078 ovl_nlink_end(new, locked);
1079 out_drop_write:
1080 ovl_drop_write(old);
1081 out:
1082 dput(opaquedir);
1083 ovl_cache_free(&list);
1084 return err;
1087 const struct inode_operations ovl_dir_inode_operations = {
1088 .lookup = ovl_lookup,
1089 .mkdir = ovl_mkdir,
1090 .symlink = ovl_symlink,
1091 .unlink = ovl_unlink,
1092 .rmdir = ovl_rmdir,
1093 .rename = ovl_rename,
1094 .link = ovl_link,
1095 .setattr = ovl_setattr,
1096 .create = ovl_create,
1097 .mknod = ovl_mknod,
1098 .permission = ovl_permission,
1099 .getattr = ovl_getattr,
1100 .listxattr = ovl_listxattr,
1101 .get_acl = ovl_get_acl,
1102 .update_time = ovl_update_time,