]> git.pld-linux.org Git - packages/kernel.git/blame - ovl05-overlay-filesystem.patch
- fix vserver patch for 3.14.5
[packages/kernel.git] / ovl05-overlay-filesystem.patch
CommitLineData
68184a5a
JR
1From 7dd7dc8ba5f16e494206477d3880b7ed09aff118 Mon Sep 17 00:00:00 2001
2From: Miklos Szeredi <mszeredi@suse.cz>
3Date: Thu, 30 Aug 2012 16:13:49 +0200
4Subject: [PATCH 04/13] overlay filesystem
5Patch-mainline: not yet
6
7Overlayfs allows one, usually read-write, directory tree to be
8overlaid onto another, read-only directory tree. All modifications
9go to the upper, writable layer.
10
11This type of mechanism is most often used for live CDs but there's a
12wide variety of other uses.
13
14The implementation differs from other "union filesystem"
15implementations in that after a file is opened all operations go
16directly to the underlying, lower or upper, filesystems. This
17simplifies the implementation and allows native performance in these
18cases.
19
20The dentry tree is duplicated from the underlying filesystems, this
21enables fast cached lookups without adding special support into the
22VFS. This uses slightly more memory than union mounts, but dentries
23are relatively small.
24
25Currently inodes are duplicated as well, but it is a possible
26optimization to share inodes for non-directories.
27
28Opening non directories results in the open forwarded to the
29underlying filesystem. This makes the behavior very similar to union
30mounts (with the same limitations vs. fchmod/fchown on O_RDONLY file
31descriptors).
32
c06a8ce3 33Usage:
68184a5a
JR
34
35 mount -t overlay -olowerdir=/lower,upperdir=/upper overlay /mnt
36
c06a8ce3 37Supported:
68184a5a
JR
38
39 - all operations
40
c06a8ce3 41Missing:
68184a5a
JR
42
43 - Currently a crash in the middle of copy-up, rename, unlink, rmdir or create
44 over a whiteout may result in filesystem corruption on the overlay level.
45 IOW these operations need to become atomic or at least the corruption needs
46 to be detected.
47
48
49The following cotributions have been folded into this patch:
50
51Neil Brown <neilb@suse.de>:
52 - minimal remount support
53 - use correct seek function for directories
54 - initialise is_real before use
55 - rename ovl_fill_cache to ovl_dir_read
56
57Felix Fietkau <nbd@openwrt.org>:
58 - fix a deadlock in ovl_dir_read_merged
59 - fix a deadlock in ovl_remove_whiteouts
60
61Erez Zadok <ezk@fsl.cs.sunysb.edu>
62 - fix cleanup after WARN_ON
63
64Sedat Dilek <sedat.dilek@googlemail.com>
65 - fix up permission to confirm to new API
66
c06a8ce3
AM
67Robin Dong <hao.bigrat@gmail.com>
68 - fix possible leak in ovl_new_inode
69 - create new inode in ovl_link
70
71Andy Whitcroft <apw@canonical.com>
72 - switch to __inode_permission()
73 - copy up i_uid/i_gid from the underlying inode
74
68184a5a
JR
75Also thanks to the following people for testing and reporting bugs:
76
77 Jordi Pujol <jordipujolp@gmail.com>
78 Andy Whitcroft <apw@canonical.com>
79 Michal Suchanek <hramrach@centrum.cz>
80 Felix Fietkau <nbd@openwrt.org>
81 Erez Zadok <ezk@fsl.cs.sunysb.edu>
82 Randy Dunlap <rdunlap@xenotime.net>
83
84Signed-off-by: Miklos Szeredi <mszeredi@suse.cz>
85---
86 fs/Kconfig | 1
87 fs/Makefile | 1
c06a8ce3 88 fs/overlayfs/Kconfig | 10
68184a5a 89 fs/overlayfs/Makefile | 7
392086de 90 fs/overlayfs/copy_up.c | 388 +++++++++++++++++++++++++++++
c06a8ce3
AM
91 fs/overlayfs/dir.c | 605 ++++++++++++++++++++++++++++++++++++++++++++++
92 fs/overlayfs/inode.c | 372 ++++++++++++++++++++++++++++
93 fs/overlayfs/overlayfs.h | 70 +++++
392086de 94 fs/overlayfs/readdir.c | 567 +++++++++++++++++++++++++++++++++++++++++++
c06a8ce3 95 fs/overlayfs/super.c | 612 +++++++++++++++++++++++++++++++++++++++++++++++
392086de 96 10 files changed, 2633 insertions(+)
68184a5a 97
514e5dae
AM
98--- a/fs/Kconfig
99+++ b/fs/Kconfig
392086de 100@@ -67,6 +67,7 @@ source "fs/quota/Kconfig"
68184a5a
JR
101
102 source "fs/autofs4/Kconfig"
103 source "fs/fuse/Kconfig"
104+source "fs/overlayfs/Kconfig"
105
514e5dae
AM
106 config GENERIC_ACL
107 bool
108--- a/fs/Makefile
109+++ b/fs/Makefile
392086de 110@@ -105,6 +105,7 @@ obj-$(CONFIG_QNX6FS_FS) += qnx6/
68184a5a
JR
111 obj-$(CONFIG_AUTOFS4_FS) += autofs4/
112 obj-$(CONFIG_ADFS_FS) += adfs/
113 obj-$(CONFIG_FUSE_FS) += fuse/
114+obj-$(CONFIG_OVERLAYFS_FS) += overlayfs/
115 obj-$(CONFIG_UDF_FS) += udf/
116 obj-$(CONFIG_SUN_OPENPROMFS) += openpromfs/
117 obj-$(CONFIG_OMFS_FS) += omfs/
514e5dae
AM
118--- /dev/null
119+++ b/fs/overlayfs/Kconfig
c06a8ce3 120@@ -0,0 +1,10 @@
68184a5a
JR
121+config OVERLAYFS_FS
122+ tristate "Overlay filesystem support"
123+ help
c06a8ce3
AM
124+ An overlay filesystem combines two filesystems - an 'upper' filesystem
125+ and a 'lower' filesystem. When a name exists in both filesystems, the
126+ object in the 'upper' filesystem is visible while the object in the
127+ 'lower' filesystem is either hidden or, in the case of directories,
128+ merged with the 'upper' object.
129+
130+ For more information see Documentation/filesystems/overlayfs.txt
514e5dae
AM
131--- /dev/null
132+++ b/fs/overlayfs/Makefile
68184a5a
JR
133@@ -0,0 +1,7 @@
134+#
135+# Makefile for the overlay filesystem.
136+#
137+
138+obj-$(CONFIG_OVERLAYFS_FS) += overlayfs.o
139+
140+overlayfs-objs := super.o inode.o dir.o readdir.o copy_up.o
514e5dae
AM
141--- /dev/null
142+++ b/fs/overlayfs/copy_up.c
392086de 143@@ -0,0 +1,388 @@
68184a5a
JR
144+/*
145+ *
146+ * Copyright (C) 2011 Novell Inc.
147+ *
148+ * This program is free software; you can redistribute it and/or modify it
149+ * under the terms of the GNU General Public License version 2 as published by
150+ * the Free Software Foundation.
151+ */
152+
153+#include <linux/fs.h>
154+#include <linux/slab.h>
155+#include <linux/file.h>
156+#include <linux/splice.h>
157+#include <linux/xattr.h>
158+#include <linux/security.h>
159+#include <linux/uaccess.h>
160+#include <linux/sched.h>
161+#include "overlayfs.h"
162+
163+#define OVL_COPY_UP_CHUNK_SIZE (1 << 20)
164+
165+static int ovl_copy_up_xattr(struct dentry *old, struct dentry *new)
166+{
167+ ssize_t list_size, size;
168+ char *buf, *name, *value;
169+ int error;
170+
171+ if (!old->d_inode->i_op->getxattr ||
172+ !new->d_inode->i_op->getxattr)
173+ return 0;
174+
175+ list_size = vfs_listxattr(old, NULL, 0);
176+ if (list_size <= 0) {
177+ if (list_size == -EOPNOTSUPP)
178+ return 0;
179+ return list_size;
180+ }
181+
182+ buf = kzalloc(list_size, GFP_KERNEL);
183+ if (!buf)
184+ return -ENOMEM;
185+
186+ error = -ENOMEM;
187+ value = kmalloc(XATTR_SIZE_MAX, GFP_KERNEL);
188+ if (!value)
189+ goto out;
190+
191+ list_size = vfs_listxattr(old, buf, list_size);
192+ if (list_size <= 0) {
193+ error = list_size;
194+ goto out_free_value;
195+ }
196+
197+ for (name = buf; name < (buf + list_size); name += strlen(name) + 1) {
198+ size = vfs_getxattr(old, name, value, XATTR_SIZE_MAX);
199+ if (size <= 0) {
200+ error = size;
201+ goto out_free_value;
202+ }
203+ error = vfs_setxattr(new, name, value, size, 0);
204+ if (error)
205+ goto out_free_value;
206+ }
207+
208+out_free_value:
209+ kfree(value);
210+out:
211+ kfree(buf);
212+ return error;
213+}
214+
215+static int ovl_copy_up_data(struct path *old, struct path *new, loff_t len)
216+{
217+ struct file *old_file;
218+ struct file *new_file;
c06a8ce3
AM
219+ loff_t old_pos = 0;
220+ loff_t new_pos = 0;
68184a5a
JR
221+ int error = 0;
222+
223+ if (len == 0)
224+ return 0;
225+
226+ old_file = ovl_path_open(old, O_RDONLY);
227+ if (IS_ERR(old_file))
228+ return PTR_ERR(old_file);
229+
230+ new_file = ovl_path_open(new, O_WRONLY);
231+ if (IS_ERR(new_file)) {
232+ error = PTR_ERR(new_file);
233+ goto out_fput;
234+ }
235+
236+ /* FIXME: copy up sparse files efficiently */
237+ while (len) {
68184a5a
JR
238+ size_t this_len = OVL_COPY_UP_CHUNK_SIZE;
239+ long bytes;
240+
241+ if (len < this_len)
242+ this_len = len;
243+
244+ if (signal_pending_state(TASK_KILLABLE, current)) {
245+ error = -EINTR;
246+ break;
247+ }
248+
c06a8ce3
AM
249+ bytes = do_splice_direct(old_file, &old_pos,
250+ new_file, &new_pos,
251+ this_len, SPLICE_F_MOVE);
68184a5a
JR
252+ if (bytes <= 0) {
253+ error = bytes;
254+ break;
255+ }
392086de 256+ WARN_ON(old_pos != new_pos);
68184a5a
JR
257+
258+ len -= bytes;
259+ }
260+
261+ fput(new_file);
262+out_fput:
263+ fput(old_file);
264+ return error;
265+}
266+
267+static char *ovl_read_symlink(struct dentry *realdentry)
268+{
269+ int res;
270+ char *buf;
271+ struct inode *inode = realdentry->d_inode;
272+ mm_segment_t old_fs;
273+
274+ res = -EINVAL;
275+ if (!inode->i_op->readlink)
276+ goto err;
277+
278+ res = -ENOMEM;
279+ buf = (char *) __get_free_page(GFP_KERNEL);
280+ if (!buf)
281+ goto err;
282+
283+ old_fs = get_fs();
284+ set_fs(get_ds());
285+ /* The cast to a user pointer is valid due to the set_fs() */
286+ res = inode->i_op->readlink(realdentry,
287+ (char __user *)buf, PAGE_SIZE - 1);
288+ set_fs(old_fs);
289+ if (res < 0) {
290+ free_page((unsigned long) buf);
291+ goto err;
292+ }
293+ buf[res] = '\0';
294+
295+ return buf;
296+
297+err:
298+ return ERR_PTR(res);
299+}
300+
301+static int ovl_set_timestamps(struct dentry *upperdentry, struct kstat *stat)
302+{
303+ struct iattr attr = {
304+ .ia_valid =
305+ ATTR_ATIME | ATTR_MTIME | ATTR_ATIME_SET | ATTR_MTIME_SET,
306+ .ia_atime = stat->atime,
307+ .ia_mtime = stat->mtime,
308+ };
309+
a25ec4e9 310+ return notify_change(upperdentry, &attr, NULL);
68184a5a
JR
311+}
312+
313+static int ovl_set_mode(struct dentry *upperdentry, umode_t mode)
314+{
315+ struct iattr attr = {
316+ .ia_valid = ATTR_MODE,
317+ .ia_mode = mode,
318+ };
319+
a25ec4e9 320+ return notify_change(upperdentry, &attr, NULL);
68184a5a
JR
321+}
322+
323+static int ovl_copy_up_locked(struct dentry *upperdir, struct dentry *dentry,
324+ struct path *lowerpath, struct kstat *stat,
325+ const char *link)
326+{
327+ int err;
328+ struct path newpath;
329+ umode_t mode = stat->mode;
330+
331+ /* Can't properly set mode on creation because of the umask */
332+ stat->mode &= S_IFMT;
333+
334+ ovl_path_upper(dentry, &newpath);
335+ WARN_ON(newpath.dentry);
336+ newpath.dentry = ovl_upper_create(upperdir, dentry, stat, link);
337+ if (IS_ERR(newpath.dentry))
338+ return PTR_ERR(newpath.dentry);
339+
340+ if (S_ISREG(stat->mode)) {
341+ err = ovl_copy_up_data(lowerpath, &newpath, stat->size);
342+ if (err)
343+ goto err_remove;
344+ }
345+
346+ err = ovl_copy_up_xattr(lowerpath->dentry, newpath.dentry);
347+ if (err)
348+ goto err_remove;
349+
350+ mutex_lock(&newpath.dentry->d_inode->i_mutex);
351+ if (!S_ISLNK(stat->mode))
352+ err = ovl_set_mode(newpath.dentry, mode);
353+ if (!err)
354+ err = ovl_set_timestamps(newpath.dentry, stat);
355+ mutex_unlock(&newpath.dentry->d_inode->i_mutex);
356+ if (err)
357+ goto err_remove;
358+
359+ ovl_dentry_update(dentry, newpath.dentry);
360+
361+ /*
362+ * Easiest way to get rid of the lower dentry reference is to
363+ * drop this dentry. This is neither needed nor possible for
364+ * directories.
365+ */
366+ if (!S_ISDIR(stat->mode))
367+ d_drop(dentry);
368+
369+ return 0;
370+
371+err_remove:
372+ if (S_ISDIR(stat->mode))
373+ vfs_rmdir(upperdir->d_inode, newpath.dentry);
374+ else
a25ec4e9 375+ vfs_unlink(upperdir->d_inode, newpath.dentry, NULL);
68184a5a
JR
376+
377+ dput(newpath.dentry);
378+
379+ return err;
380+}
381+
382+/*
383+ * Copy up a single dentry
384+ *
385+ * Directory renames only allowed on "pure upper" (already created on
386+ * upper filesystem, never copied up). Directories which are on lower or
387+ * are merged may not be renamed. For these -EXDEV is returned and
388+ * userspace has to deal with it. This means, when copying up a
389+ * directory we can rely on it and ancestors being stable.
390+ *
391+ * Non-directory renames start with copy up of source if necessary. The
392+ * actual rename will only proceed once the copy up was successful. Copy
393+ * up uses upper parent i_mutex for exclusion. Since rename can change
394+ * d_parent it is possible that the copy up will lock the old parent. At
395+ * that point the file will have already been copied up anyway.
396+ */
397+static int ovl_copy_up_one(struct dentry *parent, struct dentry *dentry,
398+ struct path *lowerpath, struct kstat *stat)
399+{
400+ int err;
401+ struct kstat pstat;
402+ struct path parentpath;
403+ struct dentry *upperdir;
404+ const struct cred *old_cred;
405+ struct cred *override_cred;
406+ char *link = NULL;
407+
408+ ovl_path_upper(parent, &parentpath);
409+ upperdir = parentpath.dentry;
410+
514e5dae 411+ err = vfs_getattr(&parentpath, &pstat);
68184a5a
JR
412+ if (err)
413+ return err;
414+
415+ if (S_ISLNK(stat->mode)) {
416+ link = ovl_read_symlink(lowerpath->dentry);
417+ if (IS_ERR(link))
418+ return PTR_ERR(link);
419+ }
420+
421+ err = -ENOMEM;
422+ override_cred = prepare_creds();
423+ if (!override_cred)
424+ goto out_free_link;
425+
426+ override_cred->fsuid = stat->uid;
427+ override_cred->fsgid = stat->gid;
428+ /*
429+ * CAP_SYS_ADMIN for copying up extended attributes
430+ * CAP_DAC_OVERRIDE for create
431+ * CAP_FOWNER for chmod, timestamp update
432+ * CAP_FSETID for chmod
433+ * CAP_MKNOD for mknod
434+ */
435+ cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
436+ cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
437+ cap_raise(override_cred->cap_effective, CAP_FOWNER);
438+ cap_raise(override_cred->cap_effective, CAP_FSETID);
439+ cap_raise(override_cred->cap_effective, CAP_MKNOD);
440+ old_cred = override_creds(override_cred);
441+
442+ mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
443+ if (ovl_path_type(dentry) != OVL_PATH_LOWER) {
444+ err = 0;
445+ } else {
446+ err = ovl_copy_up_locked(upperdir, dentry, lowerpath,
447+ stat, link);
448+ if (!err) {
449+ /* Restore timestamps on parent (best effort) */
450+ ovl_set_timestamps(upperdir, &pstat);
451+ }
452+ }
453+
454+ mutex_unlock(&upperdir->d_inode->i_mutex);
455+
456+ revert_creds(old_cred);
457+ put_cred(override_cred);
458+
459+out_free_link:
460+ if (link)
461+ free_page((unsigned long) link);
462+
463+ return err;
464+}
465+
466+int ovl_copy_up(struct dentry *dentry)
467+{
468+ int err;
469+
470+ err = 0;
471+ while (!err) {
472+ struct dentry *next;
473+ struct dentry *parent;
474+ struct path lowerpath;
475+ struct kstat stat;
476+ enum ovl_path_type type = ovl_path_type(dentry);
477+
478+ if (type != OVL_PATH_LOWER)
479+ break;
480+
481+ next = dget(dentry);
482+ /* find the topmost dentry not yet copied up */
483+ for (;;) {
484+ parent = dget_parent(next);
485+
486+ type = ovl_path_type(parent);
487+ if (type != OVL_PATH_LOWER)
488+ break;
489+
490+ dput(next);
491+ next = parent;
492+ }
493+
494+ ovl_path_lower(next, &lowerpath);
514e5dae 495+ err = vfs_getattr(&lowerpath, &stat);
68184a5a
JR
496+ if (!err)
497+ err = ovl_copy_up_one(parent, next, &lowerpath, &stat);
498+
499+ dput(parent);
500+ dput(next);
501+ }
502+
503+ return err;
504+}
505+
506+/* Optimize by not copying up the file first and truncating later */
507+int ovl_copy_up_truncate(struct dentry *dentry, loff_t size)
508+{
509+ int err;
510+ struct kstat stat;
511+ struct path lowerpath;
512+ struct dentry *parent = dget_parent(dentry);
513+
514+ err = ovl_copy_up(parent);
515+ if (err)
516+ goto out_dput_parent;
517+
518+ ovl_path_lower(dentry, &lowerpath);
514e5dae 519+ err = vfs_getattr(&lowerpath, &stat);
68184a5a
JR
520+ if (err)
521+ goto out_dput_parent;
522+
523+ if (size < stat.size)
524+ stat.size = size;
525+
526+ err = ovl_copy_up_one(parent, dentry, &lowerpath, &stat);
527+
528+out_dput_parent:
529+ dput(parent);
530+ return err;
531+}
514e5dae
AM
532--- /dev/null
533+++ b/fs/overlayfs/dir.c
a25ec4e9 534@@ -0,0 +1,606 @@
68184a5a
JR
535+/*
536+ *
537+ * Copyright (C) 2011 Novell Inc.
538+ *
539+ * This program is free software; you can redistribute it and/or modify it
540+ * under the terms of the GNU General Public License version 2 as published by
541+ * the Free Software Foundation.
542+ */
543+
544+#include <linux/fs.h>
545+#include <linux/namei.h>
546+#include <linux/xattr.h>
547+#include <linux/security.h>
548+#include <linux/cred.h>
549+#include "overlayfs.h"
550+
551+static const char *ovl_whiteout_symlink = "(overlay-whiteout)";
552+
553+static int ovl_whiteout(struct dentry *upperdir, struct dentry *dentry)
554+{
555+ int err;
556+ struct dentry *newdentry;
557+ const struct cred *old_cred;
558+ struct cred *override_cred;
559+
560+ /* FIXME: recheck lower dentry to see if whiteout is really needed */
561+
562+ err = -ENOMEM;
563+ override_cred = prepare_creds();
564+ if (!override_cred)
565+ goto out;
566+
567+ /*
568+ * CAP_SYS_ADMIN for setxattr
569+ * CAP_DAC_OVERRIDE for symlink creation
570+ * CAP_FOWNER for unlink in sticky directory
571+ */
572+ cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
573+ cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
574+ cap_raise(override_cred->cap_effective, CAP_FOWNER);
575+ override_cred->fsuid = GLOBAL_ROOT_UID;
576+ override_cred->fsgid = GLOBAL_ROOT_GID;
577+ old_cred = override_creds(override_cred);
578+
579+ newdentry = lookup_one_len(dentry->d_name.name, upperdir,
580+ dentry->d_name.len);
581+ err = PTR_ERR(newdentry);
582+ if (IS_ERR(newdentry))
583+ goto out_put_cred;
584+
585+ /* Just been removed within the same locked region */
586+ WARN_ON(newdentry->d_inode);
587+
588+ err = vfs_symlink(upperdir->d_inode, newdentry, ovl_whiteout_symlink);
589+ if (err)
590+ goto out_dput;
591+
592+ ovl_dentry_version_inc(dentry->d_parent);
593+
594+ err = vfs_setxattr(newdentry, ovl_whiteout_xattr, "y", 1, 0);
595+ if (err)
a25ec4e9 596+ vfs_unlink(upperdir->d_inode, newdentry, NULL);
68184a5a
JR
597+
598+out_dput:
599+ dput(newdentry);
600+out_put_cred:
601+ revert_creds(old_cred);
602+ put_cred(override_cred);
603+out:
604+ if (err) {
605+ /*
606+ * There's no way to recover from failure to whiteout.
607+ * What should we do? Log a big fat error and... ?
608+ */
c06a8ce3 609+ pr_err("overlayfs: ERROR - failed to whiteout '%s'\n",
68184a5a
JR
610+ dentry->d_name.name);
611+ }
612+
613+ return err;
614+}
615+
616+static struct dentry *ovl_lookup_create(struct dentry *upperdir,
617+ struct dentry *template)
618+{
619+ int err;
620+ struct dentry *newdentry;
621+ struct qstr *name = &template->d_name;
622+
623+ newdentry = lookup_one_len(name->name, upperdir, name->len);
624+ if (IS_ERR(newdentry))
625+ return newdentry;
626+
627+ if (newdentry->d_inode) {
628+ const struct cred *old_cred;
629+ struct cred *override_cred;
630+
631+ /* No need to check whiteout if lower parent is non-existent */
632+ err = -EEXIST;
633+ if (!ovl_dentry_lower(template->d_parent))
634+ goto out_dput;
635+
636+ if (!S_ISLNK(newdentry->d_inode->i_mode))
637+ goto out_dput;
638+
639+ err = -ENOMEM;
640+ override_cred = prepare_creds();
641+ if (!override_cred)
642+ goto out_dput;
643+
644+ /*
645+ * CAP_SYS_ADMIN for getxattr
646+ * CAP_FOWNER for unlink in sticky directory
647+ */
648+ cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
649+ cap_raise(override_cred->cap_effective, CAP_FOWNER);
650+ old_cred = override_creds(override_cred);
651+
652+ err = -EEXIST;
653+ if (ovl_is_whiteout(newdentry))
a25ec4e9 654+ err = vfs_unlink(upperdir->d_inode, newdentry, NULL);
68184a5a
JR
655+
656+ revert_creds(old_cred);
657+ put_cred(override_cred);
658+ if (err)
659+ goto out_dput;
660+
661+ dput(newdentry);
662+ newdentry = lookup_one_len(name->name, upperdir, name->len);
663+ if (IS_ERR(newdentry)) {
664+ ovl_whiteout(upperdir, template);
665+ return newdentry;
666+ }
667+
668+ /*
669+ * Whiteout just been successfully removed, parent
670+ * i_mutex is still held, there's no way the lookup
671+ * could return positive.
672+ */
673+ WARN_ON(newdentry->d_inode);
674+ }
675+
676+ return newdentry;
677+
678+out_dput:
679+ dput(newdentry);
680+ return ERR_PTR(err);
681+}
682+
683+struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
684+ struct kstat *stat, const char *link)
685+{
686+ int err;
687+ struct dentry *newdentry;
688+ struct inode *dir = upperdir->d_inode;
689+
690+ newdentry = ovl_lookup_create(upperdir, dentry);
691+ if (IS_ERR(newdentry))
692+ goto out;
693+
694+ switch (stat->mode & S_IFMT) {
695+ case S_IFREG:
696+ err = vfs_create(dir, newdentry, stat->mode, NULL);
697+ break;
698+
699+ case S_IFDIR:
700+ err = vfs_mkdir(dir, newdentry, stat->mode);
701+ break;
702+
703+ case S_IFCHR:
704+ case S_IFBLK:
705+ case S_IFIFO:
706+ case S_IFSOCK:
707+ err = vfs_mknod(dir, newdentry, stat->mode, stat->rdev);
708+ break;
709+
710+ case S_IFLNK:
711+ err = vfs_symlink(dir, newdentry, link);
712+ break;
713+
714+ default:
715+ err = -EPERM;
716+ }
717+ if (err) {
718+ if (ovl_dentry_is_opaque(dentry))
719+ ovl_whiteout(upperdir, dentry);
720+ dput(newdentry);
721+ newdentry = ERR_PTR(err);
722+ } else if (WARN_ON(!newdentry->d_inode)) {
723+ /*
724+ * Not quite sure if non-instantiated dentry is legal or not.
725+ * VFS doesn't seem to care so check and warn here.
726+ */
727+ dput(newdentry);
728+ newdentry = ERR_PTR(-ENOENT);
729+ }
730+
731+out:
732+ return newdentry;
733+
734+}
735+
736+static int ovl_set_opaque(struct dentry *upperdentry)
737+{
738+ int err;
739+ const struct cred *old_cred;
740+ struct cred *override_cred;
741+
742+ override_cred = prepare_creds();
743+ if (!override_cred)
744+ return -ENOMEM;
745+
746+ /* CAP_SYS_ADMIN for setxattr of "trusted" namespace */
747+ cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
748+ old_cred = override_creds(override_cred);
749+ err = vfs_setxattr(upperdentry, ovl_opaque_xattr, "y", 1, 0);
750+ revert_creds(old_cred);
751+ put_cred(override_cred);
752+
753+ return err;
754+}
755+
756+static int ovl_remove_opaque(struct dentry *upperdentry)
757+{
758+ int err;
759+ const struct cred *old_cred;
760+ struct cred *override_cred;
761+
762+ override_cred = prepare_creds();
763+ if (!override_cred)
764+ return -ENOMEM;
765+
766+ /* CAP_SYS_ADMIN for removexattr of "trusted" namespace */
767+ cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
768+ old_cred = override_creds(override_cred);
769+ err = vfs_removexattr(upperdentry, ovl_opaque_xattr);
770+ revert_creds(old_cred);
771+ put_cred(override_cred);
772+
773+ return err;
774+}
775+
776+static int ovl_dir_getattr(struct vfsmount *mnt, struct dentry *dentry,
777+ struct kstat *stat)
778+{
779+ int err;
780+ enum ovl_path_type type;
781+ struct path realpath;
782+
783+ type = ovl_path_real(dentry, &realpath);
514e5dae 784+ err = vfs_getattr(&realpath, stat);
68184a5a
JR
785+ if (err)
786+ return err;
787+
788+ stat->dev = dentry->d_sb->s_dev;
789+ stat->ino = dentry->d_inode->i_ino;
790+
791+ /*
792+ * It's probably not worth it to count subdirs to get the
793+ * correct link count. nlink=1 seems to pacify 'find' and
794+ * other utilities.
795+ */
796+ if (type == OVL_PATH_MERGE)
797+ stat->nlink = 1;
798+
799+ return 0;
800+}
801+
802+static int ovl_create_object(struct dentry *dentry, int mode, dev_t rdev,
803+ const char *link)
804+{
805+ int err;
806+ struct dentry *newdentry;
807+ struct dentry *upperdir;
808+ struct inode *inode;
809+ struct kstat stat = {
810+ .mode = mode,
811+ .rdev = rdev,
812+ };
813+
814+ err = -ENOMEM;
815+ inode = ovl_new_inode(dentry->d_sb, mode, dentry->d_fsdata);
816+ if (!inode)
817+ goto out;
818+
819+ err = ovl_copy_up(dentry->d_parent);
820+ if (err)
821+ goto out_iput;
822+
823+ upperdir = ovl_dentry_upper(dentry->d_parent);
824+ mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
825+
826+ newdentry = ovl_upper_create(upperdir, dentry, &stat, link);
827+ err = PTR_ERR(newdentry);
828+ if (IS_ERR(newdentry))
829+ goto out_unlock;
830+
831+ ovl_dentry_version_inc(dentry->d_parent);
832+ if (ovl_dentry_is_opaque(dentry) && S_ISDIR(mode)) {
833+ err = ovl_set_opaque(newdentry);
834+ if (err) {
835+ vfs_rmdir(upperdir->d_inode, newdentry);
836+ ovl_whiteout(upperdir, dentry);
837+ goto out_dput;
838+ }
839+ }
840+ ovl_dentry_update(dentry, newdentry);
c06a8ce3 841+ ovl_copyattr(newdentry->d_inode, inode);
68184a5a
JR
842+ d_instantiate(dentry, inode);
843+ inode = NULL;
844+ newdentry = NULL;
845+ err = 0;
846+
847+out_dput:
848+ dput(newdentry);
849+out_unlock:
850+ mutex_unlock(&upperdir->d_inode->i_mutex);
851+out_iput:
852+ iput(inode);
853+out:
854+ return err;
855+}
856+
857+static int ovl_create(struct inode *dir, struct dentry *dentry, umode_t mode,
858+ bool excl)
859+{
860+ return ovl_create_object(dentry, (mode & 07777) | S_IFREG, 0, NULL);
861+}
862+
863+static int ovl_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
864+{
865+ return ovl_create_object(dentry, (mode & 07777) | S_IFDIR, 0, NULL);
866+}
867+
868+static int ovl_mknod(struct inode *dir, struct dentry *dentry, umode_t mode,
869+ dev_t rdev)
870+{
871+ return ovl_create_object(dentry, mode, rdev, NULL);
872+}
873+
874+static int ovl_symlink(struct inode *dir, struct dentry *dentry,
875+ const char *link)
876+{
877+ return ovl_create_object(dentry, S_IFLNK, 0, link);
878+}
879+
880+static int ovl_do_remove(struct dentry *dentry, bool is_dir)
881+{
882+ int err;
883+ enum ovl_path_type type;
884+ struct path realpath;
885+ struct dentry *upperdir;
886+
887+ err = ovl_copy_up(dentry->d_parent);
888+ if (err)
889+ return err;
890+
891+ upperdir = ovl_dentry_upper(dentry->d_parent);
892+ mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
893+ type = ovl_path_real(dentry, &realpath);
894+ if (type != OVL_PATH_LOWER) {
895+ err = -ESTALE;
896+ if (realpath.dentry->d_parent != upperdir)
897+ goto out_d_drop;
898+
899+ /* FIXME: create whiteout up front and rename to target */
900+
901+ if (is_dir)
902+ err = vfs_rmdir(upperdir->d_inode, realpath.dentry);
903+ else
a25ec4e9
AM
904+ err = vfs_unlink(upperdir->d_inode, realpath.dentry,
905+ NULL);
68184a5a
JR
906+ if (err)
907+ goto out_d_drop;
908+
909+ ovl_dentry_version_inc(dentry->d_parent);
910+ }
911+
912+ if (type != OVL_PATH_UPPER || ovl_dentry_is_opaque(dentry))
913+ err = ovl_whiteout(upperdir, dentry);
914+
915+ /*
916+ * Keeping this dentry hashed would mean having to release
917+ * upperpath/lowerpath, which could only be done if we are the
918+ * sole user of this dentry. Too tricky... Just unhash for
919+ * now.
920+ */
921+out_d_drop:
922+ d_drop(dentry);
923+ mutex_unlock(&upperdir->d_inode->i_mutex);
924+
925+ return err;
926+}
927+
928+static int ovl_unlink(struct inode *dir, struct dentry *dentry)
929+{
930+ return ovl_do_remove(dentry, false);
931+}
932+
933+
934+static int ovl_rmdir(struct inode *dir, struct dentry *dentry)
935+{
936+ int err;
937+ enum ovl_path_type type;
938+
939+ type = ovl_path_type(dentry);
940+ if (type != OVL_PATH_UPPER) {
941+ err = ovl_check_empty_and_clear(dentry, type);
942+ if (err)
943+ return err;
944+ }
945+
946+ return ovl_do_remove(dentry, true);
947+}
948+
949+static int ovl_link(struct dentry *old, struct inode *newdir,
950+ struct dentry *new)
951+{
952+ int err;
953+ struct dentry *olddentry;
954+ struct dentry *newdentry;
955+ struct dentry *upperdir;
c06a8ce3 956+ struct inode *newinode;
68184a5a
JR
957+
958+ err = ovl_copy_up(old);
959+ if (err)
960+ goto out;
961+
962+ err = ovl_copy_up(new->d_parent);
963+ if (err)
964+ goto out;
965+
966+ upperdir = ovl_dentry_upper(new->d_parent);
967+ mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
968+ newdentry = ovl_lookup_create(upperdir, new);
969+ err = PTR_ERR(newdentry);
970+ if (IS_ERR(newdentry))
971+ goto out_unlock;
972+
973+ olddentry = ovl_dentry_upper(old);
a25ec4e9 974+ err = vfs_link(olddentry, upperdir->d_inode, newdentry, NULL);
68184a5a
JR
975+ if (!err) {
976+ if (WARN_ON(!newdentry->d_inode)) {
977+ dput(newdentry);
978+ err = -ENOENT;
979+ goto out_unlock;
980+ }
c06a8ce3
AM
981+ newinode = ovl_new_inode(old->d_sb, newdentry->d_inode->i_mode,
982+ new->d_fsdata);
983+ if (!newinode) {
984+ err = -ENOMEM;
985+ goto link_fail;
986+ }
987+ ovl_copyattr(upperdir->d_inode, newinode);
68184a5a
JR
988+
989+ ovl_dentry_version_inc(new->d_parent);
990+ ovl_dentry_update(new, newdentry);
991+
c06a8ce3 992+ d_instantiate(new, newinode);
68184a5a 993+ } else {
c06a8ce3 994+link_fail:
68184a5a
JR
995+ if (ovl_dentry_is_opaque(new))
996+ ovl_whiteout(upperdir, new);
997+ dput(newdentry);
998+ }
999+out_unlock:
1000+ mutex_unlock(&upperdir->d_inode->i_mutex);
1001+out:
1002+ return err;
68184a5a
JR
1003+}
1004+
1005+static int ovl_rename(struct inode *olddir, struct dentry *old,
1006+ struct inode *newdir, struct dentry *new)
1007+{
1008+ int err;
1009+ enum ovl_path_type old_type;
1010+ enum ovl_path_type new_type;
1011+ struct dentry *old_upperdir;
1012+ struct dentry *new_upperdir;
1013+ struct dentry *olddentry;
1014+ struct dentry *newdentry;
1015+ struct dentry *trap;
1016+ bool old_opaque;
1017+ bool new_opaque;
1018+ bool new_create = false;
1019+ bool is_dir = S_ISDIR(old->d_inode->i_mode);
1020+
1021+ /* Don't copy up directory trees */
1022+ old_type = ovl_path_type(old);
1023+ if (old_type != OVL_PATH_UPPER && is_dir)
1024+ return -EXDEV;
1025+
1026+ if (new->d_inode) {
1027+ new_type = ovl_path_type(new);
1028+
1029+ if (new_type == OVL_PATH_LOWER && old_type == OVL_PATH_LOWER) {
1030+ if (ovl_dentry_lower(old)->d_inode ==
1031+ ovl_dentry_lower(new)->d_inode)
1032+ return 0;
1033+ }
1034+ if (new_type != OVL_PATH_LOWER && old_type != OVL_PATH_LOWER) {
1035+ if (ovl_dentry_upper(old)->d_inode ==
1036+ ovl_dentry_upper(new)->d_inode)
1037+ return 0;
1038+ }
1039+
1040+ if (new_type != OVL_PATH_UPPER &&
1041+ S_ISDIR(new->d_inode->i_mode)) {
1042+ err = ovl_check_empty_and_clear(new, new_type);
1043+ if (err)
1044+ return err;
1045+ }
1046+ } else {
1047+ new_type = OVL_PATH_UPPER;
1048+ }
1049+
1050+ err = ovl_copy_up(old);
1051+ if (err)
1052+ return err;
1053+
1054+ err = ovl_copy_up(new->d_parent);
1055+ if (err)
1056+ return err;
1057+
1058+ old_upperdir = ovl_dentry_upper(old->d_parent);
1059+ new_upperdir = ovl_dentry_upper(new->d_parent);
1060+
1061+ trap = lock_rename(new_upperdir, old_upperdir);
1062+
1063+ olddentry = ovl_dentry_upper(old);
1064+ newdentry = ovl_dentry_upper(new);
1065+ if (newdentry) {
1066+ dget(newdentry);
1067+ } else {
1068+ new_create = true;
1069+ newdentry = ovl_lookup_create(new_upperdir, new);
1070+ err = PTR_ERR(newdentry);
1071+ if (IS_ERR(newdentry))
1072+ goto out_unlock;
1073+ }
1074+
1075+ err = -ESTALE;
1076+ if (olddentry->d_parent != old_upperdir)
1077+ goto out_dput;
1078+ if (newdentry->d_parent != new_upperdir)
1079+ goto out_dput;
1080+ if (olddentry == trap)
1081+ goto out_dput;
1082+ if (newdentry == trap)
1083+ goto out_dput;
1084+
1085+ old_opaque = ovl_dentry_is_opaque(old);
1086+ new_opaque = ovl_dentry_is_opaque(new) || new_type != OVL_PATH_UPPER;
1087+
1088+ if (is_dir && !old_opaque && new_opaque) {
1089+ err = ovl_set_opaque(olddentry);
1090+ if (err)
1091+ goto out_dput;
1092+ }
1093+
1094+ err = vfs_rename(old_upperdir->d_inode, olddentry,
a25ec4e9 1095+ new_upperdir->d_inode, newdentry, NULL);
68184a5a
JR
1096+
1097+ if (err) {
1098+ if (new_create && ovl_dentry_is_opaque(new))
1099+ ovl_whiteout(new_upperdir, new);
1100+ if (is_dir && !old_opaque && new_opaque)
1101+ ovl_remove_opaque(olddentry);
1102+ goto out_dput;
1103+ }
1104+
1105+ if (old_type != OVL_PATH_UPPER || old_opaque)
1106+ err = ovl_whiteout(old_upperdir, old);
1107+ if (is_dir && old_opaque && !new_opaque)
1108+ ovl_remove_opaque(olddentry);
1109+
1110+ if (old_opaque != new_opaque)
1111+ ovl_dentry_set_opaque(old, new_opaque);
1112+
1113+ ovl_dentry_version_inc(old->d_parent);
1114+ ovl_dentry_version_inc(new->d_parent);
1115+
1116+out_dput:
1117+ dput(newdentry);
1118+out_unlock:
1119+ unlock_rename(new_upperdir, old_upperdir);
1120+ return err;
1121+}
1122+
1123+const struct inode_operations ovl_dir_inode_operations = {
1124+ .lookup = ovl_lookup,
1125+ .mkdir = ovl_mkdir,
1126+ .symlink = ovl_symlink,
1127+ .unlink = ovl_unlink,
1128+ .rmdir = ovl_rmdir,
1129+ .rename = ovl_rename,
1130+ .link = ovl_link,
1131+ .setattr = ovl_setattr,
1132+ .create = ovl_create,
1133+ .mknod = ovl_mknod,
1134+ .permission = ovl_permission,
1135+ .getattr = ovl_dir_getattr,
1136+ .setxattr = ovl_setxattr,
1137+ .getxattr = ovl_getxattr,
1138+ .listxattr = ovl_listxattr,
1139+ .removexattr = ovl_removexattr,
1140+};
514e5dae
AM
1141--- /dev/null
1142+++ b/fs/overlayfs/inode.c
c06a8ce3 1143@@ -0,0 +1,372 @@
68184a5a
JR
1144+/*
1145+ *
1146+ * Copyright (C) 2011 Novell Inc.
1147+ *
1148+ * This program is free software; you can redistribute it and/or modify it
1149+ * under the terms of the GNU General Public License version 2 as published by
1150+ * the Free Software Foundation.
1151+ */
1152+
1153+#include <linux/fs.h>
1154+#include <linux/slab.h>
1155+#include <linux/xattr.h>
1156+#include "overlayfs.h"
1157+
1158+int ovl_setattr(struct dentry *dentry, struct iattr *attr)
1159+{
1160+ struct dentry *upperdentry;
1161+ int err;
1162+
1163+ if ((attr->ia_valid & ATTR_SIZE) && !ovl_dentry_upper(dentry))
1164+ err = ovl_copy_up_truncate(dentry, attr->ia_size);
1165+ else
1166+ err = ovl_copy_up(dentry);
1167+ if (err)
1168+ return err;
1169+
1170+ upperdentry = ovl_dentry_upper(dentry);
1171+
1172+ if (attr->ia_valid & (ATTR_KILL_SUID|ATTR_KILL_SGID))
1173+ attr->ia_valid &= ~ATTR_MODE;
1174+
1175+ mutex_lock(&upperdentry->d_inode->i_mutex);
a25ec4e9 1176+ err = notify_change(upperdentry, attr, NULL);
c06a8ce3
AM
1177+ if (!err)
1178+ ovl_copyattr(upperdentry->d_inode, dentry->d_inode);
68184a5a
JR
1179+ mutex_unlock(&upperdentry->d_inode->i_mutex);
1180+
1181+ return err;
1182+}
1183+
1184+static int ovl_getattr(struct vfsmount *mnt, struct dentry *dentry,
1185+ struct kstat *stat)
1186+{
1187+ struct path realpath;
1188+
1189+ ovl_path_real(dentry, &realpath);
514e5dae 1190+ return vfs_getattr(&realpath, stat);
68184a5a
JR
1191+}
1192+
1193+int ovl_permission(struct inode *inode, int mask)
1194+{
1195+ struct ovl_entry *oe;
1196+ struct dentry *alias = NULL;
1197+ struct inode *realinode;
1198+ struct dentry *realdentry;
1199+ bool is_upper;
1200+ int err;
1201+
1202+ if (S_ISDIR(inode->i_mode)) {
1203+ oe = inode->i_private;
1204+ } else if (mask & MAY_NOT_BLOCK) {
1205+ return -ECHILD;
1206+ } else {
1207+ /*
1208+ * For non-directories find an alias and get the info
1209+ * from there.
1210+ */
1211+ alias = d_find_any_alias(inode);
1212+ if (WARN_ON(!alias))
1213+ return -ENOENT;
1214+
1215+ oe = alias->d_fsdata;
1216+ }
1217+
1218+ realdentry = ovl_entry_real(oe, &is_upper);
1219+
1220+ /* Careful in RCU walk mode */
1221+ realinode = ACCESS_ONCE(realdentry->d_inode);
1222+ if (!realinode) {
1223+ WARN_ON(!(mask & MAY_NOT_BLOCK));
1224+ err = -ENOENT;
1225+ goto out_dput;
1226+ }
1227+
1228+ if (mask & MAY_WRITE) {
1229+ umode_t mode = realinode->i_mode;
1230+
1231+ /*
1232+ * Writes will always be redirected to upper layer, so
1233+ * ignore lower layer being read-only.
1234+ *
1235+ * If the overlay itself is read-only then proceed
1236+ * with the permission check, don't return EROFS.
1237+ * This will only happen if this is the lower layer of
1238+ * another overlayfs.
1239+ *
1240+ * If upper fs becomes read-only after the overlay was
1241+ * constructed return EROFS to prevent modification of
1242+ * upper layer.
1243+ */
1244+ err = -EROFS;
1245+ if (is_upper && !IS_RDONLY(inode) && IS_RDONLY(realinode) &&
1246+ (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode)))
1247+ goto out_dput;
68184a5a
JR
1248+ }
1249+
c06a8ce3 1250+ err = __inode_permission(realinode, mask);
68184a5a
JR
1251+out_dput:
1252+ dput(alias);
1253+ return err;
1254+}
1255+
1256+
1257+struct ovl_link_data {
1258+ struct dentry *realdentry;
1259+ void *cookie;
1260+};
1261+
1262+static void *ovl_follow_link(struct dentry *dentry, struct nameidata *nd)
1263+{
1264+ void *ret;
1265+ struct dentry *realdentry;
1266+ struct inode *realinode;
1267+
1268+ realdentry = ovl_dentry_real(dentry);
1269+ realinode = realdentry->d_inode;
1270+
1271+ if (WARN_ON(!realinode->i_op->follow_link))
1272+ return ERR_PTR(-EPERM);
1273+
1274+ ret = realinode->i_op->follow_link(realdentry, nd);
1275+ if (IS_ERR(ret))
1276+ return ret;
1277+
1278+ if (realinode->i_op->put_link) {
1279+ struct ovl_link_data *data;
1280+
1281+ data = kmalloc(sizeof(struct ovl_link_data), GFP_KERNEL);
1282+ if (!data) {
1283+ realinode->i_op->put_link(realdentry, nd, ret);
1284+ return ERR_PTR(-ENOMEM);
1285+ }
1286+ data->realdentry = realdentry;
1287+ data->cookie = ret;
1288+
1289+ return data;
1290+ } else {
1291+ return NULL;
1292+ }
1293+}
1294+
1295+static void ovl_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1296+{
1297+ struct inode *realinode;
1298+ struct ovl_link_data *data = c;
1299+
1300+ if (!data)
1301+ return;
1302+
1303+ realinode = data->realdentry->d_inode;
1304+ realinode->i_op->put_link(data->realdentry, nd, data->cookie);
1305+ kfree(data);
1306+}
1307+
1308+static int ovl_readlink(struct dentry *dentry, char __user *buf, int bufsiz)
1309+{
1310+ struct path realpath;
1311+ struct inode *realinode;
1312+
1313+ ovl_path_real(dentry, &realpath);
1314+ realinode = realpath.dentry->d_inode;
1315+
1316+ if (!realinode->i_op->readlink)
1317+ return -EINVAL;
1318+
1319+ touch_atime(&realpath);
1320+
1321+ return realinode->i_op->readlink(realpath.dentry, buf, bufsiz);
1322+}
1323+
1324+
1325+static bool ovl_is_private_xattr(const char *name)
1326+{
1327+ return strncmp(name, "trusted.overlay.", 14) == 0;
1328+}
1329+
1330+int ovl_setxattr(struct dentry *dentry, const char *name,
1331+ const void *value, size_t size, int flags)
1332+{
1333+ int err;
1334+ struct dentry *upperdentry;
1335+
1336+ if (ovl_is_private_xattr(name))
1337+ return -EPERM;
1338+
1339+ err = ovl_copy_up(dentry);
1340+ if (err)
1341+ return err;
1342+
1343+ upperdentry = ovl_dentry_upper(dentry);
1344+ return vfs_setxattr(upperdentry, name, value, size, flags);
1345+}
1346+
1347+ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1348+ void *value, size_t size)
1349+{
1350+ if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1351+ ovl_is_private_xattr(name))
1352+ return -ENODATA;
1353+
1354+ return vfs_getxattr(ovl_dentry_real(dentry), name, value, size);
1355+}
1356+
1357+ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size)
1358+{
1359+ ssize_t res;
1360+ int off;
1361+
1362+ res = vfs_listxattr(ovl_dentry_real(dentry), list, size);
1363+ if (res <= 0 || size == 0)
1364+ return res;
1365+
1366+ if (ovl_path_type(dentry->d_parent) != OVL_PATH_MERGE)
1367+ return res;
1368+
1369+ /* filter out private xattrs */
1370+ for (off = 0; off < res;) {
1371+ char *s = list + off;
1372+ size_t slen = strlen(s) + 1;
1373+
1374+ BUG_ON(off + slen > res);
1375+
1376+ if (ovl_is_private_xattr(s)) {
1377+ res -= slen;
1378+ memmove(s, s + slen, res - off);
1379+ } else {
1380+ off += slen;
1381+ }
1382+ }
1383+
1384+ return res;
1385+}
1386+
1387+int ovl_removexattr(struct dentry *dentry, const char *name)
1388+{
1389+ int err;
1390+ struct path realpath;
1391+ enum ovl_path_type type;
1392+
1393+ if (ovl_path_type(dentry->d_parent) == OVL_PATH_MERGE &&
1394+ ovl_is_private_xattr(name))
1395+ return -ENODATA;
1396+
1397+ type = ovl_path_real(dentry, &realpath);
1398+ if (type == OVL_PATH_LOWER) {
1399+ err = vfs_getxattr(realpath.dentry, name, NULL, 0);
1400+ if (err < 0)
1401+ return err;
1402+
1403+ err = ovl_copy_up(dentry);
1404+ if (err)
1405+ return err;
1406+
1407+ ovl_path_upper(dentry, &realpath);
1408+ }
1409+
1410+ return vfs_removexattr(realpath.dentry, name);
1411+}
1412+
1413+static bool ovl_open_need_copy_up(int flags, enum ovl_path_type type,
1414+ struct dentry *realdentry)
1415+{
1416+ if (type != OVL_PATH_LOWER)
1417+ return false;
1418+
1419+ if (special_file(realdentry->d_inode->i_mode))
1420+ return false;
1421+
1422+ if (!(OPEN_FMODE(flags) & FMODE_WRITE) && !(flags & O_TRUNC))
1423+ return false;
1424+
1425+ return true;
1426+}
1427+
1428+static int ovl_dentry_open(struct dentry *dentry, struct file *file,
1429+ const struct cred *cred)
1430+{
1431+ int err;
1432+ struct path realpath;
1433+ enum ovl_path_type type;
1434+
1435+ type = ovl_path_real(dentry, &realpath);
1436+ if (ovl_open_need_copy_up(file->f_flags, type, realpath.dentry)) {
1437+ if (file->f_flags & O_TRUNC)
1438+ err = ovl_copy_up_truncate(dentry, 0);
1439+ else
1440+ err = ovl_copy_up(dentry);
1441+ if (err)
1442+ return err;
1443+
1444+ ovl_path_upper(dentry, &realpath);
1445+ }
1446+
1447+ return vfs_open(&realpath, file, cred);
1448+}
1449+
1450+static const struct inode_operations ovl_file_inode_operations = {
1451+ .setattr = ovl_setattr,
1452+ .permission = ovl_permission,
1453+ .getattr = ovl_getattr,
1454+ .setxattr = ovl_setxattr,
1455+ .getxattr = ovl_getxattr,
1456+ .listxattr = ovl_listxattr,
1457+ .removexattr = ovl_removexattr,
1458+ .dentry_open = ovl_dentry_open,
1459+};
1460+
1461+static const struct inode_operations ovl_symlink_inode_operations = {
1462+ .setattr = ovl_setattr,
1463+ .follow_link = ovl_follow_link,
1464+ .put_link = ovl_put_link,
1465+ .readlink = ovl_readlink,
1466+ .getattr = ovl_getattr,
1467+ .setxattr = ovl_setxattr,
1468+ .getxattr = ovl_getxattr,
1469+ .listxattr = ovl_listxattr,
1470+ .removexattr = ovl_removexattr,
1471+};
1472+
1473+struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1474+ struct ovl_entry *oe)
1475+{
1476+ struct inode *inode;
1477+
1478+ inode = new_inode(sb);
1479+ if (!inode)
1480+ return NULL;
1481+
1482+ mode &= S_IFMT;
1483+
1484+ inode->i_ino = get_next_ino();
1485+ inode->i_mode = mode;
1486+ inode->i_flags |= S_NOATIME | S_NOCMTIME;
1487+
1488+ switch (mode) {
1489+ case S_IFDIR:
1490+ inode->i_private = oe;
1491+ inode->i_op = &ovl_dir_inode_operations;
1492+ inode->i_fop = &ovl_dir_operations;
1493+ break;
1494+
1495+ case S_IFLNK:
1496+ inode->i_op = &ovl_symlink_inode_operations;
1497+ break;
1498+
1499+ case S_IFREG:
1500+ case S_IFSOCK:
1501+ case S_IFBLK:
1502+ case S_IFCHR:
1503+ case S_IFIFO:
1504+ inode->i_op = &ovl_file_inode_operations;
1505+ break;
1506+
1507+ default:
1508+ WARN(1, "illegal file type: %i\n", mode);
c06a8ce3 1509+ iput(inode);
68184a5a
JR
1510+ inode = NULL;
1511+ }
1512+
1513+ return inode;
1514+
1515+}
514e5dae
AM
1516--- /dev/null
1517+++ b/fs/overlayfs/overlayfs.h
c06a8ce3 1518@@ -0,0 +1,70 @@
68184a5a
JR
1519+/*
1520+ *
1521+ * Copyright (C) 2011 Novell Inc.
1522+ *
1523+ * This program is free software; you can redistribute it and/or modify it
1524+ * under the terms of the GNU General Public License version 2 as published by
1525+ * the Free Software Foundation.
1526+ */
1527+
1528+struct ovl_entry;
1529+
1530+enum ovl_path_type {
1531+ OVL_PATH_UPPER,
1532+ OVL_PATH_MERGE,
1533+ OVL_PATH_LOWER,
1534+};
1535+
1536+extern const char *ovl_opaque_xattr;
1537+extern const char *ovl_whiteout_xattr;
1538+extern const struct dentry_operations ovl_dentry_operations;
1539+
1540+enum ovl_path_type ovl_path_type(struct dentry *dentry);
1541+u64 ovl_dentry_version_get(struct dentry *dentry);
1542+void ovl_dentry_version_inc(struct dentry *dentry);
1543+void ovl_path_upper(struct dentry *dentry, struct path *path);
1544+void ovl_path_lower(struct dentry *dentry, struct path *path);
1545+enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path);
1546+struct dentry *ovl_dentry_upper(struct dentry *dentry);
1547+struct dentry *ovl_dentry_lower(struct dentry *dentry);
1548+struct dentry *ovl_dentry_real(struct dentry *dentry);
1549+struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper);
1550+bool ovl_dentry_is_opaque(struct dentry *dentry);
1551+void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque);
1552+bool ovl_is_whiteout(struct dentry *dentry);
1553+void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry);
1554+struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
1555+ unsigned int flags);
1556+struct file *ovl_path_open(struct path *path, int flags);
1557+
1558+struct dentry *ovl_upper_create(struct dentry *upperdir, struct dentry *dentry,
1559+ struct kstat *stat, const char *link);
1560+
1561+/* readdir.c */
1562+extern const struct file_operations ovl_dir_operations;
1563+int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type);
1564+
1565+/* inode.c */
1566+int ovl_setattr(struct dentry *dentry, struct iattr *attr);
1567+int ovl_permission(struct inode *inode, int mask);
1568+int ovl_setxattr(struct dentry *dentry, const char *name,
1569+ const void *value, size_t size, int flags);
1570+ssize_t ovl_getxattr(struct dentry *dentry, const char *name,
1571+ void *value, size_t size);
1572+ssize_t ovl_listxattr(struct dentry *dentry, char *list, size_t size);
1573+int ovl_removexattr(struct dentry *dentry, const char *name);
1574+
1575+struct inode *ovl_new_inode(struct super_block *sb, umode_t mode,
1576+ struct ovl_entry *oe);
c06a8ce3
AM
1577+static inline void ovl_copyattr(struct inode *from, struct inode *to)
1578+{
1579+ to->i_uid = from->i_uid;
1580+ to->i_gid = from->i_gid;
1581+}
1582+
68184a5a
JR
1583+/* dir.c */
1584+extern const struct inode_operations ovl_dir_inode_operations;
1585+
1586+/* copy_up.c */
1587+int ovl_copy_up(struct dentry *dentry);
1588+int ovl_copy_up_truncate(struct dentry *dentry, loff_t size);
514e5dae
AM
1589--- /dev/null
1590+++ b/fs/overlayfs/readdir.c
392086de 1591@@ -0,0 +1,567 @@
68184a5a
JR
1592+/*
1593+ *
1594+ * Copyright (C) 2011 Novell Inc.
1595+ *
1596+ * This program is free software; you can redistribute it and/or modify it
1597+ * under the terms of the GNU General Public License version 2 as published by
1598+ * the Free Software Foundation.
1599+ */
1600+
1601+#include <linux/fs.h>
1602+#include <linux/slab.h>
1603+#include <linux/namei.h>
1604+#include <linux/file.h>
1605+#include <linux/xattr.h>
1606+#include <linux/rbtree.h>
1607+#include <linux/security.h>
1608+#include <linux/cred.h>
1609+#include "overlayfs.h"
1610+
1611+struct ovl_cache_entry {
1612+ const char *name;
1613+ unsigned int len;
1614+ unsigned int type;
1615+ u64 ino;
1616+ bool is_whiteout;
1617+ struct list_head l_node;
1618+ struct rb_node node;
1619+};
1620+
1621+struct ovl_readdir_data {
392086de
AM
1622+ struct dir_context ctx;
1623+ bool is_merge;
68184a5a
JR
1624+ struct rb_root *root;
1625+ struct list_head *list;
1626+ struct list_head *middle;
1627+ struct dentry *dir;
1628+ int count;
1629+ int err;
1630+};
1631+
1632+struct ovl_dir_file {
1633+ bool is_real;
1634+ bool is_cached;
1635+ struct list_head cursor;
1636+ u64 cache_version;
1637+ struct list_head cache;
1638+ struct file *realfile;
1639+};
1640+
1641+static struct ovl_cache_entry *ovl_cache_entry_from_node(struct rb_node *n)
1642+{
1643+ return container_of(n, struct ovl_cache_entry, node);
1644+}
1645+
1646+static struct ovl_cache_entry *ovl_cache_entry_find(struct rb_root *root,
1647+ const char *name, int len)
1648+{
1649+ struct rb_node *node = root->rb_node;
1650+ int cmp;
1651+
1652+ while (node) {
1653+ struct ovl_cache_entry *p = ovl_cache_entry_from_node(node);
1654+
1655+ cmp = strncmp(name, p->name, len);
1656+ if (cmp > 0)
1657+ node = p->node.rb_right;
1658+ else if (cmp < 0 || len < p->len)
1659+ node = p->node.rb_left;
1660+ else
1661+ return p;
1662+ }
1663+
1664+ return NULL;
1665+}
1666+
1667+static struct ovl_cache_entry *ovl_cache_entry_new(const char *name, int len,
1668+ u64 ino, unsigned int d_type)
1669+{
1670+ struct ovl_cache_entry *p;
1671+
1672+ p = kmalloc(sizeof(*p) + len + 1, GFP_KERNEL);
1673+ if (p) {
1674+ char *name_copy = (char *) (p + 1);
1675+ memcpy(name_copy, name, len);
1676+ name_copy[len] = '\0';
1677+ p->name = name_copy;
1678+ p->len = len;
1679+ p->type = d_type;
1680+ p->ino = ino;
1681+ p->is_whiteout = false;
1682+ }
1683+
1684+ return p;
1685+}
1686+
1687+static int ovl_cache_entry_add_rb(struct ovl_readdir_data *rdd,
1688+ const char *name, int len, u64 ino,
1689+ unsigned int d_type)
1690+{
1691+ struct rb_node **newp = &rdd->root->rb_node;
1692+ struct rb_node *parent = NULL;
1693+ struct ovl_cache_entry *p;
1694+
1695+ while (*newp) {
1696+ int cmp;
1697+ struct ovl_cache_entry *tmp;
1698+
1699+ parent = *newp;
1700+ tmp = ovl_cache_entry_from_node(*newp);
1701+ cmp = strncmp(name, tmp->name, len);
1702+ if (cmp > 0)
1703+ newp = &tmp->node.rb_right;
1704+ else if (cmp < 0 || len < tmp->len)
1705+ newp = &tmp->node.rb_left;
1706+ else
1707+ return 0;
1708+ }
1709+
1710+ p = ovl_cache_entry_new(name, len, ino, d_type);
1711+ if (p == NULL)
1712+ return -ENOMEM;
1713+
1714+ list_add_tail(&p->l_node, rdd->list);
1715+ rb_link_node(&p->node, parent, newp);
1716+ rb_insert_color(&p->node, rdd->root);
1717+
1718+ return 0;
1719+}
1720+
392086de
AM
1721+static int ovl_fill_lower(struct ovl_readdir_data *rdd,
1722+ const char *name, int namelen,
1723+ loff_t offset, u64 ino, unsigned int d_type)
68184a5a 1724+{
68184a5a
JR
1725+ struct ovl_cache_entry *p;
1726+
68184a5a
JR
1727+ p = ovl_cache_entry_find(rdd->root, name, namelen);
1728+ if (p) {
1729+ list_move_tail(&p->l_node, rdd->middle);
1730+ } else {
1731+ p = ovl_cache_entry_new(name, namelen, ino, d_type);
1732+ if (p == NULL)
1733+ rdd->err = -ENOMEM;
1734+ else
1735+ list_add_tail(&p->l_node, rdd->middle);
1736+ }
1737+
1738+ return rdd->err;
1739+}
1740+
1741+static void ovl_cache_free(struct list_head *list)
1742+{
1743+ struct ovl_cache_entry *p;
1744+ struct ovl_cache_entry *n;
1745+
1746+ list_for_each_entry_safe(p, n, list, l_node)
1747+ kfree(p);
1748+
1749+ INIT_LIST_HEAD(list);
1750+}
1751+
392086de 1752+static int ovl_fill_merge(void *buf, const char *name, int namelen,
68184a5a
JR
1753+ loff_t offset, u64 ino, unsigned int d_type)
1754+{
1755+ struct ovl_readdir_data *rdd = buf;
1756+
1757+ rdd->count++;
392086de
AM
1758+ if (!rdd->is_merge)
1759+ return ovl_cache_entry_add_rb(rdd, name, namelen, ino, d_type);
1760+ else
1761+ return ovl_fill_lower(rdd, name, namelen, offset, ino, d_type);
68184a5a
JR
1762+}
1763+
1764+static inline int ovl_dir_read(struct path *realpath,
392086de 1765+ struct ovl_readdir_data *rdd)
68184a5a
JR
1766+{
1767+ struct file *realfile;
1768+ int err;
1769+
1770+ realfile = ovl_path_open(realpath, O_RDONLY | O_DIRECTORY);
1771+ if (IS_ERR(realfile))
1772+ return PTR_ERR(realfile);
1773+
392086de 1774+ rdd->ctx.pos = 0;
68184a5a
JR
1775+ do {
1776+ rdd->count = 0;
1777+ rdd->err = 0;
392086de 1778+ err = iterate_dir(realfile, &rdd->ctx);
68184a5a
JR
1779+ if (err >= 0)
1780+ err = rdd->err;
1781+ } while (!err && rdd->count);
1782+ fput(realfile);
1783+
1784+ return 0;
1785+}
1786+
1787+static void ovl_dir_reset(struct file *file)
1788+{
1789+ struct ovl_dir_file *od = file->private_data;
1790+ enum ovl_path_type type = ovl_path_type(file->f_path.dentry);
1791+
1792+ if (ovl_dentry_version_get(file->f_path.dentry) != od->cache_version) {
1793+ list_del_init(&od->cursor);
1794+ ovl_cache_free(&od->cache);
1795+ od->is_cached = false;
1796+ }
1797+ WARN_ON(!od->is_real && type != OVL_PATH_MERGE);
1798+ if (od->is_real && type == OVL_PATH_MERGE) {
1799+ fput(od->realfile);
1800+ od->realfile = NULL;
1801+ od->is_real = false;
1802+ }
1803+}
1804+
1805+static int ovl_dir_mark_whiteouts(struct ovl_readdir_data *rdd)
1806+{
1807+ struct ovl_cache_entry *p;
1808+ struct dentry *dentry;
1809+ const struct cred *old_cred;
1810+ struct cred *override_cred;
1811+
1812+ override_cred = prepare_creds();
1813+ if (!override_cred) {
1814+ ovl_cache_free(rdd->list);
1815+ return -ENOMEM;
1816+ }
1817+
1818+ /*
1819+ * CAP_SYS_ADMIN for getxattr
1820+ * CAP_DAC_OVERRIDE for lookup
1821+ */
1822+ cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
1823+ cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
1824+ old_cred = override_creds(override_cred);
1825+
1826+ mutex_lock(&rdd->dir->d_inode->i_mutex);
1827+ list_for_each_entry(p, rdd->list, l_node) {
1828+ if (p->type != DT_LNK)
1829+ continue;
1830+
1831+ dentry = lookup_one_len(p->name, rdd->dir, p->len);
1832+ if (IS_ERR(dentry))
1833+ continue;
1834+
1835+ p->is_whiteout = ovl_is_whiteout(dentry);
1836+ dput(dentry);
1837+ }
1838+ mutex_unlock(&rdd->dir->d_inode->i_mutex);
1839+
1840+ revert_creds(old_cred);
1841+ put_cred(override_cred);
1842+
1843+ return 0;
1844+}
1845+
1846+static inline int ovl_dir_read_merged(struct path *upperpath,
1847+ struct path *lowerpath,
392086de 1848+ struct list_head *list)
68184a5a
JR
1849+{
1850+ int err;
1851+ struct rb_root root = RB_ROOT;
1852+ struct list_head middle;
392086de
AM
1853+ struct ovl_readdir_data rdd = {
1854+ .ctx.actor = ovl_fill_merge,
1855+ .list = list,
1856+ .root = &root,
1857+ .is_merge = false,
1858+ };
68184a5a 1859+
68184a5a 1860+ if (upperpath->dentry) {
392086de
AM
1861+ rdd.dir = upperpath->dentry;
1862+ err = ovl_dir_read(upperpath, &rdd);
68184a5a
JR
1863+ if (err)
1864+ goto out;
1865+
392086de 1866+ err = ovl_dir_mark_whiteouts(&rdd);
68184a5a
JR
1867+ if (err)
1868+ goto out;
1869+ }
1870+ /*
1871+ * Insert lowerpath entries before upperpath ones, this allows
1872+ * offsets to be reasonably constant
1873+ */
392086de
AM
1874+ list_add(&middle, rdd.list);
1875+ rdd.middle = &middle;
1876+ rdd.is_merge = true;
1877+ err = ovl_dir_read(lowerpath, &rdd);
68184a5a
JR
1878+ list_del(&middle);
1879+out:
68184a5a
JR
1880+ return err;
1881+}
1882+
1883+static void ovl_seek_cursor(struct ovl_dir_file *od, loff_t pos)
1884+{
1885+ struct list_head *l;
1886+ loff_t off;
1887+
1888+ l = od->cache.next;
1889+ for (off = 0; off < pos; off++) {
1890+ if (l == &od->cache)
1891+ break;
1892+ l = l->next;
1893+ }
1894+ list_move_tail(&od->cursor, l);
1895+}
1896+
392086de 1897+static int ovl_iterate(struct file *file, struct dir_context *ctx)
68184a5a
JR
1898+{
1899+ struct ovl_dir_file *od = file->private_data;
1900+ int res;
1901+
392086de 1902+ if (!ctx->pos)
68184a5a
JR
1903+ ovl_dir_reset(file);
1904+
1905+ if (od->is_real) {
392086de 1906+ res = iterate_dir(od->realfile, ctx);
68184a5a
JR
1907+
1908+ return res;
1909+ }
1910+
1911+ if (!od->is_cached) {
1912+ struct path lowerpath;
1913+ struct path upperpath;
68184a5a
JR
1914+
1915+ ovl_path_lower(file->f_path.dentry, &lowerpath);
1916+ ovl_path_upper(file->f_path.dentry, &upperpath);
1917+
392086de 1918+ res = ovl_dir_read_merged(&upperpath, &lowerpath, &od->cache);
68184a5a 1919+ if (res) {
392086de 1920+ ovl_cache_free(&od->cache);
68184a5a
JR
1921+ return res;
1922+ }
1923+
1924+ od->cache_version = ovl_dentry_version_get(file->f_path.dentry);
1925+ od->is_cached = true;
1926+
392086de 1927+ ovl_seek_cursor(od, ctx->pos);
68184a5a
JR
1928+ }
1929+
1930+ while (od->cursor.next != &od->cache) {
68184a5a
JR
1931+ struct ovl_cache_entry *p;
1932+
1933+ p = list_entry(od->cursor.next, struct ovl_cache_entry, l_node);
68184a5a 1934+ if (!p->is_whiteout) {
392086de 1935+ if (!dir_emit(ctx, p->name, p->len, p->ino, p->type))
68184a5a
JR
1936+ break;
1937+ }
392086de 1938+ ctx->pos++;
68184a5a
JR
1939+ list_move(&od->cursor, &p->l_node);
1940+ }
1941+
1942+ return 0;
1943+}
1944+
1945+static loff_t ovl_dir_llseek(struct file *file, loff_t offset, int origin)
1946+{
1947+ loff_t res;
1948+ struct ovl_dir_file *od = file->private_data;
1949+
c06a8ce3 1950+ mutex_lock(&file_inode(file)->i_mutex);
68184a5a
JR
1951+ if (!file->f_pos)
1952+ ovl_dir_reset(file);
1953+
1954+ if (od->is_real) {
1955+ res = vfs_llseek(od->realfile, offset, origin);
1956+ file->f_pos = od->realfile->f_pos;
1957+ } else {
1958+ res = -EINVAL;
1959+
1960+ switch (origin) {
1961+ case SEEK_CUR:
1962+ offset += file->f_pos;
1963+ break;
1964+ case SEEK_SET:
1965+ break;
1966+ default:
1967+ goto out_unlock;
1968+ }
1969+ if (offset < 0)
1970+ goto out_unlock;
1971+
1972+ if (offset != file->f_pos) {
1973+ file->f_pos = offset;
1974+ if (od->is_cached)
1975+ ovl_seek_cursor(od, offset);
1976+ }
1977+ res = offset;
1978+ }
1979+out_unlock:
c06a8ce3 1980+ mutex_unlock(&file_inode(file)->i_mutex);
68184a5a
JR
1981+
1982+ return res;
1983+}
1984+
1985+static int ovl_dir_fsync(struct file *file, loff_t start, loff_t end,
1986+ int datasync)
1987+{
1988+ struct ovl_dir_file *od = file->private_data;
1989+
1990+ /* May need to reopen directory if it got copied up */
1991+ if (!od->realfile) {
1992+ struct path upperpath;
1993+
1994+ ovl_path_upper(file->f_path.dentry, &upperpath);
1995+ od->realfile = ovl_path_open(&upperpath, O_RDONLY);
1996+ if (IS_ERR(od->realfile))
1997+ return PTR_ERR(od->realfile);
1998+ }
1999+
2000+ return vfs_fsync_range(od->realfile, start, end, datasync);
2001+}
2002+
2003+static int ovl_dir_release(struct inode *inode, struct file *file)
2004+{
2005+ struct ovl_dir_file *od = file->private_data;
2006+
2007+ list_del(&od->cursor);
2008+ ovl_cache_free(&od->cache);
2009+ if (od->realfile)
2010+ fput(od->realfile);
2011+ kfree(od);
2012+
2013+ return 0;
2014+}
2015+
2016+static int ovl_dir_open(struct inode *inode, struct file *file)
2017+{
2018+ struct path realpath;
2019+ struct file *realfile;
2020+ struct ovl_dir_file *od;
2021+ enum ovl_path_type type;
2022+
2023+ od = kzalloc(sizeof(struct ovl_dir_file), GFP_KERNEL);
2024+ if (!od)
2025+ return -ENOMEM;
2026+
2027+ type = ovl_path_real(file->f_path.dentry, &realpath);
2028+ realfile = ovl_path_open(&realpath, file->f_flags);
2029+ if (IS_ERR(realfile)) {
2030+ kfree(od);
2031+ return PTR_ERR(realfile);
2032+ }
2033+ INIT_LIST_HEAD(&od->cache);
2034+ INIT_LIST_HEAD(&od->cursor);
2035+ od->is_cached = false;
2036+ od->realfile = realfile;
2037+ od->is_real = (type != OVL_PATH_MERGE);
2038+ file->private_data = od;
2039+
2040+ return 0;
2041+}
2042+
2043+const struct file_operations ovl_dir_operations = {
2044+ .read = generic_read_dir,
2045+ .open = ovl_dir_open,
392086de 2046+ .iterate = ovl_iterate,
68184a5a
JR
2047+ .llseek = ovl_dir_llseek,
2048+ .fsync = ovl_dir_fsync,
2049+ .release = ovl_dir_release,
2050+};
2051+
2052+static int ovl_check_empty_dir(struct dentry *dentry, struct list_head *list)
2053+{
2054+ int err;
2055+ struct path lowerpath;
2056+ struct path upperpath;
2057+ struct ovl_cache_entry *p;
68184a5a
JR
2058+
2059+ ovl_path_upper(dentry, &upperpath);
2060+ ovl_path_lower(dentry, &lowerpath);
2061+
392086de 2062+ err = ovl_dir_read_merged(&upperpath, &lowerpath, list);
68184a5a
JR
2063+ if (err)
2064+ return err;
2065+
2066+ err = 0;
2067+
2068+ list_for_each_entry(p, list, l_node) {
2069+ if (p->is_whiteout)
2070+ continue;
2071+
2072+ if (p->name[0] == '.') {
2073+ if (p->len == 1)
2074+ continue;
2075+ if (p->len == 2 && p->name[1] == '.')
2076+ continue;
2077+ }
2078+ err = -ENOTEMPTY;
2079+ break;
2080+ }
2081+
2082+ return err;
2083+}
2084+
2085+static int ovl_remove_whiteouts(struct dentry *dir, struct list_head *list)
2086+{
2087+ struct path upperpath;
2088+ struct dentry *upperdir;
2089+ struct ovl_cache_entry *p;
2090+ const struct cred *old_cred;
2091+ struct cred *override_cred;
2092+ int err;
2093+
2094+ ovl_path_upper(dir, &upperpath);
2095+ upperdir = upperpath.dentry;
2096+
2097+ override_cred = prepare_creds();
2098+ if (!override_cred)
2099+ return -ENOMEM;
2100+
2101+ /*
2102+ * CAP_DAC_OVERRIDE for lookup and unlink
2103+ * CAP_SYS_ADMIN for setxattr of "trusted" namespace
2104+ * CAP_FOWNER for unlink in sticky directory
2105+ */
2106+ cap_raise(override_cred->cap_effective, CAP_DAC_OVERRIDE);
2107+ cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2108+ cap_raise(override_cred->cap_effective, CAP_FOWNER);
2109+ old_cred = override_creds(override_cred);
2110+
2111+ err = vfs_setxattr(upperdir, ovl_opaque_xattr, "y", 1, 0);
2112+ if (err)
2113+ goto out_revert_creds;
2114+
2115+ mutex_lock_nested(&upperdir->d_inode->i_mutex, I_MUTEX_PARENT);
2116+ list_for_each_entry(p, list, l_node) {
2117+ struct dentry *dentry;
2118+ int ret;
2119+
2120+ if (!p->is_whiteout)
2121+ continue;
2122+
2123+ dentry = lookup_one_len(p->name, upperdir, p->len);
2124+ if (IS_ERR(dentry)) {
c06a8ce3 2125+ pr_warn(
68184a5a
JR
2126+ "overlayfs: failed to lookup whiteout %.*s: %li\n",
2127+ p->len, p->name, PTR_ERR(dentry));
2128+ continue;
2129+ }
a25ec4e9 2130+ ret = vfs_unlink(upperdir->d_inode, dentry, NULL);
68184a5a
JR
2131+ dput(dentry);
2132+ if (ret)
c06a8ce3 2133+ pr_warn(
68184a5a
JR
2134+ "overlayfs: failed to unlink whiteout %.*s: %i\n",
2135+ p->len, p->name, ret);
2136+ }
2137+ mutex_unlock(&upperdir->d_inode->i_mutex);
2138+
2139+out_revert_creds:
2140+ revert_creds(old_cred);
2141+ put_cred(override_cred);
2142+
2143+ return err;
2144+}
2145+
2146+int ovl_check_empty_and_clear(struct dentry *dentry, enum ovl_path_type type)
2147+{
2148+ int err;
2149+ LIST_HEAD(list);
2150+
2151+ err = ovl_check_empty_dir(dentry, &list);
2152+ if (!err && type == OVL_PATH_MERGE)
2153+ err = ovl_remove_whiteouts(dentry, &list);
2154+
2155+ ovl_cache_free(&list);
2156+
2157+ return err;
2158+}
514e5dae
AM
2159--- /dev/null
2160+++ b/fs/overlayfs/super.c
c06a8ce3 2161@@ -0,0 +1,612 @@
68184a5a
JR
2162+/*
2163+ *
2164+ * Copyright (C) 2011 Novell Inc.
2165+ *
2166+ * This program is free software; you can redistribute it and/or modify it
2167+ * under the terms of the GNU General Public License version 2 as published by
2168+ * the Free Software Foundation.
2169+ */
2170+
2171+#include <linux/fs.h>
2172+#include <linux/namei.h>
2173+#include <linux/xattr.h>
2174+#include <linux/security.h>
2175+#include <linux/mount.h>
2176+#include <linux/slab.h>
2177+#include <linux/parser.h>
2178+#include <linux/module.h>
2179+#include <linux/cred.h>
2180+#include <linux/sched.h>
2181+#include "overlayfs.h"
2182+
2183+MODULE_AUTHOR("Miklos Szeredi <miklos@szeredi.hu>");
2184+MODULE_DESCRIPTION("Overlay filesystem");
2185+MODULE_LICENSE("GPL");
2186+
2187+struct ovl_fs {
2188+ struct vfsmount *upper_mnt;
2189+ struct vfsmount *lower_mnt;
2190+};
2191+
2192+struct ovl_entry {
2193+ /*
2194+ * Keep "double reference" on upper dentries, so that
2195+ * d_delete() doesn't think it's OK to reset d_inode to NULL.
2196+ */
2197+ struct dentry *__upperdentry;
2198+ struct dentry *lowerdentry;
2199+ union {
2200+ struct {
2201+ u64 version;
2202+ bool opaque;
2203+ };
2204+ struct rcu_head rcu;
2205+ };
2206+};
2207+
2208+const char *ovl_whiteout_xattr = "trusted.overlay.whiteout";
2209+const char *ovl_opaque_xattr = "trusted.overlay.opaque";
2210+
2211+
2212+enum ovl_path_type ovl_path_type(struct dentry *dentry)
2213+{
2214+ struct ovl_entry *oe = dentry->d_fsdata;
2215+
2216+ if (oe->__upperdentry) {
2217+ if (oe->lowerdentry && S_ISDIR(dentry->d_inode->i_mode))
2218+ return OVL_PATH_MERGE;
2219+ else
2220+ return OVL_PATH_UPPER;
2221+ } else {
2222+ return OVL_PATH_LOWER;
2223+ }
2224+}
2225+
2226+static struct dentry *ovl_upperdentry_dereference(struct ovl_entry *oe)
2227+{
2228+ struct dentry *upperdentry = ACCESS_ONCE(oe->__upperdentry);
2229+ smp_read_barrier_depends();
2230+ return upperdentry;
2231+}
2232+
2233+void ovl_path_upper(struct dentry *dentry, struct path *path)
2234+{
2235+ struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2236+ struct ovl_entry *oe = dentry->d_fsdata;
2237+
2238+ path->mnt = ofs->upper_mnt;
2239+ path->dentry = ovl_upperdentry_dereference(oe);
2240+}
2241+
2242+void ovl_path_lower(struct dentry *dentry, struct path *path)
2243+{
2244+ struct ovl_fs *ofs = dentry->d_sb->s_fs_info;
2245+ struct ovl_entry *oe = dentry->d_fsdata;
2246+
2247+ path->mnt = ofs->lower_mnt;
2248+ path->dentry = oe->lowerdentry;
2249+}
2250+
2251+enum ovl_path_type ovl_path_real(struct dentry *dentry, struct path *path)
2252+{
2253+
2254+ enum ovl_path_type type = ovl_path_type(dentry);
2255+
2256+ if (type == OVL_PATH_LOWER)
2257+ ovl_path_lower(dentry, path);
2258+ else
2259+ ovl_path_upper(dentry, path);
2260+
2261+ return type;
2262+}
2263+
2264+struct dentry *ovl_dentry_upper(struct dentry *dentry)
2265+{
2266+ struct ovl_entry *oe = dentry->d_fsdata;
2267+
2268+ return ovl_upperdentry_dereference(oe);
2269+}
2270+
2271+struct dentry *ovl_dentry_lower(struct dentry *dentry)
2272+{
2273+ struct ovl_entry *oe = dentry->d_fsdata;
2274+
2275+ return oe->lowerdentry;
2276+}
2277+
2278+struct dentry *ovl_dentry_real(struct dentry *dentry)
2279+{
2280+ struct ovl_entry *oe = dentry->d_fsdata;
2281+ struct dentry *realdentry;
2282+
2283+ realdentry = ovl_upperdentry_dereference(oe);
2284+ if (!realdentry)
2285+ realdentry = oe->lowerdentry;
2286+
2287+ return realdentry;
2288+}
2289+
2290+struct dentry *ovl_entry_real(struct ovl_entry *oe, bool *is_upper)
2291+{
2292+ struct dentry *realdentry;
2293+
2294+ realdentry = ovl_upperdentry_dereference(oe);
2295+ if (realdentry) {
2296+ *is_upper = true;
2297+ } else {
2298+ realdentry = oe->lowerdentry;
2299+ *is_upper = false;
2300+ }
2301+ return realdentry;
2302+}
2303+
2304+bool ovl_dentry_is_opaque(struct dentry *dentry)
2305+{
2306+ struct ovl_entry *oe = dentry->d_fsdata;
2307+ return oe->opaque;
2308+}
2309+
2310+void ovl_dentry_set_opaque(struct dentry *dentry, bool opaque)
2311+{
2312+ struct ovl_entry *oe = dentry->d_fsdata;
2313+ oe->opaque = opaque;
2314+}
2315+
2316+void ovl_dentry_update(struct dentry *dentry, struct dentry *upperdentry)
2317+{
2318+ struct ovl_entry *oe = dentry->d_fsdata;
2319+
2320+ WARN_ON(!mutex_is_locked(&upperdentry->d_parent->d_inode->i_mutex));
2321+ WARN_ON(oe->__upperdentry);
2322+ BUG_ON(!upperdentry->d_inode);
2323+ smp_wmb();
2324+ oe->__upperdentry = dget(upperdentry);
2325+}
2326+
2327+void ovl_dentry_version_inc(struct dentry *dentry)
2328+{
2329+ struct ovl_entry *oe = dentry->d_fsdata;
2330+
2331+ WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2332+ oe->version++;
2333+}
2334+
2335+u64 ovl_dentry_version_get(struct dentry *dentry)
2336+{
2337+ struct ovl_entry *oe = dentry->d_fsdata;
2338+
2339+ WARN_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
2340+ return oe->version;
2341+}
2342+
2343+bool ovl_is_whiteout(struct dentry *dentry)
2344+{
2345+ int res;
2346+ char val;
2347+
2348+ if (!dentry)
2349+ return false;
2350+ if (!dentry->d_inode)
2351+ return false;
2352+ if (!S_ISLNK(dentry->d_inode->i_mode))
2353+ return false;
2354+
2355+ res = vfs_getxattr(dentry, ovl_whiteout_xattr, &val, 1);
2356+ if (res == 1 && val == 'y')
2357+ return true;
2358+
2359+ return false;
2360+}
2361+
2362+static bool ovl_is_opaquedir(struct dentry *dentry)
2363+{
2364+ int res;
2365+ char val;
2366+
2367+ if (!S_ISDIR(dentry->d_inode->i_mode))
2368+ return false;
2369+
2370+ res = vfs_getxattr(dentry, ovl_opaque_xattr, &val, 1);
2371+ if (res == 1 && val == 'y')
2372+ return true;
2373+
2374+ return false;
2375+}
2376+
2377+static void ovl_entry_free(struct rcu_head *head)
2378+{
2379+ struct ovl_entry *oe = container_of(head, struct ovl_entry, rcu);
2380+ kfree(oe);
2381+}
2382+
2383+static void ovl_dentry_release(struct dentry *dentry)
2384+{
2385+ struct ovl_entry *oe = dentry->d_fsdata;
2386+
2387+ if (oe) {
2388+ dput(oe->__upperdentry);
2389+ dput(oe->__upperdentry);
2390+ dput(oe->lowerdentry);
2391+ call_rcu(&oe->rcu, ovl_entry_free);
2392+ }
2393+}
2394+
2395+const struct dentry_operations ovl_dentry_operations = {
2396+ .d_release = ovl_dentry_release,
2397+};
2398+
2399+static struct ovl_entry *ovl_alloc_entry(void)
2400+{
2401+ return kzalloc(sizeof(struct ovl_entry), GFP_KERNEL);
2402+}
2403+
2404+static inline struct dentry *ovl_lookup_real(struct dentry *dir,
2405+ struct qstr *name)
2406+{
2407+ struct dentry *dentry;
2408+
2409+ mutex_lock(&dir->d_inode->i_mutex);
2410+ dentry = lookup_one_len(name->name, dir, name->len);
2411+ mutex_unlock(&dir->d_inode->i_mutex);
2412+
2413+ if (IS_ERR(dentry)) {
2414+ if (PTR_ERR(dentry) == -ENOENT)
2415+ dentry = NULL;
2416+ } else if (!dentry->d_inode) {
2417+ dput(dentry);
2418+ dentry = NULL;
2419+ }
2420+ return dentry;
2421+}
2422+
2423+static int ovl_do_lookup(struct dentry *dentry)
2424+{
2425+ struct ovl_entry *oe;
2426+ struct dentry *upperdir;
2427+ struct dentry *lowerdir;
2428+ struct dentry *upperdentry = NULL;
2429+ struct dentry *lowerdentry = NULL;
2430+ struct inode *inode = NULL;
2431+ int err;
2432+
2433+ err = -ENOMEM;
2434+ oe = ovl_alloc_entry();
2435+ if (!oe)
2436+ goto out;
2437+
2438+ upperdir = ovl_dentry_upper(dentry->d_parent);
2439+ lowerdir = ovl_dentry_lower(dentry->d_parent);
2440+
2441+ if (upperdir) {
2442+ upperdentry = ovl_lookup_real(upperdir, &dentry->d_name);
2443+ err = PTR_ERR(upperdentry);
2444+ if (IS_ERR(upperdentry))
2445+ goto out_put_dir;
2446+
2447+ if (lowerdir && upperdentry &&
2448+ (S_ISLNK(upperdentry->d_inode->i_mode) ||
2449+ S_ISDIR(upperdentry->d_inode->i_mode))) {
2450+ const struct cred *old_cred;
2451+ struct cred *override_cred;
2452+
2453+ err = -ENOMEM;
2454+ override_cred = prepare_creds();
2455+ if (!override_cred)
2456+ goto out_dput_upper;
2457+
2458+ /* CAP_SYS_ADMIN needed for getxattr */
2459+ cap_raise(override_cred->cap_effective, CAP_SYS_ADMIN);
2460+ old_cred = override_creds(override_cred);
2461+
2462+ if (ovl_is_opaquedir(upperdentry)) {
2463+ oe->opaque = true;
2464+ } else if (ovl_is_whiteout(upperdentry)) {
2465+ dput(upperdentry);
2466+ upperdentry = NULL;
2467+ oe->opaque = true;
2468+ }
2469+ revert_creds(old_cred);
2470+ put_cred(override_cred);
2471+ }
2472+ }
2473+ if (lowerdir && !oe->opaque) {
2474+ lowerdentry = ovl_lookup_real(lowerdir, &dentry->d_name);
2475+ err = PTR_ERR(lowerdentry);
2476+ if (IS_ERR(lowerdentry))
2477+ goto out_dput_upper;
2478+ }
2479+
2480+ if (lowerdentry && upperdentry &&
2481+ (!S_ISDIR(upperdentry->d_inode->i_mode) ||
2482+ !S_ISDIR(lowerdentry->d_inode->i_mode))) {
2483+ dput(lowerdentry);
2484+ lowerdentry = NULL;
2485+ oe->opaque = true;
2486+ }
2487+
2488+ if (lowerdentry || upperdentry) {
2489+ struct dentry *realdentry;
2490+
2491+ realdentry = upperdentry ? upperdentry : lowerdentry;
2492+ err = -ENOMEM;
2493+ inode = ovl_new_inode(dentry->d_sb, realdentry->d_inode->i_mode,
2494+ oe);
2495+ if (!inode)
2496+ goto out_dput;
c06a8ce3 2497+ ovl_copyattr(realdentry->d_inode, inode);
68184a5a
JR
2498+ }
2499+
2500+ if (upperdentry)
2501+ oe->__upperdentry = dget(upperdentry);
2502+
2503+ if (lowerdentry)
2504+ oe->lowerdentry = lowerdentry;
2505+
2506+ dentry->d_fsdata = oe;
2507+ dentry->d_op = &ovl_dentry_operations;
2508+ d_add(dentry, inode);
2509+
2510+ return 0;
2511+
2512+out_dput:
2513+ dput(lowerdentry);
2514+out_dput_upper:
2515+ dput(upperdentry);
2516+out_put_dir:
2517+ kfree(oe);
2518+out:
2519+ return err;
2520+}
2521+
2522+struct dentry *ovl_lookup(struct inode *dir, struct dentry *dentry,
2523+ unsigned int flags)
2524+{
2525+ int err = ovl_do_lookup(dentry);
2526+
2527+ if (err)
2528+ return ERR_PTR(err);
2529+
2530+ return NULL;
2531+}
2532+
2533+struct file *ovl_path_open(struct path *path, int flags)
2534+{
68184a5a
JR
2535+ return dentry_open(path, flags, current_cred());
2536+}
2537+
2538+static void ovl_put_super(struct super_block *sb)
2539+{
2540+ struct ovl_fs *ufs = sb->s_fs_info;
2541+
2542+ if (!(sb->s_flags & MS_RDONLY))
2543+ mnt_drop_write(ufs->upper_mnt);
2544+
2545+ mntput(ufs->upper_mnt);
2546+ mntput(ufs->lower_mnt);
2547+
2548+ kfree(ufs);
2549+}
2550+
2551+static int ovl_remount_fs(struct super_block *sb, int *flagsp, char *data)
2552+{
2553+ int flags = *flagsp;
2554+ struct ovl_fs *ufs = sb->s_fs_info;
2555+
2556+ /* When remounting rw or ro, we need to adjust the write access to the
2557+ * upper fs.
2558+ */
2559+ if (((flags ^ sb->s_flags) & MS_RDONLY) == 0)
2560+ /* No change to readonly status */
2561+ return 0;
2562+
2563+ if (flags & MS_RDONLY) {
2564+ mnt_drop_write(ufs->upper_mnt);
2565+ return 0;
2566+ } else
2567+ return mnt_want_write(ufs->upper_mnt);
2568+}
2569+
2570+static const struct super_operations ovl_super_operations = {
2571+ .put_super = ovl_put_super,
2572+ .remount_fs = ovl_remount_fs,
2573+};
2574+
2575+struct ovl_config {
2576+ char *lowerdir;
2577+ char *upperdir;
2578+};
2579+
2580+enum {
c06a8ce3
AM
2581+ OPT_LOWERDIR,
2582+ OPT_UPPERDIR,
2583+ OPT_ERR,
68184a5a
JR
2584+};
2585+
2586+static const match_table_t ovl_tokens = {
c06a8ce3
AM
2587+ {OPT_LOWERDIR, "lowerdir=%s"},
2588+ {OPT_UPPERDIR, "upperdir=%s"},
2589+ {OPT_ERR, NULL}
68184a5a
JR
2590+};
2591+
2592+static int ovl_parse_opt(char *opt, struct ovl_config *config)
2593+{
2594+ char *p;
2595+
2596+ config->upperdir = NULL;
2597+ config->lowerdir = NULL;
2598+
2599+ while ((p = strsep(&opt, ",")) != NULL) {
2600+ int token;
2601+ substring_t args[MAX_OPT_ARGS];
2602+
2603+ if (!*p)
2604+ continue;
2605+
2606+ token = match_token(p, ovl_tokens, args);
2607+ switch (token) {
c06a8ce3 2608+ case OPT_UPPERDIR:
68184a5a
JR
2609+ kfree(config->upperdir);
2610+ config->upperdir = match_strdup(&args[0]);
2611+ if (!config->upperdir)
2612+ return -ENOMEM;
2613+ break;
2614+
c06a8ce3 2615+ case OPT_LOWERDIR:
68184a5a
JR
2616+ kfree(config->lowerdir);
2617+ config->lowerdir = match_strdup(&args[0]);
2618+ if (!config->lowerdir)
2619+ return -ENOMEM;
2620+ break;
2621+
2622+ default:
2623+ return -EINVAL;
2624+ }
2625+ }
2626+ return 0;
2627+}
2628+
2629+static int ovl_fill_super(struct super_block *sb, void *data, int silent)
2630+{
2631+ struct path lowerpath;
2632+ struct path upperpath;
2633+ struct inode *root_inode;
2634+ struct dentry *root_dentry;
2635+ struct ovl_entry *oe;
2636+ struct ovl_fs *ufs;
2637+ struct ovl_config config;
2638+ int err;
2639+
2640+ err = ovl_parse_opt((char *) data, &config);
2641+ if (err)
2642+ goto out;
2643+
2644+ err = -EINVAL;
2645+ if (!config.upperdir || !config.lowerdir) {
c06a8ce3 2646+ pr_err("overlayfs: missing upperdir or lowerdir\n");
68184a5a
JR
2647+ goto out_free_config;
2648+ }
2649+
2650+ err = -ENOMEM;
2651+ ufs = kmalloc(sizeof(struct ovl_fs), GFP_KERNEL);
2652+ if (!ufs)
2653+ goto out_free_config;
2654+
2655+ oe = ovl_alloc_entry();
2656+ if (oe == NULL)
2657+ goto out_free_ufs;
2658+
2659+ err = kern_path(config.upperdir, LOOKUP_FOLLOW, &upperpath);
2660+ if (err)
2661+ goto out_free_oe;
2662+
2663+ err = kern_path(config.lowerdir, LOOKUP_FOLLOW, &lowerpath);
2664+ if (err)
2665+ goto out_put_upperpath;
2666+
2667+ err = -ENOTDIR;
2668+ if (!S_ISDIR(upperpath.dentry->d_inode->i_mode) ||
2669+ !S_ISDIR(lowerpath.dentry->d_inode->i_mode))
2670+ goto out_put_lowerpath;
2671+
2672+ ufs->upper_mnt = clone_private_mount(&upperpath);
2673+ err = PTR_ERR(ufs->upper_mnt);
2674+ if (IS_ERR(ufs->upper_mnt)) {
c06a8ce3 2675+ pr_err("overlayfs: failed to clone upperpath\n");
68184a5a
JR
2676+ goto out_put_lowerpath;
2677+ }
2678+
2679+ ufs->lower_mnt = clone_private_mount(&lowerpath);
2680+ err = PTR_ERR(ufs->lower_mnt);
2681+ if (IS_ERR(ufs->lower_mnt)) {
c06a8ce3 2682+ pr_err("overlayfs: failed to clone lowerpath\n");
68184a5a
JR
2683+ goto out_put_upper_mnt;
2684+ }
2685+
2686+ /*
2687+ * Make lower_mnt R/O. That way fchmod/fchown on lower file
2688+ * will fail instead of modifying lower fs.
2689+ */
2690+ ufs->lower_mnt->mnt_flags |= MNT_READONLY;
2691+
2692+ /* If the upper fs is r/o, we mark overlayfs r/o too */
2693+ if (ufs->upper_mnt->mnt_sb->s_flags & MS_RDONLY)
2694+ sb->s_flags |= MS_RDONLY;
2695+
2696+ if (!(sb->s_flags & MS_RDONLY)) {
2697+ err = mnt_want_write(ufs->upper_mnt);
2698+ if (err)
2699+ goto out_put_lower_mnt;
2700+ }
2701+
2702+ err = -ENOMEM;
2703+ root_inode = ovl_new_inode(sb, S_IFDIR, oe);
2704+ if (!root_inode)
2705+ goto out_drop_write;
2706+
2707+ root_dentry = d_make_root(root_inode);
2708+ if (!root_dentry)
2709+ goto out_drop_write;
2710+
2711+ mntput(upperpath.mnt);
2712+ mntput(lowerpath.mnt);
2713+
2714+ oe->__upperdentry = dget(upperpath.dentry);
2715+ oe->lowerdentry = lowerpath.dentry;
2716+
2717+ root_dentry->d_fsdata = oe;
2718+ root_dentry->d_op = &ovl_dentry_operations;
2719+
2720+ sb->s_op = &ovl_super_operations;
2721+ sb->s_root = root_dentry;
2722+ sb->s_fs_info = ufs;
2723+
2724+ return 0;
2725+
2726+out_drop_write:
2727+ if (!(sb->s_flags & MS_RDONLY))
2728+ mnt_drop_write(ufs->upper_mnt);
2729+out_put_lower_mnt:
2730+ mntput(ufs->lower_mnt);
2731+out_put_upper_mnt:
2732+ mntput(ufs->upper_mnt);
2733+out_put_lowerpath:
2734+ path_put(&lowerpath);
2735+out_put_upperpath:
2736+ path_put(&upperpath);
2737+out_free_oe:
2738+ kfree(oe);
2739+out_free_ufs:
2740+ kfree(ufs);
2741+out_free_config:
2742+ kfree(config.lowerdir);
2743+ kfree(config.upperdir);
2744+out:
2745+ return err;
2746+}
2747+
2748+static struct dentry *ovl_mount(struct file_system_type *fs_type, int flags,
2749+ const char *dev_name, void *raw_data)
2750+{
2751+ return mount_nodev(fs_type, flags, raw_data, ovl_fill_super);
2752+}
2753+
2754+static struct file_system_type ovl_fs_type = {
2755+ .owner = THIS_MODULE,
2756+ .name = "overlayfs",
2757+ .mount = ovl_mount,
2758+ .kill_sb = kill_anon_super,
2759+};
c06a8ce3 2760+MODULE_ALIAS_FS("overlayfs");
68184a5a
JR
2761+
2762+static int __init ovl_init(void)
2763+{
2764+ return register_filesystem(&ovl_fs_type);
2765+}
2766+
2767+static void __exit ovl_exit(void)
2768+{
2769+ unregister_filesystem(&ovl_fs_type);
2770+}
2771+
2772+module_init(ovl_init);
2773+module_exit(ovl_exit);
This page took 0.539924 seconds and 4 git commands to generate.