linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Marcos Paulo de Souza <mpdesouza@suse.com>
To: linux-btrfs@vger.kernel.org
Cc: dsterba@suse.com, nborisov@suse.com,
	Marcos Paulo de Souza <mpdesouza@suse.com>
Subject: [PATCH 2/7] btrfs: backref: Use btrfs_find_item in btrfs_find_one_extref
Date: Wed,  4 Aug 2021 15:48:49 -0300	[thread overview]
Message-ID: <20210804184854.10696-3-mpdesouza@suse.com> (raw)
In-Reply-To: <20210804184854.10696-1-mpdesouza@suse.com>

btrfs_find_one_extref is using btrfs_search_slot and iterating over the
slots, but in reality it only desires to find an extref, since there is
a break without any condition at the end of the while clause.

The function can be dramatically simplified by using btrfs_find_item, which
calls the btrfs_search_slot, compares if the objectid and type found
are the same of those passed as search key, and calls
btrfs_item_key_to_cpu if no error was found.

No functional changes.

Signed-off-by: Marcos Paulo de Souza <mpdesouza@suse.com>
---
 fs/btrfs/backref.c | 64 ++++++++--------------------------------------
 1 file changed, 11 insertions(+), 53 deletions(-)

diff --git a/fs/btrfs/backref.c b/fs/btrfs/backref.c
index 9e92faaafa02..57b955c8a875 100644
--- a/fs/btrfs/backref.c
+++ b/fs/btrfs/backref.c
@@ -1588,67 +1588,25 @@ int btrfs_find_one_extref(struct btrfs_root *root, u64 inode_objectid,
 			  struct btrfs_inode_extref **ret_extref,
 			  u64 *found_off)
 {
-	int ret, slot;
+	int ret;
 	struct btrfs_key key;
-	struct btrfs_key found_key;
 	struct btrfs_inode_extref *extref;
-	const struct extent_buffer *leaf;
 	unsigned long ptr;
 
-	key.objectid = inode_objectid;
-	key.type = BTRFS_INODE_EXTREF_KEY;
-	key.offset = start_off;
-
-	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+	ret = btrfs_find_item(root, path, inode_objectid, BTRFS_INODE_EXTREF_KEY,
+			      start_off, &key);
 	if (ret < 0)
 		return ret;
+	else if (ret > 0)
+		return -ENOENT;
 
-	while (1) {
-		leaf = path->nodes[0];
-		slot = path->slots[0];
-		if (slot >= btrfs_header_nritems(leaf)) {
-			/*
-			 * If the item at offset is not found,
-			 * btrfs_search_slot will point us to the slot
-			 * where it should be inserted. In our case
-			 * that will be the slot directly before the
-			 * next INODE_REF_KEY_V2 item. In the case
-			 * that we're pointing to the last slot in a
-			 * leaf, we must move one leaf over.
-			 */
-			ret = btrfs_next_leaf(root, path);
-			if (ret) {
-				if (ret >= 1)
-					ret = -ENOENT;
-				break;
-			}
-			continue;
-		}
-
-		btrfs_item_key_to_cpu(leaf, &found_key, slot);
-
-		/*
-		 * Check that we're still looking at an extended ref key for
-		 * this particular objectid. If we have different
-		 * objectid or type then there are no more to be found
-		 * in the tree and we can exit.
-		 */
-		ret = -ENOENT;
-		if (found_key.objectid != inode_objectid)
-			break;
-		if (found_key.type != BTRFS_INODE_EXTREF_KEY)
-			break;
-
-		ret = 0;
-		ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
-		extref = (struct btrfs_inode_extref *)ptr;
-		*ret_extref = extref;
-		if (found_off)
-			*found_off = found_key.offset;
-		break;
-	}
+	ptr = btrfs_item_ptr_offset(path->nodes[0], path->slots[0]);
+	extref = (struct btrfs_inode_extref *)ptr;
+	*ret_extref = extref;
+	if (found_off)
+		*found_off = key.offset;
 
-	return ret;
+	return 0;
 }
 
 /*
-- 
2.31.1


  parent reply	other threads:[~2021-08-04 18:49 UTC|newest]

Thread overview: 22+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-04 18:48 [PATCH 0/7] btrfs: Use btrfs_find_item whenever possible Marcos Paulo de Souza
2021-08-04 18:48 ` [PATCH 1/7] btrfs: Reorder btrfs_find_item arguments Marcos Paulo de Souza
2021-08-05  2:16   ` Qu Wenruo
2021-08-05 17:28     ` Marcos Paulo de Souza
2021-08-05 22:23       ` Qu Wenruo
2021-08-16 16:51   ` David Sterba
2021-08-04 18:48 ` Marcos Paulo de Souza [this message]
2021-08-05  6:33   ` [PATCH 2/7] btrfs: backref: Use btrfs_find_item in btrfs_find_one_extref Qu Wenruo
2021-08-04 18:48 ` [PATCH 3/7] btrfs: zoned: Use btrfs_find_item in calculate_emulated_zone_size Marcos Paulo de Souza
2021-08-05  6:39   ` Qu Wenruo
2021-08-06  5:52     ` Naohiro Aota
2021-08-06  6:11       ` Qu Wenruo
2021-08-06  6:18   ` Naohiro Aota
2021-08-04 18:48 ` [PATCH 4/7] btrfs: root-tree: Use btrfs_find_item in btrfs_find_orphan_roots Marcos Paulo de Souza
2021-08-05  6:42   ` Qu Wenruo
2021-08-04 18:48 ` [PATCH 5/7] btrfs: scrub: Use btrfs_find_item in scrub_enumerate_chunks Marcos Paulo de Souza
2021-08-04 18:48 ` [PATCH 6/7] btrfs: tree-log: Simplify log_new_ancestors Marcos Paulo de Souza
2021-08-05  9:00   ` Filipe Manana
2021-08-05 12:41     ` Marcos Paulo de Souza
2021-08-04 18:48 ` [PATCH 7/7] btrfs: ioctl: Simplify btrfs_ioctl_get_subvol_info Marcos Paulo de Souza
2021-08-05 12:13   ` Anand Jain
2021-08-05 14:23     ` Marcos Paulo de Souza

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=20210804184854.10696-3-mpdesouza@suse.com \
    --to=mpdesouza@suse.com \
    --cc=dsterba@suse.com \
    --cc=linux-btrfs@vger.kernel.org \
    --cc=nborisov@suse.com \
    /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;
as well as URLs for NNTP newsgroup(s).