2021-01-20 09:55:39 +00:00
|
|
|
// Copyright © 2021 Intel Corporation
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
|
|
|
|
|
|
|
|
use crate::async_io::{
|
2021-12-07 16:42:53 +00:00
|
|
|
AsyncIo, AsyncIoError, AsyncIoResult, DiskFile, DiskFileError, DiskFileResult, DiskTopology,
|
2021-01-20 09:55:39 +00:00
|
|
|
};
|
2021-02-28 17:06:01 +00:00
|
|
|
use io_uring::{opcode, squeue, types, IoUring};
|
2021-01-20 09:55:39 +00:00
|
|
|
use std::fs::File;
|
|
|
|
use std::io::{Seek, SeekFrom};
|
|
|
|
use std::os::unix::io::{AsRawFd, RawFd};
|
|
|
|
use vmm_sys_util::eventfd::EventFd;
|
|
|
|
|
|
|
|
pub struct RawFileDisk {
|
|
|
|
file: File,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RawFileDisk {
|
|
|
|
pub fn new(file: File) -> Self {
|
|
|
|
RawFileDisk { file }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DiskFile for RawFileDisk {
|
|
|
|
fn size(&mut self) -> DiskFileResult<u64> {
|
2022-11-01 21:52:40 +00:00
|
|
|
self.file
|
2021-01-20 09:55:39 +00:00
|
|
|
.seek(SeekFrom::End(0))
|
2022-11-01 21:52:40 +00:00
|
|
|
.map_err(DiskFileError::Size)
|
2021-01-20 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fn new_async_io(&self, ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> {
|
|
|
|
Ok(Box::new(
|
|
|
|
RawFileAsync::new(self.file.as_raw_fd(), ring_depth)
|
|
|
|
.map_err(DiskFileError::NewAsyncIo)?,
|
|
|
|
) as Box<dyn AsyncIo>)
|
|
|
|
}
|
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 09:55:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct RawFileAsync {
|
|
|
|
fd: RawFd,
|
|
|
|
io_uring: IoUring,
|
|
|
|
eventfd: EventFd,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RawFileAsync {
|
|
|
|
pub fn new(fd: RawFd, ring_depth: u32) -> std::io::Result<Self> {
|
|
|
|
let io_uring = IoUring::new(ring_depth)?;
|
|
|
|
let eventfd = EventFd::new(libc::EFD_NONBLOCK)?;
|
|
|
|
|
|
|
|
// Register the io_uring eventfd that will notify when something in
|
|
|
|
// the completion queue is ready.
|
|
|
|
io_uring.submitter().register_eventfd(eventfd.as_raw_fd())?;
|
|
|
|
|
|
|
|
Ok(RawFileAsync {
|
|
|
|
fd,
|
|
|
|
io_uring,
|
|
|
|
eventfd,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl AsyncIo for RawFileAsync {
|
|
|
|
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-20 09:55:39 +00:00
|
|
|
user_data: u64,
|
|
|
|
) -> AsyncIoResult<()> {
|
2021-02-28 17:06:01 +00:00
|
|
|
let (submitter, mut sq, _) = self.io_uring.split();
|
2021-01-20 09:55:39 +00:00
|
|
|
|
2022-11-16 22:29:18 +00:00
|
|
|
// SAFETY: we know the file descriptor is valid and we
|
2021-01-20 09:55:39 +00:00
|
|
|
// relied on vm-memory to provide the buffer address.
|
|
|
|
let _ = unsafe {
|
2021-02-28 17:06:01 +00:00
|
|
|
sq.push(
|
|
|
|
&opcode::Readv::new(types::Fd(self.fd), iovecs.as_ptr(), iovecs.len() as u32)
|
|
|
|
.offset(offset)
|
|
|
|
.build()
|
|
|
|
.flags(squeue::Flags::ASYNC)
|
|
|
|
.user_data(user_data),
|
2021-01-20 09:55:39 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update the submission queue and submit new operations to the
|
|
|
|
// io_uring instance.
|
2021-02-28 17:06:01 +00:00
|
|
|
sq.sync();
|
2021-01-20 09:55:39 +00:00
|
|
|
submitter.submit().map_err(AsyncIoError::ReadVectored)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn write_vectored(
|
|
|
|
&mut self,
|
|
|
|
offset: libc::off_t,
|
2023-01-10 12:44:00 +00:00
|
|
|
iovecs: &[libc::iovec],
|
2021-01-20 09:55:39 +00:00
|
|
|
user_data: u64,
|
|
|
|
) -> AsyncIoResult<()> {
|
2021-02-28 17:06:01 +00:00
|
|
|
let (submitter, mut sq, _) = self.io_uring.split();
|
2021-01-20 09:55:39 +00:00
|
|
|
|
2022-11-16 22:29:18 +00:00
|
|
|
// SAFETY: we know the file descriptor is valid and we
|
2021-01-20 09:55:39 +00:00
|
|
|
// relied on vm-memory to provide the buffer address.
|
|
|
|
let _ = unsafe {
|
2021-02-28 17:06:01 +00:00
|
|
|
sq.push(
|
|
|
|
&opcode::Writev::new(types::Fd(self.fd), iovecs.as_ptr(), iovecs.len() as u32)
|
|
|
|
.offset(offset)
|
|
|
|
.build()
|
|
|
|
.flags(squeue::Flags::ASYNC)
|
|
|
|
.user_data(user_data),
|
2021-01-20 09:55:39 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update the submission queue and submit new operations to the
|
|
|
|
// io_uring instance.
|
2021-02-28 17:06:01 +00:00
|
|
|
sq.sync();
|
2021-01-20 09:55:39 +00:00
|
|
|
submitter.submit().map_err(AsyncIoError::WriteVectored)?;
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn fsync(&mut self, user_data: Option<u64>) -> AsyncIoResult<()> {
|
|
|
|
if let Some(user_data) = user_data {
|
2021-02-28 17:06:01 +00:00
|
|
|
let (submitter, mut sq, _) = self.io_uring.split();
|
2021-01-20 09:55:39 +00:00
|
|
|
|
2022-11-16 22:29:18 +00:00
|
|
|
// SAFETY: we know the file descriptor is valid.
|
2021-01-20 09:55:39 +00:00
|
|
|
let _ = unsafe {
|
2021-02-28 17:06:01 +00:00
|
|
|
sq.push(
|
|
|
|
&opcode::Fsync::new(types::Fd(self.fd))
|
2021-01-20 09:55:39 +00:00
|
|
|
.build()
|
|
|
|
.flags(squeue::Flags::ASYNC)
|
|
|
|
.user_data(user_data),
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
// Update the submission queue and submit new operations to the
|
|
|
|
// io_uring instance.
|
2021-02-28 17:06:01 +00:00
|
|
|
sq.sync();
|
2021-01-20 09:55:39 +00:00
|
|
|
submitter.submit().map_err(AsyncIoError::Fsync)?;
|
|
|
|
} else {
|
2022-11-16 22:29:18 +00:00
|
|
|
// SAFETY: FFI call with a valid fd
|
2021-01-20 09:55:39 +00:00
|
|
|
unsafe { libc::fsync(self.fd) };
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2023-01-10 14:28:29 +00:00
|
|
|
fn next_completed_request(&mut self) -> Option<(u64, i32)> {
|
|
|
|
self.io_uring
|
|
|
|
.completion()
|
|
|
|
.next()
|
|
|
|
.map(|entry| (entry.user_data(), entry.result()))
|
2021-01-20 09:55:39 +00:00
|
|
|
}
|
|
|
|
}
|