2e9e023b690ae771d18458701391314db99dfa74
[openwrt/svn-archive/archive.git] / target / linux / generic / patches-3.12 / 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 @@ -6230,6 +6230,13 @@ F: drivers/scsi/osd/
247 F: include/scsi/osd_*
248 F: fs/exofs/
249
250 +OVERLAYFS FILESYSTEM
251 +M: Miklos Szeredi <miklos@szeredi.hu>
252 +L: linux-fsdevel@vger.kernel.org
253 +S: Supported
254 +F: fs/overlayfs/*
255 +F: Documentation/filesystems/overlayfs.txt
256 +
257 P54 WIRELESS DRIVER
258 M: Christian Lamparter <chunkeey@googlemail.com>
259 L: linux-wireless@vger.kernel.org
260 --- a/fs/Kconfig
261 +++ b/fs/Kconfig
262 @@ -67,6 +67,7 @@ source "fs/quota/Kconfig"
263
264 source "fs/autofs4/Kconfig"
265 source "fs/fuse/Kconfig"
266 +source "fs/overlayfs/Kconfig"
267
268 config GENERIC_ACL
269 bool
270 --- a/fs/Makefile
271 +++ b/fs/Makefile
272 @@ -105,6 +105,7 @@ obj-$(CONFIG_QNX6FS_FS) += qnx6/
273 obj-$(CONFIG_AUTOFS4_FS) += autofs4/
274 obj-$(CONFIG_ADFS_FS) += adfs/
275 obj-$(CONFIG_FUSE_FS) += fuse/
276 +obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
277 obj-$(CONFIG_UDF_FS) += udf/
278 obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
279 obj-$(CONFIG_OMFS_FS) += omfs/
280 --- a/fs/ecryptfs/main.c
281 +++ b/fs/ecryptfs/main.c
282 @@ -566,6 +566,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 @@ -44,7 +44,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 @@ -142,12 +141,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 @@ -402,6 +402,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 @@ -3063,9 +3064,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 @@ -1466,6 +1466,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 @@ -814,8 +814,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 @@ -832,6 +831,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);
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);
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);
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,605 @@
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);
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);
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 + if (err)
1208 + goto out_d_drop;
1209 +
1210 + ovl_dentry_version_inc(dentry->d_parent);
1211 + }
1212 +
1213 + if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
1214 + err = ovl_whiteout(upperdir, dentry);
1215 +
1216 + /*
1217 + * Keeping this dentry hashed would mean having to release
1218 + * upperpath/lowerpath, which could only be done if we are the
1219 + * sole user of this dentry. Too tricky... Just unhash for
1220 + * now.
1221 + */
1222 +out_d_drop:
1223 + d_drop(dentry);
1224 + mutex_unlock(&upperdir->d_inode->i_mutex);
1225 +
1226 + return err;
1227 +}
1228 +
1229 +static int ovl_unlink(struct inode *dir, struct dentry *dentry)
1230 +{
1231 + return ovl_do_remove(dentry, false);
1232 +}
1233 +
1234 +
1235 +static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
1236 +{
1237 + int err;
1238 + enum ovl_path_type type;
1239 +
1240 + type = ovl_path_type(dentry);
1241 + if (type != OVL_PATH_UPPER) {
1242 + err = ovl_check_empty_and_clear(dentry, type);
1243 + if (err)
1244 + return err;
1245 + }
1246 +
1247 + return ovl_do_remove(dentry, true);
1248 +}
1249 +
1250 +static int ovl_link(struct dentry *old, struct inode *newdir,
1251 + struct dentry *new)
1252 +{
1253 + int err;
1254 + struct dentry *olddentry;
1255 + struct dentry *newdentry;
1256 + struct dentry *upperdir;
1257 + struct inode *newinode;
1258 +
1259 + err = ovl_copy_up(old);
1260 + if (err)
1261 + goto out;
1262 +
1263 + err = ovl_copy_up(new->d_parent);
1264 + if (err)
1265 + goto out;
1266 +
1267 + upperdir = ovl_dentry_upper(new->d_parent);
1268 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
1269 + newdentry = ovl_lookup_create(upperdir, new);
1270 + err = PTR_ERR(newdentry);
1271 + if (IS_ERR(newdentry))
1272 + goto out_unlock;
1273 +
1274 + olddentry = ovl_dentry_upper(old);
1275 + err = vfs_link(olddentry, upperdir->d_inode, newdentry);
1276 + if (!err) {
1277 + if (WARN_ON(!newdentry->d_inode)) {
1278 + dput(newdentry);
1279 + err = -ENOENT;
1280 + goto out_unlock;
1281 + }
1282 + newinode = ovl_new_inode(old->d_sb, newdentry->d_inode->i_mode,
1283 + new->d_fsdata);
1284 + if (!newinode) {
1285 + err = -ENOMEM;
1286 + goto link_fail;
1287 + }
1288 + ovl_copyattr(upperdir->d_inode, newinode);
1289 +
1290 + ovl_dentry_version_inc(new->d_parent);
1291 + ovl_dentry_update(new, newdentry);
1292 +
1293 + d_instantiate(new, newinode);
1294 + } else {
1295 +link_fail:
1296 + if (ovl_dentry_is_opaque(new))
1297 + ovl_whiteout(upperdir, new);
1298 + dput(newdentry);
1299 + }
1300 +out_unlock:
1301 + mutex_unlock(&upperdir->d_inode->i_mutex);
1302 +out:
1303 + return err;
1304 +}
1305 +
1306 +static int ovl_rename(struct inode *olddir, struct dentry *old,
1307 + struct inode *newdir, struct dentry *new)
1308 +{
1309 + int err;
1310 + enum ovl_path_type old_type;
1311 + enum ovl_path_type new_type;
1312 + struct dentry *old_upperdir;
1313 + struct dentry *new_upperdir;
1314 + struct dentry *olddentry;
1315 + struct dentry *newdentry;
1316 + struct dentry *trap;
1317 + bool old_opaque;
1318 + bool new_opaque;
1319 + bool new_create = false;
1320 + bool is_dir = S_ISDIR(old->d_inode->i_mode);
1321 +
1322 + /* Don't copy up directory trees */
1323 + old_type = ovl_path_type(old);
1324 + if (old_type != OVL_PATH_UPPER && is_dir)
1325 + return -EXDEV;
1326 +
1327 + if (new->d_inode) {
1328 + new_type = ovl_path_type(new);
1329 +
1330 + if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
1331 + if (ovl_dentry_lower(old)->d_inode ==
1332 + ovl_dentry_lower(new)->d_inode)
1333 + return 0;
1334 + }
1335 + if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
1336 + if (ovl_dentry_upper(old)->d_inode ==
1337 + ovl_dentry_upper(new)->d_inode)
1338 + return 0;
1339 + }
1340 +
1341 + if (new_type != OVL_PATH_UPPER &&
1342 + S_ISDIR(new->d_inode->i_mode)) {
1343 + err = ovl_check_empty_and_clear(new, new_type);
1344 + if (err)
1345 + return err;
1346 + }
1347 + } else {
1348 + new_type = OVL_PATH_UPPER;
1349 + }
1350 +
1351 + err = ovl_copy_up(old);
1352 + if (err)
1353 + return err;
1354 +
1355 + err = ovl_copy_up(new->d_parent);
1356 + if (err)
1357 + return err;
1358 +
1359 + old_upperdir = ovl_dentry_upper(old->d_parent);
1360 + new_upperdir = ovl_dentry_upper(new->d_parent);
1361 +
1362 + trap = lock_rename(new_upperdir, old_upperdir);
1363 +
1364 + olddentry = ovl_dentry_upper(old);
1365 + newdentry = ovl_dentry_upper(new);
1366 + if (newdentry) {
1367 + dget(newdentry);
1368 + } else {
1369 + new_create = true;
1370 + newdentry = ovl_lookup_create(new_upperdir, new);
1371 + err = PTR_ERR(newdentry);
1372 + if (IS_ERR(newdentry))
1373 + goto out_unlock;
1374 + }
1375 +
1376 + err = -ESTALE;
1377 + if (olddentry->d_parent != old_upperdir)
1378 + goto out_dput;
1379 + if (newdentry->d_parent != new_upperdir)
1380 + goto out_dput;
1381 + if (olddentry == trap)
1382 + goto out_dput;
1383 + if (newdentry == trap)
1384 + goto out_dput;
1385 +
1386 + old_opaque = ovl_dentry_is_opaque(old);
1387 + new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
1388 +
1389 + if (is_dir && !old_opaque && new_opaque) {
1390 + err = ovl_set_opaque(olddentry);
1391 + if (err)
1392 + goto out_dput;
1393 + }
1394 +
1395 + err = vfs_rename(old_upperdir->d_inode, olddentry,
1396 + new_upperdir->d_inode, newdentry);
1397 +
1398 + if (err) {
1399 + if (new_create && ovl_dentry_is_opaque(new))
1400 + ovl_whiteout(new_upperdir, new);
1401 + if (is_dir && !old_opaque && new_opaque)
1402 + ovl_remove_opaque(olddentry);
1403 + goto out_dput;
1404 + }
1405 +
1406 + if (old_type != OVL_PATH_UPPER || old_opaque)
1407 + err = ovl_whiteout(old_upperdir, old);
1408 + if (is_dir && old_opaque && !new_opaque)
1409 + ovl_remove_opaque(olddentry);
1410 +
1411 + if (old_opaque != new_opaque)
1412 + ovl_dentry_set_opaque(old, new_opaque);
1413 +
1414 + ovl_dentry_version_inc(old->d_parent);
1415 + ovl_dentry_version_inc(new->d_parent);
1416 +
1417 +out_dput:
1418 + dput(newdentry);
1419 +out_unlock:
1420 + unlock_rename(new_upperdir, old_upperdir);
1421 + return err;
1422 +}
1423 +
1424 +const struct inode_operations ovl_dir_inode_operations = {
1425 + .lookup = ovl_lookup,
1426 + .mkdir = ovl_mkdir,
1427 + .symlink = ovl_symlink,
1428 + .unlink = ovl_unlink,
1429 + .rmdir = ovl_rmdir,
1430 + .rename = ovl_rename,
1431 + .link = ovl_link,
1432 + .setattr = ovl_setattr,
1433 + .create = ovl_create,
1434 + .mknod = ovl_mknod,
1435 + .permission = ovl_permission,
1436 + .getattr = ovl_dir_getattr,
1437 + .setxattr = ovl_setxattr,
1438 + .getxattr = ovl_getxattr,
1439 + .listxattr = ovl_listxattr,
1440 + .removexattr = ovl_removexattr,
1441 +};
1442 --- /dev/null
1443 +++ b/fs/overlayfs/inode.c
1444 @@ -0,0 +1,372 @@
1445 +/*
1446 + *
1447 + * Copyright (C) 2011 Novell Inc.
1448 + *
1449 + * This program is free software; you can redistribute it and/or modify it
1450 + * under the terms of the GNU General Public License version 2 as published by
1451 + * the Free Software Foundation.
1452 + */
1453 +
1454 +#include <linux/fs.h>
1455 +#include <linux/slab.h>
1456 +#include <linux/xattr.h>
1457 +#include "overlayfs.h"
1458 +
1459 +int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1460 +{
1461 + struct dentry *upperdentry;
1462 + int err;
1463 +
1464 + if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1465 + err = ovl_copy_up_truncate(dentry, attr->ia_size);
1466 + else
1467 + err = ovl_copy_up(dentry);
1468 + if (err)
1469 + return err;
1470 +
1471 + upperdentry = ovl_dentry_upper(dentry);
1472 +
1473 + if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1474 + attr->ia_valid &= ~ATTR_MODE;
1475 +
1476 + mutex_lock(&upperdentry->d_inode->i_mutex);
1477 + err = notify_change(upperdentry, attr);
1478 + if (!err)
1479 + ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
1480 + mutex_unlock(&upperdentry->d_inode->i_mutex);
1481 +
1482 + return err;
1483 +}
1484 +
1485 +static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1486 + struct kstat *stat)
1487 +{
1488 + struct path realpath;
1489 +
1490 + ovl_path_real(dentry, &realpath);
1491 + return vfs_getattr(&realpath, stat);
1492 +}
1493 +
1494 +int ovl_permission(struct inode *inode, int mask)
1495 +{
1496 + struct ovl_entry *oe;
1497 + struct dentry *alias = NULL;
1498 + struct inode *realinode;
1499 + struct dentry *realdentry;
1500 + bool is_upper;
1501 + int err;
1502 +
1503 + if (S_ISDIR(inode->i_mode)) {
1504 + oe = inode->i_private;
1505 + } else if (mask & MAY_NOT_BLOCK) {
1506 + return -ECHILD;
1507 + } else {
1508 + /*
1509 + * For non-directories find an alias and get the info
1510 + * from there.
1511 + */
1512 + alias = d_find_any_alias(inode);
1513 + if (WARN_ON(!alias))
1514 + return -ENOENT;
1515 +
1516 + oe = alias->d_fsdata;
1517 + }
1518 +
1519 + realdentry = ovl_entry_real(oe, &is_upper);
1520 +
1521 + /* Careful in RCU walk mode */
1522 + realinode = ACCESS_ONCE(realdentry->d_inode);
1523 + if (!realinode) {
1524 + WARN_ON(!(mask & MAY_NOT_BLOCK));
1525 + err = -ENOENT;
1526 + goto out_dput;
1527 + }
1528 +
1529 + if (mask & MAY_WRITE) {
1530 + umode_t mode = realinode->i_mode;
1531 +
1532 + /*
1533 + * Writes will always be redirected to upper layer, so
1534 + * ignore lower layer being read-only.
1535 + *
1536 + * If the overlay itself is read-only then proceed
1537 + * with the permission check, don't return EROFS.
1538 + * This will only happen if this is the lower layer of
1539 + * another overlayfs.
1540 + *
1541 + * If upper fs becomes read-only after the overlay was
1542 + * constructed return EROFS to prevent modification of
1543 + * upper layer.
1544 + */
1545 + err = -EROFS;
1546 + if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
1547 + (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1548 + goto out_dput;
1549 + }
1550 +
1551 + err = __inode_permission(realinode, mask);
1552 +out_dput:
1553 + dput(alias);
1554 + return err;
1555 +}
1556 +
1557 +
1558 +struct ovl_link_data {
1559 + struct dentry *realdentry;
1560 + void *cookie;
1561 +};
1562 +
1563 +static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1564 +{
1565 + void *ret;
1566 + struct dentry *realdentry;
1567 + struct inode *realinode;
1568 +
1569 + realdentry = ovl_dentry_real(dentry);
1570 + realinode = realdentry->d_inode;
1571 +
1572 + if (WARN_ON(!realinode->i_op->follow_link))
1573 + return ERR_PTR(-EPERM);
1574 +
1575 + ret = realinode->i_op->follow_link(realdentry, nd);
1576 + if (IS_ERR(ret))
1577 + return ret;
1578 +
1579 + if (realinode->i_op->put_link) {
1580 + struct ovl_link_data *data;
1581 +
1582 + data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1583 + if (!data) {
1584 + realinode->i_op->put_link(realdentry, nd, ret);
1585 + return ERR_PTR(-ENOMEM);
1586 + }
1587 + data->realdentry = realdentry;
1588 + data->cookie = ret;
1589 +
1590 + return data;
1591 + } else {
1592 + return NULL;
1593 + }
1594 +}
1595 +
1596 +static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1597 +{
1598 + struct inode *realinode;
1599 + struct ovl_link_data *data = c;
1600 +
1601 + if (!data)
1602 + return;
1603 +
1604 + realinode = data->realdentry->d_inode;
1605 + realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1606 + kfree(data);
1607 +}
1608 +
1609 +static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1610 +{
1611 + struct path realpath;
1612 + struct inode *realinode;
1613 +
1614 + ovl_path_real(dentry, &realpath);
1615 + realinode = realpath.dentry->d_inode;
1616 +
1617 + if (!realinode->i_op->readlink)
1618 + return -EINVAL;
1619 +
1620 + touch_atime(&realpath);
1621 +
1622 + return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1623 +}
1624 +
1625 +
1626 +static bool ovl_is_private_xattr(const char *name)
1627 +{
1628 + return strncmp(name, "trusted.overlay.", 14) == 0;
1629 +}
1630 +
1631 +int ovl_setxattr(struct dentry *dentry, const char *name,
1632 + const void *value, size_t size, int flags)
1633 +{
1634 + int err;
1635 + struct dentry *upperdentry;
1636 +
1637 + if (ovl_is_private_xattr(name))
1638 + return -EPERM;
1639 +
1640 + err = ovl_copy_up(dentry);
1641 + if (err)
1642 + return err;
1643 +
1644 + upperdentry = ovl_dentry_upper(dentry);
1645 + return vfs_setxattr(upperdentry, name, value, size, flags);
1646 +}
1647 +
1648 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1649 + void *value, size_t size)
1650 +{
1651 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1652 + ovl_is_private_xattr(name))
1653 + return -ENODATA;
1654 +
1655 + return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
1656 +}
1657 +
1658 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
1659 +{
1660 + ssize_t res;
1661 + int off;
1662 +
1663 + res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
1664 + if (res <= 0 || size == 0)
1665 + return res;
1666 +
1667 + if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
1668 + return res;
1669 +
1670 + /* filter out private xattrs */
1671 + for (off = 0; off < res;) {
1672 + char *s = list + off;
1673 + size_t slen = strlen(s) + 1;
1674 +
1675 + BUG_ON(off + slen > res);
1676 +
1677 + if (ovl_is_private_xattr(s)) {
1678 + res -= slen;
1679 + memmove(s, s + slen, res - off);
1680 + } else {
1681 + off += slen;
1682 + }
1683 + }
1684 +
1685 + return res;
1686 +}
1687 +
1688 +int ovl_removexattr(struct dentry *dentry, const char *name)
1689 +{
1690 + int err;
1691 + struct path realpath;
1692 + enum ovl_path_type type;
1693 +
1694 + if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1695 + ovl_is_private_xattr(name))
1696 + return -ENODATA;
1697 +
1698 + type = ovl_path_real(dentry, &realpath);
1699 + if (type == OVL_PATH_LOWER) {
1700 + err = vfs_getxattr(realpath.dentry, name, NULL, 0);
1701 + if (err < 0)
1702 + return err;
1703 +
1704 + err = ovl_copy_up(dentry);
1705 + if (err)
1706 + return err;
1707 +
1708 + ovl_path_upper(dentry, &realpath);
1709 + }
1710 +
1711 + return vfs_removexattr(realpath.dentry, name);
1712 +}
1713 +
1714 +static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
1715 + struct dentry *realdentry)
1716 +{
1717 + if (type != OVL_PATH_LOWER)
1718 + return false;
1719 +
1720 + if (special_file(realdentry->d_inode->i_mode))
1721 + return false;
1722 +
1723 + if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
1724 + return false;
1725 +
1726 + return true;
1727 +}
1728 +
1729 +static int ovl_dentry_open(struct dentry *dentry, struct file *file,
1730 + const struct cred *cred)
1731 +{
1732 + int err;
1733 + struct path realpath;
1734 + enum ovl_path_type type;
1735 +
1736 + type = ovl_path_real(dentry, &realpath);
1737 + if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
1738 + if (file->f_flags & O_TRUNC)
1739 + err = ovl_copy_up_truncate(dentry, 0);
1740 + else
1741 + err = ovl_copy_up(dentry);
1742 + if (err)
1743 + return err;
1744 +
1745 + ovl_path_upper(dentry, &realpath);
1746 + }
1747 +
1748 + return vfs_open(&realpath, file, cred);
1749 +}
1750 +
1751 +static const struct inode_operations ovl_file_inode_operations = {
1752 + .setattr = ovl_setattr,
1753 + .permission = ovl_permission,
1754 + .getattr = ovl_getattr,
1755 + .setxattr = ovl_setxattr,
1756 + .getxattr = ovl_getxattr,
1757 + .listxattr = ovl_listxattr,
1758 + .removexattr = ovl_removexattr,
1759 + .dentry_open = ovl_dentry_open,
1760 +};
1761 +
1762 +static const struct inode_operations ovl_symlink_inode_operations = {
1763 + .setattr = ovl_setattr,
1764 + .follow_link = ovl_follow_link,
1765 + .put_link = ovl_put_link,
1766 + .readlink = ovl_readlink,
1767 + .getattr = ovl_getattr,
1768 + .setxattr = ovl_setxattr,
1769 + .getxattr = ovl_getxattr,
1770 + .listxattr = ovl_listxattr,
1771 + .removexattr = ovl_removexattr,
1772 +};
1773 +
1774 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1775 + struct ovl_entry *oe)
1776 +{
1777 + struct inode *inode;
1778 +
1779 + inode = new_inode(sb);
1780 + if (!inode)
1781 + return NULL;
1782 +
1783 + mode &= S_IFMT;
1784 +
1785 + inode->i_ino = get_next_ino();
1786 + inode->i_mode = mode;
1787 + inode->i_flags |= S_NOATIME | S_NOCMTIME;
1788 +
1789 + switch (mode) {
1790 + case S_IFDIR:
1791 + inode->i_private = oe;
1792 + inode->i_op = &ovl_dir_inode_operations;
1793 + inode->i_fop = &ovl_dir_operations;
1794 + break;
1795 +
1796 + case S_IFLNK:
1797 + inode->i_op = &ovl_symlink_inode_operations;
1798 + break;
1799 +
1800 + case S_IFREG:
1801 + case S_IFSOCK:
1802 + case S_IFBLK:
1803 + case S_IFCHR:
1804 + case S_IFIFO:
1805 + inode->i_op = &ovl_file_inode_operations;
1806 + break;
1807 +
1808 + default:
1809 + WARN(1, "illegal file type: %i\n", mode);
1810 + iput(inode);
1811 + inode = NULL;
1812 + }
1813 +
1814 + return inode;
1815 +
1816 +}
1817 --- /dev/null
1818 +++ b/fs/overlayfs/overlayfs.h
1819 @@ -0,0 +1,70 @@
1820 +/*
1821 + *
1822 + * Copyright (C) 2011 Novell Inc.
1823 + *
1824 + * This program is free software; you can redistribute it and/or modify it
1825 + * under the terms of the GNU General Public License version 2 as published by
1826 + * the Free Software Foundation.
1827 + */
1828 +
1829 +struct ovl_entry;
1830 +
1831 +enum ovl_path_type {
1832 + OVL_PATH_UPPER,
1833 + OVL_PATH_MERGE,
1834 + OVL_PATH_LOWER,
1835 +};
1836 +
1837 +extern const char *ovl_opaque_xattr;
1838 +extern const char *ovl_whiteout_xattr;
1839 +extern const struct dentry_operations ovl_dentry_operations;
1840 +
1841 +enum ovl_path_type ovl_path_type(struct dentry *dentry);
1842 +u64 ovl_dentry_version_get(struct dentry *dentry);
1843 +void ovl_dentry_version_inc(struct dentry *dentry);
1844 +void ovl_path_upper(struct dentry *dentry, struct path *path);
1845 +void ovl_path_lower(struct dentry *dentry, struct path *path);
1846 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1847 +struct dentry *ovl_dentry_upper(struct dentry *dentry);
1848 +struct dentry *ovl_dentry_lower(struct dentry *dentry);
1849 +struct dentry *ovl_dentry_real(struct dentry *dentry);
1850 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
1851 +bool ovl_dentry_is_opaque(struct dentry *dentry);
1852 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
1853 +bool ovl_is_whiteout(struct dentry *dentry);
1854 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
1855 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
1856 + unsigned int flags);
1857 +struct file *ovl_path_open(struct path *path, int flags);
1858 +
1859 +struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
1860 + struct kstat *stat, const char *link);
1861 +
1862 +/* readdir.c */
1863 +extern const struct file_operations ovl_dir_operations;
1864 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
1865 +
1866 +/* inode.c */
1867 +int ovl_setattr(struct dentry *dentry, struct iattr *attr);
1868 +int ovl_permission(struct inode *inode, int mask);
1869 +int ovl_setxattr(struct dentry *dentry, const char *name,
1870 + const void *value, size_t size, int flags);
1871 +ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1872 + void *value, size_t size);
1873 +ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
1874 +int ovl_removexattr(struct dentry *dentry, const char *name);
1875 +
1876 +struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1877 + struct ovl_entry *oe);
1878 +static inline void ovl_copyattr(struct inode *from, struct inode *to)
1879 +{
1880 + to->i_uid = from->i_uid;
1881 + to->i_gid = from->i_gid;
1882 +}
1883 +
1884 +/* dir.c */
1885 +extern const struct inode_operations ovl_dir_inode_operations;
1886 +
1887 +/* copy_up.c */
1888 +int ovl_copy_up(struct dentry *dentry);
1889 +int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
1890 --- /dev/null
1891 +++ b/fs/overlayfs/readdir.c
1892 @@ -0,0 +1,567 @@
1893 +/*
1894 + *
1895 + * Copyright (C) 2011 Novell Inc.
1896 + *
1897 + * This program is free software; you can redistribute it and/or modify it
1898 + * under the terms of the GNU General Public License version 2 as published by
1899 + * the Free Software Foundation.
1900 + */
1901 +
1902 +#include <linux/fs.h>
1903 +#include <linux/slab.h>
1904 +#include <linux/namei.h>
1905 +#include <linux/file.h>
1906 +#include <linux/xattr.h>
1907 +#include <linux/rbtree.h>
1908 +#include <linux/security.h>
1909 +#include <linux/cred.h>
1910 +#include "overlayfs.h"
1911 +
1912 +struct ovl_cache_entry {
1913 + const char *name;
1914 + unsigned int len;
1915 + unsigned int type;
1916 + u64 ino;
1917 + bool is_whiteout;
1918 + struct list_head l_node;
1919 + struct rb_node node;
1920 +};
1921 +
1922 +struct ovl_readdir_data {
1923 + struct dir_context ctx;
1924 + bool is_merge;
1925 + struct rb_root *root;
1926 + struct list_head *list;
1927 + struct list_head *middle;
1928 + struct dentry *dir;
1929 + int count;
1930 + int err;
1931 +};
1932 +
1933 +struct ovl_dir_file {
1934 + bool is_real;
1935 + bool is_cached;
1936 + struct list_head cursor;
1937 + u64 cache_version;
1938 + struct list_head cache;
1939 + struct file *realfile;
1940 +};
1941 +
1942 +static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
1943 +{
1944 + return container_of(n, struct ovl_cache_entry, node);
1945 +}
1946 +
1947 +static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
1948 + const char *name, int len)
1949 +{
1950 + struct rb_node *node = root->rb_node;
1951 + int cmp;
1952 +
1953 + while (node) {
1954 + struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
1955 +
1956 + cmp = strncmp(name, p->name, len);
1957 + if (cmp > 0)
1958 + node = p->node.rb_right;
1959 + else if (cmp < 0 || len < p->len)
1960 + node = p->node.rb_left;
1961 + else
1962 + return p;
1963 + }
1964 +
1965 + return NULL;
1966 +}
1967 +
1968 +static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
1969 + u64 ino, unsigned int d_type)
1970 +{
1971 + struct ovl_cache_entry *p;
1972 +
1973 + p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
1974 + if (p) {
1975 + char *name_copy = (char *) (p + 1);
1976 + memcpy(name_copy, name, len);
1977 + name_copy[len] = '\0';
1978 + p->name = name_copy;
1979 + p->len = len;
1980 + p->type = d_type;
1981 + p->ino = ino;
1982 + p->is_whiteout = false;
1983 + }
1984 +
1985 + return p;
1986 +}
1987 +
1988 +static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
1989 + const char *name, int len, u64 ino,
1990 + unsigned int d_type)
1991 +{
1992 + struct rb_node **newp = &rdd->root->rb_node;
1993 + struct rb_node *parent = NULL;
1994 + struct ovl_cache_entry *p;
1995 +
1996 + while (*newp) {
1997 + int cmp;
1998 + struct ovl_cache_entry *tmp;
1999 +
2000 + parent = *newp;
2001 + tmp = ovl_cache_entry_from_node(*newp);
2002 + cmp = strncmp(name, tmp->name, len);
2003 + if (cmp > 0)
2004 + newp = &tmp->node.rb_right;
2005 + else if (cmp < 0 || len < tmp->len)
2006 + newp = &tmp->node.rb_left;
2007 + else
2008 + return 0;
2009 + }
2010 +
2011 + p = ovl_cache_entry_new(name, len, ino, d_type);
2012 + if (p == NULL)
2013 + return -ENOMEM;
2014 +
2015 + list_add_tail(&p->l_node, rdd->list);
2016 + rb_link_node(&p->node, parent, newp);
2017 + rb_insert_color(&p->node, rdd->root);
2018 +
2019 + return 0;
2020 +}
2021 +
2022 +static int ovl_fill_lower(struct ovl_readdir_data *rdd,
2023 + const char *name, int namelen,
2024 + loff_t offset, u64 ino, unsigned int d_type)
2025 +{
2026 + struct ovl_cache_entry *p;
2027 +
2028 + p = ovl_cache_entry_find(rdd->root, name, namelen);
2029 + if (p) {
2030 + list_move_tail(&p->l_node, rdd->middle);
2031 + } else {
2032 + p = ovl_cache_entry_new(name, namelen, ino, d_type);
2033 + if (p == NULL)
2034 + rdd->err = -ENOMEM;
2035 + else
2036 + list_add_tail(&p->l_node, rdd->middle);
2037 + }
2038 +
2039 + return rdd->err;
2040 +}
2041 +
2042 +static void ovl_cache_free(struct list_head *list)
2043 +{
2044 + struct ovl_cache_entry *p;
2045 + struct ovl_cache_entry *n;
2046 +
2047 + list_for_each_entry_safe(p, n, list, l_node)
2048 + kfree(p);
2049 +
2050 + INIT_LIST_HEAD(list);
2051 +}
2052 +
2053 +static int ovl_fill_merge(void *buf, const char *name, int namelen,
2054 + loff_t offset, u64 ino, unsigned int d_type)
2055 +{
2056 + struct ovl_readdir_data *rdd = buf;
2057 +
2058 + rdd->count++;
2059 + if (!rdd->is_merge)
2060 + return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
2061 + else
2062 + return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type);
2063 +}
2064 +
2065 +static inline int ovl_dir_read(struct path *realpath,
2066 + struct ovl_readdir_data *rdd)
2067 +{
2068 + struct file *realfile;
2069 + int err;
2070 +
2071 + realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
2072 + if (IS_ERR(realfile))
2073 + return PTR_ERR(realfile);
2074 +
2075 + rdd->ctx.pos = 0;
2076 + do {
2077 + rdd->count = 0;
2078 + rdd->err = 0;
2079 + err = iterate_dir(realfile, &rdd->ctx);
2080 + if (err >= 0)
2081 + err = rdd->err;
2082 + } while (!err && rdd->count);
2083 + fput(realfile);
2084 +
2085 + return 0;
2086 +}
2087 +
2088 +static void ovl_dir_reset(struct file *file)
2089 +{
2090 + struct ovl_dir_file *od = file->private_data;
2091 + enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
2092 +
2093 + if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
2094 + list_del_init(&od->cursor);
2095 + ovl_cache_free(&od->cache);
2096 + od->is_cached = false;
2097 + }
2098 + WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
2099 + if (od->is_real && type == OVL_PATH_MERGE) {
2100 + fput(od->realfile);
2101 + od->realfile = NULL;
2102 + od->is_real = false;
2103 + }
2104 +}
2105 +
2106 +static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
2107 +{
2108 + struct ovl_cache_entry *p;
2109 + struct dentry *dentry;
2110 + const struct cred *old_cred;
2111 + struct cred *override_cred;
2112 +
2113 + override_cred = prepare_creds();
2114 + if (!override_cred) {
2115 + ovl_cache_free(rdd->list);
2116 + return -ENOMEM;
2117 + }
2118 +
2119 + /*
2120 + * CAP_SYS_ADMIN for getxattr
2121 + * CAP_DAC_OVERRIDE for lookup
2122 + */
2123 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2124 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2125 + old_cred = override_creds(override_cred);
2126 +
2127 + mutex_lock(&rdd->dir->d_inode->i_mutex);
2128 + list_for_each_entry(p, rdd->list, l_node) {
2129 + if (p->type != DT_LNK)
2130 + continue;
2131 +
2132 + dentry = lookup_one_len(p->name, rdd->dir, p->len);
2133 + if (IS_ERR(dentry))
2134 + continue;
2135 +
2136 + p->is_whiteout = ovl_is_whiteout(dentry);
2137 + dput(dentry);
2138 + }
2139 + mutex_unlock(&rdd->dir->d_inode->i_mutex);
2140 +
2141 + revert_creds(old_cred);
2142 + put_cred(override_cred);
2143 +
2144 + return 0;
2145 +}
2146 +
2147 +static inline int ovl_dir_read_merged(struct path *upperpath,
2148 + struct path *lowerpath,
2149 + struct list_head *list)
2150 +{
2151 + int err;
2152 + struct rb_root root = RB_ROOT;
2153 + struct list_head middle;
2154 + struct ovl_readdir_data rdd = {
2155 + .ctx.actor = ovl_fill_merge,
2156 + .list = list,
2157 + .root = &root,
2158 + .is_merge = false,
2159 + };
2160 +
2161 + if (upperpath->dentry) {
2162 + rdd.dir = upperpath->dentry;
2163 + err = ovl_dir_read(upperpath, &rdd);
2164 + if (err)
2165 + goto out;
2166 +
2167 + err = ovl_dir_mark_whiteouts(&rdd);
2168 + if (err)
2169 + goto out;
2170 + }
2171 + /*
2172 + * Insert lowerpath entries before upperpath ones, this allows
2173 + * offsets to be reasonably constant
2174 + */
2175 + list_add(&middle, rdd.list);
2176 + rdd.middle = &middle;
2177 + rdd.is_merge = true;
2178 + err = ovl_dir_read(lowerpath, &rdd);
2179 + list_del(&middle);
2180 +out:
2181 + return err;
2182 +}
2183 +
2184 +static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
2185 +{
2186 + struct list_head *l;
2187 + loff_t off;
2188 +
2189 + l = od->cache.next;
2190 + for (off = 0; off < pos; off++) {
2191 + if (l == &od->cache)
2192 + break;
2193 + l = l->next;
2194 + }
2195 + list_move_tail(&od->cursor, l);
2196 +}
2197 +
2198 +static int ovl_iterate(struct file *file, struct dir_context *ctx)
2199 +{
2200 + struct ovl_dir_file *od = file->private_data;
2201 + int res;
2202 +
2203 + if (!ctx->pos)
2204 + ovl_dir_reset(file);
2205 +
2206 + if (od->is_real) {
2207 + res = iterate_dir(od->realfile, ctx);
2208 +
2209 + return res;
2210 + }
2211 +
2212 + if (!od->is_cached) {
2213 + struct path lowerpath;
2214 + struct path upperpath;
2215 +
2216 + ovl_path_lower(file->f_path.dentry, &lowerpath);
2217 + ovl_path_upper(file->f_path.dentry, &upperpath);
2218 +
2219 + res = ovl_dir_read_merged(&upperpath, &lowerpath, &od->cache);
2220 + if (res) {
2221 + ovl_cache_free(&od->cache);
2222 + return res;
2223 + }
2224 +
2225 + od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
2226 + od->is_cached = true;
2227 +
2228 + ovl_seek_cursor(od, ctx->pos);
2229 + }
2230 +
2231 + while (od->cursor.next != &od->cache) {
2232 + struct ovl_cache_entry *p;
2233 +
2234 + p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
2235 + if (!p->is_whiteout) {
2236 + if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
2237 + break;
2238 + }
2239 + ctx->pos++;
2240 + list_move(&od->cursor, &p->l_node);
2241 + }
2242 +
2243 + return 0;
2244 +}
2245 +
2246 +static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
2247 +{
2248 + loff_t res;
2249 + struct ovl_dir_file *od = file->private_data;
2250 +
2251 + mutex_lock(&file_inode(file)->i_mutex);
2252 + if (!file->f_pos)
2253 + ovl_dir_reset(file);
2254 +
2255 + if (od->is_real) {
2256 + res = vfs_llseek(od->realfile, offset, origin);
2257 + file->f_pos = od->realfile->f_pos;
2258 + } else {
2259 + res = -EINVAL;
2260 +
2261 + switch (origin) {
2262 + case SEEK_CUR:
2263 + offset += file->f_pos;
2264 + break;
2265 + case SEEK_SET:
2266 + break;
2267 + default:
2268 + goto out_unlock;
2269 + }
2270 + if (offset < 0)
2271 + goto out_unlock;
2272 +
2273 + if (offset != file->f_pos) {
2274 + file->f_pos = offset;
2275 + if (od->is_cached)
2276 + ovl_seek_cursor(od, offset);
2277 + }
2278 + res = offset;
2279 + }
2280 +out_unlock:
2281 + mutex_unlock(&file_inode(file)->i_mutex);
2282 +
2283 + return res;
2284 +}
2285 +
2286 +static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
2287 + int datasync)
2288 +{
2289 + struct ovl_dir_file *od = file->private_data;
2290 +
2291 + /* May need to reopen directory if it got copied up */
2292 + if (!od->realfile) {
2293 + struct path upperpath;
2294 +
2295 + ovl_path_upper(file->f_path.dentry, &upperpath);
2296 + od->realfile = ovl_path_open(&upperpath, O_RDONLY);
2297 + if (IS_ERR(od->realfile))
2298 + return PTR_ERR(od->realfile);
2299 + }
2300 +
2301 + return vfs_fsync_range(od->realfile, start, end, datasync);
2302 +}
2303 +
2304 +static int ovl_dir_release(struct inode *inode, struct file *file)
2305 +{
2306 + struct ovl_dir_file *od = file->private_data;
2307 +
2308 + list_del(&od->cursor);
2309 + ovl_cache_free(&od->cache);
2310 + if (od->realfile)
2311 + fput(od->realfile);
2312 + kfree(od);
2313 +
2314 + return 0;
2315 +}
2316 +
2317 +static int ovl_dir_open(struct inode *inode, struct file *file)
2318 +{
2319 + struct path realpath;
2320 + struct file *realfile;
2321 + struct ovl_dir_file *od;
2322 + enum ovl_path_type type;
2323 +
2324 + od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
2325 + if (!od)
2326 + return -ENOMEM;
2327 +
2328 + type = ovl_path_real(file->f_path.dentry, &realpath);
2329 + realfile = ovl_path_open(&realpath, file->f_flags);
2330 + if (IS_ERR(realfile)) {
2331 + kfree(od);
2332 + return PTR_ERR(realfile);
2333 + }
2334 + INIT_LIST_HEAD(&od->cache);
2335 + INIT_LIST_HEAD(&od->cursor);
2336 + od->is_cached = false;
2337 + od->realfile = realfile;
2338 + od->is_real = (type != OVL_PATH_MERGE);
2339 + file->private_data = od;
2340 +
2341 + return 0;
2342 +}
2343 +
2344 +const struct file_operations ovl_dir_operations = {
2345 + .read = generic_read_dir,
2346 + .open = ovl_dir_open,
2347 + .iterate = ovl_iterate,
2348 + .llseek = ovl_dir_llseek,
2349 + .fsync = ovl_dir_fsync,
2350 + .release = ovl_dir_release,
2351 +};
2352 +
2353 +static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
2354 +{
2355 + int err;
2356 + struct path lowerpath;
2357 + struct path upperpath;
2358 + struct ovl_cache_entry *p;
2359 +
2360 + ovl_path_upper(dentry, &upperpath);
2361 + ovl_path_lower(dentry, &lowerpath);
2362 +
2363 + err = ovl_dir_read_merged(&upperpath, &lowerpath, list);
2364 + if (err)
2365 + return err;
2366 +
2367 + err = 0;
2368 +
2369 + list_for_each_entry(p, list, l_node) {
2370 + if (p->is_whiteout)
2371 + continue;
2372 +
2373 + if (p->name[0] == '.') {
2374 + if (p->len == 1)
2375 + continue;
2376 + if (p->len == 2 && p->name[1] == '.')
2377 + continue;
2378 + }
2379 + err = -ENOTEMPTY;
2380 + break;
2381 + }
2382 +
2383 + return err;
2384 +}
2385 +
2386 +static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
2387 +{
2388 + struct path upperpath;
2389 + struct dentry *upperdir;
2390 + struct ovl_cache_entry *p;
2391 + const struct cred *old_cred;
2392 + struct cred *override_cred;
2393 + int err;
2394 +
2395 + ovl_path_upper(dir, &upperpath);
2396 + upperdir = upperpath.dentry;
2397 +
2398 + override_cred = prepare_creds();
2399 + if (!override_cred)
2400 + return -ENOMEM;
2401 +
2402 + /*
2403 + * CAP_DAC_OVERRIDE for lookup and unlink
2404 + * CAP_SYS_ADMIN for setxattr of "trusted" namespace
2405 + * CAP_FOWNER for unlink in sticky directory
2406 + */
2407 + cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2408 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2409 + cap_raise(override_cred->cap_effective, CAP_FOWNER);
2410 + old_cred = override_creds(override_cred);
2411 +
2412 + err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
2413 + if (err)
2414 + goto out_revert_creds;
2415 +
2416 + mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2417 + list_for_each_entry(p, list, l_node) {
2418 + struct dentry *dentry;
2419 + int ret;
2420 +
2421 + if (!p->is_whiteout)
2422 + continue;
2423 +
2424 + dentry = lookup_one_len(p->name, upperdir, p->len);
2425 + if (IS_ERR(dentry)) {
2426 + pr_warn(
2427 + "overlayfs: failed to lookup whiteout %.*s: %li\n",
2428 + p->len, p->name, PTR_ERR(dentry));
2429 + continue;
2430 + }
2431 + ret = vfs_unlink(upperdir->d_inode, dentry);
2432 + dput(dentry);
2433 + if (ret)
2434 + pr_warn(
2435 + "overlayfs: failed to unlink whiteout %.*s: %i\n",
2436 + p->len, p->name, ret);
2437 + }
2438 + mutex_unlock(&upperdir->d_inode->i_mutex);
2439 +
2440 +out_revert_creds:
2441 + revert_creds(old_cred);
2442 + put_cred(override_cred);
2443 +
2444 + return err;
2445 +}
2446 +
2447 +int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
2448 +{
2449 + int err;
2450 + LIST_HEAD(list);
2451 +
2452 + err = ovl_check_empty_dir(dentry, &list);
2453 + if (!err && type == OVL_PATH_MERGE)
2454 + err = ovl_remove_whiteouts(dentry, &list);
2455 +
2456 + ovl_cache_free(&list);
2457 +
2458 + return err;
2459 +}
2460 --- /dev/null
2461 +++ b/fs/overlayfs/super.c
2462 @@ -0,0 +1,685 @@
2463 +/*
2464 + *
2465 + * Copyright (C) 2011 Novell Inc.
2466 + *
2467 + * This program is free software; you can redistribute it and/or modify it
2468 + * under the terms of the GNU General Public License version 2 as published by
2469 + * the Free Software Foundation.
2470 + */
2471 +
2472 +#include <linux/fs.h>
2473 +#include <linux/namei.h>
2474 +#include <linux/xattr.h>
2475 +#include <linux/security.h>
2476 +#include <linux/mount.h>
2477 +#include <linux/slab.h>
2478 +#include <linux/parser.h>
2479 +#include <linux/module.h>
2480 +#include <linux/cred.h>
2481 +#include <linux/sched.h>
2482 +#include <linux/statfs.h>
2483 +#include <linux/seq_file.h>
2484 +#include "overlayfs.h"
2485 +
2486 +MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2487 +MODULE_DESCRIPTION("Overlay filesystem");
2488 +MODULE_LICENSE("GPL");
2489 +
2490 +#define OVERLAYFS_SUPER_MAGIC 0x794c764f
2491 +
2492 +struct ovl_config {
2493 + char *lowerdir;
2494 + char *upperdir;
2495 +};
2496 +
2497 +/* private information held for overlayfs's superblock */
2498 +struct ovl_fs {
2499 + struct vfsmount *upper_mnt;
2500 + struct vfsmount *lower_mnt;
2501 + long lower_namelen;
2502 + /* pathnames of lower and upper dirs, for show_options */
2503 + struct ovl_config config;
2504 +};
2505 +
2506 +/* private information held for every overlayfs dentry */
2507 +struct ovl_entry {
2508 + /*
2509 + * Keep "double reference" on upper dentries, so that
2510 + * d_delete() doesn't think it's OK to reset d_inode to NULL.
2511 + */
2512 + struct dentry *__upperdentry;
2513 + struct dentry *lowerdentry;
2514 + union {
2515 + struct {
2516 + u64 version;
2517 + bool opaque;
2518 + };
2519 + struct rcu_head rcu;
2520 + };
2521 +};
2522 +
2523 +const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
2524 +const char *ovl_opaque_xattr = "trusted.overlay.opaque";
2525 +
2526 +
2527 +enum ovl_path_type ovl_path_type(struct dentry *dentry)
2528 +{
2529 + struct ovl_entry *oe = dentry->d_fsdata;
2530 +
2531 + if (oe->__upperdentry) {
2532 + if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
2533 + return OVL_PATH_MERGE;
2534 + else
2535 + return OVL_PATH_UPPER;
2536 + } else {
2537 + return OVL_PATH_LOWER;
2538 + }
2539 +}
2540 +
2541 +static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
2542 +{
2543 + struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
2544 + smp_read_barrier_depends();
2545 + return upperdentry;
2546 +}
2547 +
2548 +void ovl_path_upper(struct dentry *dentry, struct path *path)
2549 +{
2550 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2551 + struct ovl_entry *oe = dentry->d_fsdata;
2552 +
2553 + path->mnt = ofs->upper_mnt;
2554 + path->dentry = ovl_upperdentry_dereference(oe);
2555 +}
2556 +
2557 +void ovl_path_lower(struct dentry *dentry, struct path *path)
2558 +{
2559 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2560 + struct ovl_entry *oe = dentry->d_fsdata;
2561 +
2562 + path->mnt = ofs->lower_mnt;
2563 + path->dentry = oe->lowerdentry;
2564 +}
2565 +
2566 +enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
2567 +{
2568 +
2569 + enum ovl_path_type type = ovl_path_type(dentry);
2570 +
2571 + if (type == OVL_PATH_LOWER)
2572 + ovl_path_lower(dentry, path);
2573 + else
2574 + ovl_path_upper(dentry, path);
2575 +
2576 + return type;
2577 +}
2578 +
2579 +struct dentry *ovl_dentry_upper(struct dentry *dentry)
2580 +{
2581 + struct ovl_entry *oe = dentry->d_fsdata;
2582 +
2583 + return ovl_upperdentry_dereference(oe);
2584 +}
2585 +
2586 +struct dentry *ovl_dentry_lower(struct dentry *dentry)
2587 +{
2588 + struct ovl_entry *oe = dentry->d_fsdata;
2589 +
2590 + return oe->lowerdentry;
2591 +}
2592 +
2593 +struct dentry *ovl_dentry_real(struct dentry *dentry)
2594 +{
2595 + struct ovl_entry *oe = dentry->d_fsdata;
2596 + struct dentry *realdentry;
2597 +
2598 + realdentry = ovl_upperdentry_dereference(oe);
2599 + if (!realdentry)
2600 + realdentry = oe->lowerdentry;
2601 +
2602 + return realdentry;
2603 +}
2604 +
2605 +struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
2606 +{
2607 + struct dentry *realdentry;
2608 +
2609 + realdentry = ovl_upperdentry_dereference(oe);
2610 + if (realdentry) {
2611 + *is_upper = true;
2612 + } else {
2613 + realdentry = oe->lowerdentry;
2614 + *is_upper = false;
2615 + }
2616 + return realdentry;
2617 +}
2618 +
2619 +bool ovl_dentry_is_opaque(struct dentry *dentry)
2620 +{
2621 + struct ovl_entry *oe = dentry->d_fsdata;
2622 + return oe->opaque;
2623 +}
2624 +
2625 +void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
2626 +{
2627 + struct ovl_entry *oe = dentry->d_fsdata;
2628 + oe->opaque = opaque;
2629 +}
2630 +
2631 +void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
2632 +{
2633 + struct ovl_entry *oe = dentry->d_fsdata;
2634 +
2635 + WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
2636 + WARN_ON(oe->__upperdentry);
2637 + BUG_ON(!upperdentry->d_inode);
2638 + smp_wmb();
2639 + oe->__upperdentry = dget(upperdentry);
2640 +}
2641 +
2642 +void ovl_dentry_version_inc(struct dentry *dentry)
2643 +{
2644 + struct ovl_entry *oe = dentry->d_fsdata;
2645 +
2646 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2647 + oe->version++;
2648 +}
2649 +
2650 +u64 ovl_dentry_version_get(struct dentry *dentry)
2651 +{
2652 + struct ovl_entry *oe = dentry->d_fsdata;
2653 +
2654 + WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2655 + return oe->version;
2656 +}
2657 +
2658 +bool ovl_is_whiteout(struct dentry *dentry)
2659 +{
2660 + int res;
2661 + char val;
2662 +
2663 + if (!dentry)
2664 + return false;
2665 + if (!dentry->d_inode)
2666 + return false;
2667 + if (!S_ISLNK(dentry->d_inode->i_mode))
2668 + return false;
2669 +
2670 + res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
2671 + if (res == 1 && val == 'y')
2672 + return true;
2673 +
2674 + return false;
2675 +}
2676 +
2677 +static bool ovl_is_opaquedir(struct dentry *dentry)
2678 +{
2679 + int res;
2680 + char val;
2681 +
2682 + if (!S_ISDIR(dentry->d_inode->i_mode))
2683 + return false;
2684 +
2685 + res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
2686 + if (res == 1 && val == 'y')
2687 + return true;
2688 +
2689 + return false;
2690 +}
2691 +
2692 +static void ovl_entry_free(struct rcu_head *head)
2693 +{
2694 + struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
2695 + kfree(oe);
2696 +}
2697 +
2698 +static void ovl_dentry_release(struct dentry *dentry)
2699 +{
2700 + struct ovl_entry *oe = dentry->d_fsdata;
2701 +
2702 + if (oe) {
2703 + dput(oe->__upperdentry);
2704 + dput(oe->__upperdentry);
2705 + dput(oe->lowerdentry);
2706 + call_rcu(&oe->rcu, ovl_entry_free);
2707 + }
2708 +}
2709 +
2710 +const struct dentry_operations ovl_dentry_operations = {
2711 + .d_release = ovl_dentry_release,
2712 +};
2713 +
2714 +static struct ovl_entry *ovl_alloc_entry(void)
2715 +{
2716 + return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
2717 +}
2718 +
2719 +static inline struct dentry *ovl_lookup_real(struct dentry *dir,
2720 + struct qstr *name)
2721 +{
2722 + struct dentry *dentry;
2723 +
2724 + mutex_lock(&dir->d_inode->i_mutex);
2725 + dentry = lookup_one_len(name->name, dir, name->len);
2726 + mutex_unlock(&dir->d_inode->i_mutex);
2727 +
2728 + if (IS_ERR(dentry)) {
2729 + if (PTR_ERR(dentry) == -ENOENT)
2730 + dentry = NULL;
2731 + } else if (!dentry->d_inode) {
2732 + dput(dentry);
2733 + dentry = NULL;
2734 + }
2735 + return dentry;
2736 +}
2737 +
2738 +static int ovl_do_lookup(struct dentry *dentry)
2739 +{
2740 + struct ovl_entry *oe;
2741 + struct dentry *upperdir;
2742 + struct dentry *lowerdir;
2743 + struct dentry *upperdentry = NULL;
2744 + struct dentry *lowerdentry = NULL;
2745 + struct inode *inode = NULL;
2746 + int err;
2747 +
2748 + err = -ENOMEM;
2749 + oe = ovl_alloc_entry();
2750 + if (!oe)
2751 + goto out;
2752 +
2753 + upperdir = ovl_dentry_upper(dentry->d_parent);
2754 + lowerdir = ovl_dentry_lower(dentry->d_parent);
2755 +
2756 + if (upperdir) {
2757 + upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
2758 + err = PTR_ERR(upperdentry);
2759 + if (IS_ERR(upperdentry))
2760 + goto out_put_dir;
2761 +
2762 + if (lowerdir && upperdentry &&
2763 + (S_ISLNK(upperdentry->d_inode->i_mode) ||
2764 + S_ISDIR(upperdentry->d_inode->i_mode))) {
2765 + const struct cred *old_cred;
2766 + struct cred *override_cred;
2767 +
2768 + err = -ENOMEM;
2769 + override_cred = prepare_creds();
2770 + if (!override_cred)
2771 + goto out_dput_upper;
2772 +
2773 + /* CAP_SYS_ADMIN needed for getxattr */
2774 + cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2775 + old_cred = override_creds(override_cred);
2776 +
2777 + if (ovl_is_opaquedir(upperdentry)) {
2778 + oe->opaque = true;
2779 + } else if (ovl_is_whiteout(upperdentry)) {
2780 + dput(upperdentry);
2781 + upperdentry = NULL;
2782 + oe->opaque = true;
2783 + }
2784 + revert_creds(old_cred);
2785 + put_cred(override_cred);
2786 + }
2787 + }
2788 + if (lowerdir && !oe->opaque) {
2789 + lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
2790 + err = PTR_ERR(lowerdentry);
2791 + if (IS_ERR(lowerdentry))
2792 + goto out_dput_upper;
2793 + }
2794 +
2795 + if (lowerdentry && upperdentry &&
2796 + (!S_ISDIR(upperdentry->d_inode->i_mode) ||
2797 + !S_ISDIR(lowerdentry->d_inode->i_mode))) {
2798 + dput(lowerdentry);
2799 + lowerdentry = NULL;
2800 + oe->opaque = true;
2801 + }
2802 +
2803 + if (lowerdentry || upperdentry) {
2804 + struct dentry *realdentry;
2805 +
2806 + realdentry = upperdentry ? upperdentry : lowerdentry;
2807 + err = -ENOMEM;
2808 + inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
2809 + oe);
2810 + if (!inode)
2811 + goto out_dput;
2812 + ovl_copyattr(realdentry->d_inode, inode);
2813 + }
2814 +
2815 + if (upperdentry)
2816 + oe->__upperdentry = dget(upperdentry);
2817 +
2818 + if (lowerdentry)
2819 + oe->lowerdentry = lowerdentry;
2820 +
2821 + dentry->d_fsdata = oe;
2822 + dentry->d_op = &ovl_dentry_operations;
2823 + d_add(dentry, inode);
2824 +
2825 + return 0;
2826 +
2827 +out_dput:
2828 + dput(lowerdentry);
2829 +out_dput_upper:
2830 + dput(upperdentry);
2831 +out_put_dir:
2832 + kfree(oe);
2833 +out:
2834 + return err;
2835 +}
2836 +
2837 +struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
2838 + unsigned int flags)
2839 +{
2840 + int err = ovl_do_lookup(dentry);
2841 +
2842 + if (err)
2843 + return ERR_PTR(err);
2844 +
2845 + return NULL;
2846 +}
2847 +
2848 +struct file *ovl_path_open(struct path *path, int flags)
2849 +{
2850 + return dentry_open(path, flags, current_cred());
2851 +}
2852 +
2853 +static void ovl_put_super(struct super_block *sb)
2854 +{
2855 + struct ovl_fs *ufs = sb->s_fs_info;
2856 +
2857 + if (!(sb->s_flags & MS_RDONLY))
2858 + mnt_drop_write(ufs->upper_mnt);
2859 +
2860 + mntput(ufs->upper_mnt);
2861 + mntput(ufs->lower_mnt);
2862 +
2863 + kfree(ufs->config.lowerdir);
2864 + kfree(ufs->config.upperdir);
2865 + kfree(ufs);
2866 +}
2867 +
2868 +static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2869 +{
2870 + int flags = *flagsp;
2871 + struct ovl_fs *ufs = sb->s_fs_info;
2872 +
2873 + /* When remounting rw or ro, we need to adjust the write access to the
2874 + * upper fs.
2875 + */
2876 + if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2877 + /* No change to readonly status */
2878 + return 0;
2879 +
2880 + if (flags & MS_RDONLY) {
2881 + mnt_drop_write(ufs->upper_mnt);
2882 + return 0;
2883 + } else
2884 + return mnt_want_write(ufs->upper_mnt);
2885 +}
2886 +
2887 +/**
2888 + * ovl_statfs
2889 + * @sb: The overlayfs super block
2890 + * @buf: The struct kstatfs to fill in with stats
2891 + *
2892 + * Get the filesystem statistics. As writes always target the upper layer
2893 + * filesystem pass the statfs to the same filesystem.
2894 + */
2895 +static int ovl_statfs(struct dentry *dentry, struct kstatfs *buf)
2896 +{
2897 + struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2898 + struct dentry *root_dentry = dentry->d_sb->s_root;
2899 + struct path path;
2900 + int err;
2901 +
2902 + ovl_path_upper(root_dentry, &path);
2903 +
2904 + err = vfs_statfs(&path, buf);
2905 + if (!err) {
2906 + buf->f_namelen = max(buf->f_namelen, ofs->lower_namelen);
2907 + buf->f_type = OVERLAYFS_SUPER_MAGIC;
2908 + }
2909 +
2910 + return err;
2911 +}
2912 +
2913 +/**
2914 + * ovl_show_options
2915 + *
2916 + * Prints the mount options for a given superblock.
2917 + * Returns zero; does not fail.
2918 + */
2919 +static int ovl_show_options(struct seq_file *m, struct dentry *dentry)
2920 +{
2921 + struct super_block *sb = dentry->d_sb;
2922 + struct ovl_fs *ufs = sb->s_fs_info;
2923 +
2924 + seq_printf(m, ",lowerdir=%s", ufs->config.lowerdir);
2925 + seq_printf(m, ",upperdir=%s", ufs->config.upperdir);
2926 + return 0;
2927 +}
2928 +
2929 +static const struct super_operations ovl_super_operations = {
2930 + .put_super = ovl_put_super,
2931 + .remount_fs = ovl_remount_fs,
2932 + .statfs = ovl_statfs,
2933 + .show_options = ovl_show_options,
2934 +};
2935 +
2936 +enum {
2937 + OPT_LOWERDIR,
2938 + OPT_UPPERDIR,
2939 + OPT_ERR,
2940 +};
2941 +
2942 +static const match_table_t ovl_tokens = {
2943 + {OPT_LOWERDIR, "lowerdir=%s"},
2944 + {OPT_UPPERDIR, "upperdir=%s"},
2945 + {OPT_ERR, NULL}
2946 +};
2947 +
2948 +static int ovl_parse_opt(char *opt, struct ovl_config *config)
2949 +{
2950 + char *p;
2951 +
2952 + config->upperdir = NULL;
2953 + config->lowerdir = NULL;
2954 +
2955 + while ((p = strsep(&opt, ",")) != NULL) {
2956 + int token;
2957 + substring_t args[MAX_OPT_ARGS];
2958 +
2959 + if (!*p)
2960 + continue;
2961 +
2962 + token = match_token(p, ovl_tokens, args);
2963 + switch (token) {
2964 + case OPT_UPPERDIR:
2965 + kfree(config->upperdir);
2966 + config->upperdir = match_strdup(&args[0]);
2967 + if (!config->upperdir)
2968 + return -ENOMEM;
2969 + break;
2970 +
2971 + case OPT_LOWERDIR:
2972 + kfree(config->lowerdir);
2973 + config->lowerdir = match_strdup(&args[0]);
2974 + if (!config->lowerdir)
2975 + return -ENOMEM;
2976 + break;
2977 +
2978 + default:
2979 + return -EINVAL;
2980 + }
2981 + }
2982 + return 0;
2983 +}
2984 +
2985 +static int ovl_fill_super(struct super_block *sb, void *data, int silent)
2986 +{
2987 + struct path lowerpath;
2988 + struct path upperpath;
2989 + struct inode *root_inode;
2990 + struct dentry *root_dentry;
2991 + struct ovl_entry *oe;
2992 + struct ovl_fs *ufs;
2993 + struct kstatfs statfs;
2994 + int err;
2995 +
2996 + err = -ENOMEM;
2997 + ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
2998 + if (!ufs)
2999 + goto out;
3000 +
3001 + err = ovl_parse_opt((char *) data, &ufs->config);
3002 + if (err)
3003 + goto out_free_ufs;
3004 +
3005 + err = -EINVAL;
3006 + if (!ufs->config.upperdir || !ufs->config.lowerdir) {
3007 + pr_err("overlayfs: missing upperdir or lowerdir\n");
3008 + goto out_free_config;
3009 + }
3010 +
3011 + oe = ovl_alloc_entry();
3012 + if (oe == NULL)
3013 + goto out_free_config;
3014 +
3015 + err = kern_path(ufs->config.upperdir, LOOKUP_FOLLOW, &upperpath);
3016 + if (err)
3017 + goto out_free_oe;
3018 +
3019 + err = kern_path(ufs->config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
3020 + if (err)
3021 + goto out_put_upperpath;
3022 +
3023 + err = -ENOTDIR;
3024 + if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
3025 + !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
3026 + goto out_put_lowerpath;
3027 +
3028 + err = vfs_statfs(&lowerpath, &statfs);
3029 + if (err) {
3030 + pr_err("overlayfs: statfs failed on lowerpath\n");
3031 + goto out_put_lowerpath;
3032 + }
3033 + ufs->lower_namelen = statfs.f_namelen;
3034 +
3035 + sb->s_stack_depth = max(upperpath.mnt->mnt_sb->s_stack_depth,
3036 + lowerpath.mnt->mnt_sb->s_stack_depth) + 1;
3037 +
3038 + err = -EINVAL;
3039 + if (sb->s_stack_depth > FILESYSTEM_MAX_STACK_DEPTH) {
3040 + pr_err("overlayfs: maximum fs stacking depth exceeded\n");
3041 + goto out_put_lowerpath;
3042 + }
3043 +
3044 +
3045 + ufs->upper_mnt = clone_private_mount(&upperpath);
3046 + err = PTR_ERR(ufs->upper_mnt);
3047 + if (IS_ERR(ufs->upper_mnt)) {
3048 + pr_err("overlayfs: failed to clone upperpath\n");
3049 + goto out_put_lowerpath;
3050 + }
3051 +
3052 + ufs->lower_mnt = clone_private_mount(&lowerpath);
3053 + err = PTR_ERR(ufs->lower_mnt);
3054 + if (IS_ERR(ufs->lower_mnt)) {
3055 + pr_err("overlayfs: failed to clone lowerpath\n");
3056 + goto out_put_upper_mnt;
3057 + }
3058 +
3059 + /*
3060 + * Make lower_mnt R/O. That way fchmod/fchown on lower file
3061 + * will fail instead of modifying lower fs.
3062 + */
3063 + ufs->lower_mnt->mnt_flags |= MNT_READONLY;
3064 +
3065 + /* If the upper fs is r/o, we mark overlayfs r/o too */
3066 + if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
3067 + sb->s_flags |= MS_RDONLY;
3068 +
3069 + if (!(sb->s_flags & MS_RDONLY)) {
3070 + err = mnt_want_write(ufs->upper_mnt);
3071 + if (err)
3072 + goto out_put_lower_mnt;
3073 + }
3074 +
3075 + err = -ENOMEM;
3076 + root_inode = ovl_new_inode(sb, S_IFDIR, oe);
3077 + if (!root_inode)
3078 + goto out_drop_write;
3079 +
3080 + root_dentry = d_make_root(root_inode);
3081 + if (!root_dentry)
3082 + goto out_drop_write;
3083 +
3084 + mntput(upperpath.mnt);
3085 + mntput(lowerpath.mnt);
3086 +
3087 + oe->__upperdentry = dget(upperpath.dentry);
3088 + oe->lowerdentry = lowerpath.dentry;
3089 +
3090 + root_dentry->d_fsdata = oe;
3091 + root_dentry->d_op = &ovl_dentry_operations;
3092 +
3093 + sb->s_magic = OVERLAYFS_SUPER_MAGIC;
3094 + sb->s_op = &ovl_super_operations;
3095 + sb->s_root = root_dentry;
3096 + sb->s_fs_info = ufs;
3097 +
3098 + return 0;
3099 +
3100 +out_drop_write:
3101 + if (!(sb->s_flags & MS_RDONLY))
3102 + mnt_drop_write(ufs->upper_mnt);
3103 +out_put_lower_mnt:
3104 + mntput(ufs->lower_mnt);
3105 +out_put_upper_mnt:
3106 + mntput(ufs->upper_mnt);
3107 +out_put_lowerpath:
3108 + path_put(&lowerpath);
3109 +out_put_upperpath:
3110 + path_put(&upperpath);
3111 +out_free_oe:
3112 + kfree(oe);
3113 +out_free_config:
3114 + kfree(ufs->config.lowerdir);
3115 + kfree(ufs->config.upperdir);
3116 +out_free_ufs:
3117 + kfree(ufs);
3118 +out:
3119 + return err;
3120 +}
3121 +
3122 +static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
3123 + const char *dev_name, void *raw_data)
3124 +{
3125 + return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
3126 +}
3127 +
3128 +static struct file_system_type ovl_fs_type = {
3129 + .owner = THIS_MODULE,
3130 + .name = "overlayfs",
3131 + .mount = ovl_mount,
3132 + .kill_sb = kill_anon_super,
3133 +};
3134 +MODULE_ALIAS_FS("overlayfs");
3135 +
3136 +static int __init ovl_init(void)
3137 +{
3138 + return register_filesystem(&ovl_fs_type);
3139 +}
3140 +
3141 +static void __exit ovl_exit(void)
3142 +{
3143 + unregister_filesystem(&ovl_fs_type);
3144 +}
3145 +
3146 +module_init(ovl_init);
3147 +module_exit(ovl_exit);
3148 --- a/fs/splice.c
3149 +++ b/fs/splice.c
3150 @@ -1309,6 +1309,7 @@ long do_splice_direct(struct file *in, l
3151
3152 return ret;
3153 }
3154 +EXPORT_SYMBOL(do_splice_direct);
3155
3156 static int splice_pipe_to_pipe(struct pipe_inode_info *ipipe,
3157 struct pipe_inode_info *opipe,
3158 --- a/include/linux/fs.h
3159 +++ b/include/linux/fs.h
3160 @@ -246,6 +246,12 @@ struct iattr {
3161 */
3162 #include <linux/quota.h>
3163
3164 +/*
3165 + * Maximum number of layers of fs stack. Needs to be limited to
3166 + * prevent kernel stack overflow
3167 + */
3168 +#define FILESYSTEM_MAX_STACK_DEPTH 2
3169 +
3170 /**
3171 * enum positive_aop_returns - aop return codes with specific semantics
3172 *
3173 @@ -1330,6 +1336,11 @@ struct super_block {
3174 */
3175 struct list_lru s_dentry_lru ____cacheline_aligned_in_smp;
3176 struct list_lru s_inode_lru ____cacheline_aligned_in_smp;
3177 +
3178 + /*
3179 + * Indicates how deep in a filesystem stack this SB is
3180 + */
3181 + int s_stack_depth;
3182 };
3183
3184 extern struct timespec current_fs_time(struct super_block *sb);
3185 @@ -1585,6 +1596,7 @@ struct inode_operations {
3186 struct file *, unsigned open_flag,
3187 umode_t create_mode, int *opened);
3188 int (*tmpfile) (struct inode *, struct dentry *, umode_t);
3189 + int (*dentry_open)(struct dentry *, struct file *, const struct cred *);
3190 } ____cacheline_aligned;
3191
3192 ssize_t rw_copy_check_uvector(int type, const struct iovec __user * uvector,
3193 @@ -2019,6 +2031,7 @@ extern struct file *file_open_name(struc
3194 extern struct file *filp_open(const char *, int, umode_t);
3195 extern struct file *file_open_root(struct dentry *, struct vfsmount *,
3196 const char *, int);
3197 +extern int vfs_open(const struct path *, struct file *, const struct cred *);
3198 extern struct file * dentry_open(const struct path *, int, const struct cred *);
3199 extern int filp_close(struct file *, fl_owner_t id);
3200
3201 @@ -2225,6 +2238,7 @@ extern sector_t bmap(struct inode *, sec
3202 #endif
3203 extern int notify_change(struct dentry *, struct iattr *);
3204 extern int inode_permission(struct inode *, int);
3205 +extern int __inode_permission(struct inode *, int);
3206 extern int generic_permission(struct inode *, int);
3207
3208 static inline bool execute_ok(struct inode *inode)
3209 @@ -2430,6 +2444,9 @@ extern ssize_t generic_file_splice_write
3210 struct file *, loff_t *, size_t, unsigned int);
3211 extern ssize_t generic_splice_sendpage(struct pipe_inode_info *pipe,
3212 struct file *out, loff_t *, size_t len, unsigned int flags);
3213 +extern long do_splice_direct(struct file *in, loff_t *ppos, struct file *out,
3214 + loff_t *opos, size_t len, unsigned int flags);
3215 +
3216
3217 extern void
3218 file_ra_state_init(struct file_ra_state *ra, struct address_space *mapping);
3219 --- a/include/linux/mount.h
3220 +++ b/include/linux/mount.h
3221 @@ -69,6 +69,9 @@ extern void mnt_pin(struct vfsmount *mnt
3222 extern void mnt_unpin(struct vfsmount *mnt);
3223 extern int __mnt_is_readonly(struct vfsmount *mnt);
3224
3225 +struct path;
3226 +extern struct vfsmount *clone_private_mount(struct path *path);
3227 +
3228 struct file_system_type;
3229 extern struct vfsmount *vfs_kern_mount(struct file_system_type *type,
3230 int flags, const char *name,