1 --- a/Documentation/filesystems/Locking
2 +++ b/Documentation/filesystems/Locking
3 @@ -64,6 +64,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 @@ -91,6 +92,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
244 --- a/fs/ecryptfs/main.c
245 +++ b/fs/ecryptfs/main.c
246 @@ -567,6 +567,13 @@ static struct dentry *ecryptfs_mount(str
247 s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
248 s->s_blocksize = path.dentry->d_sb->s_blocksize;
249 s->s_magic = ECRYPTFS_SUPER_MAGIC;
250 + s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
253 + if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
254 + printk(KERN_ERR "eCryptfs: maximum fs stacking depth exceeded\n");
258 inode = ecryptfs_get_inode(path.dentry->d_inode, s);
262 @@ -42,11 +42,6 @@ static inline int __sync_blockdev(struct
263 extern void __init chrdev_init(void);
268 -extern int __inode_permission(struct inode *, int);
273 extern int copy_mount_options(const void __user *, unsigned long *);
276 @@ -67,6 +67,7 @@ source "fs/quota/Kconfig"
278 source "fs/autofs4/Kconfig"
279 source "fs/fuse/Kconfig"
280 +source "fs/overlayfs/Kconfig"
286 @@ -107,6 +107,7 @@ obj-$(CONFIG_QNX6FS_FS) += qnx6/
287 obj-$(CONFIG_AUTOFS4_FS) += autofs4/
288 obj-$(CONFIG_ADFS_FS) += adfs/
289 obj-$(CONFIG_FUSE_FS) += fuse/
290 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
291 obj-$(CONFIG_UDF_FS) += udf/
292 obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
293 obj-$(CONFIG_OMFS_FS) += omfs/
296 @@ -402,6 +402,7 @@ int __inode_permission(struct inode *ino
298 return security_inode_permission(inode, mask);
300 +EXPORT_SYMBOL(__inode_permission);
303 * sb_permission - Check superblock-level permissions
304 @@ -2876,9 +2877,12 @@ finish_open_created:
305 error = may_open(&nd->path, acc_mode, open_flag);
308 - file->f_path.mnt = nd->path.mnt;
309 - error = finish_open(file, nd->path.dentry, NULL, opened);
312 + BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
313 + error = vfs_open(&nd->path, file, current_cred());
315 + *opened |= FILE_OPENED;
317 if (error == -EOPENSTALE)
322 @@ -1421,6 +1421,24 @@ void drop_collected_mounts(struct vfsmou
323 release_mounts(&umount_list);
326 +struct vfsmount *clone_private_mount(struct path *path)
328 + struct mount *old_mnt = real_mount(path->mnt);
329 + struct mount *new_mnt;
331 + if (IS_MNT_UNBINDABLE(old_mnt))
332 + return ERR_PTR(-EINVAL);
334 + down_read(&namespace_sem);
335 + new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
336 + up_read(&namespace_sem);
338 + return ERR_PTR(-ENOMEM);
340 + return &new_mnt->mnt;
342 +EXPORT_SYMBOL_GPL(clone_private_mount);
344 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
345 struct vfsmount *root)
349 @@ -816,8 +816,7 @@ struct file *dentry_open(const struct pa
350 return ERR_PTR(error);
354 - error = do_dentry_open(f, NULL, cred);
355 + error = vfs_open(path, f, cred);
357 error = open_check_o_direct(f);
359 @@ -832,6 +831,26 @@ struct file *dentry_open(const struct pa
361 EXPORT_SYMBOL(dentry_open);
364 + * vfs_open - open the file at the given path
365 + * @path: path to open
366 + * @filp: newly allocated file with f_flag initialized
367 + * @cred: credentials to use
369 +int vfs_open(const struct path *path, struct file *filp,
370 + const struct cred *cred)
372 + struct inode *inode = path->dentry->d_inode;
374 + if (inode->i_op->dentry_open)
375 + return inode->i_op->dentry_open(path->dentry, filp, cred);
377 + filp->f_path = *path;
378 + return do_dentry_open(filp, NULL, cred);
381 +EXPORT_SYMBOL(vfs_open);
383 static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
385 int lookup_flags = 0;
387 +++ b/fs/overlayfs/copy_up.c
391 + * Copyright (C) 2011 Novell Inc.
393 + * This program is free software; you can redistribute it and/or modify it
394 + * under the terms of the GNU General Public License version 2 as published by
395 + * the Free Software Foundation.
398 +#include <linux/fs.h>
399 +#include <linux/slab.h>
400 +#include <linux/file.h>
401 +#include <linux/splice.h>
402 +#include <linux/xattr.h>
403 +#include <linux/security.h>
404 +#include <linux/uaccess.h>
405 +#include <linux/sched.h>
406 +#include "overlayfs.h"
408 +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
410 +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
412 + ssize_t list_size, size;
413 + char *buf, *name, *value;
416 + if (!old->d_inode->i_op->getxattr ||
417 + !new->d_inode->i_op->getxattr)
420 + list_size = vfs_listxattr(old, NULL, 0);
421 + if (list_size <= 0) {
422 + if (list_size == -EOPNOTSUPP)
427 + buf = kzalloc(list_size, GFP_KERNEL);
432 + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
436 + list_size = vfs_listxattr(old, buf, list_size);
437 + if (list_size <= 0) {
439 + goto out_free_value;
442 + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
443 + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
446 + goto out_free_value;
448 + error = vfs_setxattr(new, name, value, size, 0);
450 + goto out_free_value;
460 +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
462 + struct file *old_file;
463 + struct file *new_file;
469 + old_file = ovl_path_open(old, O_RDONLY);
470 + if (IS_ERR(old_file))
471 + return PTR_ERR(old_file);
473 + new_file = ovl_path_open(new, O_WRONLY);
474 + if (IS_ERR(new_file)) {
475 + error = PTR_ERR(new_file);
479 + /* FIXME: copy up sparse files efficiently */
481 + loff_t offset = new_file->f_pos;
482 + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
485 + if (len < this_len)
488 + if (signal_pending_state(TASK_KILLABLE, current)) {
493 + bytes = do_splice_direct(old_file, &offset, new_file, this_len,
509 +static char *ovl_read_symlink(struct dentry *realdentry)
513 + struct inode *inode = realdentry->d_inode;
514 + mm_segment_t old_fs;
517 + if (!inode->i_op->readlink)
521 + buf = (char *) __get_free_page(GFP_KERNEL);
527 + /* The cast to a user pointer is valid due to the set_fs() */
528 + res = inode->i_op->readlink(realdentry,
529 + (char __user *)buf, PAGE_SIZE - 1);
532 + free_page((unsigned long) buf);
540 + return ERR_PTR(res);
543 +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
545 + struct iattr attr = {
547 + ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
548 + .ia_atime = stat->atime,
549 + .ia_mtime = stat->mtime,
552 + return notify_change(upperdentry, &attr);
555 +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
557 + struct iattr attr = {
558 + .ia_valid = ATTR_MODE,
562 + return notify_change(upperdentry, &attr);
565 +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
566 + struct path *lowerpath, struct kstat *stat,
570 + struct path newpath;
571 + umode_t mode = stat->mode;
573 + /* Can't properly set mode on creation because of the umask */
574 + stat->mode &= S_IFMT;
576 + ovl_path_upper(dentry, &newpath);
577 + WARN_ON(newpath.dentry);
578 + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
579 + if (IS_ERR(newpath.dentry))
580 + return PTR_ERR(newpath.dentry);
582 + if (S_ISREG(stat->mode)) {
583 + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
588 + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
592 + mutex_lock(&newpath.dentry->d_inode->i_mutex);
593 + if (!S_ISLNK(stat->mode))
594 + err = ovl_set_mode(newpath.dentry, mode);
596 + err = ovl_set_timestamps(newpath.dentry, stat);
597 + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
601 + ovl_dentry_update(dentry, newpath.dentry);
604 + * Easiest way to get rid of the lower dentry reference is to
605 + * drop this dentry. This is neither needed nor possible for
608 + if (!S_ISDIR(stat->mode))
614 + if (S_ISDIR(stat->mode))
615 + vfs_rmdir(upperdir->d_inode, newpath.dentry);
617 + vfs_unlink(upperdir->d_inode, newpath.dentry);
619 + dput(newpath.dentry);
625 + * Copy up a single dentry
627 + * Directory renames only allowed on "pure upper" (already created on
628 + * upper filesystem, never copied up). Directories which are on lower or
629 + * are merged may not be renamed. For these -EXDEV is returned and
630 + * userspace has to deal with it. This means, when copying up a
631 + * directory we can rely on it and ancestors being stable.
633 + * Non-directory renames start with copy up of source if necessary. The
634 + * actual rename will only proceed once the copy up was successful. Copy
635 + * up uses upper parent i_mutex for exclusion. Since rename can change
636 + * d_parent it is possible that the copy up will lock the old parent. At
637 + * that point the file will have already been copied up anyway.
639 +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
640 + struct path *lowerpath, struct kstat *stat)
643 + struct kstat pstat;
644 + struct path parentpath;
645 + struct dentry *upperdir;
646 + const struct cred *old_cred;
647 + struct cred *override_cred;
650 + ovl_path_upper(parent, &parentpath);
651 + upperdir = parentpath.dentry;
653 + err = vfs_getattr(parentpath.mnt, parentpath.dentry, &pstat);
657 + if (S_ISLNK(stat->mode)) {
658 + link = ovl_read_symlink(lowerpath->dentry);
660 + return PTR_ERR(link);
664 + override_cred = prepare_creds();
665 + if (!override_cred)
666 + goto out_free_link;
668 + override_cred->fsuid = stat->uid;
669 + override_cred->fsgid = stat->gid;
671 + * CAP_SYS_ADMIN for copying up extended attributes
672 + * CAP_DAC_OVERRIDE for create
673 + * CAP_FOWNER for chmod, timestamp update
674 + * CAP_FSETID for chmod
675 + * CAP_MKNOD for mknod
677 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
678 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
679 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
680 + cap_raise(override_cred->cap_effective, CAP_FSETID);
681 + cap_raise(override_cred->cap_effective, CAP_MKNOD);
682 + old_cred = override_creds(override_cred);
684 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
685 + if (ovl_path_type(dentry) != OVL_PATH_LOWER) {
688 + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
691 + /* Restore timestamps on parent (best effort) */
692 + ovl_set_timestamps(upperdir, &pstat);
696 + mutex_unlock(&upperdir->d_inode->i_mutex);
698 + revert_creds(old_cred);
699 + put_cred(override_cred);
703 + free_page((unsigned long) link);
708 +int ovl_copy_up(struct dentry *dentry)
714 + struct dentry *next;
715 + struct dentry *parent;
716 + struct path lowerpath;
718 + enum ovl_path_type type = ovl_path_type(dentry);
720 + if (type != OVL_PATH_LOWER)
723 + next = dget(dentry);
724 + /* find the topmost dentry not yet copied up */
726 + parent = dget_parent(next);
728 + type = ovl_path_type(parent);
729 + if (type != OVL_PATH_LOWER)
736 + ovl_path_lower(next, &lowerpath);
737 + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat);
739 + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
748 +/* Optimize by not copying up the file first and truncating later */
749 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
753 + struct path lowerpath;
754 + struct dentry *parent = dget_parent(dentry);
756 + err = ovl_copy_up(parent);
758 + goto out_dput_parent;
760 + ovl_path_lower(dentry, &lowerpath);
761 + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat);
763 + goto out_dput_parent;
765 + if (size < stat.size)
768 + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
775 +++ b/fs/overlayfs/dir.c
779 + * Copyright (C) 2011 Novell Inc.
781 + * This program is free software; you can redistribute it and/or modify it
782 + * under the terms of the GNU General Public License version 2 as published by
783 + * the Free Software Foundation.
786 +#include <linux/fs.h>
787 +#include <linux/namei.h>
788 +#include <linux/xattr.h>
789 +#include <linux/security.h>
790 +#include <linux/cred.h>
791 +#include "overlayfs.h"
793 +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
795 +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
798 + struct dentry *newdentry;
799 + const struct cred *old_cred;
800 + struct cred *override_cred;
802 + /* FIXME: recheck lower dentry to see if whiteout is really needed */
805 + override_cred = prepare_creds();
806 + if (!override_cred)
810 + * CAP_SYS_ADMIN for setxattr
811 + * CAP_DAC_OVERRIDE for symlink creation
812 + * CAP_FOWNER for unlink in sticky directory
814 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
815 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
816 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
817 + override_cred->fsuid = GLOBAL_ROOT_UID;
818 + override_cred->fsgid = GLOBAL_ROOT_GID;
819 + old_cred = override_creds(override_cred);
821 + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
822 + dentry->d_name.len);
823 + err = PTR_ERR(newdentry);
824 + if (IS_ERR(newdentry))
827 + /* Just been removed within the same locked region */
828 + WARN_ON(newdentry->d_inode);
830 + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
834 + ovl_dentry_version_inc(dentry->d_parent);
836 + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
838 + vfs_unlink(upperdir->d_inode, newdentry);
843 + revert_creds(old_cred);
844 + put_cred(override_cred);
848 + * There's no way to recover from failure to whiteout.
849 + * What should we do? Log a big fat error and... ?
851 + printk(KERN_ERR "overlayfs: ERROR - failed to whiteout '%s'\n",
852 + dentry->d_name.name);
858 +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
859 + struct dentry *template)
862 + struct dentry *newdentry;
863 + struct qstr *name = &template->d_name;
865 + newdentry = lookup_one_len(name->name, upperdir, name->len);
866 + if (IS_ERR(newdentry))
869 + if (newdentry->d_inode) {
870 + const struct cred *old_cred;
871 + struct cred *override_cred;
873 + /* No need to check whiteout if lower parent is non-existent */
875 + if (!ovl_dentry_lower(template->d_parent))
878 + if (!S_ISLNK(newdentry->d_inode->i_mode))
882 + override_cred = prepare_creds();
883 + if (!override_cred)
887 + * CAP_SYS_ADMIN for getxattr
888 + * CAP_FOWNER for unlink in sticky directory
890 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
891 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
892 + old_cred = override_creds(override_cred);
895 + if (ovl_is_whiteout(newdentry))
896 + err = vfs_unlink(upperdir->d_inode, newdentry);
898 + revert_creds(old_cred);
899 + put_cred(override_cred);
904 + newdentry = lookup_one_len(name->name, upperdir, name->len);
905 + if (IS_ERR(newdentry)) {
906 + ovl_whiteout(upperdir, template);
911 + * Whiteout just been successfully removed, parent
912 + * i_mutex is still held, there's no way the lookup
913 + * could return positive.
915 + WARN_ON(newdentry->d_inode);
922 + return ERR_PTR(err);
925 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
926 + struct kstat *stat, const char *link)
929 + struct dentry *newdentry;
930 + struct inode *dir = upperdir->d_inode;
932 + newdentry = ovl_lookup_create(upperdir, dentry);
933 + if (IS_ERR(newdentry))
936 + switch (stat->mode & S_IFMT) {
938 + err = vfs_create(dir, newdentry, stat->mode, NULL);
942 + err = vfs_mkdir(dir, newdentry, stat->mode);
949 + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
953 + err = vfs_symlink(dir, newdentry, link);
960 + if (ovl_dentry_is_opaque(dentry))
961 + ovl_whiteout(upperdir, dentry);
963 + newdentry = ERR_PTR(err);
964 + } else if (WARN_ON(!newdentry->d_inode)) {
966 + * Not quite sure if non-instantiated dentry is legal or not.
967 + * VFS doesn't seem to care so check and warn here.
970 + newdentry = ERR_PTR(-ENOENT);
978 +static int ovl_set_opaque(struct dentry *upperdentry)
981 + const struct cred *old_cred;
982 + struct cred *override_cred;
984 + override_cred = prepare_creds();
985 + if (!override_cred)
988 + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
989 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
990 + old_cred = override_creds(override_cred);
991 + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
992 + revert_creds(old_cred);
993 + put_cred(override_cred);
998 +static int ovl_remove_opaque(struct dentry *upperdentry)
1001 + const struct cred *old_cred;
1002 + struct cred *override_cred;
1004 + override_cred = prepare_creds();
1005 + if (!override_cred)
1008 + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
1009 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1010 + old_cred = override_creds(override_cred);
1011 + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
1012 + revert_creds(old_cred);
1013 + put_cred(override_cred);
1018 +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1019 + struct kstat *stat)
1022 + enum ovl_path_type type;
1023 + struct path realpath;
1025 + type = ovl_path_real(dentry, &realpath);
1026 + err = vfs_getattr(realpath.mnt, realpath.dentry, stat);
1030 + stat->dev = dentry->d_sb->s_dev;
1031 + stat->ino = dentry->d_inode->i_ino;
1034 + * It's probably not worth it to count subdirs to get the
1035 + * correct link count. nlink=1 seems to pacify 'find' and
1036 + * other utilities.
1038 + if (type == OVL_PATH_MERGE)
1044 +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
1048 + struct dentry *newdentry;
1049 + struct dentry *upperdir;
1050 + struct inode *inode;
1051 + struct kstat stat = {
1057 + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
1061 + err = ovl_copy_up(dentry->d_parent);
1065 + upperdir = ovl_dentry_upper(dentry->d_parent);
1066 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1068 + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
1069 + err = PTR_ERR(newdentry);
1070 + if (IS_ERR(newdentry))
1073 + ovl_dentry_version_inc(dentry->d_parent);
1074 + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
1075 + err = ovl_set_opaque(newdentry);
1077 + vfs_rmdir(upperdir->d_inode, newdentry);
1078 + ovl_whiteout(upperdir, dentry);
1082 + ovl_dentry_update(dentry, newdentry);
1083 + ovl_copyattr(newdentry->d_inode, inode);
1084 + d_instantiate(dentry, inode);
1092 + mutex_unlock(&upperdir->d_inode->i_mutex);
1099 +static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1102 + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
1105 +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1107 + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
1110 +static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1113 + return ovl_create_object(dentry, mode, rdev, NULL);
1116 +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
1119 + return ovl_create_object(dentry, S_IFLNK, 0, link);
1122 +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
1125 + enum ovl_path_type type;
1126 + struct path realpath;
1127 + struct dentry *upperdir;
1129 + err = ovl_copy_up(dentry->d_parent);
1133 + upperdir = ovl_dentry_upper(dentry->d_parent);
1134 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1135 + type = ovl_path_real(dentry, &realpath);
1136 + if (type != OVL_PATH_LOWER) {
1138 + if (realpath.dentry->d_parent != upperdir)
1141 + /* FIXME: create whiteout up front and rename to target */
1144 + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
1146 + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
1150 + ovl_dentry_version_inc(dentry->d_parent);
1153 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1154 + err = ovl_whiteout(upperdir, dentry);
1157 + * Keeping this dentry hashed would mean having to release
1158 + * upperpath/lowerpath, which could only be done if we are the
1159 + * sole user of this dentry. Too tricky... Just unhash for
1164 + mutex_unlock(&upperdir->d_inode->i_mutex);
1169 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1171 + return ovl_do_remove(dentry, false);
1175 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
1178 + enum ovl_path_type type;
1180 + type = ovl_path_type(dentry);
1181 + if (type != OVL_PATH_UPPER) {
1182 + err = ovl_check_empty_and_clear(dentry, type);
1187 + return ovl_do_remove(dentry, true);
1190 +static int ovl_link(struct dentry *old, struct inode *newdir,
1191 + struct dentry *new)
1194 + struct dentry *olddentry;
1195 + struct dentry *newdentry;
1196 + struct dentry *upperdir;
1197 + struct inode *newinode;
1199 + err = ovl_copy_up(old);
1203 + err = ovl_copy_up(new->d_parent);
1207 + upperdir = ovl_dentry_upper(new->d_parent);
1208 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1209 + newdentry = ovl_lookup_create(upperdir, new);
1210 + err = PTR_ERR(newdentry);
1211 + if (IS_ERR(newdentry))
1214 + olddentry = ovl_dentry_upper(old);
1215 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
1217 + if (WARN_ON(!newdentry->d_inode)) {
1222 + newinode = ovl_new_inode(old->d_sb, newdentry->d_inode->i_mode,
1226 + ovl_copyattr(upperdir->d_inode, newinode);
1228 + ovl_dentry_version_inc(new->d_parent);
1229 + ovl_dentry_update(new, newdentry);
1231 + d_instantiate(new, newinode);
1234 + if (ovl_dentry_is_opaque(new))
1235 + ovl_whiteout(upperdir, new);
1239 + mutex_unlock(&upperdir->d_inode->i_mutex);
1245 +static int ovl_rename(struct inode *olddir, struct dentry *old,
1246 + struct inode *newdir, struct dentry *new)
1249 + enum ovl_path_type old_type;
1250 + enum ovl_path_type new_type;
1251 + struct dentry *old_upperdir;
1252 + struct dentry *new_upperdir;
1253 + struct dentry *olddentry;
1254 + struct dentry *newdentry;
1255 + struct dentry *trap;
1258 + bool new_create = false;
1259 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
1261 + /* Don't copy up directory trees */
1262 + old_type = ovl_path_type(old);
1263 + if (old_type != OVL_PATH_UPPER && is_dir)
1266 + if (new->d_inode) {
1267 + new_type = ovl_path_type(new);
1269 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
1270 + if (ovl_dentry_lower(old)->d_inode ==
1271 + ovl_dentry_lower(new)->d_inode)
1274 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
1275 + if (ovl_dentry_upper(old)->d_inode ==
1276 + ovl_dentry_upper(new)->d_inode)
1280 + if (new_type != OVL_PATH_UPPER &&
1281 + S_ISDIR(new->d_inode->i_mode)) {
1282 + err = ovl_check_empty_and_clear(new, new_type);
1287 + new_type = OVL_PATH_UPPER;
1290 + err = ovl_copy_up(old);
1294 + err = ovl_copy_up(new->d_parent);
1298 + old_upperdir = ovl_dentry_upper(old->d_parent);
1299 + new_upperdir = ovl_dentry_upper(new->d_parent);
1301 + trap = lock_rename(new_upperdir, old_upperdir);
1303 + olddentry = ovl_dentry_upper(old);
1304 + newdentry = ovl_dentry_upper(new);
1308 + new_create = true;
1309 + newdentry = ovl_lookup_create(new_upperdir, new);
1310 + err = PTR_ERR(newdentry);
1311 + if (IS_ERR(newdentry))
1316 + if (olddentry->d_parent != old_upperdir)
1318 + if (newdentry->d_parent != new_upperdir)
1320 + if (olddentry == trap)
1322 + if (newdentry == trap)
1325 + old_opaque = ovl_dentry_is_opaque(old);
1326 + new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
1328 + if (is_dir && !old_opaque && new_opaque) {
1329 + err = ovl_set_opaque(olddentry);
1334 + err = vfs_rename(old_upperdir->d_inode, olddentry,
1335 + new_upperdir->d_inode, newdentry);
1338 + if (new_create && ovl_dentry_is_opaque(new))
1339 + ovl_whiteout(new_upperdir, new);
1340 + if (is_dir && !old_opaque && new_opaque)
1341 + ovl_remove_opaque(olddentry);
1345 + if (old_type != OVL_PATH_UPPER || old_opaque)
1346 + err = ovl_whiteout(old_upperdir, old);
1347 + if (is_dir && old_opaque && !new_opaque)
1348 + ovl_remove_opaque(olddentry);
1350 + if (old_opaque != new_opaque)
1351 + ovl_dentry_set_opaque(old, new_opaque);
1353 + ovl_dentry_version_inc(old->d_parent);
1354 + ovl_dentry_version_inc(new->d_parent);
1359 + unlock_rename(new_upperdir, old_upperdir);
1363 +const struct inode_operations ovl_dir_inode_operations = {
1364 + .lookup = ovl_lookup,
1365 + .mkdir = ovl_mkdir,
1366 + .symlink = ovl_symlink,
1367 + .unlink = ovl_unlink,
1368 + .rmdir = ovl_rmdir,
1369 + .rename = ovl_rename,
1371 + .setattr = ovl_setattr,
1372 + .create = ovl_create,
1373 + .mknod = ovl_mknod,
1374 + .permission = ovl_permission,
1375 + .getattr = ovl_dir_getattr,
1376 + .setxattr = ovl_setxattr,
1377 + .getxattr = ovl_getxattr,
1378 + .listxattr = ovl_listxattr,
1379 + .removexattr = ovl_removexattr,
1382 +++ b/fs/overlayfs/inode.c
1386 + * Copyright (C) 2011 Novell Inc.
1388 + * This program is free software; you can redistribute it and/or modify it
1389 + * under the terms of the GNU General Public License version 2 as published by
1390 + * the Free Software Foundation.
1393 +#include <linux/fs.h>
1394 +#include <linux/slab.h>
1395 +#include <linux/xattr.h>
1396 +#include "overlayfs.h"
1398 +int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1400 + struct dentry *upperdentry;
1403 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1404 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1406 + err = ovl_copy_up(dentry);
1410 + upperdentry = ovl_dentry_upper(dentry);
1412 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1413 + attr->ia_valid &= ~ATTR_MODE;
1415 + mutex_lock(&upperdentry->d_inode->i_mutex);
1416 + err = notify_change(upperdentry, attr);
1418 + ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
1419 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1424 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1425 + struct kstat *stat)
1427 + struct path realpath;
1429 + ovl_path_real(dentry, &realpath);
1430 + return vfs_getattr(realpath.mnt, realpath.dentry, stat);
1433 +int ovl_permission(struct inode *inode, int mask)
1435 + struct ovl_entry *oe;
1436 + struct dentry *alias = NULL;
1437 + struct inode *realinode;
1438 + struct dentry *realdentry;
1442 + if (S_ISDIR(inode->i_mode)) {
1443 + oe = inode->i_private;
1444 + } else if (mask & MAY_NOT_BLOCK) {
1448 + * For non-directories find an alias and get the info
1451 + alias = d_find_any_alias(inode);
1452 + if (WARN_ON(!alias))
1455 + oe = alias->d_fsdata;
1458 + realdentry = ovl_entry_real(oe, &is_upper);
1460 + /* Careful in RCU walk mode */
1461 + realinode = ACCESS_ONCE(realdentry->d_inode);
1463 + WARN_ON(!(mask & MAY_NOT_BLOCK));
1468 + if (mask & MAY_WRITE) {
1469 + umode_t mode = realinode->i_mode;
1472 + * Writes will always be redirected to upper layer, so
1473 + * ignore lower layer being read-only.
1475 + * If the overlay itself is read-only then proceed
1476 + * with the permission check, don't return EROFS.
1477 + * This will only happen if this is the lower layer of
1478 + * another overlayfs.
1480 + * If upper fs becomes read-only after the overlay was
1481 + * constructed return EROFS to prevent modification of
1485 + if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
1486 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1490 + err = __inode_permission(realinode, mask);
1497 +struct ovl_link_data {
1498 + struct dentry *realdentry;
1502 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1505 + struct dentry *realdentry;
1506 + struct inode *realinode;
1508 + realdentry = ovl_dentry_real(dentry);
1509 + realinode = realdentry->d_inode;
1511 + if (WARN_ON(!realinode->i_op->follow_link))
1512 + return ERR_PTR(-EPERM);
1514 + ret = realinode->i_op->follow_link(realdentry, nd);
1518 + if (realinode->i_op->put_link) {
1519 + struct ovl_link_data *data;
1521 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1523 + realinode->i_op->put_link(realdentry, nd, ret);
1524 + return ERR_PTR(-ENOMEM);
1526 + data->realdentry = realdentry;
1527 + data->cookie = ret;
1535 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1537 + struct inode *realinode;
1538 + struct ovl_link_data *data = c;
1543 + realinode = data->realdentry->d_inode;
1544 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1548 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1550 + struct path realpath;
1551 + struct inode *realinode;
1553 + ovl_path_real(dentry, &realpath);
1554 + realinode = realpath.dentry->d_inode;
1556 + if (!realinode->i_op->readlink)
1559 + touch_atime(&realpath);
1561 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1565 +static bool ovl_is_private_xattr(const char *name)
1567 + return strncmp(name, "trusted.overlay.", 14) == 0;
1570 +int ovl_setxattr(struct dentry *dentry, const char *name,
1571 + const void *value, size_t size, int flags)
1574 + struct dentry *upperdentry;
1576 + if (ovl_is_private_xattr(name))
1579 + err = ovl_copy_up(dentry);
1583 + upperdentry = ovl_dentry_upper(dentry);
1584 + return vfs_setxattr(upperdentry, name, value, size, flags);
1587 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1588 + void *value, size_t size)
1590 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1591 + ovl_is_private_xattr(name))
1594 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
1597 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
1602 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
1603 + if (res <= 0 || size == 0)
1606 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
1609 + /* filter out private xattrs */
1610 + for (off = 0; off < res;) {
1611 + char *s = list + off;
1612 + size_t slen = strlen(s) + 1;
1614 + BUG_ON(off + slen > res);
1616 + if (ovl_is_private_xattr(s)) {
1618 + memmove(s, s + slen, res - off);
1627 +int ovl_removexattr(struct dentry *dentry, const char *name)
1630 + struct path realpath;
1631 + enum ovl_path_type type;
1633 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1634 + ovl_is_private_xattr(name))
1637 + type = ovl_path_real(dentry, &realpath);
1638 + if (type == OVL_PATH_LOWER) {
1639 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
1643 + err = ovl_copy_up(dentry);
1647 + ovl_path_upper(dentry, &realpath);
1650 + return vfs_removexattr(realpath.dentry, name);
1653 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
1654 + struct dentry *realdentry)
1656 + if (type != OVL_PATH_LOWER)
1659 + if (special_file(realdentry->d_inode->i_mode))
1662 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
1668 +static int ovl_dentry_open(struct dentry *dentry, struct file *file,
1669 + const struct cred *cred)
1672 + struct path realpath;
1673 + enum ovl_path_type type;
1675 + type = ovl_path_real(dentry, &realpath);
1676 + if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
1677 + if (file->f_flags & O_TRUNC)
1678 + err = ovl_copy_up_truncate(dentry, 0);
1680 + err = ovl_copy_up(dentry);
1684 + ovl_path_upper(dentry, &realpath);
1687 + return vfs_open(&realpath, file, cred);
1690 +static const struct inode_operations ovl_file_inode_operations = {
1691 + .setattr = ovl_setattr,
1692 + .permission = ovl_permission,
1693 + .getattr = ovl_getattr,
1694 + .setxattr = ovl_setxattr,
1695 + .getxattr = ovl_getxattr,
1696 + .listxattr = ovl_listxattr,
1697 + .removexattr = ovl_removexattr,
1698 + .dentry_open = ovl_dentry_open,
1701 +static const struct inode_operations ovl_symlink_inode_operations = {
1702 + .setattr = ovl_setattr,
1703 + .follow_link = ovl_follow_link,
1704 + .put_link = ovl_put_link,
1705 + .readlink = ovl_readlink,
1706 + .getattr = ovl_getattr,
1707 + .setxattr = ovl_setxattr,
1708 + .getxattr = ovl_getxattr,
1709 + .listxattr = ovl_listxattr,
1710 + .removexattr = ovl_removexattr,
1713 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1714 + struct ovl_entry *oe)
1716 + struct inode *inode;
1718 + inode = new_inode(sb);
1724 + inode->i_ino = get_next_ino();
1725 + inode->i_mode = mode;
1726 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
1730 + inode->i_private = oe;
1731 + inode->i_op = &ovl_dir_inode_operations;
1732 + inode->i_fop = &ovl_dir_operations;
1736 + inode->i_op = &ovl_symlink_inode_operations;
1744 + inode->i_op = &ovl_file_inode_operations;
1748 + WARN(1, "illegal file type: %i\n", mode);
1757 +++ b/fs/overlayfs/Kconfig
1759 +config OVERLAYFS_FS
1760 + tristate "Overlay filesystem support"
1762 + Add support for overlay filesystem.
1764 +++ b/fs/overlayfs/Makefile
1767 +# Makefile for the overlay filesystem.
1770 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
1772 +overlayfs-objs := super.o inode.o dir.o readdir.o copy_up.o
1774 +++ b/fs/overlayfs/overlayfs.h
1778 + * Copyright (C) 2011 Novell Inc.
1780 + * This program is free software; you can redistribute it and/or modify it
1781 + * under the terms of the GNU General Public License version 2 as published by
1782 + * the Free Software Foundation.
1787 +enum ovl_path_type {
1793 +extern const char *ovl_opaque_xattr;
1794 +extern const char *ovl_whiteout_xattr;
1795 +extern const struct dentry_operations ovl_dentry_operations;
1797 +enum ovl_path_type ovl_path_type(struct dentry *dentry);
1798 +u64 ovl_dentry_version_get(struct dentry *dentry);
1799 +void ovl_dentry_version_inc(struct dentry *dentry);
1800 +void ovl_path_upper(struct dentry *dentry, struct path *path);
1801 +void ovl_path_lower(struct dentry *dentry, struct path *path);
1802 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1803 +struct dentry *ovl_dentry_upper(struct dentry *dentry);
1804 +struct dentry *ovl_dentry_lower(struct dentry *dentry);
1805 +struct dentry *ovl_dentry_real(struct dentry *dentry);
1806 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
1807 +bool ovl_dentry_is_opaque(struct dentry *dentry);
1808 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
1809 +bool ovl_is_whiteout(struct dentry *dentry);
1810 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
1811 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
1812 + unsigned int flags);
1813 +struct file *ovl_path_open(struct path *path, int flags);
1815 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
1816 + struct kstat *stat, const char *link);
1819 +extern const struct file_operations ovl_dir_operations;
1820 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
1823 +int ovl_setattr(struct dentry *dentry, struct iattr *attr);
1824 +int ovl_permission(struct inode *inode, int mask);
1825 +int ovl_setxattr(struct dentry *dentry, const char *name,
1826 + const void *value, size_t size, int flags);
1827 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1828 + void *value, size_t size);
1829 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
1830 +int ovl_removexattr(struct dentry *dentry, const char *name);
1832 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1833 + struct ovl_entry *oe);
1834 +static inline void ovl_copyattr(struct inode *from, struct inode *to)
1836 + to->i_uid = from->i_uid;
1837 + to->i_gid = from->i_gid;
1841 +extern const struct inode_operations ovl_dir_inode_operations;
1844 +int ovl_copy_up(struct dentry *dentry);
1845 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
1847 +++ b/fs/overlayfs/readdir.c
1851 + * Copyright (C) 2011 Novell Inc.
1853 + * This program is free software; you can redistribute it and/or modify it
1854 + * under the terms of the GNU General Public License version 2 as published by
1855 + * the Free Software Foundation.
1858 +#include <linux/fs.h>
1859 +#include <linux/slab.h>
1860 +#include <linux/namei.h>
1861 +#include <linux/file.h>
1862 +#include <linux/xattr.h>
1863 +#include <linux/rbtree.h>
1864 +#include <linux/security.h>
1865 +#include <linux/cred.h>
1866 +#include "overlayfs.h"
1868 +struct ovl_cache_entry {
1871 + unsigned int type;
1874 + struct list_head l_node;
1875 + struct rb_node node;
1878 +struct ovl_readdir_data {
1879 + struct rb_root *root;
1880 + struct list_head *list;
1881 + struct list_head *middle;
1882 + struct dentry *dir;
1887 +struct ovl_dir_file {
1890 + struct list_head cursor;
1891 + u64 cache_version;
1892 + struct list_head cache;
1893 + struct file *realfile;
1896 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
1898 + return container_of(n, struct ovl_cache_entry, node);
1901 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
1902 + const char *name, int len)
1904 + struct rb_node *node = root->rb_node;
1908 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
1910 + cmp = strncmp(name, p->name, len);
1912 + node = p->node.rb_right;
1913 + else if (cmp < 0 || len < p->len)
1914 + node = p->node.rb_left;
1922 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
1923 + u64 ino, unsigned int d_type)
1925 + struct ovl_cache_entry *p;
1927 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
1929 + char *name_copy = (char *) (p + 1);
1930 + memcpy(name_copy, name, len);
1931 + name_copy[len] = '\0';
1932 + p->name = name_copy;
1936 + p->is_whiteout = false;
1942 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
1943 + const char *name, int len, u64 ino,
1944 + unsigned int d_type)
1946 + struct rb_node **newp = &rdd->root->rb_node;
1947 + struct rb_node *parent = NULL;
1948 + struct ovl_cache_entry *p;
1952 + struct ovl_cache_entry *tmp;
1955 + tmp = ovl_cache_entry_from_node(*newp);
1956 + cmp = strncmp(name, tmp->name, len);
1958 + newp = &tmp->node.rb_right;
1959 + else if (cmp < 0 || len < tmp->len)
1960 + newp = &tmp->node.rb_left;
1965 + p = ovl_cache_entry_new(name, len, ino, d_type);
1969 + list_add_tail(&p->l_node, rdd->list);
1970 + rb_link_node(&p->node, parent, newp);
1971 + rb_insert_color(&p->node, rdd->root);
1976 +static int ovl_fill_lower(void *buf, const char *name, int namelen,
1977 + loff_t offset, u64 ino, unsigned int d_type)
1979 + struct ovl_readdir_data *rdd = buf;
1980 + struct ovl_cache_entry *p;
1983 + p = ovl_cache_entry_find(rdd->root, name, namelen);
1985 + list_move_tail(&p->l_node, rdd->middle);
1987 + p = ovl_cache_entry_new(name, namelen, ino, d_type);
1989 + rdd->err = -ENOMEM;
1991 + list_add_tail(&p->l_node, rdd->middle);
1997 +static void ovl_cache_free(struct list_head *list)
1999 + struct ovl_cache_entry *p;
2000 + struct ovl_cache_entry *n;
2002 + list_for_each_entry_safe(p, n, list, l_node)
2005 + INIT_LIST_HEAD(list);
2008 +static int ovl_fill_upper(void *buf, const char *name, int namelen,
2009 + loff_t offset, u64 ino, unsigned int d_type)
2011 + struct ovl_readdir_data *rdd = buf;
2014 + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
2017 +static inline int ovl_dir_read(struct path *realpath,
2018 + struct ovl_readdir_data *rdd, filldir_t filler)
2020 + struct file *realfile;
2023 + realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
2024 + if (IS_ERR(realfile))
2025 + return PTR_ERR(realfile);
2030 + err = vfs_readdir(realfile, filler, rdd);
2033 + } while (!err && rdd->count);
2039 +static void ovl_dir_reset(struct file *file)
2041 + struct ovl_dir_file *od = file->private_data;
2042 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
2044 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
2045 + list_del_init(&od->cursor);
2046 + ovl_cache_free(&od->cache);
2047 + od->is_cached = false;
2049 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
2050 + if (od->is_real && type == OVL_PATH_MERGE) {
2051 + fput(od->realfile);
2052 + od->realfile = NULL;
2053 + od->is_real = false;
2057 +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
2059 + struct ovl_cache_entry *p;
2060 + struct dentry *dentry;
2061 + const struct cred *old_cred;
2062 + struct cred *override_cred;
2064 + override_cred = prepare_creds();
2065 + if (!override_cred) {
2066 + ovl_cache_free(rdd->list);
2071 + * CAP_SYS_ADMIN for getxattr
2072 + * CAP_DAC_OVERRIDE for lookup
2074 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2075 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2076 + old_cred = override_creds(override_cred);
2078 + mutex_lock(&rdd->dir->d_inode->i_mutex);
2079 + list_for_each_entry(p, rdd->list, l_node) {
2080 + if (p->type != DT_LNK)
2083 + dentry = lookup_one_len(p->name, rdd->dir, p->len);
2084 + if (IS_ERR(dentry))
2087 + p->is_whiteout = ovl_is_whiteout(dentry);
2090 + mutex_unlock(&rdd->dir->d_inode->i_mutex);
2092 + revert_creds(old_cred);
2093 + put_cred(override_cred);
2098 +static inline int ovl_dir_read_merged(struct path *upperpath,
2099 + struct path *lowerpath,
2100 + struct ovl_readdir_data *rdd)
2103 + struct rb_root root = RB_ROOT;
2104 + struct list_head middle;
2106 + rdd->root = &root;
2107 + if (upperpath->dentry) {
2108 + rdd->dir = upperpath->dentry;
2109 + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
2113 + err = ovl_dir_mark_whiteouts(rdd);
2118 + * Insert lowerpath entries before upperpath ones, this allows
2119 + * offsets to be reasonably constant
2121 + list_add(&middle, rdd->list);
2122 + rdd->middle = &middle;
2123 + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower);
2124 + list_del(&middle);
2131 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
2133 + struct list_head *l;
2136 + l = od->cache.next;
2137 + for (off = 0; off < pos; off++) {
2138 + if (l == &od->cache)
2142 + list_move_tail(&od->cursor, l);
2145 +static int ovl_readdir(struct file *file, void *buf, filldir_t filler)
2147 + struct ovl_dir_file *od = file->private_data;
2151 + ovl_dir_reset(file);
2153 + if (od->is_real) {
2154 + res = vfs_readdir(od->realfile, filler, buf);
2155 + file->f_pos = od->realfile->f_pos;
2160 + if (!od->is_cached) {
2161 + struct path lowerpath;
2162 + struct path upperpath;
2163 + struct ovl_readdir_data rdd = { .list = &od->cache };
2165 + ovl_path_lower(file->f_path.dentry, &lowerpath);
2166 + ovl_path_upper(file->f_path.dentry, &upperpath);
2168 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2170 + ovl_cache_free(rdd.list);
2174 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
2175 + od->is_cached = true;
2177 + ovl_seek_cursor(od, file->f_pos);
2180 + while (od->cursor.next != &od->cache) {
2183 + struct ovl_cache_entry *p;
2185 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
2186 + off = file->f_pos;
2187 + if (!p->is_whiteout) {
2188 + over = filler(buf, p->name, p->len, off, p->ino,
2194 + list_move(&od->cursor, &p->l_node);
2200 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
2203 + struct ovl_dir_file *od = file->private_data;
2205 + mutex_lock(&file->f_dentry->d_inode->i_mutex);
2207 + ovl_dir_reset(file);
2209 + if (od->is_real) {
2210 + res = vfs_llseek(od->realfile, offset, origin);
2211 + file->f_pos = od->realfile->f_pos;
2217 + offset += file->f_pos;
2227 + if (offset != file->f_pos) {
2228 + file->f_pos = offset;
2229 + if (od->is_cached)
2230 + ovl_seek_cursor(od, offset);
2235 + mutex_unlock(&file->f_dentry->d_inode->i_mutex);
2240 +static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
2243 + struct ovl_dir_file *od = file->private_data;
2245 + /* May need to reopen directory if it got copied up */
2246 + if (!od->realfile) {
2247 + struct path upperpath;
2249 + ovl_path_upper(file->f_path.dentry, &upperpath);
2250 + od->realfile = ovl_path_open(&upperpath, O_RDONLY);
2251 + if (IS_ERR(od->realfile))
2252 + return PTR_ERR(od->realfile);
2255 + return vfs_fsync_range(od->realfile, start, end, datasync);
2258 +static int ovl_dir_release(struct inode *inode, struct file *file)
2260 + struct ovl_dir_file *od = file->private_data;
2262 + list_del(&od->cursor);
2263 + ovl_cache_free(&od->cache);
2265 + fput(od->realfile);
2271 +static int ovl_dir_open(struct inode *inode, struct file *file)
2273 + struct path realpath;
2274 + struct file *realfile;
2275 + struct ovl_dir_file *od;
2276 + enum ovl_path_type type;
2278 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
2282 + type = ovl_path_real(file->f_path.dentry, &realpath);
2283 + realfile = ovl_path_open(&realpath, file->f_flags);
2284 + if (IS_ERR(realfile)) {
2286 + return PTR_ERR(realfile);
2288 + INIT_LIST_HEAD(&od->cache);
2289 + INIT_LIST_HEAD(&od->cursor);
2290 + od->is_cached = false;
2291 + od->realfile = realfile;
2292 + od->is_real = (type != OVL_PATH_MERGE);
2293 + file->private_data = od;
2298 +const struct file_operations ovl_dir_operations = {
2299 + .read = generic_read_dir,
2300 + .open = ovl_dir_open,
2301 + .readdir = ovl_readdir,
2302 + .llseek = ovl_dir_llseek,
2303 + .fsync = ovl_dir_fsync,
2304 + .release = ovl_dir_release,
2307 +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
2310 + struct path lowerpath;
2311 + struct path upperpath;
2312 + struct ovl_cache_entry *p;
2313 + struct ovl_readdir_data rdd = { .list = list };
2315 + ovl_path_upper(dentry, &upperpath);
2316 + ovl_path_lower(dentry, &lowerpath);
2318 + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2324 + list_for_each_entry(p, list, l_node) {
2325 + if (p->is_whiteout)
2328 + if (p->name[0] == '.') {
2331 + if (p->len == 2 && p->name[1] == '.')
2341 +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
2343 + struct path upperpath;
2344 + struct dentry *upperdir;
2345 + struct ovl_cache_entry *p;
2346 + const struct cred *old_cred;
2347 + struct cred *override_cred;
2350 + ovl_path_upper(dir, &upperpath);
2351 + upperdir = upperpath.dentry;
2353 + override_cred = prepare_creds();
2354 + if (!override_cred)
2358 + * CAP_DAC_OVERRIDE for lookup and unlink
2359 + * CAP_SYS_ADMIN for setxattr of "trusted" namespace
2360 + * CAP_FOWNER for unlink in sticky directory
2362 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2363 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2364 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
2365 + old_cred = override_creds(override_cred);
2367 + err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
2369 + goto out_revert_creds;
2371 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2372 + list_for_each_entry(p, list, l_node) {
2373 + struct dentry *dentry;
2376 + if (!p->is_whiteout)
2379 + dentry = lookup_one_len(p->name, upperdir, p->len);
2380 + if (IS_ERR(dentry)) {
2381 + printk(KERN_WARNING
2382 + "overlayfs: failed to lookup whiteout %.*s: %li\n",
2383 + p->len, p->name, PTR_ERR(dentry));
2386 + ret = vfs_unlink(upperdir->d_inode, dentry);
2389 + printk(KERN_WARNING
2390 + "overlayfs: failed to unlink whiteout %.*s: %i\n",
2391 + p->len, p->name, ret);
2393 + mutex_unlock(&upperdir->d_inode->i_mutex);
2396 + revert_creds(old_cred);
2397 + put_cred(override_cred);
2402 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
2407 + err = ovl_check_empty_dir(dentry, &list);
2408 + if (!err && type == OVL_PATH_MERGE)
2409 + err = ovl_remove_whiteouts(dentry, &list);
2411 + ovl_cache_free(&list);
2416 +++ b/fs/overlayfs/super.c
2420 + * Copyright (C) 2011 Novell Inc.
2422 + * This program is free software; you can redistribute it and/or modify it
2423 + * under the terms of the GNU General Public License version 2 as published by
2424 + * the Free Software Foundation.
2427 +#include <linux/fs.h>
2428 +#include <linux/namei.h>
2429 +#include <linux/xattr.h>
2430 +#include <linux/security.h>
2431 +#include <linux/mount.h>
2432 +#include <linux/slab.h>
2433 +#include <linux/parser.h>
2434 +#include <linux/module.h>
2435 +#include <linux/cred.h>
2436 +#include <linux/sched.h>
2437 +#include <linux/statfs.h>
2438 +#include <linux/seq_file.h>
2439 +#include "overlayfs.h"
2441 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2442 +MODULE_DESCRIPTION("Overlay filesystem");
2443 +MODULE_LICENSE("GPL");
2445 +#define OVERLAYFS_SUPER_MAGIC 0x794c764f
2447 +struct ovl_config {
2452 +/* private information held for overlayfs's superblock */
2454 + struct vfsmount *upper_mnt;
2455 + struct vfsmount *lower_mnt;
2456 + long lower_namelen;
2457 + /* pathnames of lower and upper dirs, for show_options */
2458 + struct ovl_config config;
2461 +/* private information held for every overlayfs dentry */
2464 + * Keep "double reference" on upper dentries, so that
2465 + * d_delete() doesn't think it's OK to reset d_inode to NULL.
2467 + struct dentry *__upperdentry;
2468 + struct dentry *lowerdentry;
2474 + struct rcu_head rcu;
2478 +const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
2479 +const char *ovl_opaque_xattr = "trusted.overlay.opaque";
2482 +enum ovl_path_type ovl_path_type(struct dentry *dentry)
2484 + struct ovl_entry *oe = dentry->d_fsdata;
2486 + if (oe->__upperdentry) {
2487 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
2488 + return OVL_PATH_MERGE;
2490 + return OVL_PATH_UPPER;
2492 + return OVL_PATH_LOWER;
2496 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
2498 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
2499 + smp_read_barrier_depends();
2500 + return upperdentry;
2503 +void ovl_path_upper(struct dentry *dentry, struct path *path)
2505 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2506 + struct ovl_entry *oe = dentry->d_fsdata;
2508 + path->mnt = ofs->upper_mnt;
2509 + path->dentry = ovl_upperdentry_dereference(oe);
2512 +void ovl_path_lower(struct dentry *dentry, struct path *path)
2514 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2515 + struct ovl_entry *oe = dentry->d_fsdata;
2517 + path->mnt = ofs->lower_mnt;
2518 + path->dentry = oe->lowerdentry;
2521 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
2524 + enum ovl_path_type type = ovl_path_type(dentry);
2526 + if (type == OVL_PATH_LOWER)
2527 + ovl_path_lower(dentry, path);
2529 + ovl_path_upper(dentry, path);
2534 +struct dentry *ovl_dentry_upper(struct dentry *dentry)
2536 + struct ovl_entry *oe = dentry->d_fsdata;
2538 + return ovl_upperdentry_dereference(oe);
2541 +struct dentry *ovl_dentry_lower(struct dentry *dentry)
2543 + struct ovl_entry *oe = dentry->d_fsdata;
2545 + return oe->lowerdentry;
2548 +struct dentry *ovl_dentry_real(struct dentry *dentry)
2550 + struct ovl_entry *oe = dentry->d_fsdata;
2551 + struct dentry *realdentry;
2553 + realdentry = ovl_upperdentry_dereference(oe);
2555 + realdentry = oe->lowerdentry;
2557 + return realdentry;
2560 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
2562 + struct dentry *realdentry;
2564 + realdentry = ovl_upperdentry_dereference(oe);
2568 + realdentry = oe->lowerdentry;
2569 + *is_upper = false;
2571 + return realdentry;
2574 +bool ovl_dentry_is_opaque(struct dentry *dentry)
2576 + struct ovl_entry *oe = dentry->d_fsdata;
2577 + return oe->opaque;
2580 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
2582 + struct ovl_entry *oe = dentry->d_fsdata;
2583 + oe->opaque = opaque;
2586 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
2588 + struct ovl_entry *oe = dentry->d_fsdata;
2590 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
2591 + WARN_ON(oe->__upperdentry);
2592 + BUG_ON(!upperdentry->d_inode);
2594 + oe->__upperdentry = dget(upperdentry);
2597 +void ovl_dentry_version_inc(struct dentry *dentry)
2599 + struct ovl_entry *oe = dentry->d_fsdata;
2601 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2605 +u64 ovl_dentry_version_get(struct dentry *dentry)
2607 + struct ovl_entry *oe = dentry->d_fsdata;
2609 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2610 + return oe->version;
2613 +bool ovl_is_whiteout(struct dentry *dentry)
2620 + if (!dentry->d_inode)
2622 + if (!S_ISLNK(dentry->d_inode->i_mode))
2625 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
2626 + if (res == 1 && val == 'y')
2632 +static bool ovl_is_opaquedir(struct dentry *dentry)
2637 + if (!S_ISDIR(dentry->d_inode->i_mode))
2640 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
2641 + if (res == 1 && val == 'y')
2647 +static void ovl_entry_free(struct rcu_head *head)
2649 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
2653 +static void ovl_dentry_release(struct dentry *dentry)
2655 + struct ovl_entry *oe = dentry->d_fsdata;
2658 + dput(oe->__upperdentry);
2659 + dput(oe->__upperdentry);
2660 + dput(oe->lowerdentry);
2661 + call_rcu(&oe->rcu, ovl_entry_free);
2665 +const struct dentry_operations ovl_dentry_operations = {
2666 + .d_release = ovl_dentry_release,
2669 +static struct ovl_entry *ovl_alloc_entry(void)
2671 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
2674 +static inline struct dentry *ovl_lookup_real(struct dentry *dir,
2675 + struct qstr *name)
2677 + struct dentry *dentry;
2679 + mutex_lock(&dir->d_inode->i_mutex);
2680 + dentry = lookup_one_len(name->name, dir, name->len);
2681 + mutex_unlock(&dir->d_inode->i_mutex);
2683 + if (IS_ERR(dentry)) {
2684 + if (PTR_ERR(dentry) == -ENOENT)
2686 + } else if (!dentry->d_inode) {
2693 +static int ovl_do_lookup(struct dentry *dentry)
2695 + struct ovl_entry *oe;
2696 + struct dentry *upperdir;
2697 + struct dentry *lowerdir;
2698 + struct dentry *upperdentry = NULL;
2699 + struct dentry *lowerdentry = NULL;
2700 + struct inode *inode = NULL;
2704 + oe = ovl_alloc_entry();
2708 + upperdir = ovl_dentry_upper(dentry->d_parent);
2709 + lowerdir = ovl_dentry_lower(dentry->d_parent);
2712 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
2713 + err = PTR_ERR(upperdentry);
2714 + if (IS_ERR(upperdentry))
2717 + if (lowerdir && upperdentry &&
2718 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
2719 + S_ISDIR(upperdentry->d_inode->i_mode))) {
2720 + const struct cred *old_cred;
2721 + struct cred *override_cred;
2724 + override_cred = prepare_creds();
2725 + if (!override_cred)
2726 + goto out_dput_upper;
2728 + /* CAP_SYS_ADMIN needed for getxattr */
2729 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2730 + old_cred = override_creds(override_cred);
2732 + if (ovl_is_opaquedir(upperdentry)) {
2733 + oe->opaque = true;
2734 + } else if (ovl_is_whiteout(upperdentry)) {
2735 + dput(upperdentry);
2736 + upperdentry = NULL;
2737 + oe->opaque = true;
2739 + revert_creds(old_cred);
2740 + put_cred(override_cred);
2743 + if (lowerdir && !oe->opaque) {
2744 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
2745 + err = PTR_ERR(lowerdentry);
2746 + if (IS_ERR(lowerdentry))
2747 + goto out_dput_upper;
2750 + if (lowerdentry && upperdentry &&
2751 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
2752 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
2753 + dput(lowerdentry);
2754 + lowerdentry = NULL;
2755 + oe->opaque = true;
2758 + if (lowerdentry || upperdentry) {
2759 + struct dentry *realdentry;
2761 + realdentry = upperdentry ? upperdentry : lowerdentry;
2763 + inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
2767 + ovl_copyattr(realdentry->d_inode, inode);
2771 + oe->__upperdentry = dget(upperdentry);
2774 + oe->lowerdentry = lowerdentry;
2776 + dentry->d_fsdata = oe;
2777 + dentry->d_op = &ovl_dentry_operations;
2778 + d_add(dentry, inode);
2783 + dput(lowerdentry);
2785 + dput(upperdentry);
2792 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
2793 + unsigned int flags)
2795 + int err = ovl_do_lookup(dentry);
2798 + return ERR_PTR(err);
2803 +struct file *ovl_path_open(struct path *path, int flags)
2806 + return dentry_open(path, flags, current_cred());
2809 +static void ovl_put_super(struct super_block *sb)
2811 + struct ovl_fs *ufs = sb->s_fs_info;
2813 + if (!(sb->s_flags & MS_RDONLY))
2814 + mnt_drop_write(ufs->upper_mnt);
2816 + mntput(ufs->upper_mnt);
2817 + mntput(ufs->lower_mnt);
2819 + kfree(ufs->config.lowerdir);
2820 + kfree(ufs->config.upperdir);
2824 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2826 + int flags = *flagsp;
2827 + struct ovl_fs *ufs = sb->s_fs_info;
2829 + /* When remounting rw or ro, we need to adjust the write access to the
2832 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2833 + /* No change to readonly status */
2836 + if (flags & MS_RDONLY) {
2837 + mnt_drop_write(ufs->upper_mnt);
2840 + return mnt_want_write(ufs->upper_mnt);
2845 + * @sb: The overlayfs super block
2846 + * @buf: The struct kstatfs to fill in with stats
2848 + * Get the filesystem statistics. As writes always target the upper layer
2849 + * filesystem pass the statfs to the same filesystem.
2851 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2853 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2854 + struct dentry *root_dentry = dentry->d_sb->s_root;
2858 + ovl_path_upper(root_dentry, &path);
2860 + err = vfs_statfs(&path, buf);
2862 + buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
2863 + buf->f_type = OVERLAYFS_SUPER_MAGIC;
2870 + * ovl_show_options
2872 + * Prints the mount options for a given superblock.
2873 + * Returns zero; does not fail.
2875 +static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
2877 + struct super_block *sb = dentry->d_sb;
2878 + struct ovl_fs *ufs = sb->s_fs_info;
2880 + seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
2881 + seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
2885 +static const struct super_operations ovl_super_operations = {
2886 + .put_super = ovl_put_super,
2887 + .remount_fs = ovl_remount_fs,
2888 + .statfs = ovl_statfs,
2889 + .show_options = ovl_show_options,
2898 +static const match_table_t ovl_tokens = {
2899 + {Opt_lowerdir, "lowerdir=%s"},
2900 + {Opt_upperdir, "upperdir=%s"},
2904 +static int ovl_parse_opt(char *opt, struct ovl_config *config)
2908 + config->upperdir = NULL;
2909 + config->lowerdir = NULL;
2911 + while ((p = strsep(&opt, ",")) != NULL) {
2913 + substring_t args[MAX_OPT_ARGS];
2918 + token = match_token(p, ovl_tokens, args);
2920 + case Opt_upperdir:
2921 + kfree(config->upperdir);
2922 + config->upperdir = match_strdup(&args[0]);
2923 + if (!config->upperdir)
2927 + case Opt_lowerdir:
2928 + kfree(config->lowerdir);
2929 + config->lowerdir = match_strdup(&args[0]);
2930 + if (!config->lowerdir)
2941 +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
2943 + struct path lowerpath;
2944 + struct path upperpath;
2945 + struct inode *root_inode;
2946 + struct dentry *root_dentry;
2947 + struct ovl_entry *oe;
2948 + struct ovl_fs *ufs;
2949 + struct kstatfs statfs;
2953 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
2957 + err = ovl_parse_opt((char *) data, &ufs->config);
2959 + goto out_free_ufs;
2962 + if (!ufs->config.upperdir || !ufs->config.lowerdir) {
2963 + printk(KERN_ERR "overlayfs: missing upperdir or lowerdir\n");
2964 + goto out_free_config;
2967 + oe = ovl_alloc_entry();
2969 + goto out_free_config;
2971 + err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
2975 + err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
2977 + goto out_put_upperpath;
2980 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
2981 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
2982 + goto out_put_lowerpath;
2984 + err = vfs_statfs(&lowerpath, &statfs);
2986 + printk(KERN_ERR "overlayfs: statfs failed on lowerpath\n");
2987 + goto out_put_lowerpath;
2989 + ufs->lower_namelen = statfs.f_namelen;
2991 + sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
2992 + lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
2995 + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
2996 + printk(KERN_ERR "overlayfs: maximum fs stacking depth exceeded\n");
2997 + goto out_put_lowerpath;
3001 + ufs->upper_mnt = clone_private_mount(&upperpath);
3002 + err = PTR_ERR(ufs->upper_mnt);
3003 + if (IS_ERR(ufs->upper_mnt)) {
3004 + printk(KERN_ERR "overlayfs: failed to clone upperpath\n");
3005 + goto out_put_lowerpath;
3008 + ufs->lower_mnt = clone_private_mount(&lowerpath);
3009 + err = PTR_ERR(ufs->lower_mnt);
3010 + if (IS_ERR(ufs->lower_mnt)) {
3011 + printk(KERN_ERR "overlayfs: failed to clone lowerpath\n");
3012 + goto out_put_upper_mnt;
3016 + * Make lower_mnt R/O. That way fchmod/fchown on lower file
3017 + * will fail instead of modifying lower fs.
3019 + ufs->lower_mnt->mnt_flags |= MNT_READONLY;
3021 + /* If the upper fs is r/o, we mark overlayfs r/o too */
3022 + if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
3023 + sb->s_flags |= MS_RDONLY;
3025 + if (!(sb->s_flags & MS_RDONLY)) {
3026 + err = mnt_want_write(ufs->upper_mnt);
3028 + goto out_put_lower_mnt;
3032 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
3034 + goto out_drop_write;
3036 + root_dentry = d_make_root(root_inode);
3038 + goto out_drop_write;
3040 + mntput(upperpath.mnt);
3041 + mntput(lowerpath.mnt);
3043 + oe->__upperdentry = dget(upperpath.dentry);
3044 + oe->lowerdentry = lowerpath.dentry;
3046 + root_dentry->d_fsdata = oe;
3047 + root_dentry->d_op = &ovl_dentry_operations;
3049 + sb->s_magic = OVERLAYFS_SUPER_MAGIC;
3050 + sb->s_op = &ovl_super_operations;
3051 + sb->s_root = root_dentry;
3052 + sb->s_fs_info = ufs;
3057 + if (!(sb->s_flags & MS_RDONLY))
3058 + mnt_drop_write(ufs->upper_mnt);
3060 + mntput(ufs->lower_mnt);
3062 + mntput(ufs->upper_mnt);
3064 + path_put(&lowerpath);
3066 + path_put(&upperpath);
3070 + kfree(ufs->config.lowerdir);
3071 + kfree(ufs->config.upperdir);
3078 +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
3079 + const char *dev_name, void *raw_data)
3081 + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
3084 +static struct file_system_type ovl_fs_type = {
3085 + .owner = THIS_MODULE,
3086 + .name = "overlayfs",
3087 + .mount = ovl_mount,
3088 + .kill_sb = kill_anon_super,
3091 +static int __init ovl_init(void)
3093 + return register_filesystem(&ovl_fs_type);
3096 +static void __exit ovl_exit(void)
3098 + unregister_filesystem(&ovl_fs_type);
3101 +module_init(ovl_init);
3102 +module_exit(ovl_exit);
3105 @@ -1307,6 +1307,7 @@ long do_splice_direct(struct file *in, l
3109 +EXPORT_SYMBOL(do_splice_direct);
3111 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
3112 struct pipe_inode_info *opipe,
3113 --- a/include/linux/fs.h
3114 +++ b/include/linux/fs.h
3115 @@ -244,6 +244,12 @@ struct iattr {
3117 #include <linux/quota.h>
3120 + * Maximum number of layers of fs stack. Needs to be limited to
3121 + * prevent kernel stack overflow
3123 +#define FILESYSTEM_MAX_STACK_DEPTH 2
3126 * enum positive_aop_returns - aop return codes with specific semantics
3128 @@ -1320,6 +1326,11 @@ struct super_block {
3130 /* Being remounted read-only */
3131 int s_readonly_remount;
3134 + * Indicates how deep in a filesystem stack this SB is
3136 + int s_stack_depth;
3139 /* superblock cache pruning functions */
3140 @@ -1573,6 +1584,7 @@ struct inode_operations {
3141 int (*atomic_open)(struct inode *, struct dentry *,
3142 struct file *, unsigned open_flag,
3143 umode_t create_mode, int *opened);
3144 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
3145 } ____cacheline_aligned;
3147 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
3148 @@ -2005,6 +2017,7 @@ extern struct file *file_open_name(struc
3149 extern struct file *filp_open(const char *, int, umode_t);
3150 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
3152 +extern int vfs_open(const struct path *, struct file *, const struct cred *);
3153 extern struct file * dentry_open(const struct path *, int, const struct cred *);
3154 extern int filp_close(struct file *, fl_owner_t id);
3156 @@ -2210,6 +2223,7 @@ extern sector_t bmap(struct inode *, sec
3158 extern int notify_change(struct dentry *, struct iattr *);
3159 extern int inode_permission(struct inode *, int);
3160 +extern int __inode_permission(struct inode *, int);
3161 extern int generic_permission(struct inode *, int);
3163 static inline bool execute_ok(struct inode *inode)
3164 --- a/include/linux/mount.h
3165 +++ b/include/linux/mount.h
3166 @@ -68,6 +68,9 @@ extern void mnt_pin(struct vfsmount *mnt
3167 extern void mnt_unpin(struct vfsmount *mnt);
3168 extern int __mnt_is_readonly(struct vfsmount *mnt);
3171 +extern struct vfsmount *clone_private_mount(struct path *path);
3173 struct file_system_type;
3174 extern struct vfsmount *vfs_kern_mount(struct file_system_type *type,
3175 int flags, const char *name,
3178 @@ -5666,6 +5666,13 @@ F: drivers/scsi/osd/
3179 F: include/scsi/osd_*
3182 +OVERLAYFS FILESYSTEM
3183 +M: Miklos Szeredi <miklos@szeredi.hu>
3184 +L: linux-fsdevel@vger.kernel.org
3187 +F: Documentation/filesystems/overlayfs.txt
3190 M: Christian Lamparter <chunkeey@googlemail.com>
3191 L: linux-wireless@vger.kernel.org