btrfs-progs: mkfs: don't zero extend small files
mkfs can try to write outside of small devices. The zeroing code
doesn't test the device size and runs before mkfs tests for small
devices and exits.
Testers experienced this as small regular files being extended as mkfs
failed:
$ truncate -s 1m /tmp/some-file
$ strace -epwrite ./mkfs.btrfs /tmp/some-file
SMALL VOLUME: forcing mixed metadata/data groups
WARNING! - Btrfs v3.14.2 IS EXPERIMENTAL
WARNING! - see http://btrfs.wiki.kernel.org before using
pwrite(3, ..., 2097152, 0) = 2097152
pwrite(3, ..., 4096, 65536) = 4096
pwrite(3 ..., 2097152,
18446744073708503040) = -1 EINVAL (Invalid argument)
ERROR: failed to zero device '/tmp/some-file' - Input/output error
$ ls -lh /tmp/some-file
-rw-rw-r--. 1 zab zab 2.0M Jul 16 13:49 /tmp/some-file
This simple fix adds a helper that clamps a region to be zeroed to the
size of the device. It doesn't address the larger questions of whether
to modify the device before the size test or whether or zero regions
that have been trimmed.
Finally, the error handling mess after the zeroing calls is cleaned up.
zero_blocks() and its callers only return -errno.
Signed-off-by: Zach Brown <zab@zabbo.net>
Signed-off-by: David Sterba <dsterba@suse.cz>