smsc95xx: check return value of smsc95xx_reset
[linux/fpc-iii.git] / fs / overlayfs / dir.c
blob8546384a5fdfd36577fddac11164223a410d9f05
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/posix_acl.h>
16 #include <linux/posix_acl_xattr.h>
17 #include <linux/atomic.h>
18 #include "overlayfs.h"
20 void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
22 int err;
24 dget(wdentry);
25 if (d_is_dir(wdentry))
26 err = ovl_do_rmdir(wdir, wdentry);
27 else
28 err = ovl_do_unlink(wdir, wdentry);
29 dput(wdentry);
31 if (err) {
32 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
33 wdentry, err);
37 struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
39 struct dentry *temp;
40 char name[20];
41 static atomic_t temp_id = ATOMIC_INIT(0);
43 /* counter is allowed to wrap, since temp dentries are ephemeral */
44 snprintf(name, sizeof(name), "#%x", atomic_inc_return(&temp_id));
46 temp = lookup_one_len(name, workdir, strlen(name));
47 if (!IS_ERR(temp) && temp->d_inode) {
48 pr_err("overlayfs: workdir/%s already exists\n", name);
49 dput(temp);
50 temp = ERR_PTR(-EIO);
53 return temp;
56 /* caller holds i_mutex on workdir */
57 static struct dentry *ovl_whiteout(struct dentry *workdir,
58 struct dentry *dentry)
60 int err;
61 struct dentry *whiteout;
62 struct inode *wdir = workdir->d_inode;
64 whiteout = ovl_lookup_temp(workdir, dentry);
65 if (IS_ERR(whiteout))
66 return whiteout;
68 err = ovl_do_whiteout(wdir, whiteout);
69 if (err) {
70 dput(whiteout);
71 whiteout = ERR_PTR(err);
74 return whiteout;
77 int ovl_create_real(struct inode *dir, struct dentry *newdentry,
78 struct kstat *stat, const char *link,
79 struct dentry *hardlink, bool debug)
81 int err;
83 if (newdentry->d_inode)
84 return -ESTALE;
86 if (hardlink) {
87 err = ovl_do_link(hardlink, dir, newdentry, debug);
88 } else {
89 switch (stat->mode & S_IFMT) {
90 case S_IFREG:
91 err = ovl_do_create(dir, newdentry, stat->mode, debug);
92 break;
94 case S_IFDIR:
95 err = ovl_do_mkdir(dir, newdentry, stat->mode, debug);
96 break;
98 case S_IFCHR:
99 case S_IFBLK:
100 case S_IFIFO:
101 case S_IFSOCK:
102 err = ovl_do_mknod(dir, newdentry,
103 stat->mode, stat->rdev, debug);
104 break;
106 case S_IFLNK:
107 err = ovl_do_symlink(dir, newdentry, link, debug);
108 break;
110 default:
111 err = -EPERM;
114 if (!err && WARN_ON(!newdentry->d_inode)) {
116 * Not quite sure if non-instantiated dentry is legal or not.
117 * VFS doesn't seem to care so check and warn here.
119 err = -ENOENT;
121 return err;
124 static int ovl_set_opaque(struct dentry *upperdentry)
126 return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
129 static void ovl_remove_opaque(struct dentry *upperdentry)
131 int err;
133 err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE);
134 if (err) {
135 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n",
136 upperdentry->d_name.name, err);
140 static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
141 struct kstat *stat)
143 int err;
144 enum ovl_path_type type;
145 struct path realpath;
146 const struct cred *old_cred;
148 type = ovl_path_real(dentry, &realpath);
149 old_cred = ovl_override_creds(dentry->d_sb);
150 err = vfs_getattr(&realpath, stat);
151 revert_creds(old_cred);
152 if (err)
153 return err;
155 stat->dev = dentry->d_sb->s_dev;
156 stat->ino = dentry->d_inode->i_ino;
159 * It's probably not worth it to count subdirs to get the
160 * correct link count. nlink=1 seems to pacify 'find' and
161 * other utilities.
163 if (OVL_TYPE_MERGE(type))
164 stat->nlink = 1;
166 return 0;
169 /* Common operations required to be done after creation of file on upper */
170 static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
171 struct dentry *newdentry, bool hardlink)
173 ovl_dentry_version_inc(dentry->d_parent);
174 ovl_dentry_update(dentry, newdentry);
175 if (!hardlink) {
176 ovl_inode_update(inode, d_inode(newdentry));
177 ovl_copyattr(newdentry->d_inode, inode);
178 } else {
179 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
180 inc_nlink(inode);
182 d_instantiate(dentry, inode);
183 /* Force lookup of new upper hardlink to find its lower */
184 if (hardlink)
185 d_drop(dentry);
188 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
189 struct kstat *stat, const char *link,
190 struct dentry *hardlink)
192 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
193 struct inode *udir = upperdir->d_inode;
194 struct dentry *newdentry;
195 int err;
197 if (!hardlink && !IS_POSIXACL(udir))
198 stat->mode &= ~current_umask();
200 inode_lock_nested(udir, I_MUTEX_PARENT);
201 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
202 dentry->d_name.len);
203 err = PTR_ERR(newdentry);
204 if (IS_ERR(newdentry))
205 goto out_unlock;
206 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
207 if (err)
208 goto out_dput;
210 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
211 newdentry = NULL;
212 out_dput:
213 dput(newdentry);
214 out_unlock:
215 inode_unlock(udir);
216 return err;
219 static int ovl_lock_rename_workdir(struct dentry *workdir,
220 struct dentry *upperdir)
222 /* Workdir should not be the same as upperdir */
223 if (workdir == upperdir)
224 goto err;
226 /* Workdir should not be subdir of upperdir and vice versa */
227 if (lock_rename(workdir, upperdir) != NULL)
228 goto err_unlock;
230 return 0;
232 err_unlock:
233 unlock_rename(workdir, upperdir);
234 err:
235 pr_err("overlayfs: failed to lock workdir+upperdir\n");
236 return -EIO;
239 static struct dentry *ovl_clear_empty(struct dentry *dentry,
240 struct list_head *list)
242 struct dentry *workdir = ovl_workdir(dentry);
243 struct inode *wdir = workdir->d_inode;
244 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
245 struct inode *udir = upperdir->d_inode;
246 struct path upperpath;
247 struct dentry *upper;
248 struct dentry *opaquedir;
249 struct kstat stat;
250 int err;
252 if (WARN_ON(!workdir))
253 return ERR_PTR(-EROFS);
255 err = ovl_lock_rename_workdir(workdir, upperdir);
256 if (err)
257 goto out;
259 ovl_path_upper(dentry, &upperpath);
260 err = vfs_getattr(&upperpath, &stat);
261 if (err)
262 goto out_unlock;
264 err = -ESTALE;
265 if (!S_ISDIR(stat.mode))
266 goto out_unlock;
267 upper = upperpath.dentry;
268 if (upper->d_parent->d_inode != udir)
269 goto out_unlock;
271 opaquedir = ovl_lookup_temp(workdir, dentry);
272 err = PTR_ERR(opaquedir);
273 if (IS_ERR(opaquedir))
274 goto out_unlock;
276 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
277 if (err)
278 goto out_dput;
280 err = ovl_copy_xattr(upper, opaquedir);
281 if (err)
282 goto out_cleanup;
284 err = ovl_set_opaque(opaquedir);
285 if (err)
286 goto out_cleanup;
288 inode_lock(opaquedir->d_inode);
289 err = ovl_set_attr(opaquedir, &stat);
290 inode_unlock(opaquedir->d_inode);
291 if (err)
292 goto out_cleanup;
294 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
295 if (err)
296 goto out_cleanup;
298 ovl_cleanup_whiteouts(upper, list);
299 ovl_cleanup(wdir, upper);
300 unlock_rename(workdir, upperdir);
302 /* dentry's upper doesn't match now, get rid of it */
303 d_drop(dentry);
305 return opaquedir;
307 out_cleanup:
308 ovl_cleanup(wdir, opaquedir);
309 out_dput:
310 dput(opaquedir);
311 out_unlock:
312 unlock_rename(workdir, upperdir);
313 out:
314 return ERR_PTR(err);
317 static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
319 int err;
320 struct dentry *ret = NULL;
321 enum ovl_path_type type = ovl_path_type(dentry);
322 LIST_HEAD(list);
324 err = ovl_check_empty_dir(dentry, &list);
325 if (err) {
326 ret = ERR_PTR(err);
327 goto out_free;
331 * When removing an empty opaque directory, then it makes no sense to
332 * replace it with an exact replica of itself.
334 * If no upperdentry then skip clearing whiteouts.
336 * Can race with copy-up, since we don't hold the upperdir mutex.
337 * Doesn't matter, since copy-up can't create a non-empty directory
338 * from an empty one.
340 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
341 ret = ovl_clear_empty(dentry, &list);
343 out_free:
344 ovl_cache_free(&list);
346 return ret;
349 static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
350 const struct posix_acl *acl)
352 void *buffer;
353 size_t size;
354 int err;
356 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
357 return 0;
359 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
360 buffer = kmalloc(size, GFP_KERNEL);
361 if (!buffer)
362 return -ENOMEM;
364 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
365 err = size;
366 if (err < 0)
367 goto out_free;
369 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
370 out_free:
371 kfree(buffer);
372 return err;
375 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
376 struct kstat *stat, const char *link,
377 struct dentry *hardlink)
379 struct dentry *workdir = ovl_workdir(dentry);
380 struct inode *wdir = workdir->d_inode;
381 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
382 struct inode *udir = upperdir->d_inode;
383 struct dentry *upper;
384 struct dentry *newdentry;
385 int err;
386 struct posix_acl *acl, *default_acl;
388 if (WARN_ON(!workdir))
389 return -EROFS;
391 if (!hardlink) {
392 err = posix_acl_create(dentry->d_parent->d_inode,
393 &stat->mode, &default_acl, &acl);
394 if (err)
395 return err;
398 err = ovl_lock_rename_workdir(workdir, upperdir);
399 if (err)
400 goto out;
402 newdentry = ovl_lookup_temp(workdir, dentry);
403 err = PTR_ERR(newdentry);
404 if (IS_ERR(newdentry))
405 goto out_unlock;
407 upper = lookup_one_len(dentry->d_name.name, upperdir,
408 dentry->d_name.len);
409 err = PTR_ERR(upper);
410 if (IS_ERR(upper))
411 goto out_dput;
413 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
414 if (err)
415 goto out_dput2;
418 * mode could have been mutilated due to umask (e.g. sgid directory)
420 if (!hardlink &&
421 !S_ISLNK(stat->mode) && newdentry->d_inode->i_mode != stat->mode) {
422 struct iattr attr = {
423 .ia_valid = ATTR_MODE,
424 .ia_mode = stat->mode,
426 inode_lock(newdentry->d_inode);
427 err = notify_change(newdentry, &attr, NULL);
428 inode_unlock(newdentry->d_inode);
429 if (err)
430 goto out_cleanup;
432 if (!hardlink) {
433 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
434 acl);
435 if (err)
436 goto out_cleanup;
438 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
439 default_acl);
440 if (err)
441 goto out_cleanup;
444 if (!hardlink && S_ISDIR(stat->mode)) {
445 err = ovl_set_opaque(newdentry);
446 if (err)
447 goto out_cleanup;
449 err = ovl_do_rename(wdir, newdentry, udir, upper,
450 RENAME_EXCHANGE);
451 if (err)
452 goto out_cleanup;
454 ovl_cleanup(wdir, upper);
455 } else {
456 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
457 if (err)
458 goto out_cleanup;
460 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
461 newdentry = NULL;
462 out_dput2:
463 dput(upper);
464 out_dput:
465 dput(newdentry);
466 out_unlock:
467 unlock_rename(workdir, upperdir);
468 out:
469 if (!hardlink) {
470 posix_acl_release(acl);
471 posix_acl_release(default_acl);
473 return err;
475 out_cleanup:
476 ovl_cleanup(wdir, newdentry);
477 goto out_dput2;
480 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
481 struct kstat *stat, const char *link,
482 struct dentry *hardlink)
484 int err;
485 const struct cred *old_cred;
486 struct cred *override_cred;
488 err = ovl_copy_up(dentry->d_parent);
489 if (err)
490 return err;
492 old_cred = ovl_override_creds(dentry->d_sb);
493 err = -ENOMEM;
494 override_cred = prepare_creds();
495 if (override_cred) {
496 override_cred->fsuid = inode->i_uid;
497 override_cred->fsgid = inode->i_gid;
498 if (!hardlink) {
499 err = security_dentry_create_files_as(dentry,
500 stat->mode, &dentry->d_name, old_cred,
501 override_cred);
502 if (err) {
503 put_cred(override_cred);
504 goto out_revert_creds;
507 put_cred(override_creds(override_cred));
508 put_cred(override_cred);
510 if (!ovl_dentry_is_opaque(dentry))
511 err = ovl_create_upper(dentry, inode, stat, link,
512 hardlink);
513 else
514 err = ovl_create_over_whiteout(dentry, inode, stat,
515 link, hardlink);
517 out_revert_creds:
518 revert_creds(old_cred);
519 if (!err) {
520 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
522 WARN_ON(inode->i_mode != realinode->i_mode);
523 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
524 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
526 return err;
529 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
530 const char *link)
532 int err;
533 struct inode *inode;
534 struct kstat stat = {
535 .rdev = rdev,
538 err = ovl_want_write(dentry);
539 if (err)
540 goto out;
542 err = -ENOMEM;
543 inode = ovl_new_inode(dentry->d_sb, mode);
544 if (!inode)
545 goto out_drop_write;
547 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
548 stat.mode = inode->i_mode;
550 err = ovl_create_or_link(dentry, inode, &stat, link, NULL);
551 if (err)
552 iput(inode);
554 out_drop_write:
555 ovl_drop_write(dentry);
556 out:
557 return err;
560 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
561 bool excl)
563 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
566 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
568 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
571 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
572 dev_t rdev)
574 /* Don't allow creation of "whiteout" on overlay */
575 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
576 return -EPERM;
578 return ovl_create_object(dentry, mode, rdev, NULL);
581 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
582 const char *link)
584 return ovl_create_object(dentry, S_IFLNK, 0, link);
587 static int ovl_link(struct dentry *old, struct inode *newdir,
588 struct dentry *new)
590 int err;
591 struct inode *inode;
593 err = ovl_want_write(old);
594 if (err)
595 goto out;
597 err = ovl_copy_up(old);
598 if (err)
599 goto out_drop_write;
601 inode = d_inode(old);
602 ihold(inode);
604 err = ovl_create_or_link(new, inode, NULL, NULL, ovl_dentry_upper(old));
605 if (err)
606 iput(inode);
608 out_drop_write:
609 ovl_drop_write(old);
610 out:
611 return err;
614 static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
616 struct dentry *workdir = ovl_workdir(dentry);
617 struct inode *wdir = workdir->d_inode;
618 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
619 struct inode *udir = upperdir->d_inode;
620 struct dentry *whiteout;
621 struct dentry *upper;
622 struct dentry *opaquedir = NULL;
623 int err;
624 int flags = 0;
626 if (WARN_ON(!workdir))
627 return -EROFS;
629 if (is_dir) {
630 opaquedir = ovl_check_empty_and_clear(dentry);
631 err = PTR_ERR(opaquedir);
632 if (IS_ERR(opaquedir))
633 goto out;
636 err = ovl_lock_rename_workdir(workdir, upperdir);
637 if (err)
638 goto out_dput;
640 upper = lookup_one_len(dentry->d_name.name, upperdir,
641 dentry->d_name.len);
642 err = PTR_ERR(upper);
643 if (IS_ERR(upper))
644 goto out_unlock;
646 err = -ESTALE;
647 if ((opaquedir && upper != opaquedir) ||
648 (!opaquedir && ovl_dentry_upper(dentry) &&
649 upper != ovl_dentry_upper(dentry))) {
650 goto out_dput_upper;
653 whiteout = ovl_whiteout(workdir, dentry);
654 err = PTR_ERR(whiteout);
655 if (IS_ERR(whiteout))
656 goto out_dput_upper;
658 if (d_is_dir(upper))
659 flags = RENAME_EXCHANGE;
661 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
662 if (err)
663 goto kill_whiteout;
664 if (flags)
665 ovl_cleanup(wdir, upper);
667 ovl_dentry_version_inc(dentry->d_parent);
668 out_d_drop:
669 d_drop(dentry);
670 dput(whiteout);
671 out_dput_upper:
672 dput(upper);
673 out_unlock:
674 unlock_rename(workdir, upperdir);
675 out_dput:
676 dput(opaquedir);
677 out:
678 return err;
680 kill_whiteout:
681 ovl_cleanup(wdir, whiteout);
682 goto out_d_drop;
685 static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
687 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
688 struct inode *dir = upperdir->d_inode;
689 struct dentry *upper;
690 int err;
692 inode_lock_nested(dir, I_MUTEX_PARENT);
693 upper = lookup_one_len(dentry->d_name.name, upperdir,
694 dentry->d_name.len);
695 err = PTR_ERR(upper);
696 if (IS_ERR(upper))
697 goto out_unlock;
699 err = -ESTALE;
700 if (upper == ovl_dentry_upper(dentry)) {
701 if (is_dir)
702 err = vfs_rmdir(dir, upper);
703 else
704 err = vfs_unlink(dir, upper, NULL);
705 ovl_dentry_version_inc(dentry->d_parent);
707 dput(upper);
710 * Keeping this dentry hashed would mean having to release
711 * upperpath/lowerpath, which could only be done if we are the
712 * sole user of this dentry. Too tricky... Just unhash for
713 * now.
715 if (!err)
716 d_drop(dentry);
717 out_unlock:
718 inode_unlock(dir);
720 return err;
723 static inline int ovl_check_sticky(struct dentry *dentry)
725 struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode;
726 struct inode *inode = ovl_dentry_real(dentry)->d_inode;
728 if (check_sticky(dir, inode))
729 return -EPERM;
731 return 0;
734 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
736 enum ovl_path_type type;
737 int err;
738 const struct cred *old_cred;
741 err = ovl_check_sticky(dentry);
742 if (err)
743 goto out;
745 err = ovl_want_write(dentry);
746 if (err)
747 goto out;
749 err = ovl_copy_up(dentry->d_parent);
750 if (err)
751 goto out_drop_write;
753 type = ovl_path_type(dentry);
755 old_cred = ovl_override_creds(dentry->d_sb);
756 if (OVL_TYPE_PURE_UPPER(type))
757 err = ovl_remove_upper(dentry, is_dir);
758 else
759 err = ovl_remove_and_whiteout(dentry, is_dir);
760 revert_creds(old_cred);
761 if (!err) {
762 if (is_dir)
763 clear_nlink(dentry->d_inode);
764 else
765 drop_nlink(dentry->d_inode);
767 out_drop_write:
768 ovl_drop_write(dentry);
769 out:
770 return err;
773 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
775 return ovl_do_remove(dentry, false);
778 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
780 return ovl_do_remove(dentry, true);
783 static int ovl_rename2(struct inode *olddir, struct dentry *old,
784 struct inode *newdir, struct dentry *new,
785 unsigned int flags)
787 int err;
788 enum ovl_path_type old_type;
789 enum ovl_path_type new_type;
790 struct dentry *old_upperdir;
791 struct dentry *new_upperdir;
792 struct dentry *olddentry;
793 struct dentry *newdentry;
794 struct dentry *trap;
795 bool old_opaque;
796 bool new_opaque;
797 bool cleanup_whiteout = false;
798 bool overwrite = !(flags & RENAME_EXCHANGE);
799 bool is_dir = d_is_dir(old);
800 bool new_is_dir = false;
801 struct dentry *opaquedir = NULL;
802 const struct cred *old_cred = NULL;
804 err = -EINVAL;
805 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
806 goto out;
808 flags &= ~RENAME_NOREPLACE;
810 err = ovl_check_sticky(old);
811 if (err)
812 goto out;
814 /* Don't copy up directory trees */
815 old_type = ovl_path_type(old);
816 err = -EXDEV;
817 if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
818 goto out;
820 if (new->d_inode) {
821 err = ovl_check_sticky(new);
822 if (err)
823 goto out;
825 if (d_is_dir(new))
826 new_is_dir = true;
828 new_type = ovl_path_type(new);
829 err = -EXDEV;
830 if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
831 goto out;
833 err = 0;
834 if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
835 if (ovl_dentry_lower(old)->d_inode ==
836 ovl_dentry_lower(new)->d_inode)
837 goto out;
839 if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
840 if (ovl_dentry_upper(old)->d_inode ==
841 ovl_dentry_upper(new)->d_inode)
842 goto out;
844 } else {
845 if (ovl_dentry_is_opaque(new))
846 new_type = __OVL_PATH_UPPER;
847 else
848 new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
851 err = ovl_want_write(old);
852 if (err)
853 goto out;
855 err = ovl_copy_up(old);
856 if (err)
857 goto out_drop_write;
859 err = ovl_copy_up(new->d_parent);
860 if (err)
861 goto out_drop_write;
862 if (!overwrite) {
863 err = ovl_copy_up(new);
864 if (err)
865 goto out_drop_write;
868 old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
869 new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
871 old_cred = ovl_override_creds(old->d_sb);
873 if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
874 opaquedir = ovl_check_empty_and_clear(new);
875 err = PTR_ERR(opaquedir);
876 if (IS_ERR(opaquedir)) {
877 opaquedir = NULL;
878 goto out_revert_creds;
882 if (overwrite) {
883 if (old_opaque) {
884 if (new->d_inode || !new_opaque) {
885 /* Whiteout source */
886 flags |= RENAME_WHITEOUT;
887 } else {
888 /* Switch whiteouts */
889 flags |= RENAME_EXCHANGE;
891 } else if (is_dir && !new->d_inode && new_opaque) {
892 flags |= RENAME_EXCHANGE;
893 cleanup_whiteout = true;
897 old_upperdir = ovl_dentry_upper(old->d_parent);
898 new_upperdir = ovl_dentry_upper(new->d_parent);
900 trap = lock_rename(new_upperdir, old_upperdir);
903 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
904 old->d_name.len);
905 err = PTR_ERR(olddentry);
906 if (IS_ERR(olddentry))
907 goto out_unlock;
909 err = -ESTALE;
910 if (olddentry != ovl_dentry_upper(old))
911 goto out_dput_old;
913 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
914 new->d_name.len);
915 err = PTR_ERR(newdentry);
916 if (IS_ERR(newdentry))
917 goto out_dput_old;
919 err = -ESTALE;
920 if (ovl_dentry_upper(new)) {
921 if (opaquedir) {
922 if (newdentry != opaquedir)
923 goto out_dput;
924 } else {
925 if (newdentry != ovl_dentry_upper(new))
926 goto out_dput;
928 } else {
929 if (!d_is_negative(newdentry) &&
930 (!new_opaque || !ovl_is_whiteout(newdentry)))
931 goto out_dput;
934 if (olddentry == trap)
935 goto out_dput;
936 if (newdentry == trap)
937 goto out_dput;
939 if (is_dir && !old_opaque && new_opaque) {
940 err = ovl_set_opaque(olddentry);
941 if (err)
942 goto out_dput;
944 if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
945 err = ovl_set_opaque(newdentry);
946 if (err)
947 goto out_dput;
950 if (old_opaque || new_opaque) {
951 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
952 new_upperdir->d_inode, newdentry,
953 flags);
954 } else {
955 /* No debug for the plain case */
956 BUG_ON(flags & ~RENAME_EXCHANGE);
957 err = vfs_rename(old_upperdir->d_inode, olddentry,
958 new_upperdir->d_inode, newdentry,
959 NULL, flags);
962 if (err) {
963 if (is_dir && !old_opaque && new_opaque)
964 ovl_remove_opaque(olddentry);
965 if (!overwrite && new_is_dir && old_opaque && !new_opaque)
966 ovl_remove_opaque(newdentry);
967 goto out_dput;
970 if (is_dir && old_opaque && !new_opaque)
971 ovl_remove_opaque(olddentry);
972 if (!overwrite && new_is_dir && !old_opaque && new_opaque)
973 ovl_remove_opaque(newdentry);
976 * Old dentry now lives in different location. Dentries in
977 * lowerstack are stale. We cannot drop them here because
978 * access to them is lockless. This could be only pure upper
979 * or opaque directory - numlower is zero. Or upper non-dir
980 * entry - its pureness is tracked by flag opaque.
982 if (old_opaque != new_opaque) {
983 ovl_dentry_set_opaque(old, new_opaque);
984 if (!overwrite)
985 ovl_dentry_set_opaque(new, old_opaque);
988 if (cleanup_whiteout)
989 ovl_cleanup(old_upperdir->d_inode, newdentry);
991 ovl_dentry_version_inc(old->d_parent);
992 ovl_dentry_version_inc(new->d_parent);
994 out_dput:
995 dput(newdentry);
996 out_dput_old:
997 dput(olddentry);
998 out_unlock:
999 unlock_rename(new_upperdir, old_upperdir);
1000 out_revert_creds:
1001 revert_creds(old_cred);
1002 out_drop_write:
1003 ovl_drop_write(old);
1004 out:
1005 dput(opaquedir);
1006 return err;
1009 const struct inode_operations ovl_dir_inode_operations = {
1010 .lookup = ovl_lookup,
1011 .mkdir = ovl_mkdir,
1012 .symlink = ovl_symlink,
1013 .unlink = ovl_unlink,
1014 .rmdir = ovl_rmdir,
1015 .rename = ovl_rename2,
1016 .link = ovl_link,
1017 .setattr = ovl_setattr,
1018 .create = ovl_create,
1019 .mknod = ovl_mknod,
1020 .permission = ovl_permission,
1021 .getattr = ovl_dir_getattr,
1022 .listxattr = ovl_listxattr,
1023 .get_acl = ovl_get_acl,
1024 .update_time = ovl_update_time,