kdave / btrfs-progs

Development of userspace BTRFS tools
GNU General Public License v2.0
557 stars 242 forks source link

btrfs-convert reports incorrect free space #487

Open cmurf opened 2 years ago

cmurf commented 2 years ago

btrfs-progs-5.18-1.fc36.x86_64 kernel-5.19.0-0.rc4.20220628git941e3e791269.34.fc37.x86_64

btrfs-convert is reporting wildly incorrect free space in its output, over 200% is free (?) is impossible.

Reproduce steps:

  1. truncate -s 28T test
  2. losetup /dev/loop0 test
  3. mkfs.ext4 /dev/loop0
  4. mount /dev/loop0 /mnt
  5. umount /mnt
  6. btrfs-convert /dev/loop0

Total space: 12412111552512 Free space: 29531792404480 (237.93%)

Downstream bug https://bugzilla.redhat.com/show_bug.cgi?id=2101045

cmurf commented 2 years ago

Doesn't happen with an 8T image. It might be a problem related to the size of the target file system or block device.

JasonFerrara commented 2 years ago

It looks like the free space is correct. It's the total space that's wrong.

JasonFerrara commented 2 years ago

This seems to fix the incorrect total space (and block count) issue.

diff --git a/convert/source-ext2.c b/convert/source-ext2.c
index 9fad4c50..01b630ba 100644
--- a/convert/source-ext2.c
+++ b/convert/source-ext2.c
@@ -92,8 +92,8 @@ static int ext2_open_fs(struct btrfs_convert_context *cctx, const char *name)

        cctx->fs_data = ext2_fs;
        cctx->blocksize = ext2_fs->blocksize;
-       cctx->block_count = ext2_fs->super->s_blocks_count;
-       cctx->total_bytes = (u64)ext2_fs->super->s_blocks_count * ext2_fs->blocksize;
+       cctx->block_count = ext2fs_blocks_count(ext2_fs->super);
+       cctx->total_bytes = cctx->block_count * ext2_fs->blocksize;
        cctx->label = strndup((char *)ext2_fs->super->s_volume_name, 16);
        cctx->first_data_block = ext2_fs->super->s_first_data_block;
        cctx->inodes_count = ext2_fs->super->s_inodes_count;

I'm still waiting for the conversion to finish to see if this also fixes the downstream bug. It's been running for 9 hours so far. It might be nice to add some sort of progress for the Create ext2 image file stage.