3 @@ -666,8 +666,7 @@ static inline int __get_file_write_acces
7 -static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
9 +static struct file *__dentry_open(struct path *path, struct file *f,
10 int (*open)(struct inode *, struct file *),
11 const struct cred *cred)
13 @@ -675,15 +674,16 @@ static struct file *__dentry_open(struct
18 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
19 FMODE_PREAD | FMODE_PWRITE;
21 if (unlikely(f->f_flags & O_PATH))
22 f->f_mode = FMODE_PATH;
24 - inode = dentry->d_inode;
25 + inode = path->dentry->d_inode;
26 if (f->f_mode & FMODE_WRITE) {
27 - error = __get_file_write_access(inode, mnt);
28 + error = __get_file_write_access(inode, path->mnt);
31 if (!special_file(inode->i_mode))
32 @@ -691,8 +691,7 @@ static struct file *__dentry_open(struct
35 f->f_mapping = inode->i_mapping;
36 - f->f_path.dentry = dentry;
37 - f->f_path.mnt = mnt;
40 file_sb_list_add(f, inode->i_sb);
42 @@ -745,7 +744,7 @@ cleanup_all:
43 * here, so just reset the state.
46 - mnt_drop_write(mnt);
47 + mnt_drop_write(path->mnt);
51 @@ -753,8 +752,7 @@ cleanup_all:
58 return ERR_PTR(error);
61 @@ -780,14 +778,14 @@ cleanup_file:
62 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
63 int (*open)(struct inode *, struct file *))
65 + struct path path = { .dentry = dentry, .mnt = nd->path.mnt };
66 const struct cred *cred = current_cred();
68 if (IS_ERR(nd->intent.open.file))
72 - nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
73 - nd->intent.open.file,
74 + nd->intent.open.file = __dentry_open(&path, nd->intent.open.file,
77 return nd->intent.open.file;
78 @@ -816,10 +814,17 @@ struct file *nameidata_to_filp(struct na
80 /* Has the filesystem initialised the file for us? */
81 if (filp->f_path.dentry == NULL) {
82 - path_get(&nd->path);
83 - filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
85 + struct inode *inode = nd->path.dentry->d_inode;
87 + if (inode->i_op->open) {
88 + int flags = filp->f_flags;
90 + filp = inode->i_op->open(nd->path.dentry, flags, cred);
92 + filp = __dentry_open(&nd->path, filp, NULL, cred);
99 @@ -830,26 +835,45 @@ struct file *nameidata_to_filp(struct na
100 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
101 const struct cred *cred)
106 - validate_creds(cred);
107 + struct path path = { .dentry = dentry, .mnt = mnt };
110 /* We must always pass in a valid mount pointer. */
114 + ret = vfs_open(&path, flags, cred);
119 +EXPORT_SYMBOL(dentry_open);
122 + * vfs_open - open the file at the given path
123 + * @path: path to open
124 + * @flags: open flags
125 + * @cred: credentials to use
127 + * Open the file. If successful, the returned file will have acquired
128 + * an additional reference for path.
130 +struct file *vfs_open(struct path *path, int flags, const struct cred *cred)
133 + struct inode *inode = path->dentry->d_inode;
135 + validate_creds(cred);
137 + if (inode->i_op->open)
138 + return inode->i_op->open(path->dentry, flags, cred);
139 f = get_empty_filp();
143 - return ERR_PTR(error);
146 + return ERR_PTR(-ENFILE);
149 - return __dentry_open(dentry, mnt, f, NULL, cred);
150 + return __dentry_open(path, f, NULL, cred);
152 -EXPORT_SYMBOL(dentry_open);
153 +EXPORT_SYMBOL(vfs_open);
155 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
157 --- a/include/linux/fs.h
158 +++ b/include/linux/fs.h
159 @@ -1603,6 +1603,7 @@ struct inode_operations {
160 void (*truncate_range)(struct inode *, loff_t, loff_t);
161 int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
163 + struct file *(*open)(struct dentry *, int flags, const struct cred *);
164 } ____cacheline_aligned;
167 @@ -1997,6 +1998,7 @@ extern long do_sys_open(int dfd, const c
168 extern struct file *filp_open(const char *, int, int);
169 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
171 +extern struct file *vfs_open(struct path *, int flags, const struct cred *);
172 extern struct file * dentry_open(struct dentry *, struct vfsmount *, int,
173 const struct cred *);
174 extern int filp_close(struct file *, fl_owner_t id);
177 @@ -1300,6 +1300,7 @@ long do_splice_direct(struct file *in, l
181 +EXPORT_SYMBOL(do_splice_direct);
183 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
184 struct pipe_inode_info *opipe,
187 @@ -1495,6 +1495,23 @@ void drop_collected_mounts(struct vfsmou
188 release_mounts(&umount_list);
191 +struct vfsmount *clone_private_mount(struct path *path)
193 + struct vfsmount *mnt;
195 + if (IS_MNT_UNBINDABLE(path->mnt))
196 + return ERR_PTR(-EINVAL);
198 + down_read(&namespace_sem);
199 + mnt = clone_mnt(path->mnt, path->dentry, CL_PRIVATE);
200 + up_read(&namespace_sem);
202 + return ERR_PTR(-ENOMEM);
206 +EXPORT_SYMBOL_GPL(clone_private_mount);
208 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
209 struct vfsmount *root)
211 --- a/include/linux/mount.h
212 +++ b/include/linux/mount.h
213 @@ -100,6 +100,9 @@ extern void mnt_pin(struct vfsmount *mnt
214 extern void mnt_unpin(struct vfsmount *mnt);
215 extern int __mnt_is_readonly(struct vfsmount *mnt);
218 +extern struct vfsmount *clone_private_mount(struct path *path);
220 extern struct vfsmount *do_kern_mount(const char *fstype, int flags,
221 const char *name, void *data);
225 @@ -63,6 +63,7 @@ source "fs/quota/Kconfig"
227 source "fs/autofs4/Kconfig"
228 source "fs/fuse/Kconfig"
229 +source "fs/overlayfs/Kconfig"
232 tristate "Character device in Userspace support"
235 @@ -105,6 +105,7 @@ obj-$(CONFIG_QNX4FS_FS) += qnx4/
236 obj-$(CONFIG_AUTOFS4_FS) += autofs4/
237 obj-$(CONFIG_ADFS_FS) += adfs/
238 obj-$(CONFIG_FUSE_FS) += fuse/
239 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
240 obj-$(CONFIG_UDF_FS) += udf/
241 obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
242 obj-$(CONFIG_OMFS_FS) += omfs/
244 +++ b/fs/overlayfs/Kconfig
247 + tristate "Overlay filesystem support"
249 + Add support for overlay filesystem.
251 +++ b/fs/overlayfs/Makefile
254 +# Makefile for the overlay filesystem.
257 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
259 +overlayfs-objs := super.o inode.o dir.o readdir.o copy_up.o
261 +++ b/fs/overlayfs/copy_up.c
265 + * Copyright (C) 2011 Novell Inc.
267 + * This program is free software; you can redistribute it and/or modify it
268 + * under the terms of the GNU General Public License version 2 as published by
269 + * the Free Software Foundation.
272 +#include <linux/fs.h>
273 +#include <linux/slab.h>
274 +#include <linux/file.h>
275 +#include <linux/splice.h>
276 +#include <linux/xattr.h>
277 +#include <linux/security.h>
278 +#include <linux/uaccess.h>
279 +#include "overlayfs.h"
281 +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
283 +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
285 + ssize_t list_size, size;
286 + char *buf, *name, *value;
289 + if (!old->d_inode->i_op->getxattr ||
290 + !new->d_inode->i_op->getxattr)
293 + list_size = vfs_listxattr(old, NULL, 0);
294 + if (list_size <= 0) {
295 + if (list_size == -EOPNOTSUPP)
300 + buf = kzalloc(list_size, GFP_KERNEL);
305 + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
309 + list_size = vfs_listxattr(old, buf, list_size);
310 + if (list_size <= 0) {
312 + goto out_free_value;
315 + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
316 + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
319 + goto out_free_value;
321 + error = vfs_setxattr(new, name, value, size, 0);
323 + goto out_free_value;
333 +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
335 + struct file *old_file;
336 + struct file *new_file;
342 + old_file = vfs_open(old, O_RDONLY, current_cred());
343 + if (IS_ERR(old_file))
344 + return PTR_ERR(old_file);
346 + new_file = vfs_open(new, O_WRONLY, current_cred());
347 + if (IS_ERR(new_file)) {
348 + error = PTR_ERR(new_file);
352 + /* FIXME: copy up sparse files efficiently */
354 + loff_t offset = new_file->f_pos;
355 + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
358 + if (len < this_len)
361 + if (signal_pending_state(TASK_KILLABLE, current)) {
366 + bytes = do_splice_direct(old_file, &offset, new_file, this_len,
382 +static char *ovl_read_symlink(struct dentry *realdentry)
386 + struct inode *inode = realdentry->d_inode;
387 + mm_segment_t old_fs;
390 + if (!inode->i_op->readlink)
394 + buf = (char *) __get_free_page(GFP_KERNEL);
400 + /* The cast to a user pointer is valid due to the set_fs() */
401 + res = inode->i_op->readlink(realdentry,
402 + (char __user *)buf, PAGE_SIZE - 1);
405 + free_page((unsigned long) buf);
413 + return ERR_PTR(res);
416 +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
418 + struct iattr attr = {
419 + .ia_valid = ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
420 + .ia_atime = stat->atime,
421 + .ia_mtime = stat->mtime,
424 + return notify_change(upperdentry, &attr);
427 +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
429 + struct iattr attr = {
430 + .ia_valid = ATTR_MODE,
434 + return notify_change(upperdentry, &attr);
437 +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
438 + struct path *lowerpath, struct kstat *stat,
442 + struct path newpath;
443 + umode_t mode = stat->mode;
445 + /* Can't properly set mode on creation because of the umask */
446 + stat->mode &= S_IFMT;
448 + ovl_path_upper(dentry, &newpath);
449 + WARN_ON(newpath.dentry);
450 + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
451 + if (IS_ERR(newpath.dentry))
452 + return PTR_ERR(newpath.dentry);
454 + if (S_ISREG(stat->mode)) {
455 + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
460 + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
464 + mutex_lock(&newpath.dentry->d_inode->i_mutex);
465 + if (!S_ISLNK(stat->mode))
466 + err = ovl_set_mode(newpath.dentry, mode);
468 + err = ovl_set_timestamps(newpath.dentry, stat);
469 + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
473 + ovl_dentry_update(dentry, newpath.dentry);
476 + * Easiest way to get rid of the lower dentry reference is to
477 + * drop this dentry. This is neither needed nor possible for
480 + if (!S_ISDIR(stat->mode))
486 + if (S_ISDIR(stat->mode))
487 + vfs_rmdir(upperdir->d_inode, newpath.dentry);
489 + vfs_unlink(upperdir->d_inode, newpath.dentry);
491 + dput(newpath.dentry);
497 + * Copy up a single dentry
499 + * Directory renames only allowed on "pure upper" (already created on
500 + * upper filesystem, never copied up). Directories which are on lower or
501 + * are merged may not be renamed. For these -EXDEV is returned and
502 + * userspace has to deal with it. This means, when copying up a
503 + * directory we can rely on it and ancestors being stable.
505 + * Non-directory renames start with copy up of source if necessary. The
506 + * actual rename will only proceed once the copy up was successful. Copy
507 + * up uses upper parent i_mutex for exclusion. Since rename can change
508 + * d_parent it is possible that the copy up will lock the old parent. At
509 + * that point the file will have already been copied up anyway.
511 +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
512 + struct path *lowerpath, struct kstat *stat)
515 + struct kstat pstat;
516 + struct path parentpath;
517 + struct dentry *upperdir;
518 + const struct cred *old_cred;
519 + struct cred *override_cred;
522 + ovl_path_upper(parent, &parentpath);
523 + upperdir = parentpath.dentry;
525 + err = vfs_getattr(parentpath.mnt, parentpath.dentry, &pstat);
529 + if (S_ISLNK(stat->mode)) {
530 + link = ovl_read_symlink(lowerpath->dentry);
532 + return PTR_ERR(link);
536 + override_cred = prepare_creds();
537 + if (!override_cred)
538 + goto out_free_link;
540 + override_cred->fsuid = stat->uid;
541 + override_cred->fsgid = stat->gid;
543 + * CAP_SYS_ADMIN for copying up extended attributes
544 + * CAP_DAC_OVERRIDE for create
545 + * CAP_FOWNER for chmod, timestamp update
546 + * CAP_FSETID for chmod
547 + * CAP_MKNOD for mknod
549 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
550 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
551 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
552 + cap_raise(override_cred->cap_effective, CAP_FSETID);
553 + cap_raise(override_cred->cap_effective, CAP_MKNOD);
554 + old_cred = override_creds(override_cred);
556 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
557 + if (ovl_path_type(dentry) != OVL_PATH_LOWER) {
560 + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
563 + /* Restore timestamps on parent (best effort) */
564 + ovl_set_timestamps(upperdir, &pstat);
568 + mutex_unlock(&upperdir->d_inode->i_mutex);
570 + revert_creds(old_cred);
571 + put_cred(override_cred);
575 + free_page((unsigned long) link);
580 +int ovl_copy_up(struct dentry *dentry)
586 + struct dentry *next;
587 + struct dentry *parent;
588 + struct path lowerpath;
590 + enum ovl_path_type type = ovl_path_type(dentry);
592 + if (type != OVL_PATH_LOWER)
595 + next = dget(dentry);
596 + /* find the topmost dentry not yet copied up */
598 + parent = dget_parent(next);
600 + type = ovl_path_type(parent);
601 + if (type != OVL_PATH_LOWER)
608 + ovl_path_lower(next, &lowerpath);
609 + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat);
611 + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
620 +/* Optimize by not copying up the file first and truncating later */
621 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
625 + struct path lowerpath;
626 + struct dentry *parent = dget_parent(dentry);
628 + err = ovl_copy_up(parent);
630 + goto out_dput_parent;
632 + ovl_path_lower(dentry, &lowerpath);
633 + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat);
635 + goto out_dput_parent;
637 + if (size < stat.size)
640 + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
647 +++ b/fs/overlayfs/dir.c
651 + * Copyright (C) 2011 Novell Inc.
653 + * This program is free software; you can redistribute it and/or modify it
654 + * under the terms of the GNU General Public License version 2 as published by
655 + * the Free Software Foundation.
658 +#include <linux/fs.h>
659 +#include <linux/namei.h>
660 +#include <linux/xattr.h>
661 +#include <linux/security.h>
662 +#include "overlayfs.h"
664 +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
666 +static struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
667 + struct nameidata *nd)
669 + int err = ovl_do_lookup(dentry);
672 + return ERR_PTR(err);
677 +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
680 + struct dentry *newdentry;
681 + const struct cred *old_cred;
682 + struct cred *override_cred;
684 + /* FIXME: recheck lower dentry to see if whiteout is really needed */
687 + override_cred = prepare_creds();
688 + if (!override_cred)
692 + * CAP_SYS_ADMIN for setxattr
693 + * CAP_DAC_OVERRIDE for symlink creation
694 + * CAP_FOWNER for unlink in sticky directory
696 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
697 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
698 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
699 + override_cred->fsuid = 0;
700 + override_cred->fsgid = 0;
701 + old_cred = override_creds(override_cred);
703 + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
704 + dentry->d_name.len);
705 + err = PTR_ERR(newdentry);
706 + if (IS_ERR(newdentry))
709 + /* Just been removed within the same locked region */
710 + WARN_ON(newdentry->d_inode);
712 + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
716 + ovl_dentry_version_inc(dentry->d_parent);
718 + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
720 + vfs_unlink(upperdir->d_inode, newdentry);
725 + revert_creds(old_cred);
726 + put_cred(override_cred);
730 + * There's no way to recover from failure to whiteout.
731 + * What should we do? Log a big fat error and... ?
733 + printk(KERN_ERR "overlayfs: ERROR - failed to whiteout '%s'\n",
734 + dentry->d_name.name);
740 +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
741 + struct dentry *template)
744 + struct dentry *newdentry;
745 + struct qstr *name = &template->d_name;
747 + newdentry = lookup_one_len(name->name, upperdir, name->len);
748 + if (IS_ERR(newdentry))
751 + if (newdentry->d_inode) {
752 + const struct cred *old_cred;
753 + struct cred *override_cred;
755 + /* No need to check whiteout if lower parent is non-existent */
757 + if (!ovl_dentry_lower(template->d_parent))
760 + if (!S_ISLNK(newdentry->d_inode->i_mode))
764 + override_cred = prepare_creds();
765 + if (!override_cred)
769 + * CAP_SYS_ADMIN for getxattr
770 + * CAP_FOWNER for unlink in sticky directory
772 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
773 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
774 + old_cred = override_creds(override_cred);
777 + if (ovl_is_whiteout(newdentry))
778 + err = vfs_unlink(upperdir->d_inode, newdentry);
780 + revert_creds(old_cred);
781 + put_cred(override_cred);
786 + newdentry = lookup_one_len(name->name, upperdir, name->len);
787 + if (IS_ERR(newdentry)) {
788 + ovl_whiteout(upperdir, template);
793 + * Whiteout just been successfully removed, parent
794 + * i_mutex is still held, there's no way the lookup
795 + * could return positive.
797 + WARN_ON(newdentry->d_inode);
804 + return ERR_PTR(err);
807 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
808 + struct kstat *stat, const char *link)
811 + struct dentry *newdentry;
812 + struct inode *dir = upperdir->d_inode;
814 + newdentry = ovl_lookup_create(upperdir, dentry);
815 + if (IS_ERR(newdentry))
818 + switch (stat->mode & S_IFMT) {
820 + err = vfs_create(dir, newdentry, stat->mode, NULL);
824 + err = vfs_mkdir(dir, newdentry, stat->mode);
831 + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
835 + err = vfs_symlink(dir, newdentry, link);
842 + if (ovl_dentry_is_opaque(dentry))
843 + ovl_whiteout(upperdir, dentry);
845 + newdentry = ERR_PTR(err);
846 + } else if (WARN_ON(!newdentry->d_inode)) {
848 + * Not quite sure if non-instantiated dentry is legal or not.
849 + * VFS doesn't seem to care so check and warn here.
852 + newdentry = ERR_PTR(-ENOENT);
860 +static int ovl_set_opaque(struct dentry *upperdentry)
863 + const struct cred *old_cred;
864 + struct cred *override_cred;
866 + override_cred = prepare_creds();
867 + if (!override_cred)
870 + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
871 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
872 + old_cred = override_creds(override_cred);
873 + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
874 + revert_creds(old_cred);
875 + put_cred(override_cred);
880 +static int ovl_remove_opaque(struct dentry *upperdentry)
883 + const struct cred *old_cred;
884 + struct cred *override_cred;
886 + override_cred = prepare_creds();
887 + if (!override_cred)
890 + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
891 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
892 + old_cred = override_creds(override_cred);
893 + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
894 + revert_creds(old_cred);
895 + put_cred(override_cred);
900 +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
901 + struct kstat *stat)
904 + enum ovl_path_type type;
905 + struct path realpath;
907 + type = ovl_path_real(dentry, &realpath);
908 + err = vfs_getattr(realpath.mnt, realpath.dentry, stat);
912 + stat->dev = dentry->d_sb->s_dev;
913 + stat->ino = dentry->d_inode->i_ino;
916 + * It's probably not worth it to count subdirs to get the
917 + * correct link count. nlink=1 seems to pacify 'find' and
920 + if (type == OVL_PATH_MERGE)
926 +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
930 + struct dentry *newdentry;
931 + struct dentry *upperdir;
932 + struct inode *inode;
933 + struct kstat stat = {
939 + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
943 + err = ovl_copy_up(dentry->d_parent);
947 + upperdir = ovl_dentry_upper(dentry->d_parent);
948 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
950 + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
951 + err = PTR_ERR(newdentry);
952 + if (IS_ERR(newdentry))
955 + ovl_dentry_version_inc(dentry->d_parent);
956 + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
957 + err = ovl_set_opaque(newdentry);
959 + vfs_rmdir(upperdir->d_inode, newdentry);
960 + ovl_whiteout(upperdir, dentry);
964 + ovl_dentry_update(dentry, newdentry);
965 + d_instantiate(dentry, inode);
973 + mutex_unlock(&upperdir->d_inode->i_mutex);
980 +static int ovl_create(struct inode *dir, struct dentry *dentry, int mode,
981 + struct nameidata *nd)
983 + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
986 +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, int mode)
988 + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
991 +static int ovl_mknod(struct inode *dir, struct dentry *dentry, int mode,
994 + return ovl_create_object(dentry, mode, rdev, NULL);
997 +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
1000 + return ovl_create_object(dentry, S_IFLNK, 0, link);
1003 +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
1006 + enum ovl_path_type type;
1007 + struct path realpath;
1008 + struct dentry *upperdir;
1010 + err = ovl_copy_up(dentry->d_parent);
1014 + upperdir = ovl_dentry_upper(dentry->d_parent);
1015 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1016 + type = ovl_path_real(dentry, &realpath);
1017 + if (type != OVL_PATH_LOWER) {
1019 + if (realpath.dentry->d_parent != upperdir)
1022 + /* FIXME: create whiteout up front and rename to target */
1025 + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
1027 + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
1031 + ovl_dentry_version_inc(dentry->d_parent);
1034 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1035 + err = ovl_whiteout(upperdir, dentry);
1038 + * Keeping this dentry hashed would mean having to release
1039 + * upperpath/lowerpath, which could only be done if we are the
1040 + * sole user of this dentry. Too tricky... Just unhash for
1045 + mutex_unlock(&upperdir->d_inode->i_mutex);
1050 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1052 + return ovl_do_remove(dentry, false);
1056 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
1059 + enum ovl_path_type type;
1061 + type = ovl_path_type(dentry);
1062 + if (type != OVL_PATH_UPPER) {
1063 + err = ovl_check_empty_and_clear(dentry, type);
1068 + return ovl_do_remove(dentry, true);
1071 +static int ovl_link(struct dentry *old, struct inode *newdir,
1072 + struct dentry *new)
1075 + struct dentry *olddentry;
1076 + struct dentry *newdentry;
1077 + struct dentry *upperdir;
1079 + err = ovl_copy_up(old);
1083 + err = ovl_copy_up(new->d_parent);
1087 + upperdir = ovl_dentry_upper(new->d_parent);
1088 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1089 + newdentry = ovl_lookup_create(upperdir, new);
1090 + err = PTR_ERR(newdentry);
1091 + if (IS_ERR(newdentry))
1094 + olddentry = ovl_dentry_upper(old);
1095 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
1097 + if (WARN_ON(!newdentry->d_inode)) {
1103 + ovl_dentry_version_inc(new->d_parent);
1104 + ovl_dentry_update(new, newdentry);
1106 + ihold(old->d_inode);
1107 + d_instantiate(new, old->d_inode);
1109 + if (ovl_dentry_is_opaque(new))
1110 + ovl_whiteout(upperdir, new);
1114 + mutex_unlock(&upperdir->d_inode->i_mutex);
1120 +static int ovl_rename(struct inode *olddir, struct dentry *old,
1121 + struct inode *newdir, struct dentry *new)
1124 + enum ovl_path_type old_type;
1125 + enum ovl_path_type new_type;
1126 + struct dentry *old_upperdir;
1127 + struct dentry *new_upperdir;
1128 + struct dentry *olddentry;
1129 + struct dentry *newdentry;
1130 + struct dentry *trap;
1133 + bool new_create = false;
1134 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
1136 + /* Don't copy up directory trees */
1137 + old_type = ovl_path_type(old);
1138 + if (old_type != OVL_PATH_UPPER && is_dir)
1141 + if (new->d_inode) {
1142 + new_type = ovl_path_type(new);
1144 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
1145 + if (ovl_dentry_lower(old)->d_inode ==
1146 + ovl_dentry_lower(new)->d_inode)
1149 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
1150 + if (ovl_dentry_upper(old)->d_inode ==
1151 + ovl_dentry_upper(new)->d_inode)
1155 + if (new_type != OVL_PATH_UPPER &&
1156 + S_ISDIR(new->d_inode->i_mode)) {
1157 + err = ovl_check_empty_and_clear(new, new_type);
1162 + new_type = OVL_PATH_UPPER;
1165 + err = ovl_copy_up(old);
1169 + err = ovl_copy_up(new->d_parent);
1173 + old_upperdir = ovl_dentry_upper(old->d_parent);
1174 + new_upperdir = ovl_dentry_upper(new->d_parent);
1176 + trap = lock_rename(new_upperdir, old_upperdir);
1178 + olddentry = ovl_dentry_upper(old);
1179 + newdentry = ovl_dentry_upper(new);
1183 + new_create = true;
1184 + newdentry = ovl_lookup_create(new_upperdir, new);
1185 + err = PTR_ERR(newdentry);
1186 + if (IS_ERR(newdentry))
1191 + if (olddentry->d_parent != old_upperdir)
1193 + if (newdentry->d_parent != new_upperdir)
1195 + if (olddentry == trap)
1197 + if (newdentry == trap)
1200 + old_opaque = ovl_dentry_is_opaque(old);
1201 + new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
1203 + if (is_dir && !old_opaque && new_opaque) {
1204 + err = ovl_set_opaque(olddentry);
1209 + err = vfs_rename(old_upperdir->d_inode, olddentry,
1210 + new_upperdir->d_inode, newdentry);
1213 + if (new_create && ovl_dentry_is_opaque(new))
1214 + ovl_whiteout(new_upperdir, new);
1215 + if (is_dir && !old_opaque && new_opaque)
1216 + ovl_remove_opaque(olddentry);
1220 + if (old_type != OVL_PATH_UPPER || old_opaque)
1221 + err = ovl_whiteout(old_upperdir, old);
1222 + if (is_dir && old_opaque && !new_opaque)
1223 + ovl_remove_opaque(olddentry);
1225 + if (old_opaque != new_opaque)
1226 + ovl_dentry_set_opaque(old, new_opaque);
1228 + ovl_dentry_version_inc(old->d_parent);
1229 + ovl_dentry_version_inc(new->d_parent);
1234 + unlock_rename(new_upperdir, old_upperdir);
1238 +const struct inode_operations ovl_dir_inode_operations = {
1239 + .lookup = ovl_lookup,
1240 + .mkdir = ovl_mkdir,
1241 + .symlink = ovl_symlink,
1242 + .unlink = ovl_unlink,
1243 + .rmdir = ovl_rmdir,
1244 + .rename = ovl_rename,
1246 + .setattr = ovl_setattr,
1247 + .create = ovl_create,
1248 + .mknod = ovl_mknod,
1249 + .permission = ovl_permission,
1250 + .getattr = ovl_dir_getattr,
1251 + .setxattr = ovl_setxattr,
1252 + .getxattr = ovl_getxattr,
1253 + .listxattr = ovl_listxattr,
1254 + .removexattr = ovl_removexattr,
1257 +++ b/fs/overlayfs/inode.c
1261 + * Copyright (C) 2011 Novell Inc.
1263 + * This program is free software; you can redistribute it and/or modify it
1264 + * under the terms of the GNU General Public License version 2 as published by
1265 + * the Free Software Foundation.
1268 +#include <linux/fs.h>
1269 +#include <linux/slab.h>
1270 +#include <linux/xattr.h>
1271 +#include "overlayfs.h"
1273 +int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1275 + struct dentry *upperdentry;
1278 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1279 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1281 + err = ovl_copy_up(dentry);
1285 + upperdentry = ovl_dentry_upper(dentry);
1287 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1288 + attr->ia_valid &= ~ATTR_MODE;
1290 + mutex_lock(&upperdentry->d_inode->i_mutex);
1291 + err = notify_change(upperdentry, attr);
1292 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1297 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1298 + struct kstat *stat)
1300 + struct path realpath;
1302 + ovl_path_real(dentry, &realpath);
1303 + return vfs_getattr(realpath.mnt, realpath.dentry, stat);
1306 +int ovl_permission(struct inode *inode, int mask, unsigned int flags)
1308 + struct ovl_entry *oe;
1309 + struct dentry *alias = NULL;
1310 + struct inode *realinode;
1311 + struct dentry *realdentry;
1315 + if (S_ISDIR(inode->i_mode)) {
1316 + oe = inode->i_private;
1317 + } else if (flags & IPERM_FLAG_RCU) {
1321 + * For non-directories find an alias and get the info
1324 + spin_lock(&inode->i_lock);
1325 + if (WARN_ON(list_empty(&inode->i_dentry))) {
1326 + spin_unlock(&inode->i_lock);
1329 + alias = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1331 + spin_unlock(&inode->i_lock);
1332 + oe = alias->d_fsdata;
1335 + realdentry = ovl_entry_real(oe, &is_upper);
1337 + /* Careful in RCU walk mode */
1338 + realinode = ACCESS_ONCE(realdentry->d_inode);
1340 + WARN_ON(!(flags & IPERM_FLAG_RCU));
1345 + if (mask & MAY_WRITE) {
1346 + umode_t mode = realinode->i_mode;
1349 + * Writes will always be redirected to upper layer, so
1350 + * ignore lower layer being read-only.
1353 + if (is_upper && IS_RDONLY(realinode) &&
1354 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1358 + * Nobody gets write access to an immutable file.
1361 + if (IS_IMMUTABLE(realinode))
1365 + if (realinode->i_op->permission)
1366 + err = realinode->i_op->permission(realinode, mask, flags);
1368 + err = generic_permission(realinode, mask, flags,
1369 + realinode->i_op->check_acl);
1376 +struct ovl_link_data {
1377 + struct dentry *realdentry;
1381 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1384 + struct dentry *realdentry;
1385 + struct inode *realinode;
1387 + realdentry = ovl_dentry_real(dentry);
1388 + realinode = realdentry->d_inode;
1390 + if (WARN_ON(!realinode->i_op->follow_link))
1391 + return ERR_PTR(-EPERM);
1393 + ret = realinode->i_op->follow_link(realdentry, nd);
1397 + if (realinode->i_op->put_link) {
1398 + struct ovl_link_data *data;
1400 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1402 + realinode->i_op->put_link(realdentry, nd, ret);
1403 + return ERR_PTR(-ENOMEM);
1405 + data->realdentry = realdentry;
1406 + data->cookie = ret;
1414 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1416 + struct inode *realinode;
1417 + struct ovl_link_data *data = c;
1422 + realinode = data->realdentry->d_inode;
1423 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1427 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1429 + struct path realpath;
1430 + struct inode *realinode;
1432 + ovl_path_real(dentry, &realpath);
1433 + realinode = realpath.dentry->d_inode;
1435 + if (!realinode->i_op->readlink)
1438 + touch_atime(realpath.mnt, realpath.dentry);
1440 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1444 +static bool ovl_is_private_xattr(const char *name)
1446 + return strncmp(name, "trusted.overlay.", 14) == 0;
1449 +int ovl_setxattr(struct dentry *dentry, const char *name,
1450 + const void *value, size_t size, int flags)
1453 + struct dentry *upperdentry;
1455 + if (ovl_is_private_xattr(name))
1458 + err = ovl_copy_up(dentry);
1462 + upperdentry = ovl_dentry_upper(dentry);
1463 + return vfs_setxattr(upperdentry, name, value, size, flags);
1466 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1467 + void *value, size_t size)
1469 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1470 + ovl_is_private_xattr(name))
1473 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
1476 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
1481 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
1482 + if (res <= 0 || size == 0)
1485 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
1488 + /* filter out private xattrs */
1489 + for (off = 0; off < res;) {
1490 + char *s = list + off;
1491 + size_t slen = strlen(s) + 1;
1493 + BUG_ON(off + slen > res);
1495 + if (ovl_is_private_xattr(s)) {
1497 + memmove(s, s + slen, res - off);
1506 +int ovl_removexattr(struct dentry *dentry, const char *name)
1509 + struct path realpath;
1510 + enum ovl_path_type type;
1512 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1513 + ovl_is_private_xattr(name))
1516 + type = ovl_path_real(dentry, &realpath);
1517 + if (type == OVL_PATH_LOWER) {
1518 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
1522 + err = ovl_copy_up(dentry);
1526 + ovl_path_upper(dentry, &realpath);
1529 + return vfs_removexattr(realpath.dentry, name);
1532 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
1533 + struct dentry *realdentry)
1535 + if (type != OVL_PATH_LOWER)
1538 + if (special_file(realdentry->d_inode->i_mode))
1541 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
1547 +static struct file *ovl_open(struct dentry *dentry, int flags,
1548 + const struct cred *cred)
1551 + struct path realpath;
1552 + enum ovl_path_type type;
1554 + type = ovl_path_real(dentry, &realpath);
1555 + if (ovl_open_need_copy_up(flags, type, realpath.dentry)) {
1556 + if (flags & O_TRUNC)
1557 + err = ovl_copy_up_truncate(dentry, 0);
1559 + err = ovl_copy_up(dentry);
1561 + return ERR_PTR(err);
1563 + ovl_path_upper(dentry, &realpath);
1566 + return vfs_open(&realpath, flags, cred);
1569 +static const struct inode_operations ovl_file_inode_operations = {
1570 + .setattr = ovl_setattr,
1571 + .permission = ovl_permission,
1572 + .getattr = ovl_getattr,
1573 + .setxattr = ovl_setxattr,
1574 + .getxattr = ovl_getxattr,
1575 + .listxattr = ovl_listxattr,
1576 + .removexattr = ovl_removexattr,
1580 +static const struct inode_operations ovl_symlink_inode_operations = {
1581 + .setattr = ovl_setattr,
1582 + .follow_link = ovl_follow_link,
1583 + .put_link = ovl_put_link,
1584 + .readlink = ovl_readlink,
1585 + .getattr = ovl_getattr,
1586 + .setxattr = ovl_setxattr,
1587 + .getxattr = ovl_getxattr,
1588 + .listxattr = ovl_listxattr,
1589 + .removexattr = ovl_removexattr,
1592 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1593 + struct ovl_entry *oe)
1595 + struct inode *inode;
1597 + inode = new_inode(sb);
1603 + inode->i_ino = get_next_ino();
1604 + inode->i_mode = mode;
1605 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
1609 + inode->i_private = oe;
1610 + inode->i_op = &ovl_dir_inode_operations;
1611 + inode->i_fop = &ovl_dir_operations;
1615 + inode->i_op = &ovl_symlink_inode_operations;
1623 + inode->i_op = &ovl_file_inode_operations;
1627 + WARN(1, "illegal file type: %i\n", mode);
1635 +++ b/fs/overlayfs/overlayfs.h
1639 + * Copyright (C) 2011 Novell Inc.
1641 + * This program is free software; you can redistribute it and/or modify it
1642 + * under the terms of the GNU General Public License version 2 as published by
1643 + * the Free Software Foundation.
1648 +enum ovl_path_type {
1654 +extern const char *ovl_opaque_xattr;
1655 +extern const char *ovl_whiteout_xattr;
1656 +extern const struct dentry_operations ovl_dentry_operations;
1658 +enum ovl_path_type ovl_path_type(struct dentry *dentry);
1659 +u64 ovl_dentry_version_get(struct dentry *dentry);
1660 +void ovl_dentry_version_inc(struct dentry *dentry);
1661 +void ovl_path_upper(struct dentry *dentry, struct path *path);
1662 +void ovl_path_lower(struct dentry *dentry, struct path *path);
1663 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1664 +struct dentry *ovl_dentry_upper(struct dentry *dentry);
1665 +struct dentry *ovl_dentry_lower(struct dentry *dentry);
1666 +struct dentry *ovl_dentry_real(struct dentry *dentry);
1667 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
1668 +bool ovl_dentry_is_opaque(struct dentry *dentry);
1669 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
1670 +bool ovl_is_whiteout(struct dentry *dentry);
1671 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
1672 +int ovl_do_lookup(struct dentry *dentry);
1674 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
1675 + struct kstat *stat, const char *link);
1678 +extern const struct file_operations ovl_dir_operations;
1679 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
1682 +int ovl_setattr(struct dentry *dentry, struct iattr *attr);
1683 +int ovl_permission(struct inode *inode, int mask, unsigned int flags);
1684 +int ovl_setxattr(struct dentry *dentry, const char *name,
1685 + const void *value, size_t size, int flags);
1686 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1687 + void *value, size_t size);
1688 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
1689 +int ovl_removexattr(struct dentry *dentry, const char *name);
1691 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1692 + struct ovl_entry *oe);
1694 +extern const struct inode_operations ovl_dir_inode_operations;
1697 +int ovl_copy_up(struct dentry *dentry);
1698 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
1700 +++ b/fs/overlayfs/readdir.c
1704 + * Copyright (C) 2011 Novell Inc.
1706 + * This program is free software; you can redistribute it and/or modify it
1707 + * under the terms of the GNU General Public License version 2 as published by
1708 + * the Free Software Foundation.
1711 +#include <linux/fs.h>
1712 +#include <linux/slab.h>
1713 +#include <linux/namei.h>
1714 +#include <linux/file.h>
1715 +#include <linux/xattr.h>
1716 +#include <linux/rbtree.h>
1717 +#include <linux/security.h>
1718 +#include "overlayfs.h"
1720 +struct ovl_cache_entry {
1723 + unsigned int type;
1726 + struct list_head l_node;
1727 + struct rb_node node;
1730 +struct ovl_readdir_data {
1731 + struct rb_root *root;
1732 + struct list_head *list;
1733 + struct list_head *middle;
1734 + struct dentry *dir;
1739 +struct ovl_dir_file {
1742 + struct list_head cursor;
1743 + u64 cache_version;
1744 + struct list_head cache;
1745 + struct file *realfile;
1748 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
1750 + return container_of(n, struct ovl_cache_entry, node);
1753 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
1754 + const char *name, int len)
1756 + struct rb_node *node = root->rb_node;
1760 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
1762 + cmp = strncmp(name, p->name, len);
1764 + node = p->node.rb_right;
1765 + else if (cmp < 0 || len < p->len)
1766 + node = p->node.rb_left;
1774 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
1775 + u64 ino, unsigned int d_type)
1777 + struct ovl_cache_entry *p;
1779 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
1781 + char *name_copy = (char *) (p + 1);
1782 + memcpy(name_copy, name, len);
1783 + name_copy[len] = '\0';
1784 + p->name = name_copy;
1788 + p->is_whiteout = false;
1794 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
1795 + const char *name, int len, u64 ino,
1796 + unsigned int d_type)
1798 + struct rb_node **newp = &rdd->root->rb_node;
1799 + struct rb_node *parent = NULL;
1800 + struct ovl_cache_entry *p;
1804 + struct ovl_cache_entry *tmp;
1807 + tmp = ovl_cache_entry_from_node(*newp);
1808 + cmp = strncmp(name, tmp->name, len);
1810 + newp = &tmp->node.rb_right;
1811 + else if (cmp < 0 || len < tmp->len)
1812 + newp = &tmp->node.rb_left;
1817 + p = ovl_cache_entry_new(name, len, ino, d_type);
1821 + list_add_tail(&p->l_node, rdd->list);
1822 + rb_link_node(&p->node, parent, newp);
1823 + rb_insert_color(&p->node, rdd->root);
1828 +static int ovl_fill_lower(void *buf, const char *name, int namelen,
1829 + loff_t offset, u64 ino, unsigned int d_type)
1831 + struct ovl_readdir_data *rdd = buf;
1832 + struct ovl_cache_entry *p;
1835 + p = ovl_cache_entry_find(rdd->root, name, namelen);
1837 + list_move_tail(&p->l_node, rdd->middle);
1839 + p = ovl_cache_entry_new(name, namelen, ino, d_type);
1841 + rdd->err = -ENOMEM;
1843 + list_add_tail(&p->l_node, rdd->middle);
1849 +static void ovl_cache_free(struct list_head *list)
1851 + struct ovl_cache_entry *p;
1852 + struct ovl_cache_entry *n;
1854 + list_for_each_entry_safe(p, n, list, l_node)
1857 + INIT_LIST_HEAD(list);
1860 +static int ovl_fill_upper(void *buf, const char *name, int namelen,
1861 + loff_t offset, u64 ino, unsigned int d_type)
1863 + struct ovl_readdir_data *rdd = buf;
1866 + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
1869 +static int ovl_dir_read(struct path *realpath, struct ovl_readdir_data *rdd,
1872 + struct file *realfile;
1875 + realfile = vfs_open(realpath, O_RDONLY | O_DIRECTORY, current_cred());
1876 + if (IS_ERR(realfile))
1877 + return PTR_ERR(realfile);
1882 + err = vfs_readdir(realfile, filler, rdd);
1885 + } while (!err && rdd->count);
1891 +static void ovl_dir_reset(struct file *file)
1893 + struct ovl_dir_file *od = file->private_data;
1894 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
1896 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
1897 + list_del_init(&od->cursor);
1898 + ovl_cache_free(&od->cache);
1899 + od->is_cached = false;
1901 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
1902 + if (od->is_real && type == OVL_PATH_MERGE) {
1903 + fput(od->realfile);
1904 + od->realfile = NULL;
1905 + od->is_real = false;
1909 +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
1911 + struct ovl_cache_entry *p;
1912 + struct dentry *dentry;
1913 + const struct cred *old_cred;
1914 + struct cred *override_cred;
1916 + override_cred = prepare_creds();
1917 + if (!override_cred) {
1918 + ovl_cache_free(rdd->list);
1923 + * CAP_SYS_ADMIN for getxattr
1924 + * CAP_DAC_OVERRIDE for lookup
1926 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1927 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
1928 + old_cred = override_creds(override_cred);
1930 + mutex_lock(&rdd->dir->d_inode->i_mutex);
1931 + list_for_each_entry(p, rdd->list, l_node) {
1932 + if (p->type != DT_LNK)
1935 + dentry = lookup_one_len(p->name, rdd->dir, p->len);
1936 + if (IS_ERR(dentry))
1939 + p->is_whiteout = ovl_is_whiteout(dentry);
1942 + mutex_unlock(&rdd->dir->d_inode->i_mutex);
1944 + revert_creds(old_cred);
1945 + put_cred(override_cred);
1950 +static int ovl_dir_read_merged(struct path *upperpath, struct path *lowerpath,
1951 + struct ovl_readdir_data *rdd)
1954 + struct rb_root root = RB_ROOT;
1955 + struct list_head middle;
1957 + rdd->root = &root;
1958 + if (upperpath->dentry) {
1959 + rdd->dir = upperpath->dentry;
1960 + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
1964 + err = ovl_dir_mark_whiteouts(rdd);
1969 + * Insert lowerpath entries before upperpath ones, this allows
1970 + * offsets to be reasonably constant
1972 + list_add(&middle, rdd->list);
1973 + rdd->middle = &middle;
1974 + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower);
1975 + list_del(&middle);
1982 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
1984 + struct list_head *l;
1987 + l = od->cache.next;
1988 + for (off = 0; off < pos; off++) {
1989 + if (l == &od->cache)
1993 + list_move_tail(&od->cursor, l);
1996 +static int ovl_readdir(struct file *file, void *buf, filldir_t filler)
1998 + struct ovl_dir_file *od = file->private_data;
2002 + ovl_dir_reset(file);
2004 + if (od->is_real) {
2005 + res = vfs_readdir(od->realfile, filler, buf);
2006 + file->f_pos = od->realfile->f_pos;
2011 + if (!od->is_cached) {
2012 + struct path lowerpath;
2013 + struct path upperpath;
2014 + struct ovl_readdir_data rdd = { .list = &od->cache };
2016 + ovl_path_lower(file->f_path.dentry, &lowerpath);
2017 + ovl_path_upper(file->f_path.dentry, &upperpath);
2019 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2021 + ovl_cache_free(rdd.list);
2025 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
2026 + od->is_cached = true;
2028 + ovl_seek_cursor(od, file->f_pos);
2031 + while (od->cursor.next != &od->cache) {
2034 + struct ovl_cache_entry *p;
2036 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
2037 + off = file->f_pos;
2038 + if (!p->is_whiteout) {
2039 + over = filler(buf, p->name, p->len, off, p->ino, p->type);
2044 + list_move(&od->cursor, &p->l_node);
2050 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
2053 + struct ovl_dir_file *od = file->private_data;
2055 + mutex_lock(&file->f_dentry->d_inode->i_mutex);
2057 + ovl_dir_reset(file);
2059 + if (od->is_real) {
2060 + res = vfs_llseek(od->realfile, offset, origin);
2061 + file->f_pos = od->realfile->f_pos;
2067 + offset += file->f_pos;
2077 + if (offset != file->f_pos) {
2078 + file->f_pos = offset;
2079 + if (od->is_cached)
2080 + ovl_seek_cursor(od, offset);
2085 + mutex_unlock(&file->f_dentry->d_inode->i_mutex);
2090 +static int ovl_dir_fsync(struct file *file, int datasync)
2092 + struct ovl_dir_file *od = file->private_data;
2094 + /* May need to reopen directory if it got copied up */
2095 + if (!od->realfile) {
2096 + struct path upperpath;
2098 + ovl_path_upper(file->f_path.dentry, &upperpath);
2099 + od->realfile = vfs_open(&upperpath, O_RDONLY, current_cred());
2100 + if (IS_ERR(od->realfile))
2101 + return PTR_ERR(od->realfile);
2104 + return vfs_fsync(od->realfile, datasync);
2107 +static int ovl_dir_release(struct inode *inode, struct file *file)
2109 + struct ovl_dir_file *od = file->private_data;
2111 + list_del(&od->cursor);
2112 + ovl_cache_free(&od->cache);
2114 + fput(od->realfile);
2120 +static int ovl_dir_open(struct inode *inode, struct file *file)
2122 + struct path realpath;
2123 + struct file *realfile;
2124 + struct ovl_dir_file *od;
2125 + enum ovl_path_type type;
2127 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
2131 + type = ovl_path_real(file->f_path.dentry, &realpath);
2132 + realfile = vfs_open(&realpath, file->f_flags, current_cred());
2133 + if (IS_ERR(realfile)) {
2135 + return PTR_ERR(realfile);
2137 + INIT_LIST_HEAD(&od->cache);
2138 + INIT_LIST_HEAD(&od->cursor);
2139 + od->is_cached = false;
2140 + od->realfile = realfile;
2141 + od->is_real = (type != OVL_PATH_MERGE);
2142 + file->private_data = od;
2147 +const struct file_operations ovl_dir_operations = {
2148 + .read = generic_read_dir,
2149 + .open = ovl_dir_open,
2150 + .readdir = ovl_readdir,
2151 + .llseek = ovl_dir_llseek,
2152 + .fsync = ovl_dir_fsync,
2153 + .release = ovl_dir_release,
2156 +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
2159 + struct path lowerpath;
2160 + struct path upperpath;
2161 + struct ovl_cache_entry *p;
2162 + struct ovl_readdir_data rdd = { .list = list };
2164 + ovl_path_upper(dentry, &upperpath);
2165 + ovl_path_lower(dentry, &lowerpath);
2167 + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2173 + list_for_each_entry(p, list, l_node) {
2174 + if (p->is_whiteout)
2177 + if (p->name[0] == '.') {
2180 + if (p->len == 2 && p->name[1] == '.')
2190 +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
2192 + struct path upperpath;
2193 + struct dentry *upperdir;
2194 + struct ovl_cache_entry *p;
2195 + const struct cred *old_cred;
2196 + struct cred *override_cred;
2199 + ovl_path_upper(dir, &upperpath);
2200 + upperdir = upperpath.dentry;
2202 + override_cred = prepare_creds();
2203 + if (!override_cred)
2207 + * CAP_DAC_OVERRIDE for lookup and unlink
2208 + * CAP_SYS_ADMIN for setxattr of "trusted" namespace
2209 + * CAP_FOWNER for unlink in sticky directory
2211 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2212 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2213 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
2214 + old_cred = override_creds(override_cred);
2216 + err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
2218 + goto out_revert_creds;
2220 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2221 + list_for_each_entry(p, list, l_node) {
2222 + struct dentry *dentry;
2225 + if (!p->is_whiteout)
2228 + dentry = lookup_one_len(p->name, upperdir, p->len);
2229 + if (IS_ERR(dentry)) {
2230 + printk(KERN_WARNING "overlayfs: failed to lookup whiteout %.*s: %li\n", p->len, p->name, PTR_ERR(dentry));
2233 + ret = vfs_unlink(upperdir->d_inode, dentry);
2236 + printk(KERN_WARNING "overlayfs: failed to unlink whiteout %.*s: %i\n", p->len, p->name, ret);
2238 + mutex_unlock(&upperdir->d_inode->i_mutex);
2241 + revert_creds(old_cred);
2242 + put_cred(override_cred);
2247 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
2252 + err = ovl_check_empty_dir(dentry, &list);
2253 + if (!err && type == OVL_PATH_MERGE)
2254 + err = ovl_remove_whiteouts(dentry, &list);
2256 + ovl_cache_free(&list);
2261 +++ b/fs/overlayfs/super.c
2265 + * Copyright (C) 2011 Novell Inc.
2267 + * This program is free software; you can redistribute it and/or modify it
2268 + * under the terms of the GNU General Public License version 2 as published by
2269 + * the Free Software Foundation.
2272 +#include <linux/fs.h>
2273 +#include <linux/namei.h>
2274 +#include <linux/xattr.h>
2275 +#include <linux/security.h>
2276 +#include <linux/mount.h>
2277 +#include <linux/slab.h>
2278 +#include <linux/parser.h>
2279 +#include <linux/module.h>
2280 +#include <linux/seq_file.h>
2281 +#include "overlayfs.h"
2283 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2284 +MODULE_DESCRIPTION("Overlay filesystem");
2285 +MODULE_LICENSE("GPL");
2287 +struct ovl_config {
2292 +/* private information held for overlayfs's superblock */
2294 + struct vfsmount *upper_mnt;
2295 + struct vfsmount *lower_mnt;
2296 + /* pathnames of lower and upper dirs, for show_options */
2297 + struct ovl_config config;
2300 +/* private information held for every overlayfs dentry */
2303 + * Keep "double reference" on upper dentries, so that
2304 + * d_delete() doesn't think it's OK to reset d_inode to NULL.
2306 + struct dentry *__upperdentry;
2307 + struct dentry *lowerdentry;
2313 + struct rcu_head rcu;
2317 +const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
2318 +const char *ovl_opaque_xattr = "trusted.overlay.opaque";
2321 +enum ovl_path_type ovl_path_type(struct dentry *dentry)
2323 + struct ovl_entry *oe = dentry->d_fsdata;
2325 + if (oe->__upperdentry) {
2326 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
2327 + return OVL_PATH_MERGE;
2329 + return OVL_PATH_UPPER;
2331 + return OVL_PATH_LOWER;
2335 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
2337 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
2338 + smp_read_barrier_depends();
2339 + return upperdentry;
2342 +void ovl_path_upper(struct dentry *dentry, struct path *path)
2344 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2345 + struct ovl_entry *oe = dentry->d_fsdata;
2347 + path->mnt = ofs->upper_mnt;
2348 + path->dentry = ovl_upperdentry_dereference(oe);
2351 +void ovl_path_lower(struct dentry *dentry, struct path *path)
2353 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2354 + struct ovl_entry *oe = dentry->d_fsdata;
2356 + path->mnt = ofs->lower_mnt;
2357 + path->dentry = oe->lowerdentry;
2360 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
2363 + enum ovl_path_type type = ovl_path_type(dentry);
2365 + if (type == OVL_PATH_LOWER)
2366 + ovl_path_lower(dentry, path);
2368 + ovl_path_upper(dentry, path);
2373 +struct dentry *ovl_dentry_upper(struct dentry *dentry)
2375 + struct ovl_entry *oe = dentry->d_fsdata;
2377 + return ovl_upperdentry_dereference(oe);
2380 +struct dentry *ovl_dentry_lower(struct dentry *dentry)
2382 + struct ovl_entry *oe = dentry->d_fsdata;
2384 + return oe->lowerdentry;
2387 +struct dentry *ovl_dentry_real(struct dentry *dentry)
2389 + struct ovl_entry *oe = dentry->d_fsdata;
2390 + struct dentry *realdentry;
2392 + realdentry = ovl_upperdentry_dereference(oe);
2394 + realdentry = oe->lowerdentry;
2396 + return realdentry;
2399 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
2401 + struct dentry *realdentry;
2403 + realdentry = ovl_upperdentry_dereference(oe);
2407 + realdentry = oe->lowerdentry;
2408 + *is_upper = false;
2410 + return realdentry;
2413 +bool ovl_dentry_is_opaque(struct dentry *dentry)
2415 + struct ovl_entry *oe = dentry->d_fsdata;
2416 + return oe->opaque;
2419 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
2421 + struct ovl_entry *oe = dentry->d_fsdata;
2422 + oe->opaque = opaque;
2425 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
2427 + struct ovl_entry *oe = dentry->d_fsdata;
2429 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
2430 + WARN_ON(oe->__upperdentry);
2431 + BUG_ON(!upperdentry->d_inode);
2433 + oe->__upperdentry = dget(upperdentry);
2436 +void ovl_dentry_version_inc(struct dentry *dentry)
2438 + struct ovl_entry *oe = dentry->d_fsdata;
2440 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2444 +u64 ovl_dentry_version_get(struct dentry *dentry)
2446 + struct ovl_entry *oe = dentry->d_fsdata;
2448 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2449 + return oe->version;
2452 +bool ovl_is_whiteout(struct dentry *dentry)
2459 + if (!dentry->d_inode)
2461 + if (!S_ISLNK(dentry->d_inode->i_mode))
2464 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
2465 + if (res == 1 && val == 'y')
2471 +static bool ovl_is_opaquedir(struct dentry *dentry)
2476 + if (!S_ISDIR(dentry->d_inode->i_mode))
2479 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
2480 + if (res == 1 && val == 'y')
2486 +static void ovl_entry_free(struct rcu_head *head)
2488 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
2492 +static void ovl_dentry_release(struct dentry *dentry)
2494 + struct ovl_entry *oe = dentry->d_fsdata;
2497 + dput(oe->__upperdentry);
2498 + dput(oe->__upperdentry);
2499 + dput(oe->lowerdentry);
2500 + call_rcu(&oe->rcu, ovl_entry_free);
2504 +const struct dentry_operations ovl_dentry_operations = {
2505 + .d_release = ovl_dentry_release,
2508 +static struct ovl_entry *ovl_alloc_entry(void)
2510 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
2513 +static struct dentry *ovl_lookup_real(struct dentry *dir, struct qstr *name)
2515 + struct dentry *dentry;
2517 + mutex_lock(&dir->d_inode->i_mutex);
2518 + dentry = lookup_one_len(name->name, dir, name->len);
2519 + mutex_unlock(&dir->d_inode->i_mutex);
2521 + if (IS_ERR(dentry)) {
2522 + if (PTR_ERR(dentry) == -ENOENT)
2524 + } else if (!dentry->d_inode) {
2531 +int ovl_do_lookup(struct dentry *dentry)
2533 + struct ovl_entry *oe;
2534 + struct dentry *upperdir;
2535 + struct dentry *lowerdir;
2536 + struct dentry *upperdentry = NULL;
2537 + struct dentry *lowerdentry = NULL;
2538 + struct inode *inode = NULL;
2542 + oe = ovl_alloc_entry();
2546 + upperdir = ovl_dentry_upper(dentry->d_parent);
2547 + lowerdir = ovl_dentry_lower(dentry->d_parent);
2550 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
2551 + err = PTR_ERR(upperdentry);
2552 + if (IS_ERR(upperdentry))
2555 + if (lowerdir && upperdentry &&
2556 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
2557 + S_ISDIR(upperdentry->d_inode->i_mode))) {
2558 + const struct cred *old_cred;
2559 + struct cred *override_cred;
2562 + override_cred = prepare_creds();
2563 + if (!override_cred)
2564 + goto out_dput_upper;
2566 + /* CAP_SYS_ADMIN needed for getxattr */
2567 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2568 + old_cred = override_creds(override_cred);
2570 + if (ovl_is_opaquedir(upperdentry)) {
2571 + oe->opaque = true;
2572 + } else if (ovl_is_whiteout(upperdentry)) {
2573 + dput(upperdentry);
2574 + upperdentry = NULL;
2575 + oe->opaque = true;
2577 + revert_creds(old_cred);
2578 + put_cred(override_cred);
2581 + if (lowerdir && !oe->opaque) {
2582 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
2583 + err = PTR_ERR(lowerdentry);
2584 + if (IS_ERR(lowerdentry))
2585 + goto out_dput_upper;
2588 + if (lowerdentry && upperdentry &&
2589 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
2590 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
2591 + dput(lowerdentry);
2592 + lowerdentry = NULL;
2593 + oe->opaque = true;
2596 + if (lowerdentry || upperdentry) {
2597 + struct dentry *realdentry;
2599 + realdentry = upperdentry ? upperdentry : lowerdentry;
2601 + inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, oe);
2607 + oe->__upperdentry = dget(upperdentry);
2610 + oe->lowerdentry = lowerdentry;
2612 + dentry->d_fsdata = oe;
2613 + dentry->d_op = &ovl_dentry_operations;
2614 + d_add(dentry, inode);
2619 + dput(lowerdentry);
2621 + dput(upperdentry);
2628 +static void ovl_put_super(struct super_block *sb)
2630 + struct ovl_fs *ufs = sb->s_fs_info;
2632 + if (!(sb->s_flags & MS_RDONLY))
2633 + mnt_drop_write(ufs->upper_mnt);
2635 + mntput(ufs->upper_mnt);
2636 + mntput(ufs->lower_mnt);
2638 + kfree(ufs->config.lowerdir);
2639 + kfree(ufs->config.upperdir);
2643 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2645 + int flags = *flagsp;
2646 + struct ovl_fs *ufs = sb->s_fs_info;
2648 + /* When remounting rw or ro, we need to adjust the write access to the
2651 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2652 + /* No change to readonly status */
2655 + if (flags & MS_RDONLY) {
2656 + mnt_drop_write(ufs->upper_mnt);
2659 + return mnt_want_write(ufs->upper_mnt);
2664 + * @sb: The overlayfs super block
2665 + * @buf: The struct kstatfs to fill in with stats
2667 + * Get the filesystem statistics. As writes always target the upper layer
2668 + * filesystem pass the statfs to the same filesystem.
2670 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2672 + struct dentry *root_dentry = dentry->d_sb->s_root;
2674 + ovl_path_upper(root_dentry, &path);
2676 + if (!path.dentry->d_sb->s_op->statfs)
2678 + return path.dentry->d_sb->s_op->statfs(path.dentry, buf);
2682 + * ovl_show_options
2684 + * Prints the mount options for a given superblock.
2685 + * Returns zero; does not fail.
2687 +static int ovl_show_options(struct seq_file *m, struct vfsmount *mnt)
2689 + struct super_block *sb = mnt->mnt_sb;
2690 + struct ovl_fs *ufs = sb->s_fs_info;
2692 + seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
2693 + seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
2697 +static const struct super_operations ovl_super_operations = {
2698 + .put_super = ovl_put_super,
2699 + .remount_fs = ovl_remount_fs,
2700 + .statfs = ovl_statfs,
2701 + .show_options = ovl_show_options,
2710 +static const match_table_t ovl_tokens = {
2711 + {Opt_lowerdir, "lowerdir=%s"},
2712 + {Opt_upperdir, "upperdir=%s"},
2716 +static int ovl_parse_opt(char *opt, struct ovl_config *config)
2720 + config->upperdir = NULL;
2721 + config->lowerdir = NULL;
2723 + while ((p = strsep(&opt, ",")) != NULL) {
2725 + substring_t args[MAX_OPT_ARGS];
2730 + token = match_token(p, ovl_tokens, args);
2732 + case Opt_upperdir:
2733 + kfree(config->upperdir);
2734 + config->upperdir = match_strdup(&args[0]);
2735 + if (!config->upperdir)
2739 + case Opt_lowerdir:
2740 + kfree(config->lowerdir);
2741 + config->lowerdir = match_strdup(&args[0]);
2742 + if (!config->lowerdir)
2753 +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
2755 + struct path lowerpath;
2756 + struct path upperpath;
2757 + struct inode *root_inode;
2758 + struct dentry *root_dentry;
2759 + struct ovl_entry *oe;
2760 + struct ovl_fs *ufs;
2764 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
2768 + err = ovl_parse_opt((char *) data, &ufs->config);
2770 + goto out_free_ufs;
2773 + if (!ufs->config.upperdir || !ufs->config.lowerdir) {
2774 + printk(KERN_ERR "overlayfs: missing upperdir or lowerdir\n");
2775 + goto out_free_config;
2778 + oe = ovl_alloc_entry();
2780 + goto out_free_config;
2782 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
2786 + err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
2788 + goto out_put_root;
2790 + err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
2792 + goto out_put_upperpath;
2795 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
2796 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
2797 + goto out_put_lowerpath;
2799 + ufs->upper_mnt = clone_private_mount(&upperpath);
2800 + err = PTR_ERR(ufs->upper_mnt);
2801 + if (IS_ERR(ufs->upper_mnt)) {
2802 + printk(KERN_ERR "overlayfs: failed to clone upperpath\n");
2803 + goto out_put_lowerpath;
2806 + ufs->lower_mnt = clone_private_mount(&lowerpath);
2807 + err = PTR_ERR(ufs->lower_mnt);
2808 + if (IS_ERR(ufs->lower_mnt)) {
2809 + printk(KERN_ERR "overlayfs: failed to clone lowerpath\n");
2810 + goto out_put_upper_mnt;
2813 + if (!(sb->s_flags & MS_RDONLY)) {
2814 + err = mnt_want_write(ufs->upper_mnt);
2816 + goto out_put_lower_mnt;
2820 + root_dentry = d_alloc_root(root_inode);
2822 + goto out_drop_write;
2824 + mntput(upperpath.mnt);
2825 + mntput(lowerpath.mnt);
2827 + oe->__upperdentry = dget(upperpath.dentry);
2828 + oe->lowerdentry = lowerpath.dentry;
2830 + root_dentry->d_fsdata = oe;
2831 + root_dentry->d_op = &ovl_dentry_operations;
2833 + sb->s_op = &ovl_super_operations;
2834 + sb->s_root = root_dentry;
2835 + sb->s_fs_info = ufs;
2840 + if (!(sb->s_flags & MS_RDONLY))
2841 + mnt_drop_write(ufs->upper_mnt);
2843 + mntput(ufs->lower_mnt);
2845 + mntput(ufs->upper_mnt);
2847 + path_put(&lowerpath);
2849 + path_put(&upperpath);
2855 + kfree(ufs->config.lowerdir);
2856 + kfree(ufs->config.upperdir);
2863 +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
2864 + const char *dev_name, void *raw_data)
2866 + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
2869 +static struct file_system_type ovl_fs_type = {
2870 + .owner = THIS_MODULE,
2871 + .name = "overlayfs",
2872 + .mount = ovl_mount,
2873 + .kill_sb = kill_anon_super,
2876 +static int __init ovl_init(void)
2878 + return register_filesystem(&ovl_fs_type);
2881 +static void __exit ovl_exit(void)
2883 + unregister_filesystem(&ovl_fs_type);
2886 +module_init(ovl_init);
2887 +module_exit(ovl_exit);
2889 +++ b/Documentation/filesystems/overlayfs.txt
2891 +Written by: Neil Brown <neilb@suse.de>
2896 +This document describes a prototype for a new approach to providing
2897 +overlay-filesystem functionality in Linux (sometimes referred to as
2898 +union-filesystems). An overlay-filesystem tries to present a
2899 +filesystem which is the result over overlaying one filesystem on top
2902 +The result will inevitably fail to look exactly like a normal
2903 +filesystem for various technical reasons. The expectation is that
2904 +many use cases will be able to ignore these differences.
2906 +This approach is 'hybrid' because the objects that appear in the
2907 +filesystem do not all appear to belong to that filesystem. In many
2908 +cases an object accessed in the union will be indistinguishable
2909 +from accessing the corresponding object from the original filesystem.
2910 +This is most obvious from the 'st_dev' field returned by stat(2).
2912 +While directories will report an st_dev from the overlay-filesystem,
2913 +all non-directory objects will report an st_dev from the lower or
2914 +upper filesystem that is providing the object. Similarly st_ino will
2915 +only be unique when combined with st_dev, and both of these can change
2916 +over the lifetime of a non-directory object. Many applications and
2917 +tools ignore these values and will not be affected.
2922 +An overlay filesystem combines two filesystems - an 'upper' filesystem
2923 +and a 'lower' filesystem. When a name exists in both filesystems, the
2924 +object in the 'upper' filesystem is visible while the object in the
2925 +'lower' filesystem is either hidden or, in the case of directories,
2926 +merged with the 'upper' object.
2928 +It would be more correct to refer to an upper and lower 'directory
2929 +tree' rather than 'filesystem' as it is quite possible for both
2930 +directory trees to be in the same filesystem and there is no
2931 +requirement that the root of a filesystem be given for either upper or
2934 +The lower filesystem can be any filesystem supported by Linux and does
2935 +not need to be writable. The lower filesystem can even be another
2936 +overlayfs. The upper filesystem will normally be writable and if it
2937 +is it must support the creation of trusted.* extended attributes, and
2938 +must provide valid d_type in readdir responses, at least for symbolic
2939 +links - so NFS is not suitable.
2941 +A read-only overlay of two read-only filesystems may use any
2947 +Overlaying mainly involved directories. If a given name appears in both
2948 +upper and lower filesystems and refers to a non-directory in either,
2949 +then the lower object is hidden - the name refers only to the upper
2952 +Where both upper and lower objects are directories, a merged directory
2955 +At mount time, the two directories given as mount options are combined
2956 +into a merged directory:
2958 + mount -t overlayfs overlayfs -olowerdir=/lower,upperdir=/upper /overlay
2960 +Then whenever a lookup is requested in such a merged directory, the
2961 +lookup is performed in each actual directory and the combined result
2962 +is cached in the dentry belonging to the overlay filesystem. If both
2963 +actual lookups find directories, both are stored and a merged
2964 +directory is created, otherwise only one is stored: the upper if it
2965 +exists, else the lower.
2967 +Only the lists of names from directories are merged. Other content
2968 +such as metadata and extended attributes are reported for the upper
2969 +directory only. These attributes of the lower directory are hidden.
2971 +whiteouts and opaque directories
2972 +--------------------------------
2974 +In order to support rm and rmdir without changing the lower
2975 +filesystem, an overlay filesystem needs to record in the upper filesystem
2976 +that files have been removed. This is done using whiteouts and opaque
2977 +directories (non-directories are always opaque).
2979 +The overlay filesystem uses extended attributes with a
2980 +"trusted.overlay." prefix to record these details.
2982 +A whiteout is created as a symbolic link with target
2983 +"(overlay-whiteout)" and with xattr "trusted.overlay.whiteout" set to "y".
2984 +When a whiteout is found in the upper level of a merged directory, any
2985 +matching name in the lower level is ignored, and the whiteout itself
2988 +A directory is made opaque by setting the xattr "trusted.overlay.opaque"
2989 +to "y". Where the upper filesystem contains an opaque directory, any
2990 +directory in the lower filesystem with the same name is ignored.
2995 +When a 'readdir' request is made on a merged directory, the upper and
2996 +lower directories are each read and the name lists merged in the
2997 +obvious way (upper is read first, then lower - entries that already
2998 +exist are not re-added). This merged name list is cached in the
2999 +'struct file' and so remains as long as the file is kept open. If the
3000 +directory is opened and read by two processes at the same time, they
3001 +will each have separate caches. A seekdir to the start of the
3002 +directory (offset 0) followed by a readdir will cause the cache to be
3003 +discarded and rebuilt.
3005 +This means that changes to the merged directory do not appear while a
3006 +directory is being read. This is unlikely to be noticed by many
3009 +seek offsets are assigned sequentially when the directories are read.
3011 + - read part of a directory
3012 + - remember an offset, and close the directory
3013 + - re-open the directory some time later
3014 + - seek to the remembered offset
3016 +there may be little correlation between the old and new locations in
3017 +the list of filenames, particularly if anything has changed in the
3020 +Readdir on directories that are not merged is simply handled by the
3021 +underlying directory (upper or lower).
3027 +Objects that are not directories (files, symlinks, device-special
3028 +files etc.) are presented either from the upper or lower filesystem as
3029 +appropriate. When a file in the lower filesystem is accessed in a way
3030 +the requires write-access, such as opening for write access, changing
3031 +some metadata etc., the file is first copied from the lower filesystem
3032 +to the upper filesystem (copy_up). Note that creating a hard-link
3033 +also requires copy_up, though of course creation of a symlink does
3036 +The copy_up process first makes sure that the containing directory
3037 +exists in the upper filesystem - creating it and any parents as
3038 +necessary. It then creates the object with the same metadata (owner,
3039 +mode, mtime, symlink-target etc.) and then if the object is a file, the
3040 +data is copied from the lower to the upper filesystem. Finally any
3041 +extended attributes are copied up.
3043 +Once the copy_up is complete, the overlay filesystem simply
3044 +provides direct access to the newly created file in the upper
3045 +filesystem - future operations on the file are barely noticed by the
3046 +overlay filesystem (though an operation on the name of the file such as
3047 +rename or unlink will of course be noticed and handled).
3049 +Changes to underlying filesystems
3050 +---------------------------------
3052 +Offline changes, when the overlay is not mounted, are allowed to either
3053 +the upper or the lower trees.
3055 +Changes to the underlying filesystems while part of a mounted overlay
3056 +filesystem are not allowed. This is not yet enforced, but will be in
3060 @@ -4727,6 +4727,13 @@ F: drivers/scsi/osd/
3061 F: include/scsi/osd_*
3064 +OVERLAYFS FILESYSTEM
3065 +M: Miklos Szeredi <miklos@szeredi.hu>
3066 +L: linux-fsdevel@vger.kernel.org
3069 +F: Documentation/filesystems/overlayfs.txt
3072 M: Christian Lamparter <chunkeey@googlemail.com>
3073 L: linux-wireless@vger.kernel.org