diff mbox series

[1/3] btrfs-progs: check/lowmem: detect and repair mismatched ram_bytes

Message ID eb2fc3aeed032dfa887ae39740528d0d17ce71a6.1714640642.git.wqu@suse.com (mailing list archive)
State New
Headers show
Series btrfs-progs: check: detect and repair ram_bytes mismatch for non-compressed data extents | expand

Commit Message

Qu Wenruo May 2, 2024, 9:07 a.m. UTC
For non-compressed non-hole file extent items, the ram_bytes should
match disk_num_bytes.

But due to kernel bugs, we have several cases where ram_bytes is not
correctly updated.

Thankfully this is really a very minor mismatch and can never cause data
corruption since the kernel does not utilize ram_bytes for
non-compressed extents at all.

So here we just detect and repair it for lowmem mode.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 check/mode-lowmem.c | 69 +++++++++++++++++++++++++++++++++++++++++++++
 check/mode-lowmem.h |  1 +
 2 files changed, 70 insertions(+)
diff mbox series

Patch

diff --git a/check/mode-lowmem.c b/check/mode-lowmem.c
index fd9b975c4e5f..99e1305b1f3e 100644
--- a/check/mode-lowmem.c
+++ b/check/mode-lowmem.c
@@ -2081,6 +2081,61 @@  static int check_file_extent_inline(struct btrfs_root *root,
 	return err;
 }
 
+static int repair_ram_bytes_mismatch(struct btrfs_root *root,
+				     struct btrfs_path *path)
+{
+	struct btrfs_trans_handle *trans;
+	struct btrfs_key key;
+	struct btrfs_file_extent_item *fi;
+	u64 disk_num_bytes;
+	int recover_ret;
+	int ret;
+
+	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
+	btrfs_release_path(path);
+	UASSERT(key.type == BTRFS_EXTENT_DATA_KEY);
+
+	trans = btrfs_start_transaction(root, 1);
+	if (IS_ERR(trans)) {
+		ret = PTR_ERR(trans);
+		errno = -ret;
+		error_msg(ERROR_MSG_START_TRANS, "%m");
+		return ret;
+	}
+
+	ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
+	/* Not really possible. */
+	if (ret > 0) {
+		ret = -ENOENT;
+		btrfs_release_path(path);
+		goto recover;
+	}
+
+	if (ret < 0)
+		goto recover;
+
+	fi = btrfs_item_ptr(path->nodes[0], path->slots[0],
+			    struct btrfs_file_extent_item);
+	disk_num_bytes = btrfs_file_extent_disk_num_bytes(path->nodes[0], fi);
+	btrfs_set_file_extent_ram_bytes(path->nodes[0], fi, disk_num_bytes);
+	btrfs_mark_buffer_dirty(path->nodes[0]);
+
+	ret = btrfs_commit_transaction(trans, root);
+	if (ret < 0) {
+		errno = -ret;
+		error_msg(ERROR_MSG_COMMIT_TRANS, "%m");
+	} else {
+		printf(
+	"Successfully repaired ram_bytes for non-compressed extent at root %llu ino %llu file_pos %llu\n",
+			root->objectid, key.objectid, key.offset);
+	}
+	return ret;
+recover:
+	recover_ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
+	UASSERT(recover_ret == 0);
+	return ret;
+}
+
 /*
  * Check file extent datasum/hole, update the size of the file extents,
  * check and update the last offset of the file extent.
@@ -2106,6 +2161,7 @@  static int check_file_extent(struct btrfs_root *root, struct btrfs_path *path,
 	u64 csum_found;		/* In byte size, sectorsize aligned */
 	u64 search_start;	/* Logical range start we search for csum */
 	u64 search_len;		/* Logical range len we search for csum */
+	u64 ram_bytes;
 	u64 gen;
 	u64 super_gen;
 	unsigned int extent_type;
@@ -2140,6 +2196,7 @@  static int check_file_extent(struct btrfs_root *root, struct btrfs_path *path,
 	extent_num_bytes = btrfs_file_extent_num_bytes(node, fi);
 	extent_offset = btrfs_file_extent_offset(node, fi);
 	compressed = btrfs_file_extent_compression(node, fi);
+	ram_bytes = btrfs_file_extent_ram_bytes(node, fi);
 	is_hole = (disk_bytenr == 0) && (disk_num_bytes == 0);
 	super_gen = btrfs_super_generation(gfs_info->super_copy);
 
@@ -2150,6 +2207,18 @@  static int check_file_extent(struct btrfs_root *root, struct btrfs_path *path,
 		err |= INVALID_GENERATION;
 	}
 
+	if (!compressed && disk_bytenr && disk_num_bytes != ram_bytes) {
+		error(
+		"minor ram_bytes mismatch for non-compressed data extents, have %llu expect %llu",
+		      ram_bytes, disk_num_bytes);
+		if (opt_check_repair) {
+			ret = repair_ram_bytes_mismatch(root, path);
+			if (ret < 0)
+				err |= RAM_BYTES_MISMATCH;
+		} else {
+			err |= RAM_BYTES_MISMATCH;
+		}
+	}
 	/*
 	 * Check EXTENT_DATA csum
 	 *
diff --git a/check/mode-lowmem.h b/check/mode-lowmem.h
index b45e6bc137f3..b3e212165519 100644
--- a/check/mode-lowmem.h
+++ b/check/mode-lowmem.h
@@ -47,6 +47,7 @@ 
 #define INODE_MODE_ERROR	(1U << 25)	/* Bad inode mode */
 #define INVALID_GENERATION	(1U << 26)	/* Generation is too new */
 #define SUPER_BYTES_USED_ERROR	(1U << 27)	/* Super bytes_used is invalid */
+#define RAM_BYTES_MISMATCH	(1U << 27)	/* Non-compressed extent has wrong ram_bytes */
 
 /*
  * Error bit for low memory mode check.