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 @@ -5911,6 +5911,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 @@ -107,6 +107,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 + printk(KERN_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 *);
312 @@ -402,6 +402,7 @@ int __inode_permission(struct inode *ino
314 return security_inode_permission(inode, mask);
316 +EXPORT_SYMBOL(__inode_permission);
319 * sb_permission - Check superblock-level permissions
320 @@ -2867,9 +2868,12 @@ finish_open_created:
321 error = may_open(&nd->path, acc_mode, open_flag);
324 - file->f_path.mnt = nd->path.mnt;
325 - error = finish_open(file, nd->path.dentry, NULL, opened);
328 + BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
329 + error = vfs_open(&nd->path, file, current_cred());
331 + *opened |= FILE_OPENED;
333 if (error == -EOPENSTALE)
338 @@ -1403,6 +1403,24 @@ void drop_collected_mounts(struct vfsmou
339 release_mounts(&umount_list);
342 +struct vfsmount *clone_private_mount(struct path *path)
344 + struct mount *old_mnt = real_mount(path->mnt);
345 + struct mount *new_mnt;
347 + if (IS_MNT_UNBINDABLE(old_mnt))
348 + return ERR_PTR(-EINVAL);
350 + down_read(&namespace_sem);
351 + new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
352 + up_read(&namespace_sem);
354 + return ERR_PTR(-ENOMEM);
356 + return &new_mnt->mnt;
358 +EXPORT_SYMBOL_GPL(clone_private_mount);
360 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
361 struct vfsmount *root)
365 @@ -828,8 +828,7 @@ struct file *dentry_open(const struct pa
366 f = get_empty_filp();
370 - error = do_dentry_open(f, NULL, cred);
371 + error = vfs_open(path, f, cred);
373 /* from now on we need fput() to dispose of f */
374 error = open_check_o_direct(f);
375 @@ -846,6 +845,26 @@ struct file *dentry_open(const struct pa
377 EXPORT_SYMBOL(dentry_open);
380 + * vfs_open - open the file at the given path
381 + * @path: path to open
382 + * @filp: newly allocated file with f_flag initialized
383 + * @cred: credentials to use
385 +int vfs_open(const struct path *path, struct file *filp,
386 + const struct cred *cred)
388 + struct inode *inode = path->dentry->d_inode;
390 + if (inode->i_op->dentry_open)
391 + return inode->i_op->dentry_open(path->dentry, filp, cred);
393 + filp->f_path = *path;
394 + return do_dentry_open(filp, NULL, cred);
397 +EXPORT_SYMBOL(vfs_open);
399 static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
401 int lookup_flags = 0;
403 +++ b/fs/overlayfs/Kconfig
406 + tristate "Overlay filesystem support"
408 + Add support for overlay filesystem.
410 +++ b/fs/overlayfs/Makefile
413 +# Makefile for the overlay filesystem.
416 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
418 +overlayfs-objs := super.o inode.o dir.o readdir.o copy_up.o
420 +++ b/fs/overlayfs/copy_up.c
424 + * Copyright (C) 2011 Novell Inc.
426 + * This program is free software; you can redistribute it and/or modify it
427 + * under the terms of the GNU General Public License version 2 as published by
428 + * the Free Software Foundation.
431 +#include <linux/fs.h>
432 +#include <linux/slab.h>
433 +#include <linux/file.h>
434 +#include <linux/splice.h>
435 +#include <linux/xattr.h>
436 +#include <linux/security.h>
437 +#include <linux/uaccess.h>
438 +#include <linux/sched.h>
439 +#include "overlayfs.h"
441 +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
443 +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
445 + ssize_t list_size, size;
446 + char *buf, *name, *value;
449 + if (!old->d_inode->i_op->getxattr ||
450 + !new->d_inode->i_op->getxattr)
453 + list_size = vfs_listxattr(old, NULL, 0);
454 + if (list_size <= 0) {
455 + if (list_size == -EOPNOTSUPP)
460 + buf = kzalloc(list_size, GFP_KERNEL);
465 + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
469 + list_size = vfs_listxattr(old, buf, list_size);
470 + if (list_size <= 0) {
472 + goto out_free_value;
475 + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
476 + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
479 + goto out_free_value;
481 + error = vfs_setxattr(new, name, value, size, 0);
483 + goto out_free_value;
493 +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
495 + struct file *old_file;
496 + struct file *new_file;
502 + old_file = ovl_path_open(old, O_RDONLY);
503 + if (IS_ERR(old_file))
504 + return PTR_ERR(old_file);
506 + new_file = ovl_path_open(new, O_WRONLY);
507 + if (IS_ERR(new_file)) {
508 + error = PTR_ERR(new_file);
512 + /* FIXME: copy up sparse files efficiently */
514 + loff_t offset = new_file->f_pos;
515 + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
518 + if (len < this_len)
521 + if (signal_pending_state(TASK_KILLABLE, current)) {
526 + bytes = do_splice_direct(old_file, &offset, new_file, this_len,
542 +static char *ovl_read_symlink(struct dentry *realdentry)
546 + struct inode *inode = realdentry->d_inode;
547 + mm_segment_t old_fs;
550 + if (!inode->i_op->readlink)
554 + buf = (char *) __get_free_page(GFP_KERNEL);
560 + /* The cast to a user pointer is valid due to the set_fs() */
561 + res = inode->i_op->readlink(realdentry,
562 + (char __user *)buf, PAGE_SIZE - 1);
565 + free_page((unsigned long) buf);
573 + return ERR_PTR(res);
576 +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
578 + struct iattr attr = {
580 + ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
581 + .ia_atime = stat->atime,
582 + .ia_mtime = stat->mtime,
585 + return notify_change(upperdentry, &attr);
588 +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
590 + struct iattr attr = {
591 + .ia_valid = ATTR_MODE,
595 + return notify_change(upperdentry, &attr);
598 +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
599 + struct path *lowerpath, struct kstat *stat,
603 + struct path newpath;
604 + umode_t mode = stat->mode;
606 + /* Can't properly set mode on creation because of the umask */
607 + stat->mode &= S_IFMT;
609 + ovl_path_upper(dentry, &newpath);
610 + WARN_ON(newpath.dentry);
611 + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
612 + if (IS_ERR(newpath.dentry))
613 + return PTR_ERR(newpath.dentry);
615 + if (S_ISREG(stat->mode)) {
616 + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
621 + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
625 + mutex_lock(&newpath.dentry->d_inode->i_mutex);
626 + if (!S_ISLNK(stat->mode))
627 + err = ovl_set_mode(newpath.dentry, mode);
629 + err = ovl_set_timestamps(newpath.dentry, stat);
630 + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
634 + ovl_dentry_update(dentry, newpath.dentry);
637 + * Easiest way to get rid of the lower dentry reference is to
638 + * drop this dentry. This is neither needed nor possible for
641 + if (!S_ISDIR(stat->mode))
647 + if (S_ISDIR(stat->mode))
648 + vfs_rmdir(upperdir->d_inode, newpath.dentry);
650 + vfs_unlink(upperdir->d_inode, newpath.dentry);
652 + dput(newpath.dentry);
658 + * Copy up a single dentry
660 + * Directory renames only allowed on "pure upper" (already created on
661 + * upper filesystem, never copied up). Directories which are on lower or
662 + * are merged may not be renamed. For these -EXDEV is returned and
663 + * userspace has to deal with it. This means, when copying up a
664 + * directory we can rely on it and ancestors being stable.
666 + * Non-directory renames start with copy up of source if necessary. The
667 + * actual rename will only proceed once the copy up was successful. Copy
668 + * up uses upper parent i_mutex for exclusion. Since rename can change
669 + * d_parent it is possible that the copy up will lock the old parent. At
670 + * that point the file will have already been copied up anyway.
672 +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
673 + struct path *lowerpath, struct kstat *stat)
676 + struct kstat pstat;
677 + struct path parentpath;
678 + struct dentry *upperdir;
679 + const struct cred *old_cred;
680 + struct cred *override_cred;
683 + ovl_path_upper(parent, &parentpath);
684 + upperdir = parentpath.dentry;
686 + err = vfs_getattr(&parentpath, &pstat);
690 + if (S_ISLNK(stat->mode)) {
691 + link = ovl_read_symlink(lowerpath->dentry);
693 + return PTR_ERR(link);
697 + override_cred = prepare_creds();
698 + if (!override_cred)
699 + goto out_free_link;
701 + override_cred->fsuid = stat->uid;
702 + override_cred->fsgid = stat->gid;
704 + * CAP_SYS_ADMIN for copying up extended attributes
705 + * CAP_DAC_OVERRIDE for create
706 + * CAP_FOWNER for chmod, timestamp update
707 + * CAP_FSETID for chmod
708 + * CAP_MKNOD for mknod
710 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
711 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
712 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
713 + cap_raise(override_cred->cap_effective, CAP_FSETID);
714 + cap_raise(override_cred->cap_effective, CAP_MKNOD);
715 + old_cred = override_creds(override_cred);
717 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
718 + if (ovl_path_type(dentry) != OVL_PATH_LOWER) {
721 + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
724 + /* Restore timestamps on parent (best effort) */
725 + ovl_set_timestamps(upperdir, &pstat);
729 + mutex_unlock(&upperdir->d_inode->i_mutex);
731 + revert_creds(old_cred);
732 + put_cred(override_cred);
736 + free_page((unsigned long) link);
741 +int ovl_copy_up(struct dentry *dentry)
747 + struct dentry *next;
748 + struct dentry *parent;
749 + struct path lowerpath;
751 + enum ovl_path_type type = ovl_path_type(dentry);
753 + if (type != OVL_PATH_LOWER)
756 + next = dget(dentry);
757 + /* find the topmost dentry not yet copied up */
759 + parent = dget_parent(next);
761 + type = ovl_path_type(parent);
762 + if (type != OVL_PATH_LOWER)
769 + ovl_path_lower(next, &lowerpath);
770 + err = vfs_getattr(&lowerpath, &stat);
772 + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
781 +/* Optimize by not copying up the file first and truncating later */
782 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
786 + struct path lowerpath;
787 + struct dentry *parent = dget_parent(dentry);
789 + err = ovl_copy_up(parent);
791 + goto out_dput_parent;
793 + ovl_path_lower(dentry, &lowerpath);
794 + err = vfs_getattr(&lowerpath, &stat);
796 + goto out_dput_parent;
798 + if (size < stat.size)
801 + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
808 +++ b/fs/overlayfs/dir.c
812 + * Copyright (C) 2011 Novell Inc.
814 + * This program is free software; you can redistribute it and/or modify it
815 + * under the terms of the GNU General Public License version 2 as published by
816 + * the Free Software Foundation.
819 +#include <linux/fs.h>
820 +#include <linux/namei.h>
821 +#include <linux/xattr.h>
822 +#include <linux/security.h>
823 +#include <linux/cred.h>
824 +#include "overlayfs.h"
826 +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
828 +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
831 + struct dentry *newdentry;
832 + const struct cred *old_cred;
833 + struct cred *override_cred;
835 + /* FIXME: recheck lower dentry to see if whiteout is really needed */
838 + override_cred = prepare_creds();
839 + if (!override_cred)
843 + * CAP_SYS_ADMIN for setxattr
844 + * CAP_DAC_OVERRIDE for symlink creation
845 + * CAP_FOWNER for unlink in sticky directory
847 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
848 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
849 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
850 + override_cred->fsuid = GLOBAL_ROOT_UID;
851 + override_cred->fsgid = GLOBAL_ROOT_GID;
852 + old_cred = override_creds(override_cred);
854 + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
855 + dentry->d_name.len);
856 + err = PTR_ERR(newdentry);
857 + if (IS_ERR(newdentry))
860 + /* Just been removed within the same locked region */
861 + WARN_ON(newdentry->d_inode);
863 + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
867 + ovl_dentry_version_inc(dentry->d_parent);
869 + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
871 + vfs_unlink(upperdir->d_inode, newdentry);
876 + revert_creds(old_cred);
877 + put_cred(override_cred);
881 + * There's no way to recover from failure to whiteout.
882 + * What should we do? Log a big fat error and... ?
884 + printk(KERN_ERR "overlayfs: ERROR - failed to whiteout '%s'\n",
885 + dentry->d_name.name);
891 +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
892 + struct dentry *template)
895 + struct dentry *newdentry;
896 + struct qstr *name = &template->d_name;
898 + newdentry = lookup_one_len(name->name, upperdir, name->len);
899 + if (IS_ERR(newdentry))
902 + if (newdentry->d_inode) {
903 + const struct cred *old_cred;
904 + struct cred *override_cred;
906 + /* No need to check whiteout if lower parent is non-existent */
908 + if (!ovl_dentry_lower(template->d_parent))
911 + if (!S_ISLNK(newdentry->d_inode->i_mode))
915 + override_cred = prepare_creds();
916 + if (!override_cred)
920 + * CAP_SYS_ADMIN for getxattr
921 + * CAP_FOWNER for unlink in sticky directory
923 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
924 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
925 + old_cred = override_creds(override_cred);
928 + if (ovl_is_whiteout(newdentry))
929 + err = vfs_unlink(upperdir->d_inode, newdentry);
931 + revert_creds(old_cred);
932 + put_cred(override_cred);
937 + newdentry = lookup_one_len(name->name, upperdir, name->len);
938 + if (IS_ERR(newdentry)) {
939 + ovl_whiteout(upperdir, template);
944 + * Whiteout just been successfully removed, parent
945 + * i_mutex is still held, there's no way the lookup
946 + * could return positive.
948 + WARN_ON(newdentry->d_inode);
955 + return ERR_PTR(err);
958 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
959 + struct kstat *stat, const char *link)
962 + struct dentry *newdentry;
963 + struct inode *dir = upperdir->d_inode;
965 + newdentry = ovl_lookup_create(upperdir, dentry);
966 + if (IS_ERR(newdentry))
969 + switch (stat->mode & S_IFMT) {
971 + err = vfs_create(dir, newdentry, stat->mode, NULL);
975 + err = vfs_mkdir(dir, newdentry, stat->mode);
982 + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
986 + err = vfs_symlink(dir, newdentry, link);
993 + if (ovl_dentry_is_opaque(dentry))
994 + ovl_whiteout(upperdir, dentry);
996 + newdentry = ERR_PTR(err);
997 + } else if (WARN_ON(!newdentry->d_inode)) {
999 + * Not quite sure if non-instantiated dentry is legal or not.
1000 + * VFS doesn't seem to care so check and warn here.
1003 + newdentry = ERR_PTR(-ENOENT);
1011 +static int ovl_set_opaque(struct dentry *upperdentry)
1014 + const struct cred *old_cred;
1015 + struct cred *override_cred;
1017 + override_cred = prepare_creds();
1018 + if (!override_cred)
1021 + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
1022 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1023 + old_cred = override_creds(override_cred);
1024 + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
1025 + revert_creds(old_cred);
1026 + put_cred(override_cred);
1031 +static int ovl_remove_opaque(struct dentry *upperdentry)
1034 + const struct cred *old_cred;
1035 + struct cred *override_cred;
1037 + override_cred = prepare_creds();
1038 + if (!override_cred)
1041 + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
1042 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1043 + old_cred = override_creds(override_cred);
1044 + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
1045 + revert_creds(old_cred);
1046 + put_cred(override_cred);
1051 +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1052 + struct kstat *stat)
1055 + enum ovl_path_type type;
1056 + struct path realpath;
1058 + type = ovl_path_real(dentry, &realpath);
1059 + err = vfs_getattr(&realpath, stat);
1063 + stat->dev = dentry->d_sb->s_dev;
1064 + stat->ino = dentry->d_inode->i_ino;
1067 + * It's probably not worth it to count subdirs to get the
1068 + * correct link count. nlink=1 seems to pacify 'find' and
1069 + * other utilities.
1071 + if (type == OVL_PATH_MERGE)
1077 +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
1081 + struct dentry *newdentry;
1082 + struct dentry *upperdir;
1083 + struct inode *inode;
1084 + struct kstat stat = {
1090 + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
1094 + err = ovl_copy_up(dentry->d_parent);
1098 + upperdir = ovl_dentry_upper(dentry->d_parent);
1099 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1101 + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
1102 + err = PTR_ERR(newdentry);
1103 + if (IS_ERR(newdentry))
1106 + ovl_dentry_version_inc(dentry->d_parent);
1107 + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
1108 + err = ovl_set_opaque(newdentry);
1110 + vfs_rmdir(upperdir->d_inode, newdentry);
1111 + ovl_whiteout(upperdir, dentry);
1115 + ovl_dentry_update(dentry, newdentry);
1116 + ovl_copyattr(newdentry->d_inode, inode);
1117 + d_instantiate(dentry, inode);
1125 + mutex_unlock(&upperdir->d_inode->i_mutex);
1132 +static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1135 + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
1138 +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1140 + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
1143 +static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1146 + return ovl_create_object(dentry, mode, rdev, NULL);
1149 +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
1152 + return ovl_create_object(dentry, S_IFLNK, 0, link);
1155 +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
1158 + enum ovl_path_type type;
1159 + struct path realpath;
1160 + struct dentry *upperdir;
1162 + err = ovl_copy_up(dentry->d_parent);
1166 + upperdir = ovl_dentry_upper(dentry->d_parent);
1167 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1168 + type = ovl_path_real(dentry, &realpath);
1169 + if (type != OVL_PATH_LOWER) {
1171 + if (realpath.dentry->d_parent != upperdir)
1174 + /* FIXME: create whiteout up front and rename to target */
1177 + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
1179 + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
1183 + ovl_dentry_version_inc(dentry->d_parent);
1186 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1187 + err = ovl_whiteout(upperdir, dentry);
1190 + * Keeping this dentry hashed would mean having to release
1191 + * upperpath/lowerpath, which could only be done if we are the
1192 + * sole user of this dentry. Too tricky... Just unhash for
1197 + mutex_unlock(&upperdir->d_inode->i_mutex);
1202 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1204 + return ovl_do_remove(dentry, false);
1208 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
1211 + enum ovl_path_type type;
1213 + type = ovl_path_type(dentry);
1214 + if (type != OVL_PATH_UPPER) {
1215 + err = ovl_check_empty_and_clear(dentry, type);
1220 + return ovl_do_remove(dentry, true);
1223 +static int ovl_link(struct dentry *old, struct inode *newdir,
1224 + struct dentry *new)
1227 + struct dentry *olddentry;
1228 + struct dentry *newdentry;
1229 + struct dentry *upperdir;
1230 + struct inode *newinode;
1232 + err = ovl_copy_up(old);
1236 + err = ovl_copy_up(new->d_parent);
1240 + upperdir = ovl_dentry_upper(new->d_parent);
1241 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1242 + newdentry = ovl_lookup_create(upperdir, new);
1243 + err = PTR_ERR(newdentry);
1244 + if (IS_ERR(newdentry))
1247 + olddentry = ovl_dentry_upper(old);
1248 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
1250 + if (WARN_ON(!newdentry->d_inode)) {
1255 + newinode = ovl_new_inode(old->d_sb, newdentry->d_inode->i_mode,
1259 + ovl_copyattr(upperdir->d_inode, newinode);
1261 + ovl_dentry_version_inc(new->d_parent);
1262 + ovl_dentry_update(new, newdentry);
1264 + d_instantiate(new, newinode);
1267 + if (ovl_dentry_is_opaque(new))
1268 + ovl_whiteout(upperdir, new);
1272 + mutex_unlock(&upperdir->d_inode->i_mutex);
1278 +static int ovl_rename(struct inode *olddir, struct dentry *old,
1279 + struct inode *newdir, struct dentry *new)
1282 + enum ovl_path_type old_type;
1283 + enum ovl_path_type new_type;
1284 + struct dentry *old_upperdir;
1285 + struct dentry *new_upperdir;
1286 + struct dentry *olddentry;
1287 + struct dentry *newdentry;
1288 + struct dentry *trap;
1291 + bool new_create = false;
1292 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
1294 + /* Don't copy up directory trees */
1295 + old_type = ovl_path_type(old);
1296 + if (old_type != OVL_PATH_UPPER && is_dir)
1299 + if (new->d_inode) {
1300 + new_type = ovl_path_type(new);
1302 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
1303 + if (ovl_dentry_lower(old)->d_inode ==
1304 + ovl_dentry_lower(new)->d_inode)
1307 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
1308 + if (ovl_dentry_upper(old)->d_inode ==
1309 + ovl_dentry_upper(new)->d_inode)
1313 + if (new_type != OVL_PATH_UPPER &&
1314 + S_ISDIR(new->d_inode->i_mode)) {
1315 + err = ovl_check_empty_and_clear(new, new_type);
1320 + new_type = OVL_PATH_UPPER;
1323 + err = ovl_copy_up(old);
1327 + err = ovl_copy_up(new->d_parent);
1331 + old_upperdir = ovl_dentry_upper(old->d_parent);
1332 + new_upperdir = ovl_dentry_upper(new->d_parent);
1334 + trap = lock_rename(new_upperdir, old_upperdir);
1336 + olddentry = ovl_dentry_upper(old);
1337 + newdentry = ovl_dentry_upper(new);
1341 + new_create = true;
1342 + newdentry = ovl_lookup_create(new_upperdir, new);
1343 + err = PTR_ERR(newdentry);
1344 + if (IS_ERR(newdentry))
1349 + if (olddentry->d_parent != old_upperdir)
1351 + if (newdentry->d_parent != new_upperdir)
1353 + if (olddentry == trap)
1355 + if (newdentry == trap)
1358 + old_opaque = ovl_dentry_is_opaque(old);
1359 + new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
1361 + if (is_dir && !old_opaque && new_opaque) {
1362 + err = ovl_set_opaque(olddentry);
1367 + err = vfs_rename(old_upperdir->d_inode, olddentry,
1368 + new_upperdir->d_inode, newdentry);
1371 + if (new_create && ovl_dentry_is_opaque(new))
1372 + ovl_whiteout(new_upperdir, new);
1373 + if (is_dir && !old_opaque && new_opaque)
1374 + ovl_remove_opaque(olddentry);
1378 + if (old_type != OVL_PATH_UPPER || old_opaque)
1379 + err = ovl_whiteout(old_upperdir, old);
1380 + if (is_dir && old_opaque && !new_opaque)
1381 + ovl_remove_opaque(olddentry);
1383 + if (old_opaque != new_opaque)
1384 + ovl_dentry_set_opaque(old, new_opaque);
1386 + ovl_dentry_version_inc(old->d_parent);
1387 + ovl_dentry_version_inc(new->d_parent);
1392 + unlock_rename(new_upperdir, old_upperdir);
1396 +const struct inode_operations ovl_dir_inode_operations = {
1397 + .lookup = ovl_lookup,
1398 + .mkdir = ovl_mkdir,
1399 + .symlink = ovl_symlink,
1400 + .unlink = ovl_unlink,
1401 + .rmdir = ovl_rmdir,
1402 + .rename = ovl_rename,
1404 + .setattr = ovl_setattr,
1405 + .create = ovl_create,
1406 + .mknod = ovl_mknod,
1407 + .permission = ovl_permission,
1408 + .getattr = ovl_dir_getattr,
1409 + .setxattr = ovl_setxattr,
1410 + .getxattr = ovl_getxattr,
1411 + .listxattr = ovl_listxattr,
1412 + .removexattr = ovl_removexattr,
1415 +++ b/fs/overlayfs/inode.c
1419 + * Copyright (C) 2011 Novell Inc.
1421 + * This program is free software; you can redistribute it and/or modify it
1422 + * under the terms of the GNU General Public License version 2 as published by
1423 + * the Free Software Foundation.
1426 +#include <linux/fs.h>
1427 +#include <linux/slab.h>
1428 +#include <linux/xattr.h>
1429 +#include "overlayfs.h"
1431 +int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1433 + struct dentry *upperdentry;
1436 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1437 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1439 + err = ovl_copy_up(dentry);
1443 + upperdentry = ovl_dentry_upper(dentry);
1445 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1446 + attr->ia_valid &= ~ATTR_MODE;
1448 + mutex_lock(&upperdentry->d_inode->i_mutex);
1449 + err = notify_change(upperdentry, attr);
1451 + ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
1452 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1457 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1458 + struct kstat *stat)
1460 + struct path realpath;
1462 + ovl_path_real(dentry, &realpath);
1463 + return vfs_getattr(&realpath, stat);
1466 +int ovl_permission(struct inode *inode, int mask)
1468 + struct ovl_entry *oe;
1469 + struct dentry *alias = NULL;
1470 + struct inode *realinode;
1471 + struct dentry *realdentry;
1475 + if (S_ISDIR(inode->i_mode)) {
1476 + oe = inode->i_private;
1477 + } else if (mask & MAY_NOT_BLOCK) {
1481 + * For non-directories find an alias and get the info
1484 + alias = d_find_any_alias(inode);
1485 + if (WARN_ON(!alias))
1488 + oe = alias->d_fsdata;
1491 + realdentry = ovl_entry_real(oe, &is_upper);
1493 + /* Careful in RCU walk mode */
1494 + realinode = ACCESS_ONCE(realdentry->d_inode);
1496 + WARN_ON(!(mask & MAY_NOT_BLOCK));
1501 + if (mask & MAY_WRITE) {
1502 + umode_t mode = realinode->i_mode;
1505 + * Writes will always be redirected to upper layer, so
1506 + * ignore lower layer being read-only.
1508 + * If the overlay itself is read-only then proceed
1509 + * with the permission check, don't return EROFS.
1510 + * This will only happen if this is the lower layer of
1511 + * another overlayfs.
1513 + * If upper fs becomes read-only after the overlay was
1514 + * constructed return EROFS to prevent modification of
1518 + if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
1519 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1523 + err = __inode_permission(realinode, mask);
1530 +struct ovl_link_data {
1531 + struct dentry *realdentry;
1535 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1538 + struct dentry *realdentry;
1539 + struct inode *realinode;
1541 + realdentry = ovl_dentry_real(dentry);
1542 + realinode = realdentry->d_inode;
1544 + if (WARN_ON(!realinode->i_op->follow_link))
1545 + return ERR_PTR(-EPERM);
1547 + ret = realinode->i_op->follow_link(realdentry, nd);
1551 + if (realinode->i_op->put_link) {
1552 + struct ovl_link_data *data;
1554 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1556 + realinode->i_op->put_link(realdentry, nd, ret);
1557 + return ERR_PTR(-ENOMEM);
1559 + data->realdentry = realdentry;
1560 + data->cookie = ret;
1568 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1570 + struct inode *realinode;
1571 + struct ovl_link_data *data = c;
1576 + realinode = data->realdentry->d_inode;
1577 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1581 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1583 + struct path realpath;
1584 + struct inode *realinode;
1586 + ovl_path_real(dentry, &realpath);
1587 + realinode = realpath.dentry->d_inode;
1589 + if (!realinode->i_op->readlink)
1592 + touch_atime(&realpath);
1594 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1598 +static bool ovl_is_private_xattr(const char *name)
1600 + return strncmp(name, "trusted.overlay.", 14) == 0;
1603 +int ovl_setxattr(struct dentry *dentry, const char *name,
1604 + const void *value, size_t size, int flags)
1607 + struct dentry *upperdentry;
1609 + if (ovl_is_private_xattr(name))
1612 + err = ovl_copy_up(dentry);
1616 + upperdentry = ovl_dentry_upper(dentry);
1617 + return vfs_setxattr(upperdentry, name, value, size, flags);
1620 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1621 + void *value, size_t size)
1623 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1624 + ovl_is_private_xattr(name))
1627 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
1630 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
1635 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
1636 + if (res <= 0 || size == 0)
1639 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
1642 + /* filter out private xattrs */
1643 + for (off = 0; off < res;) {
1644 + char *s = list + off;
1645 + size_t slen = strlen(s) + 1;
1647 + BUG_ON(off + slen > res);
1649 + if (ovl_is_private_xattr(s)) {
1651 + memmove(s, s + slen, res - off);
1660 +int ovl_removexattr(struct dentry *dentry, const char *name)
1663 + struct path realpath;
1664 + enum ovl_path_type type;
1666 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1667 + ovl_is_private_xattr(name))
1670 + type = ovl_path_real(dentry, &realpath);
1671 + if (type == OVL_PATH_LOWER) {
1672 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
1676 + err = ovl_copy_up(dentry);
1680 + ovl_path_upper(dentry, &realpath);
1683 + return vfs_removexattr(realpath.dentry, name);
1686 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
1687 + struct dentry *realdentry)
1689 + if (type != OVL_PATH_LOWER)
1692 + if (special_file(realdentry->d_inode->i_mode))
1695 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
1701 +static int ovl_dentry_open(struct dentry *dentry, struct file *file,
1702 + const struct cred *cred)
1705 + struct path realpath;
1706 + enum ovl_path_type type;
1708 + type = ovl_path_real(dentry, &realpath);
1709 + if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
1710 + if (file->f_flags & O_TRUNC)
1711 + err = ovl_copy_up_truncate(dentry, 0);
1713 + err = ovl_copy_up(dentry);
1717 + ovl_path_upper(dentry, &realpath);
1720 + return vfs_open(&realpath, file, cred);
1723 +static const struct inode_operations ovl_file_inode_operations = {
1724 + .setattr = ovl_setattr,
1725 + .permission = ovl_permission,
1726 + .getattr = ovl_getattr,
1727 + .setxattr = ovl_setxattr,
1728 + .getxattr = ovl_getxattr,
1729 + .listxattr = ovl_listxattr,
1730 + .removexattr = ovl_removexattr,
1731 + .dentry_open = ovl_dentry_open,
1734 +static const struct inode_operations ovl_symlink_inode_operations = {
1735 + .setattr = ovl_setattr,
1736 + .follow_link = ovl_follow_link,
1737 + .put_link = ovl_put_link,
1738 + .readlink = ovl_readlink,
1739 + .getattr = ovl_getattr,
1740 + .setxattr = ovl_setxattr,
1741 + .getxattr = ovl_getxattr,
1742 + .listxattr = ovl_listxattr,
1743 + .removexattr = ovl_removexattr,
1746 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1747 + struct ovl_entry *oe)
1749 + struct inode *inode;
1751 + inode = new_inode(sb);
1757 + inode->i_ino = get_next_ino();
1758 + inode->i_mode = mode;
1759 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
1763 + inode->i_private = oe;
1764 + inode->i_op = &ovl_dir_inode_operations;
1765 + inode->i_fop = &ovl_dir_operations;
1769 + inode->i_op = &ovl_symlink_inode_operations;
1777 + inode->i_op = &ovl_file_inode_operations;
1781 + WARN(1, "illegal file type: %i\n", mode);
1790 +++ b/fs/overlayfs/overlayfs.h
1794 + * Copyright (C) 2011 Novell Inc.
1796 + * This program is free software; you can redistribute it and/or modify it
1797 + * under the terms of the GNU General Public License version 2 as published by
1798 + * the Free Software Foundation.
1803 +enum ovl_path_type {
1809 +extern const char *ovl_opaque_xattr;
1810 +extern const char *ovl_whiteout_xattr;
1811 +extern const struct dentry_operations ovl_dentry_operations;
1813 +enum ovl_path_type ovl_path_type(struct dentry *dentry);
1814 +u64 ovl_dentry_version_get(struct dentry *dentry);
1815 +void ovl_dentry_version_inc(struct dentry *dentry);
1816 +void ovl_path_upper(struct dentry *dentry, struct path *path);
1817 +void ovl_path_lower(struct dentry *dentry, struct path *path);
1818 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1819 +struct dentry *ovl_dentry_upper(struct dentry *dentry);
1820 +struct dentry *ovl_dentry_lower(struct dentry *dentry);
1821 +struct dentry *ovl_dentry_real(struct dentry *dentry);
1822 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
1823 +bool ovl_dentry_is_opaque(struct dentry *dentry);
1824 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
1825 +bool ovl_is_whiteout(struct dentry *dentry);
1826 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
1827 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
1828 + unsigned int flags);
1829 +struct file *ovl_path_open(struct path *path, int flags);
1831 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
1832 + struct kstat *stat, const char *link);
1835 +extern const struct file_operations ovl_dir_operations;
1836 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
1839 +int ovl_setattr(struct dentry *dentry, struct iattr *attr);
1840 +int ovl_permission(struct inode *inode, int mask);
1841 +int ovl_setxattr(struct dentry *dentry, const char *name,
1842 + const void *value, size_t size, int flags);
1843 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1844 + void *value, size_t size);
1845 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
1846 +int ovl_removexattr(struct dentry *dentry, const char *name);
1848 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1849 + struct ovl_entry *oe);
1850 +static inline void ovl_copyattr(struct inode *from, struct inode *to)
1852 + to->i_uid = from->i_uid;
1853 + to->i_gid = from->i_gid;
1857 +extern const struct inode_operations ovl_dir_inode_operations;
1860 +int ovl_copy_up(struct dentry *dentry);
1861 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
1863 +++ b/fs/overlayfs/readdir.c
1867 + * Copyright (C) 2011 Novell Inc.
1869 + * This program is free software; you can redistribute it and/or modify it
1870 + * under the terms of the GNU General Public License version 2 as published by
1871 + * the Free Software Foundation.
1874 +#include <linux/fs.h>
1875 +#include <linux/slab.h>
1876 +#include <linux/namei.h>
1877 +#include <linux/file.h>
1878 +#include <linux/xattr.h>
1879 +#include <linux/rbtree.h>
1880 +#include <linux/security.h>
1881 +#include <linux/cred.h>
1882 +#include "overlayfs.h"
1884 +struct ovl_cache_entry {
1887 + unsigned int type;
1890 + struct list_head l_node;
1891 + struct rb_node node;
1894 +struct ovl_readdir_data {
1895 + struct rb_root *root;
1896 + struct list_head *list;
1897 + struct list_head *middle;
1898 + struct dentry *dir;
1903 +struct ovl_dir_file {
1906 + struct list_head cursor;
1907 + u64 cache_version;
1908 + struct list_head cache;
1909 + struct file *realfile;
1912 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
1914 + return container_of(n, struct ovl_cache_entry, node);
1917 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
1918 + const char *name, int len)
1920 + struct rb_node *node = root->rb_node;
1924 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
1926 + cmp = strncmp(name, p->name, len);
1928 + node = p->node.rb_right;
1929 + else if (cmp < 0 || len < p->len)
1930 + node = p->node.rb_left;
1938 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
1939 + u64 ino, unsigned int d_type)
1941 + struct ovl_cache_entry *p;
1943 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
1945 + char *name_copy = (char *) (p + 1);
1946 + memcpy(name_copy, name, len);
1947 + name_copy[len] = '\0';
1948 + p->name = name_copy;
1952 + p->is_whiteout = false;
1958 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
1959 + const char *name, int len, u64 ino,
1960 + unsigned int d_type)
1962 + struct rb_node **newp = &rdd->root->rb_node;
1963 + struct rb_node *parent = NULL;
1964 + struct ovl_cache_entry *p;
1968 + struct ovl_cache_entry *tmp;
1971 + tmp = ovl_cache_entry_from_node(*newp);
1972 + cmp = strncmp(name, tmp->name, len);
1974 + newp = &tmp->node.rb_right;
1975 + else if (cmp < 0 || len < tmp->len)
1976 + newp = &tmp->node.rb_left;
1981 + p = ovl_cache_entry_new(name, len, ino, d_type);
1985 + list_add_tail(&p->l_node, rdd->list);
1986 + rb_link_node(&p->node, parent, newp);
1987 + rb_insert_color(&p->node, rdd->root);
1992 +static int ovl_fill_lower(void *buf, const char *name, int namelen,
1993 + loff_t offset, u64 ino, unsigned int d_type)
1995 + struct ovl_readdir_data *rdd = buf;
1996 + struct ovl_cache_entry *p;
1999 + p = ovl_cache_entry_find(rdd->root, name, namelen);
2001 + list_move_tail(&p->l_node, rdd->middle);
2003 + p = ovl_cache_entry_new(name, namelen, ino, d_type);
2005 + rdd->err = -ENOMEM;
2007 + list_add_tail(&p->l_node, rdd->middle);
2013 +static void ovl_cache_free(struct list_head *list)
2015 + struct ovl_cache_entry *p;
2016 + struct ovl_cache_entry *n;
2018 + list_for_each_entry_safe(p, n, list, l_node)
2021 + INIT_LIST_HEAD(list);
2024 +static int ovl_fill_upper(void *buf, const char *name, int namelen,
2025 + loff_t offset, u64 ino, unsigned int d_type)
2027 + struct ovl_readdir_data *rdd = buf;
2030 + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
2033 +static inline int ovl_dir_read(struct path *realpath,
2034 + struct ovl_readdir_data *rdd, filldir_t filler)
2036 + struct file *realfile;
2039 + realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
2040 + if (IS_ERR(realfile))
2041 + return PTR_ERR(realfile);
2046 + err = vfs_readdir(realfile, filler, rdd);
2049 + } while (!err && rdd->count);
2055 +static void ovl_dir_reset(struct file *file)
2057 + struct ovl_dir_file *od = file->private_data;
2058 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
2060 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
2061 + list_del_init(&od->cursor);
2062 + ovl_cache_free(&od->cache);
2063 + od->is_cached = false;
2065 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
2066 + if (od->is_real && type == OVL_PATH_MERGE) {
2067 + fput(od->realfile);
2068 + od->realfile = NULL;
2069 + od->is_real = false;
2073 +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
2075 + struct ovl_cache_entry *p;
2076 + struct dentry *dentry;
2077 + const struct cred *old_cred;
2078 + struct cred *override_cred;
2080 + override_cred = prepare_creds();
2081 + if (!override_cred) {
2082 + ovl_cache_free(rdd->list);
2087 + * CAP_SYS_ADMIN for getxattr
2088 + * CAP_DAC_OVERRIDE for lookup
2090 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2091 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2092 + old_cred = override_creds(override_cred);
2094 + mutex_lock(&rdd->dir->d_inode->i_mutex);
2095 + list_for_each_entry(p, rdd->list, l_node) {
2096 + if (p->type != DT_LNK)
2099 + dentry = lookup_one_len(p->name, rdd->dir, p->len);
2100 + if (IS_ERR(dentry))
2103 + p->is_whiteout = ovl_is_whiteout(dentry);
2106 + mutex_unlock(&rdd->dir->d_inode->i_mutex);
2108 + revert_creds(old_cred);
2109 + put_cred(override_cred);
2114 +static inline int ovl_dir_read_merged(struct path *upperpath,
2115 + struct path *lowerpath,
2116 + struct ovl_readdir_data *rdd)
2119 + struct rb_root root = RB_ROOT;
2120 + struct list_head middle;
2122 + rdd->root = &root;
2123 + if (upperpath->dentry) {
2124 + rdd->dir = upperpath->dentry;
2125 + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
2129 + err = ovl_dir_mark_whiteouts(rdd);
2134 + * Insert lowerpath entries before upperpath ones, this allows
2135 + * offsets to be reasonably constant
2137 + list_add(&middle, rdd->list);
2138 + rdd->middle = &middle;
2139 + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower);
2140 + list_del(&middle);
2147 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
2149 + struct list_head *l;
2152 + l = od->cache.next;
2153 + for (off = 0; off < pos; off++) {
2154 + if (l == &od->cache)
2158 + list_move_tail(&od->cursor, l);
2161 +static int ovl_readdir(struct file *file, void *buf, filldir_t filler)
2163 + struct ovl_dir_file *od = file->private_data;
2167 + ovl_dir_reset(file);
2169 + if (od->is_real) {
2170 + res = vfs_readdir(od->realfile, filler, buf);
2171 + file->f_pos = od->realfile->f_pos;
2176 + if (!od->is_cached) {
2177 + struct path lowerpath;
2178 + struct path upperpath;
2179 + struct ovl_readdir_data rdd = { .list = &od->cache };
2181 + ovl_path_lower(file->f_path.dentry, &lowerpath);
2182 + ovl_path_upper(file->f_path.dentry, &upperpath);
2184 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2186 + ovl_cache_free(rdd.list);
2190 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
2191 + od->is_cached = true;
2193 + ovl_seek_cursor(od, file->f_pos);
2196 + while (od->cursor.next != &od->cache) {
2199 + struct ovl_cache_entry *p;
2201 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
2202 + off = file->f_pos;
2203 + if (!p->is_whiteout) {
2204 + over = filler(buf, p->name, p->len, off, p->ino,
2210 + list_move(&od->cursor, &p->l_node);
2216 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
2219 + struct ovl_dir_file *od = file->private_data;
2221 + mutex_lock(&file->f_dentry->d_inode->i_mutex);
2223 + ovl_dir_reset(file);
2225 + if (od->is_real) {
2226 + res = vfs_llseek(od->realfile, offset, origin);
2227 + file->f_pos = od->realfile->f_pos;
2233 + offset += file->f_pos;
2243 + if (offset != file->f_pos) {
2244 + file->f_pos = offset;
2245 + if (od->is_cached)
2246 + ovl_seek_cursor(od, offset);
2251 + mutex_unlock(&file->f_dentry->d_inode->i_mutex);
2256 +static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
2259 + struct ovl_dir_file *od = file->private_data;
2261 + /* May need to reopen directory if it got copied up */
2262 + if (!od->realfile) {
2263 + struct path upperpath;
2265 + ovl_path_upper(file->f_path.dentry, &upperpath);
2266 + od->realfile = ovl_path_open(&upperpath, O_RDONLY);
2267 + if (IS_ERR(od->realfile))
2268 + return PTR_ERR(od->realfile);
2271 + return vfs_fsync_range(od->realfile, start, end, datasync);
2274 +static int ovl_dir_release(struct inode *inode, struct file *file)
2276 + struct ovl_dir_file *od = file->private_data;
2278 + list_del(&od->cursor);
2279 + ovl_cache_free(&od->cache);
2281 + fput(od->realfile);
2287 +static int ovl_dir_open(struct inode *inode, struct file *file)
2289 + struct path realpath;
2290 + struct file *realfile;
2291 + struct ovl_dir_file *od;
2292 + enum ovl_path_type type;
2294 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
2298 + type = ovl_path_real(file->f_path.dentry, &realpath);
2299 + realfile = ovl_path_open(&realpath, file->f_flags);
2300 + if (IS_ERR(realfile)) {
2302 + return PTR_ERR(realfile);
2304 + INIT_LIST_HEAD(&od->cache);
2305 + INIT_LIST_HEAD(&od->cursor);
2306 + od->is_cached = false;
2307 + od->realfile = realfile;
2308 + od->is_real = (type != OVL_PATH_MERGE);
2309 + file->private_data = od;
2314 +const struct file_operations ovl_dir_operations = {
2315 + .read = generic_read_dir,
2316 + .open = ovl_dir_open,
2317 + .readdir = ovl_readdir,
2318 + .llseek = ovl_dir_llseek,
2319 + .fsync = ovl_dir_fsync,
2320 + .release = ovl_dir_release,
2323 +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
2326 + struct path lowerpath;
2327 + struct path upperpath;
2328 + struct ovl_cache_entry *p;
2329 + struct ovl_readdir_data rdd = { .list = list };
2331 + ovl_path_upper(dentry, &upperpath);
2332 + ovl_path_lower(dentry, &lowerpath);
2334 + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2340 + list_for_each_entry(p, list, l_node) {
2341 + if (p->is_whiteout)
2344 + if (p->name[0] == '.') {
2347 + if (p->len == 2 && p->name[1] == '.')
2357 +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
2359 + struct path upperpath;
2360 + struct dentry *upperdir;
2361 + struct ovl_cache_entry *p;
2362 + const struct cred *old_cred;
2363 + struct cred *override_cred;
2366 + ovl_path_upper(dir, &upperpath);
2367 + upperdir = upperpath.dentry;
2369 + override_cred = prepare_creds();
2370 + if (!override_cred)
2374 + * CAP_DAC_OVERRIDE for lookup and unlink
2375 + * CAP_SYS_ADMIN for setxattr of "trusted" namespace
2376 + * CAP_FOWNER for unlink in sticky directory
2378 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2379 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2380 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
2381 + old_cred = override_creds(override_cred);
2383 + err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
2385 + goto out_revert_creds;
2387 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2388 + list_for_each_entry(p, list, l_node) {
2389 + struct dentry *dentry;
2392 + if (!p->is_whiteout)
2395 + dentry = lookup_one_len(p->name, upperdir, p->len);
2396 + if (IS_ERR(dentry)) {
2397 + printk(KERN_WARNING
2398 + "overlayfs: failed to lookup whiteout %.*s: %li\n",
2399 + p->len, p->name, PTR_ERR(dentry));
2402 + ret = vfs_unlink(upperdir->d_inode, dentry);
2405 + printk(KERN_WARNING
2406 + "overlayfs: failed to unlink whiteout %.*s: %i\n",
2407 + p->len, p->name, ret);
2409 + mutex_unlock(&upperdir->d_inode->i_mutex);
2412 + revert_creds(old_cred);
2413 + put_cred(override_cred);
2418 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
2423 + err = ovl_check_empty_dir(dentry, &list);
2424 + if (!err && type == OVL_PATH_MERGE)
2425 + err = ovl_remove_whiteouts(dentry, &list);
2427 + ovl_cache_free(&list);
2432 +++ b/fs/overlayfs/super.c
2436 + * Copyright (C) 2011 Novell Inc.
2438 + * This program is free software; you can redistribute it and/or modify it
2439 + * under the terms of the GNU General Public License version 2 as published by
2440 + * the Free Software Foundation.
2443 +#include <linux/fs.h>
2444 +#include <linux/namei.h>
2445 +#include <linux/xattr.h>
2446 +#include <linux/security.h>
2447 +#include <linux/mount.h>
2448 +#include <linux/slab.h>
2449 +#include <linux/parser.h>
2450 +#include <linux/module.h>
2451 +#include <linux/cred.h>
2452 +#include <linux/sched.h>
2453 +#include <linux/statfs.h>
2454 +#include <linux/seq_file.h>
2455 +#include "overlayfs.h"
2457 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2458 +MODULE_DESCRIPTION("Overlay filesystem");
2459 +MODULE_LICENSE("GPL");
2461 +#define OVERLAYFS_SUPER_MAGIC 0x794c764f
2463 +struct ovl_config {
2468 +/* private information held for overlayfs's superblock */
2470 + struct vfsmount *upper_mnt;
2471 + struct vfsmount *lower_mnt;
2472 + long lower_namelen;
2473 + /* pathnames of lower and upper dirs, for show_options */
2474 + struct ovl_config config;
2477 +/* private information held for every overlayfs dentry */
2480 + * Keep "double reference" on upper dentries, so that
2481 + * d_delete() doesn't think it's OK to reset d_inode to NULL.
2483 + struct dentry *__upperdentry;
2484 + struct dentry *lowerdentry;
2490 + struct rcu_head rcu;
2494 +const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
2495 +const char *ovl_opaque_xattr = "trusted.overlay.opaque";
2498 +enum ovl_path_type ovl_path_type(struct dentry *dentry)
2500 + struct ovl_entry *oe = dentry->d_fsdata;
2502 + if (oe->__upperdentry) {
2503 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
2504 + return OVL_PATH_MERGE;
2506 + return OVL_PATH_UPPER;
2508 + return OVL_PATH_LOWER;
2512 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
2514 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
2515 + smp_read_barrier_depends();
2516 + return upperdentry;
2519 +void ovl_path_upper(struct dentry *dentry, struct path *path)
2521 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2522 + struct ovl_entry *oe = dentry->d_fsdata;
2524 + path->mnt = ofs->upper_mnt;
2525 + path->dentry = ovl_upperdentry_dereference(oe);
2528 +void ovl_path_lower(struct dentry *dentry, struct path *path)
2530 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2531 + struct ovl_entry *oe = dentry->d_fsdata;
2533 + path->mnt = ofs->lower_mnt;
2534 + path->dentry = oe->lowerdentry;
2537 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
2540 + enum ovl_path_type type = ovl_path_type(dentry);
2542 + if (type == OVL_PATH_LOWER)
2543 + ovl_path_lower(dentry, path);
2545 + ovl_path_upper(dentry, path);
2550 +struct dentry *ovl_dentry_upper(struct dentry *dentry)
2552 + struct ovl_entry *oe = dentry->d_fsdata;
2554 + return ovl_upperdentry_dereference(oe);
2557 +struct dentry *ovl_dentry_lower(struct dentry *dentry)
2559 + struct ovl_entry *oe = dentry->d_fsdata;
2561 + return oe->lowerdentry;
2564 +struct dentry *ovl_dentry_real(struct dentry *dentry)
2566 + struct ovl_entry *oe = dentry->d_fsdata;
2567 + struct dentry *realdentry;
2569 + realdentry = ovl_upperdentry_dereference(oe);
2571 + realdentry = oe->lowerdentry;
2573 + return realdentry;
2576 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
2578 + struct dentry *realdentry;
2580 + realdentry = ovl_upperdentry_dereference(oe);
2584 + realdentry = oe->lowerdentry;
2585 + *is_upper = false;
2587 + return realdentry;
2590 +bool ovl_dentry_is_opaque(struct dentry *dentry)
2592 + struct ovl_entry *oe = dentry->d_fsdata;
2593 + return oe->opaque;
2596 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
2598 + struct ovl_entry *oe = dentry->d_fsdata;
2599 + oe->opaque = opaque;
2602 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
2604 + struct ovl_entry *oe = dentry->d_fsdata;
2606 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
2607 + WARN_ON(oe->__upperdentry);
2608 + BUG_ON(!upperdentry->d_inode);
2610 + oe->__upperdentry = dget(upperdentry);
2613 +void ovl_dentry_version_inc(struct dentry *dentry)
2615 + struct ovl_entry *oe = dentry->d_fsdata;
2617 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2621 +u64 ovl_dentry_version_get(struct dentry *dentry)
2623 + struct ovl_entry *oe = dentry->d_fsdata;
2625 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2626 + return oe->version;
2629 +bool ovl_is_whiteout(struct dentry *dentry)
2636 + if (!dentry->d_inode)
2638 + if (!S_ISLNK(dentry->d_inode->i_mode))
2641 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
2642 + if (res == 1 && val == 'y')
2648 +static bool ovl_is_opaquedir(struct dentry *dentry)
2653 + if (!S_ISDIR(dentry->d_inode->i_mode))
2656 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
2657 + if (res == 1 && val == 'y')
2663 +static void ovl_entry_free(struct rcu_head *head)
2665 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
2669 +static void ovl_dentry_release(struct dentry *dentry)
2671 + struct ovl_entry *oe = dentry->d_fsdata;
2674 + dput(oe->__upperdentry);
2675 + dput(oe->__upperdentry);
2676 + dput(oe->lowerdentry);
2677 + call_rcu(&oe->rcu, ovl_entry_free);
2681 +const struct dentry_operations ovl_dentry_operations = {
2682 + .d_release = ovl_dentry_release,
2685 +static struct ovl_entry *ovl_alloc_entry(void)
2687 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
2690 +static inline struct dentry *ovl_lookup_real(struct dentry *dir,
2691 + struct qstr *name)
2693 + struct dentry *dentry;
2695 + mutex_lock(&dir->d_inode->i_mutex);
2696 + dentry = lookup_one_len(name->name, dir, name->len);
2697 + mutex_unlock(&dir->d_inode->i_mutex);
2699 + if (IS_ERR(dentry)) {
2700 + if (PTR_ERR(dentry) == -ENOENT)
2702 + } else if (!dentry->d_inode) {
2709 +static int ovl_do_lookup(struct dentry *dentry)
2711 + struct ovl_entry *oe;
2712 + struct dentry *upperdir;
2713 + struct dentry *lowerdir;
2714 + struct dentry *upperdentry = NULL;
2715 + struct dentry *lowerdentry = NULL;
2716 + struct inode *inode = NULL;
2720 + oe = ovl_alloc_entry();
2724 + upperdir = ovl_dentry_upper(dentry->d_parent);
2725 + lowerdir = ovl_dentry_lower(dentry->d_parent);
2728 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
2729 + err = PTR_ERR(upperdentry);
2730 + if (IS_ERR(upperdentry))
2733 + if (lowerdir && upperdentry &&
2734 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
2735 + S_ISDIR(upperdentry->d_inode->i_mode))) {
2736 + const struct cred *old_cred;
2737 + struct cred *override_cred;
2740 + override_cred = prepare_creds();
2741 + if (!override_cred)
2742 + goto out_dput_upper;
2744 + /* CAP_SYS_ADMIN needed for getxattr */
2745 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2746 + old_cred = override_creds(override_cred);
2748 + if (ovl_is_opaquedir(upperdentry)) {
2749 + oe->opaque = true;
2750 + } else if (ovl_is_whiteout(upperdentry)) {
2751 + dput(upperdentry);
2752 + upperdentry = NULL;
2753 + oe->opaque = true;
2755 + revert_creds(old_cred);
2756 + put_cred(override_cred);
2759 + if (lowerdir && !oe->opaque) {
2760 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
2761 + err = PTR_ERR(lowerdentry);
2762 + if (IS_ERR(lowerdentry))
2763 + goto out_dput_upper;
2766 + if (lowerdentry && upperdentry &&
2767 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
2768 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
2769 + dput(lowerdentry);
2770 + lowerdentry = NULL;
2771 + oe->opaque = true;
2774 + if (lowerdentry || upperdentry) {
2775 + struct dentry *realdentry;
2777 + realdentry = upperdentry ? upperdentry : lowerdentry;
2779 + inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
2783 + ovl_copyattr(realdentry->d_inode, inode);
2787 + oe->__upperdentry = dget(upperdentry);
2790 + oe->lowerdentry = lowerdentry;
2792 + dentry->d_fsdata = oe;
2793 + dentry->d_op = &ovl_dentry_operations;
2794 + d_add(dentry, inode);
2799 + dput(lowerdentry);
2801 + dput(upperdentry);
2808 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
2809 + unsigned int flags)
2811 + int err = ovl_do_lookup(dentry);
2814 + return ERR_PTR(err);
2819 +struct file *ovl_path_open(struct path *path, int flags)
2822 + return dentry_open(path, flags, current_cred());
2825 +static void ovl_put_super(struct super_block *sb)
2827 + struct ovl_fs *ufs = sb->s_fs_info;
2829 + if (!(sb->s_flags & MS_RDONLY))
2830 + mnt_drop_write(ufs->upper_mnt);
2832 + mntput(ufs->upper_mnt);
2833 + mntput(ufs->lower_mnt);
2835 + kfree(ufs->config.lowerdir);
2836 + kfree(ufs->config.upperdir);
2840 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2842 + int flags = *flagsp;
2843 + struct ovl_fs *ufs = sb->s_fs_info;
2845 + /* When remounting rw or ro, we need to adjust the write access to the
2848 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2849 + /* No change to readonly status */
2852 + if (flags & MS_RDONLY) {
2853 + mnt_drop_write(ufs->upper_mnt);
2856 + return mnt_want_write(ufs->upper_mnt);
2861 + * @sb: The overlayfs super block
2862 + * @buf: The struct kstatfs to fill in with stats
2864 + * Get the filesystem statistics. As writes always target the upper layer
2865 + * filesystem pass the statfs to the same filesystem.
2867 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2869 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2870 + struct dentry *root_dentry = dentry->d_sb->s_root;
2874 + ovl_path_upper(root_dentry, &path);
2876 + err = vfs_statfs(&path, buf);
2878 + buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
2879 + buf->f_type = OVERLAYFS_SUPER_MAGIC;
2886 + * ovl_show_options
2888 + * Prints the mount options for a given superblock.
2889 + * Returns zero; does not fail.
2891 +static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
2893 + struct super_block *sb = dentry->d_sb;
2894 + struct ovl_fs *ufs = sb->s_fs_info;
2896 + seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
2897 + seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
2901 +static const struct super_operations ovl_super_operations = {
2902 + .put_super = ovl_put_super,
2903 + .remount_fs = ovl_remount_fs,
2904 + .statfs = ovl_statfs,
2905 + .show_options = ovl_show_options,
2914 +static const match_table_t ovl_tokens = {
2915 + {Opt_lowerdir, "lowerdir=%s"},
2916 + {Opt_upperdir, "upperdir=%s"},
2920 +static int ovl_parse_opt(char *opt, struct ovl_config *config)
2924 + config->upperdir = NULL;
2925 + config->lowerdir = NULL;
2927 + while ((p = strsep(&opt, ",")) != NULL) {
2929 + substring_t args[MAX_OPT_ARGS];
2934 + token = match_token(p, ovl_tokens, args);
2936 + case Opt_upperdir:
2937 + kfree(config->upperdir);
2938 + config->upperdir = match_strdup(&args[0]);
2939 + if (!config->upperdir)
2943 + case Opt_lowerdir:
2944 + kfree(config->lowerdir);
2945 + config->lowerdir = match_strdup(&args[0]);
2946 + if (!config->lowerdir)
2957 +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
2959 + struct path lowerpath;
2960 + struct path upperpath;
2961 + struct inode *root_inode;
2962 + struct dentry *root_dentry;
2963 + struct ovl_entry *oe;
2964 + struct ovl_fs *ufs;
2965 + struct kstatfs statfs;
2969 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
2973 + err = ovl_parse_opt((char *) data, &ufs->config);
2975 + goto out_free_ufs;
2978 + if (!ufs->config.upperdir || !ufs->config.lowerdir) {
2979 + printk(KERN_ERR "overlayfs: missing upperdir or lowerdir\n");
2980 + goto out_free_config;
2983 + oe = ovl_alloc_entry();
2985 + goto out_free_config;
2987 + err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
2991 + err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
2993 + goto out_put_upperpath;
2996 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
2997 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
2998 + goto out_put_lowerpath;
3000 + err = vfs_statfs(&lowerpath, &statfs);
3002 + printk(KERN_ERR "overlayfs: statfs failed on lowerpath\n");
3003 + goto out_put_lowerpath;
3005 + ufs->lower_namelen = statfs.f_namelen;
3007 + sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
3008 + lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
3011 + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
3012 + printk(KERN_ERR "overlayfs: maximum fs stacking depth exceeded\n");
3013 + goto out_put_lowerpath;
3017 + ufs->upper_mnt = clone_private_mount(&upperpath);
3018 + err = PTR_ERR(ufs->upper_mnt);
3019 + if (IS_ERR(ufs->upper_mnt)) {
3020 + printk(KERN_ERR "overlayfs: failed to clone upperpath\n");
3021 + goto out_put_lowerpath;
3024 + ufs->lower_mnt = clone_private_mount(&lowerpath);
3025 + err = PTR_ERR(ufs->lower_mnt);
3026 + if (IS_ERR(ufs->lower_mnt)) {
3027 + printk(KERN_ERR "overlayfs: failed to clone lowerpath\n");
3028 + goto out_put_upper_mnt;
3032 + * Make lower_mnt R/O. That way fchmod/fchown on lower file
3033 + * will fail instead of modifying lower fs.
3035 + ufs->lower_mnt->mnt_flags |= MNT_READONLY;
3037 + /* If the upper fs is r/o, we mark overlayfs r/o too */
3038 + if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
3039 + sb->s_flags |= MS_RDONLY;
3041 + if (!(sb->s_flags & MS_RDONLY)) {
3042 + err = mnt_want_write(ufs->upper_mnt);
3044 + goto out_put_lower_mnt;
3048 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
3050 + goto out_drop_write;
3052 + root_dentry = d_make_root(root_inode);
3054 + goto out_drop_write;
3056 + mntput(upperpath.mnt);
3057 + mntput(lowerpath.mnt);
3059 + oe->__upperdentry = dget(upperpath.dentry);
3060 + oe->lowerdentry = lowerpath.dentry;
3062 + root_dentry->d_fsdata = oe;
3063 + root_dentry->d_op = &ovl_dentry_operations;
3065 + sb->s_magic = OVERLAYFS_SUPER_MAGIC;
3066 + sb->s_op = &ovl_super_operations;
3067 + sb->s_root = root_dentry;
3068 + sb->s_fs_info = ufs;
3073 + if (!(sb->s_flags & MS_RDONLY))
3074 + mnt_drop_write(ufs->upper_mnt);
3076 + mntput(ufs->lower_mnt);
3078 + mntput(ufs->upper_mnt);
3080 + path_put(&lowerpath);
3082 + path_put(&upperpath);
3086 + kfree(ufs->config.lowerdir);
3087 + kfree(ufs->config.upperdir);
3094 +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
3095 + const char *dev_name, void *raw_data)
3097 + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
3100 +static struct file_system_type ovl_fs_type = {
3101 + .owner = THIS_MODULE,
3102 + .name = "overlayfs",
3103 + .mount = ovl_mount,
3104 + .kill_sb = kill_anon_super,
3107 +static int __init ovl_init(void)
3109 + return register_filesystem(&ovl_fs_type);
3112 +static void __exit ovl_exit(void)
3114 + unregister_filesystem(&ovl_fs_type);
3117 +module_init(ovl_init);
3118 +module_exit(ovl_exit);
3121 @@ -1310,6 +1310,7 @@ long do_splice_direct(struct file *in, l
3125 +EXPORT_SYMBOL(do_splice_direct);
3127 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
3128 struct pipe_inode_info *opipe,
3129 --- a/include/linux/fs.h
3130 +++ b/include/linux/fs.h
3131 @@ -244,6 +244,12 @@ struct iattr {
3133 #include <linux/quota.h>
3136 + * Maximum number of layers of fs stack. Needs to be limited to
3137 + * prevent kernel stack overflow
3139 +#define FILESYSTEM_MAX_STACK_DEPTH 2
3142 * enum positive_aop_returns - aop return codes with specific semantics
3144 @@ -1320,6 +1326,11 @@ struct super_block {
3146 /* Being remounted read-only */
3147 int s_readonly_remount;
3150 + * Indicates how deep in a filesystem stack this SB is
3152 + int s_stack_depth;
3155 /* superblock cache pruning functions */
3156 @@ -1573,6 +1584,7 @@ struct inode_operations {
3157 int (*atomic_open)(struct inode *, struct dentry *,
3158 struct file *, unsigned open_flag,
3159 umode_t create_mode, int *opened);
3160 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
3161 } ____cacheline_aligned;
3163 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
3164 @@ -2006,6 +2018,7 @@ extern struct file *file_open_name(struc
3165 extern struct file *filp_open(const char *, int, umode_t);
3166 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
3168 +extern int vfs_open(const struct path *, struct file *, const struct cred *);
3169 extern struct file * dentry_open(const struct path *, int, const struct cred *);
3170 extern int filp_close(struct file *, fl_owner_t id);
3172 @@ -2211,6 +2224,7 @@ extern sector_t bmap(struct inode *, sec
3174 extern int notify_change(struct dentry *, struct iattr *);
3175 extern int inode_permission(struct inode *, int);
3176 +extern int __inode_permission(struct inode *, int);
3177 extern int generic_permission(struct inode *, int);
3179 static inline bool execute_ok(struct inode *inode)
3180 --- a/include/linux/mount.h
3181 +++ b/include/linux/mount.h
3182 @@ -68,6 +68,9 @@ extern void mnt_pin(struct vfsmount *mnt
3183 extern void mnt_unpin(struct vfsmount *mnt);
3184 extern int __mnt_is_readonly(struct vfsmount *mnt);
3187 +extern struct vfsmount *clone_private_mount(struct path *path);
3189 struct file_system_type;
3190 extern struct vfsmount *vfs_kern_mount(struct file_system_type *type,
3191 int flags, const char *name,