Commit Graph

448 Commits

Author SHA1 Message Date
Anatol Belski
15073edf08 block: vhd: Switch FixedVhdDiskAsync::new to BlockResult
Map FixedVhd::new io::Error to BlockError with ErrorOp::Open.
Update vmm CreateFixedVhdDiskAsync source type accordingly.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-30 22:16:04 +00:00
Muminul Islam
38eb10d209 block: Remove legacy DiskFile impl from RawFileDiskSync
Remove the legacy async_io::DiskFile implementation from
RawFileDiskSync now that the new disk_file trait impls are
in place.

Remove unused imports: Seek, SeekFrom, DiskFile, and
DiskFileResult.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:15:31 +00:00
Muminul Islam
662d350cf9 block: Implement AsyncDiskFile trait for RawFileDiskSync
Add disk_file::AsyncDiskFile trait implementation for
RawFileDiskSync with try_clone() and new_async_io() methods.

try_clone() duplicates the underlying file descriptor and
wraps it in a new RawFileDiskSync. new_async_io() creates a
RawFileSync (synchronous fallback) backend, wrapping errors
in BlockError instead of DiskFileError.

Add DiskFileError::Clone variant in async_io.rs for the
try_clone() error path.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:15:31 +00:00
Muminul Islam
da72a3abfb block: Implement DiskFile marker trait for RawFileDiskSync
Add empty disk_file::DiskFile impl for RawFileDiskSync.
This marker supertrait requires DiskSize + Geometry + Sync,
all of which are now satisfied.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:15:31 +00:00
Muminul Islam
6b14d27f30 block: Implement Resizable trait for RawFileDiskSync
Add disk_file::Resizable trait implementation for
RawFileDiskSync. Calls file.set_len(size) and wraps the
I/O error in BlockError on failure.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:15:31 +00:00
Muminul Islam
fe39929a2a block: Implement SparseCapable trait for RawFileDiskSync
Add disk_file::SparseCapable trait implementation for
RawFileDiskSync. Delegates to probe_sparse_support() to
detect whether the underlying file supports hole-punching.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:15:31 +00:00
Muminul Islam
aa5aa6fe89 block: Implement Geometry trait for RawFileDiskSync
Add disk_file::Geometry trait implementation for
RawFileDiskSync. Probes disk topology from the file,
falling back to defaults on failure. Takes &self instead
of &mut self and uses unwrap_or_else for cleaner error
handling.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:15:31 +00:00
Muminul Islam
bd026ce4cb block: Implement DiskFd trait for RawFileDiskSync
Add disk_file::DiskFd trait implementation for
RawFileDiskSync. Delegates to file.as_raw_fd() via
BorrowedDiskFd, taking &self instead of &mut self.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:15:31 +00:00
Muminul Islam
744c365faa block: Implement PhysicalSize trait for RawFileDiskSync
Add disk_file::PhysicalSize trait implementation for
RawFileDiskSync. Returns metadata().len() wrapped in
BlockError on failure, consistent with the DiskSize impl.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:15:31 +00:00
Muminul Islam
00a355d273 block: Implement DiskSize trait for RawFileDiskSync
Add disk_file::DiskSize trait implementation for
RawFileDiskSync using BlockError and BlockResult. Uses
metadata().len() instead of seek(SeekFrom::End(0)), taking
&self instead of &mut self.

Add BlockError, BlockErrorKind, BlockResult, and disk_file
imports needed by this and subsequent trait impls.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:15:31 +00:00
Muminul Islam
98fedac77f block: Derive Debug on RawFileDiskSync
Add #[derive(Debug)] to RawFileDiskSync. This is required
by the new disk_file traits which have Send + Debug bounds.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:15:31 +00:00
Muminul Islam
573f3af77d vmm: Switch RawFileDisk to DiskBackend::Next
Update device_manager.rs to construct DiskBackend::Next
instead of DiskBackend::Legacy for the io_uring raw backend.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Muminul Islam
491c5493c5 block: Implement AsyncDiskFile trait for RawFileDisk
Add disk_file::AsyncDiskFile trait implementation for
RawFileDisk with try_clone() and new_async_io() methods.

