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.

TEST=cargo test -p sys_util write_zeroes

Change-Id: I69ab06037f72dc219e6ea9409654f97eeaba32c3
Signed-off-by: Daniel Verkamp <>
Tested-by: kokoro <>
Reviewed-by: Dylan Reid <>
diff --git a/sys_util/src/ b/sys_util/src/
index 7e28f53..ee4d3b7 100644
--- a/sys_util/src/
+++ b/sys_util/src/
@@ -87,14 +87,13 @@
 impl WriteZeroesAt for File {
     fn write_zeroes_at(&mut self, offset: u64, length: usize) -> io::Result<usize> {
-        // 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];