From: David Howells <dhowells@redhat.com>
To: linux-fsdevel@vger.kernel.org, viro@ZenIV.linux.org.uk,
valerie.aurora@gmail.com
Cc: linux-kernel@vger.kernel.org,
David Howells <dhowells@redhat.com> (Further development)
Subject: [PATCH 56/73] union-mount: In-kernel file copyup routines [ver #2]
Date: Tue, 21 Feb 2012 18:04:30 +0000 [thread overview]
Message-ID: <20120221180430.25235.22576.stgit@warthog.procyon.org.uk> (raw)
In-Reply-To: <20120221175721.25235.8901.stgit@warthog.procyon.org.uk>
When a file on the read-only layer of a union mount is altered, it
must be copied up to the topmost read-write layer. This patch creates
union_copyup() and its supporting routines.
Thanks to Valdis Kletnieks <Valdis.Kletnieks@vt.edu> for a bug fix.
XXX - split up
XXX - If dir xattr copyup fails, delete the newly created dir
XXX - set correct owner after copyup
XXX - reimplement with get_unlinked_inode()
Original-author: Valerie Aurora <vaurora@redhat.com>
Signed-off-by: David Howells <dhowells@redhat.com> (Further development)
---
fs/union.c | 266 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
fs/union.h | 31 +++++++
2 files changed, 296 insertions(+), 1 deletions(-)
diff --git a/fs/union.c b/fs/union.c
index 8efed50..b8ee42d 100644
--- a/fs/union.c
+++ b/fs/union.c
@@ -24,6 +24,8 @@
#include <linux/xattr.h>
#include <linux/file.h>
#include <linux/security.h>
+#include <linux/splice.h>
+#include <linux/xattr.h>
#include "union.h"
@@ -197,9 +199,16 @@ int union_create_topmost_dir(struct path *parent, struct qstr *name,
error = union_copyup_xattr(lower->dentry, topmost->dentry);
if (error)
- dput(topmost->dentry);
+ goto out_rmdir;
fsnotify_mkdir(dir, topmost->dentry);
+
+ mnt_drop_write(parent->mnt);
+
+ return 0;
+out_rmdir:
+ /* XXX rm created dir */
+ dput(topmost->dentry);
out:
mnt_drop_write(parent->mnt);
return error;
@@ -439,3 +448,258 @@ int generic_readdir_fallthru(struct dentry *topmost_dentry, const char *name,
return -ENOENT;
}
EXPORT_SYMBOL(generic_readdir_fallthru);
+
+/**
+ * union_create_file
+ * @nd: namediata for source file
+ * @lower: path of the source file
+ * @new: path of the new file, negative dentry
+ *
+ * Must already have mnt_want_write() on the mnt and the parent's i_mutex.
+ */
+static int union_create_file(struct nameidata *nd, struct path *lower,
+ struct dentry *new)
+{
+ struct path *parent = &nd->path;
+
+ BUG_ON(!mutex_is_locked(&parent->dentry->d_inode->i_mutex));
+
+ return vfs_create(parent->dentry->d_inode, new,
+ lower->dentry->d_inode->i_mode, nd);
+}
+
+/**
+ * union_create_symlink
+ * @nd: namediata for source symlink
+ * @lower: path of the source symlink
+ * @new: path of the new symlink, negative dentry
+ *
+ * Must already have mnt_want_write() on the mnt and the parent's i_mutex.
+ */
+static int union_create_symlink(struct nameidata *nd, struct path *lower,
+ struct dentry *new)
+{
+ struct path *parent = &nd->path;
+ void *cookie;
+ int error;
+
+ BUG_ON(!mutex_is_locked(&parent->dentry->d_inode->i_mutex));
+
+ /* We want the contents of this symlink, not to follow it, so this is
+ * modeled on generic_readlink() rather than do_follow_link().
+ */
+ nd->depth = 0;
+ cookie = lower->dentry->d_inode->i_op->follow_link(lower->dentry, nd);
+ if (IS_ERR(cookie))
+ return PTR_ERR(cookie);
+
+ /* Create a copy of the link on the top layer */
+ error = vfs_symlink(parent->dentry->d_inode, new, nd_get_link(nd));
+ if (lower->dentry->d_inode->i_op->put_link)
+ lower->dentry->d_inode->i_op->put_link(lower->dentry, nd, cookie);
+ return error;
+}
+
+/**
+ * union_copyup_data - Copy up len bytes of old's data to new
+ * @lower: path of source file in lower layer
+ * @new_mnt: vfsmount of target file
+ * @new_dentry: dentry of target file
+ * @len: number of bytes to copy
+ */
+static int union_copyup_data(struct path *lower, struct vfsmount *new_mnt,
+ struct dentry *new_dentry, size_t len)
+{
+ const struct cred *cred = current_cred();
+ struct file *lower_file;
+ struct file *new_file;
+ loff_t offset = 0;
+ long bytes;
+ int error = 0;
+
+ if (len == 0)
+ return 0;
+
+ /* Get reference to balance later fput() */
+ path_get(lower);
+ lower_file = dentry_open(lower->dentry, lower->mnt, O_RDONLY, cred);
+ if (IS_ERR(lower_file))
+ return PTR_ERR(lower_file);
+
+ mntget(new_mnt);
+ dget(new_dentry);
+ new_file = dentry_open(new_dentry, new_mnt, O_WRONLY, cred);
+ if (IS_ERR(new_file)) {
+ error = PTR_ERR(new_file);
+ goto out_fput;
+ }
+
+ bytes = do_splice_direct(lower_file, &offset, new_file, len,
+ SPLICE_F_MOVE);
+ if (bytes < 0)
+ error = bytes;
+
+ fput(new_file);
+out_fput:
+ fput(lower_file);
+ return error;
+}
+
+/**
+ * union_copyup_file - Copy up a regular file, symlink or special file
+ * @nd: nameidata for topmost parent dir
+ * @lower: path of file to be copied up
+ * @dentry: dentry to copy up to
+ * @len: number of bytes of file data to copy up
+ */
+int union_copyup_file(struct nameidata *nd, struct path *lower,
+ struct dentry *dentry, size_t len)
+{
+ struct path *parent = &nd->path;
+ int error;
+
+ BUG_ON(!mutex_is_locked(&parent->dentry->d_inode->i_mutex));
+
+ if (S_ISREG(lower->dentry->d_inode->i_mode)) {
+ error = union_create_file(nd, lower, dentry);
+ if (error)
+ return error;
+ error = union_copyup_data(lower, parent->mnt, dentry, len);
+ } else if (S_ISLNK(lower->dentry->d_inode->i_mode)) {
+ return union_create_symlink(nd, lower, dentry);
+ } else {
+ /* Don't currently support copyup of special files, though in
+ * theory there's no reason we couldn't at least copy up
+ * blockdev, chrdev and FIFO files
+ */
+ return -EXDEV;
+ }
+ if (error)
+ /* Most likely error: ENOSPC */
+ vfs_unlink(parent->dentry->d_inode, dentry);
+
+ return error;
+}
+
+/**
+ * __union_copyup_len - Copy up a file and len bytes of data
+ * @nd: nameidata for topmost parent dir
+ * @path: path of file to be copied up
+ * @len: number of bytes of file data to copy up
+ *
+ * Parent's i_mutex must be held by caller. Newly copied up path is
+ * returned in @path and original is path_put().
+ */
+static int __union_copyup_len(struct nameidata *nd, struct path *path,
+ size_t len)
+{
+ struct dentry *dentry;
+ struct path *parent = &nd->path;
+ int error;
+
+ BUG_ON(!mutex_is_locked(&parent->dentry->d_inode->i_mutex));
+
+ dentry = lookup_one_len(path->dentry->d_name.name, parent->dentry,
+ path->dentry->d_name.len);
+ if (IS_ERR(dentry))
+ return PTR_ERR(dentry);
+
+ if (dentry->d_inode) {
+ /* We raced with someone else and "lost". That's okay, they
+ * did all the work of copying up the file.
+ *
+ * Note that currently data copyup happens under the parent
+ * dir's i_mutex. If we move it outside that, we'll need some
+ * way of waiting for the data copyup to complete here.
+ */
+ error = 0;
+ } else {
+ error = union_copyup_file(nd, path, dentry, len);
+ if (error < 0)
+ goto out_dput;
+ }
+
+ /* Move to the new dentry */
+ path_put(path);
+ path->dentry = dentry;
+ path->mnt = mntget(parent->mnt);
+ return error;
+
+out_dput:
+ /* Don't path_put(path), let caller unwind */
+ dput(dentry);
+ return error;
+}
+
+/**
+ * do_union_copyup_len - Copy up a file given its path (and its parent's)
+ * @nd: nameidata for topmost parent dir
+ * @path: path of file to be copied up
+ * @copy_all: if set, copy all of the file's data and ignore @len
+ * @len: if @copy_all is not set, number of bytes of file data to copy up
+ *
+ * Newly copied up path is returned in @path.
+ */
+static int do_union_copyup_len(struct nameidata *nd, struct path *path,
+ int copy_all, size_t len)
+{
+ struct path *parent = &nd->path;
+ int error;
+
+ if (!IS_DIR_UNIONED(parent->dentry) ||
+ parent->mnt == path->mnt)
+ return 0;
+ if (!S_ISREG(path->dentry->d_inode->i_mode) &&
+ !S_ISLNK(path->dentry->d_inode->i_mode))
+ return 0;
+
+ BUG_ON(!S_ISDIR(parent->dentry->d_inode->i_mode));
+
+ mutex_lock(&parent->dentry->d_inode->i_mutex);
+ error = -ENOENT;
+ if (IS_DEADDIR(parent->dentry->d_inode))
+ goto out_unlock;
+
+ if (copy_all && S_ISREG(path->dentry->d_inode->i_mode)) {
+ error = -EFBIG;
+ len = i_size_read(path->dentry->d_inode);
+ /* Check for overflow of file size */
+ if ((ssize_t)len != len)
+ goto out_unlock;
+ }
+
+ error = __union_copyup_len(nd, path, len);
+
+out_unlock:
+ mutex_unlock(&parent->dentry->d_inode->i_mutex);
+ return error;
+}
+
+/*
+ * Helper function to copy up all of a file
+ */
+int union_copyup(struct nameidata *nd, struct path *path)
+{
+ return do_union_copyup_len(nd, path, 1, 0);
+}
+
+/*
+ * Unlocked helper function to copy up all of a file
+ */
+int __union_copyup(struct nameidata *nd, struct path *path)
+{
+ loff_t len;
+ len = i_size_read(path->dentry->d_inode);
+ if ((ssize_t)len != len)
+ return -EFBIG;
+
+ return __union_copyup_len(nd, path, len);
+}
+
+/*
+ * Helper function to copy up part of a file for truncate and O_TRUNC.
+ */
+int union_copyup_len(struct nameidata *nd, struct path *path, size_t len)
+{
+ return do_union_copyup_len(nd, path, 0, len);
+}
diff --git a/fs/union.h b/fs/union.h
index 46944b9..62d8ef5 100644
--- a/fs/union.h
+++ b/fs/union.h
@@ -19,6 +19,7 @@
#include <linux/mount.h>
#include <linux/dcache.h>
#include <linux/path.h>
+#include <linux/namei.h>
#include <linux/bug.h>
/*
@@ -73,6 +74,11 @@ extern int union_create_topmost_dir(struct path *, struct qstr *, struct path *,
extern int union_copyup_dir(struct path *);
extern int generic_readdir_fallthru(struct dentry *topmost_dentry, const char *name,
int namlen, ino_t *ino, unsigned char *d_type);
+extern int union_copyup(struct nameidata *, struct path *);
+extern int __union_copyup(struct nameidata *, struct path *);
+extern int union_copyup_len(struct nameidata *, struct path *, size_t len);
+extern int union_copyup_file(struct nameidata *nd, struct path *lower,
+ struct dentry *dentry, size_t len);
static inline
struct path *union_find_dir(struct dentry *dentry, unsigned int layer)
@@ -147,4 +153,29 @@ int generic_readdir_fallthru(struct dentry *topmost_dentry, const char *name,
return 0;
}
+static inline int union_copyup(struct nameidata *nd, struct path *path)
+{
+ BUG();
+ return 0;
+}
+
+static inline int __union_copyup(struct nameidata *nd, struct path *path)
+{
+ BUG();
+ return 0;
+}
+
+static inline int union_copyup_len(struct nameidata *nd, struct path *path,
+ size_t len)
+{
+ BUG();
+ return 0;
+}
+
+static inline int union_copyup_file(struct nameidata *nd, struct path *lower,
+ struct dentry *dentry, size_t len)
+{
+ return 0;
+}
+
#endif /* CONFIG_UNION_MOUNT */
next prev parent reply other threads:[~2012-02-21 19:54 UTC|newest]
Thread overview: 89+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-02-21 17:57 [RFC][PATCH 00/73] Union Mount " David Howells
2012-02-21 17:57 ` [PATCH 01/73] VFS: Make chown() and lchown() call fchownat() " David Howells
2012-02-21 17:57 ` [PATCH 02/73] VFS: Make clone_mnt()/copy_tree()/collect_mounts() return errors " David Howells
2012-02-21 17:57 ` [PATCH 03/73] VFS: Comment mount following code " David Howells
2012-02-21 17:57 ` [PATCH 04/73] VFS: Make lookup_hash() return a struct path " David Howells
2012-02-21 17:58 ` [PATCH 05/73] VFS: Pass mount flags to sget() " David Howells
2012-02-21 17:58 ` [PATCH 06/73] VFS: Split inode_permission() " David Howells
2012-02-21 17:58 ` [PATCH 07/73] VFS: Add hard read-only users count to superblock " David Howells
2012-02-21 17:58 ` [PATCH 08/73] VFS: Add CL_NO_SHARED flag to clone_mnt()/copy_tree() " David Howells
2012-02-29 6:56 ` Ram Pai
2012-02-21 17:58 ` [PATCH 09/73] VFS: Add CL_NO_SLAVE " David Howells
2012-02-29 6:58 ` Ram Pai
2012-02-21 17:58 ` [PATCH 10/73] VFS: Add CL_MAKE_HARD_READONLY " David Howells
2012-02-21 17:58 ` [PATCH 11/73] whiteout/NFSD: Don't return information about whiteouts to userspace " David Howells
2012-02-21 17:58 ` [PATCH 12/73] whiteout: Define flags and operations for opaque inodes " David Howells
2012-02-21 17:59 ` [PATCH 13/73] whiteout: Add vfs_whiteout() and whiteout inode operation " David Howells
2012-02-21 17:59 ` [PATCH 14/73] whiteout: Allow removal of a directory with whiteouts " David Howells
2012-02-21 17:59 ` [PATCH 15/73] tmpfs: Add whiteout support " David Howells
2012-02-21 17:59 ` [PATCH 16/73] VFS: Basic fallthru definitions " David Howells
2012-02-21 17:59 ` [PATCH 17/73] tmpfs: Add fallthru support " David Howells
2012-02-21 17:59 ` [PATCH 18/73] union-mount: Union mounts documentation " David Howells
2012-02-27 2:57 ` Randy Dunlap
2012-02-21 17:59 ` [PATCH 19/73] union-mount: Introduce MNT_UNION and MS_UNION flags " David Howells
2012-02-21 18:00 ` [PATCH 20/73] union-mount: Add CONFIG_UNION_MOUNT option " David Howells
2012-02-21 18:00 ` [PATCH 21/73] union-mount: Create union_stack structure " David Howells
2012-02-21 18:00 ` [PATCH 22/73] union-mount: Add two superblock fields for union mounts " David Howells
2012-02-21 18:00 ` [PATCH 23/73] union-mount: Add union_alloc() " David Howells
2012-02-21 18:00 ` [PATCH 24/73] union-mount: Add union_find_dir() " David Howells
2012-02-21 18:00 ` [PATCH 25/73] union-mount: Create d_free_unions() " David Howells
2012-02-21 18:00 ` [PATCH 26/73] union-mount: Free union stack on removal of topmost dentry from dcache " David Howells
2012-02-21 18:00 ` [PATCH 27/73] union-mount: Create union_add_dir() " David Howells
2012-02-21 18:01 ` [PATCH 28/73] union-mount: Add union_create_topmost_dir() " David Howells
2012-02-21 18:01 ` [PATCH 29/73] union-mount: Create IS_MNT_UNION() " David Howells
2012-02-21 18:01 ` [PATCH 30/73] union-mount: Create needs_lookup_union() " David Howells
2012-02-21 18:01 ` [PATCH 31/73] union-mount: Create check_topmost_union_mnt() " David Howells
2012-02-21 18:01 ` [PATCH 32/73] union-mount: Add clone_union_tree() and put_union_sb() " David Howells
2012-02-21 18:01 ` [PATCH 33/73] unionmount: Mark lower layers in union " David Howells
2012-02-21 18:01 ` [PATCH 34/73] union-mount: Create build_root_union() " David Howells
2012-02-21 18:01 ` [PATCH 35/73] union-mount: Create prepare_mnt_union() and cleanup_mnt_union() " David Howells
2012-02-21 18:02 ` [PATCH 36/73] union-mount: Prevent improper union-related remounts " David Howells
2012-02-21 18:02 ` [PATCH 37/73] union-mount: Prevent topmost file system from being mounted elsewhere " David Howells
2012-02-21 18:02 ` [PATCH 38/73] union-mount: Prevent bind mounts of union mounts " David Howells
2012-02-21 18:02 ` [PATCH 39/73] union-mount: Duplicate the i_{, dir_}mutex lock classes and use for upper layer " David Howells
2012-02-21 18:02 ` [PATCH 40/73] union-mount: Implement union mount " David Howells
2012-02-21 18:02 ` [PATCH 41/73] union-mount: Temporarily disable some syscalls " David Howells
2012-02-21 18:02 ` [PATCH 42/73] union-mount: Basic infrastructure of __lookup_union() " David Howells
2012-02-21 18:02 ` [PATCH 43/73] union-mount: Process negative dentries in " David Howells
2012-02-21 18:03 ` [PATCH 44/73] union-mount: Return files found in lower layers " David Howells
2012-02-21 18:03 ` [PATCH 45/73] union-mount: Build union stack " David Howells
2012-02-21 18:03 ` [PATCH 46/73] union-mount: Follow mount " David Howells
2012-02-21 18:03 ` [PATCH 47/73] union-mount: Add lookup_union_locked() " David Howells
2012-02-21 18:03 ` [PATCH 48/73] union-mount: Add wrapper for lookup_union_locked() and RCU hook " David Howells
2012-02-21 18:03 ` [PATCH 49/73] union-mount: Call union lookup functions in lookup path " David Howells
2012-02-21 18:03 ` [PATCH 50/73] union-mount: Create whiteout on unlink() " David Howells
2012-02-21 18:03 ` [PATCH 51/73] union-mount: Create whiteout on rmdir() " David Howells
2012-02-21 18:03 ` [PATCH 52/73] union-mount: Set opaque flag on new directories in unioned file systems " David Howells
2012-02-21 18:04 ` [PATCH 53/73] union-mount: Copy up directory entries on first readdir() " David Howells
2012-02-21 18:04 ` [PATCH 54/73] union-mount: Add generic_readdir_fallthru() helper " David Howells
2012-02-21 18:04 ` [PATCH 55/73] fallthru: tmpfs support for lookup of d_type/d_ino in fallthrus " David Howells
2012-02-21 18:04 ` David Howells [this message]
2012-02-21 18:04 ` [PATCH 57/73] VFS: Create user_path_nd() to lookup both parent and target " David Howells
2012-02-21 18:04 ` [PATCH 58/73] unionmount: Add LOOKUP_COPY_UP " David Howells
2012-02-21 18:04 ` [PATCH 59/73] unionmount: Override creds when copying up a file to correctly set ownership " David Howells
2012-02-21 18:05 ` [PATCH 60/73] union-mount: Implement union-aware access()/faccessat() " David Howells
2012-02-21 18:05 ` [PATCH 61/73] union-mount: Make various syscalls aware (link, chmod, chown, utimes & setxattr) " David Howells
2012-02-21 18:05 ` [PATCH 62/73] union-mount: Implement union-aware rename() " David Howells
2012-02-21 18:05 ` [PATCH 63/73] union-mount: Implement union-aware writable open() " David Howells
2012-02-21 18:05 ` [PATCH 64/73] union-mount: Implement union-aware truncate() " David Howells
2012-02-21 18:05 ` [PATCH 65/73] ext2: Add ext2_dirent_in_use() " David Howells
2012-02-21 18:05 ` [PATCH 66/73] ext2: Split ext2_add_entry() from ext2_add_link() " David Howells
2012-02-27 0:04 ` Ted Ts'o
2012-02-27 3:30 ` Andreas Dilger
2012-02-27 19:09 ` Ted Ts'o
2012-02-27 20:45 ` Andreas Dilger
2012-02-21 18:05 ` [PATCH 67/73] ext2: Remove target inode pointer from ext2_add_entry() " David Howells
2012-02-27 0:22 ` Ted Ts'o
2012-02-21 18:06 ` [PATCH 68/73] ext2: Add whiteout and opaque directory support " David Howells
2012-02-21 18:06 ` [PATCH 69/73] ext2: Add fallthru " David Howells
2012-02-27 0:33 ` Ted Ts'o
2012-02-21 18:06 ` [PATCH 70/73] fallthru: ext2 support for lookup of d_type/d_ino in fallthrus " David Howells
2012-02-21 18:06 ` [PATCH 71/73] jffs2: Add whiteout support " David Howells
2012-02-21 18:06 ` [PATCH 72/73] jffs2: Add fallthru " David Howells
2012-02-21 18:06 ` [PATCH 73/73] fallthru: jffs2 support for lookup of d_type/d_ino in fallthrus " David Howells
2012-02-26 6:48 ` copy-up xattr (Re: [RFC][PATCH 00/73] Union Mount [ver #2]) J. R. Okajima
2012-03-26 14:22 ` David Howells
2012-03-27 4:38 ` Casey Schaufler
2012-03-27 13:10 ` David Quigley
2012-03-27 16:37 ` Casey Schaufler
2012-03-28 14:51 ` J. R. Okajima
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20120221180430.25235.22576.stgit@warthog.procyon.org.uk \
--to=dhowells@redhat.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=valerie.aurora@gmail.com \
--cc=viro@ZenIV.linux.org.uk \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
all inboxes | Powered by JetHome®