block: rename DiskFile::size() -> DiskFile::logical_size()

This better reflects the actual usage.

Signed-off-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
On-behalf-of: SAP philipp.schuster@sap.com
This commit is contained in:
Philipp Schuster
2025-12-12 09:06:40 +01:00
committed by Rob Bradford
parent 0ddb032cab
commit 53092359b4
14 changed files with 39 additions and 25 deletions

View File

@@ -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<u64>;
/// 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<u64>;
fn new_async_io(&self, ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>>;
fn topology(&mut self) -> DiskTopology {
DiskTopology::default()

View File

@@ -75,7 +75,7 @@ impl Seek for FixedVhd {
}
impl BlockBackend for FixedVhd {
fn size(&self) -> std::result::Result<u64, crate::Error> {
fn logical_size(&self) -> Result<u64, crate::Error> {
Ok(self.size)
}
}

View File

@@ -23,14 +23,18 @@ impl FixedVhdDiskAsync {
}
impl DiskFile for FixedVhdDiskAsync {
fn size(&mut self) -> DiskFileResult<u64> {
Ok(self.0.size().unwrap())
fn logical_size(&mut self) -> DiskFileResult<u64> {
Ok(self.0.logical_size().unwrap())
}
fn new_async_io(&self, ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> {
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<dyn AsyncIo>)
}

View File

@@ -23,13 +23,13 @@ impl FixedVhdDiskSync {
}
impl DiskFile for FixedVhdDiskSync {
fn size(&mut self) -> DiskFileResult<u64> {
Ok(self.0.size().unwrap())
fn logical_size(&mut self) -> DiskFileResult<u64> {
Ok(self.0.logical_size().unwrap())
}
fn new_async_io(&self, _ring_depth: u32) -> DiskFileResult<Box<dyn AsyncIo>> {
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<dyn AsyncIo>)
}

View File

@@ -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<String, Error> {
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<ImageType> {
}
pub trait BlockBackend: Read + Write + Seek + Send + Debug {
fn size(&self) -> Result<u64, Error>;
/// 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<u64, Error>;
}
#[derive(Debug)]

View File

@@ -1808,7 +1808,7 @@ impl SeekHole for QcowFile {
}
impl BlockBackend for QcowFile {
fn size(&self) -> std::result::Result<u64, crate::Error> {
fn logical_size(&self) -> std::result::Result<u64, crate::Error> {
Ok(self.virtual_size())
}
}

View File

@@ -354,7 +354,7 @@ impl SeekHole for RawFile {
}
impl BlockBackend for RawFile {
fn size(&self) -> std::result::Result<u64, crate::Error> {
fn logical_size(&self) -> std::result::Result<u64, crate::Error> {
Ok(self.metadata().map_err(crate::Error::RawFileError)?.len())
}
}

View File

@@ -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<u64> {
fn logical_size(&mut self) -> DiskFileResult<u64> {
self.qcow_file
.seek(SeekFrom::End(0))
.map_err(DiskFileError::Size)

View File

@@ -26,7 +26,7 @@ impl RawFileDisk {
}
impl DiskFile for RawFileDisk {
fn size(&mut self) -> DiskFileResult<u64> {
fn logical_size(&mut self) -> DiskFileResult<u64> {
self.file
.seek(SeekFrom::End(0))
.map_err(DiskFileError::Size)

View File

@@ -29,7 +29,7 @@ impl RawFileDiskAio {
}
impl DiskFile for RawFileDiskAio {
fn size(&mut self) -> DiskFileResult<u64> {
fn logical_size(&mut self) -> DiskFileResult<u64> {
self.file
.seek(SeekFrom::End(0))
.map_err(DiskFileError::Size)

View File

@@ -26,7 +26,7 @@ impl RawFileDiskSync {
}
impl DiskFile for RawFileDiskSync {
fn size(&mut self) -> DiskFileResult<u64> {
fn logical_size(&mut self) -> DiskFileResult<u64> {
self.file
.seek(SeekFrom::End(0))
.map_err(DiskFileError::Size)

View File

@@ -202,7 +202,7 @@ impl Seek for Vhdx {
}
impl BlockBackend for Vhdx {
fn size(&self) -> std::result::Result<u64, crate::Error> {
fn logical_size(&self) -> std::result::Result<u64, crate::Error> {
Ok(self.virtual_disk_size())
}
}

View File

@@ -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<u64> {
fn logical_size(&mut self) -> DiskFileResult<u64> {
Ok(self.vhdx_file.virtual_disk_size())
}

View File

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