From 41d889eb267cdcd066575b5647bf05c516d70201 Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Tue, 12 Nov 2019 14:42:16 -0800 Subject: [PATCH] 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 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1913520 Tested-by: kokoro Reviewed-by: Dylan Reid --- sys_util/src/write_zeroes.rs | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/sys_util/src/write_zeroes.rs b/sys_util/src/write_zeroes.rs index 7e28f5315a..ee4d3b7175 100644 --- a/sys_util/src/write_zeroes.rs +++ b/sys_util/src/write_zeroes.rs @@ -87,14 +87,13 @@ pub trait WriteZeroesAt { impl WriteZeroesAt for File { fn write_zeroes_at(&mut self, offset: u64, length: usize) -> io::Result { - // Try to punch a hole first. - if let Ok(()) = self.punch_hole(offset, length as u64) { + // Try to use fallocate() first. + if fallocate(self, FallocateMode::ZeroRange, true, offset, length as u64).is_ok() { return Ok(length); } // fall back to write() - - // punch_hole() failed; fall back to writing a buffer of zeroes + // fallocate() failed; fall back to writing a buffer of zeroes // until we have written up to length. let buf_size = min(length, 0x10000); let buf = vec![0u8; buf_size];