3 @@ -651,19 +651,19 @@ 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)
17 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
18 FMODE_PREAD | FMODE_PWRITE;
19 - inode = dentry->d_inode;
20 + inode = path->dentry->d_inode;
21 if (f->f_mode & FMODE_WRITE) {
22 - error = __get_file_write_access(inode, mnt);
23 + error = __get_file_write_access(inode, path->mnt);
26 if (!special_file(inode->i_mode))
27 @@ -671,8 +671,7 @@ static struct file *__dentry_open(struct
30 f->f_mapping = inode->i_mapping;
31 - f->f_path.dentry = dentry;
32 - f->f_path.mnt = mnt;
35 f->f_op = fops_get(inode->i_fop);
36 file_sb_list_add(f, inode->i_sb);
37 @@ -718,7 +717,7 @@ cleanup_all:
38 * here, so just reset the state.
41 - mnt_drop_write(mnt);
42 + mnt_drop_write(path->mnt);
46 @@ -726,8 +725,7 @@ cleanup_all:
53 return ERR_PTR(error);
56 @@ -753,14 +751,14 @@ cleanup_file:
57 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
58 int (*open)(struct inode *, struct file *))
60 + struct path path = { .dentry = dentry, .mnt = nd->path.mnt };
61 const struct cred *cred = current_cred();
63 if (IS_ERR(nd->intent.open.file))
67 - nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
68 - nd->intent.open.file,
69 + nd->intent.open.file = __dentry_open(&path, nd->intent.open.file,
72 return nd->intent.open.file;
73 @@ -787,10 +785,17 @@ struct file *nameidata_to_filp(struct na
74 filp = nd->intent.open.file;
75 /* Has the filesystem initialised the file for us? */
76 if (filp->f_path.dentry == NULL) {
77 - path_get(&nd->path);
78 - filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
80 + struct inode *inode = nd->path.dentry->d_inode;
82 + if (inode->i_op->open) {
83 + int flags = filp->f_flags;
85 + filp = inode->i_op->open(nd->path.dentry, flags, cred);
87 + filp = __dentry_open(&nd->path, filp, NULL, cred);
94 @@ -801,35 +806,45 @@ struct file *nameidata_to_filp(struct na
95 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
96 const struct cred *cred)
99 + struct path path = { .dentry = dentry, .mnt = mnt };
104 + ret = vfs_open(&path, flags, cred);
109 +EXPORT_SYMBOL(dentry_open);
112 + * vfs_open - open the file at the given path
113 + * @path: path to open
114 + * @flags: open flags
115 + * @cred: credentials to use
117 + * Open the file. If successful, the returned file will have acquired
118 + * an additional reference for path.
120 +struct file *vfs_open(struct path *path, int flags, const struct cred *cred)
123 + struct inode *inode = path->dentry->d_inode;
125 validate_creds(cred);
128 - * We must always pass in a valid mount pointer. Historically
129 - * callers got away with not passing it, but we must enforce this at
130 - * the earliest possible point now to avoid strange problems deep in the
131 - * filesystem stack.
134 - printk(KERN_WARNING "%s called with NULL vfsmount\n", __func__);
136 - return ERR_PTR(-EINVAL);
138 + if (inode->i_op->open)
139 + return inode->i_op->open(path->dentry, flags, cred);
142 f = get_empty_filp();
146 - return ERR_PTR(error);
149 + return ERR_PTR(-ENFILE);
152 - return __dentry_open(dentry, mnt, f, NULL, cred);
153 + return __dentry_open(path, f, NULL, cred);
155 -EXPORT_SYMBOL(dentry_open);
156 +EXPORT_SYMBOL(vfs_open);
158 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
160 --- a/include/linux/fs.h
161 +++ b/include/linux/fs.h
162 @@ -1574,6 +1574,7 @@ struct inode_operations {
164 int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
166 + struct file *(*open)(struct dentry *, int flags, const struct cred *);
170 @@ -1975,6 +1976,7 @@ extern int do_fallocate(struct file *fil
171 extern long do_sys_open(int dfd, const char __user *filename, int flags,
173 extern struct file *filp_open(const char *, int, int);
174 +extern struct file *vfs_open(struct path *, int flags, const struct cred *);
175 extern struct file * dentry_open(struct dentry *, struct vfsmount *, int,
176 const struct cred *);
177 extern int filp_close(struct file *, fl_owner_t id);
180 @@ -1307,6 +1307,7 @@ long do_splice_direct(struct file *in, l
184 +EXPORT_SYMBOL(do_splice_direct);
186 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
187 struct pipe_inode_info *opipe,
190 @@ -1307,6 +1307,23 @@ void drop_collected_mounts(struct vfsmou
191 release_mounts(&umount_list);
194 +struct vfsmount *clone_private_mount(struct path *path)
196 + struct vfsmount *mnt;
198 + if (IS_MNT_UNBINDABLE(path->mnt))
199 + return ERR_PTR(-EINVAL);
201 + down_read(&namespace_sem);
202 + mnt = clone_mnt(path->mnt, path->dentry, CL_PRIVATE);
203 + up_read(&namespace_sem);
205 + return ERR_PTR(-ENOMEM);
209 +EXPORT_SYMBOL_GPL(clone_private_mount);
211 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
212 struct vfsmount *root)
214 --- a/include/linux/mount.h
215 +++ b/include/linux/mount.h
216 @@ -121,6 +121,9 @@ static inline void mntput(struct vfsmoun
221 +extern struct vfsmount *clone_private_mount(struct path *path);
223 extern struct vfsmount *do_kern_mount(const char *fstype, int flags,
224 const char *name, void *data);
228 @@ -64,6 +64,7 @@ source "fs/quota/Kconfig"
230 source "fs/autofs4/Kconfig"
231 source "fs/fuse/Kconfig"
232 +source "fs/overlayfs/Kconfig"
235 tristate "Character device in Userspace support"
238 @@ -103,6 +103,7 @@ obj-$(CONFIG_QNX4FS_FS) += qnx4/
239 obj-$(CONFIG_AUTOFS4_FS) += autofs4/
240 obj-$(CONFIG_ADFS_FS) += adfs/
241 obj-$(CONFIG_FUSE_FS) += fuse/
242 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
243 obj-$(CONFIG_UDF_FS) += udf/
244 obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
245 obj-$(CONFIG_OMFS_FS) += omfs/
247 +++ b/fs/overlayfs/Kconfig
250 + tristate "Overlay filesystem support"
252 + Add support for overlay filesystem.
254 +++ b/fs/overlayfs/Makefile
257 +# Makefile for the overlay filesystem.
260 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
262 +++ b/fs/overlayfs/overlayfs.c
264 +#include <linux/fs.h>
265 +#include <linux/namei.h>
266 +#include <linux/sched.h>
267 +#include <linux/fs_struct.h>
268 +#include <linux/file.h>
269 +#include <linux/xattr.h>
270 +#include <linux/security.h>
271 +#include <linux/device_cgroup.h>
272 +#include <linux/mount.h>
273 +#include <linux/splice.h>
274 +#include <linux/slab.h>
275 +#include <linux/parser.h>
276 +#include <linux/module.h>
277 +#include <linux/uaccess.h>
278 +#include <linux/rbtree.h>
280 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
281 +MODULE_DESCRIPTION("Overlay filesystem");
282 +MODULE_LICENSE("GPL");
284 +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
287 + struct vfsmount *upper_mnt;
288 + struct vfsmount *lower_mnt;
292 + struct dentry *__upperdentry;
293 + struct dentry *lowerdentry;
299 + struct rcu_head rcu;
303 +static const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
304 +static const char *ovl_opaque_xattr = "trusted.overlay.opaque";
305 +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
307 +enum ovl_path_type {
313 +static enum ovl_path_type ovl_path_type(struct dentry *dentry)
315 + struct ovl_entry *oe = dentry->d_fsdata;
317 + if (oe->__upperdentry) {
318 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
319 + return OVL_PATH_MERGE;
321 + return OVL_PATH_UPPER;
323 + return OVL_PATH_LOWER;
327 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
329 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
330 + smp_read_barrier_depends();
331 + return upperdentry;
334 +static void ovl_path_upper(struct dentry *dentry, struct path *path)
336 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
337 + struct ovl_entry *oe = dentry->d_fsdata;
339 + path->mnt = ofs->upper_mnt;
340 + path->dentry = ovl_upperdentry_dereference(oe);
343 +static void ovl_path_lower(struct dentry *dentry, struct path *path)
345 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
346 + struct ovl_entry *oe = dentry->d_fsdata;
348 + path->mnt = ofs->lower_mnt;
349 + path->dentry = oe->lowerdentry;
352 +static enum ovl_path_type ovl_path_real(struct dentry *dentry,
356 + enum ovl_path_type type = ovl_path_type(dentry);
358 + if (type == OVL_PATH_LOWER)
359 + ovl_path_lower(dentry, path);
361 + ovl_path_upper(dentry, path);
366 +static struct dentry *ovl_dentry_upper(struct dentry *dentry)
368 + struct ovl_entry *oe = dentry->d_fsdata;
370 + return ovl_upperdentry_dereference(oe);
373 +static struct dentry *ovl_dentry_lower(struct dentry *dentry)
375 + struct ovl_entry *oe = dentry->d_fsdata;
377 + return oe->lowerdentry;
380 +static struct dentry *ovl_dentry_real(struct dentry *dentry)
382 + struct ovl_entry *oe = dentry->d_fsdata;
383 + struct dentry *realdentry;
385 + realdentry = ovl_upperdentry_dereference(oe);
387 + realdentry = oe->lowerdentry;
392 +static bool ovl_dentry_is_opaque(struct dentry *dentry)
394 + struct ovl_entry *oe = dentry->d_fsdata;
398 +static void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
400 + struct ovl_entry *oe = dentry->d_fsdata;
401 + oe->opaque = opaque;
404 +static void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
406 + struct ovl_entry *oe = dentry->d_fsdata;
408 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
409 + WARN_ON(oe->__upperdentry);
411 + oe->__upperdentry = upperdentry;
414 +static void ovl_dentry_version_inc(struct dentry *dentry)
416 + struct ovl_entry *oe = dentry->d_fsdata;
418 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
422 +static u64 ovl_dentry_version_get(struct dentry *dentry)
424 + struct ovl_entry *oe = dentry->d_fsdata;
426 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
427 + return oe->version;
430 +static bool ovl_is_whiteout(struct dentry *dentry)
437 + if (!dentry->d_inode)
439 + if (!S_ISLNK(dentry->d_inode->i_mode))
442 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
443 + if (res == 1 && val == 'y')
449 +static bool ovl_is_opaquedir(struct dentry *dentry)
454 + if (!S_ISDIR(dentry->d_inode->i_mode))
457 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
458 + if (res == 1 && val == 'y')
464 +struct ovl_cache_entry {
470 + struct list_head l_node;
471 + struct rb_node node;
474 +struct ovl_readdir_data {
475 + struct rb_root *root;
476 + struct list_head *list;
477 + struct list_head *middle;
478 + struct dentry *dir;
483 +struct ovl_dir_file {
486 + struct list_head cursor;
488 + struct list_head cache;
489 + struct file *realfile;
492 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
494 + return container_of(n, struct ovl_cache_entry, node);
497 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
498 + const char *name, int len)
500 + struct rb_node *node = root->rb_node;
504 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
506 + cmp = strncmp(name, p->name, len);
508 + node = p->node.rb_right;
509 + else if (cmp < 0 || len < p->len)
510 + node = p->node.rb_left;
518 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
519 + u64 ino, unsigned int d_type,
522 + struct ovl_cache_entry *p;
524 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
526 + char *name_copy = (char *) (p + 1);
527 + memcpy(name_copy, name, len);
528 + name_copy[len] = '\0';
529 + p->name = name_copy;
533 + p->is_whiteout = is_whiteout;
539 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
540 + const char *name, int len, u64 ino,
541 + unsigned int d_type, bool is_whiteout)
543 + struct rb_node **newp = &rdd->root->rb_node;
544 + struct rb_node *parent = NULL;
545 + struct ovl_cache_entry *p;
549 + struct ovl_cache_entry *tmp;
552 + tmp = ovl_cache_entry_from_node(*newp);
553 + cmp = strncmp(name, tmp->name, len);
555 + newp = &tmp->node.rb_right;
556 + else if (cmp < 0 || len < tmp->len)
557 + newp = &tmp->node.rb_left;
562 + p = ovl_cache_entry_new(name, len, ino, d_type, is_whiteout);
566 + list_add_tail(&p->l_node, rdd->list);
567 + rb_link_node(&p->node, parent, newp);
568 + rb_insert_color(&p->node, rdd->root);
573 +static int ovl_fill_lower(void *buf, const char *name, int namelen,
574 + loff_t offset, u64 ino, unsigned int d_type)
576 + struct ovl_readdir_data *rdd = buf;
577 + struct ovl_cache_entry *p;
580 + p = ovl_cache_entry_find(rdd->root, name, namelen);
582 + list_move_tail(&p->l_node, rdd->middle);
584 + p = ovl_cache_entry_new(name, namelen, ino, d_type, false);
586 + rdd->err = -ENOMEM;
588 + list_add_tail(&p->l_node, rdd->middle);
594 +static void ovl_cache_free(struct list_head *list)
596 + struct ovl_cache_entry *p;
597 + struct ovl_cache_entry *n;
599 + list_for_each_entry_safe(p, n, list, l_node)
602 + INIT_LIST_HEAD(list);
605 +static int ovl_fill_upper(void *buf, const char *name, int namelen,
606 + loff_t offset, u64 ino, unsigned int d_type)
608 + struct ovl_readdir_data *rdd = buf;
609 + bool is_whiteout = false;
612 + if (d_type == DT_LNK) {
613 + struct dentry *dentry;
615 + dentry = lookup_one_len(name, rdd->dir, namelen);
616 + if (IS_ERR(dentry)) {
617 + rdd->err = PTR_ERR(dentry);
620 + is_whiteout = ovl_is_whiteout(dentry);
624 + rdd->err = ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type,
631 +static int ovl_dir_read(struct path *realpath, struct ovl_readdir_data *rdd,
634 + const struct cred *old_cred;
635 + struct cred *override_cred;
636 + struct file *realfile;
639 + realfile = vfs_open(realpath, O_RDONLY | O_DIRECTORY, current_cred());
640 + if (IS_ERR(realfile))
641 + return PTR_ERR(realfile);
644 + override_cred = prepare_creds();
645 + if (override_cred) {
647 + * CAP_SYS_ADMIN for getxattr
648 + * CAP_DAC_OVERRIDE for lookup and unlink
650 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
651 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
652 + old_cred = override_creds(override_cred);
657 + err = vfs_readdir(realfile, filler, rdd);
660 + } while (!err && rdd->count);
662 + revert_creds(old_cred);
663 + put_cred(override_cred);
669 + ovl_cache_free(rdd->list);
676 +static void ovl_dir_reset(struct file *file)
678 + struct ovl_dir_file *od = file->private_data;
679 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
681 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
682 + list_del_init(&od->cursor);
683 + ovl_cache_free(&od->cache);
684 + od->is_cached = false;
686 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
687 + if (od->is_real && type == OVL_PATH_MERGE) {
688 + fput(od->realfile);
689 + od->realfile = NULL;
690 + od->is_real = false;
694 +static int ovl_dir_read_merged(struct path *upperpath, struct path *lowerpath,
695 + struct ovl_readdir_data *rdd)
698 + struct rb_root root = RB_ROOT;
699 + struct list_head middle;
702 + if (upperpath->dentry) {
703 + rdd->dir = upperpath->dentry;
704 + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
709 + * Insert lowerpath entries before upperpath ones, this allows
710 + * offsets to be reasonably constant
712 + list_add(&middle, rdd->list);
713 + rdd->middle = &middle;
714 + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower);
722 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
724 + struct list_head *l;
727 + l = od->cache.next;
728 + for (off = 0; off < pos; off++) {
729 + if (l == &od->cache)
733 + list_move_tail(&od->cursor, l);
736 +static int ovl_readdir(struct file *file, void *buf, filldir_t filler)
738 + struct ovl_dir_file *od = file->private_data;
742 + ovl_dir_reset(file);
745 + res = vfs_readdir(od->realfile, filler, buf);
746 + file->f_pos = od->realfile->f_pos;
751 + if (!od->is_cached) {
752 + struct path lowerpath;
753 + struct path upperpath;
754 + struct ovl_readdir_data rdd = { .list = &od->cache };
756 + ovl_path_lower(file->f_path.dentry, &lowerpath);
757 + ovl_path_upper(file->f_path.dentry, &upperpath);
759 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
763 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
764 + od->is_cached = true;
766 + ovl_seek_cursor(od, file->f_pos);
769 + while (od->cursor.next != &od->cache) {
772 + struct ovl_cache_entry *p;
774 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
777 + list_move(&od->cursor, &p->l_node);
779 + if (p->is_whiteout)
782 + over = filler(buf, p->name, p->len, off, p->ino, p->type);
790 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
793 + struct ovl_dir_file *od = file->private_data;
795 + mutex_lock(&file->f_dentry->d_inode->i_mutex);
797 + ovl_dir_reset(file);
800 + res = vfs_llseek(od->realfile, offset, origin);
801 + file->f_pos = od->realfile->f_pos;
807 + offset += file->f_pos;
817 + if (offset != file->f_pos) {
818 + file->f_pos = offset;
820 + ovl_seek_cursor(od, offset);
825 + mutex_unlock(&file->f_dentry->d_inode->i_mutex);
830 +static int ovl_dir_fsync(struct file *file, int datasync)
832 + struct ovl_dir_file *od = file->private_data;
834 + /* May need to reopen directory if it got copied up */
835 + if (!od->realfile) {
836 + struct path upperpath;
838 + ovl_path_upper(file->f_path.dentry, &upperpath);
839 + od->realfile = vfs_open(&upperpath, O_RDONLY, current_cred());
840 + if (IS_ERR(od->realfile))
841 + return PTR_ERR(od->realfile);
844 + return vfs_fsync(od->realfile, datasync);
847 +static int ovl_dir_release(struct inode *inode, struct file *file)
849 + struct ovl_dir_file *od = file->private_data;
851 + list_del(&od->cursor);
852 + ovl_cache_free(&od->cache);
854 + fput(od->realfile);
860 +static int ovl_dir_open(struct inode *inode, struct file *file)
862 + struct path realpath;
863 + struct file *realfile;
864 + struct ovl_dir_file *od;
865 + enum ovl_path_type type;
867 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
871 + type = ovl_path_real(file->f_path.dentry, &realpath);
872 + realfile = vfs_open(&realpath, file->f_flags, current_cred());
873 + if (IS_ERR(realfile)) {
875 + return PTR_ERR(realfile);
877 + INIT_LIST_HEAD(&od->cache);
878 + INIT_LIST_HEAD(&od->cursor);
879 + od->is_cached = false;
880 + od->realfile = realfile;
881 + od->is_real = (type != OVL_PATH_MERGE);
882 + file->private_data = od;
887 +static const struct file_operations ovl_dir_operations = {
888 + .read = generic_read_dir,
889 + .open = ovl_dir_open,
890 + .readdir = ovl_readdir,
891 + .llseek = ovl_dir_llseek,
892 + .fsync = ovl_dir_fsync,
893 + .release = ovl_dir_release,
896 +static const struct inode_operations ovl_dir_inode_operations;
898 +static void ovl_entry_free(struct rcu_head *head)
900 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
904 +static void ovl_dentry_release(struct dentry *dentry)
906 + struct ovl_entry *oe = dentry->d_fsdata;
909 + dput(oe->__upperdentry);
910 + dput(oe->lowerdentry);
911 + call_rcu(&oe->rcu, ovl_entry_free);
915 +static const struct dentry_operations ovl_dentry_operations = {
916 + .d_release = ovl_dentry_release,
919 +static struct dentry *ovl_lookup_real(struct dentry *dir, struct qstr *name)
921 + struct dentry *dentry;
923 + mutex_lock(&dir->d_inode->i_mutex);
924 + dentry = lookup_one_len(name->name, dir, name->len);
925 + mutex_unlock(&dir->d_inode->i_mutex);
927 + if (IS_ERR(dentry)) {
928 + if (PTR_ERR(dentry) == -ENOENT)
930 + } else if (!dentry->d_inode) {
937 +static struct ovl_entry *ovl_alloc_entry(void)
939 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
942 +static struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
943 + struct ovl_entry *oe);
945 +static struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
946 + struct nameidata *nd)
948 + struct ovl_entry *oe;
949 + struct dentry *upperdir;
950 + struct dentry *lowerdir;
951 + struct dentry *upperdentry = NULL;
952 + struct dentry *lowerdentry = NULL;
953 + struct inode *inode = NULL;
957 + oe = ovl_alloc_entry();
961 + upperdir = ovl_dentry_upper(dentry->d_parent);
962 + lowerdir = ovl_dentry_lower(dentry->d_parent);
965 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
966 + err = PTR_ERR(upperdentry);
967 + if (IS_ERR(upperdentry))
970 + if (lowerdir && upperdentry &&
971 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
972 + S_ISDIR(upperdentry->d_inode->i_mode))) {
973 + const struct cred *old_cred;
974 + struct cred *override_cred;
977 + override_cred = prepare_creds();
978 + if (!override_cred)
979 + goto out_dput_upper;
981 + /* CAP_SYS_ADMIN needed for getxattr */
982 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
983 + old_cred = override_creds(override_cred);
985 + if (ovl_is_opaquedir(upperdentry)) {
987 + } else if (ovl_is_whiteout(upperdentry)) {
989 + upperdentry = NULL;
992 + revert_creds(old_cred);
993 + put_cred(override_cred);
996 + if (lowerdir && !oe->opaque) {
997 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
998 + err = PTR_ERR(lowerdentry);
999 + if (IS_ERR(lowerdentry))
1000 + goto out_dput_upper;
1003 + if (lowerdentry && upperdentry &&
1004 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
1005 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
1006 + dput(lowerdentry);
1007 + lowerdentry = NULL;
1008 + oe->opaque = true;
1011 + if (lowerdentry || upperdentry) {
1012 + struct dentry *realdentry;
1014 + realdentry = upperdentry ? upperdentry : lowerdentry;
1016 + inode = ovl_new_inode(dir->i_sb, realdentry->d_inode->i_mode, oe);
1022 + oe->__upperdentry = upperdentry;
1025 + oe->lowerdentry = lowerdentry;
1027 + dentry->d_fsdata = oe;
1028 + dentry->d_op = &ovl_dentry_operations;
1029 + d_add(dentry, inode);
1034 + dput(lowerdentry);
1036 + dput(upperdentry);
1040 + return ERR_PTR(err);
1043 +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
1045 + ssize_t list_size, size;
1046 + char *buf, *name, *value;
1049 + if (!old->d_inode->i_op->getxattr ||
1050 + !new->d_inode->i_op->getxattr)
1053 + list_size = vfs_listxattr(old, NULL, 0);
1054 + if (list_size <= 0) {
1055 + if (list_size == -EOPNOTSUPP)
1060 + buf = kzalloc(list_size, GFP_KERNEL);
1065 + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
1069 + list_size = vfs_listxattr(old, buf, list_size);
1070 + if (list_size <= 0) {
1071 + error = list_size;
1072 + goto out_free_value;
1075 + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
1076 + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
1079 + goto out_free_value;
1081 + error = vfs_setxattr(new, name, value, size, 0);
1083 + goto out_free_value;
1093 +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
1095 + struct file *old_file;
1096 + struct file *new_file;
1102 + old_file = vfs_open(old, O_RDONLY, current_cred());
1103 + if (IS_ERR(old_file))
1104 + return PTR_ERR(old_file);
1106 + new_file = vfs_open(new, O_WRONLY, current_cred());
1107 + if (IS_ERR(new_file)) {
1108 + error = PTR_ERR(new_file);
1112 + /* FIXME: copy up sparse files efficiently */
1114 + loff_t offset = new_file->f_pos;
1115 + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
1118 + if (len < this_len)
1121 + if (signal_pending_state(TASK_KILLABLE, current))
1124 + bytes = do_splice_direct(old_file, &offset, new_file, this_len,
1140 +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
1141 + struct dentry *template)
1144 + struct dentry *newdentry;
1145 + struct qstr *name = &template->d_name;
1147 + newdentry = lookup_one_len(name->name, upperdir, name->len);
1148 + if (IS_ERR(newdentry))
1151 + if (newdentry->d_inode) {
1152 + const struct cred *old_cred;
1153 + struct cred *override_cred;
1155 + /* No need to check whiteout if lower parent is non-existent */
1157 + if (!ovl_dentry_lower(template->d_parent))
1160 + if (!S_ISLNK(newdentry->d_inode->i_mode))
1164 + override_cred = prepare_creds();
1165 + if (!override_cred)
1169 + * CAP_SYS_ADMIN for getxattr
1170 + * CAP_FOWNER for unlink in sticky directory
1172 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1173 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
1174 + old_cred = override_creds(override_cred);
1177 + if (ovl_is_whiteout(newdentry))
1178 + err = vfs_unlink(upperdir->d_inode, newdentry);
1180 + revert_creds(old_cred);
1181 + put_cred(override_cred);
1186 + newdentry = lookup_one_len(name->name, upperdir, name->len);
1187 + if (IS_ERR(newdentry))
1191 + * Whiteout just been successfully removed, parent
1192 + * i_mutex is still held, there's no way the lookup
1193 + * could return positive.
1195 + WARN_ON(newdentry->d_inode);
1202 + return ERR_PTR(err);
1205 +static struct dentry *ovl_upper_create(struct dentry *upperdir,
1206 + struct dentry *dentry,
1207 + struct kstat *stat, const char *link)
1210 + struct dentry *newdentry;
1211 + struct inode *dir = upperdir->d_inode;
1213 + newdentry = ovl_lookup_create(upperdir, dentry);
1214 + if (IS_ERR(newdentry))
1217 + switch (stat->mode & S_IFMT) {
1219 + err = vfs_create(dir, newdentry, stat->mode, NULL);
1223 + err = vfs_mkdir(dir, newdentry, stat->mode);
1230 + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
1234 + err = vfs_symlink(dir, newdentry, link);
1242 + newdentry = ERR_PTR(err);
1250 +static char *ovl_read_symlink(struct dentry *realdentry)
1254 + struct inode *inode = realdentry->d_inode;
1255 + mm_segment_t old_fs;
1258 + if (!inode->i_op->readlink)
1262 + buf = (char *) __get_free_page(GFP_KERNEL);
1266 + old_fs = get_fs();
1268 + /* The cast to a user pointer is valid due to the set_fs() */
1269 + res = inode->i_op->readlink(realdentry,
1270 + (char __user *)buf, PAGE_SIZE - 1);
1273 + free_page((unsigned long) buf);
1281 + return ERR_PTR(res);
1284 +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
1286 + struct iattr attr = {
1287 + .ia_valid = ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
1288 + .ia_atime = stat->atime,
1289 + .ia_mtime = stat->mtime,
1292 + return notify_change(upperdentry, &attr);
1295 +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
1297 + struct iattr attr = {
1298 + .ia_valid = ATTR_MODE,
1302 + return notify_change(upperdentry, &attr);
1305 +static int ovl_set_opaque(struct dentry *upperdentry)
1308 + const struct cred *old_cred;
1309 + struct cred *override_cred;
1311 + override_cred = prepare_creds();
1312 + if (!override_cred)
1315 + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
1316 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1317 + old_cred = override_creds(override_cred);
1318 + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
1319 + revert_creds(old_cred);
1320 + put_cred(override_cred);
1325 +static int ovl_remove_opaque(struct dentry *upperdentry)
1328 + const struct cred *old_cred;
1329 + struct cred *override_cred;
1331 + override_cred = prepare_creds();
1332 + if (!override_cred)
1335 + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
1336 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1337 + old_cred = override_creds(override_cred);
1338 + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
1339 + revert_creds(old_cred);
1340 + put_cred(override_cred);
1345 +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
1346 + struct path *lowerpath, struct kstat *stat,
1350 + struct path newpath;
1351 + umode_t mode = stat->mode;
1352 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
1354 + /* Can't properly set mode on creation because of the umask */
1355 + stat->mode &= S_IFMT;
1357 + newpath.mnt = ofs->upper_mnt;
1358 + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
1359 + if (IS_ERR(newpath.dentry)) {
1360 + err = PTR_ERR(newpath.dentry);
1362 + /* Already copied up? */
1363 + if (err == -EEXIST && ovl_path_type(dentry) != OVL_PATH_LOWER)
1369 + /* FIXME: recovery from failure to copy up */
1371 + if (S_ISREG(stat->mode)) {
1372 + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
1377 + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
1381 + mutex_lock(&newpath.dentry->d_inode->i_mutex);
1382 + if (!S_ISLNK(stat->mode))
1383 + err = ovl_set_mode(newpath.dentry, mode);
1385 + err = ovl_set_timestamps(newpath.dentry, stat);
1386 + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
1390 + ovl_dentry_update(dentry, newpath.dentry);
1393 + * Easiest way to get rid of the lower dentry reference is to
1394 + * drop this dentry. This is neither needed nor possible for
1397 + if (!S_ISDIR(stat->mode))
1403 +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
1404 + struct path *lowerpath, struct kstat *stat)
1407 + struct kstat pstat;
1408 + struct path parentpath;
1409 + struct dentry *upperdir;
1410 + const struct cred *old_cred;
1411 + struct cred *override_cred;
1412 + char *link = NULL;
1414 + ovl_path_upper(parent, &parentpath);
1415 + upperdir = parentpath.dentry;
1417 + err = vfs_getattr(parentpath.mnt, parentpath.dentry, &pstat);
1421 + if (S_ISLNK(stat->mode)) {
1422 + link = ovl_read_symlink(lowerpath->dentry);
1424 + return PTR_ERR(link);
1428 + override_cred = prepare_creds();
1429 + if (!override_cred)
1430 + goto out_free_link;
1432 + override_cred->fsuid = stat->uid;
1433 + override_cred->fsgid = stat->gid;
1435 + * CAP_SYS_ADMIN for copying up extended attributes
1436 + * CAP_DAC_OVERRIDE for create
1437 + * CAP_FOWNER for chmod, timestamp update
1438 + * CAP_FSETID for chmod
1439 + * CAP_MKNOD for mknod
1441 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1442 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
1443 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
1444 + cap_raise(override_cred->cap_effective, CAP_FSETID);
1445 + cap_raise(override_cred->cap_effective, CAP_MKNOD);
1446 + old_cred = override_creds(override_cred);
1448 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1450 + * Using upper filesystem locking to protect against copy up
1451 + * racing with rename (rename means the copy up was already
1454 + if (dentry->d_parent != parent) {
1455 + WARN_ON((ovl_path_type(dentry) == OVL_PATH_LOWER));
1458 + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
1461 + /* Restore timestamps on parent (best effort) */
1462 + ovl_set_timestamps(upperdir, &pstat);
1466 + mutex_unlock(&upperdir->d_inode->i_mutex);
1468 + revert_creds(old_cred);
1469 + put_cred(override_cred);
1473 + free_page((unsigned long) link);
1478 +static int ovl_copy_up(struct dentry *dentry)
1484 + struct dentry *next;
1485 + struct dentry *parent;
1486 + struct path lowerpath;
1487 + struct kstat stat;
1488 + enum ovl_path_type type = ovl_path_type(dentry);
1490 + if (type != OVL_PATH_LOWER)
1493 + next = dget(dentry);
1494 + /* find the topmost dentry not yet copied up */
1496 + parent = dget_parent(next);
1498 + type = ovl_path_type(parent);
1499 + if (type != OVL_PATH_LOWER)
1506 + ovl_path_lower(next, &lowerpath);
1507 + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat);
1509 + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
1518 +/* Optimize by not copying up the file first and truncating later */
1519 +static int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
1522 + struct kstat stat;
1523 + struct path lowerpath;
1524 + struct dentry *parent = dget_parent(dentry);
1526 + err = ovl_copy_up(parent);
1528 + goto out_dput_parent;
1530 + ovl_path_lower(dentry, &lowerpath);
1531 + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat);
1533 + goto out_dput_parent;
1535 + if (size < stat.size)
1538 + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
1545 +static int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1547 + struct dentry *upperdentry;
1550 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1551 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1553 + err = ovl_copy_up(dentry);
1557 + upperdentry = ovl_dentry_upper(dentry);
1559 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1560 + attr->ia_valid &= ~ATTR_MODE;
1562 + mutex_lock(&upperdentry->d_inode->i_mutex);
1563 + err = notify_change(upperdentry, attr);
1564 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1569 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1570 + struct kstat *stat)
1572 + struct path realpath;
1574 + ovl_path_real(dentry, &realpath);
1575 + return vfs_getattr(realpath.mnt, realpath.dentry, stat);
1578 +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1579 + struct kstat *stat)
1582 + enum ovl_path_type type;
1583 + struct path realpath;
1585 + type = ovl_path_real(dentry, &realpath);
1586 + err = vfs_getattr(realpath.mnt, realpath.dentry, stat);
1590 + stat->dev = dentry->d_sb->s_dev;
1591 + stat->ino = dentry->d_inode->i_ino;
1594 + * It's probably not worth it to count subdirs to get the
1595 + * correct link count. nlink=1 seems to pacify 'find' and
1596 + * other utilities.
1598 + if (type == OVL_PATH_MERGE)
1604 +static int ovl_permission(struct inode *inode, int mask, unsigned int flags)
1606 + struct ovl_entry *oe;
1607 + struct dentry *alias = NULL;
1608 + struct inode *realinode;
1609 + struct dentry *realdentry;
1613 + if (S_ISDIR(inode->i_mode)) {
1614 + oe = inode->i_private;
1615 + } else if (flags & IPERM_FLAG_RCU) {
1619 + * For non-directories find an alias and get the info
1622 + spin_lock(&inode->i_lock);
1623 + if (WARN_ON(list_empty(&inode->i_dentry))) {
1624 + spin_unlock(&inode->i_lock);
1627 + alias = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1629 + spin_unlock(&inode->i_lock);
1630 + oe = alias->d_fsdata;
1633 + realdentry = ovl_upperdentry_dereference(oe);
1635 + if (!realdentry) {
1636 + realdentry = oe->lowerdentry;
1640 + /* Careful in RCU walk mode */
1641 + realinode = ACCESS_ONCE(realdentry->d_inode);
1643 + WARN_ON(!(flags & IPERM_FLAG_RCU));
1647 + if (mask & MAY_WRITE) {
1648 + umode_t mode = realinode->i_mode;
1651 + * Writes will always be redirected to upper layer, so
1652 + * ignore lower layer being read-only.
1655 + if (is_upper && IS_RDONLY(realinode) &&
1656 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1660 + * Nobody gets write access to an immutable file.
1663 + if (IS_IMMUTABLE(realinode))
1667 + if (realinode->i_op->permission)
1668 + err = realinode->i_op->permission(realinode, mask, flags);
1670 + err = generic_permission(realinode, mask, flags,
1671 + realinode->i_op->check_acl);
1677 +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
1681 + struct dentry *newdentry;
1682 + struct dentry *upperdir;
1683 + struct inode *inode;
1684 + struct kstat stat = {
1690 + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
1694 + err = ovl_copy_up(dentry->d_parent);
1698 + upperdir = ovl_dentry_upper(dentry->d_parent);
1699 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1701 + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
1702 + err = PTR_ERR(newdentry);
1703 + if (IS_ERR(newdentry))
1706 + ovl_dentry_version_inc(dentry->d_parent);
1707 + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
1708 + err = ovl_set_opaque(newdentry);
1712 + ovl_dentry_update(dentry, newdentry);
1713 + d_instantiate(dentry, inode);
1721 + mutex_unlock(&upperdir->d_inode->i_mutex);
1728 +static int ovl_create(struct inode *dir, struct dentry *dentry, int mode,
1729 + struct nameidata *nd)
1731 + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
1734 +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1736 + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
1739 +static int ovl_mknod(struct inode *dir, struct dentry *dentry, int mode,
1742 + return ovl_create_object(dentry, mode, rdev, NULL);
1745 +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
1748 + return ovl_create_object(dentry, S_IFLNK, 0, link);
1751 +struct ovl_link_data {
1752 + struct dentry *realdentry;
1756 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1759 + struct dentry *realdentry;
1760 + struct inode *realinode;
1762 + realdentry = ovl_dentry_real(dentry);
1763 + realinode = realdentry->d_inode;
1765 + if (WARN_ON(!realinode->i_op->follow_link))
1766 + return ERR_PTR(-EPERM);
1768 + ret = realinode->i_op->follow_link(realdentry, nd);
1772 + if (realinode->i_op->put_link) {
1773 + struct ovl_link_data *data;
1775 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1777 + realinode->i_op->put_link(realdentry, nd, ret);
1778 + return ERR_PTR(-ENOMEM);
1780 + data->realdentry = realdentry;
1781 + data->cookie = ret;
1789 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1791 + struct inode *realinode;
1792 + struct ovl_link_data *data = c;
1797 + realinode = data->realdentry->d_inode;
1798 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1802 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1804 + struct path realpath;
1805 + struct inode *realinode;
1807 + ovl_path_real(dentry, &realpath);
1808 + realinode = realpath.dentry->d_inode;
1810 + if (!realinode->i_op->readlink)
1813 + touch_atime(realpath.mnt, realpath.dentry);
1815 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1818 +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
1821 + struct dentry *newdentry;
1822 + const struct cred *old_cred;
1823 + struct cred *override_cred;
1825 + /* FIXME: recheck lower dentry to see if whiteout is really needed */
1828 + override_cred = prepare_creds();
1829 + if (!override_cred)
1833 + * CAP_SYS_ADMIN for setxattr
1834 + * CAP_DAC_OVERRIDE for symlink creation
1836 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1837 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
1838 + override_cred->fsuid = 0;
1839 + override_cred->fsgid = 0;
1840 + old_cred = override_creds(override_cred);
1842 + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
1843 + dentry->d_name.len);
1844 + err = PTR_ERR(newdentry);
1845 + if (IS_ERR(newdentry))
1846 + goto out_put_cred;
1848 + /* Just been removed within the same locked region */
1849 + WARN_ON(newdentry->d_inode);
1851 + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
1855 + ovl_dentry_version_inc(dentry->d_parent);
1857 + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
1862 + revert_creds(old_cred);
1863 + put_cred(override_cred);
1868 +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
1871 + enum ovl_path_type type;
1872 + struct path realpath;
1873 + struct dentry *upperdir;
1875 + err = ovl_copy_up(dentry->d_parent);
1879 + upperdir = ovl_dentry_upper(dentry->d_parent);
1880 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1881 + type = ovl_path_real(dentry, &realpath);
1882 + if (type != OVL_PATH_LOWER) {
1884 + if (realpath.dentry->d_parent != upperdir)
1888 + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
1890 + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
1894 + ovl_dentry_version_inc(dentry->d_parent);
1897 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1898 + err = ovl_whiteout(upperdir, dentry);
1901 + * Keeping this dentry hashed would mean having to release
1902 + * upperpath/lowerpath, which could only be done if we are the
1903 + * sole user of this dentry. Too tricky... Just unhash for
1908 + mutex_unlock(&upperdir->d_inode->i_mutex);
1913 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1915 + return ovl_do_remove(dentry, false);
1918 +static int ovl_check_empty_dir(struct dentry *dentry)
1921 + struct path lowerpath;
1922 + struct path upperpath;
1923 + struct ovl_cache_entry *p;
1925 + struct ovl_readdir_data rdd = { .list = &list };
1927 + ovl_path_upper(dentry, &upperpath);
1928 + ovl_path_lower(dentry, &lowerpath);
1930 + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
1936 + list_for_each_entry(p, &list, l_node) {
1937 + if (p->is_whiteout)
1940 + if (p->name[0] == '.') {
1943 + if (p->len == 2 && p->name[1] == '.')
1950 + ovl_cache_free(&list);
1955 +static int ovl_unlink_whiteout(void *buf, const char *name, int namelen,
1956 + loff_t offset, u64 ino, unsigned int d_type)
1958 + struct ovl_readdir_data *rdd = buf;
1961 + /* check d_type to filter out "." and ".." */
1962 + if (d_type == DT_LNK) {
1963 + struct dentry *dentry;
1965 + dentry = lookup_one_len(name, rdd->dir, namelen);
1966 + if (IS_ERR(dentry)) {
1967 + rdd->err = PTR_ERR(dentry);
1969 + rdd->err = vfs_unlink(rdd->dir->d_inode, dentry);
1977 +static int ovl_remove_whiteouts(struct dentry *dentry)
1979 + struct path upperpath;
1980 + struct ovl_readdir_data rdd = { .list = NULL };
1982 + ovl_path_upper(dentry, &upperpath);
1983 + rdd.dir = upperpath.dentry;
1985 + return ovl_dir_read(&upperpath, &rdd, ovl_unlink_whiteout);
1988 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
1991 + enum ovl_path_type type;
1993 + type = ovl_path_type(dentry);
1994 + if (type != OVL_PATH_UPPER) {
1995 + err = ovl_check_empty_dir(dentry);
1999 + if (type == OVL_PATH_MERGE) {
2000 + err = ovl_remove_whiteouts(dentry);
2006 + return ovl_do_remove(dentry, true);
2009 +static int ovl_link(struct dentry *old, struct inode *newdir,
2010 + struct dentry *new)
2013 + struct dentry *olddentry;
2014 + struct dentry *newdentry;
2015 + struct dentry *upperdir;
2017 + err = ovl_copy_up(old);
2021 + err = ovl_copy_up(new->d_parent);
2025 + upperdir = ovl_dentry_upper(new->d_parent);
2026 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2027 + newdentry = ovl_lookup_create(upperdir, new);
2028 + err = PTR_ERR(newdentry);
2029 + if (IS_ERR(newdentry))
2032 + olddentry = ovl_dentry_upper(old);
2033 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
2035 + ovl_dentry_version_inc(new->d_parent);
2036 + ovl_dentry_update(new, newdentry);
2038 + ihold(old->d_inode);
2039 + d_instantiate(new, old->d_inode);
2044 + mutex_unlock(&upperdir->d_inode->i_mutex);
2050 +static int ovl_rename(struct inode *olddir, struct dentry *old,
2051 + struct inode *newdir, struct dentry *new)
2054 + enum ovl_path_type old_type;
2055 + struct dentry *old_upperdir;
2056 + struct dentry *new_upperdir;
2057 + struct dentry *olddentry;
2058 + struct dentry *newdentry;
2059 + struct dentry *trap;
2060 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
2062 + /* Don't copy up directory trees */
2063 + old_type = ovl_path_type(old);
2064 + if (old_type != OVL_PATH_UPPER && is_dir)
2067 + if (new->d_inode) {
2068 + enum ovl_path_type new_type;
2070 + new_type = ovl_path_type(new);
2072 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
2073 + if (ovl_dentry_lower(old)->d_inode ==
2074 + ovl_dentry_lower(new)->d_inode)
2077 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
2078 + if (ovl_dentry_upper(old)->d_inode ==
2079 + ovl_dentry_upper(new)->d_inode)
2083 + if (new_type != OVL_PATH_UPPER &&
2084 + S_ISDIR(new->d_inode->i_mode)) {
2085 + err = ovl_check_empty_dir(new);
2089 + if (new_type == OVL_PATH_MERGE) {
2090 + err = ovl_remove_whiteouts(new);
2097 + err = ovl_copy_up(old);
2101 + err = ovl_copy_up(new->d_parent);
2105 + old_upperdir = ovl_dentry_upper(old->d_parent);
2106 + new_upperdir = ovl_dentry_upper(new->d_parent);
2108 + trap = lock_rename(new_upperdir, old_upperdir);
2110 + olddentry = ovl_dentry_upper(old);
2111 + newdentry = ovl_dentry_upper(new);
2115 + newdentry = ovl_lookup_create(new_upperdir, new);
2116 + err = PTR_ERR(newdentry);
2117 + if (IS_ERR(newdentry))
2122 + if (olddentry->d_parent != old_upperdir)
2124 + if (newdentry->d_parent != new_upperdir)
2126 + if (olddentry == trap)
2128 + if (newdentry == trap)
2131 + err = vfs_rename(old_upperdir->d_inode, olddentry,
2132 + new_upperdir->d_inode, newdentry);
2135 + bool old_opaque = ovl_dentry_is_opaque(old);
2136 + bool new_opaque = ovl_dentry_is_opaque(new);
2138 + if (ovl_path_type(new) != OVL_PATH_UPPER)
2139 + new_opaque = true;
2141 + if (old_type != OVL_PATH_UPPER || old_opaque)
2142 + err = ovl_whiteout(old_upperdir, old);
2143 + if (!err && is_dir) {
2144 + if (old_opaque && !new_opaque) {
2145 + ovl_remove_opaque(olddentry);
2146 + ovl_dentry_set_opaque(old, false);
2148 + if (!old_opaque && new_opaque) {
2149 + err = ovl_set_opaque(olddentry);
2150 + ovl_dentry_set_opaque(old, true);
2153 + ovl_dentry_version_inc(old->d_parent);
2154 + ovl_dentry_version_inc(new->d_parent);
2160 + unlock_rename(new_upperdir, old_upperdir);
2164 +static bool ovl_is_private_xattr(const char *name)
2166 + return strncmp(name, "trusted.overlay.", 14) == 0;
2169 +static int ovl_setxattr(struct dentry *dentry, const char *name,
2170 + const void *value, size_t size, int flags)
2173 + struct dentry *upperdentry;
2175 + if (ovl_is_private_xattr(name))
2178 + err = ovl_copy_up(dentry);
2182 + upperdentry = ovl_dentry_upper(dentry);
2183 + return vfs_setxattr(upperdentry, name, value, size, flags);
2186 +static ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
2187 + void *value, size_t size)
2189 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
2190 + ovl_is_private_xattr(name))
2193 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
2196 +static ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
2201 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
2202 + if (res <= 0 || size == 0)
2205 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
2208 + /* filter out private xattrs */
2209 + for (off = 0; off < res;) {
2210 + char *s = list + off;
2211 + size_t slen = strlen(s) + 1;
2213 + BUG_ON(off + slen > res);
2215 + if (ovl_is_private_xattr(s)) {
2217 + memmove(s, s + slen, res - off);
2226 +static int ovl_removexattr(struct dentry *dentry, const char *name)
2229 + struct path realpath;
2230 + enum ovl_path_type type;
2232 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
2233 + ovl_is_private_xattr(name))
2236 + type = ovl_path_real(dentry, &realpath);
2237 + if (type == OVL_PATH_LOWER) {
2238 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
2242 + err = ovl_copy_up(dentry);
2246 + ovl_path_upper(dentry, &realpath);
2249 + return vfs_removexattr(realpath.dentry, name);
2252 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
2253 + struct dentry *realdentry)
2255 + if (type != OVL_PATH_LOWER)
2258 + if (special_file(realdentry->d_inode->i_mode))
2261 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
2267 +static struct file *ovl_open(struct dentry *dentry, int flags,
2268 + const struct cred *cred)
2271 + struct path realpath;
2272 + enum ovl_path_type type;
2274 + type = ovl_path_real(dentry, &realpath);
2275 + if (ovl_open_need_copy_up(flags, type, realpath.dentry)) {
2276 + if (flags & O_TRUNC)
2277 + err = ovl_copy_up_truncate(dentry, 0);
2279 + err = ovl_copy_up(dentry);
2281 + return ERR_PTR(err);
2283 + ovl_path_upper(dentry, &realpath);
2286 + return vfs_open(&realpath, flags, cred);
2289 +static const struct inode_operations ovl_dir_inode_operations = {
2290 + .lookup = ovl_lookup,
2291 + .mkdir = ovl_mkdir,
2292 + .symlink = ovl_symlink,
2293 + .unlink = ovl_unlink,
2294 + .rmdir = ovl_rmdir,
2295 + .rename = ovl_rename,
2297 + .setattr = ovl_setattr,
2298 + .create = ovl_create,
2299 + .mknod = ovl_mknod,
2300 + .permission = ovl_permission,
2301 + .getattr = ovl_dir_getattr,
2302 + .setxattr = ovl_setxattr,
2303 + .getxattr = ovl_getxattr,
2304 + .listxattr = ovl_listxattr,
2305 + .removexattr = ovl_removexattr,
2308 +static const struct inode_operations ovl_file_inode_operations = {
2309 + .setattr = ovl_setattr,
2310 + .permission = ovl_permission,
2311 + .getattr = ovl_getattr,
2312 + .setxattr = ovl_setxattr,
2313 + .getxattr = ovl_getxattr,
2314 + .listxattr = ovl_listxattr,
2315 + .removexattr = ovl_removexattr,
2319 +static const struct inode_operations ovl_symlink_inode_operations = {
2320 + .setattr = ovl_setattr,
2321 + .follow_link = ovl_follow_link,
2322 + .put_link = ovl_put_link,
2323 + .readlink = ovl_readlink,
2324 + .getattr = ovl_getattr,
2325 + .setxattr = ovl_setxattr,
2326 + .getxattr = ovl_getxattr,
2327 + .listxattr = ovl_listxattr,
2328 + .removexattr = ovl_removexattr,
2331 +static struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
2332 + struct ovl_entry *oe)
2334 + struct inode *inode;
2336 + inode = new_inode(sb);
2342 + inode->i_ino = get_next_ino();
2343 + inode->i_mode = mode;
2344 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
2348 + inode->i_private = oe;
2349 + inode->i_op = &ovl_dir_inode_operations;
2350 + inode->i_fop = &ovl_dir_operations;
2354 + inode->i_op = &ovl_symlink_inode_operations;
2362 + inode->i_op = &ovl_file_inode_operations;
2366 + WARN(1, "illegal file type: %i\n", mode);
2374 +static void ovl_put_super(struct super_block *sb)
2376 + struct ovl_fs *ufs = sb->s_fs_info;
2378 + if (!(sb->s_flags & MS_RDONLY))
2379 + mnt_drop_write(ufs->upper_mnt);
2381 + mntput(ufs->upper_mnt);
2382 + mntput(ufs->lower_mnt);
2387 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2389 + int flags = *flagsp;
2390 + struct ovl_fs *ufs = sb->s_fs_info;
2392 + /* When remounting rw or ro, we need to adjust the write access to the
2395 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2396 + /* No change to readonly status */
2399 + if (flags & MS_RDONLY) {
2400 + mnt_drop_write(ufs->upper_mnt);
2403 + return mnt_want_write(ufs->upper_mnt);
2408 + * @sb: The overlayfs super block
2409 + * @buf: The struct kstatfs to fill in with stats
2411 + * Get the filesystem statistics. As writes always target the upper layer
2412 + * filesystem pass the statfs to the same filesystem.
2414 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2416 + struct dentry *root_dentry = dentry->d_sb->s_root;
2418 + ovl_path_upper(root_dentry, &path);
2420 + if (!path.dentry->d_sb->s_op->statfs)
2422 + return path.dentry->d_sb->s_op->statfs(path.dentry, buf);
2425 +static const struct super_operations ovl_super_operations = {
2426 + .put_super = ovl_put_super,
2427 + .remount_fs = ovl_remount_fs,
2428 + .statfs = ovl_statfs,
2431 +struct ovl_config {
2442 +static const match_table_t ovl_tokens = {
2443 + {Opt_lowerdir, "lowerdir=%s"},
2444 + {Opt_upperdir, "upperdir=%s"},
2448 +static int ovl_parse_opt(char *opt, struct ovl_config *config)
2452 + config->upperdir = NULL;
2453 + config->lowerdir = NULL;
2455 + while ((p = strsep(&opt, ",")) != NULL) {
2457 + substring_t args[MAX_OPT_ARGS];
2462 + token = match_token(p, ovl_tokens, args);
2464 + case Opt_upperdir:
2465 + kfree(config->upperdir);
2466 + config->upperdir = match_strdup(&args[0]);
2467 + if (!config->upperdir)
2471 + case Opt_lowerdir:
2472 + kfree(config->lowerdir);
2473 + config->lowerdir = match_strdup(&args[0]);
2474 + if (!config->lowerdir)
2485 +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
2487 + struct path lowerpath;
2488 + struct path upperpath;
2489 + struct inode *root_inode;
2490 + struct dentry *root_dentry;
2491 + struct ovl_entry *oe;
2492 + struct ovl_fs *ufs;
2493 + struct ovl_config config;
2496 + err = ovl_parse_opt((char *) data, &config);
2501 + if (!config.upperdir || !config.lowerdir) {
2502 + printk(KERN_ERR "overlayfs: missing upperdir or lowerdir\n");
2503 + goto out_free_config;
2507 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
2509 + goto out_free_config;
2511 + oe = ovl_alloc_entry();
2513 + goto out_free_ufs;
2515 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
2519 + err = kern_path(config.upperdir, LOOKUP_FOLLOW, &upperpath);
2521 + goto out_put_root;
2523 + err = kern_path(config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
2525 + goto out_put_upperpath;
2528 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
2529 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
2530 + goto out_put_lowerpath;
2532 + ufs->upper_mnt = clone_private_mount(&upperpath);
2533 + err = PTR_ERR(ufs->upper_mnt);
2534 + if (IS_ERR(ufs->upper_mnt)) {
2535 + printk(KERN_ERR "overlayfs: failed to clone upperpath\n");
2536 + goto out_put_lowerpath;
2539 + ufs->lower_mnt = clone_private_mount(&lowerpath);
2540 + err = PTR_ERR(ufs->lower_mnt);
2541 + if (IS_ERR(ufs->lower_mnt)) {
2542 + printk(KERN_ERR "overlayfs: failed to clone lowerpath\n");
2543 + goto out_put_upper_mnt;
2546 + if (!(sb->s_flags & MS_RDONLY)) {
2547 + err = mnt_want_write(ufs->upper_mnt);
2549 + goto out_put_lower_mnt;
2553 + root_dentry = d_alloc_root(root_inode);
2555 + goto out_drop_write;
2557 + mntput(upperpath.mnt);
2558 + mntput(lowerpath.mnt);
2560 + oe->__upperdentry = upperpath.dentry;
2561 + oe->lowerdentry = lowerpath.dentry;
2563 + root_dentry->d_fsdata = oe;
2564 + root_dentry->d_op = &ovl_dentry_operations;
2566 + sb->s_op = &ovl_super_operations;
2567 + sb->s_root = root_dentry;
2568 + sb->s_fs_info = ufs;
2573 + if (!(sb->s_flags & MS_RDONLY))
2574 + mnt_drop_write(ufs->upper_mnt);
2576 + mntput(ufs->lower_mnt);
2578 + mntput(ufs->upper_mnt);
2580 + path_put(&lowerpath);
2582 + path_put(&upperpath);
2590 + kfree(config.lowerdir);
2591 + kfree(config.upperdir);
2596 +static int ovl_get_sb(struct file_system_type *fs_type,
2597 + int flags, const char *dev_name,
2598 + void *raw_data, struct vfsmount *mnt)
2600 + return get_sb_nodev(fs_type, flags, raw_data, ovl_fill_super, mnt);
2603 +static struct file_system_type ovl_fs_type = {
2604 + .owner = THIS_MODULE,
2605 + .name = "overlayfs",
2606 + .get_sb = ovl_get_sb,
2607 + .kill_sb = kill_anon_super,
2610 +static int __init ovl_init(void)
2612 + return register_filesystem(&ovl_fs_type);
2615 +static void __exit ovl_exit(void)
2617 + unregister_filesystem(&ovl_fs_type);
2620 +module_init(ovl_init);
2621 +module_exit(ovl_exit);
2623 +++ b/Documentation/filesystems/overlayfs.txt
2625 +Written by: Neil Brown <neilb@suse.de>
2630 +This document describes a prototype for a new approach to providing
2631 +overlay-filesystem functionality in Linux (sometimes referred to as
2632 +union-filesystems). An overlay-filesystem tries to present a
2633 +filesystem which is the result over overlaying one filesystem on top
2636 +The result will inevitably fail to look exactly like a normal
2637 +filesystem for various technical reasons. The expectation is that
2638 +many use cases will be able to ignore these differences.
2640 +This approach is 'hybrid' because the objects that appear in the
2641 +filesystem do not all appear to belong to that filesystem. In many
2642 +case an object accessed in the union will be indistinguishable
2643 +from accessing the corresponding object from the original filesystem.
2644 +This is most obvious from the 'st_dev' field returned by stat(2).
2646 +While directories will report an st_dev for the overlay-filesystem,
2647 +all non-directory objects will report an st_dev whichever of the
2648 +'lower' or 'upper' filesystem that is providing the object. Similarly
2649 +st_ino will only be unique when combined with st_dev, and both of
2650 +these can change over the lifetime of a non-directory object. Many
2651 +applications and tools ignore these values and will not be affected.
2656 +An overlay filesystem combines two filesystems - an 'upper' filesystem
2657 +and a 'lower' filesystem. When a name exists in both filesystems, the
2658 +object in the 'upper' filesystem is visible while the object in the
2659 +'lower' filesystem is either hidden or, in the case of directories,
2660 +merged with the 'upper' object.
2662 +It would be more correct to refer to an upper and lower 'directory
2663 +tree' rather than 'filesystem' as it is quite possible for both
2664 +directory trees to be in the same filesystem and there is no
2665 +requirement that the root of a filesystem be given for either upper or
2668 +The lower filesystem can be any filesystem supported by Linux and does
2669 +not need to be writable. The lower filesystem can even be another
2670 +overlayfs. The upper filesystem will normally be writable and if it
2671 +is it must support the creation of trusted.* extended attributes, and
2672 +must provide valid d_type in readdir responses, at least for symbolic
2673 +links - so NFS is not suitable.
2675 +A read-only overlay of two read-only filesystems may use any
2681 +Overlaying mainly involved directories. If a given name appears in both
2682 +upper and lower filesystems and refers to a non-directory in either,
2683 +then the lower object is hidden - the name refers only to the upper
2686 +Where both upper and lower objects are directories, a merged directory
2689 +At mount time, the two directories given as mount options are combined
2690 +into a merged directory. Then whenever a lookup is requested in such
2691 +a merged directory, the lookup is performed in each actual directory
2692 +and the combined result is cached in the dentry belonging to the overlay
2693 +filesystem. If both actual lookups find directories, both are stored
2694 +and a merged directory is created, otherwise only one is stored: the
2695 +upper if it exists, else the lower.
2697 +Only the lists of names from directories are merged. Other content
2698 +such as metadata and extended attributes are reported for the upper
2699 +directory only. These attributes of the lower directory are hidden.
2701 +whiteouts and opaque directories
2702 +--------------------------------
2704 +In order to support rm and rmdir without changing the lower
2705 +filesystem, an overlay filesystem needs to record in the upper filesystem
2706 +that files have been removed. This is done using whiteouts and opaque
2707 +directories (non-directories are always opaque).
2709 +The overlay filesystem uses extended attributes with a
2710 +"trusted.overlay." prefix to record these details.
2712 +A whiteout is created as a symbolic link with target
2713 +"(overlay-whiteout)" and with xattr "trusted.overlay.whiteout" set to "y".
2714 +When a whiteout is found in the upper level of a merged directory, any
2715 +matching name in the lower level is ignored, and the whiteout itself
2718 +A directory is made opaque by setting the xattr "trusted.overlay.opaque"
2719 +to "y". Where the upper filesystem contains an opaque directory, any
2720 +directory in the lower filesystem with the same name is ignored.
2725 +When a 'readdir' request is made on a merged directory, the upper and
2726 +lower directories are each read and the name lists merged in the
2727 +obvious way (upper is read first, then lower - entries that already
2728 +exist are not re-added). This merged name list is cached in the
2729 +'struct file' and so remains as long as the file is kept open. If the
2730 +directory is opened and read by two processes at the same time, they
2731 +will each have separate caches. A seekdir to the start of the
2732 +directory (offset 0) followed by a readdir will cause the cache to be
2733 +discarded and rebuilt.
2735 +This means that changes to the merged directory do not appear while a
2736 +directory is being read. This is unlikely to be noticed by many
2739 +seek offsets are assigned sequentially when the directories are read.
2741 + - read part of a directory
2742 + - remember an offset, and close the directory
2743 + - re-open the directory some time later
2744 + - seek to the remembered offset
2746 +there may be little correlation between the old and new locations in
2747 +the list of filenames, particularly if anything has changed in the
2750 +Readdir on directories that are not merged is simply handled by the
2751 +underlying directory (upper or lower).
2757 +Objects that are not directories (files, symlinks, device-special
2758 +files etc) are presented either from the upper or lower filesystem as
2759 +appropriate. When a file in the lower filesystem is accessed in a way
2760 +the requires write-access; such as opening for write access, changing
2761 +some metadata etc, the file is first copied from the lower filesystem
2762 +to the upper filesystem (copy_up). Note that creating a hard-link
2763 +also requires copy-up, though of course creation of a symlink does
2766 +The copy_up process first makes sure that the containing directory
2767 +exists in the upper filesystem - creating it and any parents as
2768 +necessary. It then creates the object with the same metadata (owner,
2769 +mode, mtime, symlink-target etc) and then if the object is a file, the
2770 +data is copied from the lower to the upper filesystem. Finally any
2771 +extended attributes are copied up.
2773 +Once the copy_up is complete, the overlay filesystem simply
2774 +provides direct access to the newly created file in the upper
2775 +filesystem - future operations on the file are barely noticed by the
2776 +overlay filesystem (though an operation on the name of the file such as
2777 +rename or unlink will of course be noticed and handled).
2779 +Changes to underlying filesystems
2780 +---------------------------------
2782 +Offline changes, when the overlay is not mounted, are allowed to either
2783 +the upper or the lower trees.
2785 +Changes to the underlying filesystems while part of a mounted overlay
2786 +filesystem are not allowed. This is not yet enforced, but will be in