public inbox for linux-btrfs@vger.kernel.org
 help / color / mirror / Atom feed
From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 3/4] btrfs-progs: check/original: add the ability to repair extent item generation
Date: Mon, 10 Aug 2020 15:27:46 +0800	[thread overview]
Message-ID: <20200810072747.64439-4-wqu@suse.com> (raw)
In-Reply-To: <20200810072747.64439-1-wqu@suse.com>

This is pretty much the same as the lowmem mode, it will try to reset
the extent item generation using either the tree block generation or
current transid.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 check/main.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++++---
 1 file changed, 70 insertions(+), 4 deletions(-)

diff --git a/check/main.c b/check/main.c
index 72fa28ad216a..dc366b125ece 100644
--- a/check/main.c
+++ b/check/main.c
@@ -7914,6 +7914,63 @@ static int record_unaligned_extent_rec(struct btrfs_fs_info *fs_info,
 	return ret;
 }
 
+static int repair_extent_item_generation(struct btrfs_fs_info *fs_info,
+					 struct extent_record *rec)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_path path;
+	struct btrfs_key key;
+	struct btrfs_extent_item *ei;
+	struct btrfs_root *extent_root = fs_info->extent_root;
+	u64 new_gen = 0;;
+	int ret;
+
+	key.objectid = rec->start;
+	key.type = BTRFS_METADATA_ITEM_KEY;
+	key.offset = (u64)-1;
+
+	get_extent_item_generation(fs_info, rec->start, &new_gen);
+	trans = btrfs_start_transaction(extent_root, 1);
+	if (IS_ERR(trans)) {
+		ret = PTR_ERR(trans);
+		errno = -ret;
+		error("failed to start transaction: %m");
+		return ret;
+	}
+	btrfs_init_path(&path);
+	ret = btrfs_search_slot(trans, extent_root, &key, &path, 0, 1);
+	/* Not possible */
+	if (ret == 0)
+		ret = -EUCLEAN;
+	if (ret < 0)
+		goto out;
+	ret = btrfs_previous_extent_item(extent_root, &path, rec->start);
+	if (ret > 0)
+		ret = -ENOENT;
+	if (ret < 0)
+		goto out;
+	
+	if (!new_gen)
+		new_gen = trans->transid;
+	ei = btrfs_item_ptr(path.nodes[0], path.slots[0],
+			    struct btrfs_extent_item);
+	btrfs_set_extent_generation(path.nodes[0], ei, new_gen);
+	ret = btrfs_commit_transaction(trans, extent_root);
+	if (ret < 0) {
+		errno = -ret;
+		error("failed to commit transaction: %m");
+		goto out;
+	}
+	printf("Reset extent item (%llu) generation to %llu\n",
+		key.objectid, new_gen);
+	rec->generation = new_gen;
+out:
+	btrfs_release_path(&path);
+	if (ret < 0)
+		btrfs_abort_transaction(trans, ret); 
+	return ret;
+}
+
 static int check_extent_refs(struct btrfs_root *root,
 			     struct cache_tree *extent_cache)
 {
@@ -8004,11 +8061,20 @@ static int check_extent_refs(struct btrfs_root *root,
 		}
 
 		if (rec->generation > super_gen + 1) {
-			error(
+			bool repaired = false;
+			if (repair) {
+				ret = repair_extent_item_generation(
+						root->fs_info, rec);
+				if (ret == 0)
+					repaired = true;
+			}
+			if (!repaired) {
+				error(
 	"invalid generation for extent %llu, have %llu expect (0, %llu]",
-				rec->start, rec->generation,
-				super_gen + 1);
-			cur_err = 1;
+					rec->start, rec->generation,
+					super_gen + 1);
+				cur_err = 1;
+			}
 		}
 		if (rec->refs != rec->extent_item_refs) {
 			fprintf(stderr, "ref mismatch on [%llu %llu] ",
-- 
2.28.0


  parent reply	other threads:[~2020-08-10  7:28 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-10  7:27 [PATCH 0/4] btrfs-progs: check: add the ability to repair extent item generation corruption Qu Wenruo
2020-08-10  7:27 ` [PATCH 1/4] btrfs-progs: check/lowmem: add the ability to repair extent item generation Qu Wenruo
2020-08-10  7:30   ` Johannes Thumshirn
2020-08-10  7:35     ` Qu Wenruo
2020-08-10  7:27 ` [PATCH 2/4] btrfs-progs: check/original: don't reset extent generation for check_block() Qu Wenruo
2020-08-10  7:27 ` Qu Wenruo [this message]
2020-08-10  7:27 ` [PATCH 4/4] btrfs-progs: tests/fsck: enhance invalid extent item geneartion test cases Qu Wenruo

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=20200810072747.64439-4-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