sys_util: do not deallocate space in write_zeroes

Replace the implementation of File write_zeroes with one that uses
fallocate() with the FALLOC_FL_ZERO_RANGE flag instead of
FALLOC_FL_PUNCH_HOLE.  This means it will keep space allocated for the
zeroed region instead of deallocating it.  The PunchHole trait is
available for this purpose instead, and the virtio-blk implementation
already relies on these two traits for their differing behaviors.

BUG=chromium:858815
TEST=cargo test -p sys_util write_zeroes

Change-Id: I69ab06037f72dc219e6ea9409654f97eeaba32c3
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1913520
Tested-by: kokoro <noreply+kokoro@google.com>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
Daniel Verkamp 2019-11-12 14:42:16 -08:00 committed by Commit Bot
parent 6eadef77a3
commit 41d889eb26

View file

@ -87,14 +87,13 @@ pub trait WriteZeroesAt {
impl WriteZeroesAt for File { impl WriteZeroesAt for File {
fn write_zeroes_at(&mut self, offset: u64, length: usize) -> io::Result<usize> { fn write_zeroes_at(&mut self, offset: u64, length: usize) -> io::Result<usize> {
// Try to punch a hole first. // Try to use fallocate() first.
if let Ok(()) = self.punch_hole(offset, length as u64) { if fallocate(self, FallocateMode::ZeroRange, true, offset, length as u64).is_ok() {
return Ok(length); return Ok(length);
} }
// fall back to write() // fall back to write()
// fallocate() failed; fall back to writing a buffer of zeroes
// punch_hole() failed; fall back to writing a buffer of zeroes
// until we have written up to length. // until we have written up to length.
let buf_size = min(length, 0x10000); let buf_size = min(length, 0x10000);
let buf = vec![0u8; buf_size]; let buf = vec![0u8; buf_size];