All of lore.kernel.org
 help / color / mirror / Atom feed
From: fdmanana@kernel.org
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 04/19] btrfs: send: avoid extra b+tree searches when checking reference overrides
Date: Wed, 11 Jan 2023 11:36:05 +0000	[thread overview]
Message-ID: <9c5cf707a0db0c0de55809fe56446cf7613ab042.1673436276.git.fdmanana@suse.com> (raw)
In-Reply-To: <cover.1673436276.git.fdmanana@suse.com>

From: Filipe Manana <fdmanana@suse.com>

During an incremental send, when processing the new references of an inode
(either it's a new inode or an existing one renamed/moved), he will search
the b+tree of the send or parent roots in order to find out the inode item
of the parent directory and extract its generation. However we are doing
that search twice, once with is_inode_existent() -> get_cur_inode_state()
and then again at did_overwrite_ref() or will_overwrite_ref().

So avoid that and get the generation at get_cur_inode_state() and then
propagate it up to did_overwrite_ref() and will_overwrite_ref().

This patch is part of a larger patchset and the changelog of the last
patch in the series contains a sample performance test and results.
The patches that comprise the patchset are the following:

  btrfs: send: directly return from did_overwrite_ref() and simplify it
  btrfs: send: avoid unnecessary generation search at did_overwrite_ref()
  btrfs: send: directly return from will_overwrite_ref() and simplify it
  btrfs: send: avoid extra b+tree searches when checking reference overrides
  btrfs: send: remove send_progress argument from can_rmdir()
  btrfs: send: avoid duplicated orphan dir allocation and initialization
  btrfs: send: avoid unnecessary orphan dir rbtree search at can_rmdir()
  btrfs: send: reduce searches on parent root when checking if dir can be removed
  btrfs: send: iterate waiting dir move rbtree only once when processing refs
  btrfs: send: use MT_FLAGS_LOCK_EXTERN for the backref cache maple tree
  btrfs: send: initialize all the red black trees earlier
  btrfs: send: genericize the backref cache to allow it to be reused
  btrfs: adapt lru cache to allow for 64 bits keys on 32 bits systems
  btrfs: send: cache information about created directories
  btrfs: allow a generation number to be associated with lru cache entries
  btrfs: add an api to delete a specific entry from the lru cache
  btrfs: send: use the lru cache to implement the name cache
  btrfs: send: update size of roots array for backref cache entries
  btrfs: send: cache utimes operations for directories if possible

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

diff --git a/fs/btrfs/send.c b/fs/btrfs/send.c
index 9be3d7db85d6..6332add4865c 100644
--- a/fs/btrfs/send.c
+++ b/fs/btrfs/send.c
@@ -1884,7 +1884,8 @@ enum inode_state {
 	inode_state_did_delete,
 };
 
-static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
+static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen,
+			       u64 *send_gen, u64 *parent_gen)
 {
 	int ret;
 	int left_ret;
@@ -1898,6 +1899,8 @@ static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
 		goto out;
 	left_ret = (info.nlink == 0) ? -ENOENT : ret;
 	left_gen = info.gen;
+	if (send_gen)
+		*send_gen = (left_ret == -ENOENT) ? 0 : info.gen;
 
 	if (!sctx->parent_root) {
 		right_ret = -ENOENT;
@@ -1907,6 +1910,8 @@ static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
 			goto out;
 		right_ret = (info.nlink == 0) ? -ENOENT : ret;
 		right_gen = info.gen;
+		if (parent_gen)
+			*parent_gen = (right_ret == -ENOENT) ? 0 : info.gen;
 	}
 
 	if (!left_ret && !right_ret) {
@@ -1951,14 +1956,15 @@ static int get_cur_inode_state(struct send_ctx *sctx, u64 ino, u64 gen)
 	return ret;
 }
 
-static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen)
+static int is_inode_existent(struct send_ctx *sctx, u64 ino, u64 gen,
+			     u64 *send_gen, u64 *parent_gen)
 {
 	int ret;
 
 	if (ino == BTRFS_FIRST_FREE_OBJECTID)
 		return 1;
 
-	ret = get_cur_inode_state(sctx, ino, gen);
+	ret = get_cur_inode_state(sctx, ino, gen, send_gen, parent_gen);
 	if (ret < 0)
 		goto out;
 
@@ -2120,14 +2126,14 @@ static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
 			      u64 *who_ino, u64 *who_gen, u64 *who_mode)
 {
 	int ret;
-	u64 gen;
+	u64 parent_root_dir_gen;
 	u64 other_inode = 0;
 	struct btrfs_inode_info info;
 
 	if (!sctx->parent_root)
 		return 0;
 
-	ret = is_inode_existent(sctx, dir, dir_gen);
+	ret = is_inode_existent(sctx, dir, dir_gen, NULL, &parent_root_dir_gen);
 	if (ret <= 0)
 		return 0;
 
@@ -2135,17 +2141,13 @@ static int will_overwrite_ref(struct send_ctx *sctx, u64 dir, u64 dir_gen,
 	 * If we have a parent root we need to verify that the parent dir was
 	 * not deleted and then re-created, if it was then we have no overwrite
 	 * and we can just unlink this entry.
+	 *
+	 * @parent_root_dir_gen was set to 0 if the inode does not exists in the
+	 * parent root.
 	 */
-	if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID) {
-		ret = get_inode_gen(sctx->parent_root, dir, &gen);
-		if (ret == -ENOENT)
-			return 0;
-		else if (ret < 0)
-			return ret;
-
-		if (gen != dir_gen)
-			return 0;
-	}
+	if (sctx->parent_root && dir != BTRFS_FIRST_FREE_OBJECTID &&
+	    parent_root_dir_gen != dir_gen)
+		return 0;
 
 	ret = lookup_dir_item_inode(sctx->parent_root, dir, name, name_len,
 				    &other_inode);
@@ -2189,26 +2191,21 @@ static int did_overwrite_ref(struct send_ctx *sctx,
 	int ret;
 	u64 ow_inode;
 	u64 ow_gen = 0;
+	u64 send_root_dir_gen;
 
 	if (!sctx->parent_root)
 		return 0;
 
-	ret = is_inode_existent(sctx, dir, dir_gen);
+	ret = is_inode_existent(sctx, dir, dir_gen, &send_root_dir_gen, NULL);
 	if (ret <= 0)
 		return ret;
 
-	if (dir != BTRFS_FIRST_FREE_OBJECTID) {
-		u64 gen;
-
-		ret = get_inode_gen(sctx->send_root, dir, &gen);
-		if (ret == -ENOENT)
-			return 0;
-		else if (ret < 0)
-			return ret;
-
-		if (gen != dir_gen)
-			return 0;
-	}
+	/*
+	 * @send_root_dir_gen was set to 0 if the inode does not exists in the
+	 * send root.
+	 */
+	if (dir != BTRFS_FIRST_FREE_OBJECTID && send_root_dir_gen != dir_gen)
+		return 0;
 
 	/* check if the ref was overwritten by another ref */
 	ret = lookup_dir_item_inode(sctx->send_root, dir, name, name_len,
@@ -2444,7 +2441,7 @@ static int __get_cur_name_and_parent(struct send_ctx *sctx,
 	 * This should only happen for the parent dir that we determine in
 	 * record_new_ref_if_needed().
 	 */
-	ret = is_inode_existent(sctx, ino, gen);
+	ret = is_inode_existent(sctx, ino, gen, NULL, NULL);
 	if (ret < 0)
 		goto out;
 
@@ -4240,7 +4237,7 @@ static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
 	 * "testdir_2".
 	 */
 	list_for_each_entry(cur, &sctx->new_refs, list) {
-		ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
+		ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
 		if (ret < 0)
 			goto out;
 		if (ret == inode_state_will_create)
@@ -4356,7 +4353,7 @@ static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
 		 * parent directory out of order. But we need to check if this
 		 * did already happen before due to other refs in the same dir.
 		 */
-		ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
+		ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
 		if (ret < 0)
 			goto out;
 		if (ret == inode_state_will_create) {
@@ -4562,7 +4559,7 @@ static int process_recorded_refs(struct send_ctx *sctx, int *pending_move)
 		if (cur->dir > sctx->cur_ino)
 			continue;
 
-		ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen);
+		ret = get_cur_inode_state(sctx, cur->dir, cur->dir_gen, NULL, NULL);
 		if (ret < 0)
 			goto out;
 
-- 
2.35.1


  parent reply	other threads:[~2023-01-11 11:39 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-11 11:36 [PATCH 00/19] btrfs: some optimizations for send fdmanana
2023-01-11 11:36 ` [PATCH 01/19] btrfs: send: directly return from did_overwrite_ref() and simplify it fdmanana
2023-01-11 11:36 ` [PATCH 02/19] btrfs: send: avoid unnecessary generation search at did_overwrite_ref() fdmanana
2023-01-11 11:36 ` [PATCH 03/19] btrfs: send: directly return from will_overwrite_ref() and simplify it fdmanana
2023-01-11 11:36 ` fdmanana [this message]
2023-01-11 11:36 ` [PATCH 05/19] btrfs: send: remove send_progress argument from can_rmdir() fdmanana
2023-01-11 11:36 ` [PATCH 06/19] btrfs: send: avoid duplicated orphan dir allocation and initialization fdmanana
2023-01-11 11:36 ` [PATCH 07/19] btrfs: send: avoid unnecessary orphan dir rbtree search at can_rmdir() fdmanana
2023-01-11 11:36 ` [PATCH 08/19] btrfs: send: reduce searches on parent root when checking if dir can be removed fdmanana
2023-01-11 11:36 ` [PATCH 09/19] btrfs: send: iterate waiting dir move rbtree only once when processing refs fdmanana
2023-01-11 11:36 ` [PATCH 10/19] btrfs: send: use MT_FLAGS_LOCK_EXTERN for the backref cache maple tree fdmanana
2023-01-11 11:36 ` [PATCH 11/19] btrfs: send: initialize all the red black trees earlier fdmanana
2023-01-11 11:36 ` [PATCH 12/19] btrfs: send: genericize the backref cache to allow it to be reused fdmanana
2023-01-11 11:36 ` [PATCH 13/19] btrfs: adapt lru cache to allow for 64 bits keys on 32 bits systems fdmanana
2023-01-11 11:36 ` [PATCH 14/19] btrfs: send: cache information about created directories fdmanana
2023-01-11 11:36 ` [PATCH 15/19] btrfs: allow a generation number to be associated with lru cache entries fdmanana
2023-01-11 11:36 ` [PATCH 16/19] btrfs: add an api to delete a specific entry from the lru cache fdmanana
2023-01-11 11:36 ` [PATCH 17/19] btrfs: send: use the lru cache to implement the name cache fdmanana
2023-01-11 11:36 ` [PATCH 18/19] btrfs: send: update size of roots array for backref cache entries fdmanana
2023-01-11 11:36 ` [PATCH 19/19] btrfs: send: cache utimes operations for directories if possible fdmanana
2023-01-19 19:39 ` [PATCH v2 00/18] btrfs: some optimizations for send fdmanana
2023-01-19 19:39   ` [PATCH v2 01/18] btrfs: send: directly return from did_overwrite_ref() and simplify it fdmanana
2023-01-19 19:39   ` [PATCH v2 02/18] btrfs: send: avoid unnecessary generation search at did_overwrite_ref() fdmanana
2023-01-19 19:39   ` [PATCH v2 03/18] btrfs: send: directly return from will_overwrite_ref() and simplify it fdmanana
2023-01-19 19:39   ` [PATCH v2 04/18] btrfs: send: avoid extra b+tree searches when checking reference overrides fdmanana
2023-01-19 19:39   ` [PATCH v2 05/18] btrfs: send: remove send_progress argument from can_rmdir() fdmanana
2023-01-19 19:39   ` [PATCH v2 06/18] btrfs: send: avoid duplicated orphan dir allocation and initialization fdmanana
2023-01-19 19:39   ` [PATCH v2 07/18] btrfs: send: avoid unnecessary orphan dir rbtree search at can_rmdir() fdmanana
2023-01-19 19:39   ` [PATCH v2 08/18] btrfs: send: reduce searches on parent root when checking if dir can be removed fdmanana
2023-01-19 19:39   ` [PATCH v2 09/18] btrfs: send: iterate waiting dir move rbtree only once when processing refs fdmanana
2023-01-19 19:39   ` [PATCH v2 10/18] btrfs: send: initialize all the red black trees earlier fdmanana
2023-01-19 19:39   ` [PATCH v2 11/18] btrfs: send: genericize the backref cache to allow it to be reused fdmanana
2023-01-19 19:39   ` [PATCH v2 12/18] btrfs: adapt lru cache to allow for 64 bits keys on 32 bits systems fdmanana
2023-01-19 19:39   ` [PATCH v2 13/18] btrfs: send: cache information about created directories fdmanana
2023-01-19 19:39   ` [PATCH v2 14/18] btrfs: allow a generation number to be associated with lru cache entries fdmanana
2023-01-19 19:39   ` [PATCH v2 15/18] btrfs: add an api to delete a specific entry from the lru cache fdmanana
2023-01-19 19:39   ` [PATCH v2 16/18] btrfs: send: use the lru cache to implement the name cache fdmanana
2023-01-19 19:39   ` [PATCH v2 17/18] btrfs: send: update size of roots array for backref cache entries fdmanana
2023-01-19 19:39   ` [PATCH v2 18/18] btrfs: send: cache utimes operations for directories if possible fdmanana
2023-01-20 18:45   ` [PATCH v2 00/18] btrfs: some optimizations for send 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=9c5cf707a0db0c0de55809fe56446cf7613ab042.1673436276.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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.