From patchwork Thu Mar 13 17:55:31 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14015691 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 386F91E9B26 for ; Thu, 13 Mar 2025 17:55:41 +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=1741888542; cv=none; b=lMgcxetwMKqsxAOqYcxm64e9GH3vFJ4Sef4zVOhtgDQ78pZdkXqf+vj/mh7Z7CgeMCDoAIYzouzWv2WEDeSkoRSP3hO1ypFap1oTQcZYQKPDELzn8Af9PGn86yYgWDOVlu1hiVy8K5vxU346nGZoj8smBITgxBP4u3bxP631534= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741888542; c=relaxed/simple; bh=Bw1/L/CeCO9LrjO4acgWC8W+Y/FFVnIFZfPMoml4oko=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=t0XsEvMBFVlMXWoarxUvGGE1VOIjtWv3VDTQxsMfAQmk1dpyzl2w/MlKtlAHboLp7m60YaEy+PADB1eGxfjNTTCN9MUlMAhqfkqPWajTja041MwlJnn1/FY5jnhWQIdKgZERvq/haQzzTrqwOhv46Jb0WIKzcIRQ5XgD3uFwmgQ= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=eiJrgqWe; 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="eiJrgqWe" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2C70CC4CEEA for ; Thu, 13 Mar 2025 17:55:40 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741888541; bh=Bw1/L/CeCO9LrjO4acgWC8W+Y/FFVnIFZfPMoml4oko=; h=From:To:Subject:Date:In-Reply-To:References:From; b=eiJrgqWeiWv1zHGezp0ie6WlAupbmQH+qk7ENcguRJS3xWwECfgsUMd/rHd2G1oxr lVeUNteJ22E8QKBBpl89kbkG4f54zddJzg2lLVznRE0Wqzzbwr05VjFyzDyxuIOgbU 4ZEEGaiG0CwAMUcuFvortkUMJG0Hj4jwKsnEzUdprQKDum47cKOjrvfHkcfeymTqcE 8lizT4XV7keTwS/q21dqrEaoh94LJsPkr7sc0lKkBgZ3cdOaFgBMypMU90p7awMWdQ A83H2nvSHsCtiGvkg59sYEBt64a6ANhx4lCfHxPCuO9Kew5efJrpyCEEPjyG5lb5yN PG1qY+UIvBn+w== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 1/7] btrfs: avoid unnecessary memory allocation and copy at overwrite_item() Date: Thu, 13 Mar 2025 17:55:31 +0000 Message-Id: <879a3362577693b50500e8dc08ba576eade8fd5e.1741887949.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 allocate memory and copy from both the destination and source extent buffers to compare if the items are equal, we can instead use memcmp_extent_buffer() which allows to do only one memory allocation and copy instead of two. So use memcmp_extent_buffer() instead of memcmp(), allowing us to avoid one memory allocation, which can fail or be slow while under memory heavy pressure, avoid the memory copying and reducing code. Signed-off-by: Filipe Manana --- fs/btrfs/tree-log.c | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 2d23223f476b..91278cc83bd4 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -422,7 +422,6 @@ static int overwrite_item(struct btrfs_trans_handle *trans, if (ret == 0) { char *src_copy; - char *dst_copy; u32 dst_size = btrfs_item_size(path->nodes[0], path->slots[0]); if (dst_size != item_size) @@ -432,23 +431,16 @@ static int overwrite_item(struct btrfs_trans_handle *trans, btrfs_release_path(path); return 0; } - dst_copy = kmalloc(item_size, GFP_NOFS); src_copy = kmalloc(item_size, GFP_NOFS); - if (!dst_copy || !src_copy) { + if (!src_copy) { btrfs_release_path(path); - kfree(dst_copy); - kfree(src_copy); return -ENOMEM; } read_extent_buffer(eb, src_copy, src_ptr, item_size); - dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); - read_extent_buffer(path->nodes[0], dst_copy, dst_ptr, - item_size); - ret = memcmp(dst_copy, src_copy, item_size); + ret = memcmp_extent_buffer(path->nodes[0], src_copy, dst_ptr, item_size); - kfree(dst_copy); kfree(src_copy); /* * they have the same contents, just return, this saves From patchwork Thu Mar 13 17:55:32 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14015692 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 450111EB5E9 for ; Thu, 13 Mar 2025 17:55:42 +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=1741888543; cv=none; b=tzP7Q6alVEVOESCx7j195snOX2RYiBqBHxF+UPUV93Y2d+nSKn58jP6jwJ3okvarbbBiBELmnC6Www6SoZJ9RkOVdCLTy30cfPIaECUZnHdpZCv5B9l8Rv67FkiRy2wFEuuyks2Bl+AMcIcQ4WGkVpffOcWkAm13YP5R+T0GHxY= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741888543; c=relaxed/simple; bh=DxOkrvm4vUv7n1tceL96XOTjO2VtqouVzsU1xnS+YzY=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=WiZGBmw9uvvTMzfG65D5k5CnSDpsyDoXrTkT1pS9Kphxe2aGaTrLkoFLcRpDPC+rcz4qC+ZpRrIpyWAvymsXp76rJqr9OFZZ6ndHaGFfEIu2Y0J7FqdRrXn9UWoPz01dlcrU1DHzjPgG2AmRXjNyq73LbcoryWVjhoNwGG98G/g= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=mtS2yd5p; 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="mtS2yd5p" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 2EC9FC4CEEB for ; Thu, 13 Mar 2025 17:55:41 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741888542; bh=DxOkrvm4vUv7n1tceL96XOTjO2VtqouVzsU1xnS+YzY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=mtS2yd5pzdFLT/zF7kK4bRH1do3zdFWw0oI6g7fV1AJ7jg5Wk+1iPQfZVFpcvcyV5 uuusXrUBfBEtfhlea34T3ymfpYguc0UnICRvIuWmDrLSfma6V1bVyzqWZv0ULHg4Xn d+LuCKoqkSS8CcKrqd23fr7lskh5NNqUEuUadD48COVX/rSzWE4tomJTZx9DS+rd8e 2Sbzdzp9C+sDbQqdGNAfZGuvXLVVQU0dfHX6cYBdCQCAWpbxUODwshRzBUgOZAzpEK fhFpZpbAXA7HWuNnqAZ4uMnJyw+avLOCS2t4L4pLYwZF66FJ+zZhBDMMnoG/DNqOq8 QOQotUGppkF6w== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 2/7] btrfs: use variables to store extent buffer and slot at overwrite_item() Date: Thu, 13 Mar 2025 17:55:32 +0000 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 Instead of referring to path->nodes[0] and path->slots[0] multiple times, which is verbose and confusing since we have an 'eb' and 'slot' variables as well, introduce local variables 'dst_eb' to point to path->nodes[0] and 'dst_slot' to have path->slots[0], reducing verbosity and making it more obvious about which extent buffer and slot we are referring to. Signed-off-by: Filipe Manana --- fs/btrfs/tree-log.c | 47 ++++++++++++++++++++++++--------------------- 1 file changed, 25 insertions(+), 22 deletions(-) diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 91278cc83bd4..f23feddb41c5 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -401,6 +401,8 @@ static int overwrite_item(struct btrfs_trans_handle *trans, int save_old_i_size = 0; unsigned long src_ptr; unsigned long dst_ptr; + struct extent_buffer *dst_eb; + int dst_slot; bool inode_item = key->type == BTRFS_INODE_ITEM_KEY; /* @@ -420,10 +422,13 @@ static int overwrite_item(struct btrfs_trans_handle *trans, if (ret < 0) return ret; + dst_eb = path->nodes[0]; + dst_slot = path->slots[0]; + if (ret == 0) { char *src_copy; - u32 dst_size = btrfs_item_size(path->nodes[0], - path->slots[0]); + const u32 dst_size = btrfs_item_size(dst_eb, dst_slot); + if (dst_size != item_size) goto insert; @@ -438,8 +443,8 @@ static int overwrite_item(struct btrfs_trans_handle *trans, } read_extent_buffer(eb, src_copy, src_ptr, item_size); - dst_ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); - ret = memcmp_extent_buffer(path->nodes[0], src_copy, dst_ptr, item_size); + dst_ptr = btrfs_item_ptr_offset(dst_eb, dst_slot); + ret = memcmp_extent_buffer(dst_eb, src_copy, dst_ptr, item_size); kfree(src_copy); /* @@ -462,9 +467,9 @@ static int overwrite_item(struct btrfs_trans_handle *trans, u64 nbytes; u32 mode; - item = btrfs_item_ptr(path->nodes[0], path->slots[0], + item = btrfs_item_ptr(dst_eb, dst_slot, struct btrfs_inode_item); - nbytes = btrfs_inode_nbytes(path->nodes[0], item); + nbytes = btrfs_inode_nbytes(dst_eb, item); item = btrfs_item_ptr(eb, slot, struct btrfs_inode_item); btrfs_set_inode_nbytes(eb, item, nbytes); @@ -506,11 +511,13 @@ static int overwrite_item(struct btrfs_trans_handle *trans, key, item_size); path->skip_release_on_error = 0; + dst_eb = path->nodes[0]; + dst_slot = path->slots[0]; + /* make sure any existing item is the correct size */ if (ret == -EEXIST || ret == -EOVERFLOW) { - u32 found_size; - found_size = btrfs_item_size(path->nodes[0], - path->slots[0]); + const u32 found_size = btrfs_item_size(dst_eb, dst_slot); + if (found_size > item_size) btrfs_truncate_item(trans, path, item_size, 1); else if (found_size < item_size) @@ -518,8 +525,7 @@ static int overwrite_item(struct btrfs_trans_handle *trans, } else if (ret) { return ret; } - dst_ptr = btrfs_item_ptr_offset(path->nodes[0], - path->slots[0]); + dst_ptr = btrfs_item_ptr_offset(dst_eb, dst_slot); /* don't overwrite an existing inode if the generation number * was logged as zero. This is done when the tree logging code @@ -538,7 +544,6 @@ static int overwrite_item(struct btrfs_trans_handle *trans, dst_item = (struct btrfs_inode_item *)dst_ptr; if (btrfs_inode_generation(eb, src_item) == 0) { - struct extent_buffer *dst_eb = path->nodes[0]; const u64 ino_size = btrfs_inode_size(eb, src_item); /* @@ -556,30 +561,28 @@ static int overwrite_item(struct btrfs_trans_handle *trans, } if (S_ISDIR(btrfs_inode_mode(eb, src_item)) && - S_ISDIR(btrfs_inode_mode(path->nodes[0], dst_item))) { + S_ISDIR(btrfs_inode_mode(dst_eb, dst_item))) { save_old_i_size = 1; - saved_i_size = btrfs_inode_size(path->nodes[0], - dst_item); + saved_i_size = btrfs_inode_size(dst_eb, dst_item); } } - copy_extent_buffer(path->nodes[0], eb, dst_ptr, - src_ptr, item_size); + copy_extent_buffer(dst_eb, eb, dst_ptr, src_ptr, item_size); if (save_old_i_size) { struct btrfs_inode_item *dst_item; + dst_item = (struct btrfs_inode_item *)dst_ptr; - btrfs_set_inode_size(path->nodes[0], dst_item, saved_i_size); + btrfs_set_inode_size(dst_eb, dst_item, saved_i_size); } /* make sure the generation is filled in */ if (key->type == BTRFS_INODE_ITEM_KEY) { struct btrfs_inode_item *dst_item; + dst_item = (struct btrfs_inode_item *)dst_ptr; - if (btrfs_inode_generation(path->nodes[0], dst_item) == 0) { - btrfs_set_inode_generation(path->nodes[0], dst_item, - trans->transid); - } + if (btrfs_inode_generation(dst_eb, dst_item) == 0) + btrfs_set_inode_generation(dst_eb, dst_item, trans->transid); } no_copy: btrfs_release_path(path); From patchwork Thu Mar 13 17:55:33 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14015693 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 3A3791EBFE2 for ; Thu, 13 Mar 2025 17:55:43 +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=1741888544; cv=none; b=HMtYYcIUblOYxIJU80GdnpaPM1vUKVlMehYCTorDgZsR8GqKCuNQN+E7P9R9GQWHQLeIEqil0yKu0jZogeoDzMoqMLp0o7BGRlqmoJX+vt1c68a86JkEw1BfYT2jihZJy1Fmbbd9ex6+3euHWDeUDjklYGCiEN2FpyGD3jOlbLg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741888544; c=relaxed/simple; bh=5KBLJlL6WBezu97iLhZh0a2nGayAwPdvVOuZg0otm18=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=AIdLwIY9rLVUTS9+TVvBrPSCNnLK/PcG0TV/OLRNDORhu0tjaFNfqAAm8wq2tKRGE4GHQ8R3BQLywbmtpH5/gwDEFt/Gu404NgVv+P+SrqkXF334/8tZvn8PkxovD1fXZa9WgwdkpbUU9mR7COyWaSwwREwdajrMKwe4MDHdV5M= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=Ry6SHqHy; 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="Ry6SHqHy" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 304F9C4CEDD for ; Thu, 13 Mar 2025 17:55:43 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741888543; bh=5KBLJlL6WBezu97iLhZh0a2nGayAwPdvVOuZg0otm18=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Ry6SHqHyvmExLjsVXxC2vK7CB8fmzOvRmlFgdyodDzUpv/G5Ri8Vc/3FbIPpX6pYa JlWzdx6YTJv8to/2/ney74uWaRrIEIOfZWfhGoBNt/1C7omGYkR8ryhU9maXhEkpce fL/jEn6g/KHYqdveRr9JtlqUS2FTW3JxUWQSv0trxFtiIE4s1uQH/spZHai5KvUeR+ vWZNP+9OslZVo1E0odvEg2htkQz4NOfzea2YaHTMPf4ZKlf8uCJtQhUDNiDtGv1Fb0 bIMXfLKQzzDaYEriHoOu+uEtmgVqkCZ/SCXAODAOuv0oAgHByGBZia9przw4Q0bMPJ cT40LFRzNgVXQ== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 3/7] btrfs: update outdated comment for overwrite_item() Date: Thu, 13 Mar 2025 17:55:33 +0000 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 The function is exclusively used for log replay since commit 3eb423442483 ("btrfs: remove outdated logic from overwrite_item() and add assertion"), so update the comment so that it doesn't say it can be used for logging. Also some minor rewording for clarity and while at it reformat the affected text so that it fits closer to the 80 characters limit for comments. Signed-off-by: Filipe Manana --- fs/btrfs/tree-log.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index f23feddb41c5..889b388c3708 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -376,12 +376,12 @@ static int process_one_buffer(struct btrfs_root *log, } /* - * Item overwrite used by replay and tree logging. eb, slot and key all refer - * to the src data we are copying out. + * Item overwrite used by log replay. The given eb, slot and key all refer to + * the source data we are copying out. * - * root is the tree we are copying into, and path is a scratch - * path for use in this function (it should be released on entry and - * will be released on exit). + * The given root is for the tree we are copying into, and path is a scratch + * path for use in this function (it should be released on entry and will be + * released on exit). * * If the key is already in the destination tree the existing item is * overwritten. If the existing item isn't big enough, it is extended. From patchwork Thu Mar 13 17:55:34 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14015694 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 438AA1EDA32 for ; Thu, 13 Mar 2025 17:55:44 +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=1741888545; cv=none; b=EkU8RihNB2xG2cDeq5F8dhzDrr8A4V45rFjfKahJyO2B9ZR9H+R3nyDV0/wBCUVM5D75xSd00TVLiIeLXxHmUBGhp8h2sonefNooHM1uzWVnBkIOGQSl6XKsaTM84OgzSGbkeYUJ0dWWbpfiqLtzqzqTPhMUKBpvp6k2Zl1v12Y= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741888545; c=relaxed/simple; bh=gRBuMwKWovCkPNcPV1bwatuEGULqlmDYuBJOz+l18go=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=JJ3IAKY9DtJsasSVw0D4c7Ix/HS5GRKBlY+4la4K1nZ/Ih/+rI4qXfozeefH1IGSyfqtWb2JFwYqBgWfiTQTYscgULWH6YW31lF5VkdQo2WWLT5W9zKyLO90nPtE/44918JF9/Vo6+dRqeKMkItvkGaYhgRmMq16OtVEtpGqlDs= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=GOIIkjGd; 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="GOIIkjGd" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 32140C4CEE3 for ; Thu, 13 Mar 2025 17:55:44 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741888544; bh=gRBuMwKWovCkPNcPV1bwatuEGULqlmDYuBJOz+l18go=; h=From:To:Subject:Date:In-Reply-To:References:From; b=GOIIkjGdYKyilXwI5pe/o09Xarw0bj01hKCcB5Vyg0npl89gjVdx1GiKdMGW4ipAC cEofmJo7s3HPzFmvC+ESTgOHI/I5DT3PBus3OimKMDzoZv1/svnDLErI+ZznGhqOPy gCh6OuElyU2v4VKsenM5C9gFuvwPqMzKeo1JHtx9wYWNOrW9nqQJ35hzZ5ABRmzJAN N6P6aknTfzWjNqFnhMEXIpoXlzbyfbrdNO1ZBYacUgES2yEISM569YvfFH+uYQ3K3p 5N5R1Vk1EX5UkjNTawB+pIc56GpqwIEWL9i7UrsbbCqpvJmWpM6b3/Wmv1cVBkGyP9 OXf6es7TST9qQ== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 4/7] btrfs: use memcmp_extent_buffer() at replay_one_extent() Date: Thu, 13 Mar 2025 17:55:34 +0000 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 Instead of using memcmp(), which requires copying both file extent items from each extent buffer into a local buffer, use memcmp_extent_buffer() so that we only need to copy one of the file extent items and directly use the extent buffer of the other file extent item for the comparison. This reduces code size, saves one memory copy and reduces stack usage. Signed-off-by: Filipe Manana --- fs/btrfs/tree-log.c | 19 ++++++------------- 1 file changed, 6 insertions(+), 13 deletions(-) diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 889b388c3708..7e0339f5fb6b 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -688,25 +688,18 @@ static noinline int replay_one_extent(struct btrfs_trans_handle *trans, if (ret == 0 && (found_type == BTRFS_FILE_EXTENT_REG || found_type == BTRFS_FILE_EXTENT_PREALLOC)) { - struct btrfs_file_extent_item cmp1; - struct btrfs_file_extent_item cmp2; - struct btrfs_file_extent_item *existing; - struct extent_buffer *leaf; - - leaf = path->nodes[0]; - existing = btrfs_item_ptr(leaf, path->slots[0], - struct btrfs_file_extent_item); + struct btrfs_file_extent_item existing; + unsigned long ptr; - read_extent_buffer(eb, &cmp1, (unsigned long)item, - sizeof(cmp1)); - read_extent_buffer(leaf, &cmp2, (unsigned long)existing, - sizeof(cmp2)); + ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]); + read_extent_buffer(path->nodes[0], &existing, ptr, sizeof(existing)); /* * we already have a pointer to this exact extent, * we don't have to do anything */ - if (memcmp(&cmp1, &cmp2, sizeof(cmp1)) == 0) { + if (memcmp_extent_buffer(eb, &existing, (unsigned long)item, + sizeof(existing)) == 0) { btrfs_release_path(path); goto out; } From patchwork Thu Mar 13 17:55:35 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14015695 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 3686C1EE7A8 for ; Thu, 13 Mar 2025 17:55:45 +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=1741888546; cv=none; b=tgcT+ZyO5maZisfShyvTdhtX+AA6L3djj4O7rM9jYXxDAMA3QePx9uSIMGJEn1B1kJoZj9ucI/Zyg7aKf5ApIKauSwSjgTA7JS/w0UodL9H7Ww3HuRiVoLp4KD2qWtApTa+4+RbPu2bdxeI4piW122/tbaDd11wGTje/M248/pw= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741888546; c=relaxed/simple; bh=6E7elXrNShAsXcmlKp8m0C7bfMznDt0njsegam2R7MM=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=YJ/uzDue/E3SsGpiE50/E+sFKrEAKZYldz5YQTCDIeNrAY5F3foqczeRcvLr0Agj8QFuJGxeIn1CoXVf+FvDwWYQhuyiF3D0WDAxxMdAwN94gvf0nRhItMjmuBi3AtVlswZqRP+5VPKn+jREwzdikcK5vtwhGqfkBMRoTgSTKc4= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=bbEOPkBJ; 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="bbEOPkBJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 33758C4CEDD for ; Thu, 13 Mar 2025 17:55:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741888545; bh=6E7elXrNShAsXcmlKp8m0C7bfMznDt0njsegam2R7MM=; h=From:To:Subject:Date:In-Reply-To:References:From; b=bbEOPkBJ5JI2Yw8wm0EfMLauY5oxIK3IscnX8DO9nCRsgY60SY/nSW43+Al+/jHCE 8xlnzMg7jrDbnMw8diKpV9YCi18osxe0xcm1ADDJDrv3Oda5fD4KiCCvL+UNPlgbZO 3ZKJyABw/7ojvatMsllftb6f9B0nw3KLtjuep/LepF8VspgoW/d09erkQ0kzGKkLmw LFEc1ab/l5CGvWNZJjJMHW+2v2F5ETRZcOv9/gdXCzZU80bBttJHKb3A1FWC6VXRdl mKtvzhPBPXYbTFe1cYJ00aiLls3JnkrZJoT27LTrN+yW5GW4lLJ4k2/gQvqqw4tXH3 /VRUg6+NaY/BA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 5/7] btrfs: remove redundant else statement from btrfs_log_inode_parent() Date: Thu, 13 Mar 2025 17:55:35 +0000 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 If we don't need to log new directory dentries, there's no point in having an else branch just to set 'ret' to zero, as it's already zero because everytime it gets a non-zero value we jump into one of the exit labels. So remove it, which reduces source code size and the module text size. Before this change: $ size fs/btrfs/btrfs.ko text data bss dec hex filename 1813855 163737 16920 1994512 1e6f10 fs/btrfs/btrfs.ko After this change: $ size fs/btrfs/btrfs.ko text data bss dec hex filename 1813807 163737 16920 1994464 1e6ee0 fs/btrfs/btrfs.ko Signed-off-by: Filipe Manana --- fs/btrfs/tree-log.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 7e0339f5fb6b..6c59c581ebe4 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -7146,8 +7146,6 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, if (log_dentries) ret = log_new_dir_dentries(trans, inode, ctx); - else - ret = 0; end_trans: if (ret < 0) { btrfs_set_log_full_commit(trans); From patchwork Thu Mar 13 17:55:36 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14015696 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 D70E31EE7BD for ; Thu, 13 Mar 2025 17:55:46 +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=1741888546; cv=none; b=RmP0BYD8VvrCq8VIs30GyCQUHIqPJKZLKU/bdWpanKQLP6cwGUyhP5GUavNYBeDvrzqRCu9EiEXupUFAAa3RZdeL6NyWLX2A43WgdTza5pcqupSLZAggK+1ziG39JEWy4iBhZHPUHK4g4XzUhQLzDOJfRZ1fWWbHRUfsrkhUvkA= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741888546; c=relaxed/simple; bh=uvF883+DhDkzjuxc4oFklMlYzQduJC7RnEBIjsm1TY0=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=Y07J1UnH8QNJhxBELJh5Ye6i98MaedocOXgZ3YIJHUQ0wFu5vd926DV2MKL37aNsuTmdYn2z5vSdn5ST61vZDBRbPOxoTHleL3EvsvZK/MuPEm5RxEA3s3ltJOHrf0Yev/b1tqc2hWakmuuUFyaxgzqr9N9IorTkxs32/208nwY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=jI0BWCSV; 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="jI0BWCSV" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3515CC4CEEB for ; Thu, 13 Mar 2025 17:55:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741888546; bh=uvF883+DhDkzjuxc4oFklMlYzQduJC7RnEBIjsm1TY0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=jI0BWCSVqtv4QnEjvH5yKyR8A4Jls+3e01iD3EAVFo0fmkKSs1DlT5tftZjvC2hZt YcTa8CFc/Du8Xmg8OYfz0oC3qu6wMRDrpTclproNH//CWUUb8A6gFmXwONicUdXnAp JcYgVAXAYfulakl0ioBc+bDTmSovVZzCrs08OSHt/6nc72P4A98L2EVr4Y2QcRQP2P nAjbKkQlwEpPvjzjFIO3wE9a4yUCP2XpYK268wGDy74YGIh5o0jUECTzB/WPQicN4d eWdNVpnegB/sswqQtQICM6gQHBrgo0YUnSO39EnTuLgs6PY9/Gkw4GERUiIu4Frk70 DTA/WchmSjsIA== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 6/7] btrfs: simplify condition for logging new dentries at btrfs_log_inode_parent() Date: Thu, 13 Mar 2025 17:55:36 +0000 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 There's no point in checking if the inode is a directory as ctx->log_new_dentries is only set in case we are logging a directory down the call chain of btrfs_log_inode(). So remove that check making the logic more simple and while at it add a comment about why use a local variable to track if we later need to log new dentries. Signed-off-by: Filipe Manana --- fs/btrfs/tree-log.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 6c59c581ebe4..6bc9f5f32393 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -7036,7 +7036,7 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, struct btrfs_root *root = inode->root; struct btrfs_fs_info *fs_info = root->fs_info; int ret = 0; - bool log_dentries = false; + bool log_dentries; if (btrfs_test_opt(fs_info, NOTREELOG)) { ret = BTRFS_LOG_FORCE_COMMIT; @@ -7090,8 +7090,11 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, goto end_trans; } - if (S_ISDIR(inode->vfs_inode.i_mode) && ctx->log_new_dentries) - log_dentries = true; + /* + * Track if we need to log dentries because ctx->log_new_dentries can + * be modified in the call chains below. + */ + log_dentries = ctx->log_new_dentries; /* * On unlink we must make sure all our current and old parent directory From patchwork Thu Mar 13 17:55:37 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Filipe Manana X-Patchwork-Id: 14015697 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 43E6B1E9B03 for ; Thu, 13 Mar 2025 17:55:47 +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=1741888548; cv=none; b=oa2wv6tKP7g15keT90Axq9EG6jAxBsYLEw4Rs4hLUB2z7+mfwQMYAu8OaxPFVqvM0oClqK/ps0YFnj1flSlmgnUoAf9lJz1x/DGZBo0rpyF6hdYSpf0gnjeR1iMnzDRVINv474zKmNQNgYCXgwDw7v9V9b6MWa8DGYcp5PoK3rI= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1741888548; c=relaxed/simple; bh=nOaoOzUG+UuZp3b1Q0pDG9qarC1xRMuvWMcuJeOhByc=; h=From:To:Subject:Date:Message-Id:In-Reply-To:References: MIME-Version; b=QfClizEgnJqJZnZNrr2rGWPiAweV3FUqnSpbEk2kP5NjDgh9+FO0iPhcXyCbFqoKn8oQMRxPKWdy3v11WeqLiSUojAggLs/QKHxd2xM/gUn6oqPmIWUsiwbT6GufKWq8MVHchCorkTbBpsqz+AI6fCpxLrxkffWWAh+PPXfoO8E= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.b=V+3XtUqJ; 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="V+3XtUqJ" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 36EBCC4CEDD for ; Thu, 13 Mar 2025 17:55:47 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1741888547; bh=nOaoOzUG+UuZp3b1Q0pDG9qarC1xRMuvWMcuJeOhByc=; h=From:To:Subject:Date:In-Reply-To:References:From; b=V+3XtUqJSAkv4HDObKeMULZImdWcJTQmxmdIzllkBQZACO7I8IJ6KSk/kN1RXQ3mN ODKSAkZ/5d3WUM1Wvi6Le14OlyFQLqweqyLmrxPcr4zqUj+LJxH5gI4I7DO2EGXTI/ Kpu8CIyrq74ZKFvFGQouAKFNr0E/ru86L1XGAco51oQtE5UMGiMt1AbvBzG+wwm6Q2 2ay53WxcekVZUKaf3ctv+M3IyjD+tj+t9D4NeW3YiLmuDX6PFbnLrjVljGvobPMc9L HeXW9YxelF6IGcym8m0kQ3AppRLnQKt2o5t506mQ9D2By/a/+GzXfJqBt0lRZECTnm M3/PLn4JfWDjg== From: fdmanana@kernel.org To: linux-btrfs@vger.kernel.org Subject: [PATCH 7/7] btrfs: remove end_no_trans label from btrfs_log_inode_parent() Date: Thu, 13 Mar 2025 17:55:37 +0000 Message-Id: <97d14290bfd4ad71700c43e73ac887a92f745bd1.1741887950.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 It's a pointless label as we don't have to do anything under it other than return from the function. So remove it and directly return from the function where we used to goto. Signed-off-by: Filipe Manana --- fs/btrfs/tree-log.c | 28 ++++++++++------------------ 1 file changed, 10 insertions(+), 18 deletions(-) diff --git a/fs/btrfs/tree-log.c b/fs/btrfs/tree-log.c index 6bc9f5f32393..90dc094cfa5e 100644 --- a/fs/btrfs/tree-log.c +++ b/fs/btrfs/tree-log.c @@ -7038,24 +7038,18 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, int ret = 0; bool log_dentries; - if (btrfs_test_opt(fs_info, NOTREELOG)) { - ret = BTRFS_LOG_FORCE_COMMIT; - goto end_no_trans; - } + if (btrfs_test_opt(fs_info, NOTREELOG)) + return BTRFS_LOG_FORCE_COMMIT; - if (btrfs_root_refs(&root->root_item) == 0) { - ret = BTRFS_LOG_FORCE_COMMIT; - goto end_no_trans; - } + if (btrfs_root_refs(&root->root_item) == 0) + return BTRFS_LOG_FORCE_COMMIT; /* * If we're logging an inode from a subvolume created in the current * transaction we must force a commit since the root is not persisted. */ - if (btrfs_root_generation(&root->root_item) == trans->transid) { - ret = BTRFS_LOG_FORCE_COMMIT; - goto end_no_trans; - } + if (btrfs_root_generation(&root->root_item) == trans->transid) + return BTRFS_LOG_FORCE_COMMIT; /* * Skip already logged inodes or inodes corresponding to tmpfiles @@ -7064,14 +7058,12 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, */ if ((btrfs_inode_in_log(inode, trans->transid) && list_empty(&ctx->ordered_extents)) || - inode->vfs_inode.i_nlink == 0) { - ret = BTRFS_NO_LOG_SYNC; - goto end_no_trans; - } + inode->vfs_inode.i_nlink == 0) + return BTRFS_NO_LOG_SYNC; ret = start_log_trans(trans, root, ctx); if (ret) - goto end_no_trans; + return ret; ret = btrfs_log_inode(trans, inode, inode_only, ctx); if (ret) @@ -7158,7 +7150,7 @@ static int btrfs_log_inode_parent(struct btrfs_trans_handle *trans, if (ret) btrfs_remove_log_ctx(root, ctx); btrfs_end_log_trans(root); -end_no_trans: + return ret; }