From ec5916daa056b2ad68a5dc2a7c604299aa3e2c8f Mon Sep 17 00:00:00 2001 From: Daniel Verkamp Date: Mon, 7 Oct 2019 13:06:22 -0700 Subject: devices: virtio: block: use FileReadWriteAtVolatile Use the "at" variants of the read/write functions in the block device. This reduces the number of syscalls on the host per I/O to one (pread64/pwrite64) rather than two (lseek + read/write). The CompositeDiskFile implementation is also updated in this commit, since it's both a producer and consumer of DiskFile, and it isn't trivial to update it in a separate commit without breaking compilation. BUG=None TEST=Start Crostini on kevin, banon, and nami Change-Id: I031e7e87cd6c99504db8c56b1725ea51c1e27a53 Signed-off-by: Daniel Verkamp Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/1845948 Tested-by: kokoro Reviewed-by: Stephen Barber --- disk/src/composite.rs | 30 ++++++++++-------------------- disk/src/disk.rs | 14 +++++++++++--- 2 files changed, 21 insertions(+), 23 deletions(-) (limited to 'disk/src') diff --git a/disk/src/composite.rs b/disk/src/composite.rs index 5d6e5a2..6c4ae04 100644 --- a/disk/src/composite.rs +++ b/disk/src/composite.rs @@ -14,7 +14,7 @@ use crate::{create_disk_file, DiskFile, ImageType}; use data_model::VolatileSlice; use protos::cdisk_spec; use remain::sorted; -use sys_util::{AsRawFds, FileReadWriteVolatile, FileSetLen, FileSync, PunchHole, WriteZeroes}; +use sys_util::{AsRawFds, FileReadWriteAtVolatile, FileSetLen, FileSync, PunchHole, WriteZeroes}; #[sorted] #[derive(Debug)] @@ -235,12 +235,10 @@ impl FileSync for CompositeDiskFile { // // If one of the component disks does a partial read or write, that also gets passed // transparently to the parent. -impl FileReadWriteVolatile for CompositeDiskFile { - fn read_volatile(&mut self, slice: VolatileSlice) -> io::Result { - let cursor_location = self.cursor_location; +impl FileReadWriteAtVolatile for CompositeDiskFile { + fn read_at_volatile(&mut self, slice: VolatileSlice, offset: u64) -> io::Result { + let cursor_location = offset; let disk = self.disk_at_offset(cursor_location)?; - disk.file - .seek(SeekFrom::Start(cursor_location - disk.offset))?; let subslice = if cursor_location + slice.size() > disk.offset + disk.length { let new_size = disk.offset + disk.length - cursor_location; slice @@ -249,17 +247,12 @@ impl FileReadWriteVolatile for CompositeDiskFile { } else { slice }; - let result = disk.file.read_volatile(subslice); - if let Ok(size) = result { - self.cursor_location += size as u64; - } - result + disk.file + .read_at_volatile(subslice, cursor_location - disk.offset) } - fn write_volatile(&mut self, slice: VolatileSlice) -> io::Result { - let cursor_location = self.cursor_location; + fn write_at_volatile(&mut self, slice: VolatileSlice, offset: u64) -> io::Result { + let cursor_location = offset; let disk = self.disk_at_offset(cursor_location)?; - disk.file - .seek(SeekFrom::Start(cursor_location - disk.offset))?; let subslice = if cursor_location + slice.size() > disk.offset + disk.length { let new_size = disk.offset + disk.length - cursor_location; slice @@ -268,11 +261,8 @@ impl FileReadWriteVolatile for CompositeDiskFile { } else { slice }; - let result = disk.file.write_volatile(subslice); - if let Ok(size) = result { - self.cursor_location += size as u64; - } - result + disk.file + .write_at_volatile(subslice, cursor_location - disk.offset) } } diff --git a/disk/src/disk.rs b/disk/src/disk.rs index f63d63c..b5692a3 100644 --- a/disk/src/disk.rs +++ b/disk/src/disk.rs @@ -11,7 +11,7 @@ use libc::EINVAL; use qcow::{QcowFile, QCOW_MAGIC}; use remain::sorted; use sys_util::{ - AsRawFds, FileReadWriteVolatile, FileSetLen, FileSync, PunchHole, SeekHole, WriteZeroes, + AsRawFds, FileReadWriteAtVolatile, FileSetLen, FileSync, PunchHole, SeekHole, WriteZeroes, }; #[cfg(feature = "composite-disk")] @@ -38,15 +38,23 @@ pub enum Error { pub type Result = std::result::Result; /// The prerequisites necessary to support a block device. +#[rustfmt::skip] // rustfmt won't wrap the long list of trait bounds. pub trait DiskFile: - FileSetLen + FileSync + FileReadWriteVolatile + PunchHole + Seek + WriteZeroes + Send + AsRawFds + FileSetLen + + FileSync + + FileReadWriteAtVolatile + + PunchHole + + Seek + + WriteZeroes + + Send + + AsRawFds { } impl< D: FileSetLen + FileSync + PunchHole - + FileReadWriteVolatile + + FileReadWriteAtVolatile + Seek + WriteZeroes + Send -- cgit 1.4.1