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.
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
)
23 if (d_is_dir(wdentry
))
24 err
= ovl_do_rmdir(wdir
, wdentry
);
26 err
= ovl_do_unlink(wdir
, wdentry
);
30 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
35 struct dentry
*ovl_lookup_temp(struct dentry
*workdir
, struct dentry
*dentry
)
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
);
54 /* caller holds i_mutex on workdir */
55 static struct dentry
*ovl_whiteout(struct dentry
*workdir
,
56 struct dentry
*dentry
)
59 struct dentry
*whiteout
;
60 struct inode
*wdir
= workdir
->d_inode
;
62 whiteout
= ovl_lookup_temp(workdir
, dentry
);
66 err
= ovl_do_whiteout(wdir
, whiteout
);
69 whiteout
= ERR_PTR(err
);
75 int ovl_create_real(struct inode
*dir
, struct dentry
*newdentry
,
76 struct kstat
*stat
, const char *link
,
77 struct dentry
*hardlink
, bool debug
)
81 if (newdentry
->d_inode
)
85 err
= ovl_do_link(hardlink
, dir
, newdentry
, debug
);
87 switch (stat
->mode
& S_IFMT
) {
89 err
= ovl_do_create(dir
, newdentry
, stat
->mode
, debug
);
93 err
= ovl_do_mkdir(dir
, newdentry
, stat
->mode
, debug
);
100 err
= ovl_do_mknod(dir
, newdentry
,
101 stat
->mode
, stat
->rdev
, debug
);
105 err
= ovl_do_symlink(dir
, newdentry
, link
, debug
);
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.
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
)
131 err
= ovl_do_removexattr(upperdentry
, OVL_XATTR_OPAQUE
);
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
,
142 enum ovl_path_type type
;
143 struct path realpath
;
145 type
= ovl_path_real(dentry
, &realpath
);
146 err
= vfs_getattr(&realpath
, stat
);
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
158 if (OVL_TYPE_MERGE(type
))
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
;
173 mutex_lock_nested(&udir
->i_mutex
, I_MUTEX_PARENT
);
174 newdentry
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
176 err
= PTR_ERR(newdentry
);
177 if (IS_ERR(newdentry
))
179 err
= ovl_create_real(udir
, newdentry
, stat
, link
, hardlink
, false);
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
);
191 mutex_unlock(&udir
->i_mutex
);
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
)
202 /* Workdir should not be subdir of upperdir and vice versa */
203 if (lock_rename(workdir
, upperdir
) != NULL
)
209 unlock_rename(workdir
, upperdir
);
211 pr_err("overlayfs: failed to lock workdir+upperdir\n");
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
;
228 if (WARN_ON(!workdir
))
229 return ERR_PTR(-EROFS
);
231 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
235 ovl_path_upper(dentry
, &upperpath
);
236 err
= vfs_getattr(&upperpath
, &stat
);
241 if (!S_ISDIR(stat
.mode
))
243 upper
= upperpath
.dentry
;
244 if (upper
->d_parent
->d_inode
!= udir
)
247 opaquedir
= ovl_lookup_temp(workdir
, dentry
);
248 err
= PTR_ERR(opaquedir
);
249 if (IS_ERR(opaquedir
))
252 err
= ovl_create_real(wdir
, opaquedir
, &stat
, NULL
, NULL
, true);
256 err
= ovl_copy_xattr(upper
, opaquedir
);
260 err
= ovl_set_opaque(opaquedir
);
264 mutex_lock(&opaquedir
->d_inode
->i_mutex
);
265 err
= ovl_set_attr(opaquedir
, &stat
);
266 mutex_unlock(&opaquedir
->d_inode
->i_mutex
);
270 err
= ovl_do_rename(wdir
, opaquedir
, udir
, upper
, RENAME_EXCHANGE
);
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 */
284 ovl_cleanup(wdir
, opaquedir
);
288 unlock_rename(workdir
, upperdir
);
293 static struct dentry
*ovl_check_empty_and_clear(struct dentry
*dentry
)
296 struct dentry
*ret
= NULL
;
299 err
= ovl_check_empty_dir(dentry
, &list
);
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
);
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
;
331 if (WARN_ON(!workdir
))
334 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
338 newdentry
= ovl_lookup_temp(workdir
, dentry
);
339 err
= PTR_ERR(newdentry
);
340 if (IS_ERR(newdentry
))
343 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
345 err
= PTR_ERR(upper
);
349 err
= ovl_create_real(wdir
, newdentry
, stat
, link
, hardlink
, true);
353 if (S_ISDIR(stat
->mode
)) {
354 err
= ovl_set_opaque(newdentry
);
358 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
,
363 ovl_cleanup(wdir
, upper
);
365 err
= ovl_do_rename(wdir
, newdentry
, udir
, upper
, 0);
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
);
379 unlock_rename(workdir
, upperdir
);
384 ovl_cleanup(wdir
, newdentry
);
388 static int ovl_create_or_link(struct dentry
*dentry
, int mode
, dev_t rdev
,
389 const char *link
, struct dentry
*hardlink
)
393 struct kstat stat
= {
399 inode
= ovl_new_inode(dentry
->d_sb
, mode
, dentry
->d_fsdata
);
403 err
= ovl_copy_up(dentry
->d_parent
);
407 if (!ovl_dentry_is_opaque(dentry
)) {
408 err
= ovl_create_upper(dentry
, inode
, &stat
, link
, hardlink
);
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
,
417 revert_creds(old_cred
);
428 static int ovl_create_object(struct dentry
*dentry
, int mode
, dev_t rdev
,
433 err
= ovl_want_write(dentry
);
435 err
= ovl_create_or_link(dentry
, mode
, rdev
, link
, NULL
);
436 ovl_drop_write(dentry
);
442 static int ovl_create(struct inode
*dir
, struct dentry
*dentry
, umode_t mode
,
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
,
456 /* Don't allow creation of "whiteout" on overlay */
457 if (S_ISCHR(mode
) && rdev
== WHITEOUT_DEV
)
460 return ovl_create_object(dentry
, mode
, rdev
, NULL
);
463 static int ovl_symlink(struct inode
*dir
, struct dentry
*dentry
,
466 return ovl_create_object(dentry
, S_IFLNK
, 0, link
);
469 static int ovl_link(struct dentry
*old
, struct inode
*newdir
,
473 struct dentry
*upper
;
475 err
= ovl_want_write(old
);
479 err
= ovl_copy_up(old
);
483 upper
= ovl_dentry_upper(old
);
484 err
= ovl_create_or_link(new, upper
->d_inode
->i_mode
, 0, NULL
, upper
);
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
;
504 if (WARN_ON(!workdir
))
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
))
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
);
528 err
= ovl_lock_rename_workdir(workdir
, upperdir
);
532 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
534 err
= PTR_ERR(upper
);
539 if ((opaquedir
&& upper
!= opaquedir
) ||
540 (!opaquedir
&& ovl_dentry_upper(dentry
) &&
541 upper
!= ovl_dentry_upper(dentry
))) {
545 whiteout
= ovl_whiteout(workdir
, dentry
);
546 err
= PTR_ERR(whiteout
);
547 if (IS_ERR(whiteout
))
551 flags
= RENAME_EXCHANGE
;
553 err
= ovl_do_rename(wdir
, whiteout
, udir
, upper
, flags
);
557 ovl_cleanup(wdir
, upper
);
559 ovl_dentry_version_inc(dentry
->d_parent
);
566 unlock_rename(workdir
, upperdir
);
573 ovl_cleanup(wdir
, whiteout
);
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
;
584 mutex_lock_nested(&dir
->i_mutex
, I_MUTEX_PARENT
);
585 upper
= lookup_one_len(dentry
->d_name
.name
, upperdir
,
587 err
= PTR_ERR(upper
);
592 if (upper
== ovl_dentry_upper(dentry
)) {
594 err
= vfs_rmdir(dir
, upper
);
596 err
= vfs_unlink(dir
, upper
, NULL
);
597 ovl_dentry_version_inc(dentry
->d_parent
);
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
610 mutex_unlock(&dir
->i_mutex
);
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
))
626 static int ovl_do_remove(struct dentry
*dentry
, bool is_dir
)
628 enum ovl_path_type type
;
631 err
= ovl_check_sticky(dentry
);
635 err
= ovl_want_write(dentry
);
639 err
= ovl_copy_up(dentry
->d_parent
);
643 type
= ovl_path_type(dentry
);
644 if (OVL_TYPE_PURE_UPPER(type
)) {
645 err
= ovl_remove_upper(dentry
, is_dir
);
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
);
654 ovl_drop_write(dentry
);
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,
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
;
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
;
692 if (flags
& ~(RENAME_EXCHANGE
| RENAME_NOREPLACE
))
695 flags
&= ~RENAME_NOREPLACE
;
697 err
= ovl_check_sticky(old
);
701 /* Don't copy up directory trees */
702 old_type
= ovl_path_type(old
);
704 if (OVL_TYPE_MERGE_OR_LOWER(old_type
) && is_dir
)
708 err
= ovl_check_sticky(new);
715 new_type
= ovl_path_type(new);
717 if (!overwrite
&& OVL_TYPE_MERGE_OR_LOWER(new_type
) && new_is_dir
)
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
)
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
)
732 if (ovl_dentry_is_opaque(new))
733 new_type
= __OVL_PATH_UPPER
;
735 new_type
= __OVL_PATH_UPPER
| __OVL_PATH_PURE
;
738 err
= ovl_want_write(old
);
742 err
= ovl_copy_up(old
);
746 err
= ovl_copy_up(new->d_parent
);
750 err
= ovl_copy_up(new);
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
)) {
766 goto out_revert_creds
;
772 if (new->d_inode
|| !new_opaque
) {
773 /* Whiteout source */
774 flags
|= RENAME_WHITEOUT
;
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
,
793 err
= PTR_ERR(olddentry
);
794 if (IS_ERR(olddentry
))
798 if (olddentry
!= ovl_dentry_upper(old
))
801 newdentry
= lookup_one_len(new->d_name
.name
, new_upperdir
,
803 err
= PTR_ERR(newdentry
);
804 if (IS_ERR(newdentry
))
808 if (ovl_dentry_upper(new)) {
810 if (newdentry
!= opaquedir
)
813 if (newdentry
!= ovl_dentry_upper(new))
818 if (!d_is_negative(newdentry
) &&
819 (!new_opaque
|| !ovl_is_whiteout(newdentry
)))
823 if (olddentry
== trap
)
825 if (newdentry
== trap
)
828 if (is_dir
&& !old_opaque
&& new_opaque
) {
829 err
= ovl_set_opaque(olddentry
);
833 if (!overwrite
&& new_is_dir
&& old_opaque
&& !new_opaque
) {
834 err
= ovl_set_opaque(newdentry
);
839 if (old_opaque
|| new_opaque
) {
840 err
= ovl_do_rename(old_upperdir
->d_inode
, olddentry
,
841 new_upperdir
->d_inode
, newdentry
,
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
,
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
);
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
);
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
);
888 unlock_rename(new_upperdir
, old_upperdir
);
890 if (old_opaque
|| new_opaque
)
891 revert_creds(old_cred
);
899 const struct inode_operations ovl_dir_inode_operations
= {
900 .lookup
= ovl_lookup
,
902 .symlink
= ovl_symlink
,
903 .unlink
= ovl_unlink
,
905 .rename2
= ovl_rename2
,
907 .setattr
= ovl_setattr
,
908 .create
= ovl_create
,
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
,