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_path() and its supporting routines. --- fs/union.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++++ include/linux/union.h | 2 + 2 files changed, 174 insertions(+), 0 deletions(-) diff --git a/fs/union.c b/fs/union.c index e2384ad..2b12085 100644 --- a/fs/union.c +++ b/fs/union.c @@ -26,6 +26,7 @@ #include <linux/namei.h> #include <linux/file.h> #include <linux/security.h> +#include <linux/splice.h> /* * This is borrowed from fs/inode.c. The hashtable for lookups. Somebody @@ -633,3 +634,174 @@ out_fput: mnt_drop_write(topmost_path->mnt); return res; } + +/** + * union_create_file + * + * @parent: path of the parent dir + * @old: 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 path *parent, struct path *old, + struct dentry *new) +{ + BUG_ON(!mutex_is_locked(&parent->dentry->d_inode->i_mutex)); + + /* + * XXX - Need to initialize real nameidata, or else pass it + * all the way through from original user_path_and_parent() + * lookup. + */ + return vfs_create(parent->dentry->d_inode, new, + old->dentry->d_inode->i_mode, NULL); +} + +/** + * union_copyup_data - Copy up len bytes of old's data to new + * + * @old: source file + * @new: target file + * @len: number of bytes to copy + */ + +static int union_copyup_data(struct path *old, struct vfsmount *new_mnt, + struct dentry *new_dentry, size_t len) +{ + struct file *old_file; + struct file *new_file; + const struct cred *cred = current_cred(); + loff_t offset = 0; + long bytes; + int error; + + if (len == 0) + return 0; + + /* Get reference to balance later fput() */ + path_get(old); + old_file = dentry_open(old->dentry, old->mnt, O_RDONLY, cred); + if (IS_ERR(old_file)) + return PTR_ERR(old_file); + + dget(new_dentry); + mntget(new_mnt); + 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(old_file, &offset, new_file, len, + SPLICE_F_MOVE); + if (bytes < 0) + error = bytes; + + fput(new_file); +out_fput: + fput(old_file); + return error; +} + +/** + * union_copyup_path_len - Copy up a file and len bytes of data + * + * @parent: parent directory's path + * @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(). + */ + +int union_copyup_path_len(struct path *parent, struct path *path, size_t len) +{ + struct dentry *dentry; + 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; + goto out_newpath; + } + /* Create file */ + error = union_create_file(parent, path, dentry); + if (error) + goto out_dput; + /* Copyup data */ + error = union_copyup_data(path, parent->mnt, dentry, len); + if (error) { + /* Most likely error: ENOSPC */ + vfs_unlink(parent->dentry->d_inode, dentry); + goto out_dput; + } + /* XXX Copyup xattrs and any other dangly bits */ +out_newpath: + /* path_put() of original must happen before we copy in new */ + 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; +} + +/** + * union_copyup_path - Copy up a file given its path (and its parent's) + * + * @parent: parent directory's path + * @path: path of file to be copied up + * @newpath: return path of newly copied up file + */ + +int union_copyup_path(struct path *parent, struct path *path) +{ + size_t len; + int error; + + if (!IS_MNT_UNION(parent->mnt)) + return 0; + if (parent->mnt == path->mnt) + return 0; + + /* Catch swapped arguments. At this point, path can only be a file. */ + BUG_ON(!S_ISDIR(parent->dentry->d_inode->i_mode)); + BUG_ON(!S_ISREG(path->dentry->d_inode->i_mode)); + + mutex_lock(&parent->dentry->d_inode->i_mutex); + if (IS_DEADDIR(parent->dentry->d_inode)) { + error = -ENOENT; + goto out_unlock; + } + + /* XXX - need more locking than parent i_mutex? */ + len = i_size_read(path->dentry->d_inode); + if (((size_t)len != len) || ((ssize_t)len != len)) { + error = -EFBIG; + goto out_unlock; + } + + error = union_copyup_path_len(parent, path, len); +out_unlock: + mutex_unlock(&parent->dentry->d_inode->i_mutex); + return error; +} diff --git a/include/linux/union.h b/include/linux/union.h index 66deeb2..5ff59c6 100644 --- a/include/linux/union.h +++ b/include/linux/union.h @@ -52,6 +52,7 @@ extern struct dentry * union_create_topmost_dir(struct path *, struct qstr *, extern int attach_mnt_union(struct vfsmount *, struct vfsmount *); extern void detach_mnt_union(struct vfsmount *); extern int union_copyup_dir(struct path *); +extern int union_copyup_path(struct path *, struct path *); #else /* CONFIG_UNION_MOUNT */ @@ -66,6 +67,7 @@ extern int union_copyup_dir(struct path *); #define attach_mnt_union(x, y) do { } while (0) #define detach_mnt_union(x) do { } while (0) #define union_copyup_dir(x) ({ BUG(); (0); }) +#define union_copyup_path(x, y) ({ BUG(); (NULL); }) #endif /* CONFIG_UNION_MOUNT */ #endif /* __KERNEL__ */ -- 1.6.3.3 -- To unsubscribe from this list: send the line "unsubscribe linux-fsdevel" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html