block: Add owned raw Linux AIO path

Start using AioDataIo from RawFileAsyncAio. This adds a temporary
submit_borrowed_operation to enable preserving the unsafe iovec api
until we can remove it in the forthcoming commits.

Signed-off-by: Dylan Reid <dgreid@fb.com>
This commit is contained in:
Dylan Reid
2026-05-19 14:37:17 -07:00
committed by Rob Bradford
parent a541baa9e4
commit 9143039805
2 changed files with 100 additions and 96 deletions
+36 -72
View File
@@ -1,44 +1,40 @@
// Copyright © 2023 Intel Corporation
//
// Copyright (c) Meta Platforms, Inc. and affiliates.
//
// SPDX-License-Identifier: Apache-2.0 AND BSD-3-Clause
//
// Copyright © 2023 Crusoe Energy Systems LLC
//
use std::collections::VecDeque;
use std::os::unix::io::{AsRawFd, RawFd};
use std::os::unix::io::RawFd;
use vmm_sys_util::aio;
use vmm_sys_util::eventfd::EventFd;
use crate::async_io::{AsyncIo, AsyncIoError, AsyncIoResult};
use crate::async_io::{
AioDataIo, AsyncIo, AsyncIoCompletion, AsyncIoError, AsyncIoOperation, AsyncIoResult,
};
use crate::error::{BlockError, BlockErrorKind, BlockResult};
use crate::sparse::{punch_hole, write_zeroes};
use crate::{SECTOR_SIZE, is_block_device};
pub struct RawFileAsyncAio {
fd: RawFd,
ctx: aio::IoContext,
eventfd: EventFd,
data_io: AioDataIo,
alignment: u64,
completion_list: VecDeque<(u64, i32)>,
is_block_device: bool,
}
impl RawFileAsyncAio {
pub fn new(fd: RawFd, queue_depth: u32) -> BlockResult<Self> {
let eventfd =
EventFd::new(libc::EFD_NONBLOCK).map_err(|e| BlockError::new(BlockErrorKind::Io, e))?;
let ctx =
aio::IoContext::new(queue_depth).map_err(|e| BlockError::new(BlockErrorKind::Io, e))?;
let data_io =
AioDataIo::new(queue_depth).map_err(|e| BlockError::new(BlockErrorKind::Io, e))?;
let is_block_device = is_block_device(fd);
Ok(RawFileAsyncAio {
fd,
ctx,
eventfd,
data_io,
alignment: SECTOR_SIZE,
completion_list: VecDeque::new(),
is_block_device,
})
}
@@ -46,7 +42,7 @@ impl RawFileAsyncAio {
impl AsyncIo for RawFileAsyncAio {
fn notifier(&self) -> &EventFd {
&self.eventfd
self.data_io.notifier()
}
fn alignment(&self) -> u64 {
@@ -59,23 +55,9 @@ impl AsyncIo for RawFileAsyncAio {
iovecs: &[libc::iovec],
user_data: u64,
) -> AsyncIoResult<()> {
let iocbs = [&mut aio::IoControlBlock {
aio_fildes: self.fd.as_raw_fd() as u32,
aio_lio_opcode: aio::IOCB_CMD_PREADV as u16,
aio_buf: iovecs.as_ptr() as u64,
aio_nbytes: iovecs.len() as u64,
aio_offset: offset,
aio_data: user_data,
aio_flags: aio::IOCB_FLAG_RESFD,
aio_resfd: self.eventfd.as_raw_fd() as u32,
..Default::default()
}];
let _ = self
.ctx
.submit(&iocbs[..])
.map_err(AsyncIoError::ReadVectored)?;
Ok(())
self.data_io
.submit_borrowed_operation(self.fd, offset, true, iovecs, user_data)
.map_err(AsyncIoError::ReadVectored)
}
fn write_vectored(
@@ -84,36 +66,27 @@ impl AsyncIo for RawFileAsyncAio {
iovecs: &[libc::iovec],
user_data: u64,
) -> AsyncIoResult<()> {
let iocbs = [&mut aio::IoControlBlock {
aio_fildes: self.fd.as_raw_fd() as u32,
aio_lio_opcode: aio::IOCB_CMD_PWRITEV as u16,
aio_buf: iovecs.as_ptr() as u64,
aio_nbytes: iovecs.len() as u64,
aio_offset: offset,
aio_data: user_data,
aio_flags: aio::IOCB_FLAG_RESFD,
aio_resfd: self.eventfd.as_raw_fd() as u32,
..Default::default()
}];
let _ = self
.ctx
.submit(&iocbs[..])
.map_err(AsyncIoError::WriteVectored)?;
self.data_io
.submit_borrowed_operation(self.fd, offset, false, iovecs, user_data)
.map_err(AsyncIoError::WriteVectored)
}
Ok(())
fn submit_data_operation(&mut self, op: AsyncIoOperation) -> AsyncIoResult<()> {
let is_read = op.is_read();
self.data_io.submit_operation(self.fd, op).map_err(|e| {
if is_read {
AsyncIoError::ReadVectored(e)
} else {
AsyncIoError::WriteVectored(e)
}
})
}
fn fsync(&mut self, user_data: Option<u64>) -> AsyncIoResult<()> {
if let Some(user_data) = user_data {
let iocbs = [&mut aio::IoControlBlock {
aio_fildes: self.fd.as_raw_fd() as u32,
aio_lio_opcode: aio::IOCB_CMD_FSYNC as u16,
aio_data: user_data,
aio_flags: aio::IOCB_FLAG_RESFD,
aio_resfd: self.eventfd.as_raw_fd() as u32,
..Default::default()
}];
let _ = self.ctx.submit(&iocbs[..]).map_err(AsyncIoError::Fsync)?;
self.data_io
.submit_fsync(self.fd, user_data)
.map_err(AsyncIoError::Fsync)?;
} else {
// SAFETY: FFI call with a valid fd
unsafe { libc::fsync(self.fd) };
@@ -122,17 +95,8 @@ impl AsyncIo for RawFileAsyncAio {
Ok(())
}
fn next_completed_request(&mut self) -> Option<(u64, i32)> {
if self.completion_list.is_empty() {
// Drain pending AIO completions batched into the same queue.
let mut events = [aio::IoEvent::default(); 32];
let rc = self.ctx.get_events(0, &mut events, None).unwrap();
for event in &events[..rc] {
self.completion_list
.push_back((event.data, event.res as i32));
}
}
self.completion_list.pop_front()
fn next_completion(&mut self) -> Option<AsyncIoCompletion> {
self.data_io.next_completion()
}
fn punch_hole(&mut self, offset: u64, length: u64, user_data: u64) -> AsyncIoResult<()> {
@@ -141,8 +105,8 @@ impl AsyncIo for RawFileAsyncAio {
// list, matching the pattern used by the sync backend (RawFileSync).
punch_hole(self.fd, self.is_block_device, offset, length)
.map_err(AsyncIoError::PunchHole)?;
self.completion_list.push_back((user_data, 0));
self.eventfd.write(1).unwrap();
self.data_io
.inject_completion(AsyncIoCompletion::new(user_data, 0, None));
Ok(())
}
@@ -151,8 +115,8 @@ impl AsyncIo for RawFileAsyncAio {
// Same as punch_hole().
write_zeroes(self.fd, self.is_block_device, offset, length)
.map_err(AsyncIoError::WriteZeroes)?;
self.completion_list.push_back((user_data, 0));
self.eventfd.write(1).unwrap();
self.data_io
.inject_completion(AsyncIoCompletion::new(user_data, 0, None));
Ok(())
}