From 4bb3e1ca04cc1d14f59992c965738a75d2fa2d5a Mon Sep 17 00:00:00 2001 From: Alexander Lvov Date: Mon, 6 Jul 2026 11:54:00 +0300 Subject: [PATCH] block: vhd: switch to AsyncIoOperation::validate_bounds Reuse global validate_bounds() operation helper instead of having a local implementation in vhd/worker/common.rs Signed-off-by: Alexander Lvov --- block/src/formats/vhd/worker/async_uring.rs | 5 ++- block/src/formats/vhd/worker/common.rs | 38 --------------------- block/src/formats/vhd/worker/mod.rs | 1 - block/src/formats/vhd/worker/sync.rs | 3 +- block/src/io/async_io/operation.rs | 1 - 5 files changed, 3 insertions(+), 45 deletions(-) delete mode 100644 block/src/formats/vhd/worker/common.rs diff --git a/block/src/formats/vhd/worker/async_uring.rs b/block/src/formats/vhd/worker/async_uring.rs index fd4879fd9..f09425bf7 100644 --- a/block/src/formats/vhd/worker/async_uring.rs +++ b/block/src/formats/vhd/worker/async_uring.rs @@ -12,7 +12,6 @@ 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::vhd::worker::common::validate_operation_bounds; pub struct FixedVhdAsync { raw_file_async: RawAsync, @@ -36,7 +35,7 @@ impl AsyncIo for FixedVhdAsync { } fn submit_data_operation(&mut self, op: AsyncIoOperation) -> AsyncIoResult<()> { - validate_operation_bounds(&op, self.size)?; + op.validate_bounds(self.size)?; self.raw_file_async.submit_data_operation(op) } @@ -66,7 +65,7 @@ impl AsyncIo for FixedVhdAsync { fn submit_batch_requests(&mut self, batch_request: Vec) -> AsyncIoResult<()> { for op in &batch_request { - validate_operation_bounds(op, self.size)?; + op.validate_bounds(self.size)?; } self.raw_file_async.submit_batch_requests(batch_request) diff --git a/block/src/formats/vhd/worker/common.rs b/block/src/formats/vhd/worker/common.rs deleted file mode 100644 index 4562c7577..000000000 --- a/block/src/formats/vhd/worker/common.rs +++ /dev/null @@ -1,38 +0,0 @@ -// Copyright 2026 The Cloud Hypervisor Authors. All rights reserved. -// -// SPDX-License-Identifier: Apache-2.0 - -use std::io; - -use crate::async_io::{AsyncIoError, AsyncIoOperation, AsyncIoResult}; - -pub(super) fn validate_operation_bounds(op: &AsyncIoOperation, size: u64) -> AsyncIoResult<()> { - let offset = u64::try_from(op.offset()).map_err(|_| bounds_error(op, size))?; - let len = u64::try_from(op.total_len()).map_err(|_| bounds_error(op, size))?; - let end = offset - .checked_add(len) - .ok_or_else(|| bounds_error(op, size))?; - - if end > size { - return Err(bounds_error(op, size)); - } - - Ok(()) -} - -fn bounds_error(op: &AsyncIoOperation, size: u64) -> AsyncIoError { - let error = io::Error::new( - io::ErrorKind::InvalidData, - format!( - "Invalid request offset {} and length {}, can't exceed file size {}", - op.offset(), - op.total_len(), - size - ), - ); - if op.is_read() { - AsyncIoError::ReadVectored(error) - } else { - AsyncIoError::WriteVectored(error) - } -} diff --git a/block/src/formats/vhd/worker/mod.rs b/block/src/formats/vhd/worker/mod.rs index f0a3eaf41..39769731c 100644 --- a/block/src/formats/vhd/worker/mod.rs +++ b/block/src/formats/vhd/worker/mod.rs @@ -9,5 +9,4 @@ #[cfg(feature = "io_uring")] pub(crate) mod async_uring; -mod common; pub(crate) mod sync; diff --git a/block/src/formats/vhd/worker/sync.rs b/block/src/formats/vhd/worker/sync.rs index 3517239f9..2291f9269 100644 --- a/block/src/formats/vhd/worker/sync.rs +++ b/block/src/formats/vhd/worker/sync.rs @@ -11,7 +11,6 @@ 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::vhd::worker::common::validate_operation_bounds; pub struct FixedVhdSync { raw_file_sync: RawSync, @@ -33,7 +32,7 @@ impl AsyncIo for FixedVhdSync { } fn submit_data_operation(&mut self, op: AsyncIoOperation) -> AsyncIoResult<()> { - validate_operation_bounds(&op, self.size)?; + op.validate_bounds(self.size)?; self.raw_file_sync.submit_data_operation(op) } diff --git a/block/src/io/async_io/operation.rs b/block/src/io/async_io/operation.rs index 49ed0d3dc..e53f2d66a 100644 --- a/block/src/io/async_io/operation.rs +++ b/block/src/io/async_io/operation.rs @@ -133,7 +133,6 @@ impl AsyncIoOperation { /// Returns the read/write-specific `AsyncIoError` variant, carrying an /// `InvalidData` error, when the offset overflows or `offset + len` /// exceeds `size`. - #[allow(dead_code)] // used starting with the commit that switches vhd over pub(crate) fn validate_bounds(&self, size: u64) -> AsyncIoResult<()> { let bounds_error = || { let error = io::Error::new(