diff mbox series

[4/4] btrfs: tree-checker: add extra ram_bytes and disk_num_bytes check

Message ID 75a7f80c92184ced08a904dc3ce0d43518d399a5.1719291793.git.wqu@suse.com (mailing list archive)
State New, archived
Headers show
Series btrfs: detect and fix the ram_bytes mismatch | expand

Commit Message

Qu Wenruo June 25, 2024, 5:07 a.m. UTC
This is to ensure non-compressed file extents (both regular and
prealloc) should have matching ram_bytes and disk_num_bytes.

This is only for CONFIG_BTRFS_DEBUG and CONFIG_BTRFS_ASSERT case,
furthermore this will not return error, but just a kernel warning to
inform developers.

Signed-off-by: Qu Wenruo <wqu@suse.com>
---
 fs/btrfs/tree-checker.c | 19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

Comments

Filipe Manana June 25, 2024, 10:37 a.m. UTC | #1
On Tue, Jun 25, 2024 at 6:08 AM Qu Wenruo <wqu@suse.com> wrote:
>
> This is to ensure non-compressed file extents (both regular and
> prealloc) should have matching ram_bytes and disk_num_bytes.
>
> This is only for CONFIG_BTRFS_DEBUG and CONFIG_BTRFS_ASSERT case,

I would leave just for DEBUG.

> furthermore this will not return error, but just a kernel warning to
> inform developers.
>
> Signed-off-by: Qu Wenruo <wqu@suse.com>
> ---
>  fs/btrfs/tree-checker.c | 19 +++++++++++++++++++
>  1 file changed, 19 insertions(+)
>
> diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
> index a2c3651a3d8f..cddabd9a0e37 100644
> --- a/fs/btrfs/tree-checker.c
> +++ b/fs/btrfs/tree-checker.c
> @@ -340,6 +340,25 @@ static int check_extent_data_item(struct extent_buffer *leaf,
>                 }
>         }
>
> +       /*
> +        * For non-compressed data extents, ram_bytes should match its disk_bytenr.
> +        * However we do not really utilize ram_bytes in this case, so this check
> +        * is only optional for DEBUG+ASSERT builds for developers to catch the
> +        * unexpected behaviors.
> +        */
> +       if (IS_ENABLED(CONFIG_BTRFS_DEBUG) && IS_ENABLED(CONFIG_BTRFS_ASSERT) &&
> +           btrfs_file_extent_compression(leaf, fi) == BTRFS_COMPRESS_NONE &&
> +           btrfs_file_extent_disk_bytenr(leaf, fi)) {
> +               if (unlikely(btrfs_file_extent_ram_bytes(leaf, fi) !=
> +                            btrfs_file_extent_disk_num_bytes(leaf, fi))) {
> +                       file_extent_err(leaf, slot,
> +"mismatch ram_bytes (%llu) and disk_num_bytes (%llu) for non-compressed extent",
> +                                       btrfs_file_extent_ram_bytes(leaf, fi),
> +                                       btrfs_file_extent_disk_num_bytes(leaf, fi));
> +                       WARN_ON(1);

Instead of adding here a WARN_ON(1) and unlikely in the if condition,
just make the if condition use WARN_ON:

if (WARN_ON(btrfs_file_extent_ram_bytes(leaf, fi) !=
btrfs_file_extent_disk_num_bytes(leaf, fi)))

The WARN_ON includes the unlikely, and further this makes it conform
to our preference of having error messages after stack traces.

Thanks.

> +               }
> +       }
> +
>         return 0;
>  }
>
> --
> 2.45.2
>
>
diff mbox series

Patch

diff --git a/fs/btrfs/tree-checker.c b/fs/btrfs/tree-checker.c
index a2c3651a3d8f..cddabd9a0e37 100644
--- a/fs/btrfs/tree-checker.c
+++ b/fs/btrfs/tree-checker.c
@@ -340,6 +340,25 @@  static int check_extent_data_item(struct extent_buffer *leaf,
 		}
 	}
 
+	/*
+	 * For non-compressed data extents, ram_bytes should match its disk_bytenr.
+	 * However we do not really utilize ram_bytes in this case, so this check
+	 * is only optional for DEBUG+ASSERT builds for developers to catch the
+	 * unexpected behaviors.
+	 */
+	if (IS_ENABLED(CONFIG_BTRFS_DEBUG) && IS_ENABLED(CONFIG_BTRFS_ASSERT) &&
+	    btrfs_file_extent_compression(leaf, fi) == BTRFS_COMPRESS_NONE &&
+	    btrfs_file_extent_disk_bytenr(leaf, fi)) {
+		if (unlikely(btrfs_file_extent_ram_bytes(leaf, fi) !=
+			     btrfs_file_extent_disk_num_bytes(leaf, fi))) {
+			file_extent_err(leaf, slot,
+"mismatch ram_bytes (%llu) and disk_num_bytes (%llu) for non-compressed extent",
+					btrfs_file_extent_ram_bytes(leaf, fi),
+					btrfs_file_extent_disk_num_bytes(leaf, fi));
+			WARN_ON(1);
+		}
+	}
+
 	return 0;
 }