From 53092359b45fe3778833dc6b1297b7ed034a0ce6 Mon Sep 17 00:00:00 2001 From: Philipp Schuster Date: Fri, 12 Dec 2025 09:06:40 +0100 Subject: [PATCH] block: rename DiskFile::size() -> DiskFile::logical_size() This better reflects the actual usage. Signed-off-by: Philipp Schuster On-behalf-of: SAP philipp.schuster@sap.com --- block/src/async_io.rs | 7 ++++++- block/src/fixed_vhd.rs | 2 +- block/src/fixed_vhd_async.rs | 12 ++++++++---- block/src/fixed_vhd_sync.rs | 6 +++--- block/src/lib.rs | 13 +++++++++---- block/src/qcow/mod.rs | 2 +- block/src/qcow/raw_file.rs | 2 +- block/src/qcow_sync.rs | 4 ++-- block/src/raw_async.rs | 2 +- block/src/raw_async_aio.rs | 2 +- block/src/raw_sync.rs | 2 +- block/src/vhdx/mod.rs | 2 +- block/src/vhdx_sync.rs | 4 ++-- virtio-devices/src/block.rs | 4 ++-- 14 files changed, 39 insertions(+), 25 deletions(-) diff --git a/block/src/async_io.rs b/block/src/async_io.rs index e4d8aaa25..fb60fde71 100644 --- a/block/src/async_io.rs +++ b/block/src/async_io.rs @@ -56,7 +56,12 @@ impl AsRawFd for BorrowedDiskFd<'_> { /// This allows abstracting over raw image formats as well as structured /// image formats. pub trait DiskFile: Send { - fn size(&mut self) -> DiskFileResult; + /// Returns the logical disk size a guest will see. + /// + /// For raw formats, this is equal to the physical size. For file formats + /// that wrap disk images in a container (e.g. QCOW2), this refers to the + /// effective size that the guest will see. + fn logical_size(&mut self) -> DiskFileResult; fn new_async_io(&self, ring_depth: u32) -> DiskFileResult>; fn topology(&mut self) -> DiskTopology { DiskTopology::default() diff --git a/block/src/fixed_vhd.rs b/block/src/fixed_vhd.rs index 379005ae2..35364906a 100644 --- a/block/src/fixed_vhd.rs +++ b/block/src/fixed_vhd.rs @@ -75,7 +75,7 @@ impl Seek for FixedVhd { } impl BlockBackend for FixedVhd { - fn size(&self) -> std::result::Result { + fn logical_size(&self) -> Result { Ok(self.size) } } diff --git a/block/src/fixed_vhd_async.rs b/block/src/fixed_vhd_async.rs index ac02e21bf..d56c1fafc 100644 --- a/block/src/fixed_vhd_async.rs +++ b/block/src/fixed_vhd_async.rs @@ -23,14 +23,18 @@ impl FixedVhdDiskAsync { } impl DiskFile for FixedVhdDiskAsync { - fn size(&mut self) -> DiskFileResult { - Ok(self.0.size().unwrap()) + fn logical_size(&mut self) -> DiskFileResult { + Ok(self.0.logical_size().unwrap()) } fn new_async_io(&self, ring_depth: u32) -> DiskFileResult> { Ok(Box::new( - FixedVhdAsync::new(self.0.as_raw_fd(), ring_depth, self.0.size().unwrap()) - .map_err(DiskFileError::NewAsyncIo)?, + FixedVhdAsync::new( + self.0.as_raw_fd(), + ring_depth, + self.0.logical_size().unwrap(), + ) + .map_err(DiskFileError::NewAsyncIo)?, ) as Box) } diff --git a/block/src/fixed_vhd_sync.rs b/block/src/fixed_vhd_sync.rs index c12571069..07702c56e 100644 --- a/block/src/fixed_vhd_sync.rs +++ b/block/src/fixed_vhd_sync.rs @@ -23,13 +23,13 @@ impl FixedVhdDiskSync { } impl DiskFile for FixedVhdDiskSync { - fn size(&mut self) -> DiskFileResult { - Ok(self.0.size().unwrap()) + fn logical_size(&mut self) -> DiskFileResult { + Ok(self.0.logical_size().unwrap()) } fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult> { Ok(Box::new( - FixedVhdSync::new(self.0.as_raw_fd(), self.0.size().unwrap()) + FixedVhdSync::new(self.0.as_raw_fd(), self.0.logical_size().unwrap()) .map_err(DiskFileError::NewAsyncIo)?, ) as Box) } diff --git a/block/src/lib.rs b/block/src/lib.rs index 212eab670..756163afe 100644 --- a/block/src/lib.rs +++ b/block/src/lib.rs @@ -80,8 +80,8 @@ pub enum Error { DetectImageType(#[source] std::io::Error), #[error("Failure in fixed vhd")] FixedVhdError(#[source] std::io::Error), - #[error("Getting a block's metadata fails for any reason")] - GetFileMetadata, + #[error("Getting a block's metadata failed")] + GetFileMetadata(#[source] std::io::Error), #[error("The requested operation would cause a seek beyond disk end")] InvalidOffset, #[error("Failure in qcow")] @@ -96,7 +96,7 @@ pub enum Error { fn build_device_id(disk_path: &Path) -> result::Result { let blk_metadata = match disk_path.metadata() { - Err(_) => return Err(Error::GetFileMetadata), + Err(e) => return Err(Error::GetFileMetadata(e)), Ok(m) => m, }; // This is how kvmtool does it. @@ -834,7 +834,12 @@ pub fn detect_image_type(f: &mut File) -> std::io::Result { } pub trait BlockBackend: Read + Write + Seek + Send + Debug { - fn size(&self) -> Result; + /// Returns the logical disk size a guest will see. + /// + /// For raw formats, this is equal to the physical_size. For file formats + /// that wrap disk images in a container (e.g. QCOW2), this refers to the + /// effective size that the guest will see. + fn logical_size(&self) -> Result; } #[derive(Debug)] diff --git a/block/src/qcow/mod.rs b/block/src/qcow/mod.rs index 4600ec118..0293d97b1 100644 --- a/block/src/qcow/mod.rs +++ b/block/src/qcow/mod.rs @@ -1808,7 +1808,7 @@ impl SeekHole for QcowFile { } impl BlockBackend for QcowFile { - fn size(&self) -> std::result::Result { + fn logical_size(&self) -> std::result::Result { Ok(self.virtual_size()) } } diff --git a/block/src/qcow/raw_file.rs b/block/src/qcow/raw_file.rs index 232871472..060fcaa2e 100644 --- a/block/src/qcow/raw_file.rs +++ b/block/src/qcow/raw_file.rs @@ -354,7 +354,7 @@ impl SeekHole for RawFile { } impl BlockBackend for RawFile { - fn size(&self) -> std::result::Result { + fn logical_size(&self) -> std::result::Result { Ok(self.metadata().map_err(crate::Error::RawFileError)?.len()) } } diff --git a/block/src/qcow_sync.rs b/block/src/qcow_sync.rs index cd6a1fb77..241bb5216 100644 --- a/block/src/qcow_sync.rs +++ b/block/src/qcow_sync.rs @@ -9,11 +9,11 @@ use std::os::fd::AsRawFd; use vmm_sys_util::eventfd::EventFd; -use crate::AsyncAdaptor; use crate::async_io::{ AsyncIo, AsyncIoResult, BorrowedDiskFd, DiskFile, DiskFileError, DiskFileResult, }; use crate::qcow::{QcowFile, RawFile, Result as QcowResult}; +use crate::{AsyncAdaptor, BlockBackend}; pub struct QcowDiskSync { qcow_file: QcowFile, @@ -28,7 +28,7 @@ impl QcowDiskSync { } impl DiskFile for QcowDiskSync { - fn size(&mut self) -> DiskFileResult { + fn logical_size(&mut self) -> DiskFileResult { self.qcow_file .seek(SeekFrom::End(0)) .map_err(DiskFileError::Size) diff --git a/block/src/raw_async.rs b/block/src/raw_async.rs index 0018b5029..30ff85384 100644 --- a/block/src/raw_async.rs +++ b/block/src/raw_async.rs @@ -26,7 +26,7 @@ impl RawFileDisk { } impl DiskFile for RawFileDisk { - fn size(&mut self) -> DiskFileResult { + fn logical_size(&mut self) -> DiskFileResult { self.file .seek(SeekFrom::End(0)) .map_err(DiskFileError::Size) diff --git a/block/src/raw_async_aio.rs b/block/src/raw_async_aio.rs index f2f070b43..1669bb51b 100644 --- a/block/src/raw_async_aio.rs +++ b/block/src/raw_async_aio.rs @@ -29,7 +29,7 @@ impl RawFileDiskAio { } impl DiskFile for RawFileDiskAio { - fn size(&mut self) -> DiskFileResult { + fn logical_size(&mut self) -> DiskFileResult { self.file .seek(SeekFrom::End(0)) .map_err(DiskFileError::Size) diff --git a/block/src/raw_sync.rs b/block/src/raw_sync.rs index b17018f72..b7d652a56 100644 --- a/block/src/raw_sync.rs +++ b/block/src/raw_sync.rs @@ -26,7 +26,7 @@ impl RawFileDiskSync { } impl DiskFile for RawFileDiskSync { - fn size(&mut self) -> DiskFileResult { + fn logical_size(&mut self) -> DiskFileResult { self.file .seek(SeekFrom::End(0)) .map_err(DiskFileError::Size) diff --git a/block/src/vhdx/mod.rs b/block/src/vhdx/mod.rs index 141c43c6d..5bcb198f2 100644 --- a/block/src/vhdx/mod.rs +++ b/block/src/vhdx/mod.rs @@ -202,7 +202,7 @@ impl Seek for Vhdx { } impl BlockBackend for Vhdx { - fn size(&self) -> std::result::Result { + fn logical_size(&self) -> std::result::Result { Ok(self.virtual_disk_size()) } } diff --git a/block/src/vhdx_sync.rs b/block/src/vhdx_sync.rs index 01bcbf5e7..cba2ad054 100644 --- a/block/src/vhdx_sync.rs +++ b/block/src/vhdx_sync.rs @@ -8,11 +8,11 @@ use std::os::fd::AsRawFd; use vmm_sys_util::eventfd::EventFd; -use crate::AsyncAdaptor; use crate::async_io::{ AsyncIo, AsyncIoResult, BorrowedDiskFd, DiskFile, DiskFileError, DiskFileResult, }; use crate::vhdx::{Result as VhdxResult, Vhdx}; +use crate::{AsyncAdaptor, BlockBackend, Error}; pub struct VhdxDiskSync { vhdx_file: Vhdx, @@ -27,7 +27,7 @@ impl VhdxDiskSync { } impl DiskFile for VhdxDiskSync { - fn size(&mut self) -> DiskFileResult { + fn logical_size(&mut self) -> DiskFileResult { Ok(self.vhdx_file.virtual_disk_size()) } diff --git a/virtio-devices/src/block.rs b/virtio-devices/src/block.rs index c358cdf74..c7fc934c4 100644 --- a/virtio-devices/src/block.rs +++ b/virtio-devices/src/block.rs @@ -675,7 +675,7 @@ impl Block { ) } else { let disk_size = disk_image - .size() + .logical_size() .map_err(|e| io::Error::other(format!("Failed getting disk size: {e}")))?; if disk_size % SECTOR_SIZE != 0 { warn!( @@ -773,7 +773,7 @@ impl Block { // TODO In future, we could add a `lock_granularity=` configuration to the CLI. // For now, we stick to QEMU behavior. fn lock_granularity(&mut self) -> LockGranularity { - self.disk_image.size().map_or_else( + self.disk_image.logical_size().map_or_else( // use a safe fallback |e| { let fallback = LockGranularity::WholeFile;