btrfs-progs/check
Qu Wenruo 0a348d3c81 btrfs-progs: check: verify the underlying block device size is valid
[BUG]
There is a bug report that, one btrfs got its underlying device shrunk
accidentally.

Fortunately the user has no data at the truncated range.  However kernel
will reject such filesystem, while btrfs-check reports nothing wrong
with it.

This can be easily reproduced by:

  # truncate -s 1G test.img
  # mkfs.btrfs test.img
  # truncate -s 996M test.img
  # btrfs check test.img
  Opening filesystem to check...
  Checking filesystem on test.img
  UUID: dbf0a16d-f158-4383-9025-29d7f4c43f17
  [1/7] checking root items
  [2/7] checking extents
  [3/7] checking free space tree
  [4/7] checking fs roots
  [5/7] checking only csums items (without verifying data)
  [6/7] checking root refs
  [7/7] checking quota groups skipped (not enabled on this FS)
  found 16527360 bytes used, no error found
                             ^^^^^^^^^^^^^^
  total csum bytes: 13836
  total tree bytes: 2359296
  total fs tree bytes: 2162688
  total extent tree bytes: 65536
  btree space waste bytes: 503569
  file data blocks allocated: 14168064
   referenced 14168064

[CAUSE]
Btrfs check really only checks the metadata cross references, not really
bothering if the underlying device has correct size.  Thus we completely
ignored such size mismatch.

[FIX]
For both regular and lowmem mode, add extra check against the underlying
block device size.
If the block device size is smaller than its total_bytes, gives a error
message and error out.

Now the check looks like this for both modes:

  ...
  [2/7] checking extents
  ERROR: block device size is smaller than total_bytes in device item, has 1046478848 expect >= 1073741824
  ERROR: errors found in extent allocation tree or chunk allocation
  [3/7] checking free space tree
  ...
  found 16527360 bytes used, error(s) found

Issue: #504
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2022-09-12 15:31:15 +02:00
..
common.h btrfs-progs: check: detect and fix invalid used for block groups 2021-08-25 15:38:54 +02:00
main.c btrfs-progs: check: verify the underlying block device size is valid 2022-09-12 15:31:15 +02:00
mode-common.c btrfs-progs: do not use btrfs_commit_transaction() just to update super blocks 2022-05-20 15:54:16 +02:00
mode-common.h btrfs-progs: check: add check and repair ability for super num devs mismatch 2022-03-24 00:36:13 +01:00
mode-lowmem.c btrfs-progs: check: verify the underlying block device size is valid 2022-09-12 15:31:15 +02:00
mode-lowmem.h btrfs-progs: check btrfs_super_used in lowmem check 2021-08-25 15:38:54 +02:00
mode-original.h btrfs-progs: check: add check for overlong xattr names 2022-04-25 18:52:49 +02:00
qgroup-verify.c btrfs-progs: check: fix two error messages used in qgroup verification 2022-03-23 18:18:31 +01:00
qgroup-verify.h btrfs-progs: move ctree.c to kernel-shared/ 2020-08-31 17:01:05 +02:00