5d1c5f6d3888e8873a9331d18ab1d97373792814
[openwrt/svn-archive/archive.git] / target / linux / generic / patches-3.10 / 100-overlayfs.patch
1 --- a/Documentation/filesystems/Locking
2 +++ b/Documentation/filesystems/Locking
3 @@ -66,6 +66,7 @@ prototypes:
4 int (*atomic_open)(struct inode *, struct dentry *,
5 struct file *, unsigned open_flag,
6 umode_t create_mode, int *opened);
7 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
8
9 locking rules:
10 all may block
11 @@ -93,6 +94,7 @@ removexattr: yes
12 fiemap: no
13 update_time: no
14 atomic_open: yes
15 +dentry_open: no
16
17 Additionally, ->rmdir(), ->unlink() and ->rename() have ->i_mutex on
18 victim.
19 --- /dev/null
20 +++ b/Documentation/filesystems/overlayfs.txt
21 @@ -0,0 +1,199 @@
22 +Written by: Neil Brown <neilb@suse.de>
23 +
24 +Overlay Filesystem
25 +==================
26 +
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
31 +of the other.
32 +
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.
36 +
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).
42 +
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.
49 +
50 +Upper and Lower
51 +---------------
52 +
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.
58 +
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
63 +lower.
64 +
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.
71 +
72 +A read-only overlay of two read-only filesystems may use any
73 +filesystem type.
74 +
75 +Directories
76 +-----------
77 +
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
81 +object.
82 +
83 +Where both upper and lower objects are directories, a merged directory
84 +is formed.
85 +
86 +At mount time, the two directories given as mount options are combined
87 +into a merged directory:
88 +
89 + mount -t overlayfs overlayfs -olowerdir=/lower,upperdir=/upper /overlay
90 +
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.
97 +
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.
101 +
102 +whiteouts and opaque directories
103 +--------------------------------
104 +
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).
109 +
110 +The overlay filesystem uses extended attributes with a
111 +"trusted.overlay." prefix to record these details.
112 +
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
117 +is also hidden.
118 +
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.
122 +
123 +readdir
124 +-------
125 +
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.
135 +
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
138 +programs.
139 +
140 +seek offsets are assigned sequentially when the directories are read.
141 +Thus if
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
146 +
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
149 +directory.
150 +
151 +Readdir on directories that are not merged is simply handled by the
152 +underlying directory (upper or lower).
153 +
154 +
155 +Non-directories
156 +---------------
157 +
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
165 +not.
166 +
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.
169 +
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.
176 +
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).
182 +
183 +
184 +Non-standard behavior
185 +---------------------
186 +
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.
190 +
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.
194 +
195 +On a file opened with O_RDONLY fchmod(2), fchown(2), futimesat(2) and
196 +fsetxattr(2) will fail with EROFS.
197 +
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.
201 +
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.
206 +
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.
210 +
211 +Changes to underlying filesystems
212 +---------------------------------
213 +
214 +Offline changes, when the overlay is not mounted, are allowed to either
215 +the upper or the lower trees.
216 +
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 *);
228 };
229
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.
234
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.
240 +
241
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/MAINTAINERS
245 +++ b/MAINTAINERS
246 @@ -6019,6 +6019,13 @@ F: drivers/scsi/osd/
247 F: include/scsi/osd_*
248 F: fs/exofs/
249
250 +OVERLAYFS FILESYSTEM
251 +M: Miklos Szeredi <miklos@szeredi.hu>
252 +L: linux-fsdevel@vger.kernel.org
253 +S: Supported
254 +F: fs/overlayfs/*
255 +F: Documentation/filesystems/overlayfs.txt
256 +
257 P54 WIRELESS DRIVER
258 M: Christian Lamparter <chunkeey@googlemail.com>
259 L: linux-wireless@vger.kernel.org
260 --- a/fs/Kconfig
261 +++ b/fs/Kconfig
262 @@ -67,6 +67,7 @@ source "fs/quota/Kconfig"
263
264 source "fs/autofs4/Kconfig"
265 source "fs/fuse/Kconfig"
266 +source "fs/overlayfs/Kconfig"
267
268 config GENERIC_ACL
269 bool
270 --- a/fs/Makefile
271 +++ b/fs/Makefile
272 @@ -105,6 +105,7 @@ obj-$(CONFIG_QNX6FS_FS) += qnx6/
273 obj-$(CONFIG_AUTOFS4_FS) += autofs4/
274 obj-$(CONFIG_ADFS_FS) += adfs/
275 obj-$(CONFIG_FUSE_FS) += fuse/
276 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
277 obj-$(CONFIG_UDF_FS) += udf/
278 obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
279 obj-$(CONFIG_OMFS_FS) += omfs/
280 --- a/fs/ecryptfs/main.c
281 +++ b/fs/ecryptfs/main.c
282 @@ -567,6 +567,13 @@ static struct dentry *ecryptfs_mount(str
283 s->s_maxbytes = path.dentry->d_sb->s_maxbytes;
284 s->s_blocksize = path.dentry->d_sb->s_blocksize;
285 s->s_magic = ECRYPTFS_SUPER_MAGIC;
286 + s->s_stack_depth = path.dentry->d_sb->s_stack_depth + 1;
287 +
288 + rc = -EINVAL;
289 + if (s->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
290 + pr_err("eCryptfs: maximum fs stacking depth exceeded\n");
291 + goto out_free;
292 + }
293
294 inode = ecryptfs_get_inode(path.dentry->d_inode, s);
295 rc = PTR_ERR(inode);
296 --- a/fs/internal.h
297 +++ b/fs/internal.h
298 @@ -42,11 +42,6 @@ static inline int __sync_blockdev(struct
299 extern void __init chrdev_init(void);
300
301 /*
302 - * namei.c
303 - */
304 -extern int __inode_permission(struct inode *, int);
305 -
306 -/*
307 * namespace.c
308 */
309 extern int copy_mount_options(const void __user *, unsigned long *);
310 @@ -132,12 +127,6 @@ extern struct dentry *__d_alloc(struct s
311 extern ssize_t __kernel_write(struct file *, const char *, size_t, loff_t *);
312
313 /*
314 - * splice.c
315 - */
316 -extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
317 - loff_t *opos, size_t len, unsigned int flags);
318 -
319 -/*
320 * pipe.c
321 */
322 extern const struct file_operations pipefifo_fops;
323 --- a/fs/namei.c
324 +++ b/fs/namei.c
325 @@ -402,6 +402,7 @@ int __inode_permission(struct inode *ino
326
327 return security_inode_permission(inode, mask);
328 }
329 +EXPORT_SYMBOL(__inode_permission);
330
331 /**
332 * sb_permission - Check superblock-level permissions
333 @@ -2867,9 +2868,12 @@ finish_open_created:
334 error = may_open(&nd->path, acc_mode, open_flag);
335 if (error)
336 goto out;
337 - file->f_path.mnt = nd->path.mnt;
338 - error = finish_open(file, nd->path.dentry, NULL, opened);
339 - if (error) {
340 +
341 + BUG_ON(*opened & FILE_OPENED); /* once it's opened, it's opened */
342 + error = vfs_open(&nd->path, file, current_cred());
343 + if (!error) {
344 + *opened |= FILE_OPENED;
345 + } else {
346 if (error == -EOPENSTALE)
347 goto stale_open;
348 goto out;
349 --- a/fs/namespace.c
350 +++ b/fs/namespace.c
351 @@ -1442,6 +1442,33 @@ void drop_collected_mounts(struct vfsmou
352 namespace_unlock();
353 }
354
355 +/**
356 + * clone_private_mount - create a private clone of a path
357 + *
358 + * This creates a new vfsmount, which will be the clone of @path. The new will
359 + * not be attached anywhere in the namespace and will be private (i.e. changes
360 + * to the originating mount won't be propagated into this).
361 + *
362 + * Release with mntput().
363 + */
364 +struct vfsmount *clone_private_mount(struct path *path)
365 +{
366 + struct mount *old_mnt = real_mount(path->mnt);
367 + struct mount *new_mnt;
368 +
369 + if (IS_MNT_UNBINDABLE(old_mnt))
370 + return ERR_PTR(-EINVAL);
371 +
372 + down_read(&namespace_sem);
373 + new_mnt = clone_mnt(old_mnt, path->dentry, CL_PRIVATE);
374 + up_read(&namespace_sem);
375 + if (IS_ERR(new_mnt))
376 + return ERR_CAST(new_mnt);
377 +
378 + return &new_mnt->mnt;
379 +}
380 +EXPORT_SYMBOL_GPL(clone_private_mount);
381 +
382 int iterate_mounts(int (*f)(struct vfsmount *, void *), void *arg,
383 struct vfsmount *root)
384 {
385 --- a/fs/open.c
386 +++ b/fs/open.c
387 @@ -800,8 +800,7 @@ struct file *dentry_open(const struct pa
388 f = get_empty_filp();
389 if (!IS_ERR(f)) {
390 f->f_flags = flags;
391 - f->f_path = *path;
392 - error = do_dentry_open(f, NULL, cred);
393 + error = vfs_open(path, f, cred);
394 if (!error) {
395 /* from now on we need fput() to dispose of f */
396 error = open_check_o_direct(f);
397 @@ -818,6 +817,26 @@ struct file *dentry_open(const struct pa
398 }
399 EXPORT_SYMBOL(dentry_open);
400
401 +/**
402 + * vfs_open - open the file at the given path
403 + * @path: path to open
404 + * @filp: newly allocated file with f_flag initialized
405 + * @cred: credentials to use
406 + */
407 +int vfs_open(const struct path *path, struct file *filp,
408 + const struct cred *cred)
409 +{
410 + struct inode *inode = path->dentry->d_inode;
411 +
412 + if (inode->i_op->dentry_open)
413 + return inode->i_op->dentry_open(path->dentry, filp, cred);
414 + else {
415 + filp->f_path = *path;
416 + return do_dentry_open(filp, NULL, cred);
417 + }
418 +}
419 +EXPORT_SYMBOL(vfs_open);
420 +
421 static inline int build_open_flags(int flags, umode_t mode, struct open_flags *op)
422 {
423 int lookup_flags = 0;
424 --- /dev/null
425 +++ b/fs/overlayfs/Kconfig
426 @@ -0,0 +1,10 @@
427 +config OVERLAYFS_FS
428 + tristate "Overlay filesystem support"
429 + help
430 + An overlay filesystem combines two filesystems - an 'upper' filesystem
431 + and a 'lower' filesystem. When a name exists in both filesystems, the
432 + object in the 'upper' filesystem is visible while the object in the
433 + 'lower' filesystem is either hidden or, in the case of directories,
434 + merged with the 'upper' object.
435 +
436 + For more information see Documentation/filesystems/overlayfs.txt
437 --- /dev/null
438 +++ b/fs/overlayfs/Makefile
439 @@ -0,0 +1,7 @@
440 +#
441 +# Makefile for the overlay filesystem.
442 +#
443 +
444 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
445 +
446 +overlayfs-objs := super.o inode.o dir.o readdir.o copy_up.o
447 --- /dev/null
448 +++ b/fs/overlayfs/copy_up.c
449 @@ -0,0 +1,387 @@
450 +/*
451 + *
452 + * Copyright (C) 2011 Novell Inc.
453 + *
454 + * This program is free software; you can redistribute it and/or modify it
455 + * under the terms of the GNU General Public License version 2 as published by
456 + * the Free Software Foundation.
457 + */
458 +
459 +#include <linux/fs.h>
460 +#include <linux/slab.h>
461 +#include <linux/file.h>
462 +#include <linux/splice.h>
463 +#include <linux/xattr.h>
464 +#include <linux/security.h>
465 +#include <linux/uaccess.h>
466 +#include <linux/sched.h>
467 +#include "overlayfs.h"
468 +
469 +#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
470 +
471 +static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
472 +{
473 + ssize_t list_size, size;
474 + char *buf, *name, *value;
475 + int error;
476 +
477 + if (!old->d_inode->i_op->getxattr ||
478 + !new->d_inode->i_op->getxattr)
479 + return 0;
480 +
481 + list_size = vfs_listxattr(old, NULL, 0);
482 + if (list_size <= 0) {
483 + if (list_size == -EOPNOTSUPP)
484 + return 0;
485 + return list_size;
486 + }
487 +
488 + buf = kzalloc(list_size, GFP_KERNEL);
489 + if (!buf)
490 + return -ENOMEM;
491 +
492 + error = -ENOMEM;
493 + value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
494 + if (!value)
495 + goto out;
496 +
497 + list_size = vfs_listxattr(old, buf, list_size);
498 + if (list_size <= 0) {
499 + error = list_size;
500 + goto out_free_value;
501 + }
502 +
503 + for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
504 + size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
505 + if (size <= 0) {
506 + error = size;
507 + goto out_free_value;
508 + }
509 + error = vfs_setxattr(new, name, value, size, 0);
510 + if (error)
511 + goto out_free_value;
512 + }
513 +
514 +out_free_value:
515 + kfree(value);
516 +out:
517 + kfree(buf);
518 + return error;
519 +}
520 +
521 +static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
522 +{
523 + struct file *old_file;
524 + struct file *new_file;
525 + loff_t old_pos = 0;
526 + loff_t new_pos = 0;
527 + int error = 0;
528 +
529 + if (len == 0)
530 + return 0;
531 +
532 + old_file = ovl_path_open(old, O_RDONLY);
533 + if (IS_ERR(old_file))
534 + return PTR_ERR(old_file);
535 +
536 + new_file = ovl_path_open(new, O_WRONLY);
537 + if (IS_ERR(new_file)) {
538 + error = PTR_ERR(new_file);
539 + goto out_fput;
540 + }
541 +
542 + /* FIXME: copy up sparse files efficiently */
543 + while (len) {
544 + size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
545 + long bytes;
546 +
547 + if (len < this_len)
548 + this_len = len;
549 +
550 + if (signal_pending_state(TASK_KILLABLE, current)) {
551 + error = -EINTR;
552 + break;
553 + }
554 +
555 + bytes = do_splice_direct(old_file, &old_pos,
556 + new_file, &new_pos,
557 + this_len, SPLICE_F_MOVE);
558 + if (bytes <= 0) {
559 + error = bytes;
560 + break;
561 + }
562 +
563 + len -= bytes;
564 + }
565 +
566 + fput(new_file);
567 +out_fput:
568 + fput(old_file);
569 + return error;
570 +}
571 +
572 +static char *ovl_read_symlink(struct dentry *realdentry)
573 +{
574 + int res;
575 + char *buf;
576 + struct inode *inode = realdentry->d_inode;
577 + mm_segment_t old_fs;
578 +
579 + res = -EINVAL;
580 + if (!inode->i_op->readlink)
581 + goto err;
582 +
583 + res = -ENOMEM;
584 + buf = (char *) __get_free_page(GFP_KERNEL);
585 + if (!buf)
586 + goto err;
587 +
588 + old_fs = get_fs();
589 + set_fs(get_ds());
590 + /* The cast to a user pointer is valid due to the set_fs() */
591 + res = inode->i_op->readlink(realdentry,
592 + (char __user *)buf, PAGE_SIZE - 1);
593 + set_fs(old_fs);
594 + if (res < 0) {
595 + free_page((unsigned long) buf);
596 + goto err;
597 + }
598 + buf[res] = '\0';
599 +
600 + return buf;
601 +
602 +err:
603 + return ERR_PTR(res);
604 +}
605 +
606 +static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
607 +{
608 + struct iattr attr = {
609 + .ia_valid =
610 + ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
611 + .ia_atime = stat->atime,
612 + .ia_mtime = stat->mtime,
613 + };
614 +
615 + return notify_change(upperdentry, &attr);
616 +}
617 +
618 +static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
619 +{
620 + struct iattr attr = {
621 + .ia_valid = ATTR_MODE,
622 + .ia_mode = mode,
623 + };
624 +
625 + return notify_change(upperdentry, &attr);
626 +}
627 +
628 +static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
629 + struct path *lowerpath, struct kstat *stat,
630 + const char *link)
631 +{
632 + int err;
633 + struct path newpath;
634 + umode_t mode = stat->mode;
635 +
636 + /* Can't properly set mode on creation because of the umask */
637 + stat->mode &= S_IFMT;
638 +
639 + ovl_path_upper(dentry, &newpath);
640 + WARN_ON(newpath.dentry);
641 + newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
642 + if (IS_ERR(newpath.dentry))
643 + return PTR_ERR(newpath.dentry);
644 +
645 + if (S_ISREG(stat->mode)) {
646 + err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
647 + if (err)
648 + goto err_remove;
649 + }
650 +
651 + err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
652 + if (err)
653 + goto err_remove;
654 +
655 + mutex_lock(&newpath.dentry->d_inode->i_mutex);
656 + if (!S_ISLNK(stat->mode))
657 + err = ovl_set_mode(newpath.dentry, mode);
658 + if (!err)
659 + err = ovl_set_timestamps(newpath.dentry, stat);
660 + mutex_unlock(&newpath.dentry->d_inode->i_mutex);
661 + if (err)
662 + goto err_remove;
663 +
664 + ovl_dentry_update(dentry, newpath.dentry);
665 +
666 + /*
667 + * Easiest way to get rid of the lower dentry reference is to
668 + * drop this dentry. This is neither needed nor possible for
669 + * directories.
670 + */
671 + if (!S_ISDIR(stat->mode))
672 + d_drop(dentry);
673 +
674 + return 0;
675 +
676 +err_remove:
677 + if (S_ISDIR(stat->mode))
678 + vfs_rmdir(upperdir->d_inode, newpath.dentry);
679 + else
680 + vfs_unlink(upperdir->d_inode, newpath.dentry);
681 +
682 + dput(newpath.dentry);
683 +
684 + return err;
685 +}
686 +
687 +/*
688 + * Copy up a single dentry
689 + *
690 + * Directory renames only allowed on "pure upper" (already created on
691 + * upper filesystem, never copied up). Directories which are on lower or
692 + * are merged may not be renamed. For these -EXDEV is returned and
693 + * userspace has to deal with it. This means, when copying up a
694 + * directory we can rely on it and ancestors being stable.
695 + *
696 + * Non-directory renames start with copy up of source if necessary. The
697 + * actual rename will only proceed once the copy up was successful. Copy
698 + * up uses upper parent i_mutex for exclusion. Since rename can change
699 + * d_parent it is possible that the copy up will lock the old parent. At
700 + * that point the file will have already been copied up anyway.
701 + */
702 +static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
703 + struct path *lowerpath, struct kstat *stat)
704 +{
705 + int err;
706 + struct kstat pstat;
707 + struct path parentpath;
708 + struct dentry *upperdir;
709 + const struct cred *old_cred;
710 + struct cred *override_cred;
711 + char *link = NULL;
712 +
713 + ovl_path_upper(parent, &parentpath);
714 + upperdir = parentpath.dentry;
715 +
716 + err = vfs_getattr(&parentpath, &pstat);
717 + if (err)
718 + return err;
719 +
720 + if (S_ISLNK(stat->mode)) {
721 + link = ovl_read_symlink(lowerpath->dentry);
722 + if (IS_ERR(link))
723 + return PTR_ERR(link);
724 + }
725 +
726 + err = -ENOMEM;
727 + override_cred = prepare_creds();
728 + if (!override_cred)
729 + goto out_free_link;
730 +
731 + override_cred->fsuid = stat->uid;
732 + override_cred->fsgid = stat->gid;
733 + /*
734 + * CAP_SYS_ADMIN for copying up extended attributes
735 + * CAP_DAC_OVERRIDE for create
736 + * CAP_FOWNER for chmod, timestamp update
737 + * CAP_FSETID for chmod
738 + * CAP_MKNOD for mknod
739 + */
740 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
741 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
742 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
743 + cap_raise(override_cred->cap_effective, CAP_FSETID);
744 + cap_raise(override_cred->cap_effective, CAP_MKNOD);
745 + old_cred = override_creds(override_cred);
746 +
747 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
748 + if (ovl_path_type(dentry) != OVL_PATH_LOWER) {
749 + err = 0;
750 + } else {
751 + err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
752 + stat, link);
753 + if (!err) {
754 + /* Restore timestamps on parent (best effort) */
755 + ovl_set_timestamps(upperdir, &pstat);
756 + }
757 + }
758 +
759 + mutex_unlock(&upperdir->d_inode->i_mutex);
760 +
761 + revert_creds(old_cred);
762 + put_cred(override_cred);
763 +
764 +out_free_link:
765 + if (link)
766 + free_page((unsigned long) link);
767 +
768 + return err;
769 +}
770 +
771 +int ovl_copy_up(struct dentry *dentry)
772 +{
773 + int err;
774 +
775 + err = 0;
776 + while (!err) {
777 + struct dentry *next;
778 + struct dentry *parent;
779 + struct path lowerpath;
780 + struct kstat stat;
781 + enum ovl_path_type type = ovl_path_type(dentry);
782 +
783 + if (type != OVL_PATH_LOWER)
784 + break;
785 +
786 + next = dget(dentry);
787 + /* find the topmost dentry not yet copied up */
788 + for (;;) {
789 + parent = dget_parent(next);
790 +
791 + type = ovl_path_type(parent);
792 + if (type != OVL_PATH_LOWER)
793 + break;
794 +
795 + dput(next);
796 + next = parent;
797 + }
798 +
799 + ovl_path_lower(next, &lowerpath);
800 + err = vfs_getattr(&lowerpath, &stat);
801 + if (!err)
802 + err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
803 +
804 + dput(parent);
805 + dput(next);
806 + }
807 +
808 + return err;
809 +}
810 +
811 +/* Optimize by not copying up the file first and truncating later */
812 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
813 +{
814 + int err;
815 + struct kstat stat;
816 + struct path lowerpath;
817 + struct dentry *parent = dget_parent(dentry);
818 +
819 + err = ovl_copy_up(parent);
820 + if (err)
821 + goto out_dput_parent;
822 +
823 + ovl_path_lower(dentry, &lowerpath);
824 + err = vfs_getattr(&lowerpath, &stat);
825 + if (err)
826 + goto out_dput_parent;
827 +
828 + if (size < stat.size)
829 + stat.size = size;
830 +
831 + err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
832 +
833 +out_dput_parent:
834 + dput(parent);
835 + return err;
836 +}
837 --- /dev/null
838 +++ b/fs/overlayfs/dir.c
839 @@ -0,0 +1,605 @@
840 +/*
841 + *
842 + * Copyright (C) 2011 Novell Inc.
843 + *
844 + * This program is free software; you can redistribute it and/or modify it
845 + * under the terms of the GNU General Public License version 2 as published by
846 + * the Free Software Foundation.
847 + */
848 +
849 +#include <linux/fs.h>
850 +#include <linux/namei.h>
851 +#include <linux/xattr.h>
852 +#include <linux/security.h>
853 +#include <linux/cred.h>
854 +#include "overlayfs.h"
855 +
856 +static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
857 +
858 +static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
859 +{
860 + int err;
861 + struct dentry *newdentry;
862 + const struct cred *old_cred;
863 + struct cred *override_cred;
864 +
865 + /* FIXME: recheck lower dentry to see if whiteout is really needed */
866 +
867 + err = -ENOMEM;
868 + override_cred = prepare_creds();
869 + if (!override_cred)
870 + goto out;
871 +
872 + /*
873 + * CAP_SYS_ADMIN for setxattr
874 + * CAP_DAC_OVERRIDE for symlink creation
875 + * CAP_FOWNER for unlink in sticky directory
876 + */
877 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
878 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
879 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
880 + override_cred->fsuid = GLOBAL_ROOT_UID;
881 + override_cred->fsgid = GLOBAL_ROOT_GID;
882 + old_cred = override_creds(override_cred);
883 +
884 + newdentry = lookup_one_len(dentry->d_name.name, upperdir,
885 + dentry->d_name.len);
886 + err = PTR_ERR(newdentry);
887 + if (IS_ERR(newdentry))
888 + goto out_put_cred;
889 +
890 + /* Just been removed within the same locked region */
891 + WARN_ON(newdentry->d_inode);
892 +
893 + err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
894 + if (err)
895 + goto out_dput;
896 +
897 + ovl_dentry_version_inc(dentry->d_parent);
898 +
899 + err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
900 + if (err)
901 + vfs_unlink(upperdir->d_inode, newdentry);
902 +
903 +out_dput:
904 + dput(newdentry);
905 +out_put_cred:
906 + revert_creds(old_cred);
907 + put_cred(override_cred);
908 +out:
909 + if (err) {
910 + /*
911 + * There's no way to recover from failure to whiteout.
912 + * What should we do? Log a big fat error and... ?
913 + */
914 + pr_err("overlayfs: ERROR - failed to whiteout '%s'\n",
915 + dentry->d_name.name);
916 + }
917 +
918 + return err;
919 +}
920 +
921 +static struct dentry *ovl_lookup_create(struct dentry *upperdir,
922 + struct dentry *template)
923 +{
924 + int err;
925 + struct dentry *newdentry;
926 + struct qstr *name = &template->d_name;
927 +
928 + newdentry = lookup_one_len(name->name, upperdir, name->len);
929 + if (IS_ERR(newdentry))
930 + return newdentry;
931 +
932 + if (newdentry->d_inode) {
933 + const struct cred *old_cred;
934 + struct cred *override_cred;
935 +
936 + /* No need to check whiteout if lower parent is non-existent */
937 + err = -EEXIST;
938 + if (!ovl_dentry_lower(template->d_parent))
939 + goto out_dput;
940 +
941 + if (!S_ISLNK(newdentry->d_inode->i_mode))
942 + goto out_dput;
943 +
944 + err = -ENOMEM;
945 + override_cred = prepare_creds();
946 + if (!override_cred)
947 + goto out_dput;
948 +
949 + /*
950 + * CAP_SYS_ADMIN for getxattr
951 + * CAP_FOWNER for unlink in sticky directory
952 + */
953 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
954 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
955 + old_cred = override_creds(override_cred);
956 +
957 + err = -EEXIST;
958 + if (ovl_is_whiteout(newdentry))
959 + err = vfs_unlink(upperdir->d_inode, newdentry);
960 +
961 + revert_creds(old_cred);
962 + put_cred(override_cred);
963 + if (err)
964 + goto out_dput;
965 +
966 + dput(newdentry);
967 + newdentry = lookup_one_len(name->name, upperdir, name->len);
968 + if (IS_ERR(newdentry)) {
969 + ovl_whiteout(upperdir, template);
970 + return newdentry;
971 + }
972 +
973 + /*
974 + * Whiteout just been successfully removed, parent
975 + * i_mutex is still held, there's no way the lookup
976 + * could return positive.
977 + */
978 + WARN_ON(newdentry->d_inode);
979 + }
980 +
981 + return newdentry;
982 +
983 +out_dput:
984 + dput(newdentry);
985 + return ERR_PTR(err);
986 +}
987 +
988 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
989 + struct kstat *stat, const char *link)
990 +{
991 + int err;
992 + struct dentry *newdentry;
993 + struct inode *dir = upperdir->d_inode;
994 +
995 + newdentry = ovl_lookup_create(upperdir, dentry);
996 + if (IS_ERR(newdentry))
997 + goto out;
998 +
999 + switch (stat->mode & S_IFMT) {
1000 + case S_IFREG:
1001 + err = vfs_create(dir, newdentry, stat->mode, NULL);
1002 + break;
1003 +
1004 + case S_IFDIR:
1005 + err = vfs_mkdir(dir, newdentry, stat->mode);
1006 + break;
1007 +
1008 + case S_IFCHR:
1009 + case S_IFBLK:
1010 + case S_IFIFO:
1011 + case S_IFSOCK:
1012 + err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
1013 + break;
1014 +
1015 + case S_IFLNK:
1016 + err = vfs_symlink(dir, newdentry, link);
1017 + break;
1018 +
1019 + default:
1020 + err = -EPERM;
1021 + }
1022 + if (err) {
1023 + if (ovl_dentry_is_opaque(dentry))
1024 + ovl_whiteout(upperdir, dentry);
1025 + dput(newdentry);
1026 + newdentry = ERR_PTR(err);
1027 + } else if (WARN_ON(!newdentry->d_inode)) {
1028 + /*
1029 + * Not quite sure if non-instantiated dentry is legal or not.
1030 + * VFS doesn't seem to care so check and warn here.
1031 + */
1032 + dput(newdentry);
1033 + newdentry = ERR_PTR(-ENOENT);
1034 + }
1035 +
1036 +out:
1037 + return newdentry;
1038 +
1039 +}
1040 +
1041 +static int ovl_set_opaque(struct dentry *upperdentry)
1042 +{
1043 + int err;
1044 + const struct cred *old_cred;
1045 + struct cred *override_cred;
1046 +
1047 + override_cred = prepare_creds();
1048 + if (!override_cred)
1049 + return -ENOMEM;
1050 +
1051 + /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
1052 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1053 + old_cred = override_creds(override_cred);
1054 + err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
1055 + revert_creds(old_cred);
1056 + put_cred(override_cred);
1057 +
1058 + return err;
1059 +}
1060 +
1061 +static int ovl_remove_opaque(struct dentry *upperdentry)
1062 +{
1063 + int err;
1064 + const struct cred *old_cred;
1065 + struct cred *override_cred;
1066 +
1067 + override_cred = prepare_creds();
1068 + if (!override_cred)
1069 + return -ENOMEM;
1070 +
1071 + /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
1072 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1073 + old_cred = override_creds(override_cred);
1074 + err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
1075 + revert_creds(old_cred);
1076 + put_cred(override_cred);
1077 +
1078 + return err;
1079 +}
1080 +
1081 +static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
1082 + struct kstat *stat)
1083 +{
1084 + int err;
1085 + enum ovl_path_type type;
1086 + struct path realpath;
1087 +
1088 + type = ovl_path_real(dentry, &realpath);
1089 + err = vfs_getattr(&realpath, stat);
1090 + if (err)
1091 + return err;
1092 +
1093 + stat->dev = dentry->d_sb->s_dev;
1094 + stat->ino = dentry->d_inode->i_ino;
1095 +
1096 + /*
1097 + * It's probably not worth it to count subdirs to get the
1098 + * correct link count. nlink=1 seems to pacify 'find' and
1099 + * other utilities.
1100 + */
1101 + if (type == OVL_PATH_MERGE)
1102 + stat->nlink = 1;
1103 +
1104 + return 0;
1105 +}
1106 +
1107 +static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
1108 + const char *link)
1109 +{
1110 + int err;
1111 + struct dentry *newdentry;
1112 + struct dentry *upperdir;
1113 + struct inode *inode;
1114 + struct kstat stat = {
1115 + .mode = mode,
1116 + .rdev = rdev,
1117 + };
1118 +
1119 + err = -ENOMEM;
1120 + inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
1121 + if (!inode)
1122 + goto out;
1123 +
1124 + err = ovl_copy_up(dentry->d_parent);
1125 + if (err)
1126 + goto out_iput;
1127 +
1128 + upperdir = ovl_dentry_upper(dentry->d_parent);
1129 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1130 +
1131 + newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
1132 + err = PTR_ERR(newdentry);
1133 + if (IS_ERR(newdentry))
1134 + goto out_unlock;
1135 +
1136 + ovl_dentry_version_inc(dentry->d_parent);
1137 + if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
1138 + err = ovl_set_opaque(newdentry);
1139 + if (err) {
1140 + vfs_rmdir(upperdir->d_inode, newdentry);
1141 + ovl_whiteout(upperdir, dentry);
1142 + goto out_dput;
1143 + }
1144 + }
1145 + ovl_dentry_update(dentry, newdentry);
1146 + ovl_copyattr(newdentry->d_inode, inode);
1147 + d_instantiate(dentry, inode);
1148 + inode = NULL;
1149 + newdentry = NULL;
1150 + err = 0;
1151 +
1152 +out_dput:
1153 + dput(newdentry);
1154 +out_unlock:
1155 + mutex_unlock(&upperdir->d_inode->i_mutex);
1156 +out_iput:
1157 + iput(inode);
1158 +out:
1159 + return err;
1160 +}
1161 +
1162 +static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
1163 + bool excl)
1164 +{
1165 + return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
1166 +}
1167 +
1168 +static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1169 +{
1170 + return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
1171 +}
1172 +
1173 +static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
1174 + dev_t rdev)
1175 +{
1176 + return ovl_create_object(dentry, mode, rdev, NULL);
1177 +}
1178 +
1179 +static int ovl_symlink(struct inode *dir, struct dentry *dentry,
1180 + const char *link)
1181 +{
1182 + return ovl_create_object(dentry, S_IFLNK, 0, link);
1183 +}
1184 +
1185 +static int ovl_do_remove(struct dentry *dentry, bool is_dir)
1186 +{
1187 + int err;
1188 + enum ovl_path_type type;
1189 + struct path realpath;
1190 + struct dentry *upperdir;
1191 +
1192 + err = ovl_copy_up(dentry->d_parent);
1193 + if (err)
1194 + return err;
1195 +
1196 + upperdir = ovl_dentry_upper(dentry->d_parent);
1197 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1198 + type = ovl_path_real(dentry, &realpath);
1199 + if (type != OVL_PATH_LOWER) {
1200 + err = -ESTALE;
1201 + if (realpath.dentry->d_parent != upperdir)
1202 + goto out_d_drop;
1203 +
1204 + /* FIXME: create whiteout up front and rename to target */
1205 +
1206 + if (is_dir)
1207 + err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
1208 + else
1209 + err = vfs_unlink(upperdir->d_inode, realpath.dentry);
1210 + if (err)
1211 + goto out_d_drop;
1212 +
1213 + ovl_dentry_version_inc(dentry->d_parent);
1214 + }
1215 +
1216 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1217 + err = ovl_whiteout(upperdir, dentry);
1218 +
1219 + /*
1220 + * Keeping this dentry hashed would mean having to release
1221 + * upperpath/lowerpath, which could only be done if we are the
1222 + * sole user of this dentry. Too tricky... Just unhash for
1223 + * now.
1224 + */
1225 +out_d_drop:
1226 + d_drop(dentry);
1227 + mutex_unlock(&upperdir->d_inode->i_mutex);
1228 +
1229 + return err;
1230 +}
1231 +
1232 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1233 +{
1234 + return ovl_do_remove(dentry, false);
1235 +}
1236 +
1237 +
1238 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
1239 +{
1240 + int err;
1241 + enum ovl_path_type type;
1242 +
1243 + type = ovl_path_type(dentry);
1244 + if (type != OVL_PATH_UPPER) {
1245 + err = ovl_check_empty_and_clear(dentry, type);
1246 + if (err)
1247 + return err;
1248 + }
1249 +
1250 + return ovl_do_remove(dentry, true);
1251 +}
1252 +
1253 +static int ovl_link(struct dentry *old, struct inode *newdir,
1254 + struct dentry *new)
1255 +{
1256 + int err;
1257 + struct dentry *olddentry;
1258 + struct dentry *newdentry;
1259 + struct dentry *upperdir;
1260 + struct inode *newinode;
1261 +
1262 + err = ovl_copy_up(old);
1263 + if (err)
1264 + goto out;
1265 +
1266 + err = ovl_copy_up(new->d_parent);
1267 + if (err)
1268 + goto out;
1269 +
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))
1275 + goto out_unlock;
1276 +
1277 + olddentry = ovl_dentry_upper(old);
1278 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
1279 + if (!err) {
1280 + if (WARN_ON(!newdentry->d_inode)) {
1281 + dput(newdentry);
1282 + err = -ENOENT;
1283 + goto out_unlock;
1284 + }
1285 + newinode = ovl_new_inode(old->d_sb, newdentry->d_inode->i_mode,
1286 + new->d_fsdata);
1287 + if (!newinode) {
1288 + err = -ENOMEM;
1289 + goto link_fail;
1290 + }
1291 + ovl_copyattr(upperdir->d_inode, newinode);
1292 +
1293 + ovl_dentry_version_inc(new->d_parent);
1294 + ovl_dentry_update(new, newdentry);
1295 +
1296 + d_instantiate(new, newinode);
1297 + } else {
1298 +link_fail:
1299 + if (ovl_dentry_is_opaque(new))
1300 + ovl_whiteout(upperdir, new);
1301 + dput(newdentry);
1302 + }
1303 +out_unlock:
1304 + mutex_unlock(&upperdir->d_inode->i_mutex);
1305 +out:
1306 + return err;
1307 +}
1308 +
1309 +static int ovl_rename(struct inode *olddir, struct dentry *old,
1310 + struct inode *newdir, struct dentry *new)
1311 +{
1312 + int err;
1313 + enum ovl_path_type old_type;
1314 + enum ovl_path_type new_type;
1315 + struct dentry *old_upperdir;
1316 + struct dentry *new_upperdir;
1317 + struct dentry *olddentry;
1318 + struct dentry *newdentry;
1319 + struct dentry *trap;
1320 + bool old_opaque;
1321 + bool new_opaque;
1322 + bool new_create = false;
1323 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
1324 +
1325 + /* Don't copy up directory trees */
1326 + old_type = ovl_path_type(old);
1327 + if (old_type != OVL_PATH_UPPER && is_dir)
1328 + return -EXDEV;
1329 +
1330 + if (new->d_inode) {
1331 + new_type = ovl_path_type(new);
1332 +
1333 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
1334 + if (ovl_dentry_lower(old)->d_inode ==
1335 + ovl_dentry_lower(new)->d_inode)
1336 + return 0;
1337 + }
1338 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
1339 + if (ovl_dentry_upper(old)->d_inode ==
1340 + ovl_dentry_upper(new)->d_inode)
1341 + return 0;
1342 + }
1343 +
1344 + if (new_type != OVL_PATH_UPPER &&
1345 + S_ISDIR(new->d_inode->i_mode)) {
1346 + err = ovl_check_empty_and_clear(new, new_type);
1347 + if (err)
1348 + return err;
1349 + }
1350 + } else {
1351 + new_type = OVL_PATH_UPPER;
1352 + }
1353 +
1354 + err = ovl_copy_up(old);
1355 + if (err)
1356 + return err;
1357 +
1358 + err = ovl_copy_up(new->d_parent);
1359 + if (err)
1360 + return err;
1361 +
1362 + old_upperdir = ovl_dentry_upper(old->d_parent);
1363 + new_upperdir = ovl_dentry_upper(new->d_parent);
1364 +
1365 + trap = lock_rename(new_upperdir, old_upperdir);
1366 +
1367 + olddentry = ovl_dentry_upper(old);
1368 + newdentry = ovl_dentry_upper(new);
1369 + if (newdentry) {
1370 + dget(newdentry);
1371 + } else {
1372 + new_create = true;
1373 + newdentry = ovl_lookup_create(new_upperdir, new);
1374 + err = PTR_ERR(newdentry);
1375 + if (IS_ERR(newdentry))
1376 + goto out_unlock;
1377 + }
1378 +
1379 + err = -ESTALE;
1380 + if (olddentry->d_parent != old_upperdir)
1381 + goto out_dput;
1382 + if (newdentry->d_parent != new_upperdir)
1383 + goto out_dput;
1384 + if (olddentry == trap)
1385 + goto out_dput;
1386 + if (newdentry == trap)
1387 + goto out_dput;
1388 +
1389 + old_opaque = ovl_dentry_is_opaque(old);
1390 + new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
1391 +
1392 + if (is_dir && !old_opaque && new_opaque) {
1393 + err = ovl_set_opaque(olddentry);
1394 + if (err)
1395 + goto out_dput;
1396 + }
1397 +
1398 + err = vfs_rename(old_upperdir->d_inode, olddentry,
1399 + new_upperdir->d_inode, newdentry);
1400 +
1401 + if (err) {
1402 + if (new_create && ovl_dentry_is_opaque(new))
1403 + ovl_whiteout(new_upperdir, new);
1404 + if (is_dir && !old_opaque && new_opaque)
1405 + ovl_remove_opaque(olddentry);
1406 + goto out_dput;
1407 + }
1408 +
1409 + if (old_type != OVL_PATH_UPPER || old_opaque)
1410 + err = ovl_whiteout(old_upperdir, old);
1411 + if (is_dir && old_opaque && !new_opaque)
1412 + ovl_remove_opaque(olddentry);
1413 +
1414 + if (old_opaque != new_opaque)
1415 + ovl_dentry_set_opaque(old, new_opaque);
1416 +
1417 + ovl_dentry_version_inc(old->d_parent);
1418 + ovl_dentry_version_inc(new->d_parent);
1419 +
1420 +out_dput:
1421 + dput(newdentry);
1422 +out_unlock:
1423 + unlock_rename(new_upperdir, old_upperdir);
1424 + return err;
1425 +}
1426 +
1427 +const struct inode_operations ovl_dir_inode_operations = {
1428 + .lookup = ovl_lookup,
1429 + .mkdir = ovl_mkdir,
1430 + .symlink = ovl_symlink,
1431 + .unlink = ovl_unlink,
1432 + .rmdir = ovl_rmdir,
1433 + .rename = ovl_rename,
1434 + .link = ovl_link,
1435 + .setattr = ovl_setattr,
1436 + .create = ovl_create,
1437 + .mknod = ovl_mknod,
1438 + .permission = ovl_permission,
1439 + .getattr = ovl_dir_getattr,
1440 + .setxattr = ovl_setxattr,
1441 + .getxattr = ovl_getxattr,
1442 + .listxattr = ovl_listxattr,
1443 + .removexattr = ovl_removexattr,
1444 +};
1445 --- /dev/null
1446 +++ b/fs/overlayfs/inode.c
1447 @@ -0,0 +1,372 @@
1448 +/*
1449 + *
1450 + * Copyright (C) 2011 Novell Inc.
1451 + *
1452 + * This program is free software; you can redistribute it and/or modify it
1453 + * under the terms of the GNU General Public License version 2 as published by
1454 + * the Free Software Foundation.
1455 + */
1456 +
1457 +#include <linux/fs.h>
1458 +#include <linux/slab.h>
1459 +#include <linux/xattr.h>
1460 +#include "overlayfs.h"
1461 +
1462 +int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1463 +{
1464 + struct dentry *upperdentry;
1465 + int err;
1466 +
1467 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1468 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1469 + else
1470 + err = ovl_copy_up(dentry);
1471 + if (err)
1472 + return err;
1473 +
1474 + upperdentry = ovl_dentry_upper(dentry);
1475 +
1476 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1477 + attr->ia_valid &= ~ATTR_MODE;
1478 +
1479 + mutex_lock(&upperdentry->d_inode->i_mutex);
1480 + err = notify_change(upperdentry, attr);
1481 + if (!err)
1482 + ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
1483 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1484 +
1485 + return err;
1486 +}
1487 +
1488 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1489 + struct kstat *stat)
1490 +{
1491 + struct path realpath;
1492 +
1493 + ovl_path_real(dentry, &realpath);
1494 + return vfs_getattr(&realpath, stat);
1495 +}
1496 +
1497 +int ovl_permission(struct inode *inode, int mask)
1498 +{
1499 + struct ovl_entry *oe;
1500 + struct dentry *alias = NULL;
1501 + struct inode *realinode;
1502 + struct dentry *realdentry;
1503 + bool is_upper;
1504 + int err;
1505 +
1506 + if (S_ISDIR(inode->i_mode)) {
1507 + oe = inode->i_private;
1508 + } else if (mask & MAY_NOT_BLOCK) {
1509 + return -ECHILD;
1510 + } else {
1511 + /*
1512 + * For non-directories find an alias and get the info
1513 + * from there.
1514 + */
1515 + alias = d_find_any_alias(inode);
1516 + if (WARN_ON(!alias))
1517 + return -ENOENT;
1518 +
1519 + oe = alias->d_fsdata;
1520 + }
1521 +
1522 + realdentry = ovl_entry_real(oe, &is_upper);
1523 +
1524 + /* Careful in RCU walk mode */
1525 + realinode = ACCESS_ONCE(realdentry->d_inode);
1526 + if (!realinode) {
1527 + WARN_ON(!(mask & MAY_NOT_BLOCK));
1528 + err = -ENOENT;
1529 + goto out_dput;
1530 + }
1531 +
1532 + if (mask & MAY_WRITE) {
1533 + umode_t mode = realinode->i_mode;
1534 +
1535 + /*
1536 + * Writes will always be redirected to upper layer, so
1537 + * ignore lower layer being read-only.
1538 + *
1539 + * If the overlay itself is read-only then proceed
1540 + * with the permission check, don't return EROFS.
1541 + * This will only happen if this is the lower layer of
1542 + * another overlayfs.
1543 + *
1544 + * If upper fs becomes read-only after the overlay was
1545 + * constructed return EROFS to prevent modification of
1546 + * upper layer.
1547 + */
1548 + err = -EROFS;
1549 + if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
1550 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1551 + goto out_dput;
1552 + }
1553 +
1554 + err = __inode_permission(realinode, mask);
1555 +out_dput:
1556 + dput(alias);
1557 + return err;
1558 +}
1559 +
1560 +
1561 +struct ovl_link_data {
1562 + struct dentry *realdentry;
1563 + void *cookie;
1564 +};
1565 +
1566 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1567 +{
1568 + void *ret;
1569 + struct dentry *realdentry;
1570 + struct inode *realinode;
1571 +
1572 + realdentry = ovl_dentry_real(dentry);
1573 + realinode = realdentry->d_inode;
1574 +
1575 + if (WARN_ON(!realinode->i_op->follow_link))
1576 + return ERR_PTR(-EPERM);
1577 +
1578 + ret = realinode->i_op->follow_link(realdentry, nd);
1579 + if (IS_ERR(ret))
1580 + return ret;
1581 +
1582 + if (realinode->i_op->put_link) {
1583 + struct ovl_link_data *data;
1584 +
1585 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1586 + if (!data) {
1587 + realinode->i_op->put_link(realdentry, nd, ret);
1588 + return ERR_PTR(-ENOMEM);
1589 + }
1590 + data->realdentry = realdentry;
1591 + data->cookie = ret;
1592 +
1593 + return data;
1594 + } else {
1595 + return NULL;
1596 + }
1597 +}
1598 +
1599 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1600 +{
1601 + struct inode *realinode;
1602 + struct ovl_link_data *data = c;
1603 +
1604 + if (!data)
1605 + return;
1606 +
1607 + realinode = data->realdentry->d_inode;
1608 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1609 + kfree(data);
1610 +}
1611 +
1612 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1613 +{
1614 + struct path realpath;
1615 + struct inode *realinode;
1616 +
1617 + ovl_path_real(dentry, &realpath);
1618 + realinode = realpath.dentry->d_inode;
1619 +
1620 + if (!realinode->i_op->readlink)
1621 + return -EINVAL;
1622 +
1623 + touch_atime(&realpath);
1624 +
1625 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1626 +}
1627 +
1628 +
1629 +static bool ovl_is_private_xattr(const char *name)
1630 +{
1631 + return strncmp(name, "trusted.overlay.", 14) == 0;
1632 +}
1633 +
1634 +int ovl_setxattr(struct dentry *dentry, const char *name,
1635 + const void *value, size_t size, int flags)
1636 +{
1637 + int err;
1638 + struct dentry *upperdentry;
1639 +
1640 + if (ovl_is_private_xattr(name))
1641 + return -EPERM;
1642 +
1643 + err = ovl_copy_up(dentry);
1644 + if (err)
1645 + return err;
1646 +
1647 + upperdentry = ovl_dentry_upper(dentry);
1648 + return vfs_setxattr(upperdentry, name, value, size, flags);
1649 +}
1650 +
1651 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1652 + void *value, size_t size)
1653 +{
1654 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1655 + ovl_is_private_xattr(name))
1656 + return -ENODATA;
1657 +
1658 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
1659 +}
1660 +
1661 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
1662 +{
1663 + ssize_t res;
1664 + int off;
1665 +
1666 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
1667 + if (res <= 0 || size == 0)
1668 + return res;
1669 +
1670 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
1671 + return res;
1672 +
1673 + /* filter out private xattrs */
1674 + for (off = 0; off < res;) {
1675 + char *s = list + off;
1676 + size_t slen = strlen(s) + 1;
1677 +
1678 + BUG_ON(off + slen > res);
1679 +
1680 + if (ovl_is_private_xattr(s)) {
1681 + res -= slen;
1682 + memmove(s, s + slen, res - off);
1683 + } else {
1684 + off += slen;
1685 + }
1686 + }
1687 +
1688 + return res;
1689 +}
1690 +
1691 +int ovl_removexattr(struct dentry *dentry, const char *name)
1692 +{
1693 + int err;
1694 + struct path realpath;
1695 + enum ovl_path_type type;
1696 +
1697 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1698 + ovl_is_private_xattr(name))
1699 + return -ENODATA;
1700 +
1701 + type = ovl_path_real(dentry, &realpath);
1702 + if (type == OVL_PATH_LOWER) {
1703 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
1704 + if (err < 0)
1705 + return err;
1706 +
1707 + err = ovl_copy_up(dentry);
1708 + if (err)
1709 + return err;
1710 +
1711 + ovl_path_upper(dentry, &realpath);
1712 + }
1713 +
1714 + return vfs_removexattr(realpath.dentry, name);
1715 +}
1716 +
1717 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
1718 + struct dentry *realdentry)
1719 +{
1720 + if (type != OVL_PATH_LOWER)
1721 + return false;
1722 +
1723 + if (special_file(realdentry->d_inode->i_mode))
1724 + return false;
1725 +
1726 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
1727 + return false;
1728 +
1729 + return true;
1730 +}
1731 +
1732 +static int ovl_dentry_open(struct dentry *dentry, struct file *file,
1733 + const struct cred *cred)
1734 +{
1735 + int err;
1736 + struct path realpath;
1737 + enum ovl_path_type type;
1738 +
1739 + type = ovl_path_real(dentry, &realpath);
1740 + if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
1741 + if (file->f_flags & O_TRUNC)
1742 + err = ovl_copy_up_truncate(dentry, 0);
1743 + else
1744 + err = ovl_copy_up(dentry);
1745 + if (err)
1746 + return err;
1747 +
1748 + ovl_path_upper(dentry, &realpath);
1749 + }
1750 +
1751 + return vfs_open(&realpath, file, cred);
1752 +}
1753 +
1754 +static const struct inode_operations ovl_file_inode_operations = {
1755 + .setattr = ovl_setattr,
1756 + .permission = ovl_permission,
1757 + .getattr = ovl_getattr,
1758 + .setxattr = ovl_setxattr,
1759 + .getxattr = ovl_getxattr,
1760 + .listxattr = ovl_listxattr,
1761 + .removexattr = ovl_removexattr,
1762 + .dentry_open = ovl_dentry_open,
1763 +};
1764 +
1765 +static const struct inode_operations ovl_symlink_inode_operations = {
1766 + .setattr = ovl_setattr,
1767 + .follow_link = ovl_follow_link,
1768 + .put_link = ovl_put_link,
1769 + .readlink = ovl_readlink,
1770 + .getattr = ovl_getattr,
1771 + .setxattr = ovl_setxattr,
1772 + .getxattr = ovl_getxattr,
1773 + .listxattr = ovl_listxattr,
1774 + .removexattr = ovl_removexattr,
1775 +};
1776 +
1777 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1778 + struct ovl_entry *oe)
1779 +{
1780 + struct inode *inode;
1781 +
1782 + inode = new_inode(sb);
1783 + if (!inode)
1784 + return NULL;
1785 +
1786 + mode &= S_IFMT;
1787 +
1788 + inode->i_ino = get_next_ino();
1789 + inode->i_mode = mode;
1790 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
1791 +
1792 + switch (mode) {
1793 + case S_IFDIR:
1794 + inode->i_private = oe;
1795 + inode->i_op = &ovl_dir_inode_operations;
1796 + inode->i_fop = &ovl_dir_operations;
1797 + break;
1798 +
1799 + case S_IFLNK:
1800 + inode->i_op = &ovl_symlink_inode_operations;
1801 + break;
1802 +
1803 + case S_IFREG:
1804 + case S_IFSOCK:
1805 + case S_IFBLK:
1806 + case S_IFCHR:
1807 + case S_IFIFO:
1808 + inode->i_op = &ovl_file_inode_operations;
1809 + break;
1810 +
1811 + default:
1812 + WARN(1, "illegal file type: %i\n", mode);
1813 + iput(inode);
1814 + inode = NULL;
1815 + }
1816 +
1817 + return inode;
1818 +
1819 +}
1820 --- /dev/null
1821 +++ b/fs/overlayfs/overlayfs.h
1822 @@ -0,0 +1,70 @@
1823 +/*
1824 + *
1825 + * Copyright (C) 2011 Novell Inc.
1826 + *
1827 + * This program is free software; you can redistribute it and/or modify it
1828 + * under the terms of the GNU General Public License version 2 as published by
1829 + * the Free Software Foundation.
1830 + */
1831 +
1832 +struct ovl_entry;
1833 +
1834 +enum ovl_path_type {
1835 + OVL_PATH_UPPER,
1836 + OVL_PATH_MERGE,
1837 + OVL_PATH_LOWER,
1838 +};
1839 +
1840 +extern const char *ovl_opaque_xattr;
1841 +extern const char *ovl_whiteout_xattr;
1842 +extern const struct dentry_operations ovl_dentry_operations;
1843 +
1844 +enum ovl_path_type ovl_path_type(struct dentry *dentry);
1845 +u64 ovl_dentry_version_get(struct dentry *dentry);
1846 +void ovl_dentry_version_inc(struct dentry *dentry);
1847 +void ovl_path_upper(struct dentry *dentry, struct path *path);
1848 +void ovl_path_lower(struct dentry *dentry, struct path *path);
1849 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1850 +struct dentry *ovl_dentry_upper(struct dentry *dentry);
1851 +struct dentry *ovl_dentry_lower(struct dentry *dentry);
1852 +struct dentry *ovl_dentry_real(struct dentry *dentry);
1853 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
1854 +bool ovl_dentry_is_opaque(struct dentry *dentry);
1855 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
1856 +bool ovl_is_whiteout(struct dentry *dentry);
1857 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
1858 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
1859 + unsigned int flags);
1860 +struct file *ovl_path_open(struct path *path, int flags);
1861 +
1862 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
1863 + struct kstat *stat, const char *link);
1864 +
1865 +/* readdir.c */
1866 +extern const struct file_operations ovl_dir_operations;
1867 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
1868 +
1869 +/* inode.c */
1870 +int ovl_setattr(struct dentry *dentry, struct iattr *attr);
1871 +int ovl_permission(struct inode *inode, int mask);
1872 +int ovl_setxattr(struct dentry *dentry, const char *name,
1873 + const void *value, size_t size, int flags);
1874 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1875 + void *value, size_t size);
1876 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
1877 +int ovl_removexattr(struct dentry *dentry, const char *name);
1878 +
1879 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1880 + struct ovl_entry *oe);
1881 +static inline void ovl_copyattr(struct inode *from, struct inode *to)
1882 +{
1883 + to->i_uid = from->i_uid;
1884 + to->i_gid = from->i_gid;
1885 +}
1886 +
1887 +/* dir.c */
1888 +extern const struct inode_operations ovl_dir_inode_operations;
1889 +
1890 +/* copy_up.c */
1891 +int ovl_copy_up(struct dentry *dentry);
1892 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
1893 --- /dev/null
1894 +++ b/fs/overlayfs/readdir.c
1895 @@ -0,0 +1,566 @@
1896 +/*
1897 + *
1898 + * Copyright (C) 2011 Novell Inc.
1899 + *
1900 + * This program is free software; you can redistribute it and/or modify it
1901 + * under the terms of the GNU General Public License version 2 as published by
1902 + * the Free Software Foundation.
1903 + */
1904 +
1905 +#include <linux/fs.h>
1906 +#include <linux/slab.h>
1907 +#include <linux/namei.h>
1908 +#include <linux/file.h>
1909 +#include <linux/xattr.h>
1910 +#include <linux/rbtree.h>
1911 +#include <linux/security.h>
1912 +#include <linux/cred.h>
1913 +#include "overlayfs.h"
1914 +
1915 +struct ovl_cache_entry {
1916 + const char *name;
1917 + unsigned int len;
1918 + unsigned int type;
1919 + u64 ino;
1920 + bool is_whiteout;
1921 + struct list_head l_node;
1922 + struct rb_node node;
1923 +};
1924 +
1925 +struct ovl_readdir_data {
1926 + struct rb_root *root;
1927 + struct list_head *list;
1928 + struct list_head *middle;
1929 + struct dentry *dir;
1930 + int count;
1931 + int err;
1932 +};
1933 +
1934 +struct ovl_dir_file {
1935 + bool is_real;
1936 + bool is_cached;
1937 + struct list_head cursor;
1938 + u64 cache_version;
1939 + struct list_head cache;
1940 + struct file *realfile;
1941 +};
1942 +
1943 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
1944 +{
1945 + return container_of(n, struct ovl_cache_entry, node);
1946 +}
1947 +
1948 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
1949 + const char *name, int len)
1950 +{
1951 + struct rb_node *node = root->rb_node;
1952 + int cmp;
1953 +
1954 + while (node) {
1955 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
1956 +
1957 + cmp = strncmp(name, p->name, len);
1958 + if (cmp > 0)
1959 + node = p->node.rb_right;
1960 + else if (cmp < 0 || len < p->len)
1961 + node = p->node.rb_left;
1962 + else
1963 + return p;
1964 + }
1965 +
1966 + return NULL;
1967 +}
1968 +
1969 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
1970 + u64 ino, unsigned int d_type)
1971 +{
1972 + struct ovl_cache_entry *p;
1973 +
1974 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
1975 + if (p) {
1976 + char *name_copy = (char *) (p + 1);
1977 + memcpy(name_copy, name, len);
1978 + name_copy[len] = '\0';
1979 + p->name = name_copy;
1980 + p->len = len;
1981 + p->type = d_type;
1982 + p->ino = ino;
1983 + p->is_whiteout = false;
1984 + }
1985 +
1986 + return p;
1987 +}
1988 +
1989 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
1990 + const char *name, int len, u64 ino,
1991 + unsigned int d_type)
1992 +{
1993 + struct rb_node **newp = &rdd->root->rb_node;
1994 + struct rb_node *parent = NULL;
1995 + struct ovl_cache_entry *p;
1996 +
1997 + while (*newp) {
1998 + int cmp;
1999 + struct ovl_cache_entry *tmp;
2000 +
2001 + parent = *newp;
2002 + tmp = ovl_cache_entry_from_node(*newp);
2003 + cmp = strncmp(name, tmp->name, len);
2004 + if (cmp > 0)
2005 + newp = &tmp->node.rb_right;
2006 + else if (cmp < 0 || len < tmp->len)
2007 + newp = &tmp->node.rb_left;
2008 + else
2009 + return 0;
2010 + }
2011 +
2012 + p = ovl_cache_entry_new(name, len, ino, d_type);
2013 + if (p == NULL)
2014 + return -ENOMEM;
2015 +
2016 + list_add_tail(&p->l_node, rdd->list);
2017 + rb_link_node(&p->node, parent, newp);
2018 + rb_insert_color(&p->node, rdd->root);
2019 +
2020 + return 0;
2021 +}
2022 +
2023 +static int ovl_fill_lower(void *buf, const char *name, int namelen,
2024 + loff_t offset, u64 ino, unsigned int d_type)
2025 +{
2026 + struct ovl_readdir_data *rdd = buf;
2027 + struct ovl_cache_entry *p;
2028 +
2029 + rdd->count++;
2030 + p = ovl_cache_entry_find(rdd->root, name, namelen);
2031 + if (p) {
2032 + list_move_tail(&p->l_node, rdd->middle);
2033 + } else {
2034 + p = ovl_cache_entry_new(name, namelen, ino, d_type);
2035 + if (p == NULL)
2036 + rdd->err = -ENOMEM;
2037 + else
2038 + list_add_tail(&p->l_node, rdd->middle);
2039 + }
2040 +
2041 + return rdd->err;
2042 +}
2043 +
2044 +static void ovl_cache_free(struct list_head *list)
2045 +{
2046 + struct ovl_cache_entry *p;
2047 + struct ovl_cache_entry *n;
2048 +
2049 + list_for_each_entry_safe(p, n, list, l_node)
2050 + kfree(p);
2051 +
2052 + INIT_LIST_HEAD(list);
2053 +}
2054 +
2055 +static int ovl_fill_upper(void *buf, const char *name, int namelen,
2056 + loff_t offset, u64 ino, unsigned int d_type)
2057 +{
2058 + struct ovl_readdir_data *rdd = buf;
2059 +
2060 + rdd->count++;
2061 + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
2062 +}
2063 +
2064 +static inline int ovl_dir_read(struct path *realpath,
2065 + struct ovl_readdir_data *rdd, filldir_t filler)
2066 +{
2067 + struct file *realfile;
2068 + int err;
2069 +
2070 + realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
2071 + if (IS_ERR(realfile))
2072 + return PTR_ERR(realfile);
2073 +
2074 + do {
2075 + rdd->count = 0;
2076 + rdd->err = 0;
2077 + err = vfs_readdir(realfile, filler, rdd);
2078 + if (err >= 0)
2079 + err = rdd->err;
2080 + } while (!err && rdd->count);
2081 + fput(realfile);
2082 +
2083 + return 0;
2084 +}
2085 +
2086 +static void ovl_dir_reset(struct file *file)
2087 +{
2088 + struct ovl_dir_file *od = file->private_data;
2089 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
2090 +
2091 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
2092 + list_del_init(&od->cursor);
2093 + ovl_cache_free(&od->cache);
2094 + od->is_cached = false;
2095 + }
2096 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
2097 + if (od->is_real && type == OVL_PATH_MERGE) {
2098 + fput(od->realfile);
2099 + od->realfile = NULL;
2100 + od->is_real = false;
2101 + }
2102 +}
2103 +
2104 +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
2105 +{
2106 + struct ovl_cache_entry *p;
2107 + struct dentry *dentry;
2108 + const struct cred *old_cred;
2109 + struct cred *override_cred;
2110 +
2111 + override_cred = prepare_creds();
2112 + if (!override_cred) {
2113 + ovl_cache_free(rdd->list);
2114 + return -ENOMEM;
2115 + }
2116 +
2117 + /*
2118 + * CAP_SYS_ADMIN for getxattr
2119 + * CAP_DAC_OVERRIDE for lookup
2120 + */
2121 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2122 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2123 + old_cred = override_creds(override_cred);
2124 +
2125 + mutex_lock(&rdd->dir->d_inode->i_mutex);
2126 + list_for_each_entry(p, rdd->list, l_node) {
2127 + if (p->type != DT_LNK)
2128 + continue;
2129 +
2130 + dentry = lookup_one_len(p->name, rdd->dir, p->len);
2131 + if (IS_ERR(dentry))
2132 + continue;
2133 +
2134 + p->is_whiteout = ovl_is_whiteout(dentry);
2135 + dput(dentry);
2136 + }
2137 + mutex_unlock(&rdd->dir->d_inode->i_mutex);
2138 +
2139 + revert_creds(old_cred);
2140 + put_cred(override_cred);
2141 +
2142 + return 0;
2143 +}
2144 +
2145 +static inline int ovl_dir_read_merged(struct path *upperpath,
2146 + struct path *lowerpath,
2147 + struct ovl_readdir_data *rdd)
2148 +{
2149 + int err;
2150 + struct rb_root root = RB_ROOT;
2151 + struct list_head middle;
2152 +
2153 + rdd->root = &root;
2154 + if (upperpath->dentry) {
2155 + rdd->dir = upperpath->dentry;
2156 + err = ovl_dir_read(upperpath, rdd, ovl_fill_upper);
2157 + if (err)
2158 + goto out;
2159 +
2160 + err = ovl_dir_mark_whiteouts(rdd);
2161 + if (err)
2162 + goto out;
2163 + }
2164 + /*
2165 + * Insert lowerpath entries before upperpath ones, this allows
2166 + * offsets to be reasonably constant
2167 + */
2168 + list_add(&middle, rdd->list);
2169 + rdd->middle = &middle;
2170 + err = ovl_dir_read(lowerpath, rdd, ovl_fill_lower);
2171 + list_del(&middle);
2172 +out:
2173 + rdd->root = NULL;
2174 +
2175 + return err;
2176 +}
2177 +
2178 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
2179 +{
2180 + struct list_head *l;
2181 + loff_t off;
2182 +
2183 + l = od->cache.next;
2184 + for (off = 0; off < pos; off++) {
2185 + if (l == &od->cache)
2186 + break;
2187 + l = l->next;
2188 + }
2189 + list_move_tail(&od->cursor, l);
2190 +}
2191 +
2192 +static int ovl_readdir(struct file *file, void *buf, filldir_t filler)
2193 +{
2194 + struct ovl_dir_file *od = file->private_data;
2195 + int res;
2196 +
2197 + if (!file->f_pos)
2198 + ovl_dir_reset(file);
2199 +
2200 + if (od->is_real) {
2201 + res = vfs_readdir(od->realfile, filler, buf);
2202 + file->f_pos = od->realfile->f_pos;
2203 +
2204 + return res;
2205 + }
2206 +
2207 + if (!od->is_cached) {
2208 + struct path lowerpath;
2209 + struct path upperpath;
2210 + struct ovl_readdir_data rdd = { .list = &od->cache };
2211 +
2212 + ovl_path_lower(file->f_path.dentry, &lowerpath);
2213 + ovl_path_upper(file->f_path.dentry, &upperpath);
2214 +
2215 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2216 + if (res) {
2217 + ovl_cache_free(rdd.list);
2218 + return res;
2219 + }
2220 +
2221 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
2222 + od->is_cached = true;
2223 +
2224 + ovl_seek_cursor(od, file->f_pos);
2225 + }
2226 +
2227 + while (od->cursor.next != &od->cache) {
2228 + int over;
2229 + loff_t off;
2230 + struct ovl_cache_entry *p;
2231 +
2232 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
2233 + off = file->f_pos;
2234 + if (!p->is_whiteout) {
2235 + over = filler(buf, p->name, p->len, off, p->ino,
2236 + p->type);
2237 + if (over)
2238 + break;
2239 + }
2240 + file->f_pos++;
2241 + list_move(&od->cursor, &p->l_node);
2242 + }
2243 +
2244 + return 0;
2245 +}
2246 +
2247 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
2248 +{
2249 + loff_t res;
2250 + struct ovl_dir_file *od = file->private_data;
2251 +
2252 + mutex_lock(&file_inode(file)->i_mutex);
2253 + if (!file->f_pos)
2254 + ovl_dir_reset(file);
2255 +
2256 + if (od->is_real) {
2257 + res = vfs_llseek(od->realfile, offset, origin);
2258 + file->f_pos = od->realfile->f_pos;
2259 + } else {
2260 + res = -EINVAL;
2261 +
2262 + switch (origin) {
2263 + case SEEK_CUR:
2264 + offset += file->f_pos;
2265 + break;
2266 + case SEEK_SET:
2267 + break;
2268 + default:
2269 + goto out_unlock;
2270 + }
2271 + if (offset < 0)
2272 + goto out_unlock;
2273 +
2274 + if (offset != file->f_pos) {
2275 + file->f_pos = offset;
2276 + if (od->is_cached)
2277 + ovl_seek_cursor(od, offset);
2278 + }
2279 + res = offset;
2280 + }
2281 +out_unlock:
2282 + mutex_unlock(&file_inode(file)->i_mutex);
2283 +
2284 + return res;
2285 +}
2286 +
2287 +static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
2288 + int datasync)
2289 +{
2290 + struct ovl_dir_file *od = file->private_data;
2291 +
2292 + /* May need to reopen directory if it got copied up */
2293 + if (!od->realfile) {
2294 + struct path upperpath;
2295 +
2296 + ovl_path_upper(file->f_path.dentry, &upperpath);
2297 + od->realfile = ovl_path_open(&upperpath, O_RDONLY);
2298 + if (IS_ERR(od->realfile))
2299 + return PTR_ERR(od->realfile);
2300 + }
2301 +
2302 + return vfs_fsync_range(od->realfile, start, end, datasync);
2303 +}
2304 +
2305 +static int ovl_dir_release(struct inode *inode, struct file *file)
2306 +{
2307 + struct ovl_dir_file *od = file->private_data;
2308 +
2309 + list_del(&od->cursor);
2310 + ovl_cache_free(&od->cache);
2311 + if (od->realfile)
2312 + fput(od->realfile);
2313 + kfree(od);
2314 +
2315 + return 0;
2316 +}
2317 +
2318 +static int ovl_dir_open(struct inode *inode, struct file *file)
2319 +{
2320 + struct path realpath;
2321 + struct file *realfile;
2322 + struct ovl_dir_file *od;
2323 + enum ovl_path_type type;
2324 +
2325 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
2326 + if (!od)
2327 + return -ENOMEM;
2328 +
2329 + type = ovl_path_real(file->f_path.dentry, &realpath);
2330 + realfile = ovl_path_open(&realpath, file->f_flags);
2331 + if (IS_ERR(realfile)) {
2332 + kfree(od);
2333 + return PTR_ERR(realfile);
2334 + }
2335 + INIT_LIST_HEAD(&od->cache);
2336 + INIT_LIST_HEAD(&od->cursor);
2337 + od->is_cached = false;
2338 + od->realfile = realfile;
2339 + od->is_real = (type != OVL_PATH_MERGE);
2340 + file->private_data = od;
2341 +
2342 + return 0;
2343 +}
2344 +
2345 +const struct file_operations ovl_dir_operations = {
2346 + .read = generic_read_dir,
2347 + .open = ovl_dir_open,
2348 + .readdir = ovl_readdir,
2349 + .llseek = ovl_dir_llseek,
2350 + .fsync = ovl_dir_fsync,
2351 + .release = ovl_dir_release,
2352 +};
2353 +
2354 +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
2355 +{
2356 + int err;
2357 + struct path lowerpath;
2358 + struct path upperpath;
2359 + struct ovl_cache_entry *p;
2360 + struct ovl_readdir_data rdd = { .list = list };
2361 +
2362 + ovl_path_upper(dentry, &upperpath);
2363 + ovl_path_lower(dentry, &lowerpath);
2364 +
2365 + err = ovl_dir_read_merged(&upperpath, &lowerpath, &rdd);
2366 + if (err)
2367 + return err;
2368 +
2369 + err = 0;
2370 +
2371 + list_for_each_entry(p, list, l_node) {
2372 + if (p->is_whiteout)
2373 + continue;
2374 +
2375 + if (p->name[0] == '.') {
2376 + if (p->len == 1)
2377 + continue;
2378 + if (p->len == 2 && p->name[1] == '.')
2379 + continue;
2380 + }
2381 + err = -ENOTEMPTY;
2382 + break;
2383 + }
2384 +
2385 + return err;
2386 +}
2387 +
2388 +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
2389 +{
2390 + struct path upperpath;
2391 + struct dentry *upperdir;
2392 + struct ovl_cache_entry *p;
2393 + const struct cred *old_cred;
2394 + struct cred *override_cred;
2395 + int err;
2396 +
2397 + ovl_path_upper(dir, &upperpath);
2398 + upperdir = upperpath.dentry;
2399 +
2400 + override_cred = prepare_creds();
2401 + if (!override_cred)
2402 + return -ENOMEM;
2403 +
2404 + /*
2405 + * CAP_DAC_OVERRIDE for lookup and unlink
2406 + * CAP_SYS_ADMIN for setxattr of "trusted" namespace
2407 + * CAP_FOWNER for unlink in sticky directory
2408 + */
2409 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2410 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2411 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
2412 + old_cred = override_creds(override_cred);
2413 +
2414 + err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
2415 + if (err)
2416 + goto out_revert_creds;
2417 +
2418 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2419 + list_for_each_entry(p, list, l_node) {
2420 + struct dentry *dentry;
2421 + int ret;
2422 +
2423 + if (!p->is_whiteout)
2424 + continue;
2425 +
2426 + dentry = lookup_one_len(p->name, upperdir, p->len);
2427 + if (IS_ERR(dentry)) {
2428 + pr_warn(
2429 + "overlayfs: failed to lookup whiteout %.*s: %li\n",
2430 + p->len, p->name, PTR_ERR(dentry));
2431 + continue;
2432 + }
2433 + ret = vfs_unlink(upperdir->d_inode, dentry);
2434 + dput(dentry);
2435 + if (ret)
2436 + pr_warn(
2437 + "overlayfs: failed to unlink whiteout %.*s: %i\n",
2438 + p->len, p->name, ret);
2439 + }
2440 + mutex_unlock(&upperdir->d_inode->i_mutex);
2441 +
2442 +out_revert_creds:
2443 + revert_creds(old_cred);
2444 + put_cred(override_cred);
2445 +
2446 + return err;
2447 +}
2448 +
2449 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
2450 +{
2451 + int err;
2452 + LIST_HEAD(list);
2453 +
2454 + err = ovl_check_empty_dir(dentry, &list);
2455 + if (!err && type == OVL_PATH_MERGE)
2456 + err = ovl_remove_whiteouts(dentry, &list);
2457 +
2458 + ovl_cache_free(&list);
2459 +
2460 + return err;
2461 +}
2462 --- /dev/null
2463 +++ b/fs/overlayfs/super.c
2464 @@ -0,0 +1,685 @@
2465 +/*
2466 + *
2467 + * Copyright (C) 2011 Novell Inc.
2468 + *
2469 + * This program is free software; you can redistribute it and/or modify it
2470 + * under the terms of the GNU General Public License version 2 as published by
2471 + * the Free Software Foundation.
2472 + */
2473 +
2474 +#include <linux/fs.h>
2475 +#include <linux/namei.h>
2476 +#include <linux/xattr.h>
2477 +#include <linux/security.h>
2478 +#include <linux/mount.h>
2479 +#include <linux/slab.h>
2480 +#include <linux/parser.h>
2481 +#include <linux/module.h>
2482 +#include <linux/cred.h>
2483 +#include <linux/sched.h>
2484 +#include <linux/statfs.h>
2485 +#include <linux/seq_file.h>
2486 +#include "overlayfs.h"
2487 +
2488 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2489 +MODULE_DESCRIPTION("Overlay filesystem");
2490 +MODULE_LICENSE("GPL");
2491 +
2492 +#define OVERLAYFS_SUPER_MAGIC 0x794c764f
2493 +
2494 +struct ovl_config {
2495 + char *lowerdir;
2496 + char *upperdir;
2497 +};
2498 +
2499 +/* private information held for overlayfs's superblock */
2500 +struct ovl_fs {
2501 + struct vfsmount *upper_mnt;
2502 + struct vfsmount *lower_mnt;
2503 + long lower_namelen;
2504 + /* pathnames of lower and upper dirs, for show_options */
2505 + struct ovl_config config;
2506 +};
2507 +
2508 +/* private information held for every overlayfs dentry */
2509 +struct ovl_entry {
2510 + /*
2511 + * Keep "double reference" on upper dentries, so that
2512 + * d_delete() doesn't think it's OK to reset d_inode to NULL.
2513 + */
2514 + struct dentry *__upperdentry;
2515 + struct dentry *lowerdentry;
2516 + union {
2517 + struct {
2518 + u64 version;
2519 + bool opaque;
2520 + };
2521 + struct rcu_head rcu;
2522 + };
2523 +};
2524 +
2525 +const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
2526 +const char *ovl_opaque_xattr = "trusted.overlay.opaque";
2527 +
2528 +
2529 +enum ovl_path_type ovl_path_type(struct dentry *dentry)
2530 +{
2531 + struct ovl_entry *oe = dentry->d_fsdata;
2532 +
2533 + if (oe->__upperdentry) {
2534 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
2535 + return OVL_PATH_MERGE;
2536 + else
2537 + return OVL_PATH_UPPER;
2538 + } else {
2539 + return OVL_PATH_LOWER;
2540 + }
2541 +}
2542 +
2543 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
2544 +{
2545 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
2546 + smp_read_barrier_depends();
2547 + return upperdentry;
2548 +}
2549 +
2550 +void ovl_path_upper(struct dentry *dentry, struct path *path)
2551 +{
2552 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2553 + struct ovl_entry *oe = dentry->d_fsdata;
2554 +
2555 + path->mnt = ofs->upper_mnt;
2556 + path->dentry = ovl_upperdentry_dereference(oe);
2557 +}
2558 +
2559 +void ovl_path_lower(struct dentry *dentry, struct path *path)
2560 +{
2561 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2562 + struct ovl_entry *oe = dentry->d_fsdata;
2563 +
2564 + path->mnt = ofs->lower_mnt;
2565 + path->dentry = oe->lowerdentry;
2566 +}
2567 +
2568 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
2569 +{
2570 +
2571 + enum ovl_path_type type = ovl_path_type(dentry);
2572 +
2573 + if (type == OVL_PATH_LOWER)
2574 + ovl_path_lower(dentry, path);
2575 + else
2576 + ovl_path_upper(dentry, path);
2577 +
2578 + return type;
2579 +}
2580 +
2581 +struct dentry *ovl_dentry_upper(struct dentry *dentry)
2582 +{
2583 + struct ovl_entry *oe = dentry->d_fsdata;
2584 +
2585 + return ovl_upperdentry_dereference(oe);
2586 +}
2587 +
2588 +struct dentry *ovl_dentry_lower(struct dentry *dentry)
2589 +{
2590 + struct ovl_entry *oe = dentry->d_fsdata;
2591 +
2592 + return oe->lowerdentry;
2593 +}
2594 +
2595 +struct dentry *ovl_dentry_real(struct dentry *dentry)
2596 +{
2597 + struct ovl_entry *oe = dentry->d_fsdata;
2598 + struct dentry *realdentry;
2599 +
2600 + realdentry = ovl_upperdentry_dereference(oe);
2601 + if (!realdentry)
2602 + realdentry = oe->lowerdentry;
2603 +
2604 + return realdentry;
2605 +}
2606 +
2607 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
2608 +{
2609 + struct dentry *realdentry;
2610 +
2611 + realdentry = ovl_upperdentry_dereference(oe);
2612 + if (realdentry) {
2613 + *is_upper = true;
2614 + } else {
2615 + realdentry = oe->lowerdentry;
2616 + *is_upper = false;
2617 + }
2618 + return realdentry;
2619 +}
2620 +
2621 +bool ovl_dentry_is_opaque(struct dentry *dentry)
2622 +{
2623 + struct ovl_entry *oe = dentry->d_fsdata;
2624 + return oe->opaque;
2625 +}
2626 +
2627 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
2628 +{
2629 + struct ovl_entry *oe = dentry->d_fsdata;
2630 + oe->opaque = opaque;
2631 +}
2632 +
2633 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
2634 +{
2635 + struct ovl_entry *oe = dentry->d_fsdata;
2636 +
2637 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
2638 + WARN_ON(oe->__upperdentry);
2639 + BUG_ON(!upperdentry->d_inode);
2640 + smp_wmb();
2641 + oe->__upperdentry = dget(upperdentry);
2642 +}
2643 +
2644 +void ovl_dentry_version_inc(struct dentry *dentry)
2645 +{
2646 + struct ovl_entry *oe = dentry->d_fsdata;
2647 +
2648 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2649 + oe->version++;
2650 +}
2651 +
2652 +u64 ovl_dentry_version_get(struct dentry *dentry)
2653 +{
2654 + struct ovl_entry *oe = dentry->d_fsdata;
2655 +
2656 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2657 + return oe->version;
2658 +}
2659 +
2660 +bool ovl_is_whiteout(struct dentry *dentry)
2661 +{
2662 + int res;
2663 + char val;
2664 +
2665 + if (!dentry)
2666 + return false;
2667 + if (!dentry->d_inode)
2668 + return false;
2669 + if (!S_ISLNK(dentry->d_inode->i_mode))
2670 + return false;
2671 +
2672 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
2673 + if (res == 1 && val == 'y')
2674 + return true;
2675 +
2676 + return false;
2677 +}
2678 +
2679 +static bool ovl_is_opaquedir(struct dentry *dentry)
2680 +{
2681 + int res;
2682 + char val;
2683 +
2684 + if (!S_ISDIR(dentry->d_inode->i_mode))
2685 + return false;
2686 +
2687 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
2688 + if (res == 1 && val == 'y')
2689 + return true;
2690 +
2691 + return false;
2692 +}
2693 +
2694 +static void ovl_entry_free(struct rcu_head *head)
2695 +{
2696 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
2697 + kfree(oe);
2698 +}
2699 +
2700 +static void ovl_dentry_release(struct dentry *dentry)
2701 +{
2702 + struct ovl_entry *oe = dentry->d_fsdata;
2703 +
2704 + if (oe) {
2705 + dput(oe->__upperdentry);
2706 + dput(oe->__upperdentry);
2707 + dput(oe->lowerdentry);
2708 + call_rcu(&oe->rcu, ovl_entry_free);
2709 + }
2710 +}
2711 +
2712 +const struct dentry_operations ovl_dentry_operations = {
2713 + .d_release = ovl_dentry_release,
2714 +};
2715 +
2716 +static struct ovl_entry *ovl_alloc_entry(void)
2717 +{
2718 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
2719 +}
2720 +
2721 +static inline struct dentry *ovl_lookup_real(struct dentry *dir,
2722 + struct qstr *name)
2723 +{
2724 + struct dentry *dentry;
2725 +
2726 + mutex_lock(&dir->d_inode->i_mutex);
2727 + dentry = lookup_one_len(name->name, dir, name->len);
2728 + mutex_unlock(&dir->d_inode->i_mutex);
2729 +
2730 + if (IS_ERR(dentry)) {
2731 + if (PTR_ERR(dentry) == -ENOENT)
2732 + dentry = NULL;
2733 + } else if (!dentry->d_inode) {
2734 + dput(dentry);
2735 + dentry = NULL;
2736 + }
2737 + return dentry;
2738 +}
2739 +
2740 +static int ovl_do_lookup(struct dentry *dentry)
2741 +{
2742 + struct ovl_entry *oe;
2743 + struct dentry *upperdir;
2744 + struct dentry *lowerdir;
2745 + struct dentry *upperdentry = NULL;
2746 + struct dentry *lowerdentry = NULL;
2747 + struct inode *inode = NULL;
2748 + int err;
2749 +
2750 + err = -ENOMEM;
2751 + oe = ovl_alloc_entry();
2752 + if (!oe)
2753 + goto out;
2754 +
2755 + upperdir = ovl_dentry_upper(dentry->d_parent);
2756 + lowerdir = ovl_dentry_lower(dentry->d_parent);
2757 +
2758 + if (upperdir) {
2759 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
2760 + err = PTR_ERR(upperdentry);
2761 + if (IS_ERR(upperdentry))
2762 + goto out_put_dir;
2763 +
2764 + if (lowerdir && upperdentry &&
2765 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
2766 + S_ISDIR(upperdentry->d_inode->i_mode))) {
2767 + const struct cred *old_cred;
2768 + struct cred *override_cred;
2769 +
2770 + err = -ENOMEM;
2771 + override_cred = prepare_creds();
2772 + if (!override_cred)
2773 + goto out_dput_upper;
2774 +
2775 + /* CAP_SYS_ADMIN needed for getxattr */
2776 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2777 + old_cred = override_creds(override_cred);
2778 +
2779 + if (ovl_is_opaquedir(upperdentry)) {
2780 + oe->opaque = true;
2781 + } else if (ovl_is_whiteout(upperdentry)) {
2782 + dput(upperdentry);
2783 + upperdentry = NULL;
2784 + oe->opaque = true;
2785 + }
2786 + revert_creds(old_cred);
2787 + put_cred(override_cred);
2788 + }
2789 + }
2790 + if (lowerdir && !oe->opaque) {
2791 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
2792 + err = PTR_ERR(lowerdentry);
2793 + if (IS_ERR(lowerdentry))
2794 + goto out_dput_upper;
2795 + }
2796 +
2797 + if (lowerdentry && upperdentry &&
2798 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
2799 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
2800 + dput(lowerdentry);
2801 + lowerdentry = NULL;
2802 + oe->opaque = true;
2803 + }
2804 +
2805 + if (lowerdentry || upperdentry) {
2806 + struct dentry *realdentry;
2807 +
2808 + realdentry = upperdentry ? upperdentry : lowerdentry;
2809 + err = -ENOMEM;
2810 + inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
2811 + oe);
2812 + if (!inode)
2813 + goto out_dput;
2814 + ovl_copyattr(realdentry->d_inode, inode);
2815 + }
2816 +
2817 + if (upperdentry)
2818 + oe->__upperdentry = dget(upperdentry);
2819 +
2820 + if (lowerdentry)
2821 + oe->lowerdentry = lowerdentry;
2822 +
2823 + dentry->d_fsdata = oe;
2824 + dentry->d_op = &ovl_dentry_operations;
2825 + d_add(dentry, inode);
2826 +
2827 + return 0;
2828 +
2829 +out_dput:
2830 + dput(lowerdentry);
2831 +out_dput_upper:
2832 + dput(upperdentry);
2833 +out_put_dir:
2834 + kfree(oe);
2835 +out:
2836 + return err;
2837 +}
2838 +
2839 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
2840 + unsigned int flags)
2841 +{
2842 + int err = ovl_do_lookup(dentry);
2843 +
2844 + if (err)
2845 + return ERR_PTR(err);
2846 +
2847 + return NULL;
2848 +}
2849 +
2850 +struct file *ovl_path_open(struct path *path, int flags)
2851 +{
2852 + return dentry_open(path, flags, current_cred());
2853 +}
2854 +
2855 +static void ovl_put_super(struct super_block *sb)
2856 +{
2857 + struct ovl_fs *ufs = sb->s_fs_info;
2858 +
2859 + if (!(sb->s_flags & MS_RDONLY))
2860 + mnt_drop_write(ufs->upper_mnt);
2861 +
2862 + mntput(ufs->upper_mnt);
2863 + mntput(ufs->lower_mnt);
2864 +
2865 + kfree(ufs->config.lowerdir);
2866 + kfree(ufs->config.upperdir);
2867 + kfree(ufs);
2868 +}
2869 +
2870 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2871 +{
2872 + int flags = *flagsp;
2873 + struct ovl_fs *ufs = sb->s_fs_info;
2874 +
2875 + /* When remounting rw or ro, we need to adjust the write access to the
2876 + * upper fs.
2877 + */
2878 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2879 + /* No change to readonly status */
2880 + return 0;
2881 +
2882 + if (flags & MS_RDONLY) {
2883 + mnt_drop_write(ufs->upper_mnt);
2884 + return 0;
2885 + } else
2886 + return mnt_want_write(ufs->upper_mnt);
2887 +}
2888 +
2889 +/**
2890 + * ovl_statfs
2891 + * @sb: The overlayfs super block
2892 + * @buf: The struct kstatfs to fill in with stats
2893 + *
2894 + * Get the filesystem statistics. As writes always target the upper layer
2895 + * filesystem pass the statfs to the same filesystem.
2896 + */
2897 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2898 +{
2899 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2900 + struct dentry *root_dentry = dentry->d_sb->s_root;
2901 + struct path path;
2902 + int err;
2903 +
2904 + ovl_path_upper(root_dentry, &path);
2905 +
2906 + err = vfs_statfs(&path, buf);
2907 + if (!err) {
2908 + buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
2909 + buf->f_type = OVERLAYFS_SUPER_MAGIC;
2910 + }
2911 +
2912 + return err;
2913 +}
2914 +
2915 +/**
2916 + * ovl_show_options
2917 + *
2918 + * Prints the mount options for a given superblock.
2919 + * Returns zero; does not fail.
2920 + */
2921 +static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
2922 +{
2923 + struct super_block *sb = dentry->d_sb;
2924 + struct ovl_fs *ufs = sb->s_fs_info;
2925 +
2926 + seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
2927 + seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
2928 + return 0;
2929 +}
2930 +
2931 +static const struct super_operations ovl_super_operations = {
2932 + .put_super = ovl_put_super,
2933 + .remount_fs = ovl_remount_fs,
2934 + .statfs = ovl_statfs,
2935 + .show_options = ovl_show_options,
2936 +};
2937 +
2938 +enum {
2939 + OPT_LOWERDIR,
2940 + OPT_UPPERDIR,
2941 + OPT_ERR,
2942 +};
2943 +
2944 +static const match_table_t ovl_tokens = {
2945 + {OPT_LOWERDIR, "lowerdir=%s"},
2946 + {OPT_UPPERDIR, "upperdir=%s"},
2947 + {OPT_ERR, NULL}
2948 +};
2949 +
2950 +static int ovl_parse_opt(char *opt, struct ovl_config *config)
2951 +{
2952 + char *p;
2953 +
2954 + config->upperdir = NULL;
2955 + config->lowerdir = NULL;
2956 +
2957 + while ((p = strsep(&opt, ",")) != NULL) {
2958 + int token;
2959 + substring_t args[MAX_OPT_ARGS];
2960 +
2961 + if (!*p)
2962 + continue;
2963 +
2964 + token = match_token(p, ovl_tokens, args);
2965 + switch (token) {
2966 + case OPT_UPPERDIR:
2967 + kfree(config->upperdir);
2968 + config->upperdir = match_strdup(&args[0]);
2969 + if (!config->upperdir)
2970 + return -ENOMEM;
2971 + break;
2972 +
2973 + case OPT_LOWERDIR:
2974 + kfree(config->lowerdir);
2975 + config->lowerdir = match_strdup(&args[0]);
2976 + if (!config->lowerdir)
2977 + return -ENOMEM;
2978 + break;
2979 +
2980 + default:
2981 + return -EINVAL;
2982 + }
2983 + }
2984 + return 0;
2985 +}
2986 +
2987 +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
2988 +{
2989 + struct path lowerpath;
2990 + struct path upperpath;
2991 + struct inode *root_inode;
2992 + struct dentry *root_dentry;
2993 + struct ovl_entry *oe;
2994 + struct ovl_fs *ufs;
2995 + struct kstatfs statfs;
2996 + int err;
2997 +
2998 + err = -ENOMEM;
2999 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
3000 + if (!ufs)
3001 + goto out;
3002 +
3003 + err = ovl_parse_opt((char *) data, &ufs->config);
3004 + if (err)
3005 + goto out_free_ufs;
3006 +
3007 + err = -EINVAL;
3008 + if (!ufs->config.upperdir || !ufs->config.lowerdir) {
3009 + pr_err("overlayfs: missing upperdir or lowerdir\n");
3010 + goto out_free_config;
3011 + }
3012 +
3013 + oe = ovl_alloc_entry();
3014 + if (oe == NULL)
3015 + goto out_free_config;
3016 +
3017 + err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
3018 + if (err)
3019 + goto out_free_oe;
3020 +
3021 + err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
3022 + if (err)
3023 + goto out_put_upperpath;
3024 +
3025 + err = -ENOTDIR;
3026 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
3027 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
3028 + goto out_put_lowerpath;
3029 +
3030 + err = vfs_statfs(&lowerpath, &statfs);
3031 + if (err) {
3032 + pr_err("overlayfs: statfs failed on lowerpath\n");
3033 + goto out_put_lowerpath;
3034 + }
3035 + ufs->lower_namelen = statfs.f_namelen;
3036 +
3037 + sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
3038 + lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
3039 +
3040 + err = -EINVAL;
3041 + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
3042 + pr_err("overlayfs: maximum fs stacking depth exceeded\n");
3043 + goto out_put_lowerpath;
3044 + }
3045 +
3046 +
3047 + ufs->upper_mnt = clone_private_mount(&upperpath);
3048 + err = PTR_ERR(ufs->upper_mnt);
3049 + if (IS_ERR(ufs->upper_mnt)) {
3050 + pr_err("overlayfs: failed to clone upperpath\n");
3051 + goto out_put_lowerpath;
3052 + }
3053 +
3054 + ufs->lower_mnt = clone_private_mount(&lowerpath);
3055 + err = PTR_ERR(ufs->lower_mnt);
3056 + if (IS_ERR(ufs->lower_mnt)) {
3057 + pr_err("overlayfs: failed to clone lowerpath\n");
3058 + goto out_put_upper_mnt;
3059 + }
3060 +
3061 + /*
3062 + * Make lower_mnt R/O. That way fchmod/fchown on lower file
3063 + * will fail instead of modifying lower fs.
3064 + */
3065 + ufs->lower_mnt->mnt_flags |= MNT_READONLY;
3066 +
3067 + /* If the upper fs is r/o, we mark overlayfs r/o too */
3068 + if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
3069 + sb->s_flags |= MS_RDONLY;
3070 +
3071 + if (!(sb->s_flags & MS_RDONLY)) {
3072 + err = mnt_want_write(ufs->upper_mnt);
3073 + if (err)
3074 + goto out_put_lower_mnt;
3075 + }
3076 +
3077 + err = -ENOMEM;
3078 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
3079 + if (!root_inode)
3080 + goto out_drop_write;
3081 +
3082 + root_dentry = d_make_root(root_inode);
3083 + if (!root_dentry)
3084 + goto out_drop_write;
3085 +
3086 + mntput(upperpath.mnt);
3087 + mntput(lowerpath.mnt);
3088 +
3089 + oe->__upperdentry = dget(upperpath.dentry);
3090 + oe->lowerdentry = lowerpath.dentry;
3091 +
3092 + root_dentry->d_fsdata = oe;
3093 + root_dentry->d_op = &ovl_dentry_operations;
3094 +
3095 + sb->s_magic = OVERLAYFS_SUPER_MAGIC;
3096 + sb->s_op = &ovl_super_operations;
3097 + sb->s_root = root_dentry;
3098 + sb->s_fs_info = ufs;
3099 +
3100 + return 0;
3101 +
3102 +out_drop_write:
3103 + if (!(sb->s_flags & MS_RDONLY))
3104 + mnt_drop_write(ufs->upper_mnt);
3105 +out_put_lower_mnt:
3106 + mntput(ufs->lower_mnt);
3107 +out_put_upper_mnt:
3108 + mntput(ufs->upper_mnt);
3109 +out_put_lowerpath:
3110 + path_put(&lowerpath);
3111 +out_put_upperpath:
3112 + path_put(&upperpath);
3113 +out_free_oe:
3114 + kfree(oe);
3115 +out_free_config:
3116 + kfree(ufs->config.lowerdir);
3117 + kfree(ufs->config.upperdir);
3118 +out_free_ufs:
3119 + kfree(ufs);
3120 +out:
3121 + return err;
3122 +}
3123 +
3124 +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
3125 + const char *dev_name, void *raw_data)
3126 +{
3127 + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
3128 +}
3129 +
3130 +static struct file_system_type ovl_fs_type = {
3131 + .owner = THIS_MODULE,
3132 + .name = "overlayfs",
3133 + .mount = ovl_mount,
3134 + .kill_sb = kill_anon_super,
3135 +};
3136 +MODULE_ALIAS_FS("overlayfs");
3137 +
3138 +static int __init ovl_init(void)
3139 +{
3140 + return register_filesystem(&ovl_fs_type);
3141 +}
3142 +
3143 +static void __exit ovl_exit(void)
3144 +{
3145 + unregister_filesystem(&ovl_fs_type);
3146 +}
3147 +
3148 +module_init(ovl_init);
3149 +module_exit(ovl_exit);
3150 --- a/fs/splice.c
3151 +++ b/fs/splice.c
3152 @@ -1313,6 +1313,7 @@ long do_splice_direct(struct file *in, l
3153
3154 return ret;
3155 }
3156 +EXPORT_SYMBOL(do_splice_direct);
3157
3158 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
3159 struct pipe_inode_info *opipe,
3160 --- a/include/linux/fs.h
3161 +++ b/include/linux/fs.h
3162 @@ -244,6 +244,12 @@ struct iattr {
3163 */
3164 #include <linux/quota.h>
3165
3166 +/*
3167 + * Maximum number of layers of fs stack. Needs to be limited to
3168 + * prevent kernel stack overflow
3169 + */
3170 +#define FILESYSTEM_MAX_STACK_DEPTH 2
3171 +
3172 /**
3173 * enum positive_aop_returns - aop return codes with specific semantics
3174 *
3175 @@ -1322,6 +1328,11 @@ struct super_block {
3176
3177 /* Being remounted read-only */
3178 int s_readonly_remount;
3179 +
3180 + /*
3181 + * Indicates how deep in a filesystem stack this SB is
3182 + */
3183 + int s_stack_depth;
3184 };
3185
3186 /* superblock cache pruning functions */
3187 @@ -1575,6 +1586,7 @@ struct inode_operations {
3188 int (*atomic_open)(struct inode *, struct dentry *,
3189 struct file *, unsigned open_flag,
3190 umode_t create_mode, int *opened);
3191 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
3192 } ____cacheline_aligned;
3193
3194 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
3195 @@ -2008,6 +2020,7 @@ extern struct file *file_open_name(struc
3196 extern struct file *filp_open(const char *, int, umode_t);
3197 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
3198 const char *, int);
3199 +extern int vfs_open(const struct path *, struct file *, const struct cred *);
3200 extern struct file * dentry_open(const struct path *, int, const struct cred *);
3201 extern int filp_close(struct file *, fl_owner_t id);
3202
3203 @@ -2208,6 +2221,7 @@ extern sector_t bmap(struct inode *, sec
3204 #endif
3205 extern int notify_change(struct dentry *, struct iattr *);
3206 extern int inode_permission(struct inode *, int);
3207 +extern int __inode_permission(struct inode *, int);
3208 extern int generic_permission(struct inode *, int);
3209
3210 static inline bool execute_ok(struct inode *inode)
3211 @@ -2414,6 +2428,9 @@ extern ssize_t generic_file_splice_write
3212 struct file *, loff_t *, size_t, unsigned int);
3213 extern ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe,
3214 struct file *out, loff_t *, size_t len, unsigned int flags);
3215 +extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
3216 + loff_t *opos, size_t len, unsigned int flags);
3217 +
3218
3219 extern void
3220 file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping);
3221 --- a/include/linux/mount.h
3222 +++ b/include/linux/mount.h
3223 @@ -68,6 +68,9 @@ extern void mnt_pin(struct vfsmount *mnt
3224 extern void mnt_unpin(struct vfsmount *mnt);
3225 extern int __mnt_is_readonly(struct vfsmount *mnt);
3226
3227 +struct path;
3228 +extern struct vfsmount *clone_private_mount(struct path *path);
3229 +
3230 struct file_system_type;
3231 extern struct vfsmount *vfs_kern_mount(struct file_system_type *type,
3232 int flags, const char *name,