From: Qu Wenruo <wqu@suse.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 1/3] btrfs-progs: check/lowmem: detect and repair mismatched ram_bytes
Date: Thu, 2 May 2024 18:37:53 +0930 [thread overview]
Message-ID: <eb2fc3aeed032dfa887ae39740528d0d17ce71a6.1714640642.git.wqu@suse.com> (raw)
In-Reply-To: <cover.1714640642.git.wqu@suse.com>
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 --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.
--
2.45.0
next prev parent reply other threads:[~2024-05-02 9:08 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-05-02 9:07 [PATCH 0/3] btrfs-progs: check: detect and repair ram_bytes mismatch for non-compressed data extents Qu Wenruo
2024-05-02 9:07 ` Qu Wenruo [this message]
2024-05-02 9:07 ` [PATCH 2/3] btrfs-progs: check/original: detect and repair ram_bytes mismatch Qu Wenruo
2024-05-02 9:07 ` [PATCH 3/3] btrfs-progs: tests/fsck: add test case for ram_bytes detection and repair Qu Wenruo
2024-05-03 13:29 ` [PATCH 0/3] btrfs-progs: check: detect and repair ram_bytes mismatch for non-compressed data extents 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=eb2fc3aeed032dfa887ae39740528d0d17ce71a6.1714640642.git.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