mirror of
https://chromium.googlesource.com/crosvm/crosvm
synced 2025-02-05 18:20:34 +00:00
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:
parent
6eadef77a3
commit
41d889eb26
1 changed files with 3 additions and 4 deletions
|
@ -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];
|
||||
|
|
Loading…
Reference in a new issue