From patchwork Fri Apr 12 15:03:15 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13627953 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 AADAE1482F3 for ; Fri, 12 Apr 2024 15:03:26 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934206; cv=none; b=XXrWeWQhYMGD7sd2E8UddRDPPoxS5jmXuSYIq9E8h+7XLf+r1O7i9zoEVrtSiVR7wbcK9bpYQ33WfJ7iPqcGj0+SdnvVCWMv2DRyNDyQIL6pChdRVOCI+umpJ3uukMq16MvlUpwXouNPkU0XHgpWBjPHAqIm2v68GeXaCQ+F5uE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934206; c=relaxed/simple; bh=pw0GEzuKXjzbv8HpOGGDW3L+JBJyEpR0E42+wlYh0Y8=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=V6bL2gSCcrwj346BBQWn7MwT1QJyjR9NRWojaX3uy8/3WjLClknyS+HGoloRdJZUk5A1tqItkzuI61lY26ScgmIV6zDyXKN4/gDVrAJQihqoqjMwBXg6XZFXK6hsur4F0wEuIMftT/0w9n+lpRSIVb5/vlfoD5+wBqE/Mo2TLgI= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=pVVCZLca; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="pVVCZLca" Received: by smtp.kernel.org (Postfix) with ESMTPSA id ABA1EC113CC for ; Fri, 12 Apr 2024 15:03:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712934206; bh=pw0GEzuKXjzbv8HpOGGDW3L+JBJyEpR0E42+wlYh0Y8=; h=From:To:Subject:Date:In-Reply-To:References:From; b=pVVCZLcaP9+LqPNAMcBGwHFavq0Nixf8vreMmO12KgZQpZzQoalvwuHa1GoNwA25A 9eHV9Ox5bo/jjBpg6TqujInqUIW99M6g+9LWjPSDVEVWchpwsXXZ4CauWT5TKY5BQ0 p0PrHbxAxCMqoOftfuPJN2MzIPeOpDFeV4bG4TYHd2uSZvn8KWEHpNwhOukJvTSoIe 2678S+xDbHNZp/Lbgu3/HPE/V934fI1a7V/j1+xGQS4JUI9AbjhH+1A0R4Ce6Fnbkt 22AvKSrWGLA61LsSO3dgjJcRp03pf+9MvDIbWLPwj8A74yFLuElxFqJ3ZsvFjs/QXH jRp43ygyjvzWg== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/6] btrfs: add function comment to btrfs_lookup_csums_list() Date: Fri, 12 Apr 2024 16:03:15 +0100 Message-Id: <6cd95c2965fedf3f2b2d8b5dbf1dcbb072067192.1712933005.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana Add a function comment to btrfs_lookup_csums_list() to document it. With another upcoming change its parameter list and return value will be less obvious. So add the documentation now so that it can be updated where needed later. Signed-off-by: Filipe Manana Reviewed-by: Qu Wenruo --- fs/btrfs/file-item.c | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c index e58fb5347e65..909438540119 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c @@ -450,6 +450,19 @@ blk_status_t btrfs_lookup_bio_sums(struct btrfs_bio *bbio) return ret; } +/* + * Search for checksums for a given logical range. + * + * @root: The root where to look for checksums. + * @start: Logical address of target checksum range. + * @end: End offset (inclusive) of the target checksum range. + * @list: List for adding each checksum that was found. + * @search_commit: Indicate if the commit root of the @root should be used + * for the search. + * @nowait: Indicate if the search must be non-blocking or not. + * + * Return < 0 on error and 0 on success. + */ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, struct list_head *list, int search_commit, bool nowait) From patchwork Fri Apr 12 15:03:16 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13627954 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 28CF7148301 for ; Fri, 12 Apr 2024 15:03:27 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934207; cv=none; b=UVr8omWQtOMAFi8+cT/3LiUrxzHnscHsCybEjT2JxmK5V94IUwNtrzX+mhOcfknZdnjxZy3XrmsXTheH9fEXiwNq15DNSuikqZ7MyA2Yo+D4r0J95X3D3i8phrePIy2fN2JtVbrSPXIzdJASZx6Cacl4DfchnTK6YivxyDR685M= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934207; c=relaxed/simple; bh=L1t3oV64TAPzIEmjZCLUcCiGTWRRY93I3arHq2o4Co4=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=lwqJkZMpiHe20sWO2yLuH1fFm9A7wAyUda8uCFJCoHpVeoYmCqDXGgbL4fUGXxrFkwhpe125YpLldoFOSKDO+l7BRgYVEEBMd2q4X2Vf4WSaJkO8qw/3tG6mc5hoDA67HGAmz4YOSs+4RxYaJ2ZiniVN+daiTfxeE0eh8TVI1mg= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=e/dLZZdh; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="e/dLZZdh" Received: by smtp.kernel.org (Postfix) with ESMTPSA id AA4EDC3277B for ; Fri, 12 Apr 2024 15:03:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712934207; bh=L1t3oV64TAPzIEmjZCLUcCiGTWRRY93I3arHq2o4Co4=; h=From:To:Subject:Date:In-Reply-To:References:From; b=e/dLZZdhzYtFh36BUgwEirD3bjKQo016+Pf5Rp5cTsNndIoREtq5y38PjxfahLUmg 9ixAWX+FnV+cdrO+zu4p1HJg0BM48O3tUwRNgJSQQZLLs6lORMQgKcTGamu8rQAHaJ 58ytyLwMVMn5ARG1/DGeM09nDMuTaTpRYHW5RRHvNZCVWRlk035w957p2DagSvtyln nRVMg1WhZWLpeM0VvaPophqu7dJEkr/rO/3udlK/LeBpd1b72GmfK2IBZKZDKeIydJ tDqPOks+wptapaWYygsUkJOBC+T3P5nguyT86KUt+wjBe83ZwTo2N6xjLLqtn3ElWy COWZgQbj/aMZg== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/6] btrfs: remove search_commit parameter from btrfs_lookup_csums_list() Date: Fri, 12 Apr 2024 16:03:16 +0100 Message-Id: <8f31c8dcf6d09d42079b9bb4164a87450d2f0adf.1712933005.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana All the callers of btrfs_lookup_csums_list() pass a value of 0 as the "search_commit" parameter. So remove it and make the function behave as to always search from the regular root. Signed-off-by: Filipe Manana Reviewed-by: Qu Wenruo --- fs/btrfs/file-item.c | 10 +--------- fs/btrfs/file-item.h | 3 +-- fs/btrfs/inode.c | 2 +- fs/btrfs/relocation.c | 2 +- fs/btrfs/tree-log.c | 6 +++--- 5 files changed, 7 insertions(+), 16 deletions(-) diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c index 909438540119..0712a0aa2dd0 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c @@ -457,15 +457,12 @@ blk_status_t btrfs_lookup_bio_sums(struct btrfs_bio *bbio) * @start: Logical address of target checksum range. * @end: End offset (inclusive) of the target checksum range. * @list: List for adding each checksum that was found. - * @search_commit: Indicate if the commit root of the @root should be used - * for the search. * @nowait: Indicate if the search must be non-blocking or not. * * Return < 0 on error and 0 on success. */ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, - struct list_head *list, int search_commit, - bool nowait) + struct list_head *list, bool nowait) { struct btrfs_fs_info *fs_info = root->fs_info; struct btrfs_key key; @@ -484,11 +481,6 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, return -ENOMEM; path->nowait = nowait; - if (search_commit) { - path->skip_locking = 1; - path->reada = READA_FORWARD; - path->search_commit_root = 1; - } key.objectid = BTRFS_EXTENT_CSUM_OBJECTID; key.offset = start; diff --git a/fs/btrfs/file-item.h b/fs/btrfs/file-item.h index 15c05cc0fce6..557dc43d7142 100644 --- a/fs/btrfs/file-item.h +++ b/fs/btrfs/file-item.h @@ -68,8 +68,7 @@ int btrfs_lookup_csums_range(struct btrfs_root *root, u64 start, u64 end, struct list_head *list, int search_commit, bool nowait); int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, - struct list_head *list, int search_commit, - bool nowait); + struct list_head *list, bool nowait); int btrfs_lookup_csums_bitmap(struct btrfs_root *root, struct btrfs_path *path, u64 start, u64 end, u8 *csum_buf, unsigned long *csum_bitmap); diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 2dae4e975e80..4e67470d847a 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -1746,7 +1746,7 @@ static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, LIST_HEAD(list); ret = btrfs_lookup_csums_list(csum_root, bytenr, bytenr + num_bytes - 1, - &list, 0, nowait); + &list, nowait); if (ret == 0 && list_empty(&list)) return 0; diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 5b19b41f64a2..4e60364b5289 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -4391,7 +4391,7 @@ int btrfs_reloc_clone_csums(struct btrfs_ordered_extent *ordered) ret = btrfs_lookup_csums_list(csum_root, disk_bytenr, disk_bytenr + ordered->num_bytes - 1, - &list, 0, false); + &list, false); if (ret) return ret; diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 4a4fca841510..e9ad2971fc7c 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -797,7 +797,7 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, ret = btrfs_lookup_csums_list(root->log_root, csum_start, csum_end - 1, - &ordered_sums, 0, false); + &ordered_sums, false); if (ret) goto out; /* @@ -4460,7 +4460,7 @@ static noinline int copy_items(struct btrfs_trans_handle *trans, disk_bytenr += extent_offset; ret = btrfs_lookup_csums_list(csum_root, disk_bytenr, disk_bytenr + extent_num_bytes - 1, - &ordered_sums, 0, false); + &ordered_sums, false); if (ret) goto out; @@ -4655,7 +4655,7 @@ static int log_extent_csums(struct btrfs_trans_handle *trans, csum_root = btrfs_csum_root(trans->fs_info, em->block_start); ret = btrfs_lookup_csums_list(csum_root, em->block_start + csum_offset, em->block_start + csum_offset + - csum_len - 1, &ordered_sums, 0, false); + csum_len - 1, &ordered_sums, false); if (ret) return ret; From patchwork Fri Apr 12 15:03:17 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13627955 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 8B91F148301 for ; Fri, 12 Apr 2024 15:03:28 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934208; cv=none; b=MzzGHr3JgNGDKiUC1tyfWyjKZn5FVWuAZrAovH5txa5R0POiuKT8qJufhkicbpZm3zCTzP55/sKJSaTxvb01P4iWd+Um25qTywh0uqWZdMAkee/3VDINewmQkd93yAvJrKLRmoy4Be6Y0w8c2Bwg9jd2db9tRo1hdMm9jb9q6xE= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934208; c=relaxed/simple; bh=/nzaOUhmq7/6PUwsnScjWmwj23J7xSX9eYlSBzu66Fg=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=OOJzDS9+aQ37XL78iBc0BvKycdfeQ1Xx02kr1uxK2Fk8817vC9fcuG9fu4Fnk4/gyb1vxNYy9uiLA/rZcHZsOXoLnxe03ImoylmZRrGPUon/qCt4UlnF9atTT0GLpPaSh876PUisbUMrycvhU0AKBO08L+pXEvGgGBS4LnldBwA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=n5nI3+jp; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="n5nI3+jp" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7AC3FC2BBFC for ; Fri, 12 Apr 2024 15:03:27 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712934208; bh=/nzaOUhmq7/6PUwsnScjWmwj23J7xSX9eYlSBzu66Fg=; h=From:To:Subject:Date:In-Reply-To:References:From; b=n5nI3+jppxJ47hoxI6f7WtHCc7PgLHpM/ZX8ZNVVyG1EpJA1/bWvWI1lUqKRJDWsx k4lhg0ucoeilfFU4T9WJJWCyuPpYhadtAytKWVRPBE3LtjsXu70ucLW2pK/3wP/b7P +KH28ADiqUE0tMwwHutSbYrvCTJYCMd6D+M8r2cwhUoarHKFx6L+72xCNryfFSYf+A Be1fj1cVNSBD5JyXmSOvFYNMG6S09tFxzJzEqS8F9HNe4OB0Ut1xg0lAls8W5e/1L7 +uzw/6x3zp4Km4S/cTTMH5lEVawcfSnAapWoLS73O6gr3V7gvl+3MeyjJUMUxumB0R Ow8rjXEQSiMcA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 3/6] btrfs: remove use of a temporary list at btrfs_lookup_csums_list() Date: Fri, 12 Apr 2024 16:03:17 +0100 Message-Id: <5ba3b6e48c1e7433b1e38ae315713d7b31813730.1712933005.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana There's no need to use a temporary list to add the checksums, we can just add them to input list and then on error delete and free any checksums that were added. So simplify and remove the temporary list. Signed-off-by: Filipe Manana Reviewed-by: Qu Wenruo --- fs/btrfs/file-item.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c index 0712a0aa2dd0..c2799325706f 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c @@ -470,7 +470,6 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, struct extent_buffer *leaf; struct btrfs_ordered_sum *sums; struct btrfs_csum_item *item; - LIST_HEAD(tmplist); int ret; ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && @@ -572,18 +571,17 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, bytes_to_csum_size(fs_info, size)); start += size; - list_add_tail(&sums->list, &tmplist); + list_add_tail(&sums->list, list); } path->slots[0]++; } ret = 0; fail: - while (ret < 0 && !list_empty(&tmplist)) { - sums = list_entry(tmplist.next, struct btrfs_ordered_sum, list); + while (ret < 0 && !list_empty(list)) { + sums = list_entry(list->next, struct btrfs_ordered_sum, list); list_del(&sums->list); kfree(sums); } - list_splice_tail(&tmplist, list); btrfs_free_path(path); return ret; From patchwork Fri Apr 12 15:03:18 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13627956 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 6C9FE1487D0 for ; Fri, 12 Apr 2024 15:03:29 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934209; cv=none; b=tbFgjIfSniMXMTPnMqoSBALN18GfTRddcNnWNIXd28ZnXOAYRY9KRTVZtzTrWlAzd6ezI6R804rtVFo+rfgdbJJ/ovSrEQbsUpUOl0TNNZCJjnhTIk+wUPT0lsRmqbeAczx/6wwkGGxu+5ac2ZRtEm1xeeD6nY6SQPao/YWN8Vo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934209; c=relaxed/simple; bh=NkcKC0WVwbG9P2OPL4ZSfIr0uUaWT87BNCfCi+YRM8E=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=hBuyHfRon+vbVYlL8VmxL75qyjyga5wp0cbyWOQq8pz+YKuKvAf2RbuaJXvW40fEQAucqznxGTl1o1bDwdhxhmm3ZaNWiiDy3IR9oizbhedZLqJ5t/sRwH6HKQY2xsbo9+GEwz16jC5rRwf4m/Cbw1JFf9PFHw35liKDf5V3cxc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=EZiPyhP2; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="EZiPyhP2" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 78CD4C2BD11 for ; Fri, 12 Apr 2024 15:03:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712934209; bh=NkcKC0WVwbG9P2OPL4ZSfIr0uUaWT87BNCfCi+YRM8E=; h=From:To:Subject:Date:In-Reply-To:References:From; b=EZiPyhP2nm2ZsTM5Lmebf4hrZD3jeGj1IL8ioDa+QmaSBccoNJKJI63VaikLHJ+t/ J9G3W/zv09wl6hNPyelbswBmoM/o5YX8ZF/t6yzcLKACkEqAw9ly8aANNruuFnQD+C prxYyES6WhbVAMZSxH3ZwxX6vSS7xcUGV+KaCPSs+ae++v3vKV5C4FJ3T9Z+dmsMZf NyTqeedYxxo3AjfBExtzSBQl0HILH1fFOLlJgDzZDkejI8UIK7a9VIppEJ9KAv4BmC uDFrC2tffEwTh5XhlOK6t1Uszz4ZjhJMDt7LJR8XqtdP9YegMDcrbRPnFQGlb9GN4y Qao9ffC1Ccozg== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 4/6] btrfs: simplify error path for btrfs_lookup_csums_list() Date: Fri, 12 Apr 2024 16:03:18 +0100 Message-Id: X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana In the error path we have this while loop that keeps iterating over the csums of the list and then delete them from the list and free them, testing for an error (ret < 0) and list emptyness as the conditions of the while loop. Simplify this by using list_for_each_entry_safe() so there's no need to delete elements from the list and need to test the error condition on each iteration. Also rename the 'fail' label to 'out' since the label is not exclusive to a failure path, as we also end up there when the function succeeds, and it's also a more common label name. Signed-off-by: Filipe Manana Reviewed-by: Qu Wenruo --- fs/btrfs/file-item.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c index c2799325706f..231abcc87f63 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c @@ -487,7 +487,7 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, ret = btrfs_search_slot(NULL, root, &key, path, 0, 0); if (ret < 0) - goto fail; + goto out; if (ret > 0 && path->slots[0] > 0) { leaf = path->nodes[0]; btrfs_item_key_to_cpu(leaf, &key, path->slots[0] - 1); @@ -522,7 +522,7 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, if (path->slots[0] >= btrfs_header_nritems(leaf)) { ret = btrfs_next_leaf(root, path); if (ret < 0) - goto fail; + goto out; if (ret > 0) break; leaf = path->nodes[0]; @@ -557,7 +557,7 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, GFP_NOFS); if (!sums) { ret = -ENOMEM; - goto fail; + goto out; } sums->logical = start; @@ -576,11 +576,12 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, path->slots[0]++; } ret = 0; -fail: - while (ret < 0 && !list_empty(list)) { - sums = list_entry(list->next, struct btrfs_ordered_sum, list); - list_del(&sums->list); - kfree(sums); +out: + if (ret < 0) { + struct btrfs_ordered_sum *tmp_sums; + + list_for_each_entry_safe(sums, tmp_sums, list, list) + kfree(sums); } btrfs_free_path(path); From patchwork Fri Apr 12 15:03:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13627957 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 68A7B1487E1 for ; Fri, 12 Apr 2024 15:03:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934210; cv=none; b=iHBLg8DpezLm5PahuVdB4n6T0msYf16FEKH7H2lFGqWO1/P62MJIfttXd3jmtOJY8IzKQZNuIM71u4Rxn3erM2IBG+bY+F9yTs4cYDF4YHk8pO1Ue66OuX21eHa3s9Xv2/PcFy5O0xilFQf+PTyA10xQlOY3ZKltwlO3yxB/ovw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934210; c=relaxed/simple; bh=zVzzwZ3NRXLcsttMmnWWYjJtOd2PcijnYFsnwjwy5nc=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=dymM3woRg/o81Jo7es604eGNKqtfVPVcJtb2aE93hXA9JLosvpOY+6yElsjI7AFUOLnD26O3nBwMa5LZqrZgX6LGZf3GY0ic2jtTVJamkrL6QUSnLD3anwEiyX0Ydl7VKOkNjqZg7i3BwIcAnSVySRzcQr90ghUmjJBJS3QWFgA= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=MGWn89+W; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="MGWn89+W" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 77E47C2BBFC for ; Fri, 12 Apr 2024 15:03:29 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712934210; bh=zVzzwZ3NRXLcsttMmnWWYjJtOd2PcijnYFsnwjwy5nc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=MGWn89+WEuDtNG4H9E7dr4qHVpC0VXIb/S8HhnVVtJmkarf2uAR2t7coHuhP0BU16 0eyJ1QYYZ40/Zvq4WFhFXJiEGpEG64P/ZEo0JqB7kk3sE8lAN2TwGZATWA0CgfigdB 8Yre5ISdvlK4N7ff0f3UR6HMuGdwyHcmQFXkL1bKED5gpvfT6OcPvkcWEgR/K8Sm9g wcnDjMM4dy7r14HbnZsylO8zKtiu+2L3za4B31Gx43Cn+8SwnsIcnX8Mn+QMxQIvAo +wCVVypu+Znk59XMni25Pbdz4ePyJ0JF7o5/y1A4zKiuhhKXaDgnjTTNYYf/YjN9bv fyG0887Tbbx2A== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 5/6] btrfs: make NOCOW checks for existence of checksums in a range more efficient Date: Fri, 12 Apr 2024 16:03:19 +0100 Message-Id: <72cd9b16a1c670d4139bb10f6f13eb76f92c3fed.1712933006.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana Before deciding if we can do a NOCOW write into a range, one of the things we have to do is check if there are checksum items for that range. We do that through the btrfs_lookup_csums_list() function, which searches for checksums and adds them to a list supplied by the caller. But all we need is to check if there is any checksum, we don't need to look for all of them and collect them into a list, which requires more search time in the checksums tree, allocating memory for checksums items to add to the list, copy checksums from a leaf into those list items, then free that memory, etc. This is all unnecessary overhead, wasting mostly CPU time, and perhaps some occasional IO if we need to read from disk any extent buffers. So change btrfs_lookup_csums_list() to allow to return immediately in case it finds any checksum, without the need to add it to a list and read it from a leaf. This is accomplised by allowing a NULL list parameter and making the function return 1 if it found any checksum, 0 if it didn't found any, and a negative value in case of an error. The following test with fio was used to measure performance: $ cat test.sh #!/bin/bash DEV=/dev/nullb0 MNT=/mnt/nullb0 cat < /tmp/fio-job.ini [global] name=fio-rand-write filename=$MNT/fio-rand-write rw=randwrite bssplit=4k/20:8k/20:16k/20:32k/20:64k/20 direct=1 numjobs=16 fallocate=posix time_based runtime=300 [file1] size=8G ioengine=io_uring iodepth=16 EOF umount $MNT &> /dev/null mkfs.btrfs -f $DEV mount -o ssd $DEV $MNT fio /tmp/fio-job.ini umount $MNT The test was run on a release kernel (Debian's default kernel config). The results before this patch: WRITE: bw=139MiB/s (146MB/s), 8204KiB/s-9504KiB/s (8401kB/s-9732kB/s), io=17.0GiB (18.3GB), run=125317-125344msec The results after this patch: WRITE: bw=153MiB/s (160MB/s), 9241KiB/s-10.0MiB/s (9463kB/s-10.5MB/s), io=17.0GiB (18.3GB), run=114054-114071msec Signed-off-by: Filipe Manana Reviewed-by: Qu Wenruo --- fs/btrfs/file-item.c | 25 ++++++++++++++++++------- fs/btrfs/inode.c | 18 ++---------------- fs/btrfs/relocation.c | 2 +- fs/btrfs/tree-log.c | 9 ++++++--- 4 files changed, 27 insertions(+), 27 deletions(-) diff --git a/fs/btrfs/file-item.c b/fs/btrfs/file-item.c index 231abcc87f63..1ea1ed44fe42 100644 --- a/fs/btrfs/file-item.c +++ b/fs/btrfs/file-item.c @@ -457,9 +457,12 @@ blk_status_t btrfs_lookup_bio_sums(struct btrfs_bio *bbio) * @start: Logical address of target checksum range. * @end: End offset (inclusive) of the target checksum range. * @list: List for adding each checksum that was found. + * Can be NULL in case the caller only wants to check if + * there any checksums for the range. * @nowait: Indicate if the search must be non-blocking or not. * - * Return < 0 on error and 0 on success. + * Return < 0 on error, 0 if no checksums were found, or 1 if checksums were + * found. */ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, struct list_head *list, bool nowait) @@ -471,6 +474,7 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, struct btrfs_ordered_sum *sums; struct btrfs_csum_item *item; int ret; + bool found_csums = false; ASSERT(IS_ALIGNED(start, fs_info->sectorsize) && IS_ALIGNED(end + 1, fs_info->sectorsize)); @@ -544,6 +548,10 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, continue; } + found_csums = true; + if (!list) + goto out; + csum_end = min(csum_end, end + 1); item = btrfs_item_ptr(path->nodes[0], path->slots[0], struct btrfs_csum_item); @@ -575,17 +583,20 @@ int btrfs_lookup_csums_list(struct btrfs_root *root, u64 start, u64 end, } path->slots[0]++; } - ret = 0; out: + btrfs_free_path(path); if (ret < 0) { - struct btrfs_ordered_sum *tmp_sums; + if (list) { + struct btrfs_ordered_sum *tmp_sums; - list_for_each_entry_safe(sums, tmp_sums, list, list) - kfree(sums); + list_for_each_entry_safe(sums, tmp_sums, list, list) + kfree(sums); + } + + return ret; } - btrfs_free_path(path); - return ret; + return found_csums ? 1 : 0; } /* diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 4e67470d847a..1d78e07d082b 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -1741,23 +1741,9 @@ static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, u64 bytenr, u64 num_bytes, bool nowait) { struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bytenr); - struct btrfs_ordered_sum *sums; - int ret; - LIST_HEAD(list); - - ret = btrfs_lookup_csums_list(csum_root, bytenr, bytenr + num_bytes - 1, - &list, nowait); - if (ret == 0 && list_empty(&list)) - return 0; - while (!list_empty(&list)) { - sums = list_entry(list.next, struct btrfs_ordered_sum, list); - list_del(&sums->list); - kfree(sums); - } - if (ret < 0) - return ret; - return 1; + return btrfs_lookup_csums_list(csum_root, bytenr, bytenr + num_bytes - 1, + NULL, nowait); } static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page, diff --git a/fs/btrfs/relocation.c b/fs/btrfs/relocation.c index 4e60364b5289..5a01aaa164de 100644 --- a/fs/btrfs/relocation.c +++ b/fs/btrfs/relocation.c @@ -4392,7 +4392,7 @@ int btrfs_reloc_clone_csums(struct btrfs_ordered_extent *ordered) ret = btrfs_lookup_csums_list(csum_root, disk_bytenr, disk_bytenr + ordered->num_bytes - 1, &list, false); - if (ret) + if (ret < 0) return ret; while (!list_empty(&list)) { diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index e9ad2971fc7c..3c86fcebafc6 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -798,8 +798,9 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, ret = btrfs_lookup_csums_list(root->log_root, csum_start, csum_end - 1, &ordered_sums, false); - if (ret) + if (ret < 0) goto out; + ret = 0; /* * Now delete all existing cums in the csum root that * cover our range. We do this because we can have an @@ -4461,8 +4462,9 @@ static noinline int copy_items(struct btrfs_trans_handle *trans, ret = btrfs_lookup_csums_list(csum_root, disk_bytenr, disk_bytenr + extent_num_bytes - 1, &ordered_sums, false); - if (ret) + if (ret < 0) goto out; + ret = 0; list_for_each_entry_safe(sums, sums_next, &ordered_sums, list) { if (!ret) @@ -4656,8 +4658,9 @@ static int log_extent_csums(struct btrfs_trans_handle *trans, ret = btrfs_lookup_csums_list(csum_root, em->block_start + csum_offset, em->block_start + csum_offset + csum_len - 1, &ordered_sums, false); - if (ret) + if (ret < 0) return ret; + ret = 0; while (!list_empty(&ordered_sums)) { struct btrfs_ordered_sum *sums = list_entry(ordered_sums.next, From patchwork Fri Apr 12 15:03:20 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 13627958 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (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 23ECC1487F0 for ; Fri, 12 Apr 2024 15:03:31 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934211; cv=none; b=O1WEo1FXRnU7XmSUoJLI0B7SVhnkGPLdGNzpF30m9lRQX7bPplhddpMHFudttlW3OF+zIM6Clj6Ho9KX+vQEHXunsPL/TG2eh84nhz9e3O0Db+ruQW5uwojhotdO9CC7GTM7eEoDhZSnWkW0w5AitiOJ77tpda9KRTPMh0J5TOo= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1712934211; c=relaxed/simple; bh=GpLwSi/xgn0Cnu29Wac2kau0tUCw78yhYAJugtiyD4E=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=luO077IfgwkYarQ154Od2DHx00fJ0zdBTWpdkz+i15T8bFSk0NT5FkQ5vfA3qGtn2/mnYbZbzpkKaVmWNlkfXeZ7CuwSXtCm+6ISX7gDWfZD8ai17rlDOU6xqbCYNyaal9Nknxgh6d8/EBpjLCjoPpzBfqRIwrQQbKlt24fvjOc= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=QsLBZZey; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b="QsLBZZey" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 761CAC113CC for ; Fri, 12 Apr 2024 15:03:30 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1712934211; bh=GpLwSi/xgn0Cnu29Wac2kau0tUCw78yhYAJugtiyD4E=; h=From:To:Subject:Date:In-Reply-To:References:From; b=QsLBZZeyCiWSOa6uIia1a3OqZGU8GeVmTSkLCQWiYHDqCtNec63eOYl/AoCPpfY5W KZgIAhol8VtfgcMOKOulZp8n6M+8tqky4b/krmCxmDF8QLy1QVMpJ976awsuc8LMLE beQ9ymGe6U4mZNjusu82WkbeDrBXcATtrUESYhLjEJAc0f4MQJcEpRT1CI5VeU54i9 8+Oo7rmLoUQEb2HZZpJZ3wNCDT6VvxG2VD/DomBPi0wB2/BN59x7NKf+9fS1bhbgAQ RR2GT2opqfg+VvyBD2mDJI0Fmu8m+v9VYVQmE+a6Aqsex8H/OxdHMFBwW6lqVSZkgp YzUxv3aOYJ75g== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 6/6] btrfs: open code csum_exist_in_range() Date: Fri, 12 Apr 2024 16:03:20 +0100 Message-Id: <43c7b46211cc26f0f47ccd8bca522d70e4e563b8.1712933006.git.fdmanana@suse.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: Precedence: bulk X-Mailing-List: linux-btrfs@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 From: Filipe Manana The csum_exist_in_range() function is now too trivial and is only used in one place, so open code it in its single caller. Signed-off-by: Filipe Manana Reviewed-by: Qu Wenruo --- fs/btrfs/inode.c | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/fs/btrfs/inode.c b/fs/btrfs/inode.c index 1d78e07d082b..f23511428e74 100644 --- a/fs/btrfs/inode.c +++ b/fs/btrfs/inode.c @@ -1737,15 +1737,6 @@ static noinline int run_delalloc_cow(struct btrfs_inode *inode, return 1; } -static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info, - u64 bytenr, u64 num_bytes, bool nowait) -{ - struct btrfs_root *csum_root = btrfs_csum_root(fs_info, bytenr); - - return btrfs_lookup_csums_list(csum_root, bytenr, bytenr + num_bytes - 1, - NULL, nowait); -} - static int fallback_to_cow(struct btrfs_inode *inode, struct page *locked_page, const u64 start, const u64 end) { @@ -1860,6 +1851,7 @@ static int can_nocow_file_extent(struct btrfs_path *path, struct extent_buffer *leaf = path->nodes[0]; struct btrfs_root *root = inode->root; struct btrfs_file_extent_item *fi; + struct btrfs_root *csum_root; u64 extent_end; u8 extent_type; int can_nocow = 0; @@ -1920,7 +1912,7 @@ static int can_nocow_file_extent(struct btrfs_path *path, if (args->free_path) { /* * We don't need the path anymore, plus through the - * csum_exist_in_range() call below we will end up allocating + * btrfs_lookup_csums_list() call below we will end up allocating * another path. So free the path to avoid unnecessary extra * memory usage. */ @@ -1941,8 +1933,11 @@ static int can_nocow_file_extent(struct btrfs_path *path, * Force COW if csums exist in the range. This ensures that csums for a * given extent are either valid or do not exist. */ - ret = csum_exist_in_range(root->fs_info, args->disk_bytenr, args->num_bytes, - nowait); + + csum_root = btrfs_csum_root(root->fs_info, args->disk_bytenr); + ret = btrfs_lookup_csums_list(csum_root, args->disk_bytenr, + args->disk_bytenr + args->num_bytes - 1, + NULL, nowait); WARN_ON_ONCE(ret > 0 && is_freespace_inode); if (ret != 0) goto out;