2 +++ b/Documentation/filesystems/overlayfs.txt
4 +Written by: Neil Brown <neilb@suse.de>
9 +This document describes a prototype for a new approach to providing
10 +overlay-filesystem functionality in Linux (sometimes referred to as
11 +union-filesystems). An overlay-filesystem tries to present a
12 +filesystem which is the result over overlaying one filesystem on top
15 +The result will inevitably fail to look exactly like a normal
16 +filesystem for various technical reasons. The expectation is that
17 +many use cases will be able to ignore these differences.
19 +This approach is 'hybrid' because the objects that appear in the
20 +filesystem do not all appear to belong to that filesystem. In many
21 +cases an object accessed in the union will be indistinguishable
22 +from accessing the corresponding object from the original filesystem.
23 +This is most obvious from the 'st_dev' field returned by stat(2).
25 +While directories will report an st_dev from the overlay-filesystem,
26 +all non-directory objects will report an st_dev from the lower or
27 +upper filesystem that is providing the object. Similarly st_ino will
28 +only be unique when combined with st_dev, and both of these can change
29 +over the lifetime of a non-directory object. Many applications and
30 +tools ignore these values and will not be affected.
35 +An overlay filesystem combines two filesystems - an 'upper' filesystem
36 +and a 'lower' filesystem. When a name exists in both filesystems, the
37 +object in the 'upper' filesystem is visible while the object in the
38 +'lower' filesystem is either hidden or, in the case of directories,
39 +merged with the 'upper' object.
41 +It would be more correct to refer to an upper and lower 'directory
42 +tree' rather than 'filesystem' as it is quite possible for both
43 +directory trees to be in the same filesystem and there is no
44 +requirement that the root of a filesystem be given for either upper or
47 +The lower filesystem can be any filesystem supported by Linux and does
48 +not need to be writable. The lower filesystem can even be another
49 +overlayfs. The upper filesystem will normally be writable and if it
50 +is it must support the creation of trusted.* extended attributes, and
51 +must provide valid d_type in readdir responses, at least for symbolic
52 +links - so NFS is not suitable.
54 +A read-only overlay of two read-only filesystems may use any
60 +Overlaying mainly involved directories. If a given name appears in both
61 +upper and lower filesystems and refers to a non-directory in either,
62 +then the lower object is hidden - the name refers only to the upper
65 +Where both upper and lower objects are directories, a merged directory
68 +At mount time, the two directories given as mount options are combined
69 +into a merged directory:
71 + mount -t overlayfs overlayfs -olowerdir=/lower,upperdir=/upper /overlay
73 +Then whenever a lookup is requested in such a merged directory, the
74 +lookup is performed in each actual directory and the combined result
75 +is cached in the dentry belonging to the overlay filesystem. If both
76 +actual lookups find directories, both are stored and a merged
77 +directory is created, otherwise only one is stored: the upper if it
78 +exists, else the lower.
80 +Only the lists of names from directories are merged. Other content
81 +such as metadata and extended attributes are reported for the upper
82 +directory only. These attributes of the lower directory are hidden.
84 +whiteouts and opaque directories
85 +--------------------------------
87 +In order to support rm and rmdir without changing the lower
88 +filesystem, an overlay filesystem needs to record in the upper filesystem
89 +that files have been removed. This is done using whiteouts and opaque
90 +directories (non-directories are always opaque).
92 +The overlay filesystem uses extended attributes with a
93 +"trusted.overlay." prefix to record these details.
95 +A whiteout is created as a symbolic link with target
96 +"(overlay-whiteout)" and with xattr "trusted.overlay.whiteout" set to "y".
97 +When a whiteout is found in the upper level of a merged directory, any
98 +matching name in the lower level is ignored, and the whiteout itself
101 +A directory is made opaque by setting the xattr "trusted.overlay.opaque"
102 +to "y". Where the upper filesystem contains an opaque directory, any
103 +directory in the lower filesystem with the same name is ignored.
108 +When a 'readdir' request is made on a merged directory, the upper and
109 +lower directories are each read and the name lists merged in the
110 +obvious way (upper is read first, then lower - entries that already
111 +exist are not re-added). This merged name list is cached in the
112 +'struct file' and so remains as long as the file is kept open. If the
113 +directory is opened and read by two processes at the same time, they
114 +will each have separate caches. A seekdir to the start of the
115 +directory (offset 0) followed by a readdir will cause the cache to be
116 +discarded and rebuilt.
118 +This means that changes to the merged directory do not appear while a
119 +directory is being read. This is unlikely to be noticed by many
122 +seek offsets are assigned sequentially when the directories are read.
124 + - read part of a directory
125 + - remember an offset, and close the directory
126 + - re-open the directory some time later
127 + - seek to the remembered offset
129 +there may be little correlation between the old and new locations in
130 +the list of filenames, particularly if anything has changed in the
133 +Readdir on directories that are not merged is simply handled by the
134 +underlying directory (upper or lower).
140 +Objects that are not directories (files, symlinks, device-special
141 +files etc.) are presented either from the upper or lower filesystem as
142 +appropriate. When a file in the lower filesystem is accessed in a way
143 +the requires write-access, such as opening for write access, changing
144 +some metadata etc., the file is first copied from the lower filesystem
145 +to the upper filesystem (copy_up). Note that creating a hard-link
146 +also requires copy_up, though of course creation of a symlink does
149 +The copy_up may turn out to be unnecessary, for example if the file is
150 +opened for read-write but the data is not modified.
152 +The copy_up process first makes sure that the containing directory
153 +exists in the upper filesystem - creating it and any parents as
154 +necessary. It then creates the object with the same metadata (owner,
155 +mode, mtime, symlink-target etc.) and then if the object is a file, the
156 +data is copied from the lower to the upper filesystem. Finally any
157 +extended attributes are copied up.
159 +Once the copy_up is complete, the overlay filesystem simply
160 +provides direct access to the newly created file in the upper
161 +filesystem - future operations on the file are barely noticed by the
162 +overlay filesystem (though an operation on the name of the file such as
163 +rename or unlink will of course be noticed and handled).
166 +Non-standard behavior
167 +---------------------
169 +The copy_up operation essentially creates a new, identical file and
170 +moves it over to the old name. The new file may be on a different
171 +filesystem, so both st_dev and st_ino of the file may change.
173 +Any open files referring to this inode will access the old data and
174 +metadata. Similarly any file locks obtained before copy_up will not
175 +apply to the copied up file.
177 +On a file is opened with O_RDONLY fchmod(2), fchown(2), futimesat(2)
178 +and fsetxattr(2) will fail with EROFS.
180 +If a file with multiple hard links is copied up, then this will
181 +"break" the link. Changes will not be propagated to other names
182 +referring to the same inode.
184 +Symlinks in /proc/PID/ and /proc/PID/fd which point to a non-directory
185 +object in overlayfs will not contain vaid absolute paths, only
186 +relative paths leading up to the filesystem's root. This will be
187 +fixed in the future.
189 +Some operations are not atomic, for example a crash during copy_up or
190 +rename will leave the filesystem in an inconsitent state. This will
191 +be addressed in the future.
193 +Changes to underlying filesystems
194 +---------------------------------
196 +Offline changes, when the overlay is not mounted, are allowed to either
197 +the upper or the lower trees.
199 +Changes to the underlying filesystems while part of a mounted overlay
200 +filesystem are not allowed. If the underlying filesystem is changed,
201 +the behavior of the overlay is undefined, though it will not result in
202 +a crash or deadlock.
205 @@ -4792,6 +4792,13 @@ F: drivers/scsi/osd/
206 F: include/scsi/osd_*
209 +OVERLAYFS FILESYSTEM
210 +M: Miklos Szeredi <miklos@szeredi.hu>
211 +L: linux-fsdevel@vger.kernel.org
214 +F: Documentation/filesystems/overlayfs.txt
217 M: Christian Lamparter <chunkeey@googlemail.com>
218 L: linux-wireless@vger.kernel.org
221 @@ -63,6 +63,7 @@ source "fs/quota/Kconfig"
223 source "fs/autofs4/Kconfig"
224 source "fs/fuse/Kconfig"
225 +source "fs/overlayfs/Kconfig"
228 tristate "Character device in Userspace support"
231 @@ -104,6 +104,7 @@ obj-$(CONFIG_QNX4FS_FS) += qnx4/
232 obj-$(CONFIG_AUTOFS4_FS) += autofs4/
233 obj-$(CONFIG_ADFS_FS) += adfs/
234 obj-$(CONFIG_FUSE_FS) += fuse/
235 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
236 obj-$(CONFIG_UDF_FS) += udf/
237 obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
238 obj-$(CONFIG_OMFS_FS) += omfs/
239 --- a/fs/ecryptfs/main.c
240 +++ b/fs/ecryptfs/main.c
241 @@ -544,6 +544,13 @@ static struct dentry *ecryptfs_mount(str
242 s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
243 s->s_blocksize = path.dentry->d_sb->s_blocksize;
244 s->s_magic = ECRYPTFS_SUPER_MAGIC;
245 + s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
248 + if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
249 + printk(KERN_ERR "eCryptfs: maximum fs stacking depth exceeded\n");
253 inode = ecryptfs_get_inode(path.dentry->d_inode, s);
257 @@ -1494,6 +1494,23 @@ void drop_collected_mounts(struct vfsmou
258 release_mounts(&umount_list);
261 +struct vfsmount *clone_private_mount(struct path *path)
263 + struct vfsmount *mnt;
265 + if (IS_MNT_UNBINDABLE(path->mnt))
266 + return ERR_PTR(-EINVAL);
268 + down_read(&namespace_sem);
269 + mnt = clone_mnt(path->mnt, path->dentry, CL_PRIVATE);
270 + up_read(&namespace_sem);
272 + return ERR_PTR(-ENOMEM);
276 +EXPORT_SYMBOL_GPL(clone_private_mount);
278 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
279 struct vfsmount *root)
283 @@ -644,8 +644,7 @@ static inline int __get_file_write_acces
287 -static struct file *__dentry_open(struct dentry *dentry, struct vfsmount *mnt,
289 +static struct file *__dentry_open(struct path *path, struct file *f,
290 int (*open)(struct inode *, struct file *),
291 const struct cred *cred)
293 @@ -653,15 +652,16 @@ static struct file *__dentry_open(struct
298 f->f_mode = OPEN_FMODE(f->f_flags) | FMODE_LSEEK |
299 FMODE_PREAD | FMODE_PWRITE;
301 if (unlikely(f->f_flags & O_PATH))
302 f->f_mode = FMODE_PATH;
304 - inode = dentry->d_inode;
305 + inode = path->dentry->d_inode;
306 if (f->f_mode & FMODE_WRITE) {
307 - error = __get_file_write_access(inode, mnt);
308 + error = __get_file_write_access(inode, path->mnt);
311 if (!special_file(inode->i_mode))
312 @@ -669,8 +669,7 @@ static struct file *__dentry_open(struct
315 f->f_mapping = inode->i_mapping;
316 - f->f_path.dentry = dentry;
317 - f->f_path.mnt = mnt;
320 file_sb_list_add(f, inode->i_sb);
322 @@ -723,7 +722,7 @@ cleanup_all:
323 * here, so just reset the state.
326 - mnt_drop_write(mnt);
327 + mnt_drop_write(path->mnt);
331 @@ -731,8 +730,7 @@ cleanup_all:
332 f->f_path.mnt = NULL;
338 return ERR_PTR(error);
341 @@ -758,14 +756,14 @@ cleanup_file:
342 struct file *lookup_instantiate_filp(struct nameidata *nd, struct dentry *dentry,
343 int (*open)(struct inode *, struct file *))
345 + struct path path = { .dentry = dentry, .mnt = nd->path.mnt };
346 const struct cred *cred = current_cred();
348 if (IS_ERR(nd->intent.open.file))
352 - nd->intent.open.file = __dentry_open(dget(dentry), mntget(nd->path.mnt),
353 - nd->intent.open.file,
354 + nd->intent.open.file = __dentry_open(&path, nd->intent.open.file,
357 return nd->intent.open.file;
358 @@ -794,10 +792,17 @@ struct file *nameidata_to_filp(struct na
360 /* Has the filesystem initialised the file for us? */
361 if (filp->f_path.dentry == NULL) {
362 - path_get(&nd->path);
363 - filp = __dentry_open(nd->path.dentry, nd->path.mnt, filp,
365 + struct inode *inode = nd->path.dentry->d_inode;
367 + if (inode->i_op->open) {
368 + int flags = filp->f_flags;
370 + filp = inode->i_op->open(nd->path.dentry, flags, cred);
372 + filp = __dentry_open(&nd->path, filp, NULL, cred);
379 @@ -808,26 +813,45 @@ struct file *nameidata_to_filp(struct na
380 struct file *dentry_open(struct dentry *dentry, struct vfsmount *mnt, int flags,
381 const struct cred *cred)
386 - validate_creds(cred);
387 + struct path path = { .dentry = dentry, .mnt = mnt };
390 /* We must always pass in a valid mount pointer. */
394 + ret = vfs_open(&path, flags, cred);
399 +EXPORT_SYMBOL(dentry_open);
402 + * vfs_open - open the file at the given path
403 + * @path: path to open
404 + * @flags: open flags
405 + * @cred: credentials to use
407 + * Open the file. If successful, the returned file will have acquired
408 + * an additional reference for path.
410 +struct file *vfs_open(struct path *path, int flags, const struct cred *cred)
413 + struct inode *inode = path->dentry->d_inode;
415 + validate_creds(cred);
417 + if (inode->i_op->open)
418 + return inode->i_op->open(path->dentry, flags, cred);
419 f = get_empty_filp();
423 - return ERR_PTR(error);
426 + return ERR_PTR(-ENFILE);
429 - return __dentry_open(dentry, mnt, f, NULL, cred);
430 + return __dentry_open(path, f, NULL, cred);
432 -EXPORT_SYMBOL(dentry_open);
433 +EXPORT_SYMBOL(vfs_open);
435 static void __put_unused_fd(struct files_struct *files, unsigned int fd)
438 +++ b/fs/overlayfs/Kconfig
441 + tristate "Overlay filesystem support"
443 + Add support for overlay filesystem.
445 +++ b/fs/overlayfs/Makefile
448 +# Makefile for the overlay filesystem.
451 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
453 +overlayfs-objs := super.o inode.o dir.o readdir.o copy_up.o
455 +++ b/fs/overlayfs/copy_up.c
459 + * Copyright (C) 2011 Novell Inc.
461 + * This program is free software; you can redistribute it and/or modify it
462 + * under the terms of the GNU General Public License version 2 as published by
463 + * the Free Software Foundation.
466 +#include <linux/fs.h>
467 +#include <linux/slab.h>
468 +#include <linux/file.h>
469 +#include <linux/splice.h>
470 +#include <linux/xattr.h>
471 +#include <linux/security.h>
472 +#include <linux/uaccess.h>
473 +#include "overlayfs.h"
475 +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
477 +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
479 + ssize_t list_size, size;
480 + char *buf, *name, *value;
483 + if (!old->d_inode->i_op->getxattr ||
484 + !new->d_inode->i_op->getxattr)
487 + list_size = vfs_listxattr(old, NULL, 0);
488 + if (list_size <= 0) {
489 + if (list_size == -EOPNOTSUPP)
494 + buf = kzalloc(list_size, GFP_KERNEL);
499 + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
503 + list_size = vfs_listxattr(old, buf, list_size);
504 + if (list_size <= 0) {
506 + goto out_free_value;
509 + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
510 + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
513 + goto out_free_value;
515 + error = vfs_setxattr(new, name, value, size, 0);
517 + goto out_free_value;
527 +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
529 + struct file *old_file;
530 + struct file *new_file;
536 + old_file = vfs_open(old, O_RDONLY, current_cred());
537 + if (IS_ERR(old_file))
538 + return PTR_ERR(old_file);
540 + new_file = vfs_open(new, O_WRONLY, current_cred());
541 + if (IS_ERR(new_file)) {
542 + error = PTR_ERR(new_file);
546 + /* FIXME: copy up sparse files efficiently */
548 + loff_t offset = new_file->f_pos;
549 + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
552 + if (len < this_len)
555 + if (signal_pending_state(TASK_KILLABLE, current)) {
560 + bytes = do_splice_direct(old_file, &offset, new_file, this_len,
576 +static char *ovl_read_symlink(struct dentry *realdentry)
580 + struct inode *inode = realdentry->d_inode;
581 + mm_segment_t old_fs;
584 + if (!inode->i_op->readlink)
588 + buf = (char *) __get_free_page(GFP_KERNEL);
594 + /* The cast to a user pointer is valid due to the set_fs() */
595 + res = inode->i_op->readlink(realdentry,
596 + (char __user *)buf, PAGE_SIZE - 1);
599 + free_page((unsigned long) buf);
607 + return ERR_PTR(res);
610 +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
612 + struct iattr attr = {
613 + .ia_valid = ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
614 + .ia_atime = stat->atime,
615 + .ia_mtime = stat->mtime,
618 + return notify_change(upperdentry, &attr);
621 +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
623 + struct iattr attr = {
624 + .ia_valid = ATTR_MODE,
628 + return notify_change(upperdentry, &attr);
631 +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
632 + struct path *lowerpath, struct kstat *stat,
636 + struct path newpath;
637 + umode_t mode = stat->mode;
639 + /* Can't properly set mode on creation because of the umask */
640 + stat->mode &= S_IFMT;
642 + ovl_path_upper(dentry, &newpath);
643 + WARN_ON(newpath.dentry);
644 + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
645 + if (IS_ERR(newpath.dentry))
646 + return PTR_ERR(newpath.dentry);
648 + if (S_ISREG(stat->mode)) {
649 + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
654 + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
658 + mutex_lock(&newpath.dentry->d_inode->i_mutex);
659 + if (!S_ISLNK(stat->mode))
660 + err = ovl_set_mode(newpath.dentry, mode);
662 + err = ovl_set_timestamps(newpath.dentry, stat);
663 + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
667 + ovl_dentry_update(dentry, newpath.dentry);
670 + * Easiest way to get rid of the lower dentry reference is to
671 + * drop this dentry. This is neither needed nor possible for
674 + if (!S_ISDIR(stat->mode))
680 + if (S_ISDIR(stat->mode))
681 + vfs_rmdir(upperdir->d_inode, newpath.dentry);
683 + vfs_unlink(upperdir->d_inode, newpath.dentry);
685 + dput(newpath.dentry);
691 + * Copy up a single dentry
693 + * Directory renames only allowed on "pure upper" (already created on
694 + * upper filesystem, never copied up). Directories which are on lower or
695 + * are merged may not be renamed. For these -EXDEV is returned and
696 + * userspace has to deal with it. This means, when copying up a
697 + * directory we can rely on it and ancestors being stable.
699 + * Non-directory renames start with copy up of source if necessary. The
700 + * actual rename will only proceed once the copy up was successful. Copy
701 + * up uses upper parent i_mutex for exclusion. Since rename can change
702 + * d_parent it is possible that the copy up will lock the old parent. At
703 + * that point the file will have already been copied up anyway.
705 +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
706 + struct path *lowerpath, struct kstat *stat)
709 + struct kstat pstat;
710 + struct path parentpath;
711 + struct dentry *upperdir;
712 + const struct cred *old_cred;
713 + struct cred *override_cred;
716 + ovl_path_upper(parent, &parentpath);
717 + upperdir = parentpath.dentry;
719 + err = vfs_getattr(parentpath.mnt, parentpath.dentry, &pstat);
723 + if (S_ISLNK(stat->mode)) {
724 + link = ovl_read_symlink(lowerpath->dentry);
726 + return PTR_ERR(link);
730 + override_cred = prepare_creds();
731 + if (!override_cred)
732 + goto out_free_link;
734 + override_cred->fsuid = stat->uid;
735 + override_cred->fsgid = stat->gid;
737 + * CAP_SYS_ADMIN for copying up extended attributes
738 + * CAP_DAC_OVERRIDE for create
739 + * CAP_FOWNER for chmod, timestamp update
740 + * CAP_FSETID for chmod
741 + * CAP_MKNOD for mknod
743 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
744 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
745 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
746 + cap_raise(override_cred->cap_effective, CAP_FSETID);
747 + cap_raise(override_cred->cap_effective, CAP_MKNOD);
748 + old_cred = override_creds(override_cred);
750 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
751 + if (ovl_path_type(dentry) != OVL_PATH_LOWER) {
754 + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
757 + /* Restore timestamps on parent (best effort) */
758 + ovl_set_timestamps(upperdir, &pstat);
762 + mutex_unlock(&upperdir->d_inode->i_mutex);
764 + revert_creds(old_cred);
765 + put_cred(override_cred);
769 + free_page((unsigned long) link);
774 +int ovl_copy_up(struct dentry *dentry)
780 + struct dentry *next;
781 + struct dentry *parent;
782 + struct path lowerpath;
784 + enum ovl_path_type type = ovl_path_type(dentry);
786 + if (type != OVL_PATH_LOWER)
789 + next = dget(dentry);
790 + /* find the topmost dentry not yet copied up */
792 + parent = dget_parent(next);
794 + type = ovl_path_type(parent);
795 + if (type != OVL_PATH_LOWER)
802 + ovl_path_lower(next, &lowerpath);
803 + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat);
805 + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
814 +/* Optimize by not copying up the file first and truncating later */
815 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
819 + struct path lowerpath;
820 + struct dentry *parent = dget_parent(dentry);
822 + err = ovl_copy_up(parent);
824 + goto out_dput_parent;
826 + ovl_path_lower(dentry, &lowerpath);
827 + err = vfs_getattr(lowerpath.mnt, lowerpath.dentry, &stat);
829 + goto out_dput_parent;
831 + if (size < stat.size)
834 + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
841 +++ b/fs/overlayfs/dir.c
845 + * Copyright (C) 2011 Novell Inc.
847 + * This program is free software; you can redistribute it and/or modify it
848 + * under the terms of the GNU General Public License version 2 as published by
849 + * the Free Software Foundation.
852 +#include <linux/fs.h>
853 +#include <linux/namei.h>
854 +#include <linux/xattr.h>
855 +#include <linux/security.h>
856 +#include "overlayfs.h"
858 +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
860 +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
863 + struct dentry *newdentry;
864 + const struct cred *old_cred;
865 + struct cred *override_cred;
867 + /* FIXME: recheck lower dentry to see if whiteout is really needed */
870 + override_cred = prepare_creds();
871 + if (!override_cred)
875 + * CAP_SYS_ADMIN for setxattr
876 + * CAP_DAC_OVERRIDE for symlink creation
877 + * CAP_FOWNER for unlink in sticky directory
879 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
880 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
881 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
882 + override_cred->fsuid = 0;
883 + override_cred->fsgid = 0;
884 + old_cred = override_creds(override_cred);
886 + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
887 + dentry->d_name.len);
888 + err = PTR_ERR(newdentry);
889 + if (IS_ERR(newdentry))
892 + /* Just been removed within the same locked region */
893 + WARN_ON(newdentry->d_inode);
895 + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
899 + ovl_dentry_version_inc(dentry->d_parent);
901 + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
903 + vfs_unlink(upperdir->d_inode, newdentry);
908 + revert_creds(old_cred);
909 + put_cred(override_cred);
913 + * There's no way to recover from failure to whiteout.
914 + * What should we do? Log a big fat error and... ?
916 + printk(KERN_ERR "overlayfs: ERROR - failed to whiteout '%s'\n",
917 + dentry->d_name.name);
923 +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
924 + struct dentry *template)
927 + struct dentry *newdentry;
928 + struct qstr *name = &template->d_name;
930 + newdentry = lookup_one_len(name->name, upperdir, name->len);
931 + if (IS_ERR(newdentry))
934 + if (newdentry->d_inode) {
935 + const struct cred *old_cred;
936 + struct cred *override_cred;
938 + /* No need to check whiteout if lower parent is non-existent */
940 + if (!ovl_dentry_lower(template->d_parent))
943 + if (!S_ISLNK(newdentry->d_inode->i_mode))
947 + override_cred = prepare_creds();
948 + if (!override_cred)
952 + * CAP_SYS_ADMIN for getxattr
953 + * CAP_FOWNER for unlink in sticky directory
955 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
956 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
957 + old_cred = override_creds(override_cred);
960 + if (ovl_is_whiteout(newdentry))
961 + err = vfs_unlink(upperdir->d_inode, newdentry);
963 + revert_creds(old_cred);
964 + put_cred(override_cred);
969 + newdentry = lookup_one_len(name->name, upperdir, name->len);
970 + if (IS_ERR(newdentry)) {
971 + ovl_whiteout(upperdir, template);
976 + * Whiteout just been successfully removed, parent
977 + * i_mutex is still held, there's no way the lookup
978 + * could return positive.
980 + WARN_ON(newdentry->d_inode);
987 + return ERR_PTR(err);
990 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
991 + struct kstat *stat, const char *link)
994 + struct dentry *newdentry;
995 + struct inode *dir = upperdir->d_inode;
997 + newdentry = ovl_lookup_create(upperdir, dentry);
998 + if (IS_ERR(newdentry))
1001 + switch (stat->mode & S_IFMT) {
1003 + err = vfs_create(dir, newdentry, stat->mode, NULL);
1007 + err = vfs_mkdir(dir, newdentry, stat->mode);
1014 + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
1018 + err = vfs_symlink(dir, newdentry, link);
1025 + if (ovl_dentry_is_opaque(dentry))
1026 + ovl_whiteout(upperdir, dentry);
1028 + newdentry = ERR_PTR(err);
1029 + } else if (WARN_ON(!newdentry->d_inode)) {
1031 + * Not quite sure if non-instantiated dentry is legal or not.
1032 + * VFS doesn't seem to care so check and warn here.
1035 + newdentry = ERR_PTR(-ENOENT);
1043 +static int ovl_set_opaque(struct dentry *upperdentry)
1046 + const struct cred *old_cred;
1047 + struct cred *override_cred;
1049 + override_cred = prepare_creds();
1050 + if (!override_cred)
1053 + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
1054 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1055 + old_cred = override_creds(override_cred);
1056 + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
1057 + revert_creds(old_cred);
1058 + put_cred(override_cred);
1063 +static int ovl_remove_opaque(struct dentry *upperdentry)
1066 + const struct cred *old_cred;
1067 + struct cred *override_cred;
1069 + override_cred = prepare_creds();
1070 + if (!override_cred)
1073 + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
1074 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1075 + old_cred = override_creds(override_cred);
1076 + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
1077 + revert_creds(old_cred);
1078 + put_cred(override_cred);
1083 +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1084 + struct kstat *stat)
1087 + enum ovl_path_type type;
1088 + struct path realpath;
1090 + type = ovl_path_real(dentry, &realpath);
1091 + err = vfs_getattr(realpath.mnt, realpath.dentry, stat);
1095 + stat->dev = dentry->d_sb->s_dev;
1096 + stat->ino = dentry->d_inode->i_ino;
1099 + * It's probably not worth it to count subdirs to get the
1100 + * correct link count. nlink=1 seems to pacify 'find' and
1101 + * other utilities.
1103 + if (type == OVL_PATH_MERGE)
1109 +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
1113 + struct dentry *newdentry;
1114 + struct dentry *upperdir;
1115 + struct inode *inode;
1116 + struct kstat stat = {
1122 + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
1126 + err = ovl_copy_up(dentry->d_parent);
1130 + upperdir = ovl_dentry_upper(dentry->d_parent);
1131 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1133 + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
1134 + err = PTR_ERR(newdentry);
1135 + if (IS_ERR(newdentry))
1138 + ovl_dentry_version_inc(dentry->d_parent);
1139 + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
1140 + err = ovl_set_opaque(newdentry);
1142 + vfs_rmdir(upperdir->d_inode, newdentry);
1143 + ovl_whiteout(upperdir, dentry);
1147 + ovl_dentry_update(dentry, newdentry);
1148 + d_instantiate(dentry, inode);
1156 + mutex_unlock(&upperdir->d_inode->i_mutex);
1163 +static int ovl_create(struct inode *dir, struct dentry *dentry, int mode,
1164 + struct nameidata *nd)
1166 + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
1169 +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1171 + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
1174 +static int ovl_mknod(struct inode *dir, struct dentry *dentry, int mode,
1177 + return ovl_create_object(dentry, mode, rdev, NULL);
1180 +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
1183 + return ovl_create_object(dentry, S_IFLNK, 0, link);
1186 +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
1189 + enum ovl_path_type type;
1190 + struct path realpath;
1191 + struct dentry *upperdir;
1193 + err = ovl_copy_up(dentry->d_parent);
1197 + upperdir = ovl_dentry_upper(dentry->d_parent);
1198 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1199 + type = ovl_path_real(dentry, &realpath);
1200 + if (type != OVL_PATH_LOWER) {
1202 + if (realpath.dentry->d_parent != upperdir)
1205 + /* FIXME: create whiteout up front and rename to target */
1208 + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
1210 + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
1214 + ovl_dentry_version_inc(dentry->d_parent);
1217 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1218 + err = ovl_whiteout(upperdir, dentry);
1221 + * Keeping this dentry hashed would mean having to release
1222 + * upperpath/lowerpath, which could only be done if we are the
1223 + * sole user of this dentry. Too tricky... Just unhash for
1228 + mutex_unlock(&upperdir->d_inode->i_mutex);
1233 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1235 + return ovl_do_remove(dentry, false);
1239 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
1242 + enum ovl_path_type type;
1244 + type = ovl_path_type(dentry);
1245 + if (type != OVL_PATH_UPPER) {
1246 + err = ovl_check_empty_and_clear(dentry, type);
1251 + return ovl_do_remove(dentry, true);
1254 +static int ovl_link(struct dentry *old, struct inode *newdir,
1255 + struct dentry *new)
1258 + struct dentry *olddentry;
1259 + struct dentry *newdentry;
1260 + struct dentry *upperdir;
1262 + err = ovl_copy_up(old);
1266 + err = ovl_copy_up(new->d_parent);
1270 + upperdir = ovl_dentry_upper(new->d_parent);
1271 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1272 + newdentry = ovl_lookup_create(upperdir, new);
1273 + err = PTR_ERR(newdentry);
1274 + if (IS_ERR(newdentry))
1277 + olddentry = ovl_dentry_upper(old);
1278 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
1280 + if (WARN_ON(!newdentry->d_inode)) {
1286 + ovl_dentry_version_inc(new->d_parent);
1287 + ovl_dentry_update(new, newdentry);
1289 + ihold(old->d_inode);
1290 + d_instantiate(new, old->d_inode);
1292 + if (ovl_dentry_is_opaque(new))
1293 + ovl_whiteout(upperdir, new);
1297 + mutex_unlock(&upperdir->d_inode->i_mutex);
1303 +static int ovl_rename(struct inode *olddir, struct dentry *old,
1304 + struct inode *newdir, struct dentry *new)
1307 + enum ovl_path_type old_type;
1308 + enum ovl_path_type new_type;
1309 + struct dentry *old_upperdir;
1310 + struct dentry *new_upperdir;
1311 + struct dentry *olddentry;
1312 + struct dentry *newdentry;
1313 + struct dentry *trap;
1316 + bool new_create = false;
1317 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
1319 + /* Don't copy up directory trees */
1320 + old_type = ovl_path_type(old);
1321 + if (old_type != OVL_PATH_UPPER && is_dir)
1324 + if (new->d_inode) {
1325 + new_type = ovl_path_type(new);
1327 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
1328 + if (ovl_dentry_lower(old)->d_inode ==
1329 + ovl_dentry_lower(new)->d_inode)
1332 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
1333 + if (ovl_dentry_upper(old)->d_inode ==
1334 + ovl_dentry_upper(new)->d_inode)
1338 + if (new_type != OVL_PATH_UPPER &&
1339 + S_ISDIR(new->d_inode->i_mode)) {
1340 + err = ovl_check_empty_and_clear(new, new_type);
1345 + new_type = OVL_PATH_UPPER;
1348 + err = ovl_copy_up(old);
1352 + err = ovl_copy_up(new->d_parent);
1356 + old_upperdir = ovl_dentry_upper(old->d_parent);
1357 + new_upperdir = ovl_dentry_upper(new->d_parent);
1359 + trap = lock_rename(new_upperdir, old_upperdir);
1361 + olddentry = ovl_dentry_upper(old);
1362 + newdentry = ovl_dentry_upper(new);
1366 + new_create = true;
1367 + newdentry = ovl_lookup_create(new_upperdir, new);
1368 + err = PTR_ERR(newdentry);
1369 + if (IS_ERR(newdentry))
1374 + if (olddentry->d_parent != old_upperdir)
1376 + if (newdentry->d_parent != new_upperdir)
1378 + if (olddentry == trap)
1380 + if (newdentry == trap)
1383 + old_opaque = ovl_dentry_is_opaque(old);
1384 + new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
1386 + if (is_dir && !old_opaque && new_opaque) {
1387 + err = ovl_set_opaque(olddentry);
1392 + err = vfs_rename(old_upperdir->d_inode, olddentry,
1393 + new_upperdir->d_inode, newdentry);
1396 + if (new_create && ovl_dentry_is_opaque(new))
1397 + ovl_whiteout(new_upperdir, new);
1398 + if (is_dir && !old_opaque && new_opaque)
1399 + ovl_remove_opaque(olddentry);
1403 + if (old_type != OVL_PATH_UPPER || old_opaque)
1404 + err = ovl_whiteout(old_upperdir, old);
1405 + if (is_dir && old_opaque && !new_opaque)
1406 + ovl_remove_opaque(olddentry);
1408 + if (old_opaque != new_opaque)
1409 + ovl_dentry_set_opaque(old, new_opaque);
1411 + ovl_dentry_version_inc(old->d_parent);
1412 + ovl_dentry_version_inc(new->d_parent);
1417 + unlock_rename(new_upperdir, old_upperdir);
1421 +const struct inode_operations ovl_dir_inode_operations = {
1422 + .lookup = ovl_lookup,
1423 + .mkdir = ovl_mkdir,
1424 + .symlink = ovl_symlink,
1425 + .unlink = ovl_unlink,
1426 + .rmdir = ovl_rmdir,
1427 + .rename = ovl_rename,
1429 + .setattr = ovl_setattr,
1430 + .create = ovl_create,
1431 + .mknod = ovl_mknod,
1432 + .permission = ovl_permission,
1433 + .getattr = ovl_dir_getattr,
1434 + .setxattr = ovl_setxattr,
1435 + .getxattr = ovl_getxattr,
1436 + .listxattr = ovl_listxattr,
1437 + .removexattr = ovl_removexattr,
1440 +++ b/fs/overlayfs/inode.c
1444 + * Copyright (C) 2011 Novell Inc.
1446 + * This program is free software; you can redistribute it and/or modify it
1447 + * under the terms of the GNU General Public License version 2 as published by
1448 + * the Free Software Foundation.
1451 +#include <linux/fs.h>
1452 +#include <linux/slab.h>
1453 +#include <linux/xattr.h>
1454 +#include "overlayfs.h"
1456 +int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1458 + struct dentry *upperdentry;
1461 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1462 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1464 + err = ovl_copy_up(dentry);
1468 + upperdentry = ovl_dentry_upper(dentry);
1470 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1471 + attr->ia_valid &= ~ATTR_MODE;
1473 + mutex_lock(&upperdentry->d_inode->i_mutex);
1474 + err = notify_change(upperdentry, attr);
1475 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1480 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1481 + struct kstat *stat)
1483 + struct path realpath;
1485 + ovl_path_real(dentry, &realpath);
1486 + return vfs_getattr(realpath.mnt, realpath.dentry, stat);
1489 +int ovl_permission(struct inode *inode, int mask)
1491 + struct ovl_entry *oe;
1492 + struct dentry *alias = NULL;
1493 + struct inode *realinode;
1494 + struct dentry *realdentry;
1498 + if (S_ISDIR(inode->i_mode)) {
1499 + oe = inode->i_private;
1500 + } else if (mask & MAY_NOT_BLOCK) {
1504 + * For non-directories find an alias and get the info
1507 + spin_lock(&inode->i_lock);
1508 + if (WARN_ON(list_empty(&inode->i_dentry))) {
1509 + spin_unlock(&inode->i_lock);
1512 + alias = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1514 + spin_unlock(&inode->i_lock);
1515 + oe = alias->d_fsdata;
1518 + realdentry = ovl_entry_real(oe, &is_upper);
1520 + /* Careful in RCU walk mode */
1521 + realinode = ACCESS_ONCE(realdentry->d_inode);
1523 + WARN_ON(!(mask & MAY_NOT_BLOCK));
1528 + if (mask & MAY_WRITE) {
1529 + umode_t mode = realinode->i_mode;
1532 + * Writes will always be redirected to upper layer, so
1533 + * ignore lower layer being read-only.
1535 + * If the overlay itself is read-only then proceed
1536 + * with the permission check, don't return EROFS.
1537 + * This will only happen if this is the lower layer of
1538 + * another overlayfs.
1540 + * If upper fs becomes read-only after the overlay was
1541 + * constructed return EROFS to prevent modification of
1545 + if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
1546 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1550 + * Nobody gets write access to an immutable file.
1553 + if (IS_IMMUTABLE(realinode))
1557 + if (realinode->i_op->permission)
1558 + err = realinode->i_op->permission(realinode, mask);
1560 + err = generic_permission(realinode, mask);
1567 +struct ovl_link_data {
1568 + struct dentry *realdentry;
1572 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1575 + struct dentry *realdentry;
1576 + struct inode *realinode;
1578 + realdentry = ovl_dentry_real(dentry);
1579 + realinode = realdentry->d_inode;
1581 + if (WARN_ON(!realinode->i_op->follow_link))
1582 + return ERR_PTR(-EPERM);
1584 + ret = realinode->i_op->follow_link(realdentry, nd);
1588 + if (realinode->i_op->put_link) {
1589 + struct ovl_link_data *data;
1591 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1593 + realinode->i_op->put_link(realdentry, nd, ret);
1594 + return ERR_PTR(-ENOMEM);
1596 + data->realdentry = realdentry;
1597 + data->cookie = ret;
1605 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1607 + struct inode *realinode;
1608 + struct ovl_link_data *data = c;
1613 + realinode = data->realdentry->d_inode;
1614 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1618 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1620 + struct path realpath;
1621 + struct inode *realinode;
1623 + ovl_path_real(dentry, &realpath);
1624 + realinode = realpath.dentry->d_inode;
1626 + if (!realinode->i_op->readlink)
1629 + touch_atime(realpath.mnt, realpath.dentry);
1631 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1635 +static bool ovl_is_private_xattr(const char *name)
1637 + return strncmp(name, "trusted.overlay.", 14) == 0;
1640 +int ovl_setxattr(struct dentry *dentry, const char *name,
1641 + const void *value, size_t size, int flags)
1644 + struct dentry *upperdentry;
1646 + if (ovl_is_private_xattr(name))
1649 + err = ovl_copy_up(dentry);
1653 + upperdentry = ovl_dentry_upper(dentry);
1654 + return vfs_setxattr(upperdentry, name, value, size, flags);
1657 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1658 + void *value, size_t size)
1660 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1661 + ovl_is_private_xattr(name))
1664 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
1667 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
1672 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
1673 + if (res <= 0 || size == 0)
1676 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
1679 + /* filter out private xattrs */
1680 + for (off = 0; off < res;) {
1681 + char *s = list + off;
1682 + size_t slen = strlen(s) + 1;
1684 + BUG_ON(off + slen > res);
1686 + if (ovl_is_private_xattr(s)) {
1688 + memmove(s, s + slen, res - off);
1697 +int ovl_removexattr(struct dentry *dentry, const char *name)
1700 + struct path realpath;
1701 + enum ovl_path_type type;
1703 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1704 + ovl_is_private_xattr(name))
1707 + type = ovl_path_real(dentry, &realpath);
1708 + if (type == OVL_PATH_LOWER) {
1709 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
1713 + err = ovl_copy_up(dentry);
1717 + ovl_path_upper(dentry, &realpath);
1720 + return vfs_removexattr(realpath.dentry, name);
1723 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
1724 + struct dentry *realdentry)
1726 + if (type != OVL_PATH_LOWER)
1729 + if (special_file(realdentry->d_inode->i_mode))
1732 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
1738 +static struct file *ovl_open(struct dentry *dentry, int flags,
1739 + const struct cred *cred)
1742 + struct path realpath;
1743 + enum ovl_path_type type;
1745 + type = ovl_path_real(dentry, &realpath);
1746 + if (ovl_open_need_copy_up(flags, type, realpath.dentry)) {
1747 + if (flags & O_TRUNC)
1748 + err = ovl_copy_up_truncate(dentry, 0);
1750 + err = ovl_copy_up(dentry);
1752 + return ERR_PTR(err);
1754 + ovl_path_upper(dentry, &realpath);
1757 + return vfs_open(&realpath, flags, cred);
1760 +static const struct inode_operations ovl_file_inode_operations = {
1761 + .setattr = ovl_setattr,
1762 + .permission = ovl_permission,
1763 + .getattr = ovl_getattr,
1764 + .setxattr = ovl_setxattr,
1765 + .getxattr = ovl_getxattr,
1766 + .listxattr = ovl_listxattr,
1767 + .removexattr = ovl_removexattr,
1771 +static const struct inode_operations ovl_symlink_inode_operations = {
1772 + .setattr = ovl_setattr,
1773 + .follow_link = ovl_follow_link,
1774 + .put_link = ovl_put_link,
1775 + .readlink = ovl_readlink,
1776 + .getattr = ovl_getattr,
1777 + .setxattr = ovl_setxattr,
1778 + .getxattr = ovl_getxattr,
1779 + .listxattr = ovl_listxattr,
1780 + .removexattr = ovl_removexattr,
1783 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1784 + struct ovl_entry *oe)
1786 + struct inode *inode;
1788 + inode = new_inode(sb);
1794 + inode->i_ino = get_next_ino();
1795 + inode->i_mode = mode;
1796 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
1800 + inode->i_private = oe;
1801 + inode->i_op = &ovl_dir_inode_operations;
1802 + inode->i_fop = &ovl_dir_operations;
1806 + inode->i_op = &ovl_symlink_inode_operations;
1814 + inode->i_op = &ovl_file_inode_operations;
1818 + WARN(1, "illegal file type: %i\n", mode);
1826 +++ b/fs/overlayfs/overlayfs.h
1830 + * Copyright (C) 2011 Novell Inc.
1832 + * This program is free software; you can redistribute it and/or modify it
1833 + * under the terms of the GNU General Public License version 2 as published by
1834 + * the Free Software Foundation.
1839 +enum ovl_path_type {
1845 +extern const char *ovl_opaque_xattr;
1846 +extern const char *ovl_whiteout_xattr;
1847 +extern const struct dentry_operations ovl_dentry_operations;
1849 +enum ovl_path_type ovl_path_type(struct dentry *dentry);
1850 +u64 ovl_dentry_version_get(struct dentry *dentry);
1851 +void ovl_dentry_version_inc(struct dentry *dentry);
1852 +void ovl_path_upper(struct dentry *dentry, struct path *path);
1853 +void ovl_path_lower(struct dentry *dentry, struct path *path);
1854 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1855 +struct dentry *ovl_dentry_upper(struct dentry *dentry);
1856 +struct dentry *ovl_dentry_lower(struct dentry *dentry);
1857 +struct dentry *ovl_dentry_real(struct dentry *dentry);
1858 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
1859 +bool ovl_dentry_is_opaque(struct dentry *dentry);
1860 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
1861 +bool ovl_is_whiteout(struct dentry *dentry);
1862 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
1863 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
1864 + struct nameidata *nd);
1866 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
1867 + struct kstat *stat, const char *link);
1870 +extern const struct file_operations ovl_dir_operations;
1871 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
1874 +int ovl_setattr(struct dentry *dentry, struct iattr *attr);
1875 +int ovl_permission(struct inode *inode, int mask);
1876 +int ovl_setxattr(struct dentry *dentry, const char *name,
1877 + const void *value, size_t size, int flags);
1878 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1879 + void *value, size_t size);
1880 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
1881 +int ovl_removexattr(struct dentry *dentry, const char *name);
1883 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1884 + struct ovl_entry *oe);
1886 +extern const struct inode_operations ovl_dir_inode_operations;
1889 +int ovl_copy_up(struct dentry *dentry);
1890 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
1892 +++ b/fs/overlayfs/readdir.c
1896 + * Copyright (C) 2011 Novell Inc.
1898 + * This program is free software; you can redistribute it and/or modify it
1899 + * under the terms of the GNU General Public License version 2 as published by
1900 + * the Free Software Foundation.
1903 +#include <linux/fs.h>
1904 +#include <linux/slab.h>
1905 +#include <linux/namei.h>
1906 +#include <linux/file.h>
1907 +#include <linux/xattr.h>
1908 +#include <linux/rbtree.h>
1909 +#include <linux/security.h>
1910 +#include "overlayfs.h"
1912 +struct ovl_cache_entry {
1915 + unsigned int type;
1918 + struct list_head l_node;
1919 + struct rb_node node;
1922 +struct ovl_readdir_data {
1923 + struct rb_root *root;
1924 + struct list_head *list;
1925 + struct list_head *middle;
1926 + struct dentry *dir;
1931 +struct ovl_dir_file {
1934 + struct list_head cursor;
1935 + u64 cache_version;
1936 + struct list_head cache;
1937 + struct file *realfile;
1940 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
1942 + return container_of(n, struct ovl_cache_entry, node);
1945 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
1946 + const char *name, int len)
1948 + struct rb_node *node = root->rb_node;
1952 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
1954 + cmp = strncmp(name, p->name, len);
1956 + node = p->node.rb_right;
1957 + else if (cmp < 0 || len < p->len)
1958 + node = p->node.rb_left;
1966 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
1967 + u64 ino, unsigned int d_type)
1969 + struct ovl_cache_entry *p;
1971 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
1973 + char *name_copy = (char *) (p + 1);
1974 + memcpy(name_copy, name, len);
1975 + name_copy[len] = '\0';
1976 + p->name = name_copy;
1980 + p->is_whiteout = false;
1986 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
1987 + const char *name, int len, u64 ino,
1988 + unsigned int d_type)
1990 + struct rb_node **newp = &rdd->root->rb_node;
1991 + struct rb_node *parent = NULL;
1992 + struct ovl_cache_entry *p;
1996 + struct ovl_cache_entry *tmp;
1999 + tmp = ovl_cache_entry_from_node(*newp);
2000 + cmp = strncmp(name, tmp->name, len);
2002 + newp = &tmp->node.rb_right;
2003 + else if (cmp < 0 || len < tmp->len)
2004 + newp = &tmp->node.rb_left;
2009 + p = ovl_cache_entry_new(name, len, ino, d_type);
2013 + list_add_tail(&p->l_node, rdd->list);
2014 + rb_link_node(&p->node, parent, newp);
2015 + rb_insert_color(&p->node, rdd->root);
2020 +static int ovl_fill_lower(void *buf, const char *name, int namelen,
2021 + loff_t offset, u64 ino, unsigned int d_type)
2023 + struct ovl_readdir_data *rdd = buf;
2024 + struct ovl_cache_entry *p;
2027 + p = ovl_cache_entry_find(rdd->root, name, namelen);
2029 + list_move_tail(&p->l_node, rdd->middle);
2031 + p = ovl_cache_entry_new(name, namelen, ino, d_type);
2033 + rdd->err = -ENOMEM;
2035 + list_add_tail(&p->l_node, rdd->middle);
2041 +static void ovl_cache_free(struct list_head *list)
2043 + struct ovl_cache_entry *p;
2044 + struct ovl_cache_entry *n;
2046 + list_for_each_entry_safe(p, n, list, l_node)
2049 + INIT_LIST_HEAD(list);
2052 +static int ovl_fill_upper(void *buf, const char *name, int namelen,
2053 + loff_t offset, u64 ino, unsigned int d_type)
2055 + struct ovl_readdir_data *rdd = buf;
2058 + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
2061 +static inline int ovl_dir_read(struct path *realpath,
2062 + struct ovl_readdir_data *rdd, filldir_t filler)
2064 + struct file *realfile;
2067 + realfile = vfs_open(realpath, O_RDONLY | O_DIRECTORY, current_cred());
2068 + if (IS_ERR(realfile))
2069 + return PTR_ERR(realfile);
2074 + err = vfs_readdir(realfile, filler, rdd);
2077 + } while (!err && rdd->count);
2083 +static void ovl_dir_reset(struct file *file)
2085 + struct ovl_dir_file *od = file->private_data;
2086 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
2088 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
2089 + list_del_init(&od->cursor);
2090 + ovl_cache_free(&od->cache);
2091 + od->is_cached = false;
2093 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
2094 + if (od->is_real && type == OVL_PATH_MERGE) {
2095 + fput(od->realfile);
2096 + od->realfile = NULL;
2097 + od->is_real = false;
2101 +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
2103 + struct ovl_cache_entry *p;
2104 + struct dentry *dentry;
2105 + const struct cred *old_cred;
2106 + struct cred *override_cred;
2108 + override_cred = prepare_creds();
2109 + if (!override_cred) {
2110 + ovl_cache_free(rdd->list);
2115 + * CAP_SYS_ADMIN for getxattr
2116 + * CAP_DAC_OVERRIDE for lookup
2118 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2119 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2120 + old_cred = override_creds(override_cred);
2122 + mutex_lock(&rdd->dir->d_inode->i_mutex);
2123 + list_for_each_entry(p, rdd->list, l_node) {
2124 + if (p->type != DT_LNK)
2127 + dentry = lookup_one_len(p->name, rdd->dir, p->len);
2128 + if (IS_ERR(dentry))
2131 + p->is_whiteout = ovl_is_whiteout(dentry);
2134 + mutex_unlock(&rdd->dir->d_inode->i_mutex);
2136 + revert_creds(old_cred);
2137 + put_cred(override_cred);
2142 +static inline int ovl_dir_read_merged(struct path *upperpath, struct path *lowerpath,
2143 + struct ovl_readdir_data *rdd)
2146 + struct rb_root root = RB_ROOT;
2147 + struct list_head middle;
2149 + rdd->root = &root;
2150 + if (upperpath->dentry) {
2151 + rdd->dir = upperpath->dentry;
2152 + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
2156 + err = ovl_dir_mark_whiteouts(rdd);
2161 + * Insert lowerpath entries before upperpath ones, this allows
2162 + * offsets to be reasonably constant
2164 + list_add(&middle, rdd->list);
2165 + rdd->middle = &middle;
2166 + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower);
2167 + list_del(&middle);
2174 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
2176 + struct list_head *l;
2179 + l = od->cache.next;
2180 + for (off = 0; off < pos; off++) {
2181 + if (l == &od->cache)
2185 + list_move_tail(&od->cursor, l);
2188 +static int ovl_readdir(struct file *file, void *buf, filldir_t filler)
2190 + struct ovl_dir_file *od = file->private_data;
2194 + ovl_dir_reset(file);
2196 + if (od->is_real) {
2197 + res = vfs_readdir(od->realfile, filler, buf);
2198 + file->f_pos = od->realfile->f_pos;
2203 + if (!od->is_cached) {
2204 + struct path lowerpath;
2205 + struct path upperpath;
2206 + struct ovl_readdir_data rdd = { .list = &od->cache };
2208 + ovl_path_lower(file->f_path.dentry, &lowerpath);
2209 + ovl_path_upper(file->f_path.dentry, &upperpath);
2211 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2213 + ovl_cache_free(rdd.list);
2217 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
2218 + od->is_cached = true;
2220 + ovl_seek_cursor(od, file->f_pos);
2223 + while (od->cursor.next != &od->cache) {
2226 + struct ovl_cache_entry *p;
2228 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
2229 + off = file->f_pos;
2230 + if (!p->is_whiteout) {
2231 + over = filler(buf, p->name, p->len, off, p->ino, p->type);
2236 + list_move(&od->cursor, &p->l_node);
2242 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
2245 + struct ovl_dir_file *od = file->private_data;
2247 + mutex_lock(&file->f_dentry->d_inode->i_mutex);
2249 + ovl_dir_reset(file);
2251 + if (od->is_real) {
2252 + res = vfs_llseek(od->realfile, offset, origin);
2253 + file->f_pos = od->realfile->f_pos;
2259 + offset += file->f_pos;
2269 + if (offset != file->f_pos) {
2270 + file->f_pos = offset;
2271 + if (od->is_cached)
2272 + ovl_seek_cursor(od, offset);
2277 + mutex_unlock(&file->f_dentry->d_inode->i_mutex);
2282 +static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
2285 + struct ovl_dir_file *od = file->private_data;
2287 + /* May need to reopen directory if it got copied up */
2288 + if (!od->realfile) {
2289 + struct path upperpath;
2291 + ovl_path_upper(file->f_path.dentry, &upperpath);
2292 + od->realfile = vfs_open(&upperpath, O_RDONLY, current_cred());
2293 + if (IS_ERR(od->realfile))
2294 + return PTR_ERR(od->realfile);
2297 + return vfs_fsync_range(od->realfile, start, end, datasync);
2300 +static int ovl_dir_release(struct inode *inode, struct file *file)
2302 + struct ovl_dir_file *od = file->private_data;
2304 + list_del(&od->cursor);
2305 + ovl_cache_free(&od->cache);
2307 + fput(od->realfile);
2313 +static int ovl_dir_open(struct inode *inode, struct file *file)
2315 + struct path realpath;
2316 + struct file *realfile;
2317 + struct ovl_dir_file *od;
2318 + enum ovl_path_type type;
2320 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
2324 + type = ovl_path_real(file->f_path.dentry, &realpath);
2325 + realfile = vfs_open(&realpath, file->f_flags, current_cred());
2326 + if (IS_ERR(realfile)) {
2328 + return PTR_ERR(realfile);
2330 + INIT_LIST_HEAD(&od->cache);
2331 + INIT_LIST_HEAD(&od->cursor);
2332 + od->is_cached = false;
2333 + od->realfile = realfile;
2334 + od->is_real = (type != OVL_PATH_MERGE);
2335 + file->private_data = od;
2340 +const struct file_operations ovl_dir_operations = {
2341 + .read = generic_read_dir,
2342 + .open = ovl_dir_open,
2343 + .readdir = ovl_readdir,
2344 + .llseek = ovl_dir_llseek,
2345 + .fsync = ovl_dir_fsync,
2346 + .release = ovl_dir_release,
2349 +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
2352 + struct path lowerpath;
2353 + struct path upperpath;
2354 + struct ovl_cache_entry *p;
2355 + struct ovl_readdir_data rdd = { .list = list };
2357 + ovl_path_upper(dentry, &upperpath);
2358 + ovl_path_lower(dentry, &lowerpath);
2360 + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2366 + list_for_each_entry(p, list, l_node) {
2367 + if (p->is_whiteout)
2370 + if (p->name[0] == '.') {
2373 + if (p->len == 2 && p->name[1] == '.')
2383 +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
2385 + struct path upperpath;
2386 + struct dentry *upperdir;
2387 + struct ovl_cache_entry *p;
2388 + const struct cred *old_cred;
2389 + struct cred *override_cred;
2392 + ovl_path_upper(dir, &upperpath);
2393 + upperdir = upperpath.dentry;
2395 + override_cred = prepare_creds();
2396 + if (!override_cred)
2400 + * CAP_DAC_OVERRIDE for lookup and unlink
2401 + * CAP_SYS_ADMIN for setxattr of "trusted" namespace
2402 + * CAP_FOWNER for unlink in sticky directory
2404 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2405 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2406 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
2407 + old_cred = override_creds(override_cred);
2409 + err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
2411 + goto out_revert_creds;
2413 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2414 + list_for_each_entry(p, list, l_node) {
2415 + struct dentry *dentry;
2418 + if (!p->is_whiteout)
2421 + dentry = lookup_one_len(p->name, upperdir, p->len);
2422 + if (IS_ERR(dentry)) {
2423 + printk(KERN_WARNING "overlayfs: failed to lookup whiteout %.*s: %li\n", p->len, p->name, PTR_ERR(dentry));
2426 + ret = vfs_unlink(upperdir->d_inode, dentry);
2429 + printk(KERN_WARNING "overlayfs: failed to unlink whiteout %.*s: %i\n", p->len, p->name, ret);
2431 + mutex_unlock(&upperdir->d_inode->i_mutex);
2434 + revert_creds(old_cred);
2435 + put_cred(override_cred);
2440 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
2445 + err = ovl_check_empty_dir(dentry, &list);
2446 + if (!err && type == OVL_PATH_MERGE)
2447 + err = ovl_remove_whiteouts(dentry, &list);
2449 + ovl_cache_free(&list);
2454 +++ b/fs/overlayfs/super.c
2458 + * Copyright (C) 2011 Novell Inc.
2460 + * This program is free software; you can redistribute it and/or modify it
2461 + * under the terms of the GNU General Public License version 2 as published by
2462 + * the Free Software Foundation.
2465 +#include <linux/fs.h>
2466 +#include <linux/namei.h>
2467 +#include <linux/xattr.h>
2468 +#include <linux/security.h>
2469 +#include <linux/mount.h>
2470 +#include <linux/slab.h>
2471 +#include <linux/parser.h>
2472 +#include <linux/module.h>
2473 +#include <linux/seq_file.h>
2474 +#include "overlayfs.h"
2476 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2477 +MODULE_DESCRIPTION("Overlay filesystem");
2478 +MODULE_LICENSE("GPL");
2480 +struct ovl_config {
2485 +/* private information held for overlayfs's superblock */
2487 + struct vfsmount *upper_mnt;
2488 + struct vfsmount *lower_mnt;
2489 + /* pathnames of lower and upper dirs, for show_options */
2490 + struct ovl_config config;
2493 +/* private information held for every overlayfs dentry */
2496 + * Keep "double reference" on upper dentries, so that
2497 + * d_delete() doesn't think it's OK to reset d_inode to NULL.
2499 + struct dentry *__upperdentry;
2500 + struct dentry *lowerdentry;
2506 + struct rcu_head rcu;
2510 +const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
2511 +const char *ovl_opaque_xattr = "trusted.overlay.opaque";
2514 +enum ovl_path_type ovl_path_type(struct dentry *dentry)
2516 + struct ovl_entry *oe = dentry->d_fsdata;
2518 + if (oe->__upperdentry) {
2519 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
2520 + return OVL_PATH_MERGE;
2522 + return OVL_PATH_UPPER;
2524 + return OVL_PATH_LOWER;
2528 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
2530 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
2531 + smp_read_barrier_depends();
2532 + return upperdentry;
2535 +void ovl_path_upper(struct dentry *dentry, struct path *path)
2537 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2538 + struct ovl_entry *oe = dentry->d_fsdata;
2540 + path->mnt = ofs->upper_mnt;
2541 + path->dentry = ovl_upperdentry_dereference(oe);
2544 +void ovl_path_lower(struct dentry *dentry, struct path *path)
2546 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2547 + struct ovl_entry *oe = dentry->d_fsdata;
2549 + path->mnt = ofs->lower_mnt;
2550 + path->dentry = oe->lowerdentry;
2553 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
2556 + enum ovl_path_type type = ovl_path_type(dentry);
2558 + if (type == OVL_PATH_LOWER)
2559 + ovl_path_lower(dentry, path);
2561 + ovl_path_upper(dentry, path);
2566 +struct dentry *ovl_dentry_upper(struct dentry *dentry)
2568 + struct ovl_entry *oe = dentry->d_fsdata;
2570 + return ovl_upperdentry_dereference(oe);
2573 +struct dentry *ovl_dentry_lower(struct dentry *dentry)
2575 + struct ovl_entry *oe = dentry->d_fsdata;
2577 + return oe->lowerdentry;
2580 +struct dentry *ovl_dentry_real(struct dentry *dentry)
2582 + struct ovl_entry *oe = dentry->d_fsdata;
2583 + struct dentry *realdentry;
2585 + realdentry = ovl_upperdentry_dereference(oe);
2587 + realdentry = oe->lowerdentry;
2589 + return realdentry;
2592 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
2594 + struct dentry *realdentry;
2596 + realdentry = ovl_upperdentry_dereference(oe);
2600 + realdentry = oe->lowerdentry;
2601 + *is_upper = false;
2603 + return realdentry;
2606 +bool ovl_dentry_is_opaque(struct dentry *dentry)
2608 + struct ovl_entry *oe = dentry->d_fsdata;
2609 + return oe->opaque;
2612 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
2614 + struct ovl_entry *oe = dentry->d_fsdata;
2615 + oe->opaque = opaque;
2618 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
2620 + struct ovl_entry *oe = dentry->d_fsdata;
2622 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
2623 + WARN_ON(oe->__upperdentry);
2624 + BUG_ON(!upperdentry->d_inode);
2626 + oe->__upperdentry = dget(upperdentry);
2629 +void ovl_dentry_version_inc(struct dentry *dentry)
2631 + struct ovl_entry *oe = dentry->d_fsdata;
2633 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2637 +u64 ovl_dentry_version_get(struct dentry *dentry)
2639 + struct ovl_entry *oe = dentry->d_fsdata;
2641 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2642 + return oe->version;
2645 +bool ovl_is_whiteout(struct dentry *dentry)
2652 + if (!dentry->d_inode)
2654 + if (!S_ISLNK(dentry->d_inode->i_mode))
2657 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
2658 + if (res == 1 && val == 'y')
2664 +static bool ovl_is_opaquedir(struct dentry *dentry)
2669 + if (!S_ISDIR(dentry->d_inode->i_mode))
2672 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
2673 + if (res == 1 && val == 'y')
2679 +static void ovl_entry_free(struct rcu_head *head)
2681 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
2685 +static void ovl_dentry_release(struct dentry *dentry)
2687 + struct ovl_entry *oe = dentry->d_fsdata;
2690 + dput(oe->__upperdentry);
2691 + dput(oe->__upperdentry);
2692 + dput(oe->lowerdentry);
2693 + call_rcu(&oe->rcu, ovl_entry_free);
2697 +const struct dentry_operations ovl_dentry_operations = {
2698 + .d_release = ovl_dentry_release,
2701 +static struct ovl_entry *ovl_alloc_entry(void)
2703 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
2706 +static inline struct dentry *ovl_lookup_real(struct dentry *dir, struct qstr *name)
2708 + struct dentry *dentry;
2710 + mutex_lock(&dir->d_inode->i_mutex);
2711 + dentry = lookup_one_len(name->name, dir, name->len);
2712 + mutex_unlock(&dir->d_inode->i_mutex);
2714 + if (IS_ERR(dentry)) {
2715 + if (PTR_ERR(dentry) == -ENOENT)
2717 + } else if (!dentry->d_inode) {
2724 +static int ovl_do_lookup(struct dentry *dentry)
2726 + struct ovl_entry *oe;
2727 + struct dentry *upperdir;
2728 + struct dentry *lowerdir;
2729 + struct dentry *upperdentry = NULL;
2730 + struct dentry *lowerdentry = NULL;
2731 + struct inode *inode = NULL;
2735 + oe = ovl_alloc_entry();
2739 + upperdir = ovl_dentry_upper(dentry->d_parent);
2740 + lowerdir = ovl_dentry_lower(dentry->d_parent);
2743 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
2744 + err = PTR_ERR(upperdentry);
2745 + if (IS_ERR(upperdentry))
2748 + if (lowerdir && upperdentry &&
2749 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
2750 + S_ISDIR(upperdentry->d_inode->i_mode))) {
2751 + const struct cred *old_cred;
2752 + struct cred *override_cred;
2755 + override_cred = prepare_creds();
2756 + if (!override_cred)
2757 + goto out_dput_upper;
2759 + /* CAP_SYS_ADMIN needed for getxattr */
2760 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2761 + old_cred = override_creds(override_cred);
2763 + if (ovl_is_opaquedir(upperdentry)) {
2764 + oe->opaque = true;
2765 + } else if (ovl_is_whiteout(upperdentry)) {
2766 + dput(upperdentry);
2767 + upperdentry = NULL;
2768 + oe->opaque = true;
2770 + revert_creds(old_cred);
2771 + put_cred(override_cred);
2774 + if (lowerdir && !oe->opaque) {
2775 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
2776 + err = PTR_ERR(lowerdentry);
2777 + if (IS_ERR(lowerdentry))
2778 + goto out_dput_upper;
2781 + if (lowerdentry && upperdentry &&
2782 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
2783 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
2784 + dput(lowerdentry);
2785 + lowerdentry = NULL;
2786 + oe->opaque = true;
2789 + if (lowerdentry || upperdentry) {
2790 + struct dentry *realdentry;
2792 + realdentry = upperdentry ? upperdentry : lowerdentry;
2794 + inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode, oe);
2800 + oe->__upperdentry = dget(upperdentry);
2803 + oe->lowerdentry = lowerdentry;
2805 + dentry->d_fsdata = oe;
2806 + dentry->d_op = &ovl_dentry_operations;
2807 + d_add(dentry, inode);
2812 + dput(lowerdentry);
2814 + dput(upperdentry);
2821 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
2822 + struct nameidata *nd)
2824 + int err = ovl_do_lookup(dentry);
2827 + return ERR_PTR(err);
2832 +static void ovl_put_super(struct super_block *sb)
2834 + struct ovl_fs *ufs = sb->s_fs_info;
2836 + if (!(sb->s_flags & MS_RDONLY))
2837 + mnt_drop_write(ufs->upper_mnt);
2839 + mntput(ufs->upper_mnt);
2840 + mntput(ufs->lower_mnt);
2842 + kfree(ufs->config.lowerdir);
2843 + kfree(ufs->config.upperdir);
2847 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2849 + int flags = *flagsp;
2850 + struct ovl_fs *ufs = sb->s_fs_info;
2852 + /* When remounting rw or ro, we need to adjust the write access to the
2855 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2856 + /* No change to readonly status */
2859 + if (flags & MS_RDONLY) {
2860 + mnt_drop_write(ufs->upper_mnt);
2863 + return mnt_want_write(ufs->upper_mnt);
2868 + * @sb: The overlayfs super block
2869 + * @buf: The struct kstatfs to fill in with stats
2871 + * Get the filesystem statistics. As writes always target the upper layer
2872 + * filesystem pass the statfs to the same filesystem.
2874 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2876 + struct dentry *root_dentry = dentry->d_sb->s_root;
2878 + ovl_path_upper(root_dentry, &path);
2880 + if (!path.dentry->d_sb->s_op->statfs)
2882 + return path.dentry->d_sb->s_op->statfs(path.dentry, buf);
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 vfsmount *mnt)
2893 + struct super_block *sb = mnt->mnt_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;
2968 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
2972 + err = ovl_parse_opt((char *) data, &ufs->config);
2974 + goto out_free_ufs;
2977 + if (!ufs->config.upperdir || !ufs->config.lowerdir) {
2978 + printk(KERN_ERR "overlayfs: missing upperdir or lowerdir\n");
2979 + goto out_free_config;
2982 + oe = ovl_alloc_entry();
2984 + goto out_free_config;
2986 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
2990 + err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
2992 + goto out_put_root;
2994 + err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
2996 + goto out_put_upperpath;
2999 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
3000 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
3001 + goto out_put_lowerpath;
3003 + sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
3004 + lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
3007 + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
3008 + printk(KERN_ERR "overlayfs: maximum fs stacking depth exceeded\n");
3009 + goto out_put_lowerpath;
3013 + ufs->upper_mnt = clone_private_mount(&upperpath);
3014 + err = PTR_ERR(ufs->upper_mnt);
3015 + if (IS_ERR(ufs->upper_mnt)) {
3016 + printk(KERN_ERR "overlayfs: failed to clone upperpath\n");
3017 + goto out_put_lowerpath;
3020 + ufs->lower_mnt = clone_private_mount(&lowerpath);
3021 + err = PTR_ERR(ufs->lower_mnt);
3022 + if (IS_ERR(ufs->lower_mnt)) {
3023 + printk(KERN_ERR "overlayfs: failed to clone lowerpath\n");
3024 + goto out_put_upper_mnt;
3028 + * Make lower_mnt R/O. That way fchmod/fchown on lower file
3029 + * will fail instead of modifying lower fs.
3031 + ufs->lower_mnt->mnt_flags |= MNT_READONLY;
3033 + /* If the upper fs is r/o, we mark overlayfs r/o too */
3034 + if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
3035 + sb->s_flags |= MS_RDONLY;
3037 + if (!(sb->s_flags & MS_RDONLY)) {
3038 + err = mnt_want_write(ufs->upper_mnt);
3040 + goto out_put_lower_mnt;
3044 + root_dentry = d_alloc_root(root_inode);
3046 + goto out_drop_write;
3048 + mntput(upperpath.mnt);
3049 + mntput(lowerpath.mnt);
3051 + oe->__upperdentry = dget(upperpath.dentry);
3052 + oe->lowerdentry = lowerpath.dentry;
3054 + root_dentry->d_fsdata = oe;
3055 + root_dentry->d_op = &ovl_dentry_operations;
3057 + sb->s_op = &ovl_super_operations;
3058 + sb->s_root = root_dentry;
3059 + sb->s_fs_info = ufs;
3064 + if (!(sb->s_flags & MS_RDONLY))
3065 + mnt_drop_write(ufs->upper_mnt);
3067 + mntput(ufs->lower_mnt);
3069 + mntput(ufs->upper_mnt);
3071 + path_put(&lowerpath);
3073 + path_put(&upperpath);
3079 + kfree(ufs->config.lowerdir);
3080 + kfree(ufs->config.upperdir);
3087 +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
3088 + const char *dev_name, void *raw_data)
3090 + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
3093 +static struct file_system_type ovl_fs_type = {
3094 + .owner = THIS_MODULE,
3095 + .name = "overlayfs",
3096 + .mount = ovl_mount,
3097 + .kill_sb = kill_anon_super,
3100 +static int __init ovl_init(void)
3102 + return register_filesystem(&ovl_fs_type);
3105 +static void __exit ovl_exit(void)
3107 + unregister_filesystem(&ovl_fs_type);
3110 +module_init(ovl_init);
3111 +module_exit(ovl_exit);
3114 @@ -1300,6 +1300,7 @@ long do_splice_direct(struct file *in, l
3118 +EXPORT_SYMBOL(do_splice_direct);
3120 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
3121 struct pipe_inode_info *opipe,
3122 --- a/include/linux/fs.h
3123 +++ b/include/linux/fs.h
3124 @@ -481,6 +481,12 @@ struct iattr {
3126 #include <linux/quota.h>
3129 + * Maximum number of layers of fs stack. Needs to be limited to
3130 + * prevent kernel stack overflow
3132 +#define FILESYSTEM_MAX_STACK_DEPTH 2
3135 * enum positive_aop_returns - aop return codes with specific semantics
3137 @@ -1461,6 +1467,11 @@ struct super_block {
3138 int cleancache_poolid;
3140 struct shrinker s_shrink; /* per-sb shrinker handle */
3143 + * Indicates how deep in a filesystem stack this SB is
3145 + int s_stack_depth;
3148 /* superblock cache pruning functions */
3149 @@ -1618,6 +1629,7 @@ struct inode_operations {
3150 void (*truncate_range)(struct inode *, loff_t, loff_t);
3151 int (*fiemap)(struct inode *, struct fiemap_extent_info *, u64 start,
3153 + struct file *(*open)(struct dentry *, int flags, const struct cred *);
3154 } ____cacheline_aligned;
3157 @@ -2022,6 +2034,7 @@ extern long do_sys_open(int dfd, const c
3158 extern struct file *filp_open(const char *, int, int);
3159 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
3161 +extern struct file *vfs_open(struct path *, int flags, const struct cred *);
3162 extern struct file * dentry_open(struct dentry *, struct vfsmount *, int,
3163 const struct cred *);
3164 extern int filp_close(struct file *, fl_owner_t id);
3165 --- a/include/linux/mount.h
3166 +++ b/include/linux/mount.h
3167 @@ -100,6 +100,9 @@ extern void mnt_pin(struct vfsmount *mnt
3168 extern void mnt_unpin(struct vfsmount *mnt);
3169 extern int __mnt_is_readonly(struct vfsmount *mnt);
3172 +extern struct vfsmount *clone_private_mount(struct path *path);
3174 extern struct vfsmount *do_kern_mount(const char *fstype, int flags,
3175 const char *name, void *data);