HID: hiddev: Fix slab-out-of-bounds write in hiddev_ioctl_usage()
[linux/fpc-iii.git] / fs / overlayfs / dir.c
blobeedacae889b95475a863d4691eded586702c7a43
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/atomic.h>
16 #include "overlayfs.h"
18 void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
20 int err;
22 dget(wdentry);
23 if (d_is_dir(wdentry))
24 err = ovl_do_rmdir(wdir, wdentry);
25 else
26 err = ovl_do_unlink(wdir, wdentry);
27 dput(wdentry);
29 if (err) {
30 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
31 wdentry, err);
35 struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
37 struct dentry *temp;
38 char name[20];
39 static atomic_t temp_id = ATOMIC_INIT(0);
41 /* counter is allowed to wrap, since temp dentries are ephemeral */
42 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
44 temp = lookup_one_len(name, workdir, strlen(name));
45 if (!IS_ERR(temp) && temp->d_inode) {
46 pr_err("overlayfs: workdir/%s already exists\n", name);
47 dput(temp);
48 temp = ERR_PTR(-EIO);
51 return temp;
54 /* caller holds i_mutex on workdir */
55 static struct dentry *ovl_whiteout(struct dentry *workdir,
56 struct dentry *dentry)
58 int err;
59 struct dentry *whiteout;
60 struct inode *wdir = workdir->d_inode;
62 whiteout = ovl_lookup_temp(workdir, dentry);
63 if (IS_ERR(whiteout))
64 return whiteout;
66 err = ovl_do_whiteout(wdir, whiteout);
67 if (err) {
68 dput(whiteout);
69 whiteout = ERR_PTR(err);
72 return whiteout;
75 int ovl_create_real(struct inode *dir, struct dentry *newdentry,
76 struct kstat *stat, const char *link,
77 struct dentry *hardlink, bool debug)
79 int err;
81 if (newdentry->d_inode)
82 return -ESTALE;
84 if (hardlink) {
85 err = ovl_do_link(hardlink, dir, newdentry, debug);
86 } else {
87 switch (stat->mode & S_IFMT) {
88 case S_IFREG:
89 err = ovl_do_create(dir, newdentry, stat->mode, debug);
90 break;
92 case S_IFDIR:
93 err = ovl_do_mkdir(dir, newdentry, stat->mode, debug);
94 break;
96 case S_IFCHR:
97 case S_IFBLK:
98 case S_IFIFO:
99 case S_IFSOCK:
100 err = ovl_do_mknod(dir, newdentry,
101 stat->mode, stat->rdev, debug);
102 break;
104 case S_IFLNK:
105 err = ovl_do_symlink(dir, newdentry, link, debug);
106 break;
108 default:
109 err = -EPERM;
112 if (!err && WARN_ON(!newdentry->d_inode)) {
114 * Not quite sure if non-instantiated dentry is legal or not.
115 * VFS doesn't seem to care so check and warn here.
117 err = -ENOENT;
119 return err;
122 static int ovl_set_opaque(struct dentry *upperdentry)
124 return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
127 static void ovl_remove_opaque(struct dentry *upperdentry)
129 int err;
131 err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE);
132 if (err) {
133 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n",
134 upperdentry->d_name.name, err);
138 static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
139 struct kstat *stat)
141 int err;
142 enum ovl_path_type type;
143 struct path realpath;
145 type = ovl_path_real(dentry, &realpath);
146 err = vfs_getattr(&realpath, stat);
147 if (err)
148 return err;
150 stat->dev = dentry->d_sb->s_dev;
151 stat->ino = dentry->d_inode->i_ino;
154 * It's probably not worth it to count subdirs to get the
155 * correct link count. nlink=1 seems to pacify 'find' and
156 * other utilities.
158 if (OVL_TYPE_MERGE(type))
159 stat->nlink = 1;
161 return 0;
164 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
165 struct kstat *stat, const char *link,
166 struct dentry *hardlink)
168 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
169 struct inode *udir = upperdir->d_inode;
170 struct dentry *newdentry;
171 int err;
173 mutex_lock_nested(&udir->i_mutex, I_MUTEX_PARENT);
174 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
175 dentry->d_name.len);
176 err = PTR_ERR(newdentry);
177 if (IS_ERR(newdentry))
178 goto out_unlock;
179 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
180 if (err)
181 goto out_dput;
183 ovl_dentry_version_inc(dentry->d_parent);
184 ovl_dentry_update(dentry, newdentry);
185 ovl_copyattr(newdentry->d_inode, inode);
186 d_instantiate(dentry, inode);
187 newdentry = NULL;
188 out_dput:
189 dput(newdentry);
190 out_unlock:
191 mutex_unlock(&udir->i_mutex);
192 return err;
195 static int ovl_lock_rename_workdir(struct dentry *workdir,
196 struct dentry *upperdir)
198 /* Workdir should not be the same as upperdir */
199 if (workdir == upperdir)
200 goto err;
202 /* Workdir should not be subdir of upperdir and vice versa */
203 if (lock_rename(workdir, upperdir) != NULL)
204 goto err_unlock;
206 return 0;
208 err_unlock:
209 unlock_rename(workdir, upperdir);
210 err:
211 pr_err("overlayfs: failed to lock workdir+upperdir\n");
212 return -EIO;
215 static struct dentry *ovl_clear_empty(struct dentry *dentry,
216 struct list_head *list)
218 struct dentry *workdir = ovl_workdir(dentry);
219 struct inode *wdir = workdir->d_inode;
220 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
221 struct inode *udir = upperdir->d_inode;
222 struct path upperpath;
223 struct dentry *upper;
224 struct dentry *opaquedir;
225 struct kstat stat;
226 int err;
228 if (WARN_ON(!workdir))
229 return ERR_PTR(-EROFS);
231 err = ovl_lock_rename_workdir(workdir, upperdir);
232 if (err)
233 goto out;
235 ovl_path_upper(dentry, &upperpath);
236 err = vfs_getattr(&upperpath, &stat);
237 if (err)
238 goto out_unlock;
240 err = -ESTALE;
241 if (!S_ISDIR(stat.mode))
242 goto out_unlock;
243 upper = upperpath.dentry;
244 if (upper->d_parent->d_inode != udir)
245 goto out_unlock;
247 opaquedir = ovl_lookup_temp(workdir, dentry);
248 err = PTR_ERR(opaquedir);
249 if (IS_ERR(opaquedir))
250 goto out_unlock;
252 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
253 if (err)
254 goto out_dput;
256 err = ovl_copy_xattr(upper, opaquedir);
257 if (err)
258 goto out_cleanup;
260 err = ovl_set_opaque(opaquedir);
261 if (err)
262 goto out_cleanup;
264 mutex_lock(&opaquedir->d_inode->i_mutex);
265 err = ovl_set_attr(opaquedir, &stat);
266 mutex_unlock(&opaquedir->d_inode->i_mutex);
267 if (err)
268 goto out_cleanup;
270 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
271 if (err)
272 goto out_cleanup;
274 ovl_cleanup_whiteouts(upper, list);
275 ovl_cleanup(wdir, upper);
276 unlock_rename(workdir, upperdir);
278 /* dentry's upper doesn't match now, get rid of it */
279 d_drop(dentry);
281 return opaquedir;
283 out_cleanup:
284 ovl_cleanup(wdir, opaquedir);
285 out_dput:
286 dput(opaquedir);
287 out_unlock:
288 unlock_rename(workdir, upperdir);
289 out:
290 return ERR_PTR(err);
293 static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
295 int err;
296 struct dentry *ret = NULL;
297 LIST_HEAD(list);
299 err = ovl_check_empty_dir(dentry, &list);
300 if (err)
301 ret = ERR_PTR(err);
302 else {
304 * If no upperdentry then skip clearing whiteouts.
306 * Can race with copy-up, since we don't hold the upperdir
307 * mutex. Doesn't matter, since copy-up can't create a
308 * non-empty directory from an empty one.
310 if (ovl_dentry_upper(dentry))
311 ret = ovl_clear_empty(dentry, &list);
314 ovl_cache_free(&list);
316 return ret;
319 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
320 struct kstat *stat, const char *link,
321 struct dentry *hardlink)
323 struct dentry *workdir = ovl_workdir(dentry);
324 struct inode *wdir = workdir->d_inode;
325 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
326 struct inode *udir = upperdir->d_inode;
327 struct dentry *upper;
328 struct dentry *newdentry;
329 int err;
331 if (WARN_ON(!workdir))
332 return -EROFS;
334 err = ovl_lock_rename_workdir(workdir, upperdir);
335 if (err)
336 goto out;
338 newdentry = ovl_lookup_temp(workdir, dentry);
339 err = PTR_ERR(newdentry);
340 if (IS_ERR(newdentry))
341 goto out_unlock;
343 upper = lookup_one_len(dentry->d_name.name, upperdir,
344 dentry->d_name.len);
345 err = PTR_ERR(upper);
346 if (IS_ERR(upper))
347 goto out_dput;
349 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
350 if (err)
351 goto out_dput2;
353 if (S_ISDIR(stat->mode)) {
354 err = ovl_set_opaque(newdentry);
355 if (err)
356 goto out_cleanup;
358 err = ovl_do_rename(wdir, newdentry, udir, upper,
359 RENAME_EXCHANGE);
360 if (err)
361 goto out_cleanup;
363 ovl_cleanup(wdir, upper);
364 } else {
365 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
366 if (err)
367 goto out_cleanup;
369 ovl_dentry_version_inc(dentry->d_parent);
370 ovl_dentry_update(dentry, newdentry);
371 ovl_copyattr(newdentry->d_inode, inode);
372 d_instantiate(dentry, inode);
373 newdentry = NULL;
374 out_dput2:
375 dput(upper);
376 out_dput:
377 dput(newdentry);
378 out_unlock:
379 unlock_rename(workdir, upperdir);
380 out:
381 return err;
383 out_cleanup:
384 ovl_cleanup(wdir, newdentry);
385 goto out_dput2;
388 static int ovl_create_or_link(struct dentry *dentry, int mode, dev_t rdev,
389 const char *link, struct dentry *hardlink)
391 int err;
392 struct inode *inode;
393 struct kstat stat = {
394 .mode = mode,
395 .rdev = rdev,
398 err = -ENOMEM;
399 inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
400 if (!inode)
401 goto out;
403 err = ovl_copy_up(dentry->d_parent);
404 if (err)
405 goto out_iput;
407 if (!ovl_dentry_is_opaque(dentry)) {
408 err = ovl_create_upper(dentry, inode, &stat, link, hardlink);
409 } else {
410 const struct cred *old_cred;
411 struct cred *override_cred;
413 old_cred = ovl_override_creds(dentry->d_sb);
415 err = -ENOMEM;
416 override_cred = prepare_creds();
417 if (override_cred) {
418 override_cred->fsuid = old_cred->fsuid;
419 override_cred->fsgid = old_cred->fsgid;
420 put_cred(override_creds(override_cred));
421 put_cred(override_cred);
423 err = ovl_create_over_whiteout(dentry, inode, &stat,
424 link, hardlink);
426 revert_creds(old_cred);
429 if (!err)
430 inode = NULL;
431 out_iput:
432 iput(inode);
433 out:
434 return err;
437 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
438 const char *link)
440 int err;
442 err = ovl_want_write(dentry);
443 if (!err) {
444 err = ovl_create_or_link(dentry, mode, rdev, link, NULL);
445 ovl_drop_write(dentry);
448 return err;
451 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
452 bool excl)
454 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
457 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
459 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
462 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
463 dev_t rdev)
465 /* Don't allow creation of "whiteout" on overlay */
466 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
467 return -EPERM;
469 return ovl_create_object(dentry, mode, rdev, NULL);
472 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
473 const char *link)
475 return ovl_create_object(dentry, S_IFLNK, 0, link);
478 static int ovl_link(struct dentry *old, struct inode *newdir,
479 struct dentry *new)
481 int err;
482 struct dentry *upper;
484 err = ovl_want_write(old);
485 if (err)
486 goto out;
488 err = ovl_copy_up(old);
489 if (err)
490 goto out_drop_write;
492 upper = ovl_dentry_upper(old);
493 err = ovl_create_or_link(new, upper->d_inode->i_mode, 0, NULL, upper);
495 out_drop_write:
496 ovl_drop_write(old);
497 out:
498 return err;
501 static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
503 struct dentry *workdir = ovl_workdir(dentry);
504 struct inode *wdir = workdir->d_inode;
505 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
506 struct inode *udir = upperdir->d_inode;
507 struct dentry *whiteout;
508 struct dentry *upper;
509 struct dentry *opaquedir = NULL;
510 int err;
511 int flags = 0;
513 if (WARN_ON(!workdir))
514 return -EROFS;
516 if (is_dir) {
517 if (OVL_TYPE_MERGE_OR_LOWER(ovl_path_type(dentry))) {
518 opaquedir = ovl_check_empty_and_clear(dentry);
519 err = PTR_ERR(opaquedir);
520 if (IS_ERR(opaquedir))
521 goto out;
522 } else {
523 LIST_HEAD(list);
526 * When removing an empty opaque directory, then it
527 * makes no sense to replace it with an exact replica of
528 * itself. But emptiness still needs to be checked.
530 err = ovl_check_empty_dir(dentry, &list);
531 ovl_cache_free(&list);
532 if (err)
533 goto out;
537 err = ovl_lock_rename_workdir(workdir, upperdir);
538 if (err)
539 goto out_dput;
541 upper = lookup_one_len(dentry->d_name.name, upperdir,
542 dentry->d_name.len);
543 err = PTR_ERR(upper);
544 if (IS_ERR(upper))
545 goto out_unlock;
547 err = -ESTALE;
548 if ((opaquedir && upper != opaquedir) ||
549 (!opaquedir && ovl_dentry_upper(dentry) &&
550 upper != ovl_dentry_upper(dentry))) {
551 goto out_dput_upper;
554 whiteout = ovl_whiteout(workdir, dentry);
555 err = PTR_ERR(whiteout);
556 if (IS_ERR(whiteout))
557 goto out_dput_upper;
559 if (d_is_dir(upper))
560 flags = RENAME_EXCHANGE;
562 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
563 if (err)
564 goto kill_whiteout;
565 if (flags)
566 ovl_cleanup(wdir, upper);
568 ovl_dentry_version_inc(dentry->d_parent);
569 out_d_drop:
570 d_drop(dentry);
571 dput(whiteout);
572 out_dput_upper:
573 dput(upper);
574 out_unlock:
575 unlock_rename(workdir, upperdir);
576 out_dput:
577 dput(opaquedir);
578 out:
579 return err;
581 kill_whiteout:
582 ovl_cleanup(wdir, whiteout);
583 goto out_d_drop;
586 static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
588 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
589 struct inode *dir = upperdir->d_inode;
590 struct dentry *upper;
591 int err;
593 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
594 upper = lookup_one_len(dentry->d_name.name, upperdir,
595 dentry->d_name.len);
596 err = PTR_ERR(upper);
597 if (IS_ERR(upper))
598 goto out_unlock;
600 err = -ESTALE;
601 if (upper == ovl_dentry_upper(dentry)) {
602 if (is_dir)
603 err = vfs_rmdir(dir, upper);
604 else
605 err = vfs_unlink(dir, upper, NULL);
606 ovl_dentry_version_inc(dentry->d_parent);
608 dput(upper);
611 * Keeping this dentry hashed would mean having to release
612 * upperpath/lowerpath, which could only be done if we are the
613 * sole user of this dentry. Too tricky... Just unhash for
614 * now.
616 if (!err)
617 d_drop(dentry);
618 out_unlock:
619 mutex_unlock(&dir->i_mutex);
621 return err;
624 static inline int ovl_check_sticky(struct dentry *dentry)
626 struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode;
627 struct inode *inode = ovl_dentry_real(dentry)->d_inode;
629 if (check_sticky(dir, inode))
630 return -EPERM;
632 return 0;
635 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
637 enum ovl_path_type type;
638 int err;
640 err = ovl_check_sticky(dentry);
641 if (err)
642 goto out;
644 err = ovl_want_write(dentry);
645 if (err)
646 goto out;
648 err = ovl_copy_up(dentry->d_parent);
649 if (err)
650 goto out_drop_write;
652 type = ovl_path_type(dentry);
653 if (OVL_TYPE_PURE_UPPER(type)) {
654 err = ovl_remove_upper(dentry, is_dir);
655 } else {
656 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
658 err = ovl_remove_and_whiteout(dentry, is_dir);
660 revert_creds(old_cred);
662 out_drop_write:
663 ovl_drop_write(dentry);
664 out:
665 return err;
668 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
670 return ovl_do_remove(dentry, false);
673 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
675 return ovl_do_remove(dentry, true);
678 static int ovl_rename2(struct inode *olddir, struct dentry *old,
679 struct inode *newdir, struct dentry *new,
680 unsigned int flags)
682 int err;
683 enum ovl_path_type old_type;
684 enum ovl_path_type new_type;
685 struct dentry *old_upperdir;
686 struct dentry *new_upperdir;
687 struct dentry *olddentry;
688 struct dentry *newdentry;
689 struct dentry *trap;
690 bool old_opaque;
691 bool new_opaque;
692 bool new_create = false;
693 bool cleanup_whiteout = false;
694 bool overwrite = !(flags & RENAME_EXCHANGE);
695 bool is_dir = d_is_dir(old);
696 bool new_is_dir = false;
697 struct dentry *opaquedir = NULL;
698 const struct cred *old_cred = NULL;
700 err = -EINVAL;
701 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
702 goto out;
704 flags &= ~RENAME_NOREPLACE;
706 err = ovl_check_sticky(old);
707 if (err)
708 goto out;
710 /* Don't copy up directory trees */
711 old_type = ovl_path_type(old);
712 err = -EXDEV;
713 if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
714 goto out;
716 if (new->d_inode) {
717 err = ovl_check_sticky(new);
718 if (err)
719 goto out;
721 if (d_is_dir(new))
722 new_is_dir = true;
724 new_type = ovl_path_type(new);
725 err = -EXDEV;
726 if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
727 goto out;
729 err = 0;
730 if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
731 if (ovl_dentry_lower(old)->d_inode ==
732 ovl_dentry_lower(new)->d_inode)
733 goto out;
735 if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
736 if (ovl_dentry_upper(old)->d_inode ==
737 ovl_dentry_upper(new)->d_inode)
738 goto out;
740 } else {
741 if (ovl_dentry_is_opaque(new))
742 new_type = __OVL_PATH_UPPER;
743 else
744 new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
747 err = ovl_want_write(old);
748 if (err)
749 goto out;
751 err = ovl_copy_up(old);
752 if (err)
753 goto out_drop_write;
755 err = ovl_copy_up(new->d_parent);
756 if (err)
757 goto out_drop_write;
758 if (!overwrite) {
759 err = ovl_copy_up(new);
760 if (err)
761 goto out_drop_write;
764 old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
765 new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
767 if (old_opaque || new_opaque)
768 old_cred = ovl_override_creds(old->d_sb);
770 if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
771 opaquedir = ovl_check_empty_and_clear(new);
772 err = PTR_ERR(opaquedir);
773 if (IS_ERR(opaquedir)) {
774 opaquedir = NULL;
775 goto out_revert_creds;
779 if (overwrite) {
780 if (old_opaque) {
781 if (new->d_inode || !new_opaque) {
782 /* Whiteout source */
783 flags |= RENAME_WHITEOUT;
784 } else {
785 /* Switch whiteouts */
786 flags |= RENAME_EXCHANGE;
788 } else if (is_dir && !new->d_inode && new_opaque) {
789 flags |= RENAME_EXCHANGE;
790 cleanup_whiteout = true;
794 old_upperdir = ovl_dentry_upper(old->d_parent);
795 new_upperdir = ovl_dentry_upper(new->d_parent);
797 trap = lock_rename(new_upperdir, old_upperdir);
800 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
801 old->d_name.len);
802 err = PTR_ERR(olddentry);
803 if (IS_ERR(olddentry))
804 goto out_unlock;
806 err = -ESTALE;
807 if (olddentry != ovl_dentry_upper(old))
808 goto out_dput_old;
810 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
811 new->d_name.len);
812 err = PTR_ERR(newdentry);
813 if (IS_ERR(newdentry))
814 goto out_dput_old;
816 err = -ESTALE;
817 if (ovl_dentry_upper(new)) {
818 if (opaquedir) {
819 if (newdentry != opaquedir)
820 goto out_dput;
821 } else {
822 if (newdentry != ovl_dentry_upper(new))
823 goto out_dput;
825 } else {
826 new_create = true;
827 if (!d_is_negative(newdentry) &&
828 (!new_opaque || !ovl_is_whiteout(newdentry)))
829 goto out_dput;
832 if (olddentry == trap)
833 goto out_dput;
834 if (newdentry == trap)
835 goto out_dput;
837 if (is_dir && !old_opaque && new_opaque) {
838 err = ovl_set_opaque(olddentry);
839 if (err)
840 goto out_dput;
842 if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
843 err = ovl_set_opaque(newdentry);
844 if (err)
845 goto out_dput;
848 if (old_opaque || new_opaque) {
849 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
850 new_upperdir->d_inode, newdentry,
851 flags);
852 } else {
853 /* No debug for the plain case */
854 BUG_ON(flags & ~RENAME_EXCHANGE);
855 err = vfs_rename(old_upperdir->d_inode, olddentry,
856 new_upperdir->d_inode, newdentry,
857 NULL, flags);
860 if (err) {
861 if (is_dir && !old_opaque && new_opaque)
862 ovl_remove_opaque(olddentry);
863 if (!overwrite && new_is_dir && old_opaque && !new_opaque)
864 ovl_remove_opaque(newdentry);
865 goto out_dput;
868 if (is_dir && old_opaque && !new_opaque)
869 ovl_remove_opaque(olddentry);
870 if (!overwrite && new_is_dir && !old_opaque && new_opaque)
871 ovl_remove_opaque(newdentry);
874 * Old dentry now lives in different location. Dentries in
875 * lowerstack are stale. We cannot drop them here because
876 * access to them is lockless. This could be only pure upper
877 * or opaque directory - numlower is zero. Or upper non-dir
878 * entry - its pureness is tracked by flag opaque.
880 if (old_opaque != new_opaque) {
881 ovl_dentry_set_opaque(old, new_opaque);
882 if (!overwrite)
883 ovl_dentry_set_opaque(new, old_opaque);
886 if (cleanup_whiteout)
887 ovl_cleanup(old_upperdir->d_inode, newdentry);
889 ovl_dentry_version_inc(old->d_parent);
890 ovl_dentry_version_inc(new->d_parent);
892 out_dput:
893 dput(newdentry);
894 out_dput_old:
895 dput(olddentry);
896 out_unlock:
897 unlock_rename(new_upperdir, old_upperdir);
898 out_revert_creds:
899 if (old_opaque || new_opaque)
900 revert_creds(old_cred);
901 out_drop_write:
902 ovl_drop_write(old);
903 out:
904 dput(opaquedir);
905 return err;
908 const struct inode_operations ovl_dir_inode_operations = {
909 .lookup = ovl_lookup,
910 .mkdir = ovl_mkdir,
911 .symlink = ovl_symlink,
912 .unlink = ovl_unlink,
913 .rmdir = ovl_rmdir,
914 .rename2 = ovl_rename2,
915 .link = ovl_link,
916 .setattr = ovl_setattr,
917 .create = ovl_create,
918 .mknod = ovl_mknod,
919 .permission = ovl_permission,
920 .getattr = ovl_dir_getattr,
921 .setxattr = ovl_setxattr,
922 .getxattr = ovl_getxattr,
923 .listxattr = ovl_listxattr,
924 .removexattr = ovl_removexattr,