try_clone() duplicates the underlying file descriptor and
wraps it in a new RawFileDisk. new_async_io() creates a
RawFileAsync (io_uring) backend, wrapping errors in
BlockError instead of DiskFileError.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Muminul Islam
607a2c769d block: Implement DiskFile marker trait for RawFileDisk
Add empty disk_file::DiskFile impl for RawFileDisk. This
marker supertrait requires DiskSize + Geometry + Sync, all
of which are now satisfied.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Muminul Islam
64b65caddd block: Implement Resizable trait for RawFileDisk
Add disk_file::Resizable trait implementation for
RawFileDisk. Calls file.set_len(size) and wraps the I/O
error in BlockError on failure.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Muminul Islam
899dec4c04 block: Implement SparseCapable trait for RawFileDisk
Add disk_file::SparseCapable trait implementation for
RawFileDisk. Delegates to probe_sparse_support() to detect
whether the underlying file supports hole-punching.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Muminul Islam
7e514d8d0e block: Implement Geometry trait for RawFileDisk
Add disk_file::Geometry trait implementation for RawFileDisk.
Probes disk topology from the file, falling back to defaults
on failure. Takes &self instead of &mut self and uses
unwrap_or_else for cleaner error handling.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Muminul Islam
ca1f48a717 block: Implement DiskFd trait for RawFileDisk
Add disk_file::DiskFd trait implementation for RawFileDisk.
Delegates to file.as_raw_fd() via BorrowedDiskFd, taking
&self instead of &mut self.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Muminul Islam
aa6a7aea0e block: Implement PhysicalSize trait for RawFileDisk
Add disk_file::PhysicalSize trait implementation for
RawFileDisk. Returns metadata().len() wrapped in BlockError
on failure, consistent with the DiskSize impl.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Muminul Islam
0647202215 block: Implement DiskSize trait for RawFileDisk
Add disk_file::DiskSize trait implementation for RawFileDisk
using BlockError and BlockResult. Uses metadata().len()
instead of seek(SeekFrom::End(0)), taking &self instead of
&mut self.

Add BlockError, BlockErrorKind, BlockResult, and disk_file
imports needed by this and subsequent trait impls.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Muminul Islam
4586ca133a block: Derive Debug on RawFileDisk
Add #[derive(Debug)] to RawFileDisk. This is required by the
new disk_file traits which have Send + Debug bounds.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Muminul Islam
6261cad0d8 block: Add DiskFileError::Clone variant
Add a Clone variant to DiskFileError for error handling in
the upcoming AsyncDiskFile::try_clone() implementations.

This variant will be used by RawFileDisk and RawFileDiskSync
when cloning the underlying file descriptor fails.

Signed-off-by: Muminul Islam <muislam@microsoft.com>
2026-03-30 22:14:42 +00:00
Anatol Belski
c21d6bb856 block: Add unit tests for query_device_size()
Test regular file with odd size (not page/sector aligned), sparse
file with punch hole verifying physical < logical, and char device
rejection, etc.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-27 11:05:07 +00:00
Anatol Belski
8b6eb83f2a block: qcow: raw_file: Use query_device_size() for size queries
Use query_device_size() instead of metadata().len() to correctly
handle block device and regular file handles.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-27 11:05:07 +00:00
Anatol Belski
1b92af5534 block: raw_async_aio: Use query_device_size() for size queries
Use query_device_size() instead of seek(End(0)) and metadata().len()
to correctly handle block device and regular file handles.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-27 11:05:07 +00:00
Anatol Belski
290f57a6e3 block: raw_sync: Use query_device_size() for size queries
Use query_device_size() instead of seek(End(0)) and metadata().len()
to correctly handle block device and regular file handles.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-27 11:05:07 +00:00
Anatol Belski
f14e2d2b40 block: raw_async: Use query_device_size() for size queries
Use query_device_size() instead of seek(End(0)) and metadata().len()
to correctly handle block device and regular file handles.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-27 11:05:07 +00:00
Anatol Belski
051a6eff5c block: Add query_device_size() for file and block device size
Add a shared helper that returns the logical size in bytes for both
regular files and block devices using an immutable &File reference.

Regular files use metadata().len(). Block devices use the BLKGETSIZE64
ioctl. Any other file type returns an InvalidInput error.

This avoids seek(SeekFrom::End(0)) which requires &mut self and can
return incorrect results for block devices when the file position is
in an unexpected state.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-27 11:05:07 +00:00
Anatol Belski
92109136f1 block: Include actual segment count in TooManySegments error
Include the number of segments found in the request payload in
the TooManySegments error variant so the logged message shows
both the actual and maximum values.

