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 -16
View File
@@ -3,15 +3,15 @@
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
use crate::async_io::{AsyncIo, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult};
use crate::{fsync_sync, read_vectored_sync, write_vectored_sync, ReadWriteSeekFile};
use crate::AsyncAdaptor;
use qcow::{QcowFile, RawFile, Result as QcowResult};
use std::fs::File;
use std::io::SeekFrom;
use std::sync::{Arc, Mutex};
use std::io::{Seek, SeekFrom};
use std::sync::{Arc, Mutex, MutexGuard};
use vmm_sys_util::eventfd::EventFd;
pub struct QcowDiskSync {
qcow_file: Arc<Mutex<dyn ReadWriteSeekFile + Send + Sync>>,
qcow_file: Arc<Mutex<QcowFile>>,
}
impl QcowDiskSync {
@@ -35,13 +35,13 @@ impl DiskFile for QcowDiskSync {
}
pub struct QcowSync {
qcow_file: Arc<Mutex<dyn ReadWriteSeekFile + Send + Sync>>,
qcow_file: Arc<Mutex<QcowFile>>,
eventfd: EventFd,
completion_list: Vec<(u64, i32)>,
}
impl QcowSync {
pub fn new(qcow_file: Arc<Mutex<dyn ReadWriteSeekFile + Send + Sync>>) -> Self {
pub fn new(qcow_file: Arc<Mutex<QcowFile>>) -> Self {
QcowSync {
qcow_file,
eventfd: EventFd::new(libc::EFD_NONBLOCK)
@@ -51,6 +51,12 @@ impl QcowSync {
}
}
impl AsyncAdaptor<QcowFile> for Arc<Mutex<QcowFile>> {
fn file(&mut self) -> MutexGuard<QcowFile> {
self.lock().unwrap()
}
}
impl AsyncIo for QcowSync {
fn notifier(&self) -> &EventFd {
&self.eventfd
@@ -62,11 +68,10 @@ impl AsyncIo for QcowSync {
iovecs: Vec<libc::iovec>,
user_data: u64,
) -> AsyncIoResult<()> {
read_vectored_sync(
self.qcow_file.read_vectored_sync(
offset,
iovecs,
user_data,
&mut self.qcow_file,
&self.eventfd,
&mut self.completion_list,
)
@@ -78,23 +83,18 @@ impl AsyncIo for QcowSync {
iovecs: Vec<libc::iovec>,
user_data: u64,
) -> AsyncIoResult<()> {
write_vectored_sync(
self.qcow_file.write_vectored_sync(
offset,
iovecs,
user_data,
&mut self.qcow_file,
&self.eventfd,
&mut self.completion_list,
)
}
fn fsync(&mut self, user_data: Option<u64>) -> AsyncIoResult<()> {
fsync_sync(
user_data,
&mut self.qcow_file,
&self.eventfd,
&mut self.completion_list,
)
self.qcow_file
.fsync_sync(user_data, &self.eventfd, &mut self.completion_list)
}
fn complete(&mut self) -> Vec<(u64, i32)> {