2021-01-22 08:56:38 +00:00
|
|
|
// Copyright © 2021 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
|
|
|
|
2021-12-05 16:54:31 +00:00
|
|
|
use crate::async_io::{AsyncIo, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult};
|
2023-07-12 02:24:28 +00:00
|
|
|
use crate::qcow::{QcowFile, RawFile, Result as QcowResult};
|
2021-12-06 10:57:42 +00:00
|
|
|
use crate::AsyncAdaptor;
|
2023-01-10 14:39:28 +00:00
|
|
|
use std::collections::VecDeque;
|
2021-01-22 08:56:38 +00:00
|
|
|
use std::fs::File;
|
2021-12-06 10:57:42 +00:00
|
|
|
use std::io::{Seek, SeekFrom};
|
|
|
|
use std::sync::{Arc, Mutex, MutexGuard};
|
2021-01-22 08:56:38 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
pub struct QcowDiskSync {
|
2021-12-06 10:57:42 +00:00
|
|
|
qcow_file: Arc<Mutex<QcowFile>>,
|
2021-01-22 08:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl QcowDiskSync {
|
2021-08-11 22:12:48 +00:00
|
|
|
pub fn new(file: File, direct_io: bool) -> QcowResult<Self> {
|
|
|
|
Ok(QcowDiskSync {
|
2021-12-05 17:53:18 +00:00
|
|
|
qcow_file: Arc::new(Mutex::new(QcowFile::from(RawFile::new(file, direct_io))?)),
|
2021-08-11 22:12:48 +00:00
|
|
|
})
|
2021-01-22 08:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiskFile for QcowDiskSync {
|
|
|
|
fn size(&mut self) -> DiskFileResult<u64> {
|
2021-12-05 17:53:18 +00:00
|
|
|
let mut file = self.qcow_file.lock().unwrap();
|
2021-12-05 16:54:31 +00:00
|
|
|
|
2022-11-01 21:52:40 +00:00
|
|
|
file.seek(SeekFrom::End(0)).map_err(DiskFileError::Size)
|
2021-01-22 08:56:38 +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(QcowSync::new(self.qcow_file.clone())) as Box<dyn AsyncIo>)
|
2021-01-22 08:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct QcowSync {
|
2021-12-06 10:57:42 +00:00
|
|
|
qcow_file: Arc<Mutex<QcowFile>>,
|
2021-01-22 08:56:38 +00:00
|
|
|
eventfd: EventFd,
|
2023-01-10 14:39:28 +00:00
|
|
|
completion_list: VecDeque<(u64, i32)>,
|
2021-01-22 08:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl QcowSync {
|
2021-12-06 10:57:42 +00:00
|
|
|
pub fn new(qcow_file: Arc<Mutex<QcowFile>>) -> Self {
|
2021-01-22 08:56:38 +00:00
|
|
|
QcowSync {
|
|
|
|
qcow_file,
|
|
|
|
eventfd: EventFd::new(libc::EFD_NONBLOCK)
|
|
|
|
.expect("Failed creating EventFd for QcowSync"),
|
2023-01-10 14:39:28 +00:00
|
|
|
completion_list: VecDeque::new(),
|
2021-01-22 08:56:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-06 10:57:42 +00:00
|
|
|
impl AsyncAdaptor<QcowFile> for Arc<Mutex<QcowFile>> {
|
|
|
|
fn file(&mut self) -> MutexGuard<QcowFile> {
|
|
|
|
self.lock().unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-22 08:56:38 +00:00
|
|
|
impl AsyncIo for QcowSync {
|
|
|
|
fn notifier(&self) -> &EventFd {
|
|
|
|
&self.eventfd
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_vectored(
|
|
|
|
&mut self,
|
|
|
|
offset: libc::off_t,
|
2023-01-10 12:44:00 +00:00
|
|
|
iovecs: &[libc::iovec],
|
2021-01-22 08:56:38 +00:00
|
|
|
user_data: u64,
|
|
|
|
) -> AsyncIoResult<()> {
|
2021-12-06 10:57:42 +00:00
|
|
|
self.qcow_file.read_vectored_sync(
|
2021-01-22 14:19:26 +00:00
|
|
|
offset,
|
|
|
|
iovecs,
|
|
|
|
user_data,
|
|
|
|
&self.eventfd,
|
|
|
|
&mut self.completion_list,
|
|
|
|
)
|
2021-01-22 08:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_vectored(
|
|
|
|
&mut self,
|
|
|
|
offset: libc::off_t,
|
2023-01-10 12:44:00 +00:00
|
|
|
iovecs: &[libc::iovec],
|
2021-01-22 08:56:38 +00:00
|
|
|
user_data: u64,
|
|
|
|
) -> AsyncIoResult<()> {
|
2021-12-06 10:57:42 +00:00
|
|
|
self.qcow_file.write_vectored_sync(
|
2021-01-22 14:19:26 +00:00
|
|
|
offset,
|
|
|
|
iovecs,
|
|
|
|
user_data,
|
|
|
|
&self.eventfd,
|
|
|
|
&mut self.completion_list,
|
|
|
|
)
|
2021-01-22 08:56:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fsync(&mut self, user_data: Option<u64>) -> AsyncIoResult<()> {
|
2021-12-06 10:57:42 +00:00
|
|
|
self.qcow_file
|
|
|
|
.fsync_sync(user_data, &self.eventfd, &mut self.completion_list)
|
2021-01-22 08:56:38 +00:00
|
|
|
}
|
|
|
|
|
2023-01-10 14:28:29 +00:00
|
|
|
fn next_completed_request(&mut self) -> Option<(u64, i32)> {
|
2023-01-10 14:39:28 +00:00
|
|
|
self.completion_list.pop_front()
|
2021-01-22 08:56:38 +00:00
|
|
|
}
|
|
|
|
}
|