Suggested-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-23 13:27:20 +00:00
Anatol Belski
edfbb7e180 block: Add DISCARD_WZ_MAX_PAYLOAD constant
Introduce DISCARD_WZ_MAX_PAYLOAD as the precomputed product of
DISCARD_WZ_SEG_SIZE and MAX_DISCARD_WRITE_ZEROES_SEG. Use it in the
DISCARD and WRITE_ZEROES segment count checks instead of repeating
the multiplication inline.

Suggested-by: Philipp Schuster <philipp.schuster@cyberus-technology.de>
Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-23 13:27:20 +00:00
Anatol Belski
c79f3acfab block: Validate sector range for DISCARD and WRITE_ZEROES requests
Add range validation for DISCARD and WRITE_ZEROES, matching the
existing check in the read/write path. Per virtio spec section
5.2.6.1, a driver must not submit a request which would cause a
read or write beyond capacity. Use checked_add to guard against
overflow, then compare against disk_nsectors.

Without this, requests beyond device capacity pass through to the
host punch_hole/write_zeroes calls, relying on backend specific
behavior rather than returning VIRTIO_BLK_S_IOERR consistently.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-22 19:12:52 +00:00
Anatol Belski
d3cad420a5 block: Validate segment count for DISCARD and WRITE_ZEROES requests
Split the data length check into two conditions:

- reject descriptors shorter than one
virtio_blk_discard_write_zeroes segment, and
- reject payloads exceeding MAX_DISCARD_WRITE_ZEROES_SEG segments

Previously only the minimum length was checked and extra segments
were silently dropped.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-22 17:55:24 +00:00
Anatol Belski
fc79d08d7d block: qcow: Remove From<qcow::Error> for BlockError
All public qcow functions now return BlockResult with explicit error
classification at every site. The temporary From impl introduced in
the first commit of this series is no longer needed and is removed.

Internal functions in header.rs and the rebuild_refcounts helpers
stay on qcow::Result. Classification happens at the call site
boundary where qcow::Result meets BlockResult.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-20 22:21:24 +00:00
Anatol Belski
5410d4b2d5 block: qcow: Switch detect_image_type to BlockResult
Switch detect_image_type from qcow::Result to BlockResult with
explicit error classification at every I/O site. This is the last
function migrated before the From scaffolding can be removed.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-20 22:21:24 +00:00
Anatol Belski
9daf1782a8 block: qcow: Switch rebuild_refcounts to BlockResult
Switch rebuild_refcounts from qcow::Result to BlockResult. The
inner helper functions remain on qcow::Result since they are purely
internal, and are wrapped with map_err at each call site where they
cross the BlockResult boundary.

InvalidRefcountTableSize errors are classified as CorruptImage since
they indicate inconsistent internal refcount structures rather than
a format violation.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-20 22:21:24 +00:00
Anatol Belski
f6ec817b9b block: qcow: Switch resize and grow_l1_table to BlockResult
Switch resize and grow_l1_table from qcow::Result to BlockResult.
All I/O error sites use explicit BlockError::new with the Io kind.
The write_to call in grow_l1_table rewraps WritingHeader as
ResizeIo to preserve the existing error semantics.

The two resize tests that check for ShrinkNotSupported and
ResizeWithBackingFile are updated to match on BlockErrorKind with
downcast to inspect the underlying variant.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-20 22:21:24 +00:00
Anatol Belski
524e620240 block: qcow: Classify errors in parse_qcow and BackingFile::new
Replace remaining automatic From conversions in parse_qcow and
BackingFile::new with explicit BlockError::new calls carrying the
appropriate BlockErrorKind at every error site.

Internal functions that still return qcow::Result (QcowHeader::new,
offset_is_cluster_boundary, clear_autoclear_features and others) are
wrapped with map_err at the boundary. These functions stay on
qcow::Result as they are internal to the qcow module and the
classification belongs at the call site rather than inside the
function itself.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-20 22:21:24 +00:00
Anatol Belski
be9ef116aa block: qcow: Switch parse_qcow and BackingFile::new to BlockResult
Switch parse_qcow and BackingFile::new from qcow::Result to
BlockResult. Every early return site now produces an explicit
BlockError with the appropriate kind. Remaining internal calls to
functions still on qcow::Result rely on the From scaffolding and
will be converted in subsequent commits.

Two helpers are added to BlockError. with_kind replaces the
classification on an existing error, used in QcowDiskSync::new to
avoid double wrapping when the caller needs a different kind.
into_source consumes the error and returns the boxed source, used
at the recursive BackingFile open to extract the qcow::Error for
BackingFileOpen without letting qcow::Error hold a BlockError.

The qcow_sync boundary is simplified to a single closure that
operates on the BlockError already returned by parse_qcow.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-20 22:21:24 +00:00
Anatol Belski
c4a5c7f843 block: qcow: Switch QcowFile constructors to BlockResult
Switch the public QcowFile constructors (new, new_from_backing,
new_from_header, from, from_backing, from_file_and_header) from
qcow::Result to BlockResult. Internal calls to header functions
that still return qcow::Result are wrapped with explicit error
classification at each call site.

Test assertions are updated to match on BlockErrorKind and use
downcast to inspect the underlying qcow::Error variant.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-20 22:21:24 +00:00
Anatol Belski
d77e3e7ca2 block: qcow: Add From<qcow::Error> for BlockError
Temporary From impl that classifies each qcow::Error variant into
the appropriate BlockErrorKind. This enables an incremental migration
of qcow functions from qcow::Result to BlockResult, where each
subsequent commit replaces bare ? sites with explicit BlockError::new
calls until this impl can be removed.

The mapping assigns InvalidFormat for structural header violations,
UnsupportedFeature for version and feature mismatches, CorruptImage
for internal inconsistencies, Overflow for nesting depth and Io for
everything else.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-20 22:21:24 +00:00
Anatol Belski
0d062962ac block: qcow: Remove async_io::DiskFile impl from QcowDiskSync
QcowDiskSync now exclusively uses disk_file::DiskFile and
disk_file::AsyncDiskFile. The old async_io::DiskFile impl is removed
along with its unused imports (DiskFile, DiskFileError, DiskFileResult).

Tests are updated to import the new traits.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-19 22:29:27 +00:00
Anatol Belski
264013b424 block: disk_file: Add DiskBackend dispatch enum
Introduce DiskBackend with two variants:
- Legacy: wraps Box<dyn async_io::DiskFile> for existing formats
- Next: wraps Box<dyn disk_file::AsyncFullDiskFile>

Methods return BlockResult, with DiskFileError converted up to
BlockError on the Legacy path. The Next path passes through
directly with zero conversion overhead.

This is a transitional type. Once all formats implement
AsyncFullDiskFile, DiskBackend and Legacy are removed and
callers hold Box<dyn AsyncFullDiskFile> directly.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-19 22:29:27 +00:00
Anatol Belski
fe6e3e8fef block: qcow_sync: impl AsyncDiskFile for QcowDiskSync
Implement try_clone by sharing the metadata Arc and cloning the data
file descriptor. The new_async_io method creates a QcowSync worker
identical to the async_io::DiskFile version.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-19 22:29:27 +00:00
Anatol Belski
1fc8e4adb6 block: qcow_sync: impl DiskFile for QcowDiskSync
Marker impl binding the DiskSize and HasTopology supertraits.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-19 22:29:27 +00:00
Anatol Belski
f35bec19e8 block: qcow_sync: impl Resizable for QcowDiskSync
Add ErrorOp::Resize variant and implement the Resizable trait.
Resize is rejected when a backing file is present.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-19 22:29:27 +00:00
Anatol Belski
554562fef2 block: qcow_sync: impl SparseCapable for QcowDiskSync
Advertise support for sparse operations and the zero flag. QCOW2
inherently supports both through cluster deallocation.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-19 22:29:27 +00:00
Anatol Belski
63f5e6e97f block: qcow_sync: impl Geometry for QcowDiskSync
Uses the default DiskTopology (512B logical/physical) since
QCOW2 does not probe the underlying device geometry.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-19 22:29:27 +00:00
Anatol Belski
b305a7670b block: qcow_sync: impl DiskFd for QcowDiskSync
Borrows the raw file descriptor from the underlying QcowRawFile
for fcntl() operations. Uses &self for shared access.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-19 22:29:27 +00:00
Anatol Belski
4b731ee771 block: qcow_sync: impl PhysicalSize for QcowDiskSync
Delegate to QcowRawFile::physical_size() which returns the actual
host allocation size of the QCOW2 container file.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-19 22:29:27 +00:00
Anatol Belski
0a7b6b089b block: qcow_sync: impl DiskSize for QcowDiskSync
Delegate to QcowMetadata::virtual_size() which returns the guest
visible capacity stored in the QCOW2 header.

Signed-off-by: Anatol Belski <anbelski@linux.microsoft.com>
2026-03-19 22:29:27 +00:00