btrfs-progs/mkfs
Qu Wenruo 4f5a455d1b btrfs-progs: mkfs: do not enlarge the target block device
[BUG]
When running mkfs.btrfs with --rootdir on a block device, and the source
directory contains a sparse file, whose size is larger than the block
size, then mkfs.btrfs would fail:

  # lsblk  /dev/test/test
  NAME      MAJ:MIN RM SIZE RO TYPE MOUNTPOINTS
  test-test 253:0    0  10G  0 lvm
  # mkdir -p /tmp/output
  # truncate -s 20G /tmp/output/file
  # mkfs.btrfs -f --rootdir /tmp/output /dev/test/test
  # sudo mkfs.btrfs  -f /dev/test/scratch1  --rootdir /tmp/output/
  btrfs-progs v6.3.3
  See https://btrfs.readthedocs.io for more information.

  ERROR: unable to zero the output file

[CAUSE]
Mkfs.btrfs would try to zero out the target file according to the total
size of the directory.

However the directory size is calculated using the file size, not the
real bytes taken by the file, thus for such sparse file with holes only,
it would still take 20G.

Then we would use that 20G size to zero out the target file, but if the
target file is a block device, we would fail as we can not enlarge a block
device.

[FIX]
When zeroing the file, we only enlarge it if the target is a regular
file.
Otherwise we warn about the size and continue.

Please note that, since "mkfs.btrfs --rootdir" doesn't handle sparse
file any differently from regular file, above case would still fail due
to ENOSPC, as will write zeros into the target file inside the fs.

Proper handling for sparse files would need a new series of patch to
address.

Issue: #653
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
2023-10-21 15:51:07 +02:00
..
common.c btrfs-progs: clean up includes, using include-what-you-use 2023-10-03 01:11:57 +02:00
common.h btrfs-progs: clean up includes, using include-what-you-use 2023-10-03 01:11:57 +02:00
main.c btrfs-progs: mkfs: do not enlarge the target block device 2023-10-21 15:51:07 +02:00
Makefile btrfs-progs: build: add stub makefile to image and mkfs 2019-07-04 15:36:01 +02:00
rootdir.c btrfs-progs: clean up includes, using include-what-you-use 2023-10-03 01:11:57 +02:00
rootdir.h btrfs-progs: mkfs: more verbose output for --rootdir 2023-05-26 22:17:33 +02:00