summary refs log tree commit diff
path: root/sys_util
diff options
context:
space:
mode:
authorDaniel Verkamp <dverkamp@chromium.org>2019-11-12 14:42:16 -0800
committerCommit Bot <commit-bot@chromium.org>2019-11-27 21:22:39 +0000
commit41d889eb267cdcd066575b5647bf05c516d70201 (patch)
tree0c022c638bc6a742a17ed896649d0a811b932e4d /sys_util
parent6eadef77a349127a013f22f58518a6bf7852d4f5 (diff)
downloadcrosvm-41d889eb267cdcd066575b5647bf05c516d70201.tar
crosvm-41d889eb267cdcd066575b5647bf05c516d70201.tar.gz
crosvm-41d889eb267cdcd066575b5647bf05c516d70201.tar.bz2
crosvm-41d889eb267cdcd066575b5647bf05c516d70201.tar.lz
crosvm-41d889eb267cdcd066575b5647bf05c516d70201.tar.xz
crosvm-41d889eb267cdcd066575b5647bf05c516d70201.tar.zst
crosvm-41d889eb267cdcd066575b5647bf05c516d70201.zip
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>
Diffstat (limited to 'sys_util')
-rw-r--r--sys_util/src/write_zeroes.rs7
1 files changed, 3 insertions, 4 deletions
diff --git a/sys_util/src/write_zeroes.rs b/sys_util/src/write_zeroes.rs
index 7e28f53..ee4d3b7 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<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];