diff mbox series

[v2,10/12] btrfs-progs: check btrfs_super_used in lowmem check

Message ID ebeaf9c035019f2d5b210ad752caca5655c69edc.1629322156.git.josef@toxicpanda.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: make check handle invalid bg items | expand

Commit Message

Josef Bacik Aug. 18, 2021, 9:33 p.m. UTC
We can already fix this problem with the block accounting code, we just
need to keep track of how much we should have used on the file system,
and then check it against the bytes_super.  The repair just piggy backs
on the block group used repair.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 check/mode-lowmem.c | 13 ++++++++++++-
 check/mode-lowmem.h |  1 +
 2 files changed, 13 insertions(+), 1 deletion(-)

Comments

Qu Wenruo Aug. 19, 2021, 5:57 a.m. UTC | #1
On 2021/8/19 上午5:33, Josef Bacik wrote:
> We can already fix this problem with the block accounting code, we just
> need to keep track of how much we should have used on the file system,
> and then check it against the bytes_super.  The repair just piggy backs
> on the block group used repair.
>
> Signed-off-by: Josef Bacik <josef@toxicpanda.com>

Reviewed-by: Qu Wenruo <wqu@suse.com>

One unrelated concern inlined below.

> ---
>   check/mode-lowmem.c | 13 ++++++++++++-
>   check/mode-lowmem.h |  1 +
>   2 files changed, 13 insertions(+), 1 deletion(-)
>
> diff --git a/check/mode-lowmem.c b/check/mode-lowmem.c
> index 14815519..dacc5445 100644
> --- a/check/mode-lowmem.c
> +++ b/check/mode-lowmem.c
> @@ -28,6 +28,7 @@
>   #include "check/mode-lowmem.h"
>
>   static u64 last_allocated_chunk;
> +static u64 total_used = 0;
>
>   static int calc_extent_flag(struct btrfs_root *root, struct extent_buffer *eb,
>   			    u64 *flags_ret)
> @@ -3654,6 +3655,8 @@ next:
>   out:
>   	btrfs_release_path(&path);
>
> +	total_used += used;
> +
>   	if (total != used) {
>   		error(
>   		"block group[%llu %llu] used %llu but extent items used %llu",
> @@ -5556,6 +5559,14 @@ next:
>   	}
>   out:
>
> +	if (total_used != btrfs_super_bytes_used(gfs_info->super_copy)) {
> +		fprintf(stderr,
> +			"super bytes used %llu mismatches actual used %llu\n",
> +			btrfs_super_bytes_used(gfs_info->super_copy),
> +			total_used);
> +		err |= SUPER_BYTES_USED_ERROR;
> +	}
> +
>   	if (repair) {
>   		ret = end_avoid_extents_overwrite();
>   		if (ret < 0)
> @@ -5568,7 +5579,7 @@ out:
>   		if (ret)
>   			err |= ret;
>   		else
> -			err &= ~BG_ACCOUNTING_ERROR;
> +			err &= ~(BG_ACCOUNTING_ERROR|SUPER_BYTES_USED_ERROR);
>   	}
>
>   	btrfs_release_path(&path);
> diff --git a/check/mode-lowmem.h b/check/mode-lowmem.h
> index da9f8600..0bcc338b 100644
> --- a/check/mode-lowmem.h
> +++ b/check/mode-lowmem.h
> @@ -48,6 +48,7 @@
>   #define DIR_ITEM_HASH_MISMATCH	(1<<24) /* Dir item hash mismatch */
>   #define INODE_MODE_ERROR	(1<<25) /* Bad inode mode */
>   #define INVALID_GENERATION	(1<<26)	/* Generation is too new */
> +#define SUPER_BYTES_USED_ERROR	(1<<27)	/* Super bytes_used is invalid */

We're reaching U32 limit now...

Thanks,
Qu
>
>   /*
>    * Error bit for low memory mode check.
>
diff mbox series

Patch

diff --git a/check/mode-lowmem.c b/check/mode-lowmem.c
index 14815519..dacc5445 100644
--- a/check/mode-lowmem.c
+++ b/check/mode-lowmem.c
@@ -28,6 +28,7 @@ 
 #include "check/mode-lowmem.h"
 
 static u64 last_allocated_chunk;
+static u64 total_used = 0;
 
 static int calc_extent_flag(struct btrfs_root *root, struct extent_buffer *eb,
 			    u64 *flags_ret)
@@ -3654,6 +3655,8 @@  next:
 out:
 	btrfs_release_path(&path);
 
+	total_used += used;
+
 	if (total != used) {
 		error(
 		"block group[%llu %llu] used %llu but extent items used %llu",
@@ -5556,6 +5559,14 @@  next:
 	}
 out:
 
+	if (total_used != btrfs_super_bytes_used(gfs_info->super_copy)) {
+		fprintf(stderr,
+			"super bytes used %llu mismatches actual used %llu\n",
+			btrfs_super_bytes_used(gfs_info->super_copy),
+			total_used);
+		err |= SUPER_BYTES_USED_ERROR;
+	}
+
 	if (repair) {
 		ret = end_avoid_extents_overwrite();
 		if (ret < 0)
@@ -5568,7 +5579,7 @@  out:
 		if (ret)
 			err |= ret;
 		else
-			err &= ~BG_ACCOUNTING_ERROR;
+			err &= ~(BG_ACCOUNTING_ERROR|SUPER_BYTES_USED_ERROR);
 	}
 
 	btrfs_release_path(&path);
diff --git a/check/mode-lowmem.h b/check/mode-lowmem.h
index da9f8600..0bcc338b 100644
--- a/check/mode-lowmem.h
+++ b/check/mode-lowmem.h
@@ -48,6 +48,7 @@ 
 #define DIR_ITEM_HASH_MISMATCH	(1<<24) /* Dir item hash mismatch */
 #define INODE_MODE_ERROR	(1<<25) /* Bad inode mode */
 #define INVALID_GENERATION	(1<<26)	/* Generation is too new */
+#define SUPER_BYTES_USED_ERROR	(1<<27)	/* Super bytes_used is invalid */
 
 /*
  * Error bit for low memory mode check.