mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
block: raw: Flatten worker module
Remove the worker submodule layer from the raw format directory. The backend files move up as engine_sync.rs, engine_uring.rs, and engine_aio.rs, the shared test helpers move up as tests.rs, and the two alignment helper functions from worker/mod.rs merge into the raw module. The vhd backends that reused the raw io_uring and sync engines are updated to the new block::formats::raw::engine_* paths. Assisted-by: Claude:Opus-4.8 Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
This commit is contained in:
committed by
Rob Bradford
parent
7120311462
commit
ecf72ba787
@@ -19,7 +19,7 @@ use crate::error::{BlockError, BlockErrorKind, BlockResult};
|
||||
use crate::sparse::{punch_hole, write_zeroes};
|
||||
use crate::{AlignedFile, is_block_device};
|
||||
|
||||
pub struct RawAio {
|
||||
pub(super) struct RawAio {
|
||||
raw_file: AlignedFile,
|
||||
data_io: AioDataIo,
|
||||
alignment: u64,
|
||||
@@ -27,7 +27,7 @@ pub struct RawAio {
|
||||
}
|
||||
|
||||
impl RawAio {
|
||||
pub fn new(raw_file: AlignedFile, queue_depth: u32) -> BlockResult<Self> {
|
||||
pub(super) fn new(raw_file: AlignedFile, queue_depth: u32) -> BlockResult<Self> {
|
||||
let data_io =
|
||||
AioDataIo::new(queue_depth).map_err(|e| BlockError::new(BlockErrorKind::Io, e))?;
|
||||
let is_block_device = is_block_device(raw_file.as_raw_fd());
|
||||
@@ -128,7 +128,7 @@ mod unit_tests {
|
||||
use vmm_sys_util::tempfile::TempFile;
|
||||
|
||||
use super::*;
|
||||
use crate::formats::raw::worker::tests;
|
||||
use crate::formats::raw::tests;
|
||||
|
||||
#[test]
|
||||
fn test_punch_hole() {
|
||||
@@ -15,7 +15,7 @@ use crate::async_io::{AsyncIo, AsyncIoCompletion, AsyncIoError, AsyncIoOperation
|
||||
use crate::sparse::{punch_hole, write_zeroes};
|
||||
use crate::{AlignedFile, is_block_device};
|
||||
|
||||
pub struct RawSync {
|
||||
pub(crate) struct RawSync {
|
||||
raw_file: AlignedFile,
|
||||
eventfd: EventFd,
|
||||
completion_list: VecDeque<AsyncIoCompletion>,
|
||||
@@ -24,7 +24,7 @@ pub struct RawSync {
|
||||
}
|
||||
|
||||
impl RawSync {
|
||||
pub fn new(raw_file: AlignedFile) -> Self {
|
||||
pub(crate) fn new(raw_file: AlignedFile) -> Self {
|
||||
let is_block_device = is_block_device(raw_file.as_raw_fd());
|
||||
let alignment = raw_file.alignment() as u64;
|
||||
RawSync {
|
||||
@@ -153,7 +153,7 @@ mod unit_tests {
|
||||
use vmm_sys_util::tempfile::TempFile;
|
||||
|
||||
use super::*;
|
||||
use crate::formats::raw::worker::tests;
|
||||
use crate::formats::raw::tests;
|
||||
|
||||
#[test]
|
||||
fn test_punch_hole() {
|
||||
@@ -17,7 +17,7 @@ use crate::error::{BlockError, BlockErrorKind, BlockResult};
|
||||
use crate::sparse::{blkdiscard, blkzeroout};
|
||||
use crate::{AlignedFile, is_block_device};
|
||||
|
||||
pub struct RawAsync {
|
||||
pub(crate) struct RawAsync {
|
||||
raw_file: AlignedFile,
|
||||
data_io: UringDataIo,
|
||||
alignment: u64,
|
||||
@@ -25,7 +25,7 @@ pub struct RawAsync {
|
||||
}
|
||||
|
||||
impl RawAsync {
|
||||
pub fn new(raw_file: AlignedFile, ring_depth: u32) -> BlockResult<Self> {
|
||||
pub(crate) fn new(raw_file: AlignedFile, ring_depth: u32) -> BlockResult<Self> {
|
||||
let data_io =
|
||||
UringDataIo::new(ring_depth).map_err(|e| BlockError::new(BlockErrorKind::Io, e))?;
|
||||
let is_block_device = is_block_device(raw_file.as_raw_fd());
|
||||
@@ -14,15 +14,22 @@ use std::os::unix::io::AsRawFd;
|
||||
|
||||
use log::warn;
|
||||
|
||||
use self::worker::async_aio::RawAio;
|
||||
use self::engine_aio::RawAio;
|
||||
use self::engine_sync::RawSync;
|
||||
#[cfg(feature = "io_uring")]
|
||||
use self::worker::async_uring::RawAsync;
|
||||
use self::worker::sync::RawSync;
|
||||
use crate::async_io::{AsyncIo, BorrowedDiskFd, DiskFileError};
|
||||
use self::engine_uring::RawAsync;
|
||||
use crate::async_io::{
|
||||
AsyncIo, AsyncIoError, AsyncIoOperation, AsyncIoResult, BorrowedDiskFd, DiskFileError,
|
||||
};
|
||||
use crate::error::{BlockError, BlockErrorKind, BlockResult};
|
||||
use crate::{AlignedFile, DiskTopology, disk_file, probe_sparse_support, query_device_size};
|
||||
|
||||
pub(crate) mod worker;
|
||||
mod engine_aio;
|
||||
pub(crate) mod engine_sync;
|
||||
#[cfg(feature = "io_uring")]
|
||||
pub(crate) mod engine_uring;
|
||||
#[cfg(test)]
|
||||
mod tests;
|
||||
|
||||
/// Selects which async I/O backend a `RawDisk` uses.
|
||||
#[derive(Clone, Copy, Debug, PartialEq)]
|
||||
@@ -155,6 +162,41 @@ impl disk_file::AsyncDiskFile for RawDisk {
|
||||
}
|
||||
}
|
||||
|
||||
/// True when `op` satisfies `alignment` and can go straight to the kernel.
|
||||
fn operation_is_aligned(op: &AsyncIoOperation, alignment: u64) -> bool {
|
||||
if alignment == 0 {
|
||||
return true;
|
||||
}
|
||||
if !(op.offset() as u64).is_multiple_of(alignment) {
|
||||
return false;
|
||||
}
|
||||
op.iovecs().iter().all(|iov| {
|
||||
(iov.iov_base as u64).is_multiple_of(alignment)
|
||||
&& (iov.iov_len as u64).is_multiple_of(alignment)
|
||||
})
|
||||
}
|
||||
|
||||
/// Runs an unaligned O_DIRECT operation synchronously through `aligned_file`.
|
||||
fn run_unaligned_operation(
|
||||
aligned_file: &AlignedFile,
|
||||
op: &mut AsyncIoOperation,
|
||||
) -> AsyncIoResult<i32> {
|
||||
let offset = op.offset() as u64;
|
||||
let total_len = op.total_len();
|
||||
|
||||
if op.is_read() {
|
||||
let n = aligned_file
|
||||
.read_unaligned(offset, total_len, |data| op.write_bytes_at(0, data))
|
||||
.map_err(AsyncIoError::ReadVectored)?;
|
||||
Ok(n as i32)
|
||||
} else {
|
||||
let n = aligned_file
|
||||
.write_unaligned(offset, total_len, |data| op.read_bytes_at(0, data))
|
||||
.map_err(AsyncIoError::WriteVectored)?;
|
||||
Ok(n as i32)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod unit_tests {
|
||||
use std::fs::File;
|
||||
|
||||
@@ -1,53 +0,0 @@
|
||||
// Copyright 2026 The Cloud Hypervisor Authors. All rights reserved.
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
//! Sync/async I/O workers for raw images.
|
||||
//!
|
||||
//! Each backend implements the [`AsyncIo`](crate::async_io::AsyncIo)
|
||||
//! trait.
|
||||
|
||||
use crate::AlignedFile;
|
||||
use crate::async_io::{AsyncIoError, AsyncIoOperation, AsyncIoResult};
|
||||
|
||||
pub(crate) mod async_aio;
|
||||
#[cfg(feature = "io_uring")]
|
||||
pub(crate) mod async_uring;
|
||||
pub(crate) mod sync;
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests;
|
||||
|
||||
/// True when `op` satisfies `alignment` and can go straight to the kernel.
|
||||
pub(crate) fn operation_is_aligned(op: &AsyncIoOperation, alignment: u64) -> bool {
|
||||
if alignment == 0 {
|
||||
return true;
|
||||
}
|
||||
if !(op.offset() as u64).is_multiple_of(alignment) {
|
||||
return false;
|
||||
}
|
||||
op.iovecs().iter().all(|iov| {
|
||||
(iov.iov_base as u64).is_multiple_of(alignment)
|
||||
&& (iov.iov_len as u64).is_multiple_of(alignment)
|
||||
})
|
||||
}
|
||||
|
||||
/// Runs an unaligned O_DIRECT operation synchronously through `aligned_file`.
|
||||
pub(crate) fn run_unaligned_operation(
|
||||
aligned_file: &AlignedFile,
|
||||
op: &mut AsyncIoOperation,
|
||||
) -> AsyncIoResult<i32> {
|
||||
let offset = op.offset() as u64;
|
||||
let total_len = op.total_len();
|
||||
|
||||
if op.is_read() {
|
||||
let n = aligned_file
|
||||
.read_unaligned(offset, total_len, |data| op.write_bytes_at(0, data))
|
||||
.map_err(AsyncIoError::ReadVectored)?;
|
||||
Ok(n as i32)
|
||||
} else {
|
||||
let n = aligned_file
|
||||
.write_unaligned(offset, total_len, |data| op.read_bytes_at(0, data))
|
||||
.map_err(AsyncIoError::WriteVectored)?;
|
||||
Ok(n as i32)
|
||||
}
|
||||
}
|
||||
@@ -11,7 +11,7 @@ use vmm_sys_util::eventfd::EventFd;
|
||||
use crate::AlignedFile;
|
||||
use crate::async_io::{AsyncIo, AsyncIoCompletion, AsyncIoError, AsyncIoOperation, AsyncIoResult};
|
||||
use crate::error::BlockResult;
|
||||
use crate::formats::raw::worker::async_uring::RawAsync;
|
||||
use crate::formats::raw::engine_uring::RawAsync;
|
||||
|
||||
pub struct FixedVhdAsync {
|
||||
raw_file_async: RawAsync,
|
||||
|
||||
@@ -10,7 +10,7 @@ use vmm_sys_util::eventfd::EventFd;
|
||||
|
||||
use crate::AlignedFile;
|
||||
use crate::async_io::{AsyncIo, AsyncIoCompletion, AsyncIoError, AsyncIoOperation, AsyncIoResult};
|
||||
use crate::formats::raw::worker::sync::RawSync;
|
||||
use crate::formats::raw::engine_sync::RawSync;
|
||||
|
||||
pub struct FixedVhdSync {
|
||||
raw_file_sync: RawSync,
|
||||
|
||||
Reference in New Issue
Block a user