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 <alexander.lvov.git@gmail.com>
This commit is contained in:
Alexander Lvov
2026-07-06 11:54:00 +03:00
committed by Rob Bradford
parent 73efde72b3
commit 4bb3e1ca04
5 changed files with 3 additions and 45 deletions

View File

@@ -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<AsyncIoOperation>) -> 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)

View File

@@ -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)
}
}

View File

@@ -9,5 +9,4 @@
#[cfg(feature = "io_uring")]
pub(crate) mod async_uring;
mod common;
pub(crate) mod sync;

View File

@@ -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)
}

View File

@@ -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(