diff mbox series

[v2,09/12] btrfs-progs: make check detect and fix problems with super_bytes_used

Message ID fd18dba016fbacb15e30c25acc8b0f9e66d10fe0.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 do not detect problems with our bytes_used counter in the super
block.  Thankfully the same method to fix block groups is used to re-set
the value in the super block, so simply add some extra code to validate
the bytes_used field and then piggy back on the repair code for block
groups.

Signed-off-by: Josef Bacik <josef@toxicpanda.com>
---
 check/main.c | 15 +++++++++++++++
 1 file changed, 15 insertions(+)

Comments

Qu Wenruo Aug. 19, 2021, 5:56 a.m. UTC | #1
On 2021/8/19 上午5:33, Josef Bacik wrote:
> We do not detect problems with our bytes_used counter in the super
> block.  Thankfully the same method to fix block groups is used to re-set
> the value in the super block, so simply add some extra code to validate
> the bytes_used field and then piggy back on the repair code for block
> groups.
>
> Signed-off-by: Josef Bacik <josef@toxicpanda.com>

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

Thanks,
Qu
> ---
>   check/main.c | 15 +++++++++++++++
>   1 file changed, 15 insertions(+)
>
> diff --git a/check/main.c b/check/main.c
> index af9e0ff3..b1b1b866 100644
> --- a/check/main.c
> +++ b/check/main.c
> @@ -8663,12 +8663,14 @@ static int check_block_groups(struct block_group_tree *bg_cache)
>   	struct btrfs_trans_handle *trans;
>   	struct cache_extent *item;
>   	struct block_group_record *bg_rec;
> +	u64 used = 0;
>   	int ret = 0;
>
>   	for (item = first_cache_extent(&bg_cache->tree); item;
>   	     item = next_cache_extent(item)) {
>   		bg_rec = container_of(item, struct block_group_record,
>   				      cache);
> +		used += bg_rec->actual_used;
>   		if (bg_rec->disk_used == bg_rec->actual_used)
>   			continue;
>   		fprintf(stderr,
> @@ -8678,6 +8680,19 @@ static int check_block_groups(struct block_group_tree *bg_cache)
>   		ret = -1;
>   	}
>
> +	/*
> +	 * We check the super bytes_used here because it's the sum of all block
> +	 * groups used, and the repair actually happens in
> +	 * btrfs_fix_block_accounting, so we can kill both birds with the same
> +	 * stone here.
> +	 */
> +	if (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), used);
> +		ret = -1;
> +	}
> +
>   	if (!repair || !ret)
>   		return ret;
>
>
diff mbox series

Patch

diff --git a/check/main.c b/check/main.c
index af9e0ff3..b1b1b866 100644
--- a/check/main.c
+++ b/check/main.c
@@ -8663,12 +8663,14 @@  static int check_block_groups(struct block_group_tree *bg_cache)
 	struct btrfs_trans_handle *trans;
 	struct cache_extent *item;
 	struct block_group_record *bg_rec;
+	u64 used = 0;
 	int ret = 0;
 
 	for (item = first_cache_extent(&bg_cache->tree); item;
 	     item = next_cache_extent(item)) {
 		bg_rec = container_of(item, struct block_group_record,
 				      cache);
+		used += bg_rec->actual_used;
 		if (bg_rec->disk_used == bg_rec->actual_used)
 			continue;
 		fprintf(stderr,
@@ -8678,6 +8680,19 @@  static int check_block_groups(struct block_group_tree *bg_cache)
 		ret = -1;
 	}
 
+	/*
+	 * We check the super bytes_used here because it's the sum of all block
+	 * groups used, and the repair actually happens in
+	 * btrfs_fix_block_accounting, so we can kill both birds with the same
+	 * stone here.
+	 */
+	if (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), used);
+		ret = -1;
+	}
+
 	if (!repair || !ret)
 		return ret;