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