2021-07-26 15:51:03 +00:00
|
|
|
// Copyright © 2021 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
use crate::async_io::{AsyncIo, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult};
|
2021-12-06 10:57:42 +00:00
|
|
|
use crate::AsyncAdaptor;
|
2021-07-26 15:51:03 +00:00
|
|
|
use std::fs::File;
|
2021-12-06 10:57:42 +00:00
|
|
|
use std::sync::{Arc, Mutex, MutexGuard};
|
2021-07-26 15:51:03 +00:00
|
|
|
use vhdx::vhdx::{Result as VhdxResult, Vhdx};
|
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
pub struct VhdxDiskSync {
|
2021-12-06 10:57:42 +00:00
|
|
|
vhdx_file: Arc<Mutex<Vhdx>>,
|
2021-07-26 15:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl VhdxDiskSync {
|
|
|
|
pub fn new(f: File) -> VhdxResult<Self> {
|
|
|
|
Ok(VhdxDiskSync {
|
2021-12-05 17:53:18 +00:00
|
|
|
vhdx_file: Arc::new(Mutex::new(Vhdx::new(f)?)),
|
2021-07-26 15:51:03 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiskFile for VhdxDiskSync {
|
|
|
|
fn size(&mut self) -> DiskFileResult<u64> {
|
2021-12-06 10:57:42 +00:00
|
|
|
Ok(self.vhdx_file.lock().unwrap().virtual_disk_size())
|
2021-07-26 15:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> {
|
2021-12-05 17:53:18 +00:00
|
|
|
Ok(
|
|
|
|
Box::new(VhdxSync::new(self.vhdx_file.clone()).map_err(DiskFileError::NewAsyncIo)?)
|
|
|
|
as Box<dyn AsyncIo>,
|
|
|
|
)
|
2021-07-26 15:51:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct VhdxSync {
|
2021-12-06 10:57:42 +00:00
|
|
|
vhdx_file: Arc<Mutex<Vhdx>>,
|
2021-07-26 15:51:03 +00:00
|
|
|
eventfd: EventFd,
|
|
|
|
completion_list: Vec<(u64, i32)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl VhdxSync {
|
2021-12-06 10:57:42 +00:00
|
|
|
pub fn new(vhdx_file: Arc<Mutex<Vhdx>>) -> std::io::Result<Self> {
|
2021-07-26 15:51:03 +00:00
|
|
|
Ok(VhdxSync {
|
|
|
|
vhdx_file,
|
|
|
|
eventfd: EventFd::new(libc::EFD_NONBLOCK)?,
|
|
|
|
completion_list: Vec::new(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 10:57:42 +00:00
|
|
|
impl AsyncAdaptor<Vhdx> for Arc<Mutex<Vhdx>> {
|
|
|
|
fn file(&mut self) -> MutexGuard<Vhdx> {
|
|
|
|
self.lock().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 15:51:03 +00:00
|
|
|
impl AsyncIo for VhdxSync {
|
|
|
|
fn notifier(&self) -> &EventFd {
|
|
|
|
&self.eventfd
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_vectored(
|
|
|
|
&mut self,
|
|
|
|
offset: libc::off_t,
|
|
|
|
iovecs: Vec<libc::iovec>,
|
|
|
|
user_data: u64,
|
|
|
|
) -> AsyncIoResult<()> {
|
2021-12-06 10:57:42 +00:00
|
|
|
self.vhdx_file.read_vectored_sync(
|
2021-07-26 15:51:03 +00:00
|
|
|
offset,
|
|
|
|
iovecs,
|
|
|
|
user_data,
|
|
|
|
&self.eventfd,
|
|
|
|
&mut self.completion_list,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_vectored(
|
|
|
|
&mut self,
|
|
|
|
offset: libc::off_t,
|
|
|
|
iovecs: Vec<libc::iovec>,
|
|
|
|
user_data: u64,
|
|
|
|
) -> AsyncIoResult<()> {
|
2021-12-06 10:57:42 +00:00
|
|
|
self.vhdx_file.write_vectored_sync(
|
2021-07-26 15:51:03 +00:00
|
|
|
offset,
|
|
|
|
iovecs,
|
|
|
|
user_data,
|
|
|
|
&self.eventfd,
|
|
|
|
&mut self.completion_list,
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fsync(&mut self, user_data: Option<u64>) -> AsyncIoResult<()> {
|
2021-12-06 10:57:42 +00:00
|
|
|
self.vhdx_file
|
|
|
|
.fsync_sync(user_data, &self.eventfd, &mut self.completion_list)
|
2021-07-26 15:51:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn complete(&mut self) -> Vec<(u64, i32)> {
|
|
|
|
self.completion_list.drain(..).collect()
|
|
|
|
}
|
|
|
|
}
|