1 --- a/Documentation/filesystems/Locking
2 +++ b/Documentation/filesystems/Locking
3 @@ -66,6 +66,7 @@ prototypes:
4 int (*atomic_open)(struct inode *, struct dentry *,
5 struct file *, unsigned open_flag,
6 umode_t create_mode, int *opened);
7 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
11 @@ -93,6 +94,7 @@ removexattr: yes
17 Additionally, ->rmdir(), ->unlink() and ->rename() have ->i_mutex on
20 +++ b/Documentation/filesystems/overlayfs.txt
22 +Written by: Neil Brown <neilb@suse.de>
27 +This document describes a prototype for a new approach to providing
28 +overlay-filesystem functionality in Linux (sometimes referred to as
29 +union-filesystems). An overlay-filesystem tries to present a
30 +filesystem which is the result over overlaying one filesystem on top
33 +The result will inevitably fail to look exactly like a normal
34 +filesystem for various technical reasons. The expectation is that
35 +many use cases will be able to ignore these differences.
37 +This approach is 'hybrid' because the objects that appear in the
38 +filesystem do not all appear to belong to that filesystem. In many
39 +cases an object accessed in the union will be indistinguishable
40 +from accessing the corresponding object from the original filesystem.
41 +This is most obvious from the 'st_dev' field returned by stat(2).
43 +While directories will report an st_dev from the overlay-filesystem,
44 +all non-directory objects will report an st_dev from the lower or
45 +upper filesystem that is providing the object. Similarly st_ino will
46 +only be unique when combined with st_dev, and both of these can change
47 +over the lifetime of a non-directory object. Many applications and
48 +tools ignore these values and will not be affected.
53 +An overlay filesystem combines two filesystems - an 'upper' filesystem
54 +and a 'lower' filesystem. When a name exists in both filesystems, the
55 +object in the 'upper' filesystem is visible while the object in the
56 +'lower' filesystem is either hidden or, in the case of directories,
57 +merged with the 'upper' object.
59 +It would be more correct to refer to an upper and lower 'directory
60 +tree' rather than 'filesystem' as it is quite possible for both
61 +directory trees to be in the same filesystem and there is no
62 +requirement that the root of a filesystem be given for either upper or
65 +The lower filesystem can be any filesystem supported by Linux and does
66 +not need to be writable. The lower filesystem can even be another
67 +overlayfs. The upper filesystem will normally be writable and if it
68 +is it must support the creation of trusted.* extended attributes, and
69 +must provide valid d_type in readdir responses, at least for symbolic
70 +links - so NFS is not suitable.
72 +A read-only overlay of two read-only filesystems may use any
78 +Overlaying mainly involves directories. If a given name appears in both
79 +upper and lower filesystems and refers to a non-directory in either,
80 +then the lower object is hidden - the name refers only to the upper
83 +Where both upper and lower objects are directories, a merged directory
86 +At mount time, the two directories given as mount options are combined
87 +into a merged directory:
89 + mount -t overlayfs overlayfs -olowerdir=/lower,upperdir=/upper /overlay
91 +Then whenever a lookup is requested in such a merged directory, the
92 +lookup is performed in each actual directory and the combined result
93 +is cached in the dentry belonging to the overlay filesystem. If both
94 +actual lookups find directories, both are stored and a merged
95 +directory is created, otherwise only one is stored: the upper if it
96 +exists, else the lower.
98 +Only the lists of names from directories are merged. Other content
99 +such as metadata and extended attributes are reported for the upper
100 +directory only. These attributes of the lower directory are hidden.
102 +whiteouts and opaque directories
103 +--------------------------------
105 +In order to support rm and rmdir without changing the lower
106 +filesystem, an overlay filesystem needs to record in the upper filesystem
107 +that files have been removed. This is done using whiteouts and opaque
108 +directories (non-directories are always opaque).
110 +The overlay filesystem uses extended attributes with a
111 +"trusted.overlay." prefix to record these details.
113 +A whiteout is created as a symbolic link with target
114 +"(overlay-whiteout)" and with xattr "trusted.overlay.whiteout" set to "y".
115 +When a whiteout is found in the upper level of a merged directory, any
116 +matching name in the lower level is ignored, and the whiteout itself
119 +A directory is made opaque by setting the xattr "trusted.overlay.opaque"
120 +to "y". Where the upper filesystem contains an opaque directory, any
121 +directory in the lower filesystem with the same name is ignored.
126 +When a 'readdir' request is made on a merged directory, the upper and
127 +lower directories are each read and the name lists merged in the
128 +obvious way (upper is read first, then lower - entries that already
129 +exist are not re-added). This merged name list is cached in the
130 +'struct file' and so remains as long as the file is kept open. If the
131 +directory is opened and read by two processes at the same time, they
132 +will each have separate caches. A seekdir to the start of the
133 +directory (offset 0) followed by a readdir will cause the cache to be
134 +discarded and rebuilt.
136 +This means that changes to the merged directory do not appear while a
137 +directory is being read. This is unlikely to be noticed by many
140 +seek offsets are assigned sequentially when the directories are read.
142 + - read part of a directory
143 + - remember an offset, and close the directory
144 + - re-open the directory some time later
145 + - seek to the remembered offset
147 +there may be little correlation between the old and new locations in
148 +the list of filenames, particularly if anything has changed in the
151 +Readdir on directories that are not merged is simply handled by the
152 +underlying directory (upper or lower).
158 +Objects that are not directories (files, symlinks, device-special
159 +files etc.) are presented either from the upper or lower filesystem as
160 +appropriate. When a file in the lower filesystem is accessed in a way
161 +the requires write-access, such as opening for write access, changing
162 +some metadata etc., the file is first copied from the lower filesystem
163 +to the upper filesystem (copy_up). Note that creating a hard-link
164 +also requires copy_up, though of course creation of a symlink does
167 +The copy_up may turn out to be unnecessary, for example if the file is
168 +opened for read-write but the data is not modified.
170 +The copy_up process first makes sure that the containing directory
171 +exists in the upper filesystem - creating it and any parents as
172 +necessary. It then creates the object with the same metadata (owner,
173 +mode, mtime, symlink-target etc.) and then if the object is a file, the
174 +data is copied from the lower to the upper filesystem. Finally any
175 +extended attributes are copied up.
177 +Once the copy_up is complete, the overlay filesystem simply
178 +provides direct access to the newly created file in the upper
179 +filesystem - future operations on the file are barely noticed by the
180 +overlay filesystem (though an operation on the name of the file such as
181 +rename or unlink will of course be noticed and handled).
184 +Non-standard behavior
185 +---------------------
187 +The copy_up operation essentially creates a new, identical file and
188 +moves it over to the old name. The new file may be on a different
189 +filesystem, so both st_dev and st_ino of the file may change.
191 +Any open files referring to this inode will access the old data and
192 +metadata. Similarly any file locks obtained before copy_up will not
193 +apply to the copied up file.
195 +On a file opened with O_RDONLY fchmod(2), fchown(2), futimesat(2) and
196 +fsetxattr(2) will fail with EROFS.
198 +If a file with multiple hard links is copied up, then this will
199 +"break" the link. Changes will not be propagated to other names
200 +referring to the same inode.
202 +Symlinks in /proc/PID/ and /proc/PID/fd which point to a non-directory
203 +object in overlayfs will not contain valid absolute paths, only
204 +relative paths leading up to the filesystem's root. This will be
205 +fixed in the future.
207 +Some operations are not atomic, for example a crash during copy_up or
208 +rename will leave the filesystem in an inconsistent state. This will
209 +be addressed in the future.
211 +Changes to underlying filesystems
212 +---------------------------------
214 +Offline changes, when the overlay is not mounted, are allowed to either
215 +the upper or the lower trees.
217 +Changes to the underlying filesystems while part of a mounted overlay
218 +filesystem are not allowed. If the underlying filesystem is changed,
219 +the behavior of the overlay is undefined, though it will not result in
220 +a crash or deadlock.
221 --- a/Documentation/filesystems/vfs.txt
222 +++ b/Documentation/filesystems/vfs.txt
223 @@ -362,6 +362,7 @@ struct inode_operations {
224 int (*atomic_open)(struct inode *, struct dentry *,
225 struct file *, unsigned open_flag,
226 umode_t create_mode, int *opened);
227 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
230 Again, all methods are called without any locks being held, unless
231 @@ -681,6 +682,12 @@ struct address_space_operations {
232 but instead uses bmap to find out where the blocks in the file
233 are and uses those addresses directly.
235 + dentry_open: this is an alternative to f_op->open(), the difference is that
236 + this method may open a file not necessarily originating from the same
237 + filesystem as the one i_op->open() was called on. It may be
238 + useful for stacking filesystems which want to allow native I/O directly
239 + on underlying files.
242 invalidatepage: If a page has PagePrivate set, then invalidatepage
243 will be called when part or all of the page is to be removed
246 @@ -6019,6 +6019,13 @@ F: drivers/scsi/osd/
247 F: include/scsi/osd_*
250 +OVERLAYFS FILESYSTEM
251 +M: Miklos Szeredi <miklos@szeredi.hu>
252 +L: linux-fsdevel@vger.kernel.org
255 +F: Documentation/filesystems/overlayfs.txt
258 M: Christian Lamparter <chunkeey@googlemail.com>
259 L: linux-wireless@vger.kernel.org
262 @@ -67,6 +67,7 @@ source "fs/quota/Kconfig"
264 source "fs/autofs4/Kconfig"
265 source "fs/fuse/Kconfig"
266 +source "fs/overlayfs/Kconfig"
272 @@ -105,6 +105,7 @@ obj-$(CONFIG_QNX6FS_FS) += qnx6/
273 obj-$(CONFIG_AUTOFS4_FS) += autofs4/
274 obj-$(CONFIG_ADFS_FS) += adfs/
275 obj-$(CONFIG_FUSE_FS) += fuse/
276 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
277 obj-$(CONFIG_UDF_FS) += udf/
278 obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
279 obj-$(CONFIG_OMFS_FS) += omfs/
280 --- a/fs/ecryptfs/main.c
281 +++ b/fs/ecryptfs/main.c
282 @@ -567,6 +567,13 @@ static struct dentry *ecryptfs_mount(str
283 s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
284 s->s_blocksize = path.dentry->d_sb->s_blocksize;
285 s->s_magic = ECRYPTFS_SUPER_MAGIC;
286 + s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
289 + if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
290 + pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
294 inode = ecryptfs_get_inode(path.dentry->d_inode, s);
298 @@ -42,11 +42,6 @@ static inline int __sync_blockdev(struct
299 extern void __init chrdev_init(void);
304 -extern int __inode_permission(struct inode *, int);
309 extern int copy_mount_options(const void __user *, unsigned long *);
310 @@ -132,12 +127,6 @@ extern struct dentry *__d_alloc(struct s
311 extern ssize_t __kernel_write(struct file *, const char *, size_t, loff_t *);
316 -extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
317 - loff_t *opos, size_t len, unsigned int flags);
322 extern const struct file_operations pipefifo_fops;
325 @@ -402,6 +402,7 @@ int __inode_permission(struct inode *ino
327 return security_inode_permission(inode, mask);
329 +EXPORT_SYMBOL(__inode_permission);
332 * sb_permission - Check superblock-level permissions
333 @@ -2868,9 +2869,12 @@ finish_open_created:
334 error = may_open(&nd->path, acc_mode, open_flag);
337 - file->f_path.mnt = nd->path.mnt;
338 - error = finish_open(file, nd->path.dentry, NULL, opened);
341 + BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
342 + error = vfs_open(&nd->path, file, current_cred());
344 + *opened |= FILE_OPENED;
346 if (error == -EOPENSTALE)
351 @@ -1442,6 +1442,33 @@ void drop_collected_mounts(struct vfsmou
356 + * clone_private_mount - create a private clone of a path
358 + * This creates a new vfsmount, which will be the clone of @path. The new will
359 + * not be attached anywhere in the namespace and will be private (i.e. changes
360 + * to the originating mount won't be propagated into this).
362 + * Release with mntput().
364 +struct vfsmount *clone_private_mount(struct path *path)
366 + struct mount *old_mnt = real_mount(path->mnt);
367 + struct mount *new_mnt;
369 + if (IS_MNT_UNBINDABLE(old_mnt))
370 + return ERR_PTR(-EINVAL);
372 + down_read(&namespace_sem);
373 + new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
374 + up_read(&namespace_sem);
375 + if (IS_ERR(new_mnt))
376 + return ERR_CAST(new_mnt);
378 + return &new_mnt->mnt;
380 +EXPORT_SYMBOL_GPL(clone_private_mount);
382 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
383 struct vfsmount *root)
387 @@ -800,8 +800,7 @@ struct file *dentry_open(const struct pa
388 f = get_empty_filp();
392 - error = do_dentry_open(f, NULL, cred);
393 + error = vfs_open(path, f, cred);
395 /* from now on we need fput() to dispose of f */
396 error = open_check_o_direct(f);
397 @@ -818,6 +817,26 @@ struct file *dentry_open(const struct pa
399 EXPORT_SYMBOL(dentry_open);
402 + * vfs_open - open the file at the given path
403 + * @path: path to open
404 + * @filp: newly allocated file with f_flag initialized
405 + * @cred: credentials to use
407 +int vfs_open(const struct path *path, struct file *filp,
408 + const struct cred *cred)
410 + struct inode *inode = path->dentry->d_inode;
412 + if (inode->i_op->dentry_open)
413 + return inode->i_op->dentry_open(path->dentry, filp, cred);
415 + filp->f_path = *path;
416 + return do_dentry_open(filp, NULL, cred);
419 +EXPORT_SYMBOL(vfs_open);
421 static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
423 int lookup_flags = 0;
425 +++ b/fs/overlayfs/Kconfig
428 + tristate "Overlay filesystem support"
430 + An overlay filesystem combines two filesystems - an 'upper' filesystem
431 + and a 'lower' filesystem. When a name exists in both filesystems, the
432 + object in the 'upper' filesystem is visible while the object in the
433 + 'lower' filesystem is either hidden or, in the case of directories,
434 + merged with the 'upper' object.
436 + For more information see Documentation/filesystems/overlayfs.txt
438 +++ b/fs/overlayfs/Makefile
441 +# Makefile for the overlay filesystem.
444 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
446 +overlayfs-objs := super.o inode.o dir.o readdir.o copy_up.o
448 +++ b/fs/overlayfs/copy_up.c
452 + * Copyright (C) 2011 Novell Inc.
454 + * This program is free software; you can redistribute it and/or modify it
455 + * under the terms of the GNU General Public License version 2 as published by
456 + * the Free Software Foundation.
459 +#include <linux/fs.h>
460 +#include <linux/slab.h>
461 +#include <linux/file.h>
462 +#include <linux/splice.h>
463 +#include <linux/xattr.h>
464 +#include <linux/security.h>
465 +#include <linux/uaccess.h>
466 +#include <linux/sched.h>
467 +#include "overlayfs.h"
469 +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
471 +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
473 + ssize_t list_size, size;
474 + char *buf, *name, *value;
477 + if (!old->d_inode->i_op->getxattr ||
478 + !new->d_inode->i_op->getxattr)
481 + list_size = vfs_listxattr(old, NULL, 0);
482 + if (list_size <= 0) {
483 + if (list_size == -EOPNOTSUPP)
488 + buf = kzalloc(list_size, GFP_KERNEL);
493 + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
497 + list_size = vfs_listxattr(old, buf, list_size);
498 + if (list_size <= 0) {
500 + goto out_free_value;
503 + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
504 + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
507 + goto out_free_value;
509 + error = vfs_setxattr(new, name, value, size, 0);
511 + goto out_free_value;
521 +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
523 + struct file *old_file;
524 + struct file *new_file;
525 + loff_t old_pos = 0;
526 + loff_t new_pos = 0;
532 + old_file = ovl_path_open(old, O_RDONLY);
533 + if (IS_ERR(old_file))
534 + return PTR_ERR(old_file);
536 + new_file = ovl_path_open(new, O_WRONLY);
537 + if (IS_ERR(new_file)) {
538 + error = PTR_ERR(new_file);
542 + /* FIXME: copy up sparse files efficiently */
544 + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
547 + if (len < this_len)
550 + if (signal_pending_state(TASK_KILLABLE, current)) {
555 + bytes = do_splice_direct(old_file, &old_pos,
556 + new_file, &new_pos,
557 + this_len, SPLICE_F_MOVE);
572 +static char *ovl_read_symlink(struct dentry *realdentry)
576 + struct inode *inode = realdentry->d_inode;
577 + mm_segment_t old_fs;
580 + if (!inode->i_op->readlink)
584 + buf = (char *) __get_free_page(GFP_KERNEL);
590 + /* The cast to a user pointer is valid due to the set_fs() */
591 + res = inode->i_op->readlink(realdentry,
592 + (char __user *)buf, PAGE_SIZE - 1);
595 + free_page((unsigned long) buf);
603 + return ERR_PTR(res);
606 +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
608 + struct iattr attr = {
610 + ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
611 + .ia_atime = stat->atime,
612 + .ia_mtime = stat->mtime,
615 + return notify_change(upperdentry, &attr);
618 +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
620 + struct iattr attr = {
621 + .ia_valid = ATTR_MODE,
625 + return notify_change(upperdentry, &attr);
628 +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
629 + struct path *lowerpath, struct kstat *stat,
633 + struct path newpath;
634 + umode_t mode = stat->mode;
636 + /* Can't properly set mode on creation because of the umask */
637 + stat->mode &= S_IFMT;
639 + ovl_path_upper(dentry, &newpath);
640 + WARN_ON(newpath.dentry);
641 + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
642 + if (IS_ERR(newpath.dentry))
643 + return PTR_ERR(newpath.dentry);
645 + if (S_ISREG(stat->mode)) {
646 + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
651 + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
655 + mutex_lock(&newpath.dentry->d_inode->i_mutex);
656 + if (!S_ISLNK(stat->mode))
657 + err = ovl_set_mode(newpath.dentry, mode);
659 + err = ovl_set_timestamps(newpath.dentry, stat);
660 + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
664 + ovl_dentry_update(dentry, newpath.dentry);
667 + * Easiest way to get rid of the lower dentry reference is to
668 + * drop this dentry. This is neither needed nor possible for
671 + if (!S_ISDIR(stat->mode))
677 + if (S_ISDIR(stat->mode))
678 + vfs_rmdir(upperdir->d_inode, newpath.dentry);
680 + vfs_unlink(upperdir->d_inode, newpath.dentry);
682 + dput(newpath.dentry);
688 + * Copy up a single dentry
690 + * Directory renames only allowed on "pure upper" (already created on
691 + * upper filesystem, never copied up). Directories which are on lower or
692 + * are merged may not be renamed. For these -EXDEV is returned and
693 + * userspace has to deal with it. This means, when copying up a
694 + * directory we can rely on it and ancestors being stable.
696 + * Non-directory renames start with copy up of source if necessary. The
697 + * actual rename will only proceed once the copy up was successful. Copy
698 + * up uses upper parent i_mutex for exclusion. Since rename can change
699 + * d_parent it is possible that the copy up will lock the old parent. At
700 + * that point the file will have already been copied up anyway.
702 +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
703 + struct path *lowerpath, struct kstat *stat)
706 + struct kstat pstat;
707 + struct path parentpath;
708 + struct dentry *upperdir;
709 + const struct cred *old_cred;
710 + struct cred *override_cred;
713 + ovl_path_upper(parent, &parentpath);
714 + upperdir = parentpath.dentry;
716 + err = vfs_getattr(&parentpath, &pstat);
720 + if (S_ISLNK(stat->mode)) {
721 + link = ovl_read_symlink(lowerpath->dentry);
723 + return PTR_ERR(link);
727 + override_cred = prepare_creds();
728 + if (!override_cred)
729 + goto out_free_link;
731 + override_cred->fsuid = stat->uid;
732 + override_cred->fsgid = stat->gid;
734 + * CAP_SYS_ADMIN for copying up extended attributes
735 + * CAP_DAC_OVERRIDE for create
736 + * CAP_FOWNER for chmod, timestamp update
737 + * CAP_FSETID for chmod
738 + * CAP_MKNOD for mknod
740 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
741 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
742 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
743 + cap_raise(override_cred->cap_effective, CAP_FSETID);
744 + cap_raise(override_cred->cap_effective, CAP_MKNOD);
745 + old_cred = override_creds(override_cred);
747 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
748 + if (ovl_path_type(dentry) != OVL_PATH_LOWER) {
751 + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
754 + /* Restore timestamps on parent (best effort) */
755 + ovl_set_timestamps(upperdir, &pstat);
759 + mutex_unlock(&upperdir->d_inode->i_mutex);
761 + revert_creds(old_cred);
762 + put_cred(override_cred);
766 + free_page((unsigned long) link);
771 +int ovl_copy_up(struct dentry *dentry)
777 + struct dentry *next;
778 + struct dentry *parent;
779 + struct path lowerpath;
781 + enum ovl_path_type type = ovl_path_type(dentry);
783 + if (type != OVL_PATH_LOWER)
786 + next = dget(dentry);
787 + /* find the topmost dentry not yet copied up */
789 + parent = dget_parent(next);
791 + type = ovl_path_type(parent);
792 + if (type != OVL_PATH_LOWER)
799 + ovl_path_lower(next, &lowerpath);
800 + err = vfs_getattr(&lowerpath, &stat);
802 + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
811 +/* Optimize by not copying up the file first and truncating later */
812 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
816 + struct path lowerpath;
817 + struct dentry *parent = dget_parent(dentry);
819 + err = ovl_copy_up(parent);
821 + goto out_dput_parent;
823 + ovl_path_lower(dentry, &lowerpath);
824 + err = vfs_getattr(&lowerpath, &stat);
826 + goto out_dput_parent;
828 + if (size < stat.size)
831 + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
838 +++ b/fs/overlayfs/dir.c
842 + * Copyright (C) 2011 Novell Inc.
844 + * This program is free software; you can redistribute it and/or modify it
845 + * under the terms of the GNU General Public License version 2 as published by
846 + * the Free Software Foundation.
849 +#include <linux/fs.h>
850 +#include <linux/namei.h>
851 +#include <linux/xattr.h>
852 +#include <linux/security.h>
853 +#include <linux/cred.h>
854 +#include "overlayfs.h"
856 +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
858 +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
861 + struct dentry *newdentry;
862 + const struct cred *old_cred;
863 + struct cred *override_cred;
865 + /* FIXME: recheck lower dentry to see if whiteout is really needed */
868 + override_cred = prepare_creds();
869 + if (!override_cred)
873 + * CAP_SYS_ADMIN for setxattr
874 + * CAP_DAC_OVERRIDE for symlink creation
875 + * CAP_FOWNER for unlink in sticky directory
877 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
878 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
879 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
880 + override_cred->fsuid = GLOBAL_ROOT_UID;
881 + override_cred->fsgid = GLOBAL_ROOT_GID;
882 + old_cred = override_creds(override_cred);
884 + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
885 + dentry->d_name.len);
886 + err = PTR_ERR(newdentry);
887 + if (IS_ERR(newdentry))
890 + /* Just been removed within the same locked region */
891 + WARN_ON(newdentry->d_inode);
893 + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
897 + ovl_dentry_version_inc(dentry->d_parent);
899 + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
901 + vfs_unlink(upperdir->d_inode, newdentry);
906 + revert_creds(old_cred);
907 + put_cred(override_cred);
911 + * There's no way to recover from failure to whiteout.
912 + * What should we do? Log a big fat error and... ?
914 + pr_err("overlayfs: ERROR - failed to whiteout '%s'\n",
915 + dentry->d_name.name);
921 +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
922 + struct dentry *template)
925 + struct dentry *newdentry;
926 + struct qstr *name = &template->d_name;
928 + newdentry = lookup_one_len(name->name, upperdir, name->len);
929 + if (IS_ERR(newdentry))
932 + if (newdentry->d_inode) {
933 + const struct cred *old_cred;
934 + struct cred *override_cred;
936 + /* No need to check whiteout if lower parent is non-existent */
938 + if (!ovl_dentry_lower(template->d_parent))
941 + if (!S_ISLNK(newdentry->d_inode->i_mode))
945 + override_cred = prepare_creds();
946 + if (!override_cred)
950 + * CAP_SYS_ADMIN for getxattr
951 + * CAP_FOWNER for unlink in sticky directory
953 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
954 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
955 + old_cred = override_creds(override_cred);
958 + if (ovl_is_whiteout(newdentry))
959 + err = vfs_unlink(upperdir->d_inode, newdentry);
961 + revert_creds(old_cred);
962 + put_cred(override_cred);
967 + newdentry = lookup_one_len(name->name, upperdir, name->len);
968 + if (IS_ERR(newdentry)) {
969 + ovl_whiteout(upperdir, template);
974 + * Whiteout just been successfully removed, parent
975 + * i_mutex is still held, there's no way the lookup
976 + * could return positive.
978 + WARN_ON(newdentry->d_inode);
985 + return ERR_PTR(err);
988 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
989 + struct kstat *stat, const char *link)
992 + struct dentry *newdentry;
993 + struct inode *dir = upperdir->d_inode;
995 + newdentry = ovl_lookup_create(upperdir, dentry);
996 + if (IS_ERR(newdentry))
999 + switch (stat->mode & S_IFMT) {
1001 + err = vfs_create(dir, newdentry, stat->mode, NULL);
1005 + err = vfs_mkdir(dir, newdentry, stat->mode);
1012 + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
1016 + err = vfs_symlink(dir, newdentry, link);
1023 + if (ovl_dentry_is_opaque(dentry))
1024 + ovl_whiteout(upperdir, dentry);
1026 + newdentry = ERR_PTR(err);
1027 + } else if (WARN_ON(!newdentry->d_inode)) {
1029 + * Not quite sure if non-instantiated dentry is legal or not.
1030 + * VFS doesn't seem to care so check and warn here.
1033 + newdentry = ERR_PTR(-ENOENT);
1041 +static int ovl_set_opaque(struct dentry *upperdentry)
1044 + const struct cred *old_cred;
1045 + struct cred *override_cred;
1047 + override_cred = prepare_creds();
1048 + if (!override_cred)
1051 + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
1052 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1053 + old_cred = override_creds(override_cred);
1054 + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
1055 + revert_creds(old_cred);
1056 + put_cred(override_cred);
1061 +static int ovl_remove_opaque(struct dentry *upperdentry)
1064 + const struct cred *old_cred;
1065 + struct cred *override_cred;
1067 + override_cred = prepare_creds();
1068 + if (!override_cred)
1071 + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
1072 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1073 + old_cred = override_creds(override_cred);
1074 + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
1075 + revert_creds(old_cred);
1076 + put_cred(override_cred);
1081 +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1082 + struct kstat *stat)
1085 + enum ovl_path_type type;
1086 + struct path realpath;
1088 + type = ovl_path_real(dentry, &realpath);
1089 + err = vfs_getattr(&realpath, stat);
1093 + stat->dev = dentry->d_sb->s_dev;
1094 + stat->ino = dentry->d_inode->i_ino;
1097 + * It's probably not worth it to count subdirs to get the
1098 + * correct link count. nlink=1 seems to pacify 'find' and
1099 + * other utilities.
1101 + if (type == OVL_PATH_MERGE)
1107 +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
1111 + struct dentry *newdentry;
1112 + struct dentry *upperdir;
1113 + struct inode *inode;
1114 + struct kstat stat = {
1120 + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
1124 + err = ovl_copy_up(dentry->d_parent);
1128 + upperdir = ovl_dentry_upper(dentry->d_parent);
1129 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1131 + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
1132 + err = PTR_ERR(newdentry);
1133 + if (IS_ERR(newdentry))
1136 + ovl_dentry_version_inc(dentry->d_parent);
1137 + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
1138 + err = ovl_set_opaque(newdentry);
1140 + vfs_rmdir(upperdir->d_inode, newdentry);
1141 + ovl_whiteout(upperdir, dentry);
1145 + ovl_dentry_update(dentry, newdentry);
1146 + ovl_copyattr(newdentry->d_inode, inode);
1147 + d_instantiate(dentry, inode);
1155 + mutex_unlock(&upperdir->d_inode->i_mutex);
1162 +static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1165 + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
1168 +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1170 + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
1173 +static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1176 + return ovl_create_object(dentry, mode, rdev, NULL);
1179 +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
1182 + return ovl_create_object(dentry, S_IFLNK, 0, link);
1185 +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
1188 + enum ovl_path_type type;
1189 + struct path realpath;
1190 + struct dentry *upperdir;
1192 + err = ovl_copy_up(dentry->d_parent);
1196 + upperdir = ovl_dentry_upper(dentry->d_parent);
1197 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1198 + type = ovl_path_real(dentry, &realpath);
1199 + if (type != OVL_PATH_LOWER) {
1201 + if (realpath.dentry->d_parent != upperdir)
1204 + /* FIXME: create whiteout up front and rename to target */
1207 + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
1209 + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
1213 + ovl_dentry_version_inc(dentry->d_parent);
1216 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1217 + err = ovl_whiteout(upperdir, dentry);
1220 + * Keeping this dentry hashed would mean having to release
1221 + * upperpath/lowerpath, which could only be done if we are the
1222 + * sole user of this dentry. Too tricky... Just unhash for
1227 + mutex_unlock(&upperdir->d_inode->i_mutex);
1232 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1234 + return ovl_do_remove(dentry, false);
1238 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
1241 + enum ovl_path_type type;
1243 + type = ovl_path_type(dentry);
1244 + if (type != OVL_PATH_UPPER) {
1245 + err = ovl_check_empty_and_clear(dentry, type);
1250 + return ovl_do_remove(dentry, true);
1253 +static int ovl_link(struct dentry *old, struct inode *newdir,
1254 + struct dentry *new)
1257 + struct dentry *olddentry;
1258 + struct dentry *newdentry;
1259 + struct dentry *upperdir;
1260 + struct inode *newinode;
1262 + err = ovl_copy_up(old);
1266 + err = ovl_copy_up(new->d_parent);
1270 + upperdir = ovl_dentry_upper(new->d_parent);
1271 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1272 + newdentry = ovl_lookup_create(upperdir, new);
1273 + err = PTR_ERR(newdentry);
1274 + if (IS_ERR(newdentry))
1277 + olddentry = ovl_dentry_upper(old);
1278 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
1280 + if (WARN_ON(!newdentry->d_inode)) {
1285 + newinode = ovl_new_inode(old->d_sb, newdentry->d_inode->i_mode,
1291 + ovl_copyattr(upperdir->d_inode, newinode);
1293 + ovl_dentry_version_inc(new->d_parent);
1294 + ovl_dentry_update(new, newdentry);
1296 + d_instantiate(new, newinode);
1299 + if (ovl_dentry_is_opaque(new))
1300 + ovl_whiteout(upperdir, new);
1304 + mutex_unlock(&upperdir->d_inode->i_mutex);
1309 +static int ovl_rename(struct inode *olddir, struct dentry *old,
1310 + struct inode *newdir, struct dentry *new)
1313 + enum ovl_path_type old_type;
1314 + enum ovl_path_type new_type;
1315 + struct dentry *old_upperdir;
1316 + struct dentry *new_upperdir;
1317 + struct dentry *olddentry;
1318 + struct dentry *newdentry;
1319 + struct dentry *trap;
1322 + bool new_create = false;
1323 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
1325 + /* Don't copy up directory trees */
1326 + old_type = ovl_path_type(old);
1327 + if (old_type != OVL_PATH_UPPER && is_dir)
1330 + if (new->d_inode) {
1331 + new_type = ovl_path_type(new);
1333 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
1334 + if (ovl_dentry_lower(old)->d_inode ==
1335 + ovl_dentry_lower(new)->d_inode)
1338 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
1339 + if (ovl_dentry_upper(old)->d_inode ==
1340 + ovl_dentry_upper(new)->d_inode)
1344 + if (new_type != OVL_PATH_UPPER &&
1345 + S_ISDIR(new->d_inode->i_mode)) {
1346 + err = ovl_check_empty_and_clear(new, new_type);
1351 + new_type = OVL_PATH_UPPER;
1354 + err = ovl_copy_up(old);
1358 + err = ovl_copy_up(new->d_parent);
1362 + old_upperdir = ovl_dentry_upper(old->d_parent);
1363 + new_upperdir = ovl_dentry_upper(new->d_parent);
1365 + trap = lock_rename(new_upperdir, old_upperdir);
1367 + olddentry = ovl_dentry_upper(old);
1368 + newdentry = ovl_dentry_upper(new);
1372 + new_create = true;
1373 + newdentry = ovl_lookup_create(new_upperdir, new);
1374 + err = PTR_ERR(newdentry);
1375 + if (IS_ERR(newdentry))
1380 + if (olddentry->d_parent != old_upperdir)
1382 + if (newdentry->d_parent != new_upperdir)
1384 + if (olddentry == trap)
1386 + if (newdentry == trap)
1389 + old_opaque = ovl_dentry_is_opaque(old);
1390 + new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
1392 + if (is_dir && !old_opaque && new_opaque) {
1393 + err = ovl_set_opaque(olddentry);
1398 + err = vfs_rename(old_upperdir->d_inode, olddentry,
1399 + new_upperdir->d_inode, newdentry);
1402 + if (new_create && ovl_dentry_is_opaque(new))
1403 + ovl_whiteout(new_upperdir, new);
1404 + if (is_dir && !old_opaque && new_opaque)
1405 + ovl_remove_opaque(olddentry);
1409 + if (old_type != OVL_PATH_UPPER || old_opaque)
1410 + err = ovl_whiteout(old_upperdir, old);
1411 + if (is_dir && old_opaque && !new_opaque)
1412 + ovl_remove_opaque(olddentry);
1414 + if (old_opaque != new_opaque)
1415 + ovl_dentry_set_opaque(old, new_opaque);
1417 + ovl_dentry_version_inc(old->d_parent);
1418 + ovl_dentry_version_inc(new->d_parent);
1423 + unlock_rename(new_upperdir, old_upperdir);
1427 +const struct inode_operations ovl_dir_inode_operations = {
1428 + .lookup = ovl_lookup,
1429 + .mkdir = ovl_mkdir,
1430 + .symlink = ovl_symlink,
1431 + .unlink = ovl_unlink,
1432 + .rmdir = ovl_rmdir,
1433 + .rename = ovl_rename,
1435 + .setattr = ovl_setattr,
1436 + .create = ovl_create,
1437 + .mknod = ovl_mknod,
1438 + .permission = ovl_permission,
1439 + .getattr = ovl_dir_getattr,
1440 + .setxattr = ovl_setxattr,
1441 + .getxattr = ovl_getxattr,
1442 + .listxattr = ovl_listxattr,
1443 + .removexattr = ovl_removexattr,
1446 +++ b/fs/overlayfs/inode.c
1450 + * Copyright (C) 2011 Novell Inc.
1452 + * This program is free software; you can redistribute it and/or modify it
1453 + * under the terms of the GNU General Public License version 2 as published by
1454 + * the Free Software Foundation.
1457 +#include <linux/fs.h>
1458 +#include <linux/slab.h>
1459 +#include <linux/xattr.h>
1460 +#include "overlayfs.h"
1462 +int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1464 + struct dentry *upperdentry;
1467 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1468 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1470 + err = ovl_copy_up(dentry);
1474 + upperdentry = ovl_dentry_upper(dentry);
1476 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1477 + attr->ia_valid &= ~ATTR_MODE;
1479 + mutex_lock(&upperdentry->d_inode->i_mutex);
1480 + err = notify_change(upperdentry, attr);
1482 + ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
1483 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1488 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1489 + struct kstat *stat)
1491 + struct path realpath;
1493 + ovl_path_real(dentry, &realpath);
1494 + return vfs_getattr(&realpath, stat);
1497 +int ovl_permission(struct inode *inode, int mask)
1499 + struct ovl_entry *oe;
1500 + struct dentry *alias = NULL;
1501 + struct inode *realinode;
1502 + struct dentry *realdentry;
1506 + if (S_ISDIR(inode->i_mode)) {
1507 + oe = inode->i_private;
1508 + } else if (mask & MAY_NOT_BLOCK) {
1512 + * For non-directories find an alias and get the info
1515 + alias = d_find_any_alias(inode);
1516 + if (WARN_ON(!alias))
1519 + oe = alias->d_fsdata;
1522 + realdentry = ovl_entry_real(oe, &is_upper);
1524 + /* Careful in RCU walk mode */
1525 + realinode = ACCESS_ONCE(realdentry->d_inode);
1527 + WARN_ON(!(mask & MAY_NOT_BLOCK));
1532 + if (mask & MAY_WRITE) {
1533 + umode_t mode = realinode->i_mode;
1536 + * Writes will always be redirected to upper layer, so
1537 + * ignore lower layer being read-only.
1539 + * If the overlay itself is read-only then proceed
1540 + * with the permission check, don't return EROFS.
1541 + * This will only happen if this is the lower layer of
1542 + * another overlayfs.
1544 + * If upper fs becomes read-only after the overlay was
1545 + * constructed return EROFS to prevent modification of
1549 + if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
1550 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1554 + err = __inode_permission(realinode, mask);
1561 +struct ovl_link_data {
1562 + struct dentry *realdentry;
1566 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1569 + struct dentry *realdentry;
1570 + struct inode *realinode;
1572 + realdentry = ovl_dentry_real(dentry);
1573 + realinode = realdentry->d_inode;
1575 + if (WARN_ON(!realinode->i_op->follow_link))
1576 + return ERR_PTR(-EPERM);
1578 + ret = realinode->i_op->follow_link(realdentry, nd);
1582 + if (realinode->i_op->put_link) {
1583 + struct ovl_link_data *data;
1585 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1587 + realinode->i_op->put_link(realdentry, nd, ret);
1588 + return ERR_PTR(-ENOMEM);
1590 + data->realdentry = realdentry;
1591 + data->cookie = ret;
1599 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1601 + struct inode *realinode;
1602 + struct ovl_link_data *data = c;
1607 + realinode = data->realdentry->d_inode;
1608 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1612 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1614 + struct path realpath;
1615 + struct inode *realinode;
1617 + ovl_path_real(dentry, &realpath);
1618 + realinode = realpath.dentry->d_inode;
1620 + if (!realinode->i_op->readlink)
1623 + touch_atime(&realpath);
1625 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1629 +static bool ovl_is_private_xattr(const char *name)
1631 + return strncmp(name, "trusted.overlay.", 14) == 0;
1634 +int ovl_setxattr(struct dentry *dentry, const char *name,
1635 + const void *value, size_t size, int flags)
1638 + struct dentry *upperdentry;
1640 + if (ovl_is_private_xattr(name))
1643 + err = ovl_copy_up(dentry);
1647 + upperdentry = ovl_dentry_upper(dentry);
1648 + return vfs_setxattr(upperdentry, name, value, size, flags);
1651 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1652 + void *value, size_t size)
1654 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1655 + ovl_is_private_xattr(name))
1658 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
1661 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
1666 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
1667 + if (res <= 0 || size == 0)
1670 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
1673 + /* filter out private xattrs */
1674 + for (off = 0; off < res;) {
1675 + char *s = list + off;
1676 + size_t slen = strlen(s) + 1;
1678 + BUG_ON(off + slen > res);
1680 + if (ovl_is_private_xattr(s)) {
1682 + memmove(s, s + slen, res - off);
1691 +int ovl_removexattr(struct dentry *dentry, const char *name)
1694 + struct path realpath;
1695 + enum ovl_path_type type;
1697 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1698 + ovl_is_private_xattr(name))
1701 + type = ovl_path_real(dentry, &realpath);
1702 + if (type == OVL_PATH_LOWER) {
1703 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
1707 + err = ovl_copy_up(dentry);
1711 + ovl_path_upper(dentry, &realpath);
1714 + return vfs_removexattr(realpath.dentry, name);
1717 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
1718 + struct dentry *realdentry)
1720 + if (type != OVL_PATH_LOWER)
1723 + if (special_file(realdentry->d_inode->i_mode))
1726 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
1732 +static int ovl_dentry_open(struct dentry *dentry, struct file *file,
1733 + const struct cred *cred)
1736 + struct path realpath;
1737 + enum ovl_path_type type;
1739 + type = ovl_path_real(dentry, &realpath);
1740 + if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
1741 + if (file->f_flags & O_TRUNC)
1742 + err = ovl_copy_up_truncate(dentry, 0);
1744 + err = ovl_copy_up(dentry);
1748 + ovl_path_upper(dentry, &realpath);
1751 + return vfs_open(&realpath, file, cred);
1754 +static const struct inode_operations ovl_file_inode_operations = {
1755 + .setattr = ovl_setattr,
1756 + .permission = ovl_permission,
1757 + .getattr = ovl_getattr,
1758 + .setxattr = ovl_setxattr,
1759 + .getxattr = ovl_getxattr,
1760 + .listxattr = ovl_listxattr,
1761 + .removexattr = ovl_removexattr,
1762 + .dentry_open = ovl_dentry_open,
1765 +static const struct inode_operations ovl_symlink_inode_operations = {
1766 + .setattr = ovl_setattr,
1767 + .follow_link = ovl_follow_link,
1768 + .put_link = ovl_put_link,
1769 + .readlink = ovl_readlink,
1770 + .getattr = ovl_getattr,
1771 + .setxattr = ovl_setxattr,
1772 + .getxattr = ovl_getxattr,
1773 + .listxattr = ovl_listxattr,
1774 + .removexattr = ovl_removexattr,
1777 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1778 + struct ovl_entry *oe)
1780 + struct inode *inode;
1782 + inode = new_inode(sb);
1788 + inode->i_ino = get_next_ino();
1789 + inode->i_mode = mode;
1790 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
1794 + inode->i_private = oe;
1795 + inode->i_op = &ovl_dir_inode_operations;
1796 + inode->i_fop = &ovl_dir_operations;
1800 + inode->i_op = &ovl_symlink_inode_operations;
1808 + inode->i_op = &ovl_file_inode_operations;
1812 + WARN(1, "illegal file type: %i\n", mode);
1821 +++ b/fs/overlayfs/overlayfs.h
1825 + * Copyright (C) 2011 Novell Inc.
1827 + * This program is free software; you can redistribute it and/or modify it
1828 + * under the terms of the GNU General Public License version 2 as published by
1829 + * the Free Software Foundation.
1834 +enum ovl_path_type {
1840 +extern const char *ovl_opaque_xattr;
1841 +extern const char *ovl_whiteout_xattr;
1842 +extern const struct dentry_operations ovl_dentry_operations;
1844 +enum ovl_path_type ovl_path_type(struct dentry *dentry);
1845 +u64 ovl_dentry_version_get(struct dentry *dentry);
1846 +void ovl_dentry_version_inc(struct dentry *dentry);
1847 +void ovl_path_upper(struct dentry *dentry, struct path *path);
1848 +void ovl_path_lower(struct dentry *dentry, struct path *path);
1849 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1850 +struct dentry *ovl_dentry_upper(struct dentry *dentry);
1851 +struct dentry *ovl_dentry_lower(struct dentry *dentry);
1852 +struct dentry *ovl_dentry_real(struct dentry *dentry);
1853 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
1854 +bool ovl_dentry_is_opaque(struct dentry *dentry);
1855 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
1856 +bool ovl_is_whiteout(struct dentry *dentry);
1857 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
1858 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
1859 + unsigned int flags);
1860 +struct file *ovl_path_open(struct path *path, int flags);
1862 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
1863 + struct kstat *stat, const char *link);
1866 +extern const struct file_operations ovl_dir_operations;
1867 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
1870 +int ovl_setattr(struct dentry *dentry, struct iattr *attr);
1871 +int ovl_permission(struct inode *inode, int mask);
1872 +int ovl_setxattr(struct dentry *dentry, const char *name,
1873 + const void *value, size_t size, int flags);
1874 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1875 + void *value, size_t size);
1876 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
1877 +int ovl_removexattr(struct dentry *dentry, const char *name);
1879 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1880 + struct ovl_entry *oe);
1881 +static inline void ovl_copyattr(struct inode *from, struct inode *to)
1883 + to->i_uid = from->i_uid;
1884 + to->i_gid = from->i_gid;
1888 +extern const struct inode_operations ovl_dir_inode_operations;
1891 +int ovl_copy_up(struct dentry *dentry);
1892 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
1894 +++ b/fs/overlayfs/readdir.c
1898 + * Copyright (C) 2011 Novell Inc.
1900 + * This program is free software; you can redistribute it and/or modify it
1901 + * under the terms of the GNU General Public License version 2 as published by
1902 + * the Free Software Foundation.
1905 +#include <linux/fs.h>
1906 +#include <linux/slab.h>
1907 +#include <linux/namei.h>
1908 +#include <linux/file.h>
1909 +#include <linux/xattr.h>
1910 +#include <linux/rbtree.h>
1911 +#include <linux/security.h>
1912 +#include <linux/cred.h>
1913 +#include "overlayfs.h"
1915 +struct ovl_cache_entry {
1918 + unsigned int type;
1921 + struct list_head l_node;
1922 + struct rb_node node;
1925 +struct ovl_readdir_data {
1926 + struct rb_root *root;
1927 + struct list_head *list;
1928 + struct list_head *middle;
1929 + struct dentry *dir;
1934 +struct ovl_dir_file {
1937 + struct list_head cursor;
1938 + u64 cache_version;
1939 + struct list_head cache;
1940 + struct file *realfile;
1943 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
1945 + return container_of(n, struct ovl_cache_entry, node);
1948 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
1949 + const char *name, int len)
1951 + struct rb_node *node = root->rb_node;
1955 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
1957 + cmp = strncmp(name, p->name, len);
1959 + node = p->node.rb_right;
1960 + else if (cmp < 0 || len < p->len)
1961 + node = p->node.rb_left;
1969 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
1970 + u64 ino, unsigned int d_type)
1972 + struct ovl_cache_entry *p;
1974 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
1976 + char *name_copy = (char *) (p + 1);
1977 + memcpy(name_copy, name, len);
1978 + name_copy[len] = '\0';
1979 + p->name = name_copy;
1983 + p->is_whiteout = false;
1989 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
1990 + const char *name, int len, u64 ino,
1991 + unsigned int d_type)
1993 + struct rb_node **newp = &rdd->root->rb_node;
1994 + struct rb_node *parent = NULL;
1995 + struct ovl_cache_entry *p;
1999 + struct ovl_cache_entry *tmp;
2002 + tmp = ovl_cache_entry_from_node(*newp);
2003 + cmp = strncmp(name, tmp->name, len);
2005 + newp = &tmp->node.rb_right;
2006 + else if (cmp < 0 || len < tmp->len)
2007 + newp = &tmp->node.rb_left;
2012 + p = ovl_cache_entry_new(name, len, ino, d_type);
2016 + list_add_tail(&p->l_node, rdd->list);
2017 + rb_link_node(&p->node, parent, newp);
2018 + rb_insert_color(&p->node, rdd->root);
2023 +static int ovl_fill_lower(void *buf, const char *name, int namelen,
2024 + loff_t offset, u64 ino, unsigned int d_type)
2026 + struct ovl_readdir_data *rdd = buf;
2027 + struct ovl_cache_entry *p;
2030 + p = ovl_cache_entry_find(rdd->root, name, namelen);
2032 + list_move_tail(&p->l_node, rdd->middle);
2034 + p = ovl_cache_entry_new(name, namelen, ino, d_type);
2036 + rdd->err = -ENOMEM;
2038 + list_add_tail(&p->l_node, rdd->middle);
2044 +static void ovl_cache_free(struct list_head *list)
2046 + struct ovl_cache_entry *p;
2047 + struct ovl_cache_entry *n;
2049 + list_for_each_entry_safe(p, n, list, l_node)
2052 + INIT_LIST_HEAD(list);
2055 +static int ovl_fill_upper(void *buf, const char *name, int namelen,
2056 + loff_t offset, u64 ino, unsigned int d_type)
2058 + struct ovl_readdir_data *rdd = buf;
2061 + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
2064 +static inline int ovl_dir_read(struct path *realpath,
2065 + struct ovl_readdir_data *rdd, filldir_t filler)
2067 + struct file *realfile;
2070 + realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
2071 + if (IS_ERR(realfile))
2072 + return PTR_ERR(realfile);
2077 + err = vfs_readdir(realfile, filler, rdd);
2080 + } while (!err && rdd->count);
2086 +static void ovl_dir_reset(struct file *file)
2088 + struct ovl_dir_file *od = file->private_data;
2089 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
2091 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
2092 + list_del_init(&od->cursor);
2093 + ovl_cache_free(&od->cache);
2094 + od->is_cached = false;
2096 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
2097 + if (od->is_real && type == OVL_PATH_MERGE) {
2098 + fput(od->realfile);
2099 + od->realfile = NULL;
2100 + od->is_real = false;
2104 +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
2106 + struct ovl_cache_entry *p;
2107 + struct dentry *dentry;
2108 + const struct cred *old_cred;
2109 + struct cred *override_cred;
2111 + override_cred = prepare_creds();
2112 + if (!override_cred) {
2113 + ovl_cache_free(rdd->list);
2118 + * CAP_SYS_ADMIN for getxattr
2119 + * CAP_DAC_OVERRIDE for lookup
2121 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2122 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2123 + old_cred = override_creds(override_cred);
2125 + mutex_lock(&rdd->dir->d_inode->i_mutex);
2126 + list_for_each_entry(p, rdd->list, l_node) {
2127 + if (p->type != DT_LNK)
2130 + dentry = lookup_one_len(p->name, rdd->dir, p->len);
2131 + if (IS_ERR(dentry))
2134 + p->is_whiteout = ovl_is_whiteout(dentry);
2137 + mutex_unlock(&rdd->dir->d_inode->i_mutex);
2139 + revert_creds(old_cred);
2140 + put_cred(override_cred);
2145 +static inline int ovl_dir_read_merged(struct path *upperpath,
2146 + struct path *lowerpath,
2147 + struct ovl_readdir_data *rdd)
2150 + struct rb_root root = RB_ROOT;
2151 + struct list_head middle;
2153 + rdd->root = &root;
2154 + if (upperpath->dentry) {
2155 + rdd->dir = upperpath->dentry;
2156 + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
2160 + err = ovl_dir_mark_whiteouts(rdd);
2165 + * Insert lowerpath entries before upperpath ones, this allows
2166 + * offsets to be reasonably constant
2168 + list_add(&middle, rdd->list);
2169 + rdd->middle = &middle;
2170 + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower);
2171 + list_del(&middle);
2178 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
2180 + struct list_head *l;
2183 + l = od->cache.next;
2184 + for (off = 0; off < pos; off++) {
2185 + if (l == &od->cache)
2189 + list_move_tail(&od->cursor, l);
2192 +static int ovl_readdir(struct file *file, void *buf, filldir_t filler)
2194 + struct ovl_dir_file *od = file->private_data;
2198 + ovl_dir_reset(file);
2200 + if (od->is_real) {
2201 + res = vfs_readdir(od->realfile, filler, buf);
2202 + file->f_pos = od->realfile->f_pos;
2207 + if (!od->is_cached) {
2208 + struct path lowerpath;
2209 + struct path upperpath;
2210 + struct ovl_readdir_data rdd = { .list = &od->cache };
2212 + ovl_path_lower(file->f_path.dentry, &lowerpath);
2213 + ovl_path_upper(file->f_path.dentry, &upperpath);
2215 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2217 + ovl_cache_free(rdd.list);
2221 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
2222 + od->is_cached = true;
2224 + ovl_seek_cursor(od, file->f_pos);
2227 + while (od->cursor.next != &od->cache) {
2230 + struct ovl_cache_entry *p;
2232 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
2233 + off = file->f_pos;
2234 + if (!p->is_whiteout) {
2235 + over = filler(buf, p->name, p->len, off, p->ino,
2241 + list_move(&od->cursor, &p->l_node);
2247 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
2250 + struct ovl_dir_file *od = file->private_data;
2252 + mutex_lock(&file_inode(file)->i_mutex);
2254 + ovl_dir_reset(file);
2256 + if (od->is_real) {
2257 + res = vfs_llseek(od->realfile, offset, origin);
2258 + file->f_pos = od->realfile->f_pos;
2264 + offset += file->f_pos;
2274 + if (offset != file->f_pos) {
2275 + file->f_pos = offset;
2276 + if (od->is_cached)
2277 + ovl_seek_cursor(od, offset);
2282 + mutex_unlock(&file_inode(file)->i_mutex);
2287 +static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
2290 + struct ovl_dir_file *od = file->private_data;
2292 + /* May need to reopen directory if it got copied up */
2293 + if (!od->realfile) {
2294 + struct path upperpath;
2296 + ovl_path_upper(file->f_path.dentry, &upperpath);
2297 + od->realfile = ovl_path_open(&upperpath, O_RDONLY);
2298 + if (IS_ERR(od->realfile))
2299 + return PTR_ERR(od->realfile);
2302 + return vfs_fsync_range(od->realfile, start, end, datasync);
2305 +static int ovl_dir_release(struct inode *inode, struct file *file)
2307 + struct ovl_dir_file *od = file->private_data;
2309 + list_del(&od->cursor);
2310 + ovl_cache_free(&od->cache);
2312 + fput(od->realfile);
2318 +static int ovl_dir_open(struct inode *inode, struct file *file)
2320 + struct path realpath;
2321 + struct file *realfile;
2322 + struct ovl_dir_file *od;
2323 + enum ovl_path_type type;
2325 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
2329 + type = ovl_path_real(file->f_path.dentry, &realpath);
2330 + realfile = ovl_path_open(&realpath, file->f_flags);
2331 + if (IS_ERR(realfile)) {
2333 + return PTR_ERR(realfile);
2335 + INIT_LIST_HEAD(&od->cache);
2336 + INIT_LIST_HEAD(&od->cursor);
2337 + od->is_cached = false;
2338 + od->realfile = realfile;
2339 + od->is_real = (type != OVL_PATH_MERGE);
2340 + file->private_data = od;
2345 +const struct file_operations ovl_dir_operations = {
2346 + .read = generic_read_dir,
2347 + .open = ovl_dir_open,
2348 + .readdir = ovl_readdir,
2349 + .llseek = ovl_dir_llseek,
2350 + .fsync = ovl_dir_fsync,
2351 + .release = ovl_dir_release,
2354 +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
2357 + struct path lowerpath;
2358 + struct path upperpath;
2359 + struct ovl_cache_entry *p;
2360 + struct ovl_readdir_data rdd = { .list = list };
2362 + ovl_path_upper(dentry, &upperpath);
2363 + ovl_path_lower(dentry, &lowerpath);
2365 + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2371 + list_for_each_entry(p, list, l_node) {
2372 + if (p->is_whiteout)
2375 + if (p->name[0] == '.') {
2378 + if (p->len == 2 && p->name[1] == '.')
2388 +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
2390 + struct path upperpath;
2391 + struct dentry *upperdir;
2392 + struct ovl_cache_entry *p;
2393 + const struct cred *old_cred;
2394 + struct cred *override_cred;
2397 + ovl_path_upper(dir, &upperpath);
2398 + upperdir = upperpath.dentry;
2400 + override_cred = prepare_creds();
2401 + if (!override_cred)
2405 + * CAP_DAC_OVERRIDE for lookup and unlink
2406 + * CAP_SYS_ADMIN for setxattr of "trusted" namespace
2407 + * CAP_FOWNER for unlink in sticky directory
2409 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2410 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2411 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
2412 + old_cred = override_creds(override_cred);
2414 + err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
2416 + goto out_revert_creds;
2418 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2419 + list_for_each_entry(p, list, l_node) {
2420 + struct dentry *dentry;
2423 + if (!p->is_whiteout)
2426 + dentry = lookup_one_len(p->name, upperdir, p->len);
2427 + if (IS_ERR(dentry)) {
2429 + "overlayfs: failed to lookup whiteout %.*s: %li\n",
2430 + p->len, p->name, PTR_ERR(dentry));
2433 + ret = vfs_unlink(upperdir->d_inode, dentry);
2437 + "overlayfs: failed to unlink whiteout %.*s: %i\n",
2438 + p->len, p->name, ret);
2440 + mutex_unlock(&upperdir->d_inode->i_mutex);
2443 + revert_creds(old_cred);
2444 + put_cred(override_cred);
2449 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
2454 + err = ovl_check_empty_dir(dentry, &list);
2455 + if (!err && type == OVL_PATH_MERGE)
2456 + err = ovl_remove_whiteouts(dentry, &list);
2458 + ovl_cache_free(&list);
2463 +++ b/fs/overlayfs/super.c
2467 + * Copyright (C) 2011 Novell Inc.
2469 + * This program is free software; you can redistribute it and/or modify it
2470 + * under the terms of the GNU General Public License version 2 as published by
2471 + * the Free Software Foundation.
2474 +#include <linux/fs.h>
2475 +#include <linux/namei.h>
2476 +#include <linux/xattr.h>
2477 +#include <linux/security.h>
2478 +#include <linux/mount.h>
2479 +#include <linux/slab.h>
2480 +#include <linux/parser.h>
2481 +#include <linux/module.h>
2482 +#include <linux/cred.h>
2483 +#include <linux/sched.h>
2484 +#include <linux/statfs.h>
2485 +#include <linux/seq_file.h>
2486 +#include "overlayfs.h"
2488 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2489 +MODULE_DESCRIPTION("Overlay filesystem");
2490 +MODULE_LICENSE("GPL");
2492 +#define OVERLAYFS_SUPER_MAGIC 0x794c764f
2494 +struct ovl_config {
2499 +/* private information held for overlayfs's superblock */
2501 + struct vfsmount *upper_mnt;
2502 + struct vfsmount *lower_mnt;
2503 + long lower_namelen;
2504 + /* pathnames of lower and upper dirs, for show_options */
2505 + struct ovl_config config;
2508 +/* private information held for every overlayfs dentry */
2511 + * Keep "double reference" on upper dentries, so that
2512 + * d_delete() doesn't think it's OK to reset d_inode to NULL.
2514 + struct dentry *__upperdentry;
2515 + struct dentry *lowerdentry;
2521 + struct rcu_head rcu;
2525 +const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
2526 +const char *ovl_opaque_xattr = "trusted.overlay.opaque";
2529 +enum ovl_path_type ovl_path_type(struct dentry *dentry)
2531 + struct ovl_entry *oe = dentry->d_fsdata;
2533 + if (oe->__upperdentry) {
2534 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
2535 + return OVL_PATH_MERGE;
2537 + return OVL_PATH_UPPER;
2539 + return OVL_PATH_LOWER;
2543 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
2545 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
2546 + smp_read_barrier_depends();
2547 + return upperdentry;
2550 +void ovl_path_upper(struct dentry *dentry, struct path *path)
2552 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2553 + struct ovl_entry *oe = dentry->d_fsdata;
2555 + path->mnt = ofs->upper_mnt;
2556 + path->dentry = ovl_upperdentry_dereference(oe);
2559 +void ovl_path_lower(struct dentry *dentry, struct path *path)
2561 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2562 + struct ovl_entry *oe = dentry->d_fsdata;
2564 + path->mnt = ofs->lower_mnt;
2565 + path->dentry = oe->lowerdentry;
2568 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
2571 + enum ovl_path_type type = ovl_path_type(dentry);
2573 + if (type == OVL_PATH_LOWER)
2574 + ovl_path_lower(dentry, path);
2576 + ovl_path_upper(dentry, path);
2581 +struct dentry *ovl_dentry_upper(struct dentry *dentry)
2583 + struct ovl_entry *oe = dentry->d_fsdata;
2585 + return ovl_upperdentry_dereference(oe);
2588 +struct dentry *ovl_dentry_lower(struct dentry *dentry)
2590 + struct ovl_entry *oe = dentry->d_fsdata;
2592 + return oe->lowerdentry;
2595 +struct dentry *ovl_dentry_real(struct dentry *dentry)
2597 + struct ovl_entry *oe = dentry->d_fsdata;
2598 + struct dentry *realdentry;
2600 + realdentry = ovl_upperdentry_dereference(oe);
2602 + realdentry = oe->lowerdentry;
2604 + return realdentry;
2607 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
2609 + struct dentry *realdentry;
2611 + realdentry = ovl_upperdentry_dereference(oe);
2615 + realdentry = oe->lowerdentry;
2616 + *is_upper = false;
2618 + return realdentry;
2621 +bool ovl_dentry_is_opaque(struct dentry *dentry)
2623 + struct ovl_entry *oe = dentry->d_fsdata;
2624 + return oe->opaque;
2627 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
2629 + struct ovl_entry *oe = dentry->d_fsdata;
2630 + oe->opaque = opaque;
2633 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
2635 + struct ovl_entry *oe = dentry->d_fsdata;
2637 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
2638 + WARN_ON(oe->__upperdentry);
2639 + BUG_ON(!upperdentry->d_inode);
2641 + oe->__upperdentry = dget(upperdentry);
2644 +void ovl_dentry_version_inc(struct dentry *dentry)
2646 + struct ovl_entry *oe = dentry->d_fsdata;
2648 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2652 +u64 ovl_dentry_version_get(struct dentry *dentry)
2654 + struct ovl_entry *oe = dentry->d_fsdata;
2656 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2657 + return oe->version;
2660 +bool ovl_is_whiteout(struct dentry *dentry)
2667 + if (!dentry->d_inode)
2669 + if (!S_ISLNK(dentry->d_inode->i_mode))
2672 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
2673 + if (res == 1 && val == 'y')
2679 +static bool ovl_is_opaquedir(struct dentry *dentry)
2684 + if (!S_ISDIR(dentry->d_inode->i_mode))
2687 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
2688 + if (res == 1 && val == 'y')
2694 +static void ovl_entry_free(struct rcu_head *head)
2696 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
2700 +static void ovl_dentry_release(struct dentry *dentry)
2702 + struct ovl_entry *oe = dentry->d_fsdata;
2705 + dput(oe->__upperdentry);
2706 + dput(oe->__upperdentry);
2707 + dput(oe->lowerdentry);
2708 + call_rcu(&oe->rcu, ovl_entry_free);
2712 +const struct dentry_operations ovl_dentry_operations = {
2713 + .d_release = ovl_dentry_release,
2716 +static struct ovl_entry *ovl_alloc_entry(void)
2718 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
2721 +static inline struct dentry *ovl_lookup_real(struct dentry *dir,
2722 + struct qstr *name)
2724 + struct dentry *dentry;
2726 + mutex_lock(&dir->d_inode->i_mutex);
2727 + dentry = lookup_one_len(name->name, dir, name->len);
2728 + mutex_unlock(&dir->d_inode->i_mutex);
2730 + if (IS_ERR(dentry)) {
2731 + if (PTR_ERR(dentry) == -ENOENT)
2733 + } else if (!dentry->d_inode) {
2740 +static int ovl_do_lookup(struct dentry *dentry)
2742 + struct ovl_entry *oe;
2743 + struct dentry *upperdir;
2744 + struct dentry *lowerdir;
2745 + struct dentry *upperdentry = NULL;
2746 + struct dentry *lowerdentry = NULL;
2747 + struct inode *inode = NULL;
2751 + oe = ovl_alloc_entry();
2755 + upperdir = ovl_dentry_upper(dentry->d_parent);
2756 + lowerdir = ovl_dentry_lower(dentry->d_parent);
2759 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
2760 + err = PTR_ERR(upperdentry);
2761 + if (IS_ERR(upperdentry))
2764 + if (lowerdir && upperdentry &&
2765 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
2766 + S_ISDIR(upperdentry->d_inode->i_mode))) {
2767 + const struct cred *old_cred;
2768 + struct cred *override_cred;
2771 + override_cred = prepare_creds();
2772 + if (!override_cred)
2773 + goto out_dput_upper;
2775 + /* CAP_SYS_ADMIN needed for getxattr */
2776 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2777 + old_cred = override_creds(override_cred);
2779 + if (ovl_is_opaquedir(upperdentry)) {
2780 + oe->opaque = true;
2781 + } else if (ovl_is_whiteout(upperdentry)) {
2782 + dput(upperdentry);
2783 + upperdentry = NULL;
2784 + oe->opaque = true;
2786 + revert_creds(old_cred);
2787 + put_cred(override_cred);
2790 + if (lowerdir && !oe->opaque) {
2791 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
2792 + err = PTR_ERR(lowerdentry);
2793 + if (IS_ERR(lowerdentry))
2794 + goto out_dput_upper;
2797 + if (lowerdentry && upperdentry &&
2798 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
2799 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
2800 + dput(lowerdentry);
2801 + lowerdentry = NULL;
2802 + oe->opaque = true;
2805 + if (lowerdentry || upperdentry) {
2806 + struct dentry *realdentry;
2808 + realdentry = upperdentry ? upperdentry : lowerdentry;
2810 + inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
2814 + ovl_copyattr(realdentry->d_inode, inode);
2818 + oe->__upperdentry = dget(upperdentry);
2821 + oe->lowerdentry = lowerdentry;
2823 + dentry->d_fsdata = oe;
2824 + dentry->d_op = &ovl_dentry_operations;
2825 + d_add(dentry, inode);
2830 + dput(lowerdentry);
2832 + dput(upperdentry);
2839 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
2840 + unsigned int flags)
2842 + int err = ovl_do_lookup(dentry);
2845 + return ERR_PTR(err);
2850 +struct file *ovl_path_open(struct path *path, int flags)
2852 + return dentry_open(path, flags, current_cred());
2855 +static void ovl_put_super(struct super_block *sb)
2857 + struct ovl_fs *ufs = sb->s_fs_info;
2859 + if (!(sb->s_flags & MS_RDONLY))
2860 + mnt_drop_write(ufs->upper_mnt);
2862 + mntput(ufs->upper_mnt);
2863 + mntput(ufs->lower_mnt);
2865 + kfree(ufs->config.lowerdir);
2866 + kfree(ufs->config.upperdir);
2870 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2872 + int flags = *flagsp;
2873 + struct ovl_fs *ufs = sb->s_fs_info;
2875 + /* When remounting rw or ro, we need to adjust the write access to the
2878 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2879 + /* No change to readonly status */
2882 + if (flags & MS_RDONLY) {
2883 + mnt_drop_write(ufs->upper_mnt);
2886 + return mnt_want_write(ufs->upper_mnt);
2891 + * @sb: The overlayfs super block
2892 + * @buf: The struct kstatfs to fill in with stats
2894 + * Get the filesystem statistics. As writes always target the upper layer
2895 + * filesystem pass the statfs to the same filesystem.
2897 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2899 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2900 + struct dentry *root_dentry = dentry->d_sb->s_root;
2904 + ovl_path_upper(root_dentry, &path);
2906 + err = vfs_statfs(&path, buf);
2908 + buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
2909 + buf->f_type = OVERLAYFS_SUPER_MAGIC;
2916 + * ovl_show_options
2918 + * Prints the mount options for a given superblock.
2919 + * Returns zero; does not fail.
2921 +static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
2923 + struct super_block *sb = dentry->d_sb;
2924 + struct ovl_fs *ufs = sb->s_fs_info;
2926 + seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
2927 + seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
2931 +static const struct super_operations ovl_super_operations = {
2932 + .put_super = ovl_put_super,
2933 + .remount_fs = ovl_remount_fs,
2934 + .statfs = ovl_statfs,
2935 + .show_options = ovl_show_options,
2944 +static const match_table_t ovl_tokens = {
2945 + {OPT_LOWERDIR, "lowerdir=%s"},
2946 + {OPT_UPPERDIR, "upperdir=%s"},
2950 +static int ovl_parse_opt(char *opt, struct ovl_config *config)
2954 + config->upperdir = NULL;
2955 + config->lowerdir = NULL;
2957 + while ((p = strsep(&opt, ",")) != NULL) {
2959 + substring_t args[MAX_OPT_ARGS];
2964 + token = match_token(p, ovl_tokens, args);
2966 + case OPT_UPPERDIR:
2967 + kfree(config->upperdir);
2968 + config->upperdir = match_strdup(&args[0]);
2969 + if (!config->upperdir)
2973 + case OPT_LOWERDIR:
2974 + kfree(config->lowerdir);
2975 + config->lowerdir = match_strdup(&args[0]);
2976 + if (!config->lowerdir)
2987 +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
2989 + struct path lowerpath;
2990 + struct path upperpath;
2991 + struct inode *root_inode;
2992 + struct dentry *root_dentry;
2993 + struct ovl_entry *oe;
2994 + struct ovl_fs *ufs;
2995 + struct kstatfs statfs;
2999 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
3003 + err = ovl_parse_opt((char *) data, &ufs->config);
3005 + goto out_free_ufs;
3008 + if (!ufs->config.upperdir || !ufs->config.lowerdir) {
3009 + pr_err("overlayfs: missing upperdir or lowerdir\n");
3010 + goto out_free_config;
3013 + oe = ovl_alloc_entry();
3015 + goto out_free_config;
3017 + err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
3021 + err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
3023 + goto out_put_upperpath;
3026 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
3027 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
3028 + goto out_put_lowerpath;
3030 + err = vfs_statfs(&lowerpath, &statfs);
3032 + pr_err("overlayfs: statfs failed on lowerpath\n");
3033 + goto out_put_lowerpath;
3035 + ufs->lower_namelen = statfs.f_namelen;
3037 + sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
3038 + lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
3041 + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
3042 + pr_err("overlayfs: maximum fs stacking depth exceeded\n");
3043 + goto out_put_lowerpath;
3047 + ufs->upper_mnt = clone_private_mount(&upperpath);
3048 + err = PTR_ERR(ufs->upper_mnt);
3049 + if (IS_ERR(ufs->upper_mnt)) {
3050 + pr_err("overlayfs: failed to clone upperpath\n");
3051 + goto out_put_lowerpath;
3054 + ufs->lower_mnt = clone_private_mount(&lowerpath);
3055 + err = PTR_ERR(ufs->lower_mnt);
3056 + if (IS_ERR(ufs->lower_mnt)) {
3057 + pr_err("overlayfs: failed to clone lowerpath\n");
3058 + goto out_put_upper_mnt;
3062 + * Make lower_mnt R/O. That way fchmod/fchown on lower file
3063 + * will fail instead of modifying lower fs.
3065 + ufs->lower_mnt->mnt_flags |= MNT_READONLY;
3067 + /* If the upper fs is r/o, we mark overlayfs r/o too */
3068 + if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
3069 + sb->s_flags |= MS_RDONLY;
3071 + if (!(sb->s_flags & MS_RDONLY)) {
3072 + err = mnt_want_write(ufs->upper_mnt);
3074 + goto out_put_lower_mnt;
3078 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
3080 + goto out_drop_write;
3082 + root_dentry = d_make_root(root_inode);
3084 + goto out_drop_write;
3086 + mntput(upperpath.mnt);
3087 + mntput(lowerpath.mnt);
3089 + oe->__upperdentry = dget(upperpath.dentry);
3090 + oe->lowerdentry = lowerpath.dentry;
3092 + root_dentry->d_fsdata = oe;
3093 + root_dentry->d_op = &ovl_dentry_operations;
3095 + sb->s_magic = OVERLAYFS_SUPER_MAGIC;
3096 + sb->s_op = &ovl_super_operations;
3097 + sb->s_root = root_dentry;
3098 + sb->s_fs_info = ufs;
3103 + if (!(sb->s_flags & MS_RDONLY))
3104 + mnt_drop_write(ufs->upper_mnt);
3106 + mntput(ufs->lower_mnt);
3108 + mntput(ufs->upper_mnt);
3110 + path_put(&lowerpath);
3112 + path_put(&upperpath);
3116 + kfree(ufs->config.lowerdir);
3117 + kfree(ufs->config.upperdir);
3124 +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
3125 + const char *dev_name, void *raw_data)
3127 + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
3130 +static struct file_system_type ovl_fs_type = {
3131 + .owner = THIS_MODULE,
3132 + .name = "overlayfs",
3133 + .mount = ovl_mount,
3134 + .kill_sb = kill_anon_super,
3136 +MODULE_ALIAS_FS("overlayfs");
3138 +static int __init ovl_init(void)
3140 + return register_filesystem(&ovl_fs_type);
3143 +static void __exit ovl_exit(void)
3145 + unregister_filesystem(&ovl_fs_type);
3148 +module_init(ovl_init);
3149 +module_exit(ovl_exit);
3152 @@ -1313,6 +1313,7 @@ long do_splice_direct(struct file *in, l
3156 +EXPORT_SYMBOL(do_splice_direct);
3158 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
3159 struct pipe_inode_info *opipe,
3160 --- a/include/linux/fs.h
3161 +++ b/include/linux/fs.h
3162 @@ -244,6 +244,12 @@ struct iattr {
3164 #include <linux/quota.h>
3167 + * Maximum number of layers of fs stack. Needs to be limited to
3168 + * prevent kernel stack overflow
3170 +#define FILESYSTEM_MAX_STACK_DEPTH 2
3173 * enum positive_aop_returns - aop return codes with specific semantics
3175 @@ -1322,6 +1328,11 @@ struct super_block {
3177 /* Being remounted read-only */
3178 int s_readonly_remount;
3181 + * Indicates how deep in a filesystem stack this SB is
3183 + int s_stack_depth;
3186 /* superblock cache pruning functions */
3187 @@ -1575,6 +1586,7 @@ struct inode_operations {
3188 int (*atomic_open)(struct inode *, struct dentry *,
3189 struct file *, unsigned open_flag,
3190 umode_t create_mode, int *opened);
3191 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
3192 } ____cacheline_aligned;
3194 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
3195 @@ -2008,6 +2020,7 @@ extern struct file *file_open_name(struc
3196 extern struct file *filp_open(const char *, int, umode_t);
3197 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
3199 +extern int vfs_open(const struct path *, struct file *, const struct cred *);
3200 extern struct file * dentry_open(const struct path *, int, const struct cred *);
3201 extern int filp_close(struct file *, fl_owner_t id);
3203 @@ -2208,6 +2221,7 @@ extern sector_t bmap(struct inode *, sec
3205 extern int notify_change(struct dentry *, struct iattr *);
3206 extern int inode_permission(struct inode *, int);
3207 +extern int __inode_permission(struct inode *, int);
3208 extern int generic_permission(struct inode *, int);
3210 static inline bool execute_ok(struct inode *inode)
3211 @@ -2414,6 +2428,9 @@ extern ssize_t generic_file_splice_write
3212 struct file *, loff_t *, size_t, unsigned int);
3213 extern ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe,
3214 struct file *out, loff_t *, size_t len, unsigned int flags);
3215 +extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
3216 + loff_t *opos, size_t len, unsigned int flags);
3220 file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping);
3221 --- a/include/linux/mount.h
3222 +++ b/include/linux/mount.h
3223 @@ -68,6 +68,9 @@ extern void mnt_pin(struct vfsmount *mnt
3224 extern void mnt_unpin(struct vfsmount *mnt);
3225 extern int __mnt_is_readonly(struct vfsmount *mnt);
3228 +extern struct vfsmount *clone_private_mount(struct path *path);
3230 struct file_system_type;
3231 extern struct vfsmount *vfs_kern_mount(struct file_system_type *type,
3232 int flags, const char *name,