diff --git a/block/src/lib.rs b/block/src/lib.rs index b081c8773..aed9c0ab3 100644 --- a/block/src/lib.rs +++ b/block/src/lib.rs @@ -39,7 +39,7 @@ use std::io::{self, IoSlice, IoSliceMut, Read, Seek, SeekFrom, Write}; use std::os::linux::fs::MetadataExt; use std::os::unix::io::AsRawFd; use std::path::Path; -use std::sync::{Arc, MutexGuard}; +use std::sync::Arc; use std::time::Instant; use std::{cmp, result}; @@ -646,10 +646,7 @@ pub fn block_io_uring_is_supported() -> bool { } } -pub trait AsyncAdaptor -where - F: Read + Write + Seek, -{ +pub trait AsyncAdaptor { fn read_vectored_sync( &mut self, offset: libc::off_t, @@ -657,7 +654,10 @@ where user_data: u64, eventfd: &EventFd, completion_list: &mut VecDeque<(u64, i32)>, - ) -> AsyncIoResult<()> { + ) -> AsyncIoResult<()> + where + Self: Read + Seek, + { // Convert libc::iovec into IoSliceMut let mut slices: SmallVec<[IoSliceMut; DEFAULT_DESCRIPTOR_VEC_SIZE]> = SmallVec::with_capacity(iovecs.len()); @@ -669,15 +669,13 @@ where } let result = { - let mut file = self.file(); - // Move the cursor to the right offset - file.seek(SeekFrom::Start(offset as u64)) + self.seek(SeekFrom::Start(offset as u64)) .map_err(AsyncIoError::ReadVectored)?; let mut r = 0; for b in slices.iter_mut() { - r += file.read(b).map_err(AsyncIoError::ReadVectored)?; + r += self.read(b).map_err(AsyncIoError::ReadVectored)?; } r }; @@ -695,7 +693,10 @@ where user_data: u64, eventfd: &EventFd, completion_list: &mut VecDeque<(u64, i32)>, - ) -> AsyncIoResult<()> { + ) -> AsyncIoResult<()> + where + Self: Write + Seek, + { // Convert libc::iovec into IoSlice let mut slices: SmallVec<[IoSlice; DEFAULT_DESCRIPTOR_VEC_SIZE]> = SmallVec::with_capacity(iovecs.len()); @@ -707,15 +708,13 @@ where } let result = { - let mut file = self.file(); - // Move the cursor to the right offset - file.seek(SeekFrom::Start(offset as u64)) + self.seek(SeekFrom::Start(offset as u64)) .map_err(AsyncIoError::WriteVectored)?; let mut r = 0; for b in slices.iter() { - r += file.write(b).map_err(AsyncIoError::WriteVectored)?; + r += self.write(b).map_err(AsyncIoError::WriteVectored)?; } r }; @@ -731,12 +730,13 @@ where user_data: Option, eventfd: &EventFd, completion_list: &mut VecDeque<(u64, i32)>, - ) -> AsyncIoResult<()> { + ) -> AsyncIoResult<()> + where + Self: Write, + { let result: i32 = { - let mut file = self.file(); - // Flush - file.flush().map_err(AsyncIoError::Fsync)?; + self.flush().map_err(AsyncIoError::Fsync)?; 0 }; @@ -748,8 +748,6 @@ where Ok(()) } - - fn file(&mut self) -> MutexGuard<'_, F>; } pub enum ImageType { diff --git a/block/src/qcow/mod.rs b/block/src/qcow/mod.rs index 6d74232dd..7733ff0ce 100644 --- a/block/src/qcow/mod.rs +++ b/block/src/qcow/mod.rs @@ -425,7 +425,7 @@ fn max_refcount_clusters(refcount_order: u32, cluster_size: u32, num_clusters: u /// # Ok(()) /// # } /// ``` -#[derive(Debug)] +#[derive(Clone, Debug)] pub struct QcowFile { raw_file: QcowRawFile, header: QcowHeader, diff --git a/block/src/qcow_sync.rs b/block/src/qcow_sync.rs index f07e245e0..332db2486 100644 --- a/block/src/qcow_sync.rs +++ b/block/src/qcow_sync.rs @@ -6,7 +6,6 @@ use std::collections::VecDeque; use std::fs::File; use std::io::{Seek, SeekFrom}; use std::os::fd::AsRawFd; -use std::sync::{Arc, Mutex, MutexGuard}; use vmm_sys_util::eventfd::EventFd; @@ -17,22 +16,22 @@ use crate::qcow::{QcowFile, RawFile, Result as QcowResult}; use crate::AsyncAdaptor; pub struct QcowDiskSync { - qcow_file: Arc>, + qcow_file: QcowFile, } impl QcowDiskSync { pub fn new(file: File, direct_io: bool) -> QcowResult { Ok(QcowDiskSync { - qcow_file: Arc::new(Mutex::new(QcowFile::from(RawFile::new(file, direct_io))?)), + qcow_file: QcowFile::from(RawFile::new(file, direct_io))?, }) } } impl DiskFile for QcowDiskSync { fn size(&mut self) -> DiskFileResult { - let mut file = self.qcow_file.lock().unwrap(); - - file.seek(SeekFrom::End(0)).map_err(DiskFileError::Size) + self.qcow_file + .seek(SeekFrom::End(0)) + .map_err(DiskFileError::Size) } fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult> { @@ -40,19 +39,18 @@ impl DiskFile for QcowDiskSync { } fn fd(&mut self) -> BorrowedDiskFd<'_> { - let lock = self.qcow_file.lock().unwrap(); - BorrowedDiskFd::new(lock.as_raw_fd()) + BorrowedDiskFd::new(self.qcow_file.as_raw_fd()) } } pub struct QcowSync { - qcow_file: Arc>, + qcow_file: QcowFile, eventfd: EventFd, completion_list: VecDeque<(u64, i32)>, } impl QcowSync { - pub fn new(qcow_file: Arc>) -> Self { + pub fn new(qcow_file: QcowFile) -> Self { QcowSync { qcow_file, eventfd: EventFd::new(libc::EFD_NONBLOCK) @@ -62,11 +60,7 @@ impl QcowSync { } } -impl AsyncAdaptor for Arc> { - fn file(&mut self) -> MutexGuard<'_, QcowFile> { - self.lock().unwrap() - } -} +impl AsyncAdaptor for QcowFile {} impl AsyncIo for QcowSync { fn notifier(&self) -> &EventFd { diff --git a/block/src/vhdx_sync.rs b/block/src/vhdx_sync.rs index d832f5e3c..fd5888e63 100644 --- a/block/src/vhdx_sync.rs +++ b/block/src/vhdx_sync.rs @@ -5,7 +5,6 @@ use std::collections::VecDeque; use std::fs::File; use std::os::fd::AsRawFd; -use std::sync::{Arc, Mutex, MutexGuard}; use vmm_sys_util::eventfd::EventFd; @@ -16,20 +15,20 @@ use crate::vhdx::{Result as VhdxResult, Vhdx}; use crate::AsyncAdaptor; pub struct VhdxDiskSync { - vhdx_file: Arc>, + vhdx_file: Vhdx, } impl VhdxDiskSync { pub fn new(f: File) -> VhdxResult { Ok(VhdxDiskSync { - vhdx_file: Arc::new(Mutex::new(Vhdx::new(f)?)), + vhdx_file: Vhdx::new(f)?, }) } } impl DiskFile for VhdxDiskSync { fn size(&mut self) -> DiskFileResult { - Ok(self.vhdx_file.lock().unwrap().virtual_disk_size()) + Ok(self.vhdx_file.virtual_disk_size()) } fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult> { @@ -40,19 +39,18 @@ impl DiskFile for VhdxDiskSync { } fn fd(&mut self) -> BorrowedDiskFd<'_> { - let lock = self.vhdx_file.lock().unwrap(); - BorrowedDiskFd::new(lock.as_raw_fd()) + BorrowedDiskFd::new(self.vhdx_file.as_raw_fd()) } } pub struct VhdxSync { - vhdx_file: Arc>, + vhdx_file: Vhdx, eventfd: EventFd, completion_list: VecDeque<(u64, i32)>, } impl VhdxSync { - pub fn new(vhdx_file: Arc>) -> std::io::Result { + pub fn new(vhdx_file: Vhdx) -> std::io::Result { Ok(VhdxSync { vhdx_file, eventfd: EventFd::new(libc::EFD_NONBLOCK)?, @@ -61,11 +59,7 @@ impl VhdxSync { } } -impl AsyncAdaptor for Arc> { - fn file(&mut self) -> MutexGuard<'_, Vhdx> { - self.lock().unwrap() - } -} +impl AsyncAdaptor for Vhdx {} impl AsyncIo for VhdxSync { fn notifier(&self) -> &EventFd {