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/posix_acl.h>
16 #include <linux/posix_acl_xattr.h>
17 #include "overlayfs.h"
19 void ovl_cleanup(struct inode *wdir, struct dentry *wdentry)
24 if (d_is_dir(wdentry))
25 err = ovl_do_rmdir(wdir, wdentry);
27 err = ovl_do_unlink(wdir, wdentry);
31 pr_err("overlayfs: cleanup of '%pd2' failed (%i)\n",
36 struct dentry *ovl_lookup_temp(struct dentry *workdir, struct dentry *dentry)
41 snprintf(name, sizeof(name), "#%lx", (unsigned long) dentry);
43 temp = lookup_one_len(name, workdir, strlen(name));
44 if (!IS_ERR(temp) && temp->d_inode) {
45 pr_err("overlayfs: workdir/%s already exists\n", name);
53 /* caller holds i_mutex on workdir */
54 static struct dentry *ovl_whiteout(struct dentry *workdir,
55 struct dentry *dentry)
58 struct dentry *whiteout;
59 struct inode *wdir = workdir->d_inode;
61 whiteout = ovl_lookup_temp(workdir, dentry);
65 err = ovl_do_whiteout(wdir, whiteout);
68 whiteout = ERR_PTR(err);
74 int ovl_create_real(struct inode *dir, struct dentry *newdentry,
75 struct kstat *stat, const char *link,
76 struct dentry *hardlink, bool debug)
80 if (newdentry->d_inode)
84 err = ovl_do_link(hardlink, dir, newdentry, debug);
86 switch (stat->mode & S_IFMT) {
88 err = ovl_do_create(dir, newdentry, stat->mode, debug);
92 err = ovl_do_mkdir(dir, newdentry, stat->mode, debug);
99 err = ovl_do_mknod(dir, newdentry,
100 stat->mode, stat->rdev, debug);
104 err = ovl_do_symlink(dir, newdentry, link, debug);
111 if (!err && WARN_ON(!newdentry->d_inode)) {
113 * Not quite sure if non-instantiated dentry is legal or not.
114 * VFS doesn't seem to care so check and warn here.
121 static int ovl_set_opaque(struct dentry *upperdentry)
123 return ovl_do_setxattr(upperdentry, OVL_XATTR_OPAQUE, "y", 1, 0);
126 static void ovl_remove_opaque(struct dentry *upperdentry)
130 err = ovl_do_removexattr(upperdentry, OVL_XATTR_OPAQUE);
132 pr_warn("overlayfs: failed to remove opaque from '%s' (%i)\n",
133 upperdentry->d_name.name, err);
137 static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
141 enum ovl_path_type type;
142 struct path realpath;
143 const struct cred *old_cred;
145 type = ovl_path_real(dentry, &realpath);
146 old_cred = ovl_override_creds(dentry->d_sb);
147 err = vfs_getattr(&realpath, stat);
148 revert_creds(old_cred);
152 stat->dev = dentry->d_sb->s_dev;
153 stat->ino = dentry->d_inode->i_ino;
156 * It's probably not worth it to count subdirs to get the
157 * correct link count. nlink=1 seems to pacify 'find' and
160 if (OVL_TYPE_MERGE(type))
166 /* Common operations required to be done after creation of file on upper */
167 static void ovl_instantiate(struct dentry *dentry, struct inode *inode,
168 struct dentry *newdentry, bool hardlink)
170 ovl_dentry_version_inc(dentry->d_parent);
171 ovl_dentry_update(dentry, newdentry);
173 ovl_inode_update(inode, d_inode(newdentry));
174 ovl_copyattr(newdentry->d_inode, inode);
176 WARN_ON(ovl_inode_real(inode, NULL) != d_inode(newdentry));
179 d_instantiate(dentry, inode);
182 static int ovl_create_upper(struct dentry *dentry, struct inode *inode,
183 struct kstat *stat, const char *link,
184 struct dentry *hardlink)
186 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
187 struct inode *udir = upperdir->d_inode;
188 struct dentry *newdentry;
191 if (!hardlink && !IS_POSIXACL(udir))
192 stat->mode &= ~current_umask();
194 inode_lock_nested(udir, I_MUTEX_PARENT);
195 newdentry = lookup_one_len(dentry->d_name.name, upperdir,
197 err = PTR_ERR(newdentry);
198 if (IS_ERR(newdentry))
200 err = ovl_create_real(udir, newdentry, stat, link, hardlink, false);
204 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
213 static int ovl_lock_rename_workdir(struct dentry *workdir,
214 struct dentry *upperdir)
216 /* Workdir should not be the same as upperdir */
217 if (workdir == upperdir)
220 /* Workdir should not be subdir of upperdir and vice versa */
221 if (lock_rename(workdir, upperdir) != NULL)
227 unlock_rename(workdir, upperdir);
229 pr_err("overlayfs: failed to lock workdir+upperdir\n");
233 static struct dentry *ovl_clear_empty(struct dentry *dentry,
234 struct list_head *list)
236 struct dentry *workdir = ovl_workdir(dentry);
237 struct inode *wdir = workdir->d_inode;
238 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
239 struct inode *udir = upperdir->d_inode;
240 struct path upperpath;
241 struct dentry *upper;
242 struct dentry *opaquedir;
246 if (WARN_ON(!workdir))
247 return ERR_PTR(-EROFS);
249 err = ovl_lock_rename_workdir(workdir, upperdir);
253 ovl_path_upper(dentry, &upperpath);
254 err = vfs_getattr(&upperpath, &stat);
259 if (!S_ISDIR(stat.mode))
261 upper = upperpath.dentry;
262 if (upper->d_parent->d_inode != udir)
265 opaquedir = ovl_lookup_temp(workdir, dentry);
266 err = PTR_ERR(opaquedir);
267 if (IS_ERR(opaquedir))
270 err = ovl_create_real(wdir, opaquedir, &stat, NULL, NULL, true);
274 err = ovl_copy_xattr(upper, opaquedir);
278 err = ovl_set_opaque(opaquedir);
282 inode_lock(opaquedir->d_inode);
283 err = ovl_set_attr(opaquedir, &stat);
284 inode_unlock(opaquedir->d_inode);
288 err = ovl_do_rename(wdir, opaquedir, udir, upper, RENAME_EXCHANGE);
292 ovl_cleanup_whiteouts(upper, list);
293 ovl_cleanup(wdir, upper);
294 unlock_rename(workdir, upperdir);
296 /* dentry's upper doesn't match now, get rid of it */
302 ovl_cleanup(wdir, opaquedir);
306 unlock_rename(workdir, upperdir);
311 static struct dentry *ovl_check_empty_and_clear(struct dentry *dentry)
314 struct dentry *ret = NULL;
315 enum ovl_path_type type = ovl_path_type(dentry);
318 err = ovl_check_empty_dir(dentry, &list);
325 * When removing an empty opaque directory, then it makes no sense to
326 * replace it with an exact replica of itself.
328 * If no upperdentry then skip clearing whiteouts.
330 * Can race with copy-up, since we don't hold the upperdir mutex.
331 * Doesn't matter, since copy-up can't create a non-empty directory
334 if (OVL_TYPE_UPPER(type) && OVL_TYPE_MERGE(type))
335 ret = ovl_clear_empty(dentry, &list);
338 ovl_cache_free(&list);
343 static int ovl_set_upper_acl(struct dentry *upperdentry, const char *name,
344 const struct posix_acl *acl)
350 if (!IS_ENABLED(CONFIG_FS_POSIX_ACL) || !acl)
353 size = posix_acl_to_xattr(NULL, acl, NULL, 0);
354 buffer = kmalloc(size, GFP_KERNEL);
358 size = posix_acl_to_xattr(&init_user_ns, acl, buffer, size);
363 err = vfs_setxattr(upperdentry, name, buffer, size, XATTR_CREATE);
369 static int ovl_create_over_whiteout(struct dentry *dentry, struct inode *inode,
370 struct kstat *stat, const char *link,
371 struct dentry *hardlink)
373 struct dentry *workdir = ovl_workdir(dentry);
374 struct inode *wdir = workdir->d_inode;
375 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
376 struct inode *udir = upperdir->d_inode;
377 struct dentry *upper;
378 struct dentry *newdentry;
380 struct posix_acl *acl, *default_acl;
382 if (WARN_ON(!workdir))
386 err = posix_acl_create(dentry->d_parent->d_inode,
387 &stat->mode, &default_acl, &acl);
392 err = ovl_lock_rename_workdir(workdir, upperdir);
396 newdentry = ovl_lookup_temp(workdir, dentry);
397 err = PTR_ERR(newdentry);
398 if (IS_ERR(newdentry))
401 upper = lookup_one_len(dentry->d_name.name, upperdir,
403 err = PTR_ERR(upper);
407 err = ovl_create_real(wdir, newdentry, stat, link, hardlink, true);
412 * mode could have been mutilated due to umask (e.g. sgid directory)
415 !S_ISLNK(stat->mode) && newdentry->d_inode->i_mode != stat->mode) {
416 struct iattr attr = {
417 .ia_valid = ATTR_MODE,
418 .ia_mode = stat->mode,
420 inode_lock(newdentry->d_inode);
421 err = notify_change(newdentry, &attr, NULL);
422 inode_unlock(newdentry->d_inode);
427 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_ACCESS,
432 err = ovl_set_upper_acl(newdentry, XATTR_NAME_POSIX_ACL_DEFAULT,
438 if (!hardlink && S_ISDIR(stat->mode)) {
439 err = ovl_set_opaque(newdentry);
443 err = ovl_do_rename(wdir, newdentry, udir, upper,
448 ovl_cleanup(wdir, upper);
450 err = ovl_do_rename(wdir, newdentry, udir, upper, 0);
454 ovl_instantiate(dentry, inode, newdentry, !!hardlink);
461 unlock_rename(workdir, upperdir);
464 posix_acl_release(acl);
465 posix_acl_release(default_acl);
470 ovl_cleanup(wdir, newdentry);
474 static int ovl_create_or_link(struct dentry *dentry, struct inode *inode,
475 struct kstat *stat, const char *link,
476 struct dentry *hardlink)
479 const struct cred *old_cred;
480 struct cred *override_cred;
482 err = ovl_copy_up(dentry->d_parent);
486 old_cred = ovl_override_creds(dentry->d_sb);
488 override_cred = prepare_creds();
490 override_cred->fsuid = inode->i_uid;
491 override_cred->fsgid = inode->i_gid;
492 put_cred(override_creds(override_cred));
493 put_cred(override_cred);
495 if (!ovl_dentry_is_opaque(dentry))
496 err = ovl_create_upper(dentry, inode, stat, link,
499 err = ovl_create_over_whiteout(dentry, inode, stat,
502 revert_creds(old_cred);
504 struct inode *realinode = d_inode(ovl_dentry_upper(dentry));
506 WARN_ON(inode->i_mode != realinode->i_mode);
507 WARN_ON(!uid_eq(inode->i_uid, realinode->i_uid));
508 WARN_ON(!gid_eq(inode->i_gid, realinode->i_gid));
513 static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
518 struct kstat stat = {
522 err = ovl_want_write(dentry);
527 inode = ovl_new_inode(dentry->d_sb, mode);
531 inode_init_owner(inode, dentry->d_parent->d_inode, mode);
532 stat.mode = inode->i_mode;
534 err = ovl_create_or_link(dentry, inode, &stat, link, NULL);
539 ovl_drop_write(dentry);
544 static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
547 return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
550 static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
552 return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
555 static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
558 /* Don't allow creation of "whiteout" on overlay */
559 if (S_ISCHR(mode) && rdev == WHITEOUT_DEV)
562 return ovl_create_object(dentry, mode, rdev, NULL);
565 static int ovl_symlink(struct inode *dir, struct dentry *dentry,
568 return ovl_create_object(dentry, S_IFLNK, 0, link);
571 static int ovl_link(struct dentry *old, struct inode *newdir,
577 err = ovl_want_write(old);
581 err = ovl_copy_up(old);
585 inode = d_inode(old);
588 err = ovl_create_or_link(new, inode, NULL, NULL, ovl_dentry_upper(old));
598 static int ovl_remove_and_whiteout(struct dentry *dentry, bool is_dir)
600 struct dentry *workdir = ovl_workdir(dentry);
601 struct inode *wdir = workdir->d_inode;
602 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
603 struct inode *udir = upperdir->d_inode;
604 struct dentry *whiteout;
605 struct dentry *upper;
606 struct dentry *opaquedir = NULL;
610 if (WARN_ON(!workdir))
614 opaquedir = ovl_check_empty_and_clear(dentry);
615 err = PTR_ERR(opaquedir);
616 if (IS_ERR(opaquedir))
620 err = ovl_lock_rename_workdir(workdir, upperdir);
624 upper = lookup_one_len(dentry->d_name.name, upperdir,
626 err = PTR_ERR(upper);
631 if ((opaquedir && upper != opaquedir) ||
632 (!opaquedir && ovl_dentry_upper(dentry) &&
633 upper != ovl_dentry_upper(dentry))) {
637 whiteout = ovl_whiteout(workdir, dentry);
638 err = PTR_ERR(whiteout);
639 if (IS_ERR(whiteout))
643 flags = RENAME_EXCHANGE;
645 err = ovl_do_rename(wdir, whiteout, udir, upper, flags);
649 ovl_cleanup(wdir, upper);
651 ovl_dentry_version_inc(dentry->d_parent);
658 unlock_rename(workdir, upperdir);
665 ovl_cleanup(wdir, whiteout);
669 static int ovl_remove_upper(struct dentry *dentry, bool is_dir)
671 struct dentry *upperdir = ovl_dentry_upper(dentry->d_parent);
672 struct inode *dir = upperdir->d_inode;
673 struct dentry *upper;
676 inode_lock_nested(dir, I_MUTEX_PARENT);
677 upper = lookup_one_len(dentry->d_name.name, upperdir,
679 err = PTR_ERR(upper);
684 if (upper == ovl_dentry_upper(dentry)) {
686 err = vfs_rmdir(dir, upper);
688 err = vfs_unlink(dir, upper, NULL);
689 ovl_dentry_version_inc(dentry->d_parent);
694 * Keeping this dentry hashed would mean having to release
695 * upperpath/lowerpath, which could only be done if we are the
696 * sole user of this dentry. Too tricky... Just unhash for
707 static inline int ovl_check_sticky(struct dentry *dentry)
709 struct inode *dir = ovl_dentry_real(dentry->d_parent)->d_inode;
710 struct inode *inode = ovl_dentry_real(dentry)->d_inode;
712 if (check_sticky(dir, inode))
718 static int ovl_do_remove(struct dentry *dentry, bool is_dir)
720 enum ovl_path_type type;
722 const struct cred *old_cred;
725 err = ovl_check_sticky(dentry);
729 err = ovl_want_write(dentry);
733 err = ovl_copy_up(dentry->d_parent);
737 type = ovl_path_type(dentry);
739 old_cred = ovl_override_creds(dentry->d_sb);
740 if (OVL_TYPE_PURE_UPPER(type))
741 err = ovl_remove_upper(dentry, is_dir);
743 err = ovl_remove_and_whiteout(dentry, is_dir);
744 revert_creds(old_cred);
747 clear_nlink(dentry->d_inode);
749 drop_nlink(dentry->d_inode);
752 ovl_drop_write(dentry);
757 static int ovl_unlink(struct inode *dir, struct dentry *dentry)
759 return ovl_do_remove(dentry, false);
762 static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
764 return ovl_do_remove(dentry, true);
767 static int ovl_rename2(struct inode *olddir, struct dentry *old,
768 struct inode *newdir, struct dentry *new,
772 enum ovl_path_type old_type;
773 enum ovl_path_type new_type;
774 struct dentry *old_upperdir;
775 struct dentry *new_upperdir;
776 struct dentry *olddentry;
777 struct dentry *newdentry;
781 bool cleanup_whiteout = false;
782 bool overwrite = !(flags & RENAME_EXCHANGE);
783 bool is_dir = d_is_dir(old);
784 bool new_is_dir = false;
785 struct dentry *opaquedir = NULL;
786 const struct cred *old_cred = NULL;
789 if (flags & ~(RENAME_EXCHANGE | RENAME_NOREPLACE))
792 flags &= ~RENAME_NOREPLACE;
794 err = ovl_check_sticky(old);
798 /* Don't copy up directory trees */
799 old_type = ovl_path_type(old);
801 if (OVL_TYPE_MERGE_OR_LOWER(old_type) && is_dir)
805 err = ovl_check_sticky(new);
812 new_type = ovl_path_type(new);
814 if (!overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir)
818 if (!OVL_TYPE_UPPER(new_type) && !OVL_TYPE_UPPER(old_type)) {
819 if (ovl_dentry_lower(old)->d_inode ==
820 ovl_dentry_lower(new)->d_inode)
823 if (OVL_TYPE_UPPER(new_type) && OVL_TYPE_UPPER(old_type)) {
824 if (ovl_dentry_upper(old)->d_inode ==
825 ovl_dentry_upper(new)->d_inode)
829 if (ovl_dentry_is_opaque(new))
830 new_type = __OVL_PATH_UPPER;
832 new_type = __OVL_PATH_UPPER | __OVL_PATH_PURE;
835 err = ovl_want_write(old);
839 err = ovl_copy_up(old);
843 err = ovl_copy_up(new->d_parent);
847 err = ovl_copy_up(new);
852 old_opaque = !OVL_TYPE_PURE_UPPER(old_type);
853 new_opaque = !OVL_TYPE_PURE_UPPER(new_type);
855 old_cred = ovl_override_creds(old->d_sb);
857 if (overwrite && OVL_TYPE_MERGE_OR_LOWER(new_type) && new_is_dir) {
858 opaquedir = ovl_check_empty_and_clear(new);
859 err = PTR_ERR(opaquedir);
860 if (IS_ERR(opaquedir)) {
862 goto out_revert_creds;
868 if (new->d_inode || !new_opaque) {
869 /* Whiteout source */
870 flags |= RENAME_WHITEOUT;
872 /* Switch whiteouts */
873 flags |= RENAME_EXCHANGE;
875 } else if (is_dir && !new->d_inode && new_opaque) {
876 flags |= RENAME_EXCHANGE;
877 cleanup_whiteout = true;
881 old_upperdir = ovl_dentry_upper(old->d_parent);
882 new_upperdir = ovl_dentry_upper(new->d_parent);
884 trap = lock_rename(new_upperdir, old_upperdir);
887 olddentry = lookup_one_len(old->d_name.name, old_upperdir,
889 err = PTR_ERR(olddentry);
890 if (IS_ERR(olddentry))
894 if (olddentry != ovl_dentry_upper(old))
897 newdentry = lookup_one_len(new->d_name.name, new_upperdir,
899 err = PTR_ERR(newdentry);
900 if (IS_ERR(newdentry))
904 if (ovl_dentry_upper(new)) {
906 if (newdentry != opaquedir)
909 if (newdentry != ovl_dentry_upper(new))
913 if (!d_is_negative(newdentry) &&
914 (!new_opaque || !ovl_is_whiteout(newdentry)))
918 if (olddentry == trap)
920 if (newdentry == trap)
923 if (is_dir && !old_opaque && new_opaque) {
924 err = ovl_set_opaque(olddentry);
928 if (!overwrite && new_is_dir && old_opaque && !new_opaque) {
929 err = ovl_set_opaque(newdentry);
934 if (old_opaque || new_opaque) {
935 err = ovl_do_rename(old_upperdir->d_inode, olddentry,
936 new_upperdir->d_inode, newdentry,
939 /* No debug for the plain case */
940 BUG_ON(flags & ~RENAME_EXCHANGE);
941 err = vfs_rename(old_upperdir->d_inode, olddentry,
942 new_upperdir->d_inode, newdentry,
947 if (is_dir && !old_opaque && new_opaque)
948 ovl_remove_opaque(olddentry);
949 if (!overwrite && new_is_dir && old_opaque && !new_opaque)
950 ovl_remove_opaque(newdentry);
954 if (is_dir && old_opaque && !new_opaque)
955 ovl_remove_opaque(olddentry);
956 if (!overwrite && new_is_dir && !old_opaque && new_opaque)
957 ovl_remove_opaque(newdentry);
960 * Old dentry now lives in different location. Dentries in
961 * lowerstack are stale. We cannot drop them here because
962 * access to them is lockless. This could be only pure upper
963 * or opaque directory - numlower is zero. Or upper non-dir
964 * entry - its pureness is tracked by flag opaque.
966 if (old_opaque != new_opaque) {
967 ovl_dentry_set_opaque(old, new_opaque);
969 ovl_dentry_set_opaque(new, old_opaque);
972 if (cleanup_whiteout)
973 ovl_cleanup(old_upperdir->d_inode, newdentry);
975 ovl_dentry_version_inc(old->d_parent);
976 ovl_dentry_version_inc(new->d_parent);
983 unlock_rename(new_upperdir, old_upperdir);
985 revert_creds(old_cred);
993 const struct inode_operations ovl_dir_inode_operations = {
994 .lookup = ovl_lookup,
996 .symlink = ovl_symlink,
997 .unlink = ovl_unlink,
999 .rename2 = ovl_rename2,
1001 .setattr = ovl_setattr,
1002 .create = ovl_create,
1004 .permission = ovl_permission,
1005 .getattr = ovl_dir_getattr,
1006 .setxattr = generic_setxattr,
1007 .getxattr = generic_getxattr,
1008 .listxattr = ovl_listxattr,
1009 .removexattr = generic_removexattr,
1010 .get_acl = ovl_get_acl,
1011 .update_time = ovl_update_time,