1 --- a/Documentation/filesystems/Locking
2 +++ b/Documentation/filesystems/Locking
3 @@ -65,6 +65,7 @@ prototypes:
4 struct file *, unsigned open_flag,
5 umode_t create_mode, int *opened);
6 int (*tmpfile) (struct inode *, struct dentry *, umode_t);
7 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
11 @@ -93,6 +94,7 @@ fiemap: no
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 *, struct file *,
225 unsigned open_flag, umode_t create_mode, int *opened);
226 int (*tmpfile) (struct inode *, struct dentry *, umode_t);
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 @@ -6245,6 +6245,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 @@ -566,6 +566,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 @@ -44,7 +44,6 @@ extern void __init chrdev_init(void);
302 -extern int __inode_permission(struct inode *, int);
303 extern int user_path_mountpoint_at(int, const char __user *, unsigned int, struct path *);
304 extern int vfs_path_lookup(struct dentry *, struct vfsmount *,
305 const char *, unsigned int, struct path *);
306 @@ -142,12 +141,6 @@ extern ssize_t __kernel_write(struct fil
307 extern int rw_verify_area(int, struct file *, const loff_t *, size_t);
312 -extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
313 - loff_t *opos, size_t len, unsigned int flags);
318 extern const struct file_operations pipefifo_fops;
321 @@ -402,6 +402,7 @@ int __inode_permission(struct inode *ino
323 return security_inode_permission(inode, mask);
325 +EXPORT_SYMBOL(__inode_permission);
328 * sb_permission - Check superblock-level permissions
329 @@ -3065,9 +3066,12 @@ finish_open_created:
330 error = may_open(&nd->path, acc_mode, open_flag);
333 - file->f_path.mnt = nd->path.mnt;
334 - error = finish_open(file, nd->path.dentry, NULL, opened);
337 + BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
338 + error = vfs_open(&nd->path, file, current_cred());
340 + *opened |= FILE_OPENED;
342 if (error == -EOPENSTALE)
347 @@ -1466,6 +1466,33 @@ void drop_collected_mounts(struct vfsmou
352 + * clone_private_mount - create a private clone of a path
354 + * This creates a new vfsmount, which will be the clone of @path. The new will
355 + * not be attached anywhere in the namespace and will be private (i.e. changes
356 + * to the originating mount won't be propagated into this).
358 + * Release with mntput().
360 +struct vfsmount *clone_private_mount(struct path *path)
362 + struct mount *old_mnt = real_mount(path->mnt);
363 + struct mount *new_mnt;
365 + if (IS_MNT_UNBINDABLE(old_mnt))
366 + return ERR_PTR(-EINVAL);
368 + down_read(&namespace_sem);
369 + new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
370 + up_read(&namespace_sem);
371 + if (IS_ERR(new_mnt))
372 + return ERR_CAST(new_mnt);
374 + return &new_mnt->mnt;
376 +EXPORT_SYMBOL_GPL(clone_private_mount);
378 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
379 struct vfsmount *root)
383 @@ -814,8 +814,7 @@ struct file *dentry_open(const struct pa
384 f = get_empty_filp();
388 - error = do_dentry_open(f, NULL, cred);
389 + error = vfs_open(path, f, cred);
391 /* from now on we need fput() to dispose of f */
392 error = open_check_o_direct(f);
393 @@ -832,6 +831,26 @@ struct file *dentry_open(const struct pa
395 EXPORT_SYMBOL(dentry_open);
398 + * vfs_open - open the file at the given path
399 + * @path: path to open
400 + * @filp: newly allocated file with f_flag initialized
401 + * @cred: credentials to use
403 +int vfs_open(const struct path *path, struct file *filp,
404 + const struct cred *cred)
406 + struct inode *inode = path->dentry->d_inode;
408 + if (inode->i_op->dentry_open)
409 + return inode->i_op->dentry_open(path->dentry, filp, cred);
411 + filp->f_path = *path;
412 + return do_dentry_open(filp, NULL, cred);
415 +EXPORT_SYMBOL(vfs_open);
417 static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
419 int lookup_flags = 0;
421 +++ b/fs/overlayfs/Kconfig
424 + tristate "Overlay filesystem support"
426 + An overlay filesystem combines two filesystems - an 'upper' filesystem
427 + and a 'lower' filesystem. When a name exists in both filesystems, the
428 + object in the 'upper' filesystem is visible while the object in the
429 + 'lower' filesystem is either hidden or, in the case of directories,
430 + merged with the 'upper' object.
432 + For more information see Documentation/filesystems/overlayfs.txt
434 +++ b/fs/overlayfs/Makefile
437 +# Makefile for the overlay filesystem.
440 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
442 +overlayfs-objs := super.o inode.o dir.o readdir.o copy_up.o
444 +++ b/fs/overlayfs/copy_up.c
448 + * Copyright (C) 2011 Novell Inc.
450 + * This program is free software; you can redistribute it and/or modify it
451 + * under the terms of the GNU General Public License version 2 as published by
452 + * the Free Software Foundation.
455 +#include <linux/fs.h>
456 +#include <linux/slab.h>
457 +#include <linux/file.h>
458 +#include <linux/splice.h>
459 +#include <linux/xattr.h>
460 +#include <linux/security.h>
461 +#include <linux/uaccess.h>
462 +#include <linux/sched.h>
463 +#include "overlayfs.h"
465 +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
467 +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
469 + ssize_t list_size, size;
470 + char *buf, *name, *value;
473 + if (!old->d_inode->i_op->getxattr ||
474 + !new->d_inode->i_op->getxattr)
477 + list_size = vfs_listxattr(old, NULL, 0);
478 + if (list_size <= 0) {
479 + if (list_size == -EOPNOTSUPP)
484 + buf = kzalloc(list_size, GFP_KERNEL);
489 + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
493 + list_size = vfs_listxattr(old, buf, list_size);
494 + if (list_size <= 0) {
496 + goto out_free_value;
499 + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
500 + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
503 + goto out_free_value;
505 + error = vfs_setxattr(new, name, value, size, 0);
507 + goto out_free_value;
517 +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
519 + struct file *old_file;
520 + struct file *new_file;
521 + loff_t old_pos = 0;
522 + loff_t new_pos = 0;
528 + old_file = ovl_path_open(old, O_RDONLY);
529 + if (IS_ERR(old_file))
530 + return PTR_ERR(old_file);
532 + new_file = ovl_path_open(new, O_WRONLY);
533 + if (IS_ERR(new_file)) {
534 + error = PTR_ERR(new_file);
538 + /* FIXME: copy up sparse files efficiently */
540 + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
543 + if (len < this_len)
546 + if (signal_pending_state(TASK_KILLABLE, current)) {
551 + bytes = do_splice_direct(old_file, &old_pos,
552 + new_file, &new_pos,
553 + this_len, SPLICE_F_MOVE);
558 + WARN_ON(old_pos != new_pos);
569 +static char *ovl_read_symlink(struct dentry *realdentry)
573 + struct inode *inode = realdentry->d_inode;
574 + mm_segment_t old_fs;
577 + if (!inode->i_op->readlink)
581 + buf = (char *) __get_free_page(GFP_KERNEL);
587 + /* The cast to a user pointer is valid due to the set_fs() */
588 + res = inode->i_op->readlink(realdentry,
589 + (char __user *)buf, PAGE_SIZE - 1);
592 + free_page((unsigned long) buf);
600 + return ERR_PTR(res);
603 +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
605 + struct iattr attr = {
607 + ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
608 + .ia_atime = stat->atime,
609 + .ia_mtime = stat->mtime,
612 + return notify_change(upperdentry, &attr);
615 +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
617 + struct iattr attr = {
618 + .ia_valid = ATTR_MODE,
622 + return notify_change(upperdentry, &attr);
625 +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
626 + struct path *lowerpath, struct kstat *stat,
630 + struct path newpath;
631 + umode_t mode = stat->mode;
633 + /* Can't properly set mode on creation because of the umask */
634 + stat->mode &= S_IFMT;
636 + ovl_path_upper(dentry, &newpath);
637 + WARN_ON(newpath.dentry);
638 + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
639 + if (IS_ERR(newpath.dentry))
640 + return PTR_ERR(newpath.dentry);
642 + if (S_ISREG(stat->mode)) {
643 + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
648 + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
652 + mutex_lock(&newpath.dentry->d_inode->i_mutex);
653 + if (!S_ISLNK(stat->mode))
654 + err = ovl_set_mode(newpath.dentry, mode);
656 + err = ovl_set_timestamps(newpath.dentry, stat);
657 + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
661 + ovl_dentry_update(dentry, newpath.dentry);
664 + * Easiest way to get rid of the lower dentry reference is to
665 + * drop this dentry. This is neither needed nor possible for
668 + if (!S_ISDIR(stat->mode))
674 + if (S_ISDIR(stat->mode))
675 + vfs_rmdir(upperdir->d_inode, newpath.dentry);
677 + vfs_unlink(upperdir->d_inode, newpath.dentry);
679 + dput(newpath.dentry);
685 + * Copy up a single dentry
687 + * Directory renames only allowed on "pure upper" (already created on
688 + * upper filesystem, never copied up). Directories which are on lower or
689 + * are merged may not be renamed. For these -EXDEV is returned and
690 + * userspace has to deal with it. This means, when copying up a
691 + * directory we can rely on it and ancestors being stable.
693 + * Non-directory renames start with copy up of source if necessary. The
694 + * actual rename will only proceed once the copy up was successful. Copy
695 + * up uses upper parent i_mutex for exclusion. Since rename can change
696 + * d_parent it is possible that the copy up will lock the old parent. At
697 + * that point the file will have already been copied up anyway.
699 +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
700 + struct path *lowerpath, struct kstat *stat)
703 + struct kstat pstat;
704 + struct path parentpath;
705 + struct dentry *upperdir;
706 + const struct cred *old_cred;
707 + struct cred *override_cred;
710 + ovl_path_upper(parent, &parentpath);
711 + upperdir = parentpath.dentry;
713 + err = vfs_getattr(&parentpath, &pstat);
717 + if (S_ISLNK(stat->mode)) {
718 + link = ovl_read_symlink(lowerpath->dentry);
720 + return PTR_ERR(link);
724 + override_cred = prepare_creds();
725 + if (!override_cred)
726 + goto out_free_link;
728 + override_cred->fsuid = stat->uid;
729 + override_cred->fsgid = stat->gid;
731 + * CAP_SYS_ADMIN for copying up extended attributes
732 + * CAP_DAC_OVERRIDE for create
733 + * CAP_FOWNER for chmod, timestamp update
734 + * CAP_FSETID for chmod
735 + * CAP_MKNOD for mknod
737 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
738 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
739 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
740 + cap_raise(override_cred->cap_effective, CAP_FSETID);
741 + cap_raise(override_cred->cap_effective, CAP_MKNOD);
742 + old_cred = override_creds(override_cred);
744 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
745 + if (ovl_path_type(dentry) != OVL_PATH_LOWER) {
748 + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
751 + /* Restore timestamps on parent (best effort) */
752 + ovl_set_timestamps(upperdir, &pstat);
756 + mutex_unlock(&upperdir->d_inode->i_mutex);
758 + revert_creds(old_cred);
759 + put_cred(override_cred);
763 + free_page((unsigned long) link);
768 +int ovl_copy_up(struct dentry *dentry)
774 + struct dentry *next;
775 + struct dentry *parent;
776 + struct path lowerpath;
778 + enum ovl_path_type type = ovl_path_type(dentry);
780 + if (type != OVL_PATH_LOWER)
783 + next = dget(dentry);
784 + /* find the topmost dentry not yet copied up */
786 + parent = dget_parent(next);
788 + type = ovl_path_type(parent);
789 + if (type != OVL_PATH_LOWER)
796 + ovl_path_lower(next, &lowerpath);
797 + err = vfs_getattr(&lowerpath, &stat);
799 + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
808 +/* Optimize by not copying up the file first and truncating later */
809 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
813 + struct path lowerpath;
814 + struct dentry *parent = dget_parent(dentry);
816 + err = ovl_copy_up(parent);
818 + goto out_dput_parent;
820 + ovl_path_lower(dentry, &lowerpath);
821 + err = vfs_getattr(&lowerpath, &stat);
823 + goto out_dput_parent;
825 + if (size < stat.size)
828 + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
835 +++ b/fs/overlayfs/dir.c
839 + * Copyright (C) 2011 Novell Inc.
841 + * This program is free software; you can redistribute it and/or modify it
842 + * under the terms of the GNU General Public License version 2 as published by
843 + * the Free Software Foundation.
846 +#include <linux/fs.h>
847 +#include <linux/namei.h>
848 +#include <linux/xattr.h>
849 +#include <linux/security.h>
850 +#include <linux/cred.h>
851 +#include "overlayfs.h"
853 +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
855 +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
858 + struct dentry *newdentry;
859 + const struct cred *old_cred;
860 + struct cred *override_cred;
862 + /* FIXME: recheck lower dentry to see if whiteout is really needed */
865 + override_cred = prepare_creds();
866 + if (!override_cred)
870 + * CAP_SYS_ADMIN for setxattr
871 + * CAP_DAC_OVERRIDE for symlink creation
872 + * CAP_FOWNER for unlink in sticky directory
874 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
875 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
876 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
877 + override_cred->fsuid = GLOBAL_ROOT_UID;
878 + override_cred->fsgid = GLOBAL_ROOT_GID;
879 + old_cred = override_creds(override_cred);
881 + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
882 + dentry->d_name.len);
883 + err = PTR_ERR(newdentry);
884 + if (IS_ERR(newdentry))
887 + /* Just been removed within the same locked region */
888 + WARN_ON(newdentry->d_inode);
890 + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
894 + ovl_dentry_version_inc(dentry->d_parent);
896 + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
898 + vfs_unlink(upperdir->d_inode, newdentry);
903 + revert_creds(old_cred);
904 + put_cred(override_cred);
908 + * There's no way to recover from failure to whiteout.
909 + * What should we do? Log a big fat error and... ?
911 + pr_err("overlayfs: ERROR - failed to whiteout '%s'\n",
912 + dentry->d_name.name);
918 +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
919 + struct dentry *template)
922 + struct dentry *newdentry;
923 + struct qstr *name = &template->d_name;
925 + newdentry = lookup_one_len(name->name, upperdir, name->len);
926 + if (IS_ERR(newdentry))
929 + if (newdentry->d_inode) {
930 + const struct cred *old_cred;
931 + struct cred *override_cred;
933 + /* No need to check whiteout if lower parent is non-existent */
935 + if (!ovl_dentry_lower(template->d_parent))
938 + if (!S_ISLNK(newdentry->d_inode->i_mode))
942 + override_cred = prepare_creds();
943 + if (!override_cred)
947 + * CAP_SYS_ADMIN for getxattr
948 + * CAP_FOWNER for unlink in sticky directory
950 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
951 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
952 + old_cred = override_creds(override_cred);
955 + if (ovl_is_whiteout(newdentry))
956 + err = vfs_unlink(upperdir->d_inode, newdentry);
958 + revert_creds(old_cred);
959 + put_cred(override_cred);
964 + newdentry = lookup_one_len(name->name, upperdir, name->len);
965 + if (IS_ERR(newdentry)) {
966 + ovl_whiteout(upperdir, template);
971 + * Whiteout just been successfully removed, parent
972 + * i_mutex is still held, there's no way the lookup
973 + * could return positive.
975 + WARN_ON(newdentry->d_inode);
982 + return ERR_PTR(err);
985 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
986 + struct kstat *stat, const char *link)
989 + struct dentry *newdentry;
990 + struct inode *dir = upperdir->d_inode;
992 + newdentry = ovl_lookup_create(upperdir, dentry);
993 + if (IS_ERR(newdentry))
996 + switch (stat->mode & S_IFMT) {
998 + err = vfs_create(dir, newdentry, stat->mode, NULL);
1002 + err = vfs_mkdir(dir, newdentry, stat->mode);
1009 + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
1013 + err = vfs_symlink(dir, newdentry, link);
1020 + if (ovl_dentry_is_opaque(dentry))
1021 + ovl_whiteout(upperdir, dentry);
1023 + newdentry = ERR_PTR(err);
1024 + } else if (WARN_ON(!newdentry->d_inode)) {
1026 + * Not quite sure if non-instantiated dentry is legal or not.
1027 + * VFS doesn't seem to care so check and warn here.
1030 + newdentry = ERR_PTR(-ENOENT);
1038 +static int ovl_set_opaque(struct dentry *upperdentry)
1041 + const struct cred *old_cred;
1042 + struct cred *override_cred;
1044 + override_cred = prepare_creds();
1045 + if (!override_cred)
1048 + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
1049 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1050 + old_cred = override_creds(override_cred);
1051 + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
1052 + revert_creds(old_cred);
1053 + put_cred(override_cred);
1058 +static int ovl_remove_opaque(struct dentry *upperdentry)
1061 + const struct cred *old_cred;
1062 + struct cred *override_cred;
1064 + override_cred = prepare_creds();
1065 + if (!override_cred)
1068 + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
1069 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1070 + old_cred = override_creds(override_cred);
1071 + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
1072 + revert_creds(old_cred);
1073 + put_cred(override_cred);
1078 +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1079 + struct kstat *stat)
1082 + enum ovl_path_type type;
1083 + struct path realpath;
1085 + type = ovl_path_real(dentry, &realpath);
1086 + err = vfs_getattr(&realpath, stat);
1090 + stat->dev = dentry->d_sb->s_dev;
1091 + stat->ino = dentry->d_inode->i_ino;
1094 + * It's probably not worth it to count subdirs to get the
1095 + * correct link count. nlink=1 seems to pacify 'find' and
1096 + * other utilities.
1098 + if (type == OVL_PATH_MERGE)
1104 +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
1108 + struct dentry *newdentry;
1109 + struct dentry *upperdir;
1110 + struct inode *inode;
1111 + struct kstat stat = {
1117 + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
1121 + err = ovl_copy_up(dentry->d_parent);
1125 + upperdir = ovl_dentry_upper(dentry->d_parent);
1126 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1128 + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
1129 + err = PTR_ERR(newdentry);
1130 + if (IS_ERR(newdentry))
1133 + ovl_dentry_version_inc(dentry->d_parent);
1134 + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
1135 + err = ovl_set_opaque(newdentry);
1137 + vfs_rmdir(upperdir->d_inode, newdentry);
1138 + ovl_whiteout(upperdir, dentry);
1142 + ovl_dentry_update(dentry, newdentry);
1143 + ovl_copyattr(newdentry->d_inode, inode);
1144 + d_instantiate(dentry, inode);
1152 + mutex_unlock(&upperdir->d_inode->i_mutex);
1159 +static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1162 + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
1165 +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1167 + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
1170 +static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1173 + return ovl_create_object(dentry, mode, rdev, NULL);
1176 +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
1179 + return ovl_create_object(dentry, S_IFLNK, 0, link);
1182 +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
1185 + enum ovl_path_type type;
1186 + struct path realpath;
1187 + struct dentry *upperdir;
1189 + err = ovl_copy_up(dentry->d_parent);
1193 + upperdir = ovl_dentry_upper(dentry->d_parent);
1194 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1195 + type = ovl_path_real(dentry, &realpath);
1196 + if (type != OVL_PATH_LOWER) {
1198 + if (realpath.dentry->d_parent != upperdir)
1201 + /* FIXME: create whiteout up front and rename to target */
1204 + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
1206 + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
1210 + ovl_dentry_version_inc(dentry->d_parent);
1213 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1214 + err = ovl_whiteout(upperdir, dentry);
1217 + * Keeping this dentry hashed would mean having to release
1218 + * upperpath/lowerpath, which could only be done if we are the
1219 + * sole user of this dentry. Too tricky... Just unhash for
1224 + mutex_unlock(&upperdir->d_inode->i_mutex);
1229 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1231 + return ovl_do_remove(dentry, false);
1235 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
1238 + enum ovl_path_type type;
1240 + type = ovl_path_type(dentry);
1241 + if (type != OVL_PATH_UPPER) {
1242 + err = ovl_check_empty_and_clear(dentry, type);
1247 + return ovl_do_remove(dentry, true);
1250 +static int ovl_link(struct dentry *old, struct inode *newdir,
1251 + struct dentry *new)
1254 + struct dentry *olddentry;
1255 + struct dentry *newdentry;
1256 + struct dentry *upperdir;
1257 + struct inode *newinode;
1259 + err = ovl_copy_up(old);
1263 + err = ovl_copy_up(new->d_parent);
1267 + upperdir = ovl_dentry_upper(new->d_parent);
1268 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1269 + newdentry = ovl_lookup_create(upperdir, new);
1270 + err = PTR_ERR(newdentry);
1271 + if (IS_ERR(newdentry))
1274 + olddentry = ovl_dentry_upper(old);
1275 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
1277 + if (WARN_ON(!newdentry->d_inode)) {
1282 + newinode = ovl_new_inode(old->d_sb, newdentry->d_inode->i_mode,
1288 + ovl_copyattr(upperdir->d_inode, newinode);
1290 + ovl_dentry_version_inc(new->d_parent);
1291 + ovl_dentry_update(new, newdentry);
1293 + d_instantiate(new, newinode);
1296 + if (ovl_dentry_is_opaque(new))
1297 + ovl_whiteout(upperdir, new);
1301 + mutex_unlock(&upperdir->d_inode->i_mutex);
1306 +static int ovl_rename(struct inode *olddir, struct dentry *old,
1307 + struct inode *newdir, struct dentry *new)
1310 + enum ovl_path_type old_type;
1311 + enum ovl_path_type new_type;
1312 + struct dentry *old_upperdir;
1313 + struct dentry *new_upperdir;
1314 + struct dentry *olddentry;
1315 + struct dentry *newdentry;
1316 + struct dentry *trap;
1319 + bool new_create = false;
1320 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
1322 + /* Don't copy up directory trees */
1323 + old_type = ovl_path_type(old);
1324 + if (old_type != OVL_PATH_UPPER && is_dir)
1327 + if (new->d_inode) {
1328 + new_type = ovl_path_type(new);
1330 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
1331 + if (ovl_dentry_lower(old)->d_inode ==
1332 + ovl_dentry_lower(new)->d_inode)
1335 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
1336 + if (ovl_dentry_upper(old)->d_inode ==
1337 + ovl_dentry_upper(new)->d_inode)
1341 + if (new_type != OVL_PATH_UPPER &&
1342 + S_ISDIR(new->d_inode->i_mode)) {
1343 + err = ovl_check_empty_and_clear(new, new_type);
1348 + new_type = OVL_PATH_UPPER;
1351 + err = ovl_copy_up(old);
1355 + err = ovl_copy_up(new->d_parent);
1359 + old_upperdir = ovl_dentry_upper(old->d_parent);
1360 + new_upperdir = ovl_dentry_upper(new->d_parent);
1362 + trap = lock_rename(new_upperdir, old_upperdir);
1364 + olddentry = ovl_dentry_upper(old);
1365 + newdentry = ovl_dentry_upper(new);
1369 + new_create = true;
1370 + newdentry = ovl_lookup_create(new_upperdir, new);
1371 + err = PTR_ERR(newdentry);
1372 + if (IS_ERR(newdentry))
1377 + if (olddentry->d_parent != old_upperdir)
1379 + if (newdentry->d_parent != new_upperdir)
1381 + if (olddentry == trap)
1383 + if (newdentry == trap)
1386 + old_opaque = ovl_dentry_is_opaque(old);
1387 + new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
1389 + if (is_dir && !old_opaque && new_opaque) {
1390 + err = ovl_set_opaque(olddentry);
1395 + err = vfs_rename(old_upperdir->d_inode, olddentry,
1396 + new_upperdir->d_inode, newdentry);
1399 + if (new_create && ovl_dentry_is_opaque(new))
1400 + ovl_whiteout(new_upperdir, new);
1401 + if (is_dir && !old_opaque && new_opaque)
1402 + ovl_remove_opaque(olddentry);
1406 + if (old_type != OVL_PATH_UPPER || old_opaque)
1407 + err = ovl_whiteout(old_upperdir, old);
1408 + if (is_dir && old_opaque && !new_opaque)
1409 + ovl_remove_opaque(olddentry);
1411 + if (old_opaque != new_opaque)
1412 + ovl_dentry_set_opaque(old, new_opaque);
1414 + ovl_dentry_version_inc(old->d_parent);
1415 + ovl_dentry_version_inc(new->d_parent);
1420 + unlock_rename(new_upperdir, old_upperdir);
1424 +const struct inode_operations ovl_dir_inode_operations = {
1425 + .lookup = ovl_lookup,
1426 + .mkdir = ovl_mkdir,
1427 + .symlink = ovl_symlink,
1428 + .unlink = ovl_unlink,
1429 + .rmdir = ovl_rmdir,
1430 + .rename = ovl_rename,
1432 + .setattr = ovl_setattr,
1433 + .create = ovl_create,
1434 + .mknod = ovl_mknod,
1435 + .permission = ovl_permission,
1436 + .getattr = ovl_dir_getattr,
1437 + .setxattr = ovl_setxattr,
1438 + .getxattr = ovl_getxattr,
1439 + .listxattr = ovl_listxattr,
1440 + .removexattr = ovl_removexattr,
1443 +++ b/fs/overlayfs/inode.c
1447 + * Copyright (C) 2011 Novell Inc.
1449 + * This program is free software; you can redistribute it and/or modify it
1450 + * under the terms of the GNU General Public License version 2 as published by
1451 + * the Free Software Foundation.
1454 +#include <linux/fs.h>
1455 +#include <linux/slab.h>
1456 +#include <linux/xattr.h>
1457 +#include "overlayfs.h"
1459 +int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1461 + struct dentry *upperdentry;
1464 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1465 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1467 + err = ovl_copy_up(dentry);
1471 + upperdentry = ovl_dentry_upper(dentry);
1473 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1474 + attr->ia_valid &= ~ATTR_MODE;
1476 + mutex_lock(&upperdentry->d_inode->i_mutex);
1477 + err = notify_change(upperdentry, attr);
1479 + ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
1480 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1485 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1486 + struct kstat *stat)
1488 + struct path realpath;
1490 + ovl_path_real(dentry, &realpath);
1491 + return vfs_getattr(&realpath, stat);
1494 +int ovl_permission(struct inode *inode, int mask)
1496 + struct ovl_entry *oe;
1497 + struct dentry *alias = NULL;
1498 + struct inode *realinode;
1499 + struct dentry *realdentry;
1503 + if (S_ISDIR(inode->i_mode)) {
1504 + oe = inode->i_private;
1505 + } else if (mask & MAY_NOT_BLOCK) {
1509 + * For non-directories find an alias and get the info
1512 + alias = d_find_any_alias(inode);
1513 + if (WARN_ON(!alias))
1516 + oe = alias->d_fsdata;
1519 + realdentry = ovl_entry_real(oe, &is_upper);
1521 + /* Careful in RCU walk mode */
1522 + realinode = ACCESS_ONCE(realdentry->d_inode);
1524 + WARN_ON(!(mask & MAY_NOT_BLOCK));
1529 + if (mask & MAY_WRITE) {
1530 + umode_t mode = realinode->i_mode;
1533 + * Writes will always be redirected to upper layer, so
1534 + * ignore lower layer being read-only.
1536 + * If the overlay itself is read-only then proceed
1537 + * with the permission check, don't return EROFS.
1538 + * This will only happen if this is the lower layer of
1539 + * another overlayfs.
1541 + * If upper fs becomes read-only after the overlay was
1542 + * constructed return EROFS to prevent modification of
1546 + if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
1547 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1551 + err = __inode_permission(realinode, mask);
1558 +struct ovl_link_data {
1559 + struct dentry *realdentry;
1563 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1566 + struct dentry *realdentry;
1567 + struct inode *realinode;
1569 + realdentry = ovl_dentry_real(dentry);
1570 + realinode = realdentry->d_inode;
1572 + if (WARN_ON(!realinode->i_op->follow_link))
1573 + return ERR_PTR(-EPERM);
1575 + ret = realinode->i_op->follow_link(realdentry, nd);
1579 + if (realinode->i_op->put_link) {
1580 + struct ovl_link_data *data;
1582 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1584 + realinode->i_op->put_link(realdentry, nd, ret);
1585 + return ERR_PTR(-ENOMEM);
1587 + data->realdentry = realdentry;
1588 + data->cookie = ret;
1596 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1598 + struct inode *realinode;
1599 + struct ovl_link_data *data = c;
1604 + realinode = data->realdentry->d_inode;
1605 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1609 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1611 + struct path realpath;
1612 + struct inode *realinode;
1614 + ovl_path_real(dentry, &realpath);
1615 + realinode = realpath.dentry->d_inode;
1617 + if (!realinode->i_op->readlink)
1620 + touch_atime(&realpath);
1622 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1626 +static bool ovl_is_private_xattr(const char *name)
1628 + return strncmp(name, "trusted.overlay.", 14) == 0;
1631 +int ovl_setxattr(struct dentry *dentry, const char *name,
1632 + const void *value, size_t size, int flags)
1635 + struct dentry *upperdentry;
1637 + if (ovl_is_private_xattr(name))
1640 + err = ovl_copy_up(dentry);
1644 + upperdentry = ovl_dentry_upper(dentry);
1645 + return vfs_setxattr(upperdentry, name, value, size, flags);
1648 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1649 + void *value, size_t size)
1651 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1652 + ovl_is_private_xattr(name))
1655 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
1658 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
1663 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
1664 + if (res <= 0 || size == 0)
1667 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
1670 + /* filter out private xattrs */
1671 + for (off = 0; off < res;) {
1672 + char *s = list + off;
1673 + size_t slen = strlen(s) + 1;
1675 + BUG_ON(off + slen > res);
1677 + if (ovl_is_private_xattr(s)) {
1679 + memmove(s, s + slen, res - off);
1688 +int ovl_removexattr(struct dentry *dentry, const char *name)
1691 + struct path realpath;
1692 + enum ovl_path_type type;
1694 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1695 + ovl_is_private_xattr(name))
1698 + type = ovl_path_real(dentry, &realpath);
1699 + if (type == OVL_PATH_LOWER) {
1700 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
1704 + err = ovl_copy_up(dentry);
1708 + ovl_path_upper(dentry, &realpath);
1711 + return vfs_removexattr(realpath.dentry, name);
1714 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
1715 + struct dentry *realdentry)
1717 + if (type != OVL_PATH_LOWER)
1720 + if (special_file(realdentry->d_inode->i_mode))
1723 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
1729 +static int ovl_dentry_open(struct dentry *dentry, struct file *file,
1730 + const struct cred *cred)
1733 + struct path realpath;
1734 + enum ovl_path_type type;
1736 + type = ovl_path_real(dentry, &realpath);
1737 + if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
1738 + if (file->f_flags & O_TRUNC)
1739 + err = ovl_copy_up_truncate(dentry, 0);
1741 + err = ovl_copy_up(dentry);
1745 + ovl_path_upper(dentry, &realpath);
1748 + return vfs_open(&realpath, file, cred);
1751 +static const struct inode_operations ovl_file_inode_operations = {
1752 + .setattr = ovl_setattr,
1753 + .permission = ovl_permission,
1754 + .getattr = ovl_getattr,
1755 + .setxattr = ovl_setxattr,
1756 + .getxattr = ovl_getxattr,
1757 + .listxattr = ovl_listxattr,
1758 + .removexattr = ovl_removexattr,
1759 + .dentry_open = ovl_dentry_open,
1762 +static const struct inode_operations ovl_symlink_inode_operations = {
1763 + .setattr = ovl_setattr,
1764 + .follow_link = ovl_follow_link,
1765 + .put_link = ovl_put_link,
1766 + .readlink = ovl_readlink,
1767 + .getattr = ovl_getattr,
1768 + .setxattr = ovl_setxattr,
1769 + .getxattr = ovl_getxattr,
1770 + .listxattr = ovl_listxattr,
1771 + .removexattr = ovl_removexattr,
1774 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1775 + struct ovl_entry *oe)
1777 + struct inode *inode;
1779 + inode = new_inode(sb);
1785 + inode->i_ino = get_next_ino();
1786 + inode->i_mode = mode;
1787 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
1791 + inode->i_private = oe;
1792 + inode->i_op = &ovl_dir_inode_operations;
1793 + inode->i_fop = &ovl_dir_operations;
1797 + inode->i_op = &ovl_symlink_inode_operations;
1805 + inode->i_op = &ovl_file_inode_operations;
1809 + WARN(1, "illegal file type: %i\n", mode);
1818 +++ b/fs/overlayfs/overlayfs.h
1822 + * Copyright (C) 2011 Novell Inc.
1824 + * This program is free software; you can redistribute it and/or modify it
1825 + * under the terms of the GNU General Public License version 2 as published by
1826 + * the Free Software Foundation.
1831 +enum ovl_path_type {
1837 +extern const char *ovl_opaque_xattr;
1838 +extern const char *ovl_whiteout_xattr;
1839 +extern const struct dentry_operations ovl_dentry_operations;
1841 +enum ovl_path_type ovl_path_type(struct dentry *dentry);
1842 +u64 ovl_dentry_version_get(struct dentry *dentry);
1843 +void ovl_dentry_version_inc(struct dentry *dentry);
1844 +void ovl_path_upper(struct dentry *dentry, struct path *path);
1845 +void ovl_path_lower(struct dentry *dentry, struct path *path);
1846 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1847 +struct dentry *ovl_dentry_upper(struct dentry *dentry);
1848 +struct dentry *ovl_dentry_lower(struct dentry *dentry);
1849 +struct dentry *ovl_dentry_real(struct dentry *dentry);
1850 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
1851 +bool ovl_dentry_is_opaque(struct dentry *dentry);
1852 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
1853 +bool ovl_is_whiteout(struct dentry *dentry);
1854 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
1855 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
1856 + unsigned int flags);
1857 +struct file *ovl_path_open(struct path *path, int flags);
1859 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
1860 + struct kstat *stat, const char *link);
1863 +extern const struct file_operations ovl_dir_operations;
1864 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
1867 +int ovl_setattr(struct dentry *dentry, struct iattr *attr);
1868 +int ovl_permission(struct inode *inode, int mask);
1869 +int ovl_setxattr(struct dentry *dentry, const char *name,
1870 + const void *value, size_t size, int flags);
1871 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1872 + void *value, size_t size);
1873 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
1874 +int ovl_removexattr(struct dentry *dentry, const char *name);
1876 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1877 + struct ovl_entry *oe);
1878 +static inline void ovl_copyattr(struct inode *from, struct inode *to)
1880 + to->i_uid = from->i_uid;
1881 + to->i_gid = from->i_gid;
1885 +extern const struct inode_operations ovl_dir_inode_operations;
1888 +int ovl_copy_up(struct dentry *dentry);
1889 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
1891 +++ b/fs/overlayfs/readdir.c
1895 + * Copyright (C) 2011 Novell Inc.
1897 + * This program is free software; you can redistribute it and/or modify it
1898 + * under the terms of the GNU General Public License version 2 as published by
1899 + * the Free Software Foundation.
1902 +#include <linux/fs.h>
1903 +#include <linux/slab.h>
1904 +#include <linux/namei.h>
1905 +#include <linux/file.h>
1906 +#include <linux/xattr.h>
1907 +#include <linux/rbtree.h>
1908 +#include <linux/security.h>
1909 +#include <linux/cred.h>
1910 +#include "overlayfs.h"
1912 +struct ovl_cache_entry {
1915 + unsigned int type;
1918 + struct list_head l_node;
1919 + struct rb_node node;
1922 +struct ovl_readdir_data {
1923 + struct dir_context ctx;
1925 + struct rb_root *root;
1926 + struct list_head *list;
1927 + struct list_head *middle;
1928 + struct dentry *dir;
1933 +struct ovl_dir_file {
1936 + struct list_head cursor;
1937 + u64 cache_version;
1938 + struct list_head cache;
1939 + struct file *realfile;
1942 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
1944 + return container_of(n, struct ovl_cache_entry, node);
1947 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
1948 + const char *name, int len)
1950 + struct rb_node *node = root->rb_node;
1954 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
1956 + cmp = strncmp(name, p->name, len);
1958 + node = p->node.rb_right;
1959 + else if (cmp < 0 || len < p->len)
1960 + node = p->node.rb_left;
1968 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
1969 + u64 ino, unsigned int d_type)
1971 + struct ovl_cache_entry *p;
1973 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
1975 + char *name_copy = (char *) (p + 1);
1976 + memcpy(name_copy, name, len);
1977 + name_copy[len] = '\0';
1978 + p->name = name_copy;
1982 + p->is_whiteout = false;
1988 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
1989 + const char *name, int len, u64 ino,
1990 + unsigned int d_type)
1992 + struct rb_node **newp = &rdd->root->rb_node;
1993 + struct rb_node *parent = NULL;
1994 + struct ovl_cache_entry *p;
1998 + struct ovl_cache_entry *tmp;
2001 + tmp = ovl_cache_entry_from_node(*newp);
2002 + cmp = strncmp(name, tmp->name, len);
2004 + newp = &tmp->node.rb_right;
2005 + else if (cmp < 0 || len < tmp->len)
2006 + newp = &tmp->node.rb_left;
2011 + p = ovl_cache_entry_new(name, len, ino, d_type);
2015 + list_add_tail(&p->l_node, rdd->list);
2016 + rb_link_node(&p->node, parent, newp);
2017 + rb_insert_color(&p->node, rdd->root);
2022 +static int ovl_fill_lower(struct ovl_readdir_data *rdd,
2023 + const char *name, int namelen,
2024 + loff_t offset, u64 ino, unsigned int d_type)
2026 + struct ovl_cache_entry *p;
2028 + p = ovl_cache_entry_find(rdd->root, name, namelen);
2030 + list_move_tail(&p->l_node, rdd->middle);
2032 + p = ovl_cache_entry_new(name, namelen, ino, d_type);
2034 + rdd->err = -ENOMEM;
2036 + list_add_tail(&p->l_node, rdd->middle);
2042 +static void ovl_cache_free(struct list_head *list)
2044 + struct ovl_cache_entry *p;
2045 + struct ovl_cache_entry *n;
2047 + list_for_each_entry_safe(p, n, list, l_node)
2050 + INIT_LIST_HEAD(list);
2053 +static int ovl_fill_merge(void *buf, const char *name, int namelen,
2054 + loff_t offset, u64 ino, unsigned int d_type)
2056 + struct ovl_readdir_data *rdd = buf;
2059 + if (!rdd->is_merge)
2060 + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
2062 + return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type);
2065 +static inline int ovl_dir_read(struct path *realpath,
2066 + struct ovl_readdir_data *rdd)
2068 + struct file *realfile;
2071 + realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
2072 + if (IS_ERR(realfile))
2073 + return PTR_ERR(realfile);
2079 + err = iterate_dir(realfile, &rdd->ctx);
2082 + } while (!err && rdd->count);
2088 +static void ovl_dir_reset(struct file *file)
2090 + struct ovl_dir_file *od = file->private_data;
2091 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
2093 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
2094 + list_del_init(&od->cursor);
2095 + ovl_cache_free(&od->cache);
2096 + od->is_cached = false;
2098 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
2099 + if (od->is_real && type == OVL_PATH_MERGE) {
2100 + fput(od->realfile);
2101 + od->realfile = NULL;
2102 + od->is_real = false;
2106 +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
2108 + struct ovl_cache_entry *p;
2109 + struct dentry *dentry;
2110 + const struct cred *old_cred;
2111 + struct cred *override_cred;
2113 + override_cred = prepare_creds();
2114 + if (!override_cred) {
2115 + ovl_cache_free(rdd->list);
2120 + * CAP_SYS_ADMIN for getxattr
2121 + * CAP_DAC_OVERRIDE for lookup
2123 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2124 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2125 + old_cred = override_creds(override_cred);
2127 + mutex_lock(&rdd->dir->d_inode->i_mutex);
2128 + list_for_each_entry(p, rdd->list, l_node) {
2129 + if (p->type != DT_LNK)
2132 + dentry = lookup_one_len(p->name, rdd->dir, p->len);
2133 + if (IS_ERR(dentry))
2136 + p->is_whiteout = ovl_is_whiteout(dentry);
2139 + mutex_unlock(&rdd->dir->d_inode->i_mutex);
2141 + revert_creds(old_cred);
2142 + put_cred(override_cred);
2147 +static inline int ovl_dir_read_merged(struct path *upperpath,
2148 + struct path *lowerpath,
2149 + struct list_head *list)
2152 + struct rb_root root = RB_ROOT;
2153 + struct list_head middle;
2154 + struct ovl_readdir_data rdd = {
2155 + .ctx.actor = ovl_fill_merge,
2158 + .is_merge = false,
2161 + if (upperpath->dentry) {
2162 + rdd.dir = upperpath->dentry;
2163 + err = ovl_dir_read(upperpath, &rdd);
2167 + err = ovl_dir_mark_whiteouts(&rdd);
2172 + * Insert lowerpath entries before upperpath ones, this allows
2173 + * offsets to be reasonably constant
2175 + list_add(&middle, rdd.list);
2176 + rdd.middle = &middle;
2177 + rdd.is_merge = true;
2178 + err = ovl_dir_read(lowerpath, &rdd);
2179 + list_del(&middle);
2184 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
2186 + struct list_head *l;
2189 + l = od->cache.next;
2190 + for (off = 0; off < pos; off++) {
2191 + if (l == &od->cache)
2195 + list_move_tail(&od->cursor, l);
2198 +static int ovl_iterate(struct file *file, struct dir_context *ctx)
2200 + struct ovl_dir_file *od = file->private_data;
2204 + ovl_dir_reset(file);
2206 + if (od->is_real) {
2207 + res = iterate_dir(od->realfile, ctx);
2212 + if (!od->is_cached) {
2213 + struct path lowerpath;
2214 + struct path upperpath;
2216 + ovl_path_lower(file->f_path.dentry, &lowerpath);
2217 + ovl_path_upper(file->f_path.dentry, &upperpath);
2219 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &od->cache);
2221 + ovl_cache_free(&od->cache);
2225 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
2226 + od->is_cached = true;
2228 + ovl_seek_cursor(od, ctx->pos);
2231 + while (od->cursor.next != &od->cache) {
2232 + struct ovl_cache_entry *p;
2234 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
2235 + if (!p->is_whiteout) {
2236 + if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
2240 + list_move(&od->cursor, &p->l_node);
2246 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
2249 + struct ovl_dir_file *od = file->private_data;
2251 + mutex_lock(&file_inode(file)->i_mutex);
2253 + ovl_dir_reset(file);
2255 + if (od->is_real) {
2256 + res = vfs_llseek(od->realfile, offset, origin);
2257 + file->f_pos = od->realfile->f_pos;
2263 + offset += file->f_pos;
2273 + if (offset != file->f_pos) {
2274 + file->f_pos = offset;
2275 + if (od->is_cached)
2276 + ovl_seek_cursor(od, offset);
2281 + mutex_unlock(&file_inode(file)->i_mutex);
2286 +static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
2289 + struct ovl_dir_file *od = file->private_data;
2291 + /* May need to reopen directory if it got copied up */
2292 + if (!od->realfile) {
2293 + struct path upperpath;
2295 + ovl_path_upper(file->f_path.dentry, &upperpath);
2296 + od->realfile = ovl_path_open(&upperpath, O_RDONLY);
2297 + if (IS_ERR(od->realfile))
2298 + return PTR_ERR(od->realfile);
2301 + return vfs_fsync_range(od->realfile, start, end, datasync);
2304 +static int ovl_dir_release(struct inode *inode, struct file *file)
2306 + struct ovl_dir_file *od = file->private_data;
2308 + list_del(&od->cursor);
2309 + ovl_cache_free(&od->cache);
2311 + fput(od->realfile);
2317 +static int ovl_dir_open(struct inode *inode, struct file *file)
2319 + struct path realpath;
2320 + struct file *realfile;
2321 + struct ovl_dir_file *od;
2322 + enum ovl_path_type type;
2324 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
2328 + type = ovl_path_real(file->f_path.dentry, &realpath);
2329 + realfile = ovl_path_open(&realpath, file->f_flags);
2330 + if (IS_ERR(realfile)) {
2332 + return PTR_ERR(realfile);
2334 + INIT_LIST_HEAD(&od->cache);
2335 + INIT_LIST_HEAD(&od->cursor);
2336 + od->is_cached = false;
2337 + od->realfile = realfile;
2338 + od->is_real = (type != OVL_PATH_MERGE);
2339 + file->private_data = od;
2344 +const struct file_operations ovl_dir_operations = {
2345 + .read = generic_read_dir,
2346 + .open = ovl_dir_open,
2347 + .iterate = ovl_iterate,
2348 + .llseek = ovl_dir_llseek,
2349 + .fsync = ovl_dir_fsync,
2350 + .release = ovl_dir_release,
2353 +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
2356 + struct path lowerpath;
2357 + struct path upperpath;
2358 + struct ovl_cache_entry *p;
2360 + ovl_path_upper(dentry, &upperpath);
2361 + ovl_path_lower(dentry, &lowerpath);
2363 + err = ovl_dir_read_merged(&upperpath, &lowerpath, list);
2369 + list_for_each_entry(p, list, l_node) {
2370 + if (p->is_whiteout)
2373 + if (p->name[0] == '.') {
2376 + if (p->len == 2 && p->name[1] == '.')
2386 +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
2388 + struct path upperpath;
2389 + struct dentry *upperdir;
2390 + struct ovl_cache_entry *p;
2391 + const struct cred *old_cred;
2392 + struct cred *override_cred;
2395 + ovl_path_upper(dir, &upperpath);
2396 + upperdir = upperpath.dentry;
2398 + override_cred = prepare_creds();
2399 + if (!override_cred)
2403 + * CAP_DAC_OVERRIDE for lookup and unlink
2404 + * CAP_SYS_ADMIN for setxattr of "trusted" namespace
2405 + * CAP_FOWNER for unlink in sticky directory
2407 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2408 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2409 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
2410 + old_cred = override_creds(override_cred);
2412 + err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
2414 + goto out_revert_creds;
2416 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2417 + list_for_each_entry(p, list, l_node) {
2418 + struct dentry *dentry;
2421 + if (!p->is_whiteout)
2424 + dentry = lookup_one_len(p->name, upperdir, p->len);
2425 + if (IS_ERR(dentry)) {
2427 + "overlayfs: failed to lookup whiteout %.*s: %li\n",
2428 + p->len, p->name, PTR_ERR(dentry));
2431 + ret = vfs_unlink(upperdir->d_inode, dentry);
2435 + "overlayfs: failed to unlink whiteout %.*s: %i\n",
2436 + p->len, p->name, ret);
2438 + mutex_unlock(&upperdir->d_inode->i_mutex);
2441 + revert_creds(old_cred);
2442 + put_cred(override_cred);
2447 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
2452 + err = ovl_check_empty_dir(dentry, &list);
2453 + if (!err && type == OVL_PATH_MERGE)
2454 + err = ovl_remove_whiteouts(dentry, &list);
2456 + ovl_cache_free(&list);
2461 +++ b/fs/overlayfs/super.c
2465 + * Copyright (C) 2011 Novell Inc.
2467 + * This program is free software; you can redistribute it and/or modify it
2468 + * under the terms of the GNU General Public License version 2 as published by
2469 + * the Free Software Foundation.
2472 +#include <linux/fs.h>
2473 +#include <linux/namei.h>
2474 +#include <linux/xattr.h>
2475 +#include <linux/security.h>
2476 +#include <linux/mount.h>
2477 +#include <linux/slab.h>
2478 +#include <linux/parser.h>
2479 +#include <linux/module.h>
2480 +#include <linux/cred.h>
2481 +#include <linux/sched.h>
2482 +#include <linux/statfs.h>
2483 +#include <linux/seq_file.h>
2484 +#include "overlayfs.h"
2486 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2487 +MODULE_DESCRIPTION("Overlay filesystem");
2488 +MODULE_LICENSE("GPL");
2490 +#define OVERLAYFS_SUPER_MAGIC 0x794c764f
2492 +struct ovl_config {
2497 +/* private information held for overlayfs's superblock */
2499 + struct vfsmount *upper_mnt;
2500 + struct vfsmount *lower_mnt;
2501 + long lower_namelen;
2502 + /* pathnames of lower and upper dirs, for show_options */
2503 + struct ovl_config config;
2506 +/* private information held for every overlayfs dentry */
2509 + * Keep "double reference" on upper dentries, so that
2510 + * d_delete() doesn't think it's OK to reset d_inode to NULL.
2512 + struct dentry *__upperdentry;
2513 + struct dentry *lowerdentry;
2519 + struct rcu_head rcu;
2523 +const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
2524 +const char *ovl_opaque_xattr = "trusted.overlay.opaque";
2527 +enum ovl_path_type ovl_path_type(struct dentry *dentry)
2529 + struct ovl_entry *oe = dentry->d_fsdata;
2531 + if (oe->__upperdentry) {
2532 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
2533 + return OVL_PATH_MERGE;
2535 + return OVL_PATH_UPPER;
2537 + return OVL_PATH_LOWER;
2541 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
2543 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
2544 + smp_read_barrier_depends();
2545 + return upperdentry;
2548 +void ovl_path_upper(struct dentry *dentry, struct path *path)
2550 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2551 + struct ovl_entry *oe = dentry->d_fsdata;
2553 + path->mnt = ofs->upper_mnt;
2554 + path->dentry = ovl_upperdentry_dereference(oe);
2557 +void ovl_path_lower(struct dentry *dentry, struct path *path)
2559 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2560 + struct ovl_entry *oe = dentry->d_fsdata;
2562 + path->mnt = ofs->lower_mnt;
2563 + path->dentry = oe->lowerdentry;
2566 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
2569 + enum ovl_path_type type = ovl_path_type(dentry);
2571 + if (type == OVL_PATH_LOWER)
2572 + ovl_path_lower(dentry, path);
2574 + ovl_path_upper(dentry, path);
2579 +struct dentry *ovl_dentry_upper(struct dentry *dentry)
2581 + struct ovl_entry *oe = dentry->d_fsdata;
2583 + return ovl_upperdentry_dereference(oe);
2586 +struct dentry *ovl_dentry_lower(struct dentry *dentry)
2588 + struct ovl_entry *oe = dentry->d_fsdata;
2590 + return oe->lowerdentry;
2593 +struct dentry *ovl_dentry_real(struct dentry *dentry)
2595 + struct ovl_entry *oe = dentry->d_fsdata;
2596 + struct dentry *realdentry;
2598 + realdentry = ovl_upperdentry_dereference(oe);
2600 + realdentry = oe->lowerdentry;
2602 + return realdentry;
2605 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
2607 + struct dentry *realdentry;
2609 + realdentry = ovl_upperdentry_dereference(oe);
2613 + realdentry = oe->lowerdentry;
2614 + *is_upper = false;
2616 + return realdentry;
2619 +bool ovl_dentry_is_opaque(struct dentry *dentry)
2621 + struct ovl_entry *oe = dentry->d_fsdata;
2622 + return oe->opaque;
2625 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
2627 + struct ovl_entry *oe = dentry->d_fsdata;
2628 + oe->opaque = opaque;
2631 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
2633 + struct ovl_entry *oe = dentry->d_fsdata;
2635 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
2636 + WARN_ON(oe->__upperdentry);
2637 + BUG_ON(!upperdentry->d_inode);
2639 + oe->__upperdentry = dget(upperdentry);
2642 +void ovl_dentry_version_inc(struct dentry *dentry)
2644 + struct ovl_entry *oe = dentry->d_fsdata;
2646 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2650 +u64 ovl_dentry_version_get(struct dentry *dentry)
2652 + struct ovl_entry *oe = dentry->d_fsdata;
2654 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2655 + return oe->version;
2658 +bool ovl_is_whiteout(struct dentry *dentry)
2665 + if (!dentry->d_inode)
2667 + if (!S_ISLNK(dentry->d_inode->i_mode))
2670 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
2671 + if (res == 1 && val == 'y')
2677 +static bool ovl_is_opaquedir(struct dentry *dentry)
2682 + if (!S_ISDIR(dentry->d_inode->i_mode))
2685 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
2686 + if (res == 1 && val == 'y')
2692 +static void ovl_entry_free(struct rcu_head *head)
2694 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
2698 +static void ovl_dentry_release(struct dentry *dentry)
2700 + struct ovl_entry *oe = dentry->d_fsdata;
2703 + dput(oe->__upperdentry);
2704 + dput(oe->__upperdentry);
2705 + dput(oe->lowerdentry);
2706 + call_rcu(&oe->rcu, ovl_entry_free);
2710 +const struct dentry_operations ovl_dentry_operations = {
2711 + .d_release = ovl_dentry_release,
2714 +static struct ovl_entry *ovl_alloc_entry(void)
2716 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
2719 +static inline struct dentry *ovl_lookup_real(struct dentry *dir,
2720 + struct qstr *name)
2722 + struct dentry *dentry;
2724 + mutex_lock(&dir->d_inode->i_mutex);
2725 + dentry = lookup_one_len(name->name, dir, name->len);
2726 + mutex_unlock(&dir->d_inode->i_mutex);
2728 + if (IS_ERR(dentry)) {
2729 + if (PTR_ERR(dentry) == -ENOENT)
2731 + } else if (!dentry->d_inode) {
2738 +static int ovl_do_lookup(struct dentry *dentry)
2740 + struct ovl_entry *oe;
2741 + struct dentry *upperdir;
2742 + struct dentry *lowerdir;
2743 + struct dentry *upperdentry = NULL;
2744 + struct dentry *lowerdentry = NULL;
2745 + struct inode *inode = NULL;
2749 + oe = ovl_alloc_entry();
2753 + upperdir = ovl_dentry_upper(dentry->d_parent);
2754 + lowerdir = ovl_dentry_lower(dentry->d_parent);
2757 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
2758 + err = PTR_ERR(upperdentry);
2759 + if (IS_ERR(upperdentry))
2762 + if (lowerdir && upperdentry &&
2763 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
2764 + S_ISDIR(upperdentry->d_inode->i_mode))) {
2765 + const struct cred *old_cred;
2766 + struct cred *override_cred;
2769 + override_cred = prepare_creds();
2770 + if (!override_cred)
2771 + goto out_dput_upper;
2773 + /* CAP_SYS_ADMIN needed for getxattr */
2774 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2775 + old_cred = override_creds(override_cred);
2777 + if (ovl_is_opaquedir(upperdentry)) {
2778 + oe->opaque = true;
2779 + } else if (ovl_is_whiteout(upperdentry)) {
2780 + dput(upperdentry);
2781 + upperdentry = NULL;
2782 + oe->opaque = true;
2784 + revert_creds(old_cred);
2785 + put_cred(override_cred);
2788 + if (lowerdir && !oe->opaque) {
2789 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
2790 + err = PTR_ERR(lowerdentry);
2791 + if (IS_ERR(lowerdentry))
2792 + goto out_dput_upper;
2795 + if (lowerdentry && upperdentry &&
2796 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
2797 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
2798 + dput(lowerdentry);
2799 + lowerdentry = NULL;
2800 + oe->opaque = true;
2803 + if (lowerdentry || upperdentry) {
2804 + struct dentry *realdentry;
2806 + realdentry = upperdentry ? upperdentry : lowerdentry;
2808 + inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
2812 + ovl_copyattr(realdentry->d_inode, inode);
2816 + oe->__upperdentry = dget(upperdentry);
2819 + oe->lowerdentry = lowerdentry;
2821 + dentry->d_fsdata = oe;
2822 + dentry->d_op = &ovl_dentry_operations;
2823 + d_add(dentry, inode);
2828 + dput(lowerdentry);
2830 + dput(upperdentry);
2837 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
2838 + unsigned int flags)
2840 + int err = ovl_do_lookup(dentry);
2843 + return ERR_PTR(err);
2848 +struct file *ovl_path_open(struct path *path, int flags)
2850 + return dentry_open(path, flags, current_cred());
2853 +static void ovl_put_super(struct super_block *sb)
2855 + struct ovl_fs *ufs = sb->s_fs_info;
2857 + if (!(sb->s_flags & MS_RDONLY))
2858 + mnt_drop_write(ufs->upper_mnt);
2860 + mntput(ufs->upper_mnt);
2861 + mntput(ufs->lower_mnt);
2863 + kfree(ufs->config.lowerdir);
2864 + kfree(ufs->config.upperdir);
2868 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2870 + int flags = *flagsp;
2871 + struct ovl_fs *ufs = sb->s_fs_info;
2873 + /* When remounting rw or ro, we need to adjust the write access to the
2876 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2877 + /* No change to readonly status */
2880 + if (flags & MS_RDONLY) {
2881 + mnt_drop_write(ufs->upper_mnt);
2884 + return mnt_want_write(ufs->upper_mnt);
2889 + * @sb: The overlayfs super block
2890 + * @buf: The struct kstatfs to fill in with stats
2892 + * Get the filesystem statistics. As writes always target the upper layer
2893 + * filesystem pass the statfs to the same filesystem.
2895 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2897 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2898 + struct dentry *root_dentry = dentry->d_sb->s_root;
2902 + ovl_path_upper(root_dentry, &path);
2904 + err = vfs_statfs(&path, buf);
2906 + buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
2907 + buf->f_type = OVERLAYFS_SUPER_MAGIC;
2914 + * ovl_show_options
2916 + * Prints the mount options for a given superblock.
2917 + * Returns zero; does not fail.
2919 +static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
2921 + struct super_block *sb = dentry->d_sb;
2922 + struct ovl_fs *ufs = sb->s_fs_info;
2924 + seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
2925 + seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
2929 +static const struct super_operations ovl_super_operations = {
2930 + .put_super = ovl_put_super,
2931 + .remount_fs = ovl_remount_fs,
2932 + .statfs = ovl_statfs,
2933 + .show_options = ovl_show_options,
2942 +static const match_table_t ovl_tokens = {
2943 + {OPT_LOWERDIR, "lowerdir=%s"},
2944 + {OPT_UPPERDIR, "upperdir=%s"},
2948 +static int ovl_parse_opt(char *opt, struct ovl_config *config)
2952 + config->upperdir = NULL;
2953 + config->lowerdir = NULL;
2955 + while ((p = strsep(&opt, ",")) != NULL) {
2957 + substring_t args[MAX_OPT_ARGS];
2962 + token = match_token(p, ovl_tokens, args);
2964 + case OPT_UPPERDIR:
2965 + kfree(config->upperdir);
2966 + config->upperdir = match_strdup(&args[0]);
2967 + if (!config->upperdir)
2971 + case OPT_LOWERDIR:
2972 + kfree(config->lowerdir);
2973 + config->lowerdir = match_strdup(&args[0]);
2974 + if (!config->lowerdir)
2985 +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
2987 + struct path lowerpath;
2988 + struct path upperpath;
2989 + struct inode *root_inode;
2990 + struct dentry *root_dentry;
2991 + struct ovl_entry *oe;
2992 + struct ovl_fs *ufs;
2993 + struct kstatfs statfs;
2997 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
3001 + err = ovl_parse_opt((char *) data, &ufs->config);
3003 + goto out_free_ufs;
3006 + if (!ufs->config.upperdir || !ufs->config.lowerdir) {
3007 + pr_err("overlayfs: missing upperdir or lowerdir\n");
3008 + goto out_free_config;
3011 + oe = ovl_alloc_entry();
3013 + goto out_free_config;
3015 + err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
3019 + err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
3021 + goto out_put_upperpath;
3024 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
3025 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
3026 + goto out_put_lowerpath;
3028 + err = vfs_statfs(&lowerpath, &statfs);
3030 + pr_err("overlayfs: statfs failed on lowerpath\n");
3031 + goto out_put_lowerpath;
3033 + ufs->lower_namelen = statfs.f_namelen;
3035 + sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
3036 + lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
3039 + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
3040 + pr_err("overlayfs: maximum fs stacking depth exceeded\n");
3041 + goto out_put_lowerpath;
3045 + ufs->upper_mnt = clone_private_mount(&upperpath);
3046 + err = PTR_ERR(ufs->upper_mnt);
3047 + if (IS_ERR(ufs->upper_mnt)) {
3048 + pr_err("overlayfs: failed to clone upperpath\n");
3049 + goto out_put_lowerpath;
3052 + ufs->lower_mnt = clone_private_mount(&lowerpath);
3053 + err = PTR_ERR(ufs->lower_mnt);
3054 + if (IS_ERR(ufs->lower_mnt)) {
3055 + pr_err("overlayfs: failed to clone lowerpath\n");
3056 + goto out_put_upper_mnt;
3060 + * Make lower_mnt R/O. That way fchmod/fchown on lower file
3061 + * will fail instead of modifying lower fs.
3063 + ufs->lower_mnt->mnt_flags |= MNT_READONLY;
3065 + /* If the upper fs is r/o, we mark overlayfs r/o too */
3066 + if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
3067 + sb->s_flags |= MS_RDONLY;
3069 + if (!(sb->s_flags & MS_RDONLY)) {
3070 + err = mnt_want_write(ufs->upper_mnt);
3072 + goto out_put_lower_mnt;
3076 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
3078 + goto out_drop_write;
3080 + root_dentry = d_make_root(root_inode);
3082 + goto out_drop_write;
3084 + mntput(upperpath.mnt);
3085 + mntput(lowerpath.mnt);
3087 + oe->__upperdentry = dget(upperpath.dentry);
3088 + oe->lowerdentry = lowerpath.dentry;
3090 + root_dentry->d_fsdata = oe;
3091 + root_dentry->d_op = &ovl_dentry_operations;
3093 + sb->s_magic = OVERLAYFS_SUPER_MAGIC;
3094 + sb->s_op = &ovl_super_operations;
3095 + sb->s_root = root_dentry;
3096 + sb->s_fs_info = ufs;
3101 + if (!(sb->s_flags & MS_RDONLY))
3102 + mnt_drop_write(ufs->upper_mnt);
3104 + mntput(ufs->lower_mnt);
3106 + mntput(ufs->upper_mnt);
3108 + path_put(&lowerpath);
3110 + path_put(&upperpath);
3114 + kfree(ufs->config.lowerdir);
3115 + kfree(ufs->config.upperdir);
3122 +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
3123 + const char *dev_name, void *raw_data)
3125 + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
3128 +static struct file_system_type ovl_fs_type = {
3129 + .owner = THIS_MODULE,
3130 + .name = "overlayfs",
3131 + .mount = ovl_mount,
3132 + .kill_sb = kill_anon_super,
3134 +MODULE_ALIAS_FS("overlayfs");
3136 +static int __init ovl_init(void)
3138 + return register_filesystem(&ovl_fs_type);
3141 +static void __exit ovl_exit(void)
3143 + unregister_filesystem(&ovl_fs_type);
3146 +module_init(ovl_init);
3147 +module_exit(ovl_exit);
3150 @@ -1309,6 +1309,7 @@ long do_splice_direct(struct file *in, l
3154 +EXPORT_SYMBOL(do_splice_direct);
3156 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
3157 struct pipe_inode_info *opipe,
3158 --- a/include/linux/fs.h
3159 +++ b/include/linux/fs.h
3160 @@ -246,6 +246,12 @@ struct iattr {
3162 #include <linux/quota.h>
3165 + * Maximum number of layers of fs stack. Needs to be limited to
3166 + * prevent kernel stack overflow
3168 +#define FILESYSTEM_MAX_STACK_DEPTH 2
3171 * enum positive_aop_returns - aop return codes with specific semantics
3173 @@ -1330,6 +1336,11 @@ struct super_block {
3175 struct list_lru s_dentry_lru ____cacheline_aligned_in_smp;
3176 struct list_lru s_inode_lru ____cacheline_aligned_in_smp;
3179 + * Indicates how deep in a filesystem stack this SB is
3181 + int s_stack_depth;
3184 extern struct timespec current_fs_time(struct super_block *sb);
3185 @@ -1585,6 +1596,7 @@ struct inode_operations {
3186 struct file *, unsigned open_flag,
3187 umode_t create_mode, int *opened);
3188 int (*tmpfile) (struct inode *, struct dentry *, umode_t);
3189 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
3190 } ____cacheline_aligned;
3192 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
3193 @@ -2019,6 +2031,7 @@ extern struct file *file_open_name(struc
3194 extern struct file *filp_open(const char *, int, umode_t);
3195 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
3197 +extern int vfs_open(const struct path *, struct file *, const struct cred *);
3198 extern struct file * dentry_open(const struct path *, int, const struct cred *);
3199 extern int filp_close(struct file *, fl_owner_t id);
3201 @@ -2225,6 +2238,7 @@ extern sector_t bmap(struct inode *, sec
3203 extern int notify_change(struct dentry *, struct iattr *);
3204 extern int inode_permission(struct inode *, int);
3205 +extern int __inode_permission(struct inode *, int);
3206 extern int generic_permission(struct inode *, int);
3208 static inline bool execute_ok(struct inode *inode)
3209 @@ -2430,6 +2444,9 @@ extern ssize_t generic_file_splice_write
3210 struct file *, loff_t *, size_t, unsigned int);
3211 extern ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe,
3212 struct file *out, loff_t *, size_t len, unsigned int flags);
3213 +extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
3214 + loff_t *opos, size_t len, unsigned int flags);
3218 file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping);
3219 --- a/include/linux/mount.h
3220 +++ b/include/linux/mount.h
3221 @@ -69,6 +69,9 @@ extern void mnt_pin(struct vfsmount *mnt
3222 extern void mnt_unpin(struct vfsmount *mnt);
3223 extern int __mnt_is_readonly(struct vfsmount *mnt);
3226 +extern struct vfsmount *clone_private_mount(struct path *path);
3228 struct file_system_type;
3229 extern struct vfsmount *vfs_kern_mount(struct file_system_type *type,
3230 int flags, const char *name,