Move the end of btrfs_ioctl_encoded_read, responsible for copying to userspace and cleanup, into its own function. Signed-off-by: Mark Harmstone <maharmstone@xxxxxx> --- fs/btrfs/btrfs_inode.h | 1 + fs/btrfs/inode.c | 29 +++++++++++----------------- fs/btrfs/ioctl.c | 43 ++++++++++++++++++++++++++++++------------ 3 files changed, 43 insertions(+), 30 deletions(-) diff --git a/fs/btrfs/btrfs_inode.h b/fs/btrfs/btrfs_inode.h index 5cd4308bd337..f4d77c3bb544 100644 --- a/fs/btrfs/btrfs_inode.h +++ b/fs/btrfs/btrfs_inode.h @@ -619,6 +619,7 @@ struct btrfs_encoded_read_private { struct iov_iter iter; struct btrfs_ioctl_encoded_io_args args; struct file *file; + void __user *copy_out; }; ssize_t btrfs_encoded_read(struct btrfs_encoded_read_private *priv); diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index c1292e58366a..1e53977a4854 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -9169,13 +9169,13 @@ static ssize_t btrfs_encoded_read_regular(struct btrfs_encoded_read_private *pri priv->nr_pages = DIV_ROUND_UP(disk_io_size, PAGE_SIZE); priv->pages = kcalloc(priv->nr_pages, sizeof(struct page *), GFP_NOFS); - if (!priv->pages) + if (!priv->pages) { + priv->nr_pages = 0; return -ENOMEM; + } ret = btrfs_alloc_page_array(priv->nr_pages, priv->pages, false); - if (ret) { - ret = -ENOMEM; - goto out; - } + if (ret) + return -ENOMEM; _btrfs_encoded_read_regular_fill_pages(inode, start, disk_bytenr, disk_io_size, priv); @@ -9185,7 +9185,7 @@ static ssize_t btrfs_encoded_read_regular(struct btrfs_encoded_read_private *pri ret = blk_status_to_errno(READ_ONCE(priv->status)); if (ret) - goto out; + return ret; unlock_extent(io_tree, start, lockend, &priv->cached_state); btrfs_inode_unlock(inode, BTRFS_ILOCK_SHARED); @@ -9204,22 +9204,15 @@ static ssize_t btrfs_encoded_read_regular(struct btrfs_encoded_read_private *pri PAGE_SIZE - page_offset); if (copy_page_to_iter(priv->pages[i], page_offset, bytes, - &priv->iter) != bytes) { - ret = -EFAULT; - goto out; - } + &priv->iter) != bytes) + return -EFAULT; + i++; cur += bytes; page_offset = 0; } - ret = priv->count; -out: - for (i = 0; i < priv->nr_pages; i++) { - if (priv->pages[i]) - __free_page(priv->pages[i]); - } - kfree(priv->pages); - return ret; + + return priv->count; } ssize_t btrfs_encoded_read(struct btrfs_encoded_read_private *priv) diff --git a/fs/btrfs/ioctl.c b/fs/btrfs/ioctl.c index 770bd609f386..3fa661322c26 100644 --- a/fs/btrfs/ioctl.c +++ b/fs/btrfs/ioctl.c @@ -4509,6 +4509,34 @@ static int _btrfs_ioctl_send(struct btrfs_inode *inode, void __user *argp, bool return ret; } +static ssize_t btrfs_encoded_read_finish(struct btrfs_encoded_read_private *priv, + ssize_t ret) +{ + size_t copy_end_kernel = offsetofend(struct btrfs_ioctl_encoded_io_args, + flags); + unsigned long i; + + if (ret >= 0) { + fsnotify_access(priv->file); + if (copy_to_user(priv->copy_out, + (char *)&priv->args + copy_end_kernel, + sizeof(priv->args) - copy_end_kernel)) + ret = -EFAULT; + } + + for (i = 0; i < priv->nr_pages; i++) { + if (priv->pages[i]) + __free_page(priv->pages[i]); + } + kfree(priv->pages); + kfree(priv->iov); + + if (ret > 0) + add_rchar(current, ret); + inc_syscr(current); + return ret; +} + static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp, bool compat) { @@ -4573,21 +4601,12 @@ static int btrfs_ioctl_encoded_read(struct file *file, void __user *argp, if (ret < 0) goto out; + priv.copy_out = argp + copy_end; + ret = btrfs_encoded_read(&priv); - if (ret >= 0) { - fsnotify_access(file); - if (copy_to_user(argp + copy_end, - (char *)&priv.args + copy_end_kernel, - sizeof(priv.args) - copy_end_kernel)) - ret = -EFAULT; - } out: - kfree(priv.iov); - if (ret > 0) - add_rchar(current, ret); - inc_syscr(current); - return ret; + return btrfs_encoded_read_finish(&priv, ret); } static int btrfs_ioctl_encoded_write(struct file *file, void __user *argp, bool compat) -- 2.44.2