From patchwork Fri Aug 23 16:27:45 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Mark Harmstone X-Patchwork-Id: 13775575 Received: from mx0a-00082601.pphosted.com (mx0a-00082601.pphosted.com [67.231.145.42]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 9104E18BB84 for ; Fri, 23 Aug 2024 16:28:38 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=67.231.145.42 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724430520; cv=none; b=pOxCwtrd8pW8vZzlDn7FfvzqLwqM3jV7wO6oL2zH2d6qbsSoLHu/fO1CWqk9B3nYCbHbyUnkRA51gOtE5oyj/xVfWknHUfoTg9IRGSu5zrprrL9iDgcgtYNKEzoCpvZTXZryk6C1rlTUxBdQ9A/85agrd0zuFbi4h8PeSquE3GA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1724430520; c=relaxed/simple; bh=KwMYlfHX/PmGkdiIuzM7kl/6Up3b28z6Ru4Z1UgrjYk=; h=From:To:CC:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Type; b=rNk5uLXX22/NKOLopYwuRtLALNWfCWy8pi18mjbF0Z0w4Q4l6il6gqjQBvEz+KTn79wtd6IYZDmPExyRC/wc9vxxoCR+XXipuDVJ2g/kC0ZAiD0ThZ0oQBEkjUP25IKNaGVrEpylVZjAyBQ9hkdbDAFqG1LJFHQ3TSoRuDopTzc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com; spf=pass smtp.mailfrom=meta.com; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b=d/ghgaSc; arc=none smtp.client-ip=67.231.145.42 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=fb.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=meta.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=fb.com header.i=@fb.com header.b="d/ghgaSc" Received: from pps.filterd (m0044010.ppops.net [127.0.0.1]) by mx0a-00082601.pphosted.com (8.18.1.2/8.18.1.2) with ESMTP id 47NETb5Y003019 for ; Fri, 23 Aug 2024 09:28:38 -0700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=fb.com; h=from :to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding:content-type; s= facebook; bh=Adr9di2edXlvFwPXO+C8IEXp4Yyjc8TC9nOMa63S1Q8=; b=d/g hgaScy/yGzn/LY2qjWIed0vb+vHdqoQCx+4spohVMIX3cCP9xMXwT5/iZgXjPR4p JWGrp0FahRV/j0MWneuVvPdvyYG9G7vo4EvbKHxO2MnT2y/ZyRnFtJjIPkPpfse3 Z8v3WEO1KmKLhDJwnQhTkO5rP7Q5DZf/Xu6B8ShA= Received: from maileast.thefacebook.com ([163.114.130.16]) by mx0a-00082601.pphosted.com (PPS) with ESMTPS id 416v82gt3g-6 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128 verify=NOT) for ; Fri, 23 Aug 2024 09:28:37 -0700 (PDT) Received: from twshared18321.17.frc2.facebook.com (2620:10d:c0a8:1b::2d) by mail.thefacebook.com (2620:10d:c0a9:6f::8fd4) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.2.1544.11; Fri, 23 Aug 2024 16:28:28 +0000 Received: by devbig276.nha1.facebook.com (Postfix, from userid 660015) id 46E7C5CB7F7B; Fri, 23 Aug 2024 17:28:13 +0100 (BST) From: Mark Harmstone To: , CC: Mark Harmstone Subject: [PATCH 3/6] btrfs: add btrfs_encoded_read_finish Date: Fri, 23 Aug 2024 17:27:45 +0100 Message-ID: <20240823162810.1668399-4-maharmstone@fb.com> X-Mailer: git-send-email 2.43.5 In-Reply-To: <20240823162810.1668399-1-maharmstone@fb.com> References: <20240823162810.1668399-1-maharmstone@fb.com> Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 X-FB-Internal: Safe X-Proofpoint-GUID: UnTtQLhReAlMKgzMdHZtkJOYzAAwh0pv X-Proofpoint-ORIG-GUID: UnTtQLhReAlMKgzMdHZtkJOYzAAwh0pv X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.293,Aquarius:18.0.1039,Hydra:6.0.680,FMLib:17.12.28.16 definitions=2024-08-23_13,2024-08-22_01,2024-05-17_01 Move the end of btrfs_ioctl_encoded_read, responsible for copying to userspace and cleanup, into its own function. Signed-off-by: Mark Harmstone --- 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)