mirror of
https://github.com/cloud-hypervisor/cloud-hypervisor.git
synced 2026-08-05 02:19:16 +00:00
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:
committed by
Rob Bradford
parent
73efde72b3
commit
4bb3e1ca04
@@ -12,7 +12,6 @@ use crate::AlignedFile;
|
|||||||
use crate::async_io::{AsyncIo, AsyncIoCompletion, AsyncIoError, AsyncIoOperation, AsyncIoResult};
|
use crate::async_io::{AsyncIo, AsyncIoCompletion, AsyncIoError, AsyncIoOperation, AsyncIoResult};
|
||||||
use crate::error::BlockResult;
|
use crate::error::BlockResult;
|
||||||
use crate::formats::raw::worker::async_uring::RawAsync;
|
use crate::formats::raw::worker::async_uring::RawAsync;
|
||||||
use crate::formats::vhd::worker::common::validate_operation_bounds;
|
|
||||||
|
|
||||||
pub struct FixedVhdAsync {
|
pub struct FixedVhdAsync {
|
||||||
raw_file_async: RawAsync,
|
raw_file_async: RawAsync,
|
||||||
@@ -36,7 +35,7 @@ impl AsyncIo for FixedVhdAsync {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn submit_data_operation(&mut self, op: AsyncIoOperation) -> AsyncIoResult<()> {
|
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)
|
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<()> {
|
fn submit_batch_requests(&mut self, batch_request: Vec<AsyncIoOperation>) -> AsyncIoResult<()> {
|
||||||
for op in &batch_request {
|
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)
|
self.raw_file_async.submit_batch_requests(batch_request)
|
||||||
|
|||||||
@@ -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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -9,5 +9,4 @@
|
|||||||
|
|
||||||
#[cfg(feature = "io_uring")]
|
#[cfg(feature = "io_uring")]
|
||||||
pub(crate) mod async_uring;
|
pub(crate) mod async_uring;
|
||||||
mod common;
|
|
||||||
pub(crate) mod sync;
|
pub(crate) mod sync;
|
||||||
|
|||||||
@@ -11,7 +11,6 @@ use vmm_sys_util::eventfd::EventFd;
|
|||||||
use crate::AlignedFile;
|
use crate::AlignedFile;
|
||||||
use crate::async_io::{AsyncIo, AsyncIoCompletion, AsyncIoError, AsyncIoOperation, AsyncIoResult};
|
use crate::async_io::{AsyncIo, AsyncIoCompletion, AsyncIoError, AsyncIoOperation, AsyncIoResult};
|
||||||
use crate::formats::raw::worker::sync::RawSync;
|
use crate::formats::raw::worker::sync::RawSync;
|
||||||
use crate::formats::vhd::worker::common::validate_operation_bounds;
|
|
||||||
|
|
||||||
pub struct FixedVhdSync {
|
pub struct FixedVhdSync {
|
||||||
raw_file_sync: RawSync,
|
raw_file_sync: RawSync,
|
||||||
@@ -33,7 +32,7 @@ impl AsyncIo for FixedVhdSync {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn submit_data_operation(&mut self, op: AsyncIoOperation) -> AsyncIoResult<()> {
|
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)
|
self.raw_file_sync.submit_data_operation(op)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -133,7 +133,6 @@ impl AsyncIoOperation {
|
|||||||
/// Returns the read/write-specific `AsyncIoError` variant, carrying an
|
/// Returns the read/write-specific `AsyncIoError` variant, carrying an
|
||||||
/// `InvalidData` error, when the offset overflows or `offset + len`
|
/// `InvalidData` error, when the offset overflows or `offset + len`
|
||||||
/// exceeds `size`.
|
/// exceeds `size`.
|
||||||
#[allow(dead_code)] // used starting with the commit that switches vhd over
|
|
||||||
pub(crate) fn validate_bounds(&self, size: u64) -> AsyncIoResult<()> {
|
pub(crate) fn validate_bounds(&self, size: u64) -> AsyncIoResult<()> {
|
||||||
let bounds_error = || {
|
let bounds_error = || {
|
||||||
let error = io::Error::new(
|
let error = io::Error::new(
|
||||||
|
|||||||
Reference in New Issue
Block a user