2021-01-20 18:03:59 +00:00
|
|
|
// Copyright © 2021 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
|
|
|
|
2021-01-28 16:29:33 +00:00
|
|
|
use crate::async_io::{
|
2021-12-07 16:42:53 +00:00
|
|
|
AsyncIo, AsyncIoError, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult, DiskTopology,
|
2021-01-28 16:29:33 +00:00
|
|
|
};
|
2021-01-20 18:03:59 +00:00
|
|
|
use std::fs::File;
|
2021-01-28 16:29:33 +00:00
|
|
|
use std::io::{Seek, SeekFrom};
|
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
2021-01-20 18:03:59 +00:00
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
pub struct RawFileDiskSync {
|
2021-01-28 16:29:33 +00:00
|
|
|
file: File,
|
2021-01-20 18:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RawFileDiskSync {
|
2021-01-28 16:29:33 +00:00
|
|
|
pub fn new(file: File) -> Self {
|
|
|
|
RawFileDiskSync { file }
|
2021-01-20 18:03:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiskFile for RawFileDiskSync {
|
|
|
|
fn size(&mut self) -> DiskFileResult<u64> {
|
2022-11-01 21:52:40 +00:00
|
|
|
self.file
|
2021-01-28 16:29:33 +00:00
|
|
|
.seek(SeekFrom::End(0))
|
2022-11-01 21:52:40 +00:00
|
|
|
.map_err(DiskFileError::Size)
|
2021-01-20 18:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> {
|
2021-01-28 16:29:33 +00:00
|
|
|
Ok(Box::new(RawFileSync::new(self.file.as_raw_fd())) as Box<dyn AsyncIo>)
|
2021-01-20 18:03:59 +00:00
|
|
|
}
|
2021-12-07 16:42:53 +00:00
|
|
|
|
|
|
|
fn topology(&mut self) -> DiskTopology {
|
|
|
|
if let Ok(topology) = DiskTopology::probe(&mut self.file) {
|
|
|
|
topology
|
|
|
|
} else {
|
|
|
|
warn!("Unable to get device topology. Using default topology");
|
|
|
|
DiskTopology::default()
|
|
|
|
}
|
|
|
|
}
|
2021-01-20 18:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RawFileSync {
|
2021-01-28 16:29:33 +00:00
|
|
|
fd: RawFd,
|
2021-01-20 18:03:59 +00:00
|
|
|
eventfd: EventFd,
|
|
|
|
completion_list: Vec<(u64, i32)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RawFileSync {
|
2021-01-28 16:29:33 +00:00
|
|
|
pub fn new(fd: RawFd) -> Self {
|
2021-01-20 18:03:59 +00:00
|
|
|
RawFileSync {
|
2021-01-28 16:29:33 +00:00
|
|
|
fd,
|
2021-01-20 18:03:59 +00:00
|
|
|
eventfd: EventFd::new(libc::EFD_NONBLOCK).expect("Failed creating EventFd for RawFile"),
|
|
|
|
completion_list: Vec::new(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncIo for RawFileSync {
|
|
|
|
fn notifier(&self) -> &EventFd {
|
|
|
|
&self.eventfd
|
|
|
|
}
|
|
|
|
|
|
|
|
fn read_vectored(
|
|
|
|
&mut self,
|
|
|
|
offset: libc::off_t,
|
|
|
|
iovecs: Vec<libc::iovec>,
|
|
|
|
user_data: u64,
|
|
|
|
) -> AsyncIoResult<()> {
|
2022-11-16 22:29:18 +00:00
|
|
|
// SAFETY: FFI call with valid arguments
|
2021-01-28 16:29:33 +00:00
|
|
|
let result = unsafe {
|
|
|
|
libc::preadv(
|
|
|
|
self.fd as libc::c_int,
|
|
|
|
iovecs.as_ptr() as *const libc::iovec,
|
|
|
|
iovecs.len() as libc::c_int,
|
|
|
|
offset,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if result < 0 {
|
|
|
|
return Err(AsyncIoError::ReadVectored(std::io::Error::last_os_error()));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.completion_list.push((user_data, result as i32));
|
|
|
|
self.eventfd.write(1).unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
2021-01-20 18:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_vectored(
|
|
|
|
&mut self,
|
|
|
|
offset: libc::off_t,
|
|
|
|
iovecs: Vec<libc::iovec>,
|
|
|
|
user_data: u64,
|
|
|
|
) -> AsyncIoResult<()> {
|
2022-11-16 22:29:18 +00:00
|
|
|
// SAFETY: FFI call with valid arguments
|
2021-01-28 16:29:33 +00:00
|
|
|
let result = unsafe {
|
|
|
|
libc::pwritev(
|
|
|
|
self.fd as libc::c_int,
|
|
|
|
iovecs.as_ptr() as *const libc::iovec,
|
|
|
|
iovecs.len() as libc::c_int,
|
|
|
|
offset,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
if result < 0 {
|
|
|
|
return Err(AsyncIoError::WriteVectored(std::io::Error::last_os_error()));
|
|
|
|
}
|
|
|
|
|
|
|
|
self.completion_list.push((user_data, result as i32));
|
|
|
|
self.eventfd.write(1).unwrap();
|
|
|
|
|
|
|
|
Ok(())
|
2021-01-20 18:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn fsync(&mut self, user_data: Option<u64>) -> AsyncIoResult<()> {
|
2022-11-16 22:29:18 +00:00
|
|
|
// SAFETY: FFI call
|
2021-01-28 16:29:33 +00:00
|
|
|
let result = unsafe { libc::fsync(self.fd as libc::c_int) };
|
|
|
|
if result < 0 {
|
|
|
|
return Err(AsyncIoError::Fsync(std::io::Error::last_os_error()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(user_data) = user_data {
|
2022-11-01 21:52:40 +00:00
|
|
|
self.completion_list.push((user_data, result));
|
2021-01-28 16:29:33 +00:00
|
|
|
self.eventfd.write(1).unwrap();
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
2021-01-20 18:03:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn complete(&mut self) -> Vec<(u64, i32)> {
|
|
|
|
self.completion_list.drain(..).collect()
|
|
|
|
}
|
|
|
|
}
|