1 --- a/include/linux/fs.h
2 +++ b/include/linux/fs.h
3 @@ -1594,6 +1594,7 @@ struct inode_operations {
4 void (*truncate_range)(struct inode *, loff_t, loff_t);
5 int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
7 + struct file *(*open)(struct dentry *, int flags, const struct cred *);
8 } ____cacheline_aligned;
11 @@ -1988,6 +1989,7 @@ extern long do_sys_open(int dfd, const c
12 extern struct file *filp_open(const char *, int, int);
13 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
15 +extern struct file *vfs_open(struct path *, int flags, const struct cred *);
16 extern struct file * dentry_open(struct dentry *, struct vfsmount *, int,
18 extern int filp_close(struct file *, fl_owner_t id);
21 @@ -666,8 +666,7 @@ static inline int __get_file_write_acces
25 -static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
27 +static struct file *__dentry_open(struct path *path, struct file *f,
28 int (*open)(struct inode *, struct file *),
29 const struct cred *cred)
31 @@ -675,15 +674,16 @@ static struct file *__dentry_open(struct
36 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
37 FMODE_PREAD | FMODE_PWRITE;
39 if (unlikely(f->f_flags & O_PATH))
40 f->f_mode = FMODE_PATH;
42 - inode = dentry->d_inode;
43 + inode = path->dentry->d_inode;
44 if (f->f_mode & FMODE_WRITE) {
45 - error = __get_file_write_access(inode, mnt);
46 + error = __get_file_write_access(inode, path->mnt);
49 if (!special_file(inode->i_mode))
50 @@ -691,8 +691,7 @@ static struct file *__dentry_open(struct
53 f->f_mapping = inode->i_mapping;
54 - f->f_path.dentry = dentry;
55 - f->f_path.mnt = mnt;
58 file_sb_list_add(f, inode->i_sb);
60 @@ -745,7 +744,7 @@ cleanup_all:
61 * here, so just reset the state.
64 - mnt_drop_write(mnt);
65 + mnt_drop_write(path->mnt);
69 @@ -753,8 +752,7 @@ cleanup_all:
76 return ERR_PTR(error);
79 @@ -780,14 +778,14 @@ cleanup_file:
80 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
81 int (*open)(struct inode *, struct file *))
83 + struct path path = { .dentry = dentry, .mnt = nd->path.mnt };
84 const struct cred *cred = current_cred();
86 if (IS_ERR(nd->intent.open.file))
90 - nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
91 - nd->intent.open.file,
92 + nd->intent.open.file = __dentry_open(&path, nd->intent.open.file,
95 return nd->intent.open.file;
96 @@ -816,10 +814,17 @@ struct file *nameidata_to_filp(struct na
98 /* Has the filesystem initialised the file for us? */
99 if (filp->f_path.dentry == NULL) {
100 - path_get(&nd->path);
101 - filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
103 + struct inode *inode = nd->path.dentry->d_inode;
105 + if (inode->i_op->open) {
106 + int flags = filp->f_flags;
108 + filp = inode->i_op->open(nd->path.dentry, flags, cred);
110 + filp = __dentry_open(&nd->path, filp, NULL, cred);
117 @@ -830,26 +835,45 @@ struct file *nameidata_to_filp(struct na
118 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
119 const struct cred *cred)
124 - validate_creds(cred);
125 + struct path path = { .dentry = dentry, .mnt = mnt };
128 /* We must always pass in a valid mount pointer. */
132 + ret = vfs_open(&path, flags, cred);
137 +EXPORT_SYMBOL(dentry_open);
140 + * vfs_open - open the file at the given path
141 + * @path: path to open
142 + * @flags: open flags
143 + * @cred: credentials to use
145 + * Open the file. If successful, the returned file will have acquired
146 + * an additional reference for path.
148 +struct file *vfs_open(struct path *path, int flags, const struct cred *cred)
151 + struct inode *inode = path->dentry->d_inode;
153 + validate_creds(cred);
155 + if (inode->i_op->open)
156 + return inode->i_op->open(path->dentry, flags, cred);
157 f = get_empty_filp();
161 - return ERR_PTR(error);
164 + return ERR_PTR(-ENFILE);
167 - return __dentry_open(dentry, mnt, f, NULL, cred);
168 + return __dentry_open(path, f, NULL, cred);
170 -EXPORT_SYMBOL(dentry_open);
171 +EXPORT_SYMBOL(vfs_open);
173 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
177 @@ -1296,6 +1296,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 @@ -1494,6 +1494,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);
224 +++ b/fs/overlayfs/overlayfs.c
226 +#include <linux/fs.h>
227 +#include <linux/namei.h>
228 +#include <linux/sched.h>
229 +#include <linux/fs_struct.h>
230 +#include <linux/file.h>
231 +#include <linux/xattr.h>
232 +#include <linux/security.h>
233 +#include <linux/device_cgroup.h>
234 +#include <linux/mount.h>
235 +#include <linux/splice.h>
236 +#include <linux/slab.h>
237 +#include <linux/parser.h>
238 +#include <linux/module.h>
239 +#include <linux/uaccess.h>
240 +#include <linux/rbtree.h>
242 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
243 +MODULE_DESCRIPTION("Overlay filesystem");
244 +MODULE_LICENSE("GPL");
246 +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
249 + struct vfsmount *upper_mnt;
250 + struct vfsmount *lower_mnt;
254 + struct dentry *__upperdentry;
255 + struct dentry *lowerdentry;
261 + struct rcu_head rcu;
265 +static const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
266 +static const char *ovl_opaque_xattr = "trusted.overlay.opaque";
267 +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
269 +enum ovl_path_type {
275 +static enum ovl_path_type ovl_path_type(struct dentry *dentry)
277 + struct ovl_entry *oe = dentry->d_fsdata;
279 + if (oe->__upperdentry) {
280 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
281 + return OVL_PATH_MERGE;
283 + return OVL_PATH_UPPER;
285 + return OVL_PATH_LOWER;
289 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
291 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
292 + smp_read_barrier_depends();
293 + return upperdentry;
296 +static void ovl_path_upper(struct dentry *dentry, struct path *path)
298 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
299 + struct ovl_entry *oe = dentry->d_fsdata;
301 + path->mnt = ofs->upper_mnt;
302 + path->dentry = ovl_upperdentry_dereference(oe);
305 +static void ovl_path_lower(struct dentry *dentry, struct path *path)
307 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
308 + struct ovl_entry *oe = dentry->d_fsdata;
310 + path->mnt = ofs->lower_mnt;
311 + path->dentry = oe->lowerdentry;
314 +static enum ovl_path_type ovl_path_real(struct dentry *dentry,
318 + enum ovl_path_type type = ovl_path_type(dentry);
320 + if (type == OVL_PATH_LOWER)
321 + ovl_path_lower(dentry, path);
323 + ovl_path_upper(dentry, path);
328 +static struct dentry *ovl_dentry_upper(struct dentry *dentry)
330 + struct ovl_entry *oe = dentry->d_fsdata;
332 + return ovl_upperdentry_dereference(oe);
335 +static struct dentry *ovl_dentry_lower(struct dentry *dentry)
337 + struct ovl_entry *oe = dentry->d_fsdata;
339 + return oe->lowerdentry;
342 +static struct dentry *ovl_dentry_real(struct dentry *dentry)
344 + struct ovl_entry *oe = dentry->d_fsdata;
345 + struct dentry *realdentry;
347 + realdentry = ovl_upperdentry_dereference(oe);
349 + realdentry = oe->lowerdentry;
354 +static bool ovl_dentry_is_opaque(struct dentry *dentry)
356 + struct ovl_entry *oe = dentry->d_fsdata;
360 +static void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
362 + struct ovl_entry *oe = dentry->d_fsdata;
363 + oe->opaque = opaque;
366 +static void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
368 + struct ovl_entry *oe = dentry->d_fsdata;
370 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
371 + WARN_ON(oe->__upperdentry);
373 + oe->__upperdentry = upperdentry;
376 +static void ovl_dentry_version_inc(struct dentry *dentry)
378 + struct ovl_entry *oe = dentry->d_fsdata;
380 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
384 +static u64 ovl_dentry_version_get(struct dentry *dentry)
386 + struct ovl_entry *oe = dentry->d_fsdata;
388 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
389 + return oe->version;
392 +static bool ovl_is_whiteout(struct dentry *dentry)
399 + if (!dentry->d_inode)
401 + if (!S_ISLNK(dentry->d_inode->i_mode))
404 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
405 + if (res == 1 && val == 'y')
411 +static bool ovl_is_opaquedir(struct dentry *dentry)
416 + if (!S_ISDIR(dentry->d_inode->i_mode))
419 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
420 + if (res == 1 && val == 'y')
426 +struct ovl_cache_entry {
432 + struct list_head l_node;
433 + struct rb_node node;
436 +struct ovl_readdir_data {
437 + struct rb_root *root;
438 + struct list_head *list;
439 + struct list_head *middle;
440 + struct dentry *dir;
445 +struct ovl_dir_file {
448 + struct list_head cursor;
450 + struct list_head cache;
451 + struct file *realfile;
454 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
456 + return container_of(n, struct ovl_cache_entry, node);
459 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
460 + const char *name, int len)
462 + struct rb_node *node = root->rb_node;
466 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
468 + cmp = strncmp(name, p->name, len);
470 + node = p->node.rb_right;
471 + else if (cmp < 0 || len < p->len)
472 + node = p->node.rb_left;
480 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
481 + u64 ino, unsigned int d_type)
483 + struct ovl_cache_entry *p;
485 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
487 + char *name_copy = (char *) (p + 1);
488 + memcpy(name_copy, name, len);
489 + name_copy[len] = '\0';
490 + p->name = name_copy;
494 + p->is_whiteout = false;
500 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
501 + const char *name, int len, u64 ino,
502 + unsigned int d_type)
504 + struct rb_node **newp = &rdd->root->rb_node;
505 + struct rb_node *parent = NULL;
506 + struct ovl_cache_entry *p;
510 + struct ovl_cache_entry *tmp;
513 + tmp = ovl_cache_entry_from_node(*newp);
514 + cmp = strncmp(name, tmp->name, len);
516 + newp = &tmp->node.rb_right;
517 + else if (cmp < 0 || len < tmp->len)
518 + newp = &tmp->node.rb_left;
523 + p = ovl_cache_entry_new(name, len, ino, d_type);
527 + list_add_tail(&p->l_node, rdd->list);
528 + rb_link_node(&p->node, parent, newp);
529 + rb_insert_color(&p->node, rdd->root);
534 +static int ovl_fill_lower(void *buf, const char *name, int namelen,
535 + loff_t offset, u64 ino, unsigned int d_type)
537 + struct ovl_readdir_data *rdd = buf;
538 + struct ovl_cache_entry *p;
541 + p = ovl_cache_entry_find(rdd->root, name, namelen);
543 + list_move_tail(&p->l_node, rdd->middle);
545 + p = ovl_cache_entry_new(name, namelen, ino, d_type);
547 + rdd->err = -ENOMEM;
549 + list_add_tail(&p->l_node, rdd->middle);
555 +static void ovl_cache_free(struct list_head *list)
557 + struct ovl_cache_entry *p;
558 + struct ovl_cache_entry *n;
560 + list_for_each_entry_safe(p, n, list, l_node)
563 + INIT_LIST_HEAD(list);
566 +static int ovl_fill_upper(void *buf, const char *name, int namelen,
567 + loff_t offset, u64 ino, unsigned int d_type)
569 + struct ovl_readdir_data *rdd = buf;
572 + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
575 +static int ovl_dir_read(struct path *realpath, struct ovl_readdir_data *rdd,
578 + struct file *realfile;
581 + realfile = vfs_open(realpath, O_RDONLY | O_DIRECTORY, current_cred());
582 + if (IS_ERR(realfile))
583 + return PTR_ERR(realfile);
588 + err = vfs_readdir(realfile, filler, rdd);
591 + } while (!err && rdd->count);
597 +static void ovl_dir_reset(struct file *file)
599 + struct ovl_dir_file *od = file->private_data;
600 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
602 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
603 + list_del_init(&od->cursor);
604 + ovl_cache_free(&od->cache);
605 + od->is_cached = false;
607 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
608 + if (od->is_real && type == OVL_PATH_MERGE) {
609 + fput(od->realfile);
610 + od->realfile = NULL;
611 + od->is_real = false;
615 +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
617 + struct ovl_cache_entry *p;
618 + struct dentry *dentry;
619 + const struct cred *old_cred;
620 + struct cred *override_cred;
622 + override_cred = prepare_creds();
623 + if (!override_cred) {
624 + ovl_cache_free(rdd->list);
629 + * CAP_SYS_ADMIN for getxattr
630 + * CAP_DAC_OVERRIDE for lookup
632 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
633 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
634 + old_cred = override_creds(override_cred);
636 + mutex_lock(&rdd->dir->d_inode->i_mutex);
637 + list_for_each_entry(p, rdd->list, l_node) {
638 + if (p->type != DT_LNK)
641 + dentry = lookup_one_len(p->name, rdd->dir, p->len);
642 + if (IS_ERR(dentry))
645 + p->is_whiteout = ovl_is_whiteout(dentry);
648 + mutex_unlock(&rdd->dir->d_inode->i_mutex);
650 + revert_creds(old_cred);
651 + put_cred(override_cred);
656 +static int ovl_dir_read_merged(struct path *upperpath, struct path *lowerpath,
657 + struct ovl_readdir_data *rdd)
660 + struct rb_root root = RB_ROOT;
661 + struct list_head middle;
664 + if (upperpath->dentry) {
665 + rdd->dir = upperpath->dentry;
666 + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
670 + err = ovl_dir_mark_whiteouts(rdd);
675 + * Insert lowerpath entries before upperpath ones, this allows
676 + * offsets to be reasonably constant
678 + list_add(&middle, rdd->list);
679 + rdd->middle = &middle;
680 + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower);
688 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
690 + struct list_head *l;
693 + l = od->cache.next;
694 + for (off = 0; off < pos; off++) {
695 + if (l == &od->cache)
699 + list_move_tail(&od->cursor, l);
702 +static int ovl_readdir(struct file *file, void *buf, filldir_t filler)
704 + struct ovl_dir_file *od = file->private_data;
708 + ovl_dir_reset(file);
711 + res = vfs_readdir(od->realfile, filler, buf);
712 + file->f_pos = od->realfile->f_pos;
717 + if (!od->is_cached) {
718 + struct path lowerpath;
719 + struct path upperpath;
720 + struct ovl_readdir_data rdd = { .list = &od->cache };
722 + ovl_path_lower(file->f_path.dentry, &lowerpath);
723 + ovl_path_upper(file->f_path.dentry, &upperpath);
725 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
727 + ovl_cache_free(rdd.list);
731 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
732 + od->is_cached = true;
734 + ovl_seek_cursor(od, file->f_pos);
737 + while (od->cursor.next != &od->cache) {
740 + struct ovl_cache_entry *p;
742 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
745 + list_move(&od->cursor, &p->l_node);
747 + if (p->is_whiteout)
750 + over = filler(buf, p->name, p->len, off, p->ino, p->type);
758 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
761 + struct ovl_dir_file *od = file->private_data;
763 + mutex_lock(&file->f_dentry->d_inode->i_mutex);
765 + ovl_dir_reset(file);
768 + res = vfs_llseek(od->realfile, offset, origin);
769 + file->f_pos = od->realfile->f_pos;
775 + offset += file->f_pos;
785 + if (offset != file->f_pos) {
786 + file->f_pos = offset;
788 + ovl_seek_cursor(od, offset);
793 + mutex_unlock(&file->f_dentry->d_inode->i_mutex);
798 +static int ovl_dir_fsync(struct file *file, int datasync)
800 + struct ovl_dir_file *od = file->private_data;
802 + /* May need to reopen directory if it got copied up */
803 + if (!od->realfile) {
804 + struct path upperpath;
806 + ovl_path_upper(file->f_path.dentry, &upperpath);
807 + od->realfile = vfs_open(&upperpath, O_RDONLY, current_cred());
808 + if (IS_ERR(od->realfile))
809 + return PTR_ERR(od->realfile);
812 + return vfs_fsync(od->realfile, datasync);
815 +static int ovl_dir_release(struct inode *inode, struct file *file)
817 + struct ovl_dir_file *od = file->private_data;
819 + list_del(&od->cursor);
820 + ovl_cache_free(&od->cache);
822 + fput(od->realfile);
828 +static int ovl_dir_open(struct inode *inode, struct file *file)
830 + struct path realpath;
831 + struct file *realfile;
832 + struct ovl_dir_file *od;
833 + enum ovl_path_type type;
835 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
839 + type = ovl_path_real(file->f_path.dentry, &realpath);
840 + realfile = vfs_open(&realpath, file->f_flags, current_cred());
841 + if (IS_ERR(realfile)) {
843 + return PTR_ERR(realfile);
845 + INIT_LIST_HEAD(&od->cache);
846 + INIT_LIST_HEAD(&od->cursor);
847 + od->is_cached = false;
848 + od->realfile = realfile;
849 + od->is_real = (type != OVL_PATH_MERGE);
850 + file->private_data = od;
855 +static const struct file_operations ovl_dir_operations = {
856 + .read = generic_read_dir,
857 + .open = ovl_dir_open,
858 + .readdir = ovl_readdir,
859 + .llseek = ovl_dir_llseek,
860 + .fsync = ovl_dir_fsync,
861 + .release = ovl_dir_release,
864 +static const struct inode_operations ovl_dir_inode_operations;
866 +static void ovl_entry_free(struct rcu_head *head)
868 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
872 +static void ovl_dentry_release(struct dentry *dentry)
874 + struct ovl_entry *oe = dentry->d_fsdata;
877 + dput(oe->__upperdentry);
878 + dput(oe->lowerdentry);
879 + call_rcu(&oe->rcu, ovl_entry_free);
883 +static const struct dentry_operations ovl_dentry_operations = {
884 + .d_release = ovl_dentry_release,
887 +static struct dentry *ovl_lookup_real(struct dentry *dir, struct qstr *name)
889 + struct dentry *dentry;
891 + mutex_lock(&dir->d_inode->i_mutex);
892 + dentry = lookup_one_len(name->name, dir, name->len);
893 + mutex_unlock(&dir->d_inode->i_mutex);
895 + if (IS_ERR(dentry)) {
896 + if (PTR_ERR(dentry) == -ENOENT)
898 + } else if (!dentry->d_inode) {
905 +static struct ovl_entry *ovl_alloc_entry(void)
907 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
910 +static struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
911 + struct ovl_entry *oe);
913 +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
916 + struct dentry *newdentry;
917 + const struct cred *old_cred;
918 + struct cred *override_cred;
920 + /* FIXME: recheck lower dentry to see if whiteout is really needed */
923 + override_cred = prepare_creds();
924 + if (!override_cred)
928 + * CAP_SYS_ADMIN for setxattr
929 + * CAP_DAC_OVERRIDE for symlink creation
931 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
932 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
933 + override_cred->fsuid = 0;
934 + override_cred->fsgid = 0;
935 + old_cred = override_creds(override_cred);
937 + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
938 + dentry->d_name.len);
939 + err = PTR_ERR(newdentry);
940 + if (IS_ERR(newdentry))
943 + /* Just been removed within the same locked region */
944 + WARN_ON(newdentry->d_inode);
946 + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
950 + ovl_dentry_version_inc(dentry->d_parent);
952 + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
954 + vfs_unlink(upperdir->d_inode, newdentry);
959 + revert_creds(old_cred);
960 + put_cred(override_cred);
964 + * There's no way to recover from failure to whiteout.
965 + * What should we do? Log a big fat error and... ?
967 + printk(KERN_ERR "overlayfs: ERROR - failed to whiteout '%s'\n",
968 + dentry->d_name.name);
974 +static struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
975 + struct nameidata *nd)
977 + struct ovl_entry *oe;
978 + struct dentry *upperdir;
979 + struct dentry *lowerdir;
980 + struct dentry *upperdentry = NULL;
981 + struct dentry *lowerdentry = NULL;
982 + struct inode *inode = NULL;
986 + oe = ovl_alloc_entry();
990 + upperdir = ovl_dentry_upper(dentry->d_parent);
991 + lowerdir = ovl_dentry_lower(dentry->d_parent);
994 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
995 + err = PTR_ERR(upperdentry);
996 + if (IS_ERR(upperdentry))
999 + if (lowerdir && upperdentry &&
1000 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
1001 + S_ISDIR(upperdentry->d_inode->i_mode))) {
1002 + const struct cred *old_cred;
1003 + struct cred *override_cred;
1006 + override_cred = prepare_creds();
1007 + if (!override_cred)
1008 + goto out_dput_upper;
1010 + /* CAP_SYS_ADMIN needed for getxattr */
1011 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1012 + old_cred = override_creds(override_cred);
1014 + if (ovl_is_opaquedir(upperdentry)) {
1015 + oe->opaque = true;
1016 + } else if (ovl_is_whiteout(upperdentry)) {
1017 + dput(upperdentry);
1018 + upperdentry = NULL;
1019 + oe->opaque = true;
1021 + revert_creds(old_cred);
1022 + put_cred(override_cred);
1025 + if (lowerdir && !oe->opaque) {
1026 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
1027 + err = PTR_ERR(lowerdentry);
1028 + if (IS_ERR(lowerdentry))
1029 + goto out_dput_upper;
1032 + if (lowerdentry && upperdentry &&
1033 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
1034 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
1035 + dput(lowerdentry);
1036 + lowerdentry = NULL;
1037 + oe->opaque = true;
1040 + if (lowerdentry || upperdentry) {
1041 + struct dentry *realdentry;
1043 + realdentry = upperdentry ? upperdentry : lowerdentry;
1045 + inode = ovl_new_inode(dir->i_sb, realdentry->d_inode->i_mode, oe);
1051 + oe->__upperdentry = upperdentry;
1054 + oe->lowerdentry = lowerdentry;
1056 + dentry->d_fsdata = oe;
1057 + dentry->d_op = &ovl_dentry_operations;
1058 + d_add(dentry, inode);
1063 + dput(lowerdentry);
1065 + dput(upperdentry);
1069 + return ERR_PTR(err);
1072 +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
1074 + ssize_t list_size, size;
1075 + char *buf, *name, *value;
1078 + if (!old->d_inode->i_op->getxattr ||
1079 + !new->d_inode->i_op->getxattr)
1082 + list_size = vfs_listxattr(old, NULL, 0);
1083 + if (list_size <= 0) {
1084 + if (list_size == -EOPNOTSUPP)
1089 + buf = kzalloc(list_size, GFP_KERNEL);
1094 + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
1098 + list_size = vfs_listxattr(old, buf, list_size);
1099 + if (list_size <= 0) {
1100 + error = list_size;
1101 + goto out_free_value;
1104 + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
1105 + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
1108 + goto out_free_value;
1110 + error = vfs_setxattr(new, name, value, size, 0);
1112 + goto out_free_value;
1122 +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
1124 + struct file *old_file;
1125 + struct file *new_file;
1131 + old_file = vfs_open(old, O_RDONLY, current_cred());
1132 + if (IS_ERR(old_file))
1133 + return PTR_ERR(old_file);
1135 + new_file = vfs_open(new, O_WRONLY, current_cred());
1136 + if (IS_ERR(new_file)) {
1137 + error = PTR_ERR(new_file);
1141 + /* FIXME: copy up sparse files efficiently */
1143 + loff_t offset = new_file->f_pos;
1144 + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
1147 + if (len < this_len)
1150 + if (signal_pending_state(TASK_KILLABLE, current)) {
1155 + bytes = do_splice_direct(old_file, &offset, new_file, this_len,
1171 +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
1172 + struct dentry *template)
1175 + struct dentry *newdentry;
1176 + struct qstr *name = &template->d_name;
1178 + newdentry = lookup_one_len(name->name, upperdir, name->len);
1179 + if (IS_ERR(newdentry))
1182 + if (newdentry->d_inode) {
1183 + const struct cred *old_cred;
1184 + struct cred *override_cred;
1186 + /* No need to check whiteout if lower parent is non-existent */
1188 + if (!ovl_dentry_lower(template->d_parent))
1191 + if (!S_ISLNK(newdentry->d_inode->i_mode))
1195 + override_cred = prepare_creds();
1196 + if (!override_cred)
1200 + * CAP_SYS_ADMIN for getxattr
1201 + * CAP_FOWNER for unlink in sticky directory
1203 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1204 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
1205 + old_cred = override_creds(override_cred);
1208 + if (ovl_is_whiteout(newdentry))
1209 + err = vfs_unlink(upperdir->d_inode, newdentry);
1211 + revert_creds(old_cred);
1212 + put_cred(override_cred);
1217 + newdentry = lookup_one_len(name->name, upperdir, name->len);
1218 + if (IS_ERR(newdentry)) {
1219 + ovl_whiteout(upperdir, template);
1224 + * Whiteout just been successfully removed, parent
1225 + * i_mutex is still held, there's no way the lookup
1226 + * could return positive.
1228 + WARN_ON(newdentry->d_inode);
1235 + return ERR_PTR(err);
1238 +static struct dentry *ovl_upper_create(struct dentry *upperdir,
1239 + struct dentry *dentry,
1240 + struct kstat *stat, const char *link)
1243 + struct dentry *newdentry;
1244 + struct inode *dir = upperdir->d_inode;
1246 + newdentry = ovl_lookup_create(upperdir, dentry);
1247 + if (IS_ERR(newdentry))
1250 + switch (stat->mode & S_IFMT) {
1252 + err = vfs_create(dir, newdentry, stat->mode, NULL);
1256 + err = vfs_mkdir(dir, newdentry, stat->mode);
1263 + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
1267 + err = vfs_symlink(dir, newdentry, link);
1274 + if (ovl_dentry_is_opaque(dentry))
1275 + ovl_whiteout(upperdir, dentry);
1277 + newdentry = ERR_PTR(err);
1285 +static char *ovl_read_symlink(struct dentry *realdentry)
1289 + struct inode *inode = realdentry->d_inode;
1290 + mm_segment_t old_fs;
1293 + if (!inode->i_op->readlink)
1297 + buf = (char *) __get_free_page(GFP_KERNEL);
1301 + old_fs = get_fs();
1303 + /* The cast to a user pointer is valid due to the set_fs() */
1304 + res = inode->i_op->readlink(realdentry,
1305 + (char __user *)buf, PAGE_SIZE - 1);
1308 + free_page((unsigned long) buf);
1316 + return ERR_PTR(res);
1319 +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
1321 + struct iattr attr = {
1322 + .ia_valid = ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
1323 + .ia_atime = stat->atime,
1324 + .ia_mtime = stat->mtime,
1327 + return notify_change(upperdentry, &attr);
1330 +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
1332 + struct iattr attr = {
1333 + .ia_valid = ATTR_MODE,
1337 + return notify_change(upperdentry, &attr);
1340 +static int ovl_set_opaque(struct dentry *upperdentry)
1343 + const struct cred *old_cred;
1344 + struct cred *override_cred;
1346 + override_cred = prepare_creds();
1347 + if (!override_cred)
1350 + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
1351 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1352 + old_cred = override_creds(override_cred);
1353 + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
1354 + revert_creds(old_cred);
1355 + put_cred(override_cred);
1360 +static int ovl_remove_opaque(struct dentry *upperdentry)
1363 + const struct cred *old_cred;
1364 + struct cred *override_cred;
1366 + override_cred = prepare_creds();
1367 + if (!override_cred)
1370 + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
1371 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1372 + old_cred = override_creds(override_cred);
1373 + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
1374 + revert_creds(old_cred);
1375 + put_cred(override_cred);
1380 +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
1381 + struct path *lowerpath, struct kstat *stat,
1385 + struct path newpath;
1386 + umode_t mode = stat->mode;
1387 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
1389 + /* Can't properly set mode on creation because of the umask */
1390 + stat->mode &= S_IFMT;
1392 + newpath.mnt = ofs->upper_mnt;
1393 + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
1394 + if (IS_ERR(newpath.dentry)) {
1395 + err = PTR_ERR(newpath.dentry);
1397 + /* Already copied up? */
1398 + if (err == -EEXIST && ovl_path_type(dentry) != OVL_PATH_LOWER)
1404 + if (S_ISREG(stat->mode)) {
1405 + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
1410 + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
1414 + mutex_lock(&newpath.dentry->d_inode->i_mutex);
1415 + if (!S_ISLNK(stat->mode))
1416 + err = ovl_set_mode(newpath.dentry, mode);
1418 + err = ovl_set_timestamps(newpath.dentry, stat);
1419 + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
1423 + ovl_dentry_update(dentry, newpath.dentry);
1426 + * Easiest way to get rid of the lower dentry reference is to
1427 + * drop this dentry. This is neither needed nor possible for
1430 + if (!S_ISDIR(stat->mode))
1436 + if (S_ISDIR(stat->mode))
1437 + vfs_rmdir(upperdir->d_inode, newpath.dentry);
1439 + vfs_unlink(upperdir->d_inode, newpath.dentry);
1441 + dput(newpath.dentry);
1446 +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
1447 + struct path *lowerpath, struct kstat *stat)
1450 + struct kstat pstat;
1451 + struct path parentpath;
1452 + struct dentry *upperdir;
1453 + const struct cred *old_cred;
1454 + struct cred *override_cred;
1455 + char *link = NULL;
1457 + ovl_path_upper(parent, &parentpath);
1458 + upperdir = parentpath.dentry;
1460 + err = vfs_getattr(parentpath.mnt, parentpath.dentry, &pstat);
1464 + if (S_ISLNK(stat->mode)) {
1465 + link = ovl_read_symlink(lowerpath->dentry);
1467 + return PTR_ERR(link);
1471 + override_cred = prepare_creds();
1472 + if (!override_cred)
1473 + goto out_free_link;
1475 + override_cred->fsuid = stat->uid;
1476 + override_cred->fsgid = stat->gid;
1478 + * CAP_SYS_ADMIN for copying up extended attributes
1479 + * CAP_DAC_OVERRIDE for create
1480 + * CAP_FOWNER for chmod, timestamp update
1481 + * CAP_FSETID for chmod
1482 + * CAP_MKNOD for mknod
1484 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1485 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
1486 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
1487 + cap_raise(override_cred->cap_effective, CAP_FSETID);
1488 + cap_raise(override_cred->cap_effective, CAP_MKNOD);
1489 + old_cred = override_creds(override_cred);
1491 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1493 + * Using upper filesystem locking to protect against copy up
1494 + * racing with rename (rename means the copy up was already
1497 + if (dentry->d_parent != parent) {
1498 + WARN_ON((ovl_path_type(dentry) == OVL_PATH_LOWER));
1501 + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
1504 + /* Restore timestamps on parent (best effort) */
1505 + ovl_set_timestamps(upperdir, &pstat);
1509 + mutex_unlock(&upperdir->d_inode->i_mutex);
1511 + revert_creds(old_cred);
1512 + put_cred(override_cred);
1516 + free_page((unsigned long) link);
1521 +static int ovl_copy_up(struct dentry *dentry)
1527 + struct dentry *next;
1528 + struct dentry *parent;
1529 + struct path lowerpath;
1530 + struct kstat stat;
1531 + enum ovl_path_type type = ovl_path_type(dentry);
1533 + if (type != OVL_PATH_LOWER)
1536 + next = dget(dentry);
1537 + /* find the topmost dentry not yet copied up */
1539 + parent = dget_parent(next);
1541 + type = ovl_path_type(parent);
1542 + if (type != OVL_PATH_LOWER)
1549 + ovl_path_lower(next, &lowerpath);
1550 + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat);
1552 + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
1561 +/* Optimize by not copying up the file first and truncating later */
1562 +static int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
1565 + struct kstat stat;
1566 + struct path lowerpath;
1567 + struct dentry *parent = dget_parent(dentry);
1569 + err = ovl_copy_up(parent);
1571 + goto out_dput_parent;
1573 + ovl_path_lower(dentry, &lowerpath);
1574 + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat);
1576 + goto out_dput_parent;
1578 + if (size < stat.size)
1581 + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
1588 +static int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1590 + struct dentry *upperdentry;
1593 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1594 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1596 + err = ovl_copy_up(dentry);
1600 + upperdentry = ovl_dentry_upper(dentry);
1602 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1603 + attr->ia_valid &= ~ATTR_MODE;
1605 + mutex_lock(&upperdentry->d_inode->i_mutex);
1606 + err = notify_change(upperdentry, attr);
1607 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1612 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1613 + struct kstat *stat)
1615 + struct path realpath;
1617 + ovl_path_real(dentry, &realpath);
1618 + return vfs_getattr(realpath.mnt, realpath.dentry, stat);
1621 +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1622 + struct kstat *stat)
1625 + enum ovl_path_type type;
1626 + struct path realpath;
1628 + type = ovl_path_real(dentry, &realpath);
1629 + err = vfs_getattr(realpath.mnt, realpath.dentry, stat);
1633 + stat->dev = dentry->d_sb->s_dev;
1634 + stat->ino = dentry->d_inode->i_ino;
1637 + * It's probably not worth it to count subdirs to get the
1638 + * correct link count. nlink=1 seems to pacify 'find' and
1639 + * other utilities.
1641 + if (type == OVL_PATH_MERGE)
1647 +static int ovl_permission(struct inode *inode, int mask, unsigned int flags)
1649 + struct ovl_entry *oe;
1650 + struct dentry *alias = NULL;
1651 + struct inode *realinode;
1652 + struct dentry *realdentry;
1656 + if (S_ISDIR(inode->i_mode)) {
1657 + oe = inode->i_private;
1658 + } else if (flags & IPERM_FLAG_RCU) {
1662 + * For non-directories find an alias and get the info
1665 + spin_lock(&inode->i_lock);
1666 + if (WARN_ON(list_empty(&inode->i_dentry))) {
1667 + spin_unlock(&inode->i_lock);
1670 + alias = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1672 + spin_unlock(&inode->i_lock);
1673 + oe = alias->d_fsdata;
1676 + realdentry = ovl_upperdentry_dereference(oe);
1678 + if (!realdentry) {
1679 + realdentry = oe->lowerdentry;
1683 + /* Careful in RCU walk mode */
1684 + realinode = ACCESS_ONCE(realdentry->d_inode);
1686 + WARN_ON(!(flags & IPERM_FLAG_RCU));
1690 + if (mask & MAY_WRITE) {
1691 + umode_t mode = realinode->i_mode;
1694 + * Writes will always be redirected to upper layer, so
1695 + * ignore lower layer being read-only.
1698 + if (is_upper && IS_RDONLY(realinode) &&
1699 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1703 + * Nobody gets write access to an immutable file.
1706 + if (IS_IMMUTABLE(realinode))
1710 + if (realinode->i_op->permission)
1711 + err = realinode->i_op->permission(realinode, mask, flags);
1713 + err = generic_permission(realinode, mask, flags,
1714 + realinode->i_op->check_acl);
1720 +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
1724 + struct dentry *newdentry;
1725 + struct dentry *upperdir;
1726 + struct inode *inode;
1727 + struct kstat stat = {
1733 + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
1737 + err = ovl_copy_up(dentry->d_parent);
1741 + upperdir = ovl_dentry_upper(dentry->d_parent);
1742 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1744 + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
1745 + err = PTR_ERR(newdentry);
1746 + if (IS_ERR(newdentry))
1749 + ovl_dentry_version_inc(dentry->d_parent);
1750 + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
1751 + err = ovl_set_opaque(newdentry);
1753 + vfs_rmdir(upperdir->d_inode, newdentry);
1754 + ovl_whiteout(upperdir, dentry);
1758 + ovl_dentry_update(dentry, newdentry);
1759 + d_instantiate(dentry, inode);
1767 + mutex_unlock(&upperdir->d_inode->i_mutex);
1774 +static int ovl_create(struct inode *dir, struct dentry *dentry, int mode,
1775 + struct nameidata *nd)
1777 + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
1780 +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1782 + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
1785 +static int ovl_mknod(struct inode *dir, struct dentry *dentry, int mode,
1788 + return ovl_create_object(dentry, mode, rdev, NULL);
1791 +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
1794 + return ovl_create_object(dentry, S_IFLNK, 0, link);
1797 +struct ovl_link_data {
1798 + struct dentry *realdentry;
1802 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1805 + struct dentry *realdentry;
1806 + struct inode *realinode;
1808 + realdentry = ovl_dentry_real(dentry);
1809 + realinode = realdentry->d_inode;
1811 + if (WARN_ON(!realinode->i_op->follow_link))
1812 + return ERR_PTR(-EPERM);
1814 + ret = realinode->i_op->follow_link(realdentry, nd);
1818 + if (realinode->i_op->put_link) {
1819 + struct ovl_link_data *data;
1821 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1823 + realinode->i_op->put_link(realdentry, nd, ret);
1824 + return ERR_PTR(-ENOMEM);
1826 + data->realdentry = realdentry;
1827 + data->cookie = ret;
1835 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1837 + struct inode *realinode;
1838 + struct ovl_link_data *data = c;
1843 + realinode = data->realdentry->d_inode;
1844 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1848 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1850 + struct path realpath;
1851 + struct inode *realinode;
1853 + ovl_path_real(dentry, &realpath);
1854 + realinode = realpath.dentry->d_inode;
1856 + if (!realinode->i_op->readlink)
1859 + touch_atime(realpath.mnt, realpath.dentry);
1861 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1864 +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
1867 + enum ovl_path_type type;
1868 + struct path realpath;
1869 + struct dentry *upperdir;
1871 + err = ovl_copy_up(dentry->d_parent);
1875 + upperdir = ovl_dentry_upper(dentry->d_parent);
1876 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1877 + type = ovl_path_real(dentry, &realpath);
1878 + if (type != OVL_PATH_LOWER) {
1880 + if (realpath.dentry->d_parent != upperdir)
1884 + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
1886 + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
1890 + ovl_dentry_version_inc(dentry->d_parent);
1893 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1894 + err = ovl_whiteout(upperdir, dentry);
1897 + * Keeping this dentry hashed would mean having to release
1898 + * upperpath/lowerpath, which could only be done if we are the
1899 + * sole user of this dentry. Too tricky... Just unhash for
1904 + mutex_unlock(&upperdir->d_inode->i_mutex);
1909 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1911 + return ovl_do_remove(dentry, false);
1914 +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
1917 + struct path lowerpath;
1918 + struct path upperpath;
1919 + struct ovl_cache_entry *p;
1920 + struct ovl_readdir_data rdd = { .list = list };
1922 + ovl_path_upper(dentry, &upperpath);
1923 + ovl_path_lower(dentry, &lowerpath);
1925 + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
1931 + list_for_each_entry(p, list, l_node) {
1932 + if (p->is_whiteout)
1935 + if (p->name[0] == '.') {
1938 + if (p->len == 2 && p->name[1] == '.')
1948 +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
1950 + struct path upperpath;
1951 + struct dentry *upperdir;
1952 + struct ovl_cache_entry *p;
1953 + const struct cred *old_cred;
1954 + struct cred *override_cred;
1957 + ovl_path_upper(dir, &upperpath);
1958 + upperdir = upperpath.dentry;
1960 + override_cred = prepare_creds();
1961 + if (!override_cred)
1965 + * CAP_DAC_OVERRIDE for lookup and unlink
1967 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
1968 + old_cred = override_creds(override_cred);
1970 + mutex_lock(&upperdir->d_inode->i_mutex);
1971 + list_for_each_entry(p, list, l_node) {
1972 + if (p->is_whiteout) {
1973 + struct dentry *dentry;
1975 + dentry = lookup_one_len(p->name, upperdir, p->len);
1976 + if (IS_ERR(dentry)) {
1977 + ret = PTR_ERR(dentry);
1980 + ret = vfs_unlink(upperdir->d_inode, dentry);
1986 + mutex_unlock(&upperdir->d_inode->i_mutex);
1988 + revert_creds(old_cred);
1989 + put_cred(override_cred);
1994 +static int ovl_check_empty_and_clear(struct dentry *dentry,
1995 + enum ovl_path_type type)
2000 + err = ovl_check_empty_dir(dentry, &list);
2001 + if (!err && type == OVL_PATH_MERGE)
2002 + err = ovl_remove_whiteouts(dentry, &list);
2004 + ovl_cache_free(&list);
2009 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
2012 + enum ovl_path_type type;
2014 + type = ovl_path_type(dentry);
2015 + if (type != OVL_PATH_UPPER) {
2016 + err = ovl_check_empty_and_clear(dentry, type);
2021 + return ovl_do_remove(dentry, true);
2024 +static int ovl_link(struct dentry *old, struct inode *newdir,
2025 + struct dentry *new)
2028 + struct dentry *olddentry;
2029 + struct dentry *newdentry;
2030 + struct dentry *upperdir;
2032 + err = ovl_copy_up(old);
2036 + err = ovl_copy_up(new->d_parent);
2040 + upperdir = ovl_dentry_upper(new->d_parent);
2041 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2042 + newdentry = ovl_lookup_create(upperdir, new);
2043 + err = PTR_ERR(newdentry);
2044 + if (IS_ERR(newdentry))
2047 + olddentry = ovl_dentry_upper(old);
2048 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
2050 + ovl_dentry_version_inc(new->d_parent);
2051 + ovl_dentry_update(new, newdentry);
2053 + ihold(old->d_inode);
2054 + d_instantiate(new, old->d_inode);
2056 + if (ovl_dentry_is_opaque(new))
2057 + ovl_whiteout(upperdir, new);
2061 + mutex_unlock(&upperdir->d_inode->i_mutex);
2067 +static int ovl_rename(struct inode *olddir, struct dentry *old,
2068 + struct inode *newdir, struct dentry *new)
2071 + enum ovl_path_type old_type;
2072 + struct dentry *old_upperdir;
2073 + struct dentry *new_upperdir;
2074 + struct dentry *olddentry;
2075 + struct dentry *newdentry;
2076 + struct dentry *trap;
2079 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
2081 + /* Don't copy up directory trees */
2082 + old_type = ovl_path_type(old);
2083 + if (old_type != OVL_PATH_UPPER && is_dir)
2086 + if (new->d_inode) {
2087 + enum ovl_path_type new_type;
2089 + new_type = ovl_path_type(new);
2091 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
2092 + if (ovl_dentry_lower(old)->d_inode ==
2093 + ovl_dentry_lower(new)->d_inode)
2096 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
2097 + if (ovl_dentry_upper(old)->d_inode ==
2098 + ovl_dentry_upper(new)->d_inode)
2102 + if (new_type != OVL_PATH_UPPER &&
2103 + S_ISDIR(new->d_inode->i_mode)) {
2104 + err = ovl_check_empty_and_clear(new, new_type);
2110 + err = ovl_copy_up(old);
2114 + err = ovl_copy_up(new->d_parent);
2118 + old_upperdir = ovl_dentry_upper(old->d_parent);
2119 + new_upperdir = ovl_dentry_upper(new->d_parent);
2121 + trap = lock_rename(new_upperdir, old_upperdir);
2123 + olddentry = ovl_dentry_upper(old);
2124 + newdentry = ovl_dentry_upper(new);
2128 + newdentry = ovl_lookup_create(new_upperdir, new);
2129 + err = PTR_ERR(newdentry);
2130 + if (IS_ERR(newdentry))
2135 + if (olddentry->d_parent != old_upperdir)
2137 + if (newdentry->d_parent != new_upperdir)
2139 + if (olddentry == trap)
2141 + if (newdentry == trap)
2144 + old_opaque = ovl_dentry_is_opaque(old);
2145 + new_opaque = ovl_dentry_is_opaque(new) ||
2146 + ovl_path_type(new) != OVL_PATH_UPPER;
2148 + if (is_dir && !old_opaque && new_opaque) {
2149 + err = ovl_set_opaque(olddentry);
2154 + err = vfs_rename(old_upperdir->d_inode, olddentry,
2155 + new_upperdir->d_inode, newdentry);
2158 + if (ovl_dentry_is_opaque(new))
2159 + ovl_whiteout(new_upperdir, new);
2160 + if (is_dir && !old_opaque && new_opaque)
2161 + ovl_remove_opaque(olddentry);
2165 + if (old_type != OVL_PATH_UPPER || old_opaque)
2166 + err = ovl_whiteout(old_upperdir, old);
2167 + if (is_dir && old_opaque && !new_opaque)
2168 + ovl_remove_opaque(olddentry);
2170 + if (old_opaque != new_opaque)
2171 + ovl_dentry_set_opaque(old, new_opaque);
2173 + ovl_dentry_version_inc(old->d_parent);
2174 + ovl_dentry_version_inc(new->d_parent);
2179 + unlock_rename(new_upperdir, old_upperdir);
2183 +static bool ovl_is_private_xattr(const char *name)
2185 + return strncmp(name, "trusted.overlay.", 14) == 0;
2188 +static int ovl_setxattr(struct dentry *dentry, const char *name,
2189 + const void *value, size_t size, int flags)
2192 + struct dentry *upperdentry;
2194 + if (ovl_is_private_xattr(name))
2197 + err = ovl_copy_up(dentry);
2201 + upperdentry = ovl_dentry_upper(dentry);
2202 + return vfs_setxattr(upperdentry, name, value, size, flags);
2205 +static ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
2206 + void *value, size_t size)
2208 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
2209 + ovl_is_private_xattr(name))
2212 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
2215 +static ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
2220 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
2221 + if (res <= 0 || size == 0)
2224 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
2227 + /* filter out private xattrs */
2228 + for (off = 0; off < res;) {
2229 + char *s = list + off;
2230 + size_t slen = strlen(s) + 1;
2232 + BUG_ON(off + slen > res);
2234 + if (ovl_is_private_xattr(s)) {
2236 + memmove(s, s + slen, res - off);
2245 +static int ovl_removexattr(struct dentry *dentry, const char *name)
2248 + struct path realpath;
2249 + enum ovl_path_type type;
2251 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
2252 + ovl_is_private_xattr(name))
2255 + type = ovl_path_real(dentry, &realpath);
2256 + if (type == OVL_PATH_LOWER) {
2257 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
2261 + err = ovl_copy_up(dentry);
2265 + ovl_path_upper(dentry, &realpath);
2268 + return vfs_removexattr(realpath.dentry, name);
2271 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
2272 + struct dentry *realdentry)
2274 + if (type != OVL_PATH_LOWER)
2277 + if (special_file(realdentry->d_inode->i_mode))
2280 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
2286 +static struct file *ovl_open(struct dentry *dentry, int flags,
2287 + const struct cred *cred)
2290 + struct path realpath;
2291 + enum ovl_path_type type;
2293 + type = ovl_path_real(dentry, &realpath);
2294 + if (ovl_open_need_copy_up(flags, type, realpath.dentry)) {
2295 + if (flags & O_TRUNC)
2296 + err = ovl_copy_up_truncate(dentry, 0);
2298 + err = ovl_copy_up(dentry);
2300 + return ERR_PTR(err);
2302 + ovl_path_upper(dentry, &realpath);
2305 + return vfs_open(&realpath, flags, cred);
2308 +static const struct inode_operations ovl_dir_inode_operations = {
2309 + .lookup = ovl_lookup,
2310 + .mkdir = ovl_mkdir,
2311 + .symlink = ovl_symlink,
2312 + .unlink = ovl_unlink,
2313 + .rmdir = ovl_rmdir,
2314 + .rename = ovl_rename,
2316 + .setattr = ovl_setattr,
2317 + .create = ovl_create,
2318 + .mknod = ovl_mknod,
2319 + .permission = ovl_permission,
2320 + .getattr = ovl_dir_getattr,
2321 + .setxattr = ovl_setxattr,
2322 + .getxattr = ovl_getxattr,
2323 + .listxattr = ovl_listxattr,
2324 + .removexattr = ovl_removexattr,
2327 +static const struct inode_operations ovl_file_inode_operations = {
2328 + .setattr = ovl_setattr,
2329 + .permission = ovl_permission,
2330 + .getattr = ovl_getattr,
2331 + .setxattr = ovl_setxattr,
2332 + .getxattr = ovl_getxattr,
2333 + .listxattr = ovl_listxattr,
2334 + .removexattr = ovl_removexattr,
2338 +static const struct inode_operations ovl_symlink_inode_operations = {
2339 + .setattr = ovl_setattr,
2340 + .follow_link = ovl_follow_link,
2341 + .put_link = ovl_put_link,
2342 + .readlink = ovl_readlink,
2343 + .getattr = ovl_getattr,
2344 + .setxattr = ovl_setxattr,
2345 + .getxattr = ovl_getxattr,
2346 + .listxattr = ovl_listxattr,
2347 + .removexattr = ovl_removexattr,
2350 +static struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
2351 + struct ovl_entry *oe)
2353 + struct inode *inode;
2355 + inode = new_inode(sb);
2361 + inode->i_ino = get_next_ino();
2362 + inode->i_mode = mode;
2363 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
2367 + inode->i_private = oe;
2368 + inode->i_op = &ovl_dir_inode_operations;
2369 + inode->i_fop = &ovl_dir_operations;
2373 + inode->i_op = &ovl_symlink_inode_operations;
2381 + inode->i_op = &ovl_file_inode_operations;
2385 + WARN(1, "illegal file type: %i\n", mode);
2393 +static void ovl_put_super(struct super_block *sb)
2395 + struct ovl_fs *ufs = sb->s_fs_info;
2397 + if (!(sb->s_flags & MS_RDONLY))
2398 + mnt_drop_write(ufs->upper_mnt);
2400 + mntput(ufs->upper_mnt);
2401 + mntput(ufs->lower_mnt);
2406 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2408 + int flags = *flagsp;
2409 + struct ovl_fs *ufs = sb->s_fs_info;
2411 + /* When remounting rw or ro, we need to adjust the write access to the
2414 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2415 + /* No change to readonly status */
2418 + if (flags & MS_RDONLY) {
2419 + mnt_drop_write(ufs->upper_mnt);
2422 + return mnt_want_write(ufs->upper_mnt);
2427 + * @sb: The overlayfs super block
2428 + * @buf: The struct kstatfs to fill in with stats
2430 + * Get the filesystem statistics. As writes always target the upper layer
2431 + * filesystem pass the statfs to the same filesystem.
2433 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2435 + struct dentry *root_dentry = dentry->d_sb->s_root;
2437 + ovl_path_upper(root_dentry, &path);
2439 + if (!path.dentry->d_sb->s_op->statfs)
2441 + return path.dentry->d_sb->s_op->statfs(path.dentry, buf);
2444 +static const struct super_operations ovl_super_operations = {
2445 + .put_super = ovl_put_super,
2446 + .remount_fs = ovl_remount_fs,
2447 + .statfs = ovl_statfs,
2450 +struct ovl_config {
2461 +static const match_table_t ovl_tokens = {
2462 + {Opt_lowerdir, "lowerdir=%s"},
2463 + {Opt_upperdir, "upperdir=%s"},
2467 +static int ovl_parse_opt(char *opt, struct ovl_config *config)
2471 + config->upperdir = NULL;
2472 + config->lowerdir = NULL;
2474 + while ((p = strsep(&opt, ",")) != NULL) {
2476 + substring_t args[MAX_OPT_ARGS];
2481 + token = match_token(p, ovl_tokens, args);
2483 + case Opt_upperdir:
2484 + kfree(config->upperdir);
2485 + config->upperdir = match_strdup(&args[0]);
2486 + if (!config->upperdir)
2490 + case Opt_lowerdir:
2491 + kfree(config->lowerdir);
2492 + config->lowerdir = match_strdup(&args[0]);
2493 + if (!config->lowerdir)
2504 +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
2506 + struct path lowerpath;
2507 + struct path upperpath;
2508 + struct inode *root_inode;
2509 + struct dentry *root_dentry;
2510 + struct ovl_entry *oe;
2511 + struct ovl_fs *ufs;
2512 + struct ovl_config config;
2515 + err = ovl_parse_opt((char *) data, &config);
2520 + if (!config.upperdir || !config.lowerdir) {
2521 + printk(KERN_ERR "overlayfs: missing upperdir or lowerdir\n");
2522 + goto out_free_config;
2526 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
2528 + goto out_free_config;
2530 + oe = ovl_alloc_entry();
2532 + goto out_free_ufs;
2534 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
2538 + err = kern_path(config.upperdir, LOOKUP_FOLLOW, &upperpath);
2540 + goto out_put_root;
2542 + err = kern_path(config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
2544 + goto out_put_upperpath;
2547 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
2548 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
2549 + goto out_put_lowerpath;
2551 + ufs->upper_mnt = clone_private_mount(&upperpath);
2552 + err = PTR_ERR(ufs->upper_mnt);
2553 + if (IS_ERR(ufs->upper_mnt)) {
2554 + printk(KERN_ERR "overlayfs: failed to clone upperpath\n");
2555 + goto out_put_lowerpath;
2558 + ufs->lower_mnt = clone_private_mount(&lowerpath);
2559 + err = PTR_ERR(ufs->lower_mnt);
2560 + if (IS_ERR(ufs->lower_mnt)) {
2561 + printk(KERN_ERR "overlayfs: failed to clone lowerpath\n");
2562 + goto out_put_upper_mnt;
2565 + if (!(sb->s_flags & MS_RDONLY)) {
2566 + err = mnt_want_write(ufs->upper_mnt);
2568 + goto out_put_lower_mnt;
2572 + root_dentry = d_alloc_root(root_inode);
2574 + goto out_drop_write;
2576 + mntput(upperpath.mnt);
2577 + mntput(lowerpath.mnt);
2579 + oe->__upperdentry = upperpath.dentry;
2580 + oe->lowerdentry = lowerpath.dentry;
2582 + root_dentry->d_fsdata = oe;
2583 + root_dentry->d_op = &ovl_dentry_operations;
2585 + sb->s_op = &ovl_super_operations;
2586 + sb->s_root = root_dentry;
2587 + sb->s_fs_info = ufs;
2592 + if (!(sb->s_flags & MS_RDONLY))
2593 + mnt_drop_write(ufs->upper_mnt);
2595 + mntput(ufs->lower_mnt);
2597 + mntput(ufs->upper_mnt);
2599 + path_put(&lowerpath);
2601 + path_put(&upperpath);
2609 + kfree(config.lowerdir);
2610 + kfree(config.upperdir);
2615 +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
2616 + const char *dev_name, void *raw_data)
2618 + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
2621 +static struct file_system_type ovl_fs_type = {
2622 + .owner = THIS_MODULE,
2623 + .name = "overlayfs",
2624 + .mount = ovl_mount,
2625 + .kill_sb = kill_anon_super,
2628 +static int __init ovl_init(void)
2630 + return register_filesystem(&ovl_fs_type);
2633 +static void __exit ovl_exit(void)
2635 + unregister_filesystem(&ovl_fs_type);
2638 +module_init(ovl_init);
2639 +module_exit(ovl_exit);
2642 @@ -63,6 +63,7 @@ source "fs/quota/Kconfig"
2644 source "fs/autofs4/Kconfig"
2645 source "fs/fuse/Kconfig"
2646 +source "fs/overlayfs/Kconfig"
2649 tristate "Character device in Userspace support"
2652 @@ -105,6 +105,7 @@ obj-$(CONFIG_QNX4FS_FS) += qnx4/
2653 obj-$(CONFIG_AUTOFS4_FS) += autofs4/
2654 obj-$(CONFIG_ADFS_FS) += adfs/
2655 obj-$(CONFIG_FUSE_FS) += fuse/
2656 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
2657 obj-$(CONFIG_UDF_FS) += udf/
2658 obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
2659 obj-$(CONFIG_OMFS_FS) += omfs/
2661 +++ b/fs/overlayfs/Kconfig
2663 +config OVERLAYFS_FS
2664 + tristate "Overlay filesystem support"
2666 + Add support for overlay filesystem.
2668 +++ b/fs/overlayfs/Makefile
2671 +# Makefile for the overlay filesystem.
2674 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
2676 +++ b/Documentation/filesystems/overlayfs.txt
2678 +Written by: Neil Brown <neilb@suse.de>
2683 +This document describes a prototype for a new approach to providing
2684 +overlay-filesystem functionality in Linux (sometimes referred to as
2685 +union-filesystems). An overlay-filesystem tries to present a
2686 +filesystem which is the result over overlaying one filesystem on top
2689 +The result will inevitably fail to look exactly like a normal
2690 +filesystem for various technical reasons. The expectation is that
2691 +many use cases will be able to ignore these differences.
2693 +This approach is 'hybrid' because the objects that appear in the
2694 +filesystem do not all appear to belong to that filesystem. In many
2695 +cases an object accessed in the union will be indistinguishable
2696 +from accessing the corresponding object from the original filesystem.
2697 +This is most obvious from the 'st_dev' field returned by stat(2).
2699 +While directories will report an st_dev from the overlay-filesystem,
2700 +all non-directory objects will report an st_dev from the lower or
2701 +upper filesystem that is providing the object. Similarly st_ino will
2702 +only be unique when combined with st_dev, and both of these can change
2703 +over the lifetime of a non-directory object. Many applications and
2704 +tools ignore these values and will not be affected.
2709 +An overlay filesystem combines two filesystems - an 'upper' filesystem
2710 +and a 'lower' filesystem. When a name exists in both filesystems, the
2711 +object in the 'upper' filesystem is visible while the object in the
2712 +'lower' filesystem is either hidden or, in the case of directories,
2713 +merged with the 'upper' object.
2715 +It would be more correct to refer to an upper and lower 'directory
2716 +tree' rather than 'filesystem' as it is quite possible for both
2717 +directory trees to be in the same filesystem and there is no
2718 +requirement that the root of a filesystem be given for either upper or
2721 +The lower filesystem can be any filesystem supported by Linux and does
2722 +not need to be writable. The lower filesystem can even be another
2723 +overlayfs. The upper filesystem will normally be writable and if it
2724 +is it must support the creation of trusted.* extended attributes, and
2725 +must provide valid d_type in readdir responses, at least for symbolic
2726 +links - so NFS is not suitable.
2728 +A read-only overlay of two read-only filesystems may use any
2734 +Overlaying mainly involved directories. If a given name appears in both
2735 +upper and lower filesystems and refers to a non-directory in either,
2736 +then the lower object is hidden - the name refers only to the upper
2739 +Where both upper and lower objects are directories, a merged directory
2742 +At mount time, the two directories given as mount options are combined
2743 +into a merged directory. Then whenever a lookup is requested in such
2744 +a merged directory, the lookup is performed in each actual directory
2745 +and the combined result is cached in the dentry belonging to the overlay
2746 +filesystem. If both actual lookups find directories, both are stored
2747 +and a merged directory is created, otherwise only one is stored: the
2748 +upper if it exists, else the lower.
2750 +Only the lists of names from directories are merged. Other content
2751 +such as metadata and extended attributes are reported for the upper
2752 +directory only. These attributes of the lower directory are hidden.
2754 +whiteouts and opaque directories
2755 +--------------------------------
2757 +In order to support rm and rmdir without changing the lower
2758 +filesystem, an overlay filesystem needs to record in the upper filesystem
2759 +that files have been removed. This is done using whiteouts and opaque
2760 +directories (non-directories are always opaque).
2762 +The overlay filesystem uses extended attributes with a
2763 +"trusted.overlay." prefix to record these details.
2765 +A whiteout is created as a symbolic link with target
2766 +"(overlay-whiteout)" and with xattr "trusted.overlay.whiteout" set to "y".
2767 +When a whiteout is found in the upper level of a merged directory, any
2768 +matching name in the lower level is ignored, and the whiteout itself
2771 +A directory is made opaque by setting the xattr "trusted.overlay.opaque"
2772 +to "y". Where the upper filesystem contains an opaque directory, any
2773 +directory in the lower filesystem with the same name is ignored.
2778 +When a 'readdir' request is made on a merged directory, the upper and
2779 +lower directories are each read and the name lists merged in the
2780 +obvious way (upper is read first, then lower - entries that already
2781 +exist are not re-added). This merged name list is cached in the
2782 +'struct file' and so remains as long as the file is kept open. If the
2783 +directory is opened and read by two processes at the same time, they
2784 +will each have separate caches. A seekdir to the start of the
2785 +directory (offset 0) followed by a readdir will cause the cache to be
2786 +discarded and rebuilt.
2788 +This means that changes to the merged directory do not appear while a
2789 +directory is being read. This is unlikely to be noticed by many
2792 +seek offsets are assigned sequentially when the directories are read.
2794 + - read part of a directory
2795 + - remember an offset, and close the directory
2796 + - re-open the directory some time later
2797 + - seek to the remembered offset
2799 +there may be little correlation between the old and new locations in
2800 +the list of filenames, particularly if anything has changed in the
2803 +Readdir on directories that are not merged is simply handled by the
2804 +underlying directory (upper or lower).
2810 +Objects that are not directories (files, symlinks, device-special
2811 +files etc.) are presented either from the upper or lower filesystem as
2812 +appropriate. When a file in the lower filesystem is accessed in a way
2813 +the requires write-access, such as opening for write access, changing
2814 +some metadata etc., the file is first copied from the lower filesystem
2815 +to the upper filesystem (copy_up). Note that creating a hard-link
2816 +also requires copy_up, though of course creation of a symlink does
2819 +The copy_up process first makes sure that the containing directory
2820 +exists in the upper filesystem - creating it and any parents as
2821 +necessary. It then creates the object with the same metadata (owner,
2822 +mode, mtime, symlink-target etc.) and then if the object is a file, the
2823 +data is copied from the lower to the upper filesystem. Finally any
2824 +extended attributes are copied up.
2826 +Once the copy_up is complete, the overlay filesystem simply
2827 +provides direct access to the newly created file in the upper
2828 +filesystem - future operations on the file are barely noticed by the
2829 +overlay filesystem (though an operation on the name of the file such as
2830 +rename or unlink will of course be noticed and handled).
2832 +Changes to underlying filesystems
2833 +---------------------------------
2835 +Offline changes, when the overlay is not mounted, are allowed to either
2836 +the upper or the lower trees.
2838 +Changes to the underlying filesystems while part of a mounted overlay
2839 +filesystem are not allowed. This is not yet enforced, but will be in