sys_util: use PUNCH_HOLE to implement write_zeroes

Some filesystems do not support FALLOC_FL_ZERO_RANGE; in particular,
encrypted files on ext4 fail this request with -EOPNOTSUPP.  Use
fallocate with FALLOC_FL_PUNCH_HOLE instead, which is more widely
supported.

BUG=None
TEST=strace crosvm using qcow files on encrypted ext4 and verify that
     fallocate(FALLOC_FL_PUNCH_HOLE) is issued when required.

Change-Id: Idffabc75ea0e1153efbb13cec8b4a25570427235
Signed-off-by: Daniel Verkamp <dverkamp@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1250022
Reviewed-by: Stephen Barber <smbarber@chromium.org>
Reviewed-by: Dylan Reid <dgreid@chromium.org>
This commit is contained in:
Daniel Verkamp 2018-09-27 13:47:31 -07:00 committed by chrome-bot
parent 6c765fd2ac
commit abbe0c8cf0

View file

@ -17,9 +17,9 @@ pub trait WriteZeroes {
impl WriteZeroes for File {
fn write_zeroes(&mut self, length: usize) -> io::Result<usize> {
// Try to use fallocate(FALLOC_FL_ZERO_RANGE) first.
// Try to use fallocate(FALLOC_FL_PUNCH_HOLE) first.
let offset = self.seek(SeekFrom::Current(0))?;
match fallocate(self, FallocateMode::ZeroRange, false, offset, length as u64) {
match fallocate(self, FallocateMode::PunchHole, true, offset, length as u64) {
Ok(()) => {
// Advance the seek cursor as if we had done a real write().
self.seek(SeekFrom::Current(length as i64))?;