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