Linux Btrfs filesystem development
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 4/4] btrfs-progs: map-logical: remove the extent item check
Date: Thu, 12 Aug 2021 13:35:08 +0800	[thread overview]
Message-ID: <20210812053508.175737-5-wqu@suse.com> (raw)
In-Reply-To: <20210812053508.175737-1-wqu@suse.com>

Tool btrfs-map-logical is really only doing logical -> physical mapping,
mostly utilized by developers or experienced users.

There is really no need to check whether the specified range has an
extent or not.

In fact the extent check behavior is a big blockage for corrupted fs as
such fs can have corrupted extent tree, doing an extent item search can
lead to -EIO error.

This patch will just remove the extent item check, allowing users to do
any logical -> physical mapping lookup as long as there is a chunk.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 btrfs-map-logical.c | 145 ++------------------------------------------
 1 file changed, 5 insertions(+), 140 deletions(-)

diff --git a/btrfs-map-logical.c b/btrfs-map-logical.c
index 9f119d08bad8..96263013a5c6 100644
--- a/btrfs-map-logical.c
+++ b/btrfs-map-logical.c
@@ -38,65 +38,6 @@
  * */
 static FILE *info_file;
 
-static int map_one_extent(struct btrfs_fs_info *fs_info,
-			  u64 *logical_ret, u64 *len_ret, int search_forward)
-{
-	struct btrfs_path *path;
-	struct btrfs_key key;
-	u64 logical;
-	u64 len = 0;
-	int ret = 0;
-
-	BUG_ON(!logical_ret);
-	logical = *logical_ret;
-
-	path = btrfs_alloc_path();
-	if (!path)
-		return -ENOMEM;
-
-	key.objectid = logical;
-	key.type = 0;
-	key.offset = 0;
-
-	ret = btrfs_search_slot(NULL, fs_info->extent_root, &key, path,
-				0, 0);
-	if (ret < 0)
-		goto out;
-	BUG_ON(ret == 0);
-	ret = 0;
-
-again:
-	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
-	if ((search_forward && key.objectid < logical) ||
-	    (!search_forward && key.objectid > logical) ||
-	    (key.type != BTRFS_EXTENT_ITEM_KEY &&
-	     key.type != BTRFS_METADATA_ITEM_KEY)) {
-		if (!search_forward)
-			ret = btrfs_previous_extent_item(fs_info->extent_root,
-							 path, 0);
-		else
-			ret = btrfs_next_extent_item(fs_info->extent_root,
-						     path, 0);
-		if (ret)
-			goto out;
-		goto again;
-	}
-	logical = key.objectid;
-	if (key.type == BTRFS_METADATA_ITEM_KEY)
-		len = fs_info->nodesize;
-	else
-		len = key.offset;
-
-out:
-	btrfs_free_path(path);
-	if (!ret) {
-		*logical_ret = logical;
-		if (len_ret)
-			*len_ret = len;
-	}
-	return ret;
-}
-
 static int __print_mapping_info(struct btrfs_fs_info *fs_info, u64 logical,
 				u64 len, int mirror_num)
 {
@@ -134,16 +75,6 @@ static int __print_mapping_info(struct btrfs_fs_info *fs_info, u64 logical,
 	return ret;
 }
 
-/*
- * Logical and len is the exact value of a extent.
- * And offset is the offset inside the extent. It's only used for case
- * where user only want to print part of the extent.
- *
- * Caller *MUST* ensure the range [logical,logical+len) are in one extent.
- * Or we can encounter the following case, causing a -ENOENT error:
- * |<-----given parameter------>|
- *		|<------ Extent A ----->|
- */
 static int print_mapping_info(struct btrfs_fs_info *fs_info, u64 logical,
 			      u64 len)
 {
@@ -213,10 +144,7 @@ int main(int argc, char **argv)
 	u64 copy = 0;
 	u64 logical = 0;
 	u64 bytes = 0;
-	u64 cur_logical = 0;
-	u64 cur_len = 0;
 	int out_fd = -1;
-	int found = 0;
 	int ret = 0;
 
 	while(1) {
@@ -300,76 +228,13 @@ int main(int argc, char **argv)
 		goto close;
 	}
 
-	cur_logical = logical;
-	cur_len = bytes;
-
-	/* First find the nearest extent */
-	ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 0);
-	if (ret < 0) {
-		errno = -ret;
-		fprintf(stderr, "Failed to find extent at [%llu,%llu): %m\n",
-			cur_logical, cur_logical + cur_len);
+	ret = print_mapping_info(root->fs_info, logical, bytes);
+	if (ret < 0)
 		goto out_close_fd;
-	}
-	/*
-	 * Normally, search backward should be OK, but for special case like
-	 * given logical is quite small where no extents are before it,
-	 * we need to search forward.
-	 */
-	if (ret > 0) {
-		ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 1);
-		if (ret < 0) {
-			errno = -ret;
-			fprintf(stderr,
-				"Failed to find extent at [%llu,%llu): %m\n",
-				cur_logical, cur_logical + cur_len);
-			goto out_close_fd;
-		}
-		if (ret > 0) {
-			fprintf(stderr,
-				"Failed to find any extent at [%llu,%llu)\n",
-				cur_logical, cur_logical + cur_len);
-			goto out_close_fd;
-		}
-	}
-
-	while (cur_logical + cur_len >= logical && cur_logical < logical +
-	       bytes) {
-		u64 real_logical;
-		u64 real_len;
+	if (output_file && out_fd != -1)
+		ret = write_extent_content(root->fs_info, out_fd, logical,
+					   bytes, copy);
 
-		found = 1;
-		ret = map_one_extent(root->fs_info, &cur_logical, &cur_len, 1);
-		if (ret < 0)
-			goto out_close_fd;
-		if (ret > 0)
-			break;
-		/* check again if there is overlap. */
-		if (cur_logical + cur_len < logical ||
-		    cur_logical >= logical + bytes)
-			break;
-
-		real_logical = max(logical, cur_logical);
-		real_len = min(logical + bytes, cur_logical + cur_len) -
-			   real_logical;
-
-		ret = print_mapping_info(root->fs_info, real_logical, real_len);
-		if (ret < 0)
-			goto out_close_fd;
-		if (output_file && out_fd != -1) {
-			ret = write_extent_content(root->fs_info, out_fd,
-					real_logical, real_len, copy);
-			if (ret < 0)
-				goto out_close_fd;
-		}
-
-		cur_logical += cur_len;
-	}
-
-	if (!found) {
-		fprintf(stderr, "No extent found at range [%llu,%llu)\n",
-			logical, logical + bytes);
-	}
 out_close_fd:
 	if (output_file && out_fd != 1)
 		close(out_fd);
-- 
2.32.0


      parent reply	other threads:[~2021-08-12  5:35 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-08-12  5:35 [PATCH 0/4] btrfs-progs: map-logical: remove the extent check Qu Wenruo
2021-08-12  5:35 ` [PATCH 1/4] btrfs-progs: map-logical: use sectorsize as default size Qu Wenruo
2021-08-12  5:35 ` [PATCH 2/4] btrfs-progs: map-logical: reject unaligned logical/bytes pair Qu Wenruo
2021-08-12  6:42   ` Nikolay Borisov
2021-08-12  5:35 ` [PATCH 3/4] btrfs-progs: map-logical: loosen the required trees to open the filesystem Qu Wenruo
2021-08-12  6:41   ` Nikolay Borisov
2021-08-12  5:35 ` Qu Wenruo [this message]

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=20210812053508.175737-5-wqu@suse.com \
    --to=wqu@suse.com \
    --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