block: introduce trait BlockBackend for block types

This commit introduces the trait `BlockBackend` with generic ops
including read, write and seek, which can be used for common I/O
interfaces for the block types without using `DiskFile` and `AsyncIo`.

Signed-off-by: Yu Li <liyu.yukiteru@bytedance.com>
This commit is contained in:
Yu Li
2023-07-12 10:27:45 +08:00
committed by Rob Bradford
parent 447cad3861
commit 741d640330
7 changed files with 71 additions and 8 deletions

View File

@@ -7,6 +7,7 @@ use crate::async_io::{
};
use crate::fixed_vhd::FixedVhd;
use crate::raw_sync::RawFileSync;
use crate::BlockBackend;
use std::fs::File;
use std::os::unix::io::{AsRawFd, RawFd};
use vmm_sys_util::eventfd::EventFd;
@@ -21,12 +22,12 @@ impl FixedVhdDiskSync {
impl DiskFile for FixedVhdDiskSync {
fn size(&mut self) -> DiskFileResult<u64> {
Ok(self.0.size())
Ok(self.0.size().unwrap())
}
fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> {
Ok(Box::new(
FixedVhdSync::new(self.0.as_raw_fd(), self.0.size())
FixedVhdSync::new(self.0.as_raw_fd(), self.0.size().unwrap())
.map_err(DiskFileError::NewAsyncIo)?,
) as Box<dyn AsyncIo>)
}