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