From ecf72ba787392b47ce35113fa9f0b39d80fc2097 Mon Sep 17 00:00:00 2001 From: Anatol Belski Date: Tue, 7 Jul 2026 20:03:00 +0200 Subject: [PATCH] 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 --- .../{worker/async_aio.rs => engine_aio.rs} | 6 +-- .../raw/{worker/sync.rs => engine_sync.rs} | 6 +-- .../async_uring.rs => engine_uring.rs} | 4 +- block/src/formats/raw/mod.rs | 52 ++++++++++++++++-- block/src/formats/raw/{worker => }/tests.rs | 0 block/src/formats/raw/worker/mod.rs | 53 ------------------- block/src/formats/vhd/worker/async_uring.rs | 2 +- block/src/formats/vhd/worker/sync.rs | 2 +- 8 files changed, 57 insertions(+), 68 deletions(-) rename block/src/formats/raw/{worker/async_aio.rs => engine_aio.rs} (96%) rename block/src/formats/raw/{worker/sync.rs => engine_sync.rs} (97%) rename block/src/formats/raw/{worker/async_uring.rs => engine_uring.rs} (97%) rename block/src/formats/raw/{worker => }/tests.rs (100%) delete mode 100644 block/src/formats/raw/worker/mod.rs diff --git a/block/src/formats/raw/worker/async_aio.rs b/block/src/formats/raw/engine_aio.rs similarity index 96% rename from block/src/formats/raw/worker/async_aio.rs rename to block/src/formats/raw/engine_aio.rs index ff12125d6..ba7385f27 100644 --- a/block/src/formats/raw/worker/async_aio.rs +++ b/block/src/formats/raw/engine_aio.rs @@ -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 { + pub(super) fn new(raw_file: AlignedFile, queue_depth: u32) -> BlockResult { 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() { diff --git a/block/src/formats/raw/worker/sync.rs b/block/src/formats/raw/engine_sync.rs similarity index 97% rename from block/src/formats/raw/worker/sync.rs rename to block/src/formats/raw/engine_sync.rs index 4abc82b80..44480813d 100644 --- a/block/src/formats/raw/worker/sync.rs +++ b/block/src/formats/raw/engine_sync.rs @@ -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, @@ -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() { diff --git a/block/src/formats/raw/worker/async_uring.rs b/block/src/formats/raw/engine_uring.rs similarity index 97% rename from block/src/formats/raw/worker/async_uring.rs rename to block/src/formats/raw/engine_uring.rs index 806e4bd44..68a58530f 100644 --- a/block/src/formats/raw/worker/async_uring.rs +++ b/block/src/formats/raw/engine_uring.rs @@ -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 { + pub(crate) fn new(raw_file: AlignedFile, ring_depth: u32) -> BlockResult { 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()); diff --git a/block/src/formats/raw/mod.rs b/block/src/formats/raw/mod.rs index c5801a04e..4729ce392 100644 --- a/block/src/formats/raw/mod.rs +++ b/block/src/formats/raw/mod.rs @@ -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 { + 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; diff --git a/block/src/formats/raw/worker/tests.rs b/block/src/formats/raw/tests.rs similarity index 100% rename from block/src/formats/raw/worker/tests.rs rename to block/src/formats/raw/tests.rs diff --git a/block/src/formats/raw/worker/mod.rs b/block/src/formats/raw/worker/mod.rs deleted file mode 100644 index 9d664e486..000000000 --- a/block/src/formats/raw/worker/mod.rs +++ /dev/null @@ -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 { - 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) - } -} diff --git a/block/src/formats/vhd/worker/async_uring.rs b/block/src/formats/vhd/worker/async_uring.rs index f09425bf7..c622dedd0 100644 --- a/block/src/formats/vhd/worker/async_uring.rs +++ b/block/src/formats/vhd/worker/async_uring.rs @@ -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, diff --git a/block/src/formats/vhd/worker/sync.rs b/block/src/formats/vhd/worker/sync.rs index 2291f9269..9cc55fd91 100644 --- a/block/src/formats/vhd/worker/sync.rs +++ b/block/src/formats/vhd/worker/sync.rs @@ -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,