1 diff --git a/Documentation/filesystems/Locking b/Documentation/filesystems/Locking
2 index 0706d32..4331290 100644
3 --- a/Documentation/filesystems/Locking
4 +++ b/Documentation/filesystems/Locking
5 @@ -66,6 +66,7 @@ prototypes:
6 int (*atomic_open)(struct inode *, struct dentry *,
7 struct file *, unsigned open_flag,
8 umode_t create_mode, int *opened);
9 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
13 @@ -93,6 +94,7 @@ removexattr: yes
19 Additionally, ->rmdir(), ->unlink() and ->rename() have ->i_mutex on
21 diff --git a/Documentation/filesystems/overlayfs.txt b/Documentation/filesystems/overlayfs.txt
23 index 0000000..00dbab0
25 +++ b/Documentation/filesystems/overlayfs.txt
27 +Written by: Neil Brown <neilb@suse.de>
32 +This document describes a prototype for a new approach to providing
33 +overlay-filesystem functionality in Linux (sometimes referred to as
34 +union-filesystems). An overlay-filesystem tries to present a
35 +filesystem which is the result over overlaying one filesystem on top
38 +The result will inevitably fail to look exactly like a normal
39 +filesystem for various technical reasons. The expectation is that
40 +many use cases will be able to ignore these differences.
42 +This approach is 'hybrid' because the objects that appear in the
43 +filesystem do not all appear to belong to that filesystem. In many
44 +cases an object accessed in the union will be indistinguishable
45 +from accessing the corresponding object from the original filesystem.
46 +This is most obvious from the 'st_dev' field returned by stat(2).
48 +While directories will report an st_dev from the overlay-filesystem,
49 +all non-directory objects will report an st_dev from the lower or
50 +upper filesystem that is providing the object. Similarly st_ino will
51 +only be unique when combined with st_dev, and both of these can change
52 +over the lifetime of a non-directory object. Many applications and
53 +tools ignore these values and will not be affected.
58 +An overlay filesystem combines two filesystems - an 'upper' filesystem
59 +and a 'lower' filesystem. When a name exists in both filesystems, the
60 +object in the 'upper' filesystem is visible while the object in the
61 +'lower' filesystem is either hidden or, in the case of directories,
62 +merged with the 'upper' object.
64 +It would be more correct to refer to an upper and lower 'directory
65 +tree' rather than 'filesystem' as it is quite possible for both
66 +directory trees to be in the same filesystem and there is no
67 +requirement that the root of a filesystem be given for either upper or
70 +The lower filesystem can be any filesystem supported by Linux and does
71 +not need to be writable. The lower filesystem can even be another
72 +overlayfs. The upper filesystem will normally be writable and if it
73 +is it must support the creation of trusted.* extended attributes, and
74 +must provide valid d_type in readdir responses, at least for symbolic
75 +links - so NFS is not suitable.
77 +A read-only overlay of two read-only filesystems may use any
83 +Overlaying mainly involves directories. If a given name appears in both
84 +upper and lower filesystems and refers to a non-directory in either,
85 +then the lower object is hidden - the name refers only to the upper
88 +Where both upper and lower objects are directories, a merged directory
91 +At mount time, the two directories given as mount options are combined
92 +into a merged directory:
94 + mount -t overlayfs overlayfs -olowerdir=/lower,upperdir=/upper /overlay
96 +Then whenever a lookup is requested in such a merged directory, the
97 +lookup is performed in each actual directory and the combined result
98 +is cached in the dentry belonging to the overlay filesystem. If both
99 +actual lookups find directories, both are stored and a merged
100 +directory is created, otherwise only one is stored: the upper if it
101 +exists, else the lower.
103 +Only the lists of names from directories are merged. Other content
104 +such as metadata and extended attributes are reported for the upper
105 +directory only. These attributes of the lower directory are hidden.
107 +whiteouts and opaque directories
108 +--------------------------------
110 +In order to support rm and rmdir without changing the lower
111 +filesystem, an overlay filesystem needs to record in the upper filesystem
112 +that files have been removed. This is done using whiteouts and opaque
113 +directories (non-directories are always opaque).
115 +The overlay filesystem uses extended attributes with a
116 +"trusted.overlay." prefix to record these details.
118 +A whiteout is created as a symbolic link with target
119 +"(overlay-whiteout)" and with xattr "trusted.overlay.whiteout" set to "y".
120 +When a whiteout is found in the upper level of a merged directory, any
121 +matching name in the lower level is ignored, and the whiteout itself
124 +A directory is made opaque by setting the xattr "trusted.overlay.opaque"
125 +to "y". Where the upper filesystem contains an opaque directory, any
126 +directory in the lower filesystem with the same name is ignored.
131 +When a 'readdir' request is made on a merged directory, the upper and
132 +lower directories are each read and the name lists merged in the
133 +obvious way (upper is read first, then lower - entries that already
134 +exist are not re-added). This merged name list is cached in the
135 +'struct file' and so remains as long as the file is kept open. If the
136 +directory is opened and read by two processes at the same time, they
137 +will each have separate caches. A seekdir to the start of the
138 +directory (offset 0) followed by a readdir will cause the cache to be
139 +discarded and rebuilt.
141 +This means that changes to the merged directory do not appear while a
142 +directory is being read. This is unlikely to be noticed by many
145 +seek offsets are assigned sequentially when the directories are read.
147 + - read part of a directory
148 + - remember an offset, and close the directory
149 + - re-open the directory some time later
150 + - seek to the remembered offset
152 +there may be little correlation between the old and new locations in
153 +the list of filenames, particularly if anything has changed in the
156 +Readdir on directories that are not merged is simply handled by the
157 +underlying directory (upper or lower).
163 +Objects that are not directories (files, symlinks, device-special
164 +files etc.) are presented either from the upper or lower filesystem as
165 +appropriate. When a file in the lower filesystem is accessed in a way
166 +the requires write-access, such as opening for write access, changing
167 +some metadata etc., the file is first copied from the lower filesystem
168 +to the upper filesystem (copy_up). Note that creating a hard-link
169 +also requires copy_up, though of course creation of a symlink does
172 +The copy_up may turn out to be unnecessary, for example if the file is
173 +opened for read-write but the data is not modified.
175 +The copy_up process first makes sure that the containing directory
176 +exists in the upper filesystem - creating it and any parents as
177 +necessary. It then creates the object with the same metadata (owner,
178 +mode, mtime, symlink-target etc.) and then if the object is a file, the
179 +data is copied from the lower to the upper filesystem. Finally any
180 +extended attributes are copied up.
182 +Once the copy_up is complete, the overlay filesystem simply
183 +provides direct access to the newly created file in the upper
184 +filesystem - future operations on the file are barely noticed by the
185 +overlay filesystem (though an operation on the name of the file such as
186 +rename or unlink will of course be noticed and handled).
189 +Non-standard behavior
190 +---------------------
192 +The copy_up operation essentially creates a new, identical file and
193 +moves it over to the old name. The new file may be on a different
194 +filesystem, so both st_dev and st_ino of the file may change.
196 +Any open files referring to this inode will access the old data and
197 +metadata. Similarly any file locks obtained before copy_up will not
198 +apply to the copied up file.
200 +On a file opened with O_RDONLY fchmod(2), fchown(2), futimesat(2) and
201 +fsetxattr(2) will fail with EROFS.
203 +If a file with multiple hard links is copied up, then this will
204 +"break" the link. Changes will not be propagated to other names
205 +referring to the same inode.
207 +Symlinks in /proc/PID/ and /proc/PID/fd which point to a non-directory
208 +object in overlayfs will not contain valid absolute paths, only
209 +relative paths leading up to the filesystem's root. This will be
210 +fixed in the future.
212 +Some operations are not atomic, for example a crash during copy_up or
213 +rename will leave the filesystem in an inconsistent state. This will
214 +be addressed in the future.
216 +Changes to underlying filesystems
217 +---------------------------------
219 +Offline changes, when the overlay is not mounted, are allowed to either
220 +the upper or the lower trees.
222 +Changes to the underlying filesystems while part of a mounted overlay
223 +filesystem are not allowed. If the underlying filesystem is changed,
224 +the behavior of the overlay is undefined, though it will not result in
225 +a crash or deadlock.
226 diff --git a/Documentation/filesystems/vfs.txt b/Documentation/filesystems/vfs.txt
227 index bc4b06b..f64a4d1 100644
228 --- a/Documentation/filesystems/vfs.txt
229 +++ b/Documentation/filesystems/vfs.txt
230 @@ -362,6 +362,7 @@ struct inode_operations {
231 int (*atomic_open)(struct inode *, struct dentry *,
232 struct file *, unsigned open_flag,
233 umode_t create_mode, int *opened);
234 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
237 Again, all methods are called without any locks being held, unless
238 @@ -681,6 +682,12 @@ struct address_space_operations {
239 but instead uses bmap to find out where the blocks in the file
240 are and uses those addresses directly.
242 + dentry_open: this is an alternative to f_op->open(), the difference is that
243 + this method may open a file not necessarily originating from the same
244 + filesystem as the one i_op->open() was called on. It may be
245 + useful for stacking filesystems which want to allow native I/O directly
246 + on underlying files.
249 invalidatepage: If a page has PagePrivate set, then invalidatepage
250 will be called when part or all of the page is to be removed
251 diff --git a/MAINTAINERS b/MAINTAINERS
252 index 9561658..9ea89b8 100644
255 @@ -5872,6 +5872,13 @@ F: drivers/scsi/osd/
256 F: include/scsi/osd_*
259 +OVERLAYFS FILESYSTEM
260 +M: Miklos Szeredi <miklos@szeredi.hu>
261 +L: linux-fsdevel@vger.kernel.org
264 +F: Documentation/filesystems/overlayfs.txt
267 M: Christian Lamparter <chunkeey@googlemail.com>
268 L: linux-wireless@vger.kernel.org
269 diff --git a/fs/Kconfig b/fs/Kconfig
270 index 780725a..9e2ccd5 100644
273 @@ -67,6 +67,7 @@ source "fs/quota/Kconfig"
275 source "fs/autofs4/Kconfig"
276 source "fs/fuse/Kconfig"
277 +source "fs/overlayfs/Kconfig"
281 diff --git a/fs/Makefile b/fs/Makefile
282 index 9d53192..479a720 100644
285 @@ -107,6 +107,7 @@ obj-$(CONFIG_QNX6FS_FS) += qnx6/
286 obj-$(CONFIG_AUTOFS4_FS) += autofs4/
287 obj-$(CONFIG_ADFS_FS) += adfs/
288 obj-$(CONFIG_FUSE_FS) += fuse/
289 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
290 obj-$(CONFIG_UDF_FS) += udf/
291 obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
292 obj-$(CONFIG_OMFS_FS) += omfs/
293 diff --git a/fs/ecryptfs/main.c b/fs/ecryptfs/main.c
294 index e924cf4..8f7551e 100644
295 --- a/fs/ecryptfs/main.c
296 +++ b/fs/ecryptfs/main.c
297 @@ -567,6 +567,13 @@ static struct dentry *ecryptfs_mount(struct file_system_type *fs_type, int flags
298 s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
299 s->s_blocksize = path.dentry->d_sb->s_blocksize;
300 s->s_magic = ECRYPTFS_SUPER_MAGIC;
301 + s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
304 + if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
305 + printk(KERN_ERR "eCryptfs: maximum fs stacking depth exceeded\n");
309 inode = ecryptfs_get_inode(path.dentry->d_inode, s);
311 diff --git a/fs/internal.h b/fs/internal.h
312 index 507141f..89481ac 100644
315 @@ -42,11 +42,6 @@ static inline int __sync_blockdev(struct block_device *bdev, int wait)
316 extern void __init chrdev_init(void);
321 -extern int __inode_permission(struct inode *, int);
326 extern int copy_mount_options(const void __user *, unsigned long *);
327 diff --git a/fs/namei.c b/fs/namei.c
328 index 57ae9c8..e1cffbd 100644
331 @@ -402,6 +402,7 @@ int __inode_permission(struct inode *inode, int mask)
333 return security_inode_permission(inode, mask);
335 +EXPORT_SYMBOL(__inode_permission);
338 * sb_permission - Check superblock-level permissions
339 @@ -2867,9 +2868,12 @@ finish_open_created:
340 error = may_open(&nd->path, acc_mode, open_flag);
343 - file->f_path.mnt = nd->path.mnt;
344 - error = finish_open(file, nd->path.dentry, NULL, opened);
347 + BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
348 + error = vfs_open(&nd->path, file, current_cred());
350 + *opened |= FILE_OPENED;
352 if (error == -EOPENSTALE)
355 diff --git a/fs/namespace.c b/fs/namespace.c
356 index 50ca17d..9791b4e 100644
359 @@ -1399,6 +1399,24 @@ void drop_collected_mounts(struct vfsmount *mnt)
360 release_mounts(&umount_list);
363 +struct vfsmount *clone_private_mount(struct path *path)
365 + struct mount *old_mnt = real_mount(path->mnt);
366 + struct mount *new_mnt;
368 + if (IS_MNT_UNBINDABLE(old_mnt))
369 + return ERR_PTR(-EINVAL);
371 + down_read(&namespace_sem);
372 + new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
373 + up_read(&namespace_sem);
375 + return ERR_PTR(-ENOMEM);
377 + return &new_mnt->mnt;
379 +EXPORT_SYMBOL_GPL(clone_private_mount);
381 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
382 struct vfsmount *root)
384 diff --git a/fs/open.c b/fs/open.c
385 index 6835446..b9d9f9e 100644
388 @@ -828,8 +828,7 @@ struct file *dentry_open(const struct path *path, int flags,
389 f = get_empty_filp();
393 - error = do_dentry_open(f, NULL, cred);
394 + error = vfs_open(path, f, cred);
396 /* from now on we need fput() to dispose of f */
397 error = open_check_o_direct(f);
398 @@ -846,6 +845,26 @@ struct file *dentry_open(const struct path *path, int flags,
400 EXPORT_SYMBOL(dentry_open);
403 + * vfs_open - open the file at the given path
404 + * @path: path to open
405 + * @filp: newly allocated file with f_flag initialized
406 + * @cred: credentials to use
408 +int vfs_open(const struct path *path, struct file *filp,
409 + const struct cred *cred)
411 + struct inode *inode = path->dentry->d_inode;
413 + if (inode->i_op->dentry_open)
414 + return inode->i_op->dentry_open(path->dentry, filp, cred);
416 + filp->f_path = *path;
417 + return do_dentry_open(filp, NULL, cred);
420 +EXPORT_SYMBOL(vfs_open);
422 static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
424 int lookup_flags = 0;
425 diff --git a/fs/overlayfs/Kconfig b/fs/overlayfs/Kconfig
427 index 0000000..c4517da
429 +++ b/fs/overlayfs/Kconfig
432 + tristate "Overlay filesystem support"
434 + Add support for overlay filesystem.
435 diff --git a/fs/overlayfs/Makefile b/fs/overlayfs/Makefile
437 index 0000000..8f91889
439 +++ b/fs/overlayfs/Makefile
442 +# Makefile for the overlay filesystem.
445 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
447 +overlayfs-objs := super.o inode.o dir.o readdir.o copy_up.o
448 diff --git a/fs/overlayfs/copy_up.c b/fs/overlayfs/copy_up.c
450 index 0000000..eef85e0
452 +++ b/fs/overlayfs/copy_up.c
456 + * Copyright (C) 2011 Novell Inc.
458 + * This program is free software; you can redistribute it and/or modify it
459 + * under the terms of the GNU General Public License version 2 as published by
460 + * the Free Software Foundation.
463 +#include <linux/fs.h>
464 +#include <linux/slab.h>
465 +#include <linux/file.h>
466 +#include <linux/splice.h>
467 +#include <linux/xattr.h>
468 +#include <linux/security.h>
469 +#include <linux/uaccess.h>
470 +#include <linux/sched.h>
471 +#include "overlayfs.h"
473 +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
475 +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
477 + ssize_t list_size, size;
478 + char *buf, *name, *value;
481 + if (!old->d_inode->i_op->getxattr ||
482 + !new->d_inode->i_op->getxattr)
485 + list_size = vfs_listxattr(old, NULL, 0);
486 + if (list_size <= 0) {
487 + if (list_size == -EOPNOTSUPP)
492 + buf = kzalloc(list_size, GFP_KERNEL);
497 + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
501 + list_size = vfs_listxattr(old, buf, list_size);
502 + if (list_size <= 0) {
504 + goto out_free_value;
507 + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
508 + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
511 + goto out_free_value;
513 + error = vfs_setxattr(new, name, value, size, 0);
515 + goto out_free_value;
525 +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
527 + struct file *old_file;
528 + struct file *new_file;
534 + old_file = ovl_path_open(old, O_RDONLY);
535 + if (IS_ERR(old_file))
536 + return PTR_ERR(old_file);
538 + new_file = ovl_path_open(new, O_WRONLY);
539 + if (IS_ERR(new_file)) {
540 + error = PTR_ERR(new_file);
544 + /* FIXME: copy up sparse files efficiently */
546 + loff_t offset = new_file->f_pos;
547 + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
550 + if (len < this_len)
553 + if (signal_pending_state(TASK_KILLABLE, current)) {
558 + bytes = do_splice_direct(old_file, &offset, new_file, this_len,
574 +static char *ovl_read_symlink(struct dentry *realdentry)
578 + struct inode *inode = realdentry->d_inode;
579 + mm_segment_t old_fs;
582 + if (!inode->i_op->readlink)
586 + buf = (char *) __get_free_page(GFP_KERNEL);
592 + /* The cast to a user pointer is valid due to the set_fs() */
593 + res = inode->i_op->readlink(realdentry,
594 + (char __user *)buf, PAGE_SIZE - 1);
597 + free_page((unsigned long) buf);
605 + return ERR_PTR(res);
608 +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
610 + struct iattr attr = {
612 + ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
613 + .ia_atime = stat->atime,
614 + .ia_mtime = stat->mtime,
617 + return notify_change(upperdentry, &attr);
620 +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
622 + struct iattr attr = {
623 + .ia_valid = ATTR_MODE,
627 + return notify_change(upperdentry, &attr);
630 +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
631 + struct path *lowerpath, struct kstat *stat,
635 + struct path newpath;
636 + umode_t mode = stat->mode;
638 + /* Can't properly set mode on creation because of the umask */
639 + stat->mode &= S_IFMT;
641 + ovl_path_upper(dentry, &newpath);
642 + WARN_ON(newpath.dentry);
643 + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
644 + if (IS_ERR(newpath.dentry))
645 + return PTR_ERR(newpath.dentry);
647 + if (S_ISREG(stat->mode)) {
648 + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
653 + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
657 + mutex_lock(&newpath.dentry->d_inode->i_mutex);
658 + if (!S_ISLNK(stat->mode))
659 + err = ovl_set_mode(newpath.dentry, mode);
661 + err = ovl_set_timestamps(newpath.dentry, stat);
662 + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
666 + ovl_dentry_update(dentry, newpath.dentry);
669 + * Easiest way to get rid of the lower dentry reference is to
670 + * drop this dentry. This is neither needed nor possible for
673 + if (!S_ISDIR(stat->mode))
679 + if (S_ISDIR(stat->mode))
680 + vfs_rmdir(upperdir->d_inode, newpath.dentry);
682 + vfs_unlink(upperdir->d_inode, newpath.dentry);
684 + dput(newpath.dentry);
690 + * Copy up a single dentry
692 + * Directory renames only allowed on "pure upper" (already created on
693 + * upper filesystem, never copied up). Directories which are on lower or
694 + * are merged may not be renamed. For these -EXDEV is returned and
695 + * userspace has to deal with it. This means, when copying up a
696 + * directory we can rely on it and ancestors being stable.
698 + * Non-directory renames start with copy up of source if necessary. The
699 + * actual rename will only proceed once the copy up was successful. Copy
700 + * up uses upper parent i_mutex for exclusion. Since rename can change
701 + * d_parent it is possible that the copy up will lock the old parent. At
702 + * that point the file will have already been copied up anyway.
704 +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
705 + struct path *lowerpath, struct kstat *stat)
708 + struct kstat pstat;
709 + struct path parentpath;
710 + struct dentry *upperdir;
711 + const struct cred *old_cred;
712 + struct cred *override_cred;
715 + ovl_path_upper(parent, &parentpath);
716 + upperdir = parentpath.dentry;
718 + err = vfs_getattr(&parentpath, &pstat);
722 + if (S_ISLNK(stat->mode)) {
723 + link = ovl_read_symlink(lowerpath->dentry);
725 + return PTR_ERR(link);
729 + override_cred = prepare_creds();
730 + if (!override_cred)
731 + goto out_free_link;
733 + override_cred->fsuid = stat->uid;
734 + override_cred->fsgid = stat->gid;
736 + * CAP_SYS_ADMIN for copying up extended attributes
737 + * CAP_DAC_OVERRIDE for create
738 + * CAP_FOWNER for chmod, timestamp update
739 + * CAP_FSETID for chmod
740 + * CAP_MKNOD for mknod
742 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
743 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
744 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
745 + cap_raise(override_cred->cap_effective, CAP_FSETID);
746 + cap_raise(override_cred->cap_effective, CAP_MKNOD);
747 + old_cred = override_creds(override_cred);
749 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
750 + if (ovl_path_type(dentry) != OVL_PATH_LOWER) {
753 + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
756 + /* Restore timestamps on parent (best effort) */
757 + ovl_set_timestamps(upperdir, &pstat);
761 + mutex_unlock(&upperdir->d_inode->i_mutex);
763 + revert_creds(old_cred);
764 + put_cred(override_cred);
768 + free_page((unsigned long) link);
773 +int ovl_copy_up(struct dentry *dentry)
779 + struct dentry *next;
780 + struct dentry *parent;
781 + struct path lowerpath;
783 + enum ovl_path_type type = ovl_path_type(dentry);
785 + if (type != OVL_PATH_LOWER)
788 + next = dget(dentry);
789 + /* find the topmost dentry not yet copied up */
791 + parent = dget_parent(next);
793 + type = ovl_path_type(parent);
794 + if (type != OVL_PATH_LOWER)
801 + ovl_path_lower(next, &lowerpath);
802 + err = vfs_getattr(&lowerpath, &stat);
804 + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
813 +/* Optimize by not copying up the file first and truncating later */
814 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
818 + struct path lowerpath;
819 + struct dentry *parent = dget_parent(dentry);
821 + err = ovl_copy_up(parent);
823 + goto out_dput_parent;
825 + ovl_path_lower(dentry, &lowerpath);
826 + err = vfs_getattr(&lowerpath, &stat);
828 + goto out_dput_parent;
830 + if (size < stat.size)
833 + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
839 diff --git a/fs/overlayfs/dir.c b/fs/overlayfs/dir.c
841 index 0000000..b530e56
843 +++ b/fs/overlayfs/dir.c
847 + * Copyright (C) 2011 Novell Inc.
849 + * This program is free software; you can redistribute it and/or modify it
850 + * under the terms of the GNU General Public License version 2 as published by
851 + * the Free Software Foundation.
854 +#include <linux/fs.h>
855 +#include <linux/namei.h>
856 +#include <linux/xattr.h>
857 +#include <linux/security.h>
858 +#include <linux/cred.h>
859 +#include "overlayfs.h"
861 +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
863 +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
866 + struct dentry *newdentry;
867 + const struct cred *old_cred;
868 + struct cred *override_cred;
870 + /* FIXME: recheck lower dentry to see if whiteout is really needed */
873 + override_cred = prepare_creds();
874 + if (!override_cred)
878 + * CAP_SYS_ADMIN for setxattr
879 + * CAP_DAC_OVERRIDE for symlink creation
880 + * CAP_FOWNER for unlink in sticky directory
882 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
883 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
884 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
885 + override_cred->fsuid = GLOBAL_ROOT_UID;
886 + override_cred->fsgid = GLOBAL_ROOT_GID;
887 + old_cred = override_creds(override_cred);
889 + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
890 + dentry->d_name.len);
891 + err = PTR_ERR(newdentry);
892 + if (IS_ERR(newdentry))
895 + /* Just been removed within the same locked region */
896 + WARN_ON(newdentry->d_inode);
898 + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
902 + ovl_dentry_version_inc(dentry->d_parent);
904 + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
906 + vfs_unlink(upperdir->d_inode, newdentry);
911 + revert_creds(old_cred);
912 + put_cred(override_cred);
916 + * There's no way to recover from failure to whiteout.
917 + * What should we do? Log a big fat error and... ?
919 + printk(KERN_ERR "overlayfs: ERROR - failed to whiteout '%s'\n",
920 + dentry->d_name.name);
926 +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
927 + struct dentry *template)
930 + struct dentry *newdentry;
931 + struct qstr *name = &template->d_name;
933 + newdentry = lookup_one_len(name->name, upperdir, name->len);
934 + if (IS_ERR(newdentry))
937 + if (newdentry->d_inode) {
938 + const struct cred *old_cred;
939 + struct cred *override_cred;
941 + /* No need to check whiteout if lower parent is non-existent */
943 + if (!ovl_dentry_lower(template->d_parent))
946 + if (!S_ISLNK(newdentry->d_inode->i_mode))
950 + override_cred = prepare_creds();
951 + if (!override_cred)
955 + * CAP_SYS_ADMIN for getxattr
956 + * CAP_FOWNER for unlink in sticky directory
958 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
959 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
960 + old_cred = override_creds(override_cred);
963 + if (ovl_is_whiteout(newdentry))
964 + err = vfs_unlink(upperdir->d_inode, newdentry);
966 + revert_creds(old_cred);
967 + put_cred(override_cred);
972 + newdentry = lookup_one_len(name->name, upperdir, name->len);
973 + if (IS_ERR(newdentry)) {
974 + ovl_whiteout(upperdir, template);
979 + * Whiteout just been successfully removed, parent
980 + * i_mutex is still held, there's no way the lookup
981 + * could return positive.
983 + WARN_ON(newdentry->d_inode);
990 + return ERR_PTR(err);
993 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
994 + struct kstat *stat, const char *link)
997 + struct dentry *newdentry;
998 + struct inode *dir = upperdir->d_inode;
1000 + newdentry = ovl_lookup_create(upperdir, dentry);
1001 + if (IS_ERR(newdentry))
1004 + switch (stat->mode & S_IFMT) {
1006 + err = vfs_create(dir, newdentry, stat->mode, NULL);
1010 + err = vfs_mkdir(dir, newdentry, stat->mode);
1017 + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
1021 + err = vfs_symlink(dir, newdentry, link);
1028 + if (ovl_dentry_is_opaque(dentry))
1029 + ovl_whiteout(upperdir, dentry);
1031 + newdentry = ERR_PTR(err);
1032 + } else if (WARN_ON(!newdentry->d_inode)) {
1034 + * Not quite sure if non-instantiated dentry is legal or not.
1035 + * VFS doesn't seem to care so check and warn here.
1038 + newdentry = ERR_PTR(-ENOENT);
1046 +static int ovl_set_opaque(struct dentry *upperdentry)
1049 + const struct cred *old_cred;
1050 + struct cred *override_cred;
1052 + override_cred = prepare_creds();
1053 + if (!override_cred)
1056 + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
1057 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1058 + old_cred = override_creds(override_cred);
1059 + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
1060 + revert_creds(old_cred);
1061 + put_cred(override_cred);
1066 +static int ovl_remove_opaque(struct dentry *upperdentry)
1069 + const struct cred *old_cred;
1070 + struct cred *override_cred;
1072 + override_cred = prepare_creds();
1073 + if (!override_cred)
1076 + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
1077 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1078 + old_cred = override_creds(override_cred);
1079 + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
1080 + revert_creds(old_cred);
1081 + put_cred(override_cred);
1086 +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1087 + struct kstat *stat)
1090 + enum ovl_path_type type;
1091 + struct path realpath;
1093 + type = ovl_path_real(dentry, &realpath);
1094 + err = vfs_getattr(&realpath, stat);
1098 + stat->dev = dentry->d_sb->s_dev;
1099 + stat->ino = dentry->d_inode->i_ino;
1102 + * It's probably not worth it to count subdirs to get the
1103 + * correct link count. nlink=1 seems to pacify 'find' and
1104 + * other utilities.
1106 + if (type == OVL_PATH_MERGE)
1112 +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
1116 + struct dentry *newdentry;
1117 + struct dentry *upperdir;
1118 + struct inode *inode;
1119 + struct kstat stat = {
1125 + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
1129 + err = ovl_copy_up(dentry->d_parent);
1133 + upperdir = ovl_dentry_upper(dentry->d_parent);
1134 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1136 + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
1137 + err = PTR_ERR(newdentry);
1138 + if (IS_ERR(newdentry))
1141 + ovl_dentry_version_inc(dentry->d_parent);
1142 + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
1143 + err = ovl_set_opaque(newdentry);
1145 + vfs_rmdir(upperdir->d_inode, newdentry);
1146 + ovl_whiteout(upperdir, dentry);
1150 + ovl_dentry_update(dentry, newdentry);
1151 + ovl_copyattr(newdentry->d_inode, inode);
1152 + d_instantiate(dentry, inode);
1160 + mutex_unlock(&upperdir->d_inode->i_mutex);
1167 +static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1170 + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
1173 +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1175 + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
1178 +static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1181 + return ovl_create_object(dentry, mode, rdev, NULL);
1184 +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
1187 + return ovl_create_object(dentry, S_IFLNK, 0, link);
1190 +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
1193 + enum ovl_path_type type;
1194 + struct path realpath;
1195 + struct dentry *upperdir;
1197 + err = ovl_copy_up(dentry->d_parent);
1201 + upperdir = ovl_dentry_upper(dentry->d_parent);
1202 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1203 + type = ovl_path_real(dentry, &realpath);
1204 + if (type != OVL_PATH_LOWER) {
1206 + if (realpath.dentry->d_parent != upperdir)
1209 + /* FIXME: create whiteout up front and rename to target */
1212 + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
1214 + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
1218 + ovl_dentry_version_inc(dentry->d_parent);
1221 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1222 + err = ovl_whiteout(upperdir, dentry);
1225 + * Keeping this dentry hashed would mean having to release
1226 + * upperpath/lowerpath, which could only be done if we are the
1227 + * sole user of this dentry. Too tricky... Just unhash for
1232 + mutex_unlock(&upperdir->d_inode->i_mutex);
1237 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1239 + return ovl_do_remove(dentry, false);
1243 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
1246 + enum ovl_path_type type;
1248 + type = ovl_path_type(dentry);
1249 + if (type != OVL_PATH_UPPER) {
1250 + err = ovl_check_empty_and_clear(dentry, type);
1255 + return ovl_do_remove(dentry, true);
1258 +static int ovl_link(struct dentry *old, struct inode *newdir,
1259 + struct dentry *new)
1262 + struct dentry *olddentry;
1263 + struct dentry *newdentry;
1264 + struct dentry *upperdir;
1265 + struct inode *newinode;
1267 + err = ovl_copy_up(old);
1271 + err = ovl_copy_up(new->d_parent);
1275 + upperdir = ovl_dentry_upper(new->d_parent);
1276 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1277 + newdentry = ovl_lookup_create(upperdir, new);
1278 + err = PTR_ERR(newdentry);
1279 + if (IS_ERR(newdentry))
1282 + olddentry = ovl_dentry_upper(old);
1283 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
1285 + if (WARN_ON(!newdentry->d_inode)) {
1290 + newinode = ovl_new_inode(old->d_sb, newdentry->d_inode->i_mode,
1294 + ovl_copyattr(upperdir->d_inode, newinode);
1296 + ovl_dentry_version_inc(new->d_parent);
1297 + ovl_dentry_update(new, newdentry);
1299 + d_instantiate(new, newinode);
1302 + if (ovl_dentry_is_opaque(new))
1303 + ovl_whiteout(upperdir, new);
1307 + mutex_unlock(&upperdir->d_inode->i_mutex);
1313 +static int ovl_rename(struct inode *olddir, struct dentry *old,
1314 + struct inode *newdir, struct dentry *new)
1317 + enum ovl_path_type old_type;
1318 + enum ovl_path_type new_type;
1319 + struct dentry *old_upperdir;
1320 + struct dentry *new_upperdir;
1321 + struct dentry *olddentry;
1322 + struct dentry *newdentry;
1323 + struct dentry *trap;
1326 + bool new_create = false;
1327 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
1329 + /* Don't copy up directory trees */
1330 + old_type = ovl_path_type(old);
1331 + if (old_type != OVL_PATH_UPPER && is_dir)
1334 + if (new->d_inode) {
1335 + new_type = ovl_path_type(new);
1337 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
1338 + if (ovl_dentry_lower(old)->d_inode ==
1339 + ovl_dentry_lower(new)->d_inode)
1342 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
1343 + if (ovl_dentry_upper(old)->d_inode ==
1344 + ovl_dentry_upper(new)->d_inode)
1348 + if (new_type != OVL_PATH_UPPER &&
1349 + S_ISDIR(new->d_inode->i_mode)) {
1350 + err = ovl_check_empty_and_clear(new, new_type);
1355 + new_type = OVL_PATH_UPPER;
1358 + err = ovl_copy_up(old);
1362 + err = ovl_copy_up(new->d_parent);
1366 + old_upperdir = ovl_dentry_upper(old->d_parent);
1367 + new_upperdir = ovl_dentry_upper(new->d_parent);
1369 + trap = lock_rename(new_upperdir, old_upperdir);
1371 + olddentry = ovl_dentry_upper(old);
1372 + newdentry = ovl_dentry_upper(new);
1376 + new_create = true;
1377 + newdentry = ovl_lookup_create(new_upperdir, new);
1378 + err = PTR_ERR(newdentry);
1379 + if (IS_ERR(newdentry))
1384 + if (olddentry->d_parent != old_upperdir)
1386 + if (newdentry->d_parent != new_upperdir)
1388 + if (olddentry == trap)
1390 + if (newdentry == trap)
1393 + old_opaque = ovl_dentry_is_opaque(old);
1394 + new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
1396 + if (is_dir && !old_opaque && new_opaque) {
1397 + err = ovl_set_opaque(olddentry);
1402 + err = vfs_rename(old_upperdir->d_inode, olddentry,
1403 + new_upperdir->d_inode, newdentry);
1406 + if (new_create && ovl_dentry_is_opaque(new))
1407 + ovl_whiteout(new_upperdir, new);
1408 + if (is_dir && !old_opaque && new_opaque)
1409 + ovl_remove_opaque(olddentry);
1413 + if (old_type != OVL_PATH_UPPER || old_opaque)
1414 + err = ovl_whiteout(old_upperdir, old);
1415 + if (is_dir && old_opaque && !new_opaque)
1416 + ovl_remove_opaque(olddentry);
1418 + if (old_opaque != new_opaque)
1419 + ovl_dentry_set_opaque(old, new_opaque);
1421 + ovl_dentry_version_inc(old->d_parent);
1422 + ovl_dentry_version_inc(new->d_parent);
1427 + unlock_rename(new_upperdir, old_upperdir);
1431 +const struct inode_operations ovl_dir_inode_operations = {
1432 + .lookup = ovl_lookup,
1433 + .mkdir = ovl_mkdir,
1434 + .symlink = ovl_symlink,
1435 + .unlink = ovl_unlink,
1436 + .rmdir = ovl_rmdir,
1437 + .rename = ovl_rename,
1439 + .setattr = ovl_setattr,
1440 + .create = ovl_create,
1441 + .mknod = ovl_mknod,
1442 + .permission = ovl_permission,
1443 + .getattr = ovl_dir_getattr,
1444 + .setxattr = ovl_setxattr,
1445 + .getxattr = ovl_getxattr,
1446 + .listxattr = ovl_listxattr,
1447 + .removexattr = ovl_removexattr,
1449 diff --git a/fs/overlayfs/inode.c b/fs/overlayfs/inode.c
1450 new file mode 100644
1451 index 0000000..ee37e92
1453 +++ b/fs/overlayfs/inode.c
1457 + * Copyright (C) 2011 Novell Inc.
1459 + * This program is free software; you can redistribute it and/or modify it
1460 + * under the terms of the GNU General Public License version 2 as published by
1461 + * the Free Software Foundation.
1464 +#include <linux/fs.h>
1465 +#include <linux/slab.h>
1466 +#include <linux/xattr.h>
1467 +#include "overlayfs.h"
1469 +int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1471 + struct dentry *upperdentry;
1474 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1475 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1477 + err = ovl_copy_up(dentry);
1481 + upperdentry = ovl_dentry_upper(dentry);
1483 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1484 + attr->ia_valid &= ~ATTR_MODE;
1486 + mutex_lock(&upperdentry->d_inode->i_mutex);
1487 + err = notify_change(upperdentry, attr);
1489 + ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
1490 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1495 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1496 + struct kstat *stat)
1498 + struct path realpath;
1500 + ovl_path_real(dentry, &realpath);
1501 + return vfs_getattr(&realpath, stat);
1504 +int ovl_permission(struct inode *inode, int mask)
1506 + struct ovl_entry *oe;
1507 + struct dentry *alias = NULL;
1508 + struct inode *realinode;
1509 + struct dentry *realdentry;
1513 + if (S_ISDIR(inode->i_mode)) {
1514 + oe = inode->i_private;
1515 + } else if (mask & MAY_NOT_BLOCK) {
1519 + * For non-directories find an alias and get the info
1522 + alias = d_find_any_alias(inode);
1523 + if (WARN_ON(!alias))
1526 + oe = alias->d_fsdata;
1529 + realdentry = ovl_entry_real(oe, &is_upper);
1531 + /* Careful in RCU walk mode */
1532 + realinode = ACCESS_ONCE(realdentry->d_inode);
1534 + WARN_ON(!(mask & MAY_NOT_BLOCK));
1539 + if (mask & MAY_WRITE) {
1540 + umode_t mode = realinode->i_mode;
1543 + * Writes will always be redirected to upper layer, so
1544 + * ignore lower layer being read-only.
1546 + * If the overlay itself is read-only then proceed
1547 + * with the permission check, don't return EROFS.
1548 + * This will only happen if this is the lower layer of
1549 + * another overlayfs.
1551 + * If upper fs becomes read-only after the overlay was
1552 + * constructed return EROFS to prevent modification of
1556 + if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
1557 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1561 + err = __inode_permission(realinode, mask);
1568 +struct ovl_link_data {
1569 + struct dentry *realdentry;
1573 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1576 + struct dentry *realdentry;
1577 + struct inode *realinode;
1579 + realdentry = ovl_dentry_real(dentry);
1580 + realinode = realdentry->d_inode;
1582 + if (WARN_ON(!realinode->i_op->follow_link))
1583 + return ERR_PTR(-EPERM);
1585 + ret = realinode->i_op->follow_link(realdentry, nd);
1589 + if (realinode->i_op->put_link) {
1590 + struct ovl_link_data *data;
1592 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1594 + realinode->i_op->put_link(realdentry, nd, ret);
1595 + return ERR_PTR(-ENOMEM);
1597 + data->realdentry = realdentry;
1598 + data->cookie = ret;
1606 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1608 + struct inode *realinode;
1609 + struct ovl_link_data *data = c;
1614 + realinode = data->realdentry->d_inode;
1615 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1619 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1621 + struct path realpath;
1622 + struct inode *realinode;
1624 + ovl_path_real(dentry, &realpath);
1625 + realinode = realpath.dentry->d_inode;
1627 + if (!realinode->i_op->readlink)
1630 + touch_atime(&realpath);
1632 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1636 +static bool ovl_is_private_xattr(const char *name)
1638 + return strncmp(name, "trusted.overlay.", 14) == 0;
1641 +int ovl_setxattr(struct dentry *dentry, const char *name,
1642 + const void *value, size_t size, int flags)
1645 + struct dentry *upperdentry;
1647 + if (ovl_is_private_xattr(name))
1650 + err = ovl_copy_up(dentry);
1654 + upperdentry = ovl_dentry_upper(dentry);
1655 + return vfs_setxattr(upperdentry, name, value, size, flags);
1658 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1659 + void *value, size_t size)
1661 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1662 + ovl_is_private_xattr(name))
1665 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
1668 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
1673 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
1674 + if (res <= 0 || size == 0)
1677 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
1680 + /* filter out private xattrs */
1681 + for (off = 0; off < res;) {
1682 + char *s = list + off;
1683 + size_t slen = strlen(s) + 1;
1685 + BUG_ON(off + slen > res);
1687 + if (ovl_is_private_xattr(s)) {
1689 + memmove(s, s + slen, res - off);
1698 +int ovl_removexattr(struct dentry *dentry, const char *name)
1701 + struct path realpath;
1702 + enum ovl_path_type type;
1704 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1705 + ovl_is_private_xattr(name))
1708 + type = ovl_path_real(dentry, &realpath);
1709 + if (type == OVL_PATH_LOWER) {
1710 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
1714 + err = ovl_copy_up(dentry);
1718 + ovl_path_upper(dentry, &realpath);
1721 + return vfs_removexattr(realpath.dentry, name);
1724 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
1725 + struct dentry *realdentry)
1727 + if (type != OVL_PATH_LOWER)
1730 + if (special_file(realdentry->d_inode->i_mode))
1733 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
1739 +static int ovl_dentry_open(struct dentry *dentry, struct file *file,
1740 + const struct cred *cred)
1743 + struct path realpath;
1744 + enum ovl_path_type type;
1746 + type = ovl_path_real(dentry, &realpath);
1747 + if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
1748 + if (file->f_flags & O_TRUNC)
1749 + err = ovl_copy_up_truncate(dentry, 0);
1751 + err = ovl_copy_up(dentry);
1755 + ovl_path_upper(dentry, &realpath);
1758 + return vfs_open(&realpath, file, cred);
1761 +static const struct inode_operations ovl_file_inode_operations = {
1762 + .setattr = ovl_setattr,
1763 + .permission = ovl_permission,
1764 + .getattr = ovl_getattr,
1765 + .setxattr = ovl_setxattr,
1766 + .getxattr = ovl_getxattr,
1767 + .listxattr = ovl_listxattr,
1768 + .removexattr = ovl_removexattr,
1769 + .dentry_open = ovl_dentry_open,
1772 +static const struct inode_operations ovl_symlink_inode_operations = {
1773 + .setattr = ovl_setattr,
1774 + .follow_link = ovl_follow_link,
1775 + .put_link = ovl_put_link,
1776 + .readlink = ovl_readlink,
1777 + .getattr = ovl_getattr,
1778 + .setxattr = ovl_setxattr,
1779 + .getxattr = ovl_getxattr,
1780 + .listxattr = ovl_listxattr,
1781 + .removexattr = ovl_removexattr,
1784 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1785 + struct ovl_entry *oe)
1787 + struct inode *inode;
1789 + inode = new_inode(sb);
1795 + inode->i_ino = get_next_ino();
1796 + inode->i_mode = mode;
1797 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
1801 + inode->i_private = oe;
1802 + inode->i_op = &ovl_dir_inode_operations;
1803 + inode->i_fop = &ovl_dir_operations;
1807 + inode->i_op = &ovl_symlink_inode_operations;
1815 + inode->i_op = &ovl_file_inode_operations;
1819 + WARN(1, "illegal file type: %i\n", mode);
1827 diff --git a/fs/overlayfs/overlayfs.h b/fs/overlayfs/overlayfs.h
1828 new file mode 100644
1829 index 0000000..1cba38f
1831 +++ b/fs/overlayfs/overlayfs.h
1835 + * Copyright (C) 2011 Novell Inc.
1837 + * This program is free software; you can redistribute it and/or modify it
1838 + * under the terms of the GNU General Public License version 2 as published by
1839 + * the Free Software Foundation.
1844 +enum ovl_path_type {
1850 +extern const char *ovl_opaque_xattr;
1851 +extern const char *ovl_whiteout_xattr;
1852 +extern const struct dentry_operations ovl_dentry_operations;
1854 +enum ovl_path_type ovl_path_type(struct dentry *dentry);
1855 +u64 ovl_dentry_version_get(struct dentry *dentry);
1856 +void ovl_dentry_version_inc(struct dentry *dentry);
1857 +void ovl_path_upper(struct dentry *dentry, struct path *path);
1858 +void ovl_path_lower(struct dentry *dentry, struct path *path);
1859 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1860 +struct dentry *ovl_dentry_upper(struct dentry *dentry);
1861 +struct dentry *ovl_dentry_lower(struct dentry *dentry);
1862 +struct dentry *ovl_dentry_real(struct dentry *dentry);
1863 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
1864 +bool ovl_dentry_is_opaque(struct dentry *dentry);
1865 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
1866 +bool ovl_is_whiteout(struct dentry *dentry);
1867 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
1868 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
1869 + unsigned int flags);
1870 +struct file *ovl_path_open(struct path *path, int flags);
1872 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
1873 + struct kstat *stat, const char *link);
1876 +extern const struct file_operations ovl_dir_operations;
1877 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
1880 +int ovl_setattr(struct dentry *dentry, struct iattr *attr);
1881 +int ovl_permission(struct inode *inode, int mask);
1882 +int ovl_setxattr(struct dentry *dentry, const char *name,
1883 + const void *value, size_t size, int flags);
1884 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1885 + void *value, size_t size);
1886 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
1887 +int ovl_removexattr(struct dentry *dentry, const char *name);
1889 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1890 + struct ovl_entry *oe);
1891 +static inline void ovl_copyattr(struct inode *from, struct inode *to)
1893 + to->i_uid = from->i_uid;
1894 + to->i_gid = from->i_gid;
1898 +extern const struct inode_operations ovl_dir_inode_operations;
1901 +int ovl_copy_up(struct dentry *dentry);
1902 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
1903 diff --git a/fs/overlayfs/readdir.c b/fs/overlayfs/readdir.c
1904 new file mode 100644
1905 index 0000000..0797efb
1907 +++ b/fs/overlayfs/readdir.c
1911 + * Copyright (C) 2011 Novell Inc.
1913 + * This program is free software; you can redistribute it and/or modify it
1914 + * under the terms of the GNU General Public License version 2 as published by
1915 + * the Free Software Foundation.
1918 +#include <linux/fs.h>
1919 +#include <linux/slab.h>
1920 +#include <linux/namei.h>
1921 +#include <linux/file.h>
1922 +#include <linux/xattr.h>
1923 +#include <linux/rbtree.h>
1924 +#include <linux/security.h>
1925 +#include <linux/cred.h>
1926 +#include "overlayfs.h"
1928 +struct ovl_cache_entry {
1931 + unsigned int type;
1934 + struct list_head l_node;
1935 + struct rb_node node;
1938 +struct ovl_readdir_data {
1939 + struct rb_root *root;
1940 + struct list_head *list;
1941 + struct list_head *middle;
1942 + struct dentry *dir;
1947 +struct ovl_dir_file {
1950 + struct list_head cursor;
1951 + u64 cache_version;
1952 + struct list_head cache;
1953 + struct file *realfile;
1956 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
1958 + return container_of(n, struct ovl_cache_entry, node);
1961 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
1962 + const char *name, int len)
1964 + struct rb_node *node = root->rb_node;
1968 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
1970 + cmp = strncmp(name, p->name, len);
1972 + node = p->node.rb_right;
1973 + else if (cmp < 0 || len < p->len)
1974 + node = p->node.rb_left;
1982 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
1983 + u64 ino, unsigned int d_type)
1985 + struct ovl_cache_entry *p;
1987 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
1989 + char *name_copy = (char *) (p + 1);
1990 + memcpy(name_copy, name, len);
1991 + name_copy[len] = '\0';
1992 + p->name = name_copy;
1996 + p->is_whiteout = false;
2002 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
2003 + const char *name, int len, u64 ino,
2004 + unsigned int d_type)
2006 + struct rb_node **newp = &rdd->root->rb_node;
2007 + struct rb_node *parent = NULL;
2008 + struct ovl_cache_entry *p;
2012 + struct ovl_cache_entry *tmp;
2015 + tmp = ovl_cache_entry_from_node(*newp);
2016 + cmp = strncmp(name, tmp->name, len);
2018 + newp = &tmp->node.rb_right;
2019 + else if (cmp < 0 || len < tmp->len)
2020 + newp = &tmp->node.rb_left;
2025 + p = ovl_cache_entry_new(name, len, ino, d_type);
2029 + list_add_tail(&p->l_node, rdd->list);
2030 + rb_link_node(&p->node, parent, newp);
2031 + rb_insert_color(&p->node, rdd->root);
2036 +static int ovl_fill_lower(void *buf, const char *name, int namelen,
2037 + loff_t offset, u64 ino, unsigned int d_type)
2039 + struct ovl_readdir_data *rdd = buf;
2040 + struct ovl_cache_entry *p;
2043 + p = ovl_cache_entry_find(rdd->root, name, namelen);
2045 + list_move_tail(&p->l_node, rdd->middle);
2047 + p = ovl_cache_entry_new(name, namelen, ino, d_type);
2049 + rdd->err = -ENOMEM;
2051 + list_add_tail(&p->l_node, rdd->middle);
2057 +static void ovl_cache_free(struct list_head *list)
2059 + struct ovl_cache_entry *p;
2060 + struct ovl_cache_entry *n;
2062 + list_for_each_entry_safe(p, n, list, l_node)
2065 + INIT_LIST_HEAD(list);
2068 +static int ovl_fill_upper(void *buf, const char *name, int namelen,
2069 + loff_t offset, u64 ino, unsigned int d_type)
2071 + struct ovl_readdir_data *rdd = buf;
2074 + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
2077 +static inline int ovl_dir_read(struct path *realpath,
2078 + struct ovl_readdir_data *rdd, filldir_t filler)
2080 + struct file *realfile;
2083 + realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
2084 + if (IS_ERR(realfile))
2085 + return PTR_ERR(realfile);
2090 + err = vfs_readdir(realfile, filler, rdd);
2093 + } while (!err && rdd->count);
2099 +static void ovl_dir_reset(struct file *file)
2101 + struct ovl_dir_file *od = file->private_data;
2102 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
2104 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
2105 + list_del_init(&od->cursor);
2106 + ovl_cache_free(&od->cache);
2107 + od->is_cached = false;
2109 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
2110 + if (od->is_real && type == OVL_PATH_MERGE) {
2111 + fput(od->realfile);
2112 + od->realfile = NULL;
2113 + od->is_real = false;
2117 +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
2119 + struct ovl_cache_entry *p;
2120 + struct dentry *dentry;
2121 + const struct cred *old_cred;
2122 + struct cred *override_cred;
2124 + override_cred = prepare_creds();
2125 + if (!override_cred) {
2126 + ovl_cache_free(rdd->list);
2131 + * CAP_SYS_ADMIN for getxattr
2132 + * CAP_DAC_OVERRIDE for lookup
2134 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2135 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2136 + old_cred = override_creds(override_cred);
2138 + mutex_lock(&rdd->dir->d_inode->i_mutex);
2139 + list_for_each_entry(p, rdd->list, l_node) {
2140 + if (p->type != DT_LNK)
2143 + dentry = lookup_one_len(p->name, rdd->dir, p->len);
2144 + if (IS_ERR(dentry))
2147 + p->is_whiteout = ovl_is_whiteout(dentry);
2150 + mutex_unlock(&rdd->dir->d_inode->i_mutex);
2152 + revert_creds(old_cred);
2153 + put_cred(override_cred);
2158 +static inline int ovl_dir_read_merged(struct path *upperpath,
2159 + struct path *lowerpath,
2160 + struct ovl_readdir_data *rdd)
2163 + struct rb_root root = RB_ROOT;
2164 + struct list_head middle;
2166 + rdd->root = &root;
2167 + if (upperpath->dentry) {
2168 + rdd->dir = upperpath->dentry;
2169 + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
2173 + err = ovl_dir_mark_whiteouts(rdd);
2178 + * Insert lowerpath entries before upperpath ones, this allows
2179 + * offsets to be reasonably constant
2181 + list_add(&middle, rdd->list);
2182 + rdd->middle = &middle;
2183 + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower);
2184 + list_del(&middle);
2191 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
2193 + struct list_head *l;
2196 + l = od->cache.next;
2197 + for (off = 0; off < pos; off++) {
2198 + if (l == &od->cache)
2202 + list_move_tail(&od->cursor, l);
2205 +static int ovl_readdir(struct file *file, void *buf, filldir_t filler)
2207 + struct ovl_dir_file *od = file->private_data;
2211 + ovl_dir_reset(file);
2213 + if (od->is_real) {
2214 + res = vfs_readdir(od->realfile, filler, buf);
2215 + file->f_pos = od->realfile->f_pos;
2220 + if (!od->is_cached) {
2221 + struct path lowerpath;
2222 + struct path upperpath;
2223 + struct ovl_readdir_data rdd = { .list = &od->cache };
2225 + ovl_path_lower(file->f_path.dentry, &lowerpath);
2226 + ovl_path_upper(file->f_path.dentry, &upperpath);
2228 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2230 + ovl_cache_free(rdd.list);
2234 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
2235 + od->is_cached = true;
2237 + ovl_seek_cursor(od, file->f_pos);
2240 + while (od->cursor.next != &od->cache) {
2243 + struct ovl_cache_entry *p;
2245 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
2246 + off = file->f_pos;
2247 + if (!p->is_whiteout) {
2248 + over = filler(buf, p->name, p->len, off, p->ino,
2254 + list_move(&od->cursor, &p->l_node);
2260 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
2263 + struct ovl_dir_file *od = file->private_data;
2265 + mutex_lock(&file->f_dentry->d_inode->i_mutex);
2267 + ovl_dir_reset(file);
2269 + if (od->is_real) {
2270 + res = vfs_llseek(od->realfile, offset, origin);
2271 + file->f_pos = od->realfile->f_pos;
2277 + offset += file->f_pos;
2287 + if (offset != file->f_pos) {
2288 + file->f_pos = offset;
2289 + if (od->is_cached)
2290 + ovl_seek_cursor(od, offset);
2295 + mutex_unlock(&file->f_dentry->d_inode->i_mutex);
2300 +static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
2303 + struct ovl_dir_file *od = file->private_data;
2305 + /* May need to reopen directory if it got copied up */
2306 + if (!od->realfile) {
2307 + struct path upperpath;
2309 + ovl_path_upper(file->f_path.dentry, &upperpath);
2310 + od->realfile = ovl_path_open(&upperpath, O_RDONLY);
2311 + if (IS_ERR(od->realfile))
2312 + return PTR_ERR(od->realfile);
2315 + return vfs_fsync_range(od->realfile, start, end, datasync);
2318 +static int ovl_dir_release(struct inode *inode, struct file *file)
2320 + struct ovl_dir_file *od = file->private_data;
2322 + list_del(&od->cursor);
2323 + ovl_cache_free(&od->cache);
2325 + fput(od->realfile);
2331 +static int ovl_dir_open(struct inode *inode, struct file *file)
2333 + struct path realpath;
2334 + struct file *realfile;
2335 + struct ovl_dir_file *od;
2336 + enum ovl_path_type type;
2338 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
2342 + type = ovl_path_real(file->f_path.dentry, &realpath);
2343 + realfile = ovl_path_open(&realpath, file->f_flags);
2344 + if (IS_ERR(realfile)) {
2346 + return PTR_ERR(realfile);
2348 + INIT_LIST_HEAD(&od->cache);
2349 + INIT_LIST_HEAD(&od->cursor);
2350 + od->is_cached = false;
2351 + od->realfile = realfile;
2352 + od->is_real = (type != OVL_PATH_MERGE);
2353 + file->private_data = od;
2358 +const struct file_operations ovl_dir_operations = {
2359 + .read = generic_read_dir,
2360 + .open = ovl_dir_open,
2361 + .readdir = ovl_readdir,
2362 + .llseek = ovl_dir_llseek,
2363 + .fsync = ovl_dir_fsync,
2364 + .release = ovl_dir_release,
2367 +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
2370 + struct path lowerpath;
2371 + struct path upperpath;
2372 + struct ovl_cache_entry *p;
2373 + struct ovl_readdir_data rdd = { .list = list };
2375 + ovl_path_upper(dentry, &upperpath);
2376 + ovl_path_lower(dentry, &lowerpath);
2378 + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2384 + list_for_each_entry(p, list, l_node) {
2385 + if (p->is_whiteout)
2388 + if (p->name[0] == '.') {
2391 + if (p->len == 2 && p->name[1] == '.')
2401 +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
2403 + struct path upperpath;
2404 + struct dentry *upperdir;
2405 + struct ovl_cache_entry *p;
2406 + const struct cred *old_cred;
2407 + struct cred *override_cred;
2410 + ovl_path_upper(dir, &upperpath);
2411 + upperdir = upperpath.dentry;
2413 + override_cred = prepare_creds();
2414 + if (!override_cred)
2418 + * CAP_DAC_OVERRIDE for lookup and unlink
2419 + * CAP_SYS_ADMIN for setxattr of "trusted" namespace
2420 + * CAP_FOWNER for unlink in sticky directory
2422 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2423 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2424 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
2425 + old_cred = override_creds(override_cred);
2427 + err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
2429 + goto out_revert_creds;
2431 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2432 + list_for_each_entry(p, list, l_node) {
2433 + struct dentry *dentry;
2436 + if (!p->is_whiteout)
2439 + dentry = lookup_one_len(p->name, upperdir, p->len);
2440 + if (IS_ERR(dentry)) {
2441 + printk(KERN_WARNING
2442 + "overlayfs: failed to lookup whiteout %.*s: %li\n",
2443 + p->len, p->name, PTR_ERR(dentry));
2446 + ret = vfs_unlink(upperdir->d_inode, dentry);
2449 + printk(KERN_WARNING
2450 + "overlayfs: failed to unlink whiteout %.*s: %i\n",
2451 + p->len, p->name, ret);
2453 + mutex_unlock(&upperdir->d_inode->i_mutex);
2456 + revert_creds(old_cred);
2457 + put_cred(override_cred);
2462 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
2467 + err = ovl_check_empty_dir(dentry, &list);
2468 + if (!err && type == OVL_PATH_MERGE)
2469 + err = ovl_remove_whiteouts(dentry, &list);
2471 + ovl_cache_free(&list);
2475 diff --git a/fs/overlayfs/super.c b/fs/overlayfs/super.c
2476 new file mode 100644
2477 index 0000000..357d6e8
2479 +++ b/fs/overlayfs/super.c
2483 + * Copyright (C) 2011 Novell Inc.
2485 + * This program is free software; you can redistribute it and/or modify it
2486 + * under the terms of the GNU General Public License version 2 as published by
2487 + * the Free Software Foundation.
2490 +#include <linux/fs.h>
2491 +#include <linux/namei.h>
2492 +#include <linux/xattr.h>
2493 +#include <linux/security.h>
2494 +#include <linux/mount.h>
2495 +#include <linux/slab.h>
2496 +#include <linux/parser.h>
2497 +#include <linux/module.h>
2498 +#include <linux/cred.h>
2499 +#include <linux/sched.h>
2500 +#include <linux/statfs.h>
2501 +#include <linux/seq_file.h>
2502 +#include "overlayfs.h"
2504 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2505 +MODULE_DESCRIPTION("Overlay filesystem");
2506 +MODULE_LICENSE("GPL");
2508 +#define OVERLAYFS_SUPER_MAGIC 0x794c764f
2510 +struct ovl_config {
2515 +/* private information held for overlayfs's superblock */
2517 + struct vfsmount *upper_mnt;
2518 + struct vfsmount *lower_mnt;
2519 + long lower_namelen;
2520 + /* pathnames of lower and upper dirs, for show_options */
2521 + struct ovl_config config;
2524 +/* private information held for every overlayfs dentry */
2527 + * Keep "double reference" on upper dentries, so that
2528 + * d_delete() doesn't think it's OK to reset d_inode to NULL.
2530 + struct dentry *__upperdentry;
2531 + struct dentry *lowerdentry;
2537 + struct rcu_head rcu;
2541 +const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
2542 +const char *ovl_opaque_xattr = "trusted.overlay.opaque";
2545 +enum ovl_path_type ovl_path_type(struct dentry *dentry)
2547 + struct ovl_entry *oe = dentry->d_fsdata;
2549 + if (oe->__upperdentry) {
2550 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
2551 + return OVL_PATH_MERGE;
2553 + return OVL_PATH_UPPER;
2555 + return OVL_PATH_LOWER;
2559 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
2561 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
2562 + smp_read_barrier_depends();
2563 + return upperdentry;
2566 +void ovl_path_upper(struct dentry *dentry, struct path *path)
2568 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2569 + struct ovl_entry *oe = dentry->d_fsdata;
2571 + path->mnt = ofs->upper_mnt;
2572 + path->dentry = ovl_upperdentry_dereference(oe);
2575 +void ovl_path_lower(struct dentry *dentry, struct path *path)
2577 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2578 + struct ovl_entry *oe = dentry->d_fsdata;
2580 + path->mnt = ofs->lower_mnt;
2581 + path->dentry = oe->lowerdentry;
2584 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
2587 + enum ovl_path_type type = ovl_path_type(dentry);
2589 + if (type == OVL_PATH_LOWER)
2590 + ovl_path_lower(dentry, path);
2592 + ovl_path_upper(dentry, path);
2597 +struct dentry *ovl_dentry_upper(struct dentry *dentry)
2599 + struct ovl_entry *oe = dentry->d_fsdata;
2601 + return ovl_upperdentry_dereference(oe);
2604 +struct dentry *ovl_dentry_lower(struct dentry *dentry)
2606 + struct ovl_entry *oe = dentry->d_fsdata;
2608 + return oe->lowerdentry;
2611 +struct dentry *ovl_dentry_real(struct dentry *dentry)
2613 + struct ovl_entry *oe = dentry->d_fsdata;
2614 + struct dentry *realdentry;
2616 + realdentry = ovl_upperdentry_dereference(oe);
2618 + realdentry = oe->lowerdentry;
2620 + return realdentry;
2623 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
2625 + struct dentry *realdentry;
2627 + realdentry = ovl_upperdentry_dereference(oe);
2631 + realdentry = oe->lowerdentry;
2632 + *is_upper = false;
2634 + return realdentry;
2637 +bool ovl_dentry_is_opaque(struct dentry *dentry)
2639 + struct ovl_entry *oe = dentry->d_fsdata;
2640 + return oe->opaque;
2643 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
2645 + struct ovl_entry *oe = dentry->d_fsdata;
2646 + oe->opaque = opaque;
2649 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
2651 + struct ovl_entry *oe = dentry->d_fsdata;
2653 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
2654 + WARN_ON(oe->__upperdentry);
2655 + BUG_ON(!upperdentry->d_inode);
2657 + oe->__upperdentry = dget(upperdentry);
2660 +void ovl_dentry_version_inc(struct dentry *dentry)
2662 + struct ovl_entry *oe = dentry->d_fsdata;
2664 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2668 +u64 ovl_dentry_version_get(struct dentry *dentry)
2670 + struct ovl_entry *oe = dentry->d_fsdata;
2672 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2673 + return oe->version;
2676 +bool ovl_is_whiteout(struct dentry *dentry)
2683 + if (!dentry->d_inode)
2685 + if (!S_ISLNK(dentry->d_inode->i_mode))
2688 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
2689 + if (res == 1 && val == 'y')
2695 +static bool ovl_is_opaquedir(struct dentry *dentry)
2700 + if (!S_ISDIR(dentry->d_inode->i_mode))
2703 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
2704 + if (res == 1 && val == 'y')
2710 +static void ovl_entry_free(struct rcu_head *head)
2712 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
2716 +static void ovl_dentry_release(struct dentry *dentry)
2718 + struct ovl_entry *oe = dentry->d_fsdata;
2721 + dput(oe->__upperdentry);
2722 + dput(oe->__upperdentry);
2723 + dput(oe->lowerdentry);
2724 + call_rcu(&oe->rcu, ovl_entry_free);
2728 +const struct dentry_operations ovl_dentry_operations = {
2729 + .d_release = ovl_dentry_release,
2732 +static struct ovl_entry *ovl_alloc_entry(void)
2734 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
2737 +static inline struct dentry *ovl_lookup_real(struct dentry *dir,
2738 + struct qstr *name)
2740 + struct dentry *dentry;
2742 + mutex_lock(&dir->d_inode->i_mutex);
2743 + dentry = lookup_one_len(name->name, dir, name->len);
2744 + mutex_unlock(&dir->d_inode->i_mutex);
2746 + if (IS_ERR(dentry)) {
2747 + if (PTR_ERR(dentry) == -ENOENT)
2749 + } else if (!dentry->d_inode) {
2756 +static int ovl_do_lookup(struct dentry *dentry)
2758 + struct ovl_entry *oe;
2759 + struct dentry *upperdir;
2760 + struct dentry *lowerdir;
2761 + struct dentry *upperdentry = NULL;
2762 + struct dentry *lowerdentry = NULL;
2763 + struct inode *inode = NULL;
2767 + oe = ovl_alloc_entry();
2771 + upperdir = ovl_dentry_upper(dentry->d_parent);
2772 + lowerdir = ovl_dentry_lower(dentry->d_parent);
2775 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
2776 + err = PTR_ERR(upperdentry);
2777 + if (IS_ERR(upperdentry))
2780 + if (lowerdir && upperdentry &&
2781 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
2782 + S_ISDIR(upperdentry->d_inode->i_mode))) {
2783 + const struct cred *old_cred;
2784 + struct cred *override_cred;
2787 + override_cred = prepare_creds();
2788 + if (!override_cred)
2789 + goto out_dput_upper;
2791 + /* CAP_SYS_ADMIN needed for getxattr */
2792 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2793 + old_cred = override_creds(override_cred);
2795 + if (ovl_is_opaquedir(upperdentry)) {
2796 + oe->opaque = true;
2797 + } else if (ovl_is_whiteout(upperdentry)) {
2798 + dput(upperdentry);
2799 + upperdentry = NULL;
2800 + oe->opaque = true;
2802 + revert_creds(old_cred);
2803 + put_cred(override_cred);
2806 + if (lowerdir && !oe->opaque) {
2807 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
2808 + err = PTR_ERR(lowerdentry);
2809 + if (IS_ERR(lowerdentry))
2810 + goto out_dput_upper;
2813 + if (lowerdentry && upperdentry &&
2814 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
2815 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
2816 + dput(lowerdentry);
2817 + lowerdentry = NULL;
2818 + oe->opaque = true;
2821 + if (lowerdentry || upperdentry) {
2822 + struct dentry *realdentry;
2824 + realdentry = upperdentry ? upperdentry : lowerdentry;
2826 + inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
2830 + ovl_copyattr(realdentry->d_inode, inode);
2834 + oe->__upperdentry = dget(upperdentry);
2837 + oe->lowerdentry = lowerdentry;
2839 + dentry->d_fsdata = oe;
2840 + dentry->d_op = &ovl_dentry_operations;
2841 + d_add(dentry, inode);
2846 + dput(lowerdentry);
2848 + dput(upperdentry);
2855 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
2856 + unsigned int flags)
2858 + int err = ovl_do_lookup(dentry);
2861 + return ERR_PTR(err);
2866 +struct file *ovl_path_open(struct path *path, int flags)
2869 + return dentry_open(path, flags, current_cred());
2872 +static void ovl_put_super(struct super_block *sb)
2874 + struct ovl_fs *ufs = sb->s_fs_info;
2876 + if (!(sb->s_flags & MS_RDONLY))
2877 + mnt_drop_write(ufs->upper_mnt);
2879 + mntput(ufs->upper_mnt);
2880 + mntput(ufs->lower_mnt);
2882 + kfree(ufs->config.lowerdir);
2883 + kfree(ufs->config.upperdir);
2887 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2889 + int flags = *flagsp;
2890 + struct ovl_fs *ufs = sb->s_fs_info;
2892 + /* When remounting rw or ro, we need to adjust the write access to the
2895 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2896 + /* No change to readonly status */
2899 + if (flags & MS_RDONLY) {
2900 + mnt_drop_write(ufs->upper_mnt);
2903 + return mnt_want_write(ufs->upper_mnt);
2908 + * @sb: The overlayfs super block
2909 + * @buf: The struct kstatfs to fill in with stats
2911 + * Get the filesystem statistics. As writes always target the upper layer
2912 + * filesystem pass the statfs to the same filesystem.
2914 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2916 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2917 + struct dentry *root_dentry = dentry->d_sb->s_root;
2921 + ovl_path_upper(root_dentry, &path);
2923 + err = vfs_statfs(&path, buf);
2925 + buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
2926 + buf->f_type = OVERLAYFS_SUPER_MAGIC;
2933 + * ovl_show_options
2935 + * Prints the mount options for a given superblock.
2936 + * Returns zero; does not fail.
2938 +static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
2940 + struct super_block *sb = dentry->d_sb;
2941 + struct ovl_fs *ufs = sb->s_fs_info;
2943 + seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
2944 + seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
2948 +static const struct super_operations ovl_super_operations = {
2949 + .put_super = ovl_put_super,
2950 + .remount_fs = ovl_remount_fs,
2951 + .statfs = ovl_statfs,
2952 + .show_options = ovl_show_options,
2961 +static const match_table_t ovl_tokens = {
2962 + {Opt_lowerdir, "lowerdir=%s"},
2963 + {Opt_upperdir, "upperdir=%s"},
2967 +static int ovl_parse_opt(char *opt, struct ovl_config *config)
2971 + config->upperdir = NULL;
2972 + config->lowerdir = NULL;
2974 + while ((p = strsep(&opt, ",")) != NULL) {
2976 + substring_t args[MAX_OPT_ARGS];
2981 + token = match_token(p, ovl_tokens, args);
2983 + case Opt_upperdir:
2984 + kfree(config->upperdir);
2985 + config->upperdir = match_strdup(&args[0]);
2986 + if (!config->upperdir)
2990 + case Opt_lowerdir:
2991 + kfree(config->lowerdir);
2992 + config->lowerdir = match_strdup(&args[0]);
2993 + if (!config->lowerdir)
3004 +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
3006 + struct path lowerpath;
3007 + struct path upperpath;
3008 + struct inode *root_inode;
3009 + struct dentry *root_dentry;
3010 + struct ovl_entry *oe;
3011 + struct ovl_fs *ufs;
3012 + struct kstatfs statfs;
3016 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
3020 + err = ovl_parse_opt((char *) data, &ufs->config);
3022 + goto out_free_ufs;
3025 + if (!ufs->config.upperdir || !ufs->config.lowerdir) {
3026 + printk(KERN_ERR "overlayfs: missing upperdir or lowerdir\n");
3027 + goto out_free_config;
3030 + oe = ovl_alloc_entry();
3032 + goto out_free_config;
3034 + err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
3038 + err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
3040 + goto out_put_upperpath;
3043 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
3044 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
3045 + goto out_put_lowerpath;
3047 + err = vfs_statfs(&lowerpath, &statfs);
3049 + printk(KERN_ERR "overlayfs: statfs failed on lowerpath\n");
3050 + goto out_put_lowerpath;
3052 + ufs->lower_namelen = statfs.f_namelen;
3054 + sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
3055 + lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
3058 + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
3059 + printk(KERN_ERR "overlayfs: maximum fs stacking depth exceeded\n");
3060 + goto out_put_lowerpath;
3064 + ufs->upper_mnt = clone_private_mount(&upperpath);
3065 + err = PTR_ERR(ufs->upper_mnt);
3066 + if (IS_ERR(ufs->upper_mnt)) {
3067 + printk(KERN_ERR "overlayfs: failed to clone upperpath\n");
3068 + goto out_put_lowerpath;
3071 + ufs->lower_mnt = clone_private_mount(&lowerpath);
3072 + err = PTR_ERR(ufs->lower_mnt);
3073 + if (IS_ERR(ufs->lower_mnt)) {
3074 + printk(KERN_ERR "overlayfs: failed to clone lowerpath\n");
3075 + goto out_put_upper_mnt;
3079 + * Make lower_mnt R/O. That way fchmod/fchown on lower file
3080 + * will fail instead of modifying lower fs.
3082 + ufs->lower_mnt->mnt_flags |= MNT_READONLY;
3084 + /* If the upper fs is r/o, we mark overlayfs r/o too */
3085 + if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
3086 + sb->s_flags |= MS_RDONLY;
3088 + if (!(sb->s_flags & MS_RDONLY)) {
3089 + err = mnt_want_write(ufs->upper_mnt);
3091 + goto out_put_lower_mnt;
3095 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
3097 + goto out_drop_write;
3099 + root_dentry = d_make_root(root_inode);
3101 + goto out_drop_write;
3103 + mntput(upperpath.mnt);
3104 + mntput(lowerpath.mnt);
3106 + oe->__upperdentry = dget(upperpath.dentry);
3107 + oe->lowerdentry = lowerpath.dentry;
3109 + root_dentry->d_fsdata = oe;
3110 + root_dentry->d_op = &ovl_dentry_operations;
3112 + sb->s_magic = OVERLAYFS_SUPER_MAGIC;
3113 + sb->s_op = &ovl_super_operations;
3114 + sb->s_root = root_dentry;
3115 + sb->s_fs_info = ufs;
3120 + if (!(sb->s_flags & MS_RDONLY))
3121 + mnt_drop_write(ufs->upper_mnt);
3123 + mntput(ufs->lower_mnt);
3125 + mntput(ufs->upper_mnt);
3127 + path_put(&lowerpath);
3129 + path_put(&upperpath);
3133 + kfree(ufs->config.lowerdir);
3134 + kfree(ufs->config.upperdir);
3141 +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
3142 + const char *dev_name, void *raw_data)
3144 + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
3147 +static struct file_system_type ovl_fs_type = {
3148 + .owner = THIS_MODULE,
3149 + .name = "overlayfs",
3150 + .mount = ovl_mount,
3151 + .kill_sb = kill_anon_super,
3154 +static int __init ovl_init(void)
3156 + return register_filesystem(&ovl_fs_type);
3159 +static void __exit ovl_exit(void)
3161 + unregister_filesystem(&ovl_fs_type);
3164 +module_init(ovl_init);
3165 +module_exit(ovl_exit);
3166 diff --git a/fs/splice.c b/fs/splice.c
3167 index 718bd00..0e8f44a 100644
3170 @@ -1308,6 +1308,7 @@ long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
3174 +EXPORT_SYMBOL(do_splice_direct);
3176 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
3177 struct pipe_inode_info *opipe,
3178 diff --git a/include/linux/fs.h b/include/linux/fs.h
3179 index 2c28271..3353de6 100644
3180 --- a/include/linux/fs.h
3181 +++ b/include/linux/fs.h
3182 @@ -244,6 +244,12 @@ struct iattr {
3184 #include <linux/quota.h>
3187 + * Maximum number of layers of fs stack. Needs to be limited to
3188 + * prevent kernel stack overflow
3190 +#define FILESYSTEM_MAX_STACK_DEPTH 2
3193 * enum positive_aop_returns - aop return codes with specific semantics
3195 @@ -1320,6 +1326,11 @@ struct super_block {
3197 /* Being remounted read-only */
3198 int s_readonly_remount;
3201 + * Indicates how deep in a filesystem stack this SB is
3203 + int s_stack_depth;
3206 /* superblock cache pruning functions */
3207 @@ -1573,6 +1584,7 @@ struct inode_operations {
3208 int (*atomic_open)(struct inode *, struct dentry *,
3209 struct file *, unsigned open_flag,
3210 umode_t create_mode, int *opened);
3211 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
3212 } ____cacheline_aligned;
3214 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
3215 @@ -2006,6 +2018,7 @@ extern struct file *file_open_name(struct filename *, int, umode_t);
3216 extern struct file *filp_open(const char *, int, umode_t);
3217 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
3219 +extern int vfs_open(const struct path *, struct file *, const struct cred *);
3220 extern struct file * dentry_open(const struct path *, int, const struct cred *);
3221 extern int filp_close(struct file *, fl_owner_t id);
3223 @@ -2211,6 +2224,7 @@ extern sector_t bmap(struct inode *, sector_t);
3225 extern int notify_change(struct dentry *, struct iattr *);
3226 extern int inode_permission(struct inode *, int);
3227 +extern int __inode_permission(struct inode *, int);
3228 extern int generic_permission(struct inode *, int);
3230 static inline bool execute_ok(struct inode *inode)
3231 diff --git a/include/linux/mount.h b/include/linux/mount.h
3232 index d7029f4..344a262 100644
3233 --- a/include/linux/mount.h
3234 +++ b/include/linux/mount.h
3235 @@ -66,6 +66,9 @@ extern void mnt_pin(struct vfsmount *mnt);
3236 extern void mnt_unpin(struct vfsmount *mnt);
3237 extern int __mnt_is_readonly(struct vfsmount *mnt);
3240 +extern struct vfsmount *clone_private_mount(struct path *path);
3242 struct file_system_type;
3243 extern struct vfsmount *vfs_kern_mount(struct file_system_type *type,
3244 int flags, const char *name,