block_util: provide and use AsyncAdaptor trait

The observation is that the code in question was used to bridge
synchronized and asynchronized code.

We can group the functions for that purpose under an adaptor trait. To
limit the scope of locking, the users of the trait are required to
implement a method to return a MutexGuard for the underlying file.

This then allows us to use concrete types (QcowFile and Vhdx) in code,
which is easier to read than a bunch of traits.

No functional change intended.

Signed-off-by: Wei Liu <liuwe@microsoft.com>
This commit is contained in:
Wei Liu
2021-12-06 10:57:42 +00:00
committed by Sebastien Boeuf
parent a29e53e436
commit af770c814b
3 changed files with 118 additions and 133 deletions
+16 -25
View File
@@ -3,14 +3,14 @@
// SPDX-License-Identifier: Apache-2.0
use crate::async_io::{AsyncIo, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult};
use crate::{fsync_sync, read_vectored_sync, write_vectored_sync, ReadWriteSeekFile};
use crate::AsyncAdaptor;
use std::fs::File;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, MutexGuard};
use vhdx::vhdx::{Result as VhdxResult, Vhdx};
use vmm_sys_util::eventfd::EventFd;
pub struct VhdxDiskSync {
vhdx_file: Arc<Mutex<dyn ReadWriteSeekFile + Sync + Send>>,
vhdx_file: Arc<Mutex<Vhdx>>,
}
impl VhdxDiskSync {
@@ -23,14 +23,7 @@ impl VhdxDiskSync {
impl DiskFile for VhdxDiskSync {
fn size(&mut self) -> DiskFileResult<u64> {
Ok(self
.vhdx_file
.lock()
.unwrap()
.as_any()
.downcast_ref::<Vhdx>()
.unwrap()
.virtual_disk_size())
Ok(self.vhdx_file.lock().unwrap().virtual_disk_size())
}
fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> {
@@ -42,15 +35,13 @@ impl DiskFile for VhdxDiskSync {
}
pub struct VhdxSync {
vhdx_file: Arc<Mutex<dyn ReadWriteSeekFile + Sync + Send>>,
vhdx_file: Arc<Mutex<Vhdx>>,
eventfd: EventFd,
completion_list: Vec<(u64, i32)>,
}
impl VhdxSync {
pub fn new(
vhdx_file: Arc<Mutex<dyn ReadWriteSeekFile + Sync + Send>>,
) -> std::io::Result<Self> {
pub fn new(vhdx_file: Arc<Mutex<Vhdx>>) -> std::io::Result<Self> {
Ok(VhdxSync {
vhdx_file,
eventfd: EventFd::new(libc::EFD_NONBLOCK)?,
@@ -59,6 +50,12 @@ impl VhdxSync {
}
}
impl AsyncAdaptor<Vhdx> for Arc<Mutex<Vhdx>> {
fn file(&mut self) -> MutexGuard<Vhdx> {
self.lock().unwrap()
}
}
impl AsyncIo for VhdxSync {
fn notifier(&self) -> &EventFd {
&self.eventfd
@@ -70,11 +67,10 @@ impl AsyncIo for VhdxSync {
iovecs: Vec<libc::iovec>,
user_data: u64,
) -> AsyncIoResult<()> {
read_vectored_sync(
self.vhdx_file.read_vectored_sync(
offset,
iovecs,
user_data,
&mut self.vhdx_file,
&self.eventfd,
&mut self.completion_list,
)
@@ -86,23 +82,18 @@ impl AsyncIo for VhdxSync {
iovecs: Vec<libc::iovec>,
user_data: u64,
) -> AsyncIoResult<()> {
write_vectored_sync(
self.vhdx_file.write_vectored_sync(
offset,
iovecs,
user_data,
&mut self.vhdx_file,
&self.eventfd,
&mut self.completion_list,
)
}
fn fsync(&mut self, user_data: Option<u64>) -> AsyncIoResult<()> {
fsync_sync(
user_data,
&mut self.vhdx_file,
&self.eventfd,
&mut self.completion_list,
)
self.vhdx_file
.fsync_sync(user_data, &self.eventfd, &mut self.completion_list)
}
fn complete(&mut self) -> Vec<(u64, i32)> {