ocfs2: fix locking for res->tracking and dlm->tracking_list
[linux/fpc-iii.git] / fs / overlayfs / dir.c
blobf8aa542721213281c1f3df0982c649afec2ebf0d
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;
412 old_cred = ovl_override_creds(dentry->d_sb);
414 err = ovl_create_over_whiteout(dentry, inode, &stat, link,
415 hardlink);
417 revert_creds(old_cred);
420 if (!err)
421 inode = NULL;
422 out_iput:
423 iput(inode);
424 out:
425 return err;
428 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
429 const char *link)
431 int err;
433 err = ovl_want_write(dentry);
434 if (!err) {
435 err = ovl_create_or_link(dentry, mode, rdev, link, NULL);
436 ovl_drop_write(dentry);
439 return err;
442 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
443 bool excl)
445 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
448 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
450 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
453 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
454 dev_t rdev)
456 /* Don't allow creation of "whiteout" on overlay */
457 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
458 return -EPERM;
460 return ovl_create_object(dentry, mode, rdev, NULL);
463 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
464 const char *link)
466 return ovl_create_object(dentry, S_IFLNK, 0, link);
469 static int ovl_link(struct dentry *old, struct inode *newdir,
470 struct dentry *new)
472 int err;
473 struct dentry *upper;
475 err = ovl_want_write(old);
476 if (err)
477 goto out;
479 err = ovl_copy_up(old);
480 if (err)
481 goto out_drop_write;
483 upper = ovl_dentry_upper(old);
484 err = ovl_create_or_link(new, upper->d_inode->i_mode, 0, NULL, upper);
486 out_drop_write:
487 ovl_drop_write(old);
488 out:
489 return err;
492 static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
494 struct dentry *workdir = ovl_workdir(dentry);
495 struct inode *wdir = workdir->d_inode;
496 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
497 struct inode *udir = upperdir->d_inode;
498 struct dentry *whiteout;
499 struct dentry *upper;
500 struct dentry *opaquedir = NULL;
501 int err;
502 int flags = 0;
504 if (WARN_ON(!workdir))
505 return -EROFS;
507 if (is_dir) {
508 if (OVL_TYPE_MERGE_OR_LOWER(ovl_path_type(dentry))) {
509 opaquedir = ovl_check_empty_and_clear(dentry);
510 err = PTR_ERR(opaquedir);
511 if (IS_ERR(opaquedir))
512 goto out;
513 } else {
514 LIST_HEAD(list);
517 * When removing an empty opaque directory, then it
518 * makes no sense to replace it with an exact replica of
519 * itself. But emptiness still needs to be checked.
521 err = ovl_check_empty_dir(dentry, &list);
522 ovl_cache_free(&list);
523 if (err)
524 goto out;
528 err = ovl_lock_rename_workdir(workdir, upperdir);
529 if (err)
530 goto out_dput;
532 upper = lookup_one_len(dentry->d_name.name, upperdir,
533 dentry->d_name.len);
534 err = PTR_ERR(upper);
535 if (IS_ERR(upper))
536 goto out_unlock;
538 err = -ESTALE;
539 if ((opaquedir && upper != opaquedir) ||
540 (!opaquedir && ovl_dentry_upper(dentry) &&
541 upper != ovl_dentry_upper(dentry))) {
542 goto out_dput_upper;
545 whiteout = ovl_whiteout(workdir, dentry);
546 err = PTR_ERR(whiteout);
547 if (IS_ERR(whiteout))
548 goto out_dput_upper;
550 if (d_is_dir(upper))
551 flags = RENAME_EXCHANGE;
553 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
554 if (err)
555 goto kill_whiteout;
556 if (flags)
557 ovl_cleanup(wdir, upper);
559 ovl_dentry_version_inc(dentry->d_parent);
560 out_d_drop:
561 d_drop(dentry);
562 dput(whiteout);
563 out_dput_upper:
564 dput(upper);
565 out_unlock:
566 unlock_rename(workdir, upperdir);
567 out_dput:
568 dput(opaquedir);
569 out:
570 return err;
572 kill_whiteout:
573 ovl_cleanup(wdir, whiteout);
574 goto out_d_drop;
577 static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
579 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
580 struct inode *dir = upperdir->d_inode;
581 struct dentry *upper;
582 int err;
584 mutex_lock_nested(&dir->i_mutex, I_MUTEX_PARENT);
585 upper = lookup_one_len(dentry->d_name.name, upperdir,
586 dentry->d_name.len);
587 err = PTR_ERR(upper);
588 if (IS_ERR(upper))
589 goto out_unlock;
591 err = -ESTALE;
592 if (upper == ovl_dentry_upper(dentry)) {
593 if (is_dir)
594 err = vfs_rmdir(dir, upper);
595 else
596 err = vfs_unlink(dir, upper, NULL);
597 ovl_dentry_version_inc(dentry->d_parent);
599 dput(upper);
602 * Keeping this dentry hashed would mean having to release
603 * upperpath/lowerpath, which could only be done if we are the
604 * sole user of this dentry. Too tricky... Just unhash for
605 * now.
607 if (!err)
608 d_drop(dentry);
609 out_unlock:
610 mutex_unlock(&dir->i_mutex);
612 return err;
615 static inline int ovl_check_sticky(struct dentry *dentry)
617 struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode;
618 struct inode *inode = ovl_dentry_real(dentry)->d_inode;
620 if (check_sticky(dir, inode))
621 return -EPERM;
623 return 0;
626 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
628 enum ovl_path_type type;
629 int err;
631 err = ovl_check_sticky(dentry);
632 if (err)
633 goto out;
635 err = ovl_want_write(dentry);
636 if (err)
637 goto out;
639 err = ovl_copy_up(dentry->d_parent);
640 if (err)
641 goto out_drop_write;
643 type = ovl_path_type(dentry);
644 if (OVL_TYPE_PURE_UPPER(type)) {
645 err = ovl_remove_upper(dentry, is_dir);
646 } else {
647 const struct cred *old_cred = ovl_override_creds(dentry->d_sb);
649 err = ovl_remove_and_whiteout(dentry, is_dir);
651 revert_creds(old_cred);
653 out_drop_write:
654 ovl_drop_write(dentry);
655 out:
656 return err;
659 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
661 return ovl_do_remove(dentry, false);
664 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
666 return ovl_do_remove(dentry, true);
669 static int ovl_rename2(struct inode *olddir, struct dentry *old,
670 struct inode *newdir, struct dentry *new,
671 unsigned int flags)
673 int err;
674 enum ovl_path_type old_type;
675 enum ovl_path_type new_type;
676 struct dentry *old_upperdir;
677 struct dentry *new_upperdir;
678 struct dentry *olddentry;
679 struct dentry *newdentry;
680 struct dentry *trap;
681 bool old_opaque;
682 bool new_opaque;
683 bool new_create = false;
684 bool cleanup_whiteout = false;
685 bool overwrite = !(flags & RENAME_EXCHANGE);
686 bool is_dir = d_is_dir(old);
687 bool new_is_dir = false;
688 struct dentry *opaquedir = NULL;
689 const struct cred *old_cred = NULL;
691 err = -EINVAL;
692 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
693 goto out;
695 flags &= ~RENAME_NOREPLACE;
697 err = ovl_check_sticky(old);
698 if (err)
699 goto out;
701 /* Don't copy up directory trees */
702 old_type = ovl_path_type(old);
703 err = -EXDEV;
704 if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
705 goto out;
707 if (new->d_inode) {
708 err = ovl_check_sticky(new);
709 if (err)
710 goto out;
712 if (d_is_dir(new))
713 new_is_dir = true;
715 new_type = ovl_path_type(new);
716 err = -EXDEV;
717 if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
718 goto out;
720 err = 0;
721 if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
722 if (ovl_dentry_lower(old)->d_inode ==
723 ovl_dentry_lower(new)->d_inode)
724 goto out;
726 if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
727 if (ovl_dentry_upper(old)->d_inode ==
728 ovl_dentry_upper(new)->d_inode)
729 goto out;
731 } else {
732 if (ovl_dentry_is_opaque(new))
733 new_type = __OVL_PATH_UPPER;
734 else
735 new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
738 err = ovl_want_write(old);
739 if (err)
740 goto out;
742 err = ovl_copy_up(old);
743 if (err)
744 goto out_drop_write;
746 err = ovl_copy_up(new->d_parent);
747 if (err)
748 goto out_drop_write;
749 if (!overwrite) {
750 err = ovl_copy_up(new);
751 if (err)
752 goto out_drop_write;
755 old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
756 new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
758 if (old_opaque || new_opaque)
759 old_cred = ovl_override_creds(old->d_sb);
761 if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
762 opaquedir = ovl_check_empty_and_clear(new);
763 err = PTR_ERR(opaquedir);
764 if (IS_ERR(opaquedir)) {
765 opaquedir = NULL;
766 goto out_revert_creds;
770 if (overwrite) {
771 if (old_opaque) {
772 if (new->d_inode || !new_opaque) {
773 /* Whiteout source */
774 flags |= RENAME_WHITEOUT;
775 } else {
776 /* Switch whiteouts */
777 flags |= RENAME_EXCHANGE;
779 } else if (is_dir && !new->d_inode && new_opaque) {
780 flags |= RENAME_EXCHANGE;
781 cleanup_whiteout = true;
785 old_upperdir = ovl_dentry_upper(old->d_parent);
786 new_upperdir = ovl_dentry_upper(new->d_parent);
788 trap = lock_rename(new_upperdir, old_upperdir);
791 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
792 old->d_name.len);
793 err = PTR_ERR(olddentry);
794 if (IS_ERR(olddentry))
795 goto out_unlock;
797 err = -ESTALE;
798 if (olddentry != ovl_dentry_upper(old))
799 goto out_dput_old;
801 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
802 new->d_name.len);
803 err = PTR_ERR(newdentry);
804 if (IS_ERR(newdentry))
805 goto out_dput_old;
807 err = -ESTALE;
808 if (ovl_dentry_upper(new)) {
809 if (opaquedir) {
810 if (newdentry != opaquedir)
811 goto out_dput;
812 } else {
813 if (newdentry != ovl_dentry_upper(new))
814 goto out_dput;
816 } else {
817 new_create = true;
818 if (!d_is_negative(newdentry) &&
819 (!new_opaque || !ovl_is_whiteout(newdentry)))
820 goto out_dput;
823 if (olddentry == trap)
824 goto out_dput;
825 if (newdentry == trap)
826 goto out_dput;
828 if (is_dir && !old_opaque && new_opaque) {
829 err = ovl_set_opaque(olddentry);
830 if (err)
831 goto out_dput;
833 if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
834 err = ovl_set_opaque(newdentry);
835 if (err)
836 goto out_dput;
839 if (old_opaque || new_opaque) {
840 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
841 new_upperdir->d_inode, newdentry,
842 flags);
843 } else {
844 /* No debug for the plain case */
845 BUG_ON(flags & ~RENAME_EXCHANGE);
846 err = vfs_rename(old_upperdir->d_inode, olddentry,
847 new_upperdir->d_inode, newdentry,
848 NULL, flags);
851 if (err) {
852 if (is_dir && !old_opaque && new_opaque)
853 ovl_remove_opaque(olddentry);
854 if (!overwrite && new_is_dir && old_opaque && !new_opaque)
855 ovl_remove_opaque(newdentry);
856 goto out_dput;
859 if (is_dir && old_opaque && !new_opaque)
860 ovl_remove_opaque(olddentry);
861 if (!overwrite && new_is_dir && !old_opaque && new_opaque)
862 ovl_remove_opaque(newdentry);
865 * Old dentry now lives in different location. Dentries in
866 * lowerstack are stale. We cannot drop them here because
867 * access to them is lockless. This could be only pure upper
868 * or opaque directory - numlower is zero. Or upper non-dir
869 * entry - its pureness is tracked by flag opaque.
871 if (old_opaque != new_opaque) {
872 ovl_dentry_set_opaque(old, new_opaque);
873 if (!overwrite)
874 ovl_dentry_set_opaque(new, old_opaque);
877 if (cleanup_whiteout)
878 ovl_cleanup(old_upperdir->d_inode, newdentry);
880 ovl_dentry_version_inc(old->d_parent);
881 ovl_dentry_version_inc(new->d_parent);
883 out_dput:
884 dput(newdentry);
885 out_dput_old:
886 dput(olddentry);
887 out_unlock:
888 unlock_rename(new_upperdir, old_upperdir);
889 out_revert_creds:
890 if (old_opaque || new_opaque)
891 revert_creds(old_cred);
892 out_drop_write:
893 ovl_drop_write(old);
894 out:
895 dput(opaquedir);
896 return err;
899 const struct inode_operations ovl_dir_inode_operations = {
900 .lookup = ovl_lookup,
901 .mkdir = ovl_mkdir,
902 .symlink = ovl_symlink,
903 .unlink = ovl_unlink,
904 .rmdir = ovl_rmdir,
905 .rename2 = ovl_rename2,
906 .link = ovl_link,
907 .setattr = ovl_setattr,
908 .create = ovl_create,
909 .mknod = ovl_mknod,
910 .permission = ovl_permission,
911 .getattr = ovl_dir_getattr,
912 .setxattr = ovl_setxattr,
913 .getxattr = ovl_getxattr,
914 .listxattr = ovl_listxattr,
915 .removexattr = ovl_removexattr,