public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: fdmanana@kernel.org
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 16/18] btrfs: move up backref sharedness cache store and lookup functions
Date: Mon, 10 Oct 2022 11:22:18 +0100	[thread overview]
Message-ID: <4feb670d89ca1506088d461df2db30fbb40d689e.1665396437.git.fdmanana@suse.com> (raw)
In-Reply-To: <cover.1665396437.git.fdmanana@suse.com>

From: Filipe Manana <fdmanana@suse.com>

Move the static functions to lookup and store sharedness check of an
extent buffer to a location above find_all_parents(), because in the
next patch the lookup function will be used by find_all_parents().
The store function is also moved just because it's the counter part
to the lookup function and it's best to have their definitions close
together.

Signed-off-by: Filipe Manana <fdmanana@suse.com>
---
 fs/btrfs/backref.c | 236 ++++++++++++++++++++++-----------------------
 1 file changed, 118 insertions(+), 118 deletions(-)

diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index 8d4e7ddbefb4..977f07903156 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -1167,6 +1167,124 @@ static int add_keyed_refs(struct btrfs_root *extent_root,
 	return ret;
 }
 
+/*
+ * The caller has joined a transaction or is holding a read lock on the
+ * fs_info->commit_root_sem semaphore, so no need to worry about the root's last
+ * snapshot field changing while updating or checking the cache.
+ */
+static bool lookup_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx,
+					struct btrfs_root *root,
+					u64 bytenr, int level, bool *is_shared)
+{
+	struct btrfs_backref_shared_cache_entry *entry;
+
+	if (!ctx->use_path_cache)
+		return false;
+
+	if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL))
+		return false;
+
+	/*
+	 * Level -1 is used for the data extent, which is not reliable to cache
+	 * because its reference count can increase or decrease without us
+	 * realizing. We cache results only for extent buffers that lead from
+	 * the root node down to the leaf with the file extent item.
+	 */
+	ASSERT(level >= 0);
+
+	entry = &ctx->path_cache_entries[level];
+
+	/* Unused cache entry or being used for some other extent buffer. */
+	if (entry->bytenr != bytenr)
+		return false;
+
+	/*
+	 * We cached a false result, but the last snapshot generation of the
+	 * root changed, so we now have a snapshot. Don't trust the result.
+	 */
+	if (!entry->is_shared &&
+	    entry->gen != btrfs_root_last_snapshot(&root->root_item))
+		return false;
+
+	/*
+	 * If we cached a true result and the last generation used for dropping
+	 * a root changed, we can not trust the result, because the dropped root
+	 * could be a snapshot sharing this extent buffer.
+	 */
+	if (entry->is_shared &&
+	    entry->gen != btrfs_get_last_root_drop_gen(root->fs_info))
+		return false;
+
+	*is_shared = entry->is_shared;
+	/*
+	 * If the node at this level is shared, than all nodes below are also
+	 * shared. Currently some of the nodes below may be marked as not shared
+	 * because we have just switched from one leaf to another, and switched
+	 * also other nodes above the leaf and below the current level, so mark
+	 * them as shared.
+	 */
+	if (*is_shared) {
+		for (int i = 0; i < level; i++) {
+			ctx->path_cache_entries[i].is_shared = true;
+			ctx->path_cache_entries[i].gen = entry->gen;
+		}
+	}
+
+	return true;
+}
+
+/*
+ * The caller has joined a transaction or is holding a read lock on the
+ * fs_info->commit_root_sem semaphore, so no need to worry about the root's last
+ * snapshot field changing while updating or checking the cache.
+ */
+static void store_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx,
+				       struct btrfs_root *root,
+				       u64 bytenr, int level, bool is_shared)
+{
+	struct btrfs_backref_shared_cache_entry *entry;
+	u64 gen;
+
+	if (!ctx->use_path_cache)
+		return;
+
+	if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL))
+		return;
+
+	/*
+	 * Level -1 is used for the data extent, which is not reliable to cache
+	 * because its reference count can increase or decrease without us
+	 * realizing. We cache results only for extent buffers that lead from
+	 * the root node down to the leaf with the file extent item.
+	 */
+	ASSERT(level >= 0);
+
+	if (is_shared)
+		gen = btrfs_get_last_root_drop_gen(root->fs_info);
+	else
+		gen = btrfs_root_last_snapshot(&root->root_item);
+
+	entry = &ctx->path_cache_entries[level];
+	entry->bytenr = bytenr;
+	entry->is_shared = is_shared;
+	entry->gen = gen;
+
+	/*
+	 * If we found an extent buffer is shared, set the cache result for all
+	 * extent buffers below it to true. As nodes in the path are COWed,
+	 * their sharedness is moved to their children, and if a leaf is COWed,
+	 * then the sharedness of a data extent becomes direct, the refcount of
+	 * data extent is increased in the extent item at the extent tree.
+	 */
+	if (is_shared) {
+		for (int i = 0; i < level; i++) {
+			entry = &ctx->path_cache_entries[i];
+			entry->is_shared = is_shared;
+			entry->gen = gen;
+		}
+	}
+}
+
 /*
  * this adds all existing backrefs (inline backrefs, backrefs and delayed
  * refs) for the given bytenr to the refs list, merges duplicates and resolves
@@ -1546,124 +1664,6 @@ int btrfs_find_all_roots(struct btrfs_trans_handle *trans,
 	return ret;
 }
 
-/*
- * The caller has joined a transaction or is holding a read lock on the
- * fs_info->commit_root_sem semaphore, so no need to worry about the root's last
- * snapshot field changing while updating or checking the cache.
- */
-static bool lookup_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx,
-					struct btrfs_root *root,
-					u64 bytenr, int level, bool *is_shared)
-{
-	struct btrfs_backref_shared_cache_entry *entry;
-
-	if (!ctx->use_path_cache)
-		return false;
-
-	if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL))
-		return false;
-
-	/*
-	 * Level -1 is used for the data extent, which is not reliable to cache
-	 * because its reference count can increase or decrease without us
-	 * realizing. We cache results only for extent buffers that lead from
-	 * the root node down to the leaf with the file extent item.
-	 */
-	ASSERT(level >= 0);
-
-	entry = &ctx->path_cache_entries[level];
-
-	/* Unused cache entry or being used for some other extent buffer. */
-	if (entry->bytenr != bytenr)
-		return false;
-
-	/*
-	 * We cached a false result, but the last snapshot generation of the
-	 * root changed, so we now have a snapshot. Don't trust the result.
-	 */
-	if (!entry->is_shared &&
-	    entry->gen != btrfs_root_last_snapshot(&root->root_item))
-		return false;
-
-	/*
-	 * If we cached a true result and the last generation used for dropping
-	 * a root changed, we can not trust the result, because the dropped root
-	 * could be a snapshot sharing this extent buffer.
-	 */
-	if (entry->is_shared &&
-	    entry->gen != btrfs_get_last_root_drop_gen(root->fs_info))
-		return false;
-
-	*is_shared = entry->is_shared;
-	/*
-	 * If the node at this level is shared, than all nodes below are also
-	 * shared. Currently some of the nodes below may be marked as not shared
-	 * because we have just switched from one leaf to another, and switched
-	 * also other nodes above the leaf and below the current level, so mark
-	 * them as shared.
-	 */
-	if (*is_shared) {
-		for (int i = 0; i < level; i++) {
-			ctx->path_cache_entries[i].is_shared = true;
-			ctx->path_cache_entries[i].gen = entry->gen;
-		}
-	}
-
-	return true;
-}
-
-/*
- * The caller has joined a transaction or is holding a read lock on the
- * fs_info->commit_root_sem semaphore, so no need to worry about the root's last
- * snapshot field changing while updating or checking the cache.
- */
-static void store_backref_shared_cache(struct btrfs_backref_share_check_ctx *ctx,
-				       struct btrfs_root *root,
-				       u64 bytenr, int level, bool is_shared)
-{
-	struct btrfs_backref_shared_cache_entry *entry;
-	u64 gen;
-
-	if (!ctx->use_path_cache)
-		return;
-
-	if (WARN_ON_ONCE(level >= BTRFS_MAX_LEVEL))
-		return;
-
-	/*
-	 * Level -1 is used for the data extent, which is not reliable to cache
-	 * because its reference count can increase or decrease without us
-	 * realizing. We cache results only for extent buffers that lead from
-	 * the root node down to the leaf with the file extent item.
-	 */
-	ASSERT(level >= 0);
-
-	if (is_shared)
-		gen = btrfs_get_last_root_drop_gen(root->fs_info);
-	else
-		gen = btrfs_root_last_snapshot(&root->root_item);
-
-	entry = &ctx->path_cache_entries[level];
-	entry->bytenr = bytenr;
-	entry->is_shared = is_shared;
-	entry->gen = gen;
-
-	/*
-	 * If we found an extent buffer is shared, set the cache result for all
-	 * extent buffers below it to true. As nodes in the path are COWed,
-	 * their sharedness is moved to their children, and if a leaf is COWed,
-	 * then the sharedness of a data extent becomes direct, the refcount of
-	 * data extent is increased in the extent item at the extent tree.
-	 */
-	if (is_shared) {
-		for (int i = 0; i < level; i++) {
-			entry = &ctx->path_cache_entries[i];
-			entry->is_shared = is_shared;
-			entry->gen = gen;
-		}
-	}
-}
-
 struct btrfs_backref_share_check_ctx *btrfs_alloc_backref_share_check_ctx(void)
 {
 	struct btrfs_backref_share_check_ctx *ctx;
-- 
2.35.1


  parent reply	other threads:[~2022-10-10 10:23 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-10 10:22 [PATCH 00/18] btrfs: fixes, cleanups and optimizations around fiemap fdmanana
2022-10-10 10:22 ` [PATCH 01/18] btrfs: fix processing of delayed tree block refs during backref walking fdmanana
2022-10-10 10:22 ` [PATCH 02/18] btrfs: ignore fiemap path cache if we have multiple leaves for a data extent fdmanana
2022-10-10 10:22 ` [PATCH 03/18] btrfs: get the next extent map during fiemap/lseek more efficiently fdmanana
2022-10-10 10:22 ` [PATCH 04/18] btrfs: skip unnecessary extent map searches during fiemap and lseek fdmanana
2022-10-10 10:22 ` [PATCH 05/18] btrfs: skip unnecessary delalloc search " fdmanana
2022-10-10 10:22 ` [PATCH 06/18] btrfs: drop pointless memset when cloning extent buffer fdmanana
2022-10-10 10:22 ` [PATCH 07/18] btrfs: drop redundant bflags initialization when allocating " fdmanana
2022-10-10 10:22 ` [PATCH 08/18] btrfs: remove checks for a root with id 0 during backref walking fdmanana
2022-10-10 10:22 ` [PATCH 09/18] btrfs: remove checks for a 0 inode number " fdmanana
2022-10-10 10:22 ` [PATCH 10/18] btrfs: directly pass the inode to btrfs_is_data_extent_shared() fdmanana
2022-10-10 10:22 ` [PATCH 11/18] btrfs: turn the backref sharedness check cache into a context object fdmanana
2022-10-10 10:22 ` [PATCH 12/18] btrfs: move ulists to data extent sharedness check context fdmanana
2022-10-10 10:22 ` [PATCH 13/18] btrfs: remove roots ulist when checking data extent sharedness fdmanana
2022-10-10 10:22 ` [PATCH 14/18] btrfs: remove useless logic when finding parent nodes fdmanana
2022-10-10 10:22 ` [PATCH 15/18] btrfs: cache sharedness of the last few data extents during fiemap fdmanana
2022-10-10 10:22 ` fdmanana [this message]
2022-10-10 10:22 ` [PATCH 17/18] btrfs: avoid duplicated resolution of indirect backrefs " fdmanana
2022-10-10 10:22 ` [PATCH 18/18] btrfs: avoid unnecessary " fdmanana
2022-10-11 12:16 ` [PATCH v2 00/19] btrfs: fixes, cleanups and optimizations around fiemap fdmanana
2022-10-11 12:16   ` [PATCH v2 01/19] btrfs: fix processing of delayed data refs during backref walking fdmanana
2022-10-11 12:16   ` [PATCH v2 02/19] btrfs: fix processing of delayed tree block " fdmanana
2022-10-11 12:16   ` [PATCH v2 03/19] btrfs: ignore fiemap path cache if we have multiple leaves for a data extent fdmanana
2022-10-11 12:16   ` [PATCH v2 04/19] btrfs: get the next extent map during fiemap/lseek more efficiently fdmanana
2022-10-11 12:16   ` [PATCH v2 05/19] btrfs: skip unnecessary extent map searches during fiemap and lseek fdmanana
2022-10-11 12:16   ` [PATCH v2 06/19] btrfs: skip unnecessary delalloc search " fdmanana
2022-10-11 12:16   ` [PATCH v2 07/19] btrfs: drop pointless memset when cloning extent buffer fdmanana
2022-10-11 12:16   ` [PATCH v2 08/19] btrfs: drop redundant bflags initialization when allocating " fdmanana
2022-10-11 12:16   ` [PATCH v2 09/19] btrfs: remove checks for a root with id 0 during backref walking fdmanana
2022-10-11 12:17   ` [PATCH v2 10/19] btrfs: remove checks for a 0 inode number " fdmanana
2022-10-11 12:17   ` [PATCH v2 11/19] btrfs: directly pass the inode to btrfs_is_data_extent_shared() fdmanana
2022-10-11 12:17   ` [PATCH v2 12/19] btrfs: turn the backref sharedness check cache into a context object fdmanana
2022-10-11 12:17   ` [PATCH v2 13/19] btrfs: move ulists to data extent sharedness check context fdmanana
2022-10-11 12:17   ` [PATCH v2 14/19] btrfs: remove roots ulist when checking data extent sharedness fdmanana
2022-10-11 12:17   ` [PATCH v2 15/19] btrfs: remove useless logic when finding parent nodes fdmanana
2022-10-11 12:17   ` [PATCH v2 16/19] btrfs: cache sharedness of the last few data extents during fiemap fdmanana
2022-10-11 12:17   ` [PATCH v2 17/19] btrfs: move up backref sharedness cache store and lookup functions fdmanana
2022-10-11 12:17   ` [PATCH v2 18/19] btrfs: avoid duplicated resolution of indirect backrefs during fiemap fdmanana
2022-10-11 12:17   ` [PATCH v2 19/19] btrfs: avoid unnecessary " fdmanana
2022-10-11 12:54   ` [PATCH v2 00/19] btrfs: fixes, cleanups and optimizations around fiemap David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=4feb670d89ca1506088d461df2db30fbb40d689e.1665396437.git.fdmanana@suse.com \
    --to=fdmanana@kernel.org \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox