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