block: qcow: Support variable refcount widths - #7633
Conversation
19cf38b to
bbbe287
Compare
| type RefcountWriter = fn(&mut RawFile, &[u64]) -> io::Result<()>; | ||
|
|
||
| /// Trait for byte-aligned refcount types (8, 16, 32, 64 bits). | ||
| trait RefcountBytes: Sized + Copy { |
There was a problem hiding this comment.
nit: Isn't this just a generic BE file access trait - maybe it could have a more generic name?
There was a problem hiding this comment.
Good point! I renamed it to BeUint and removed the _be affix from the methods. Thanks
There was a problem hiding this comment.
Maybe something to address in a follow-up PR - but there is also https://github.com/cloud-hypervisor/cloud-hypervisor/blob/main/block/src/qcow/mod.rs#L434 which are BE file read functions.
There was a problem hiding this comment.
Yeah, that's a good call on the BE I/O. The place you pointed to appears to be CrosVM inheritance. There's definitely a consolidation opportunity there. Will open a follow-up for that. Thanks
QCOW2 v3 specifies refcount_order 0-6 with refcount_bits = 1 << refcount_order. Previously only 16-bit (order 4) was supported. Changes: - RefcountBytes trait handles byte-aligned types (8/16/32/64-bit) - Generic pack/unpack for sub-byte widths (1/2/4-bit) - Function pointers for read/write selected at open time - Internal refcount type widened from u16 to u64 Signed-off-by: Anatol Belski <[email protected]>
Reject refcount values exceeding the maximum for the image's refcount_order. This prevents silent truncation when storing refcounts in narrow widths (e.g., 1-bit max is 1, 4-bit max is 15, etc.). Returns RefcountOverflow error with the attempted value, maximum, and bit width. Propagates as EINVAL to the guest. Signed-off-by: Anatol Belski <[email protected]>
Test all refcount_order values (0-6): - Basic open for each width - Write/read roundtrip - Overwrite and multi-cluster allocation - L2 cache eviction under memory pressure - Sub-byte and byte-aligned max value handling - Overflow error detection Signed-off-by: Anatol Belski <[email protected]>
bbbe287 to
8a60d45
Compare
rbradford
left a comment
There was a problem hiding this comment.
Still lgtm (although I didn't dive into deep QCOW2 detail)
|
Thanks for the review and quick merge, @rbradford! With the QCOW2 changes - I'm currently walking down the QCOW2 v3 spec and picking up missing pieces. This round focused on safety and compatibility as a foundation for future performance optimizations and refactoring. Next station is the dirty bit support in the incompatible features :) |
Add support for variable QCOW2 refcount widths.
Support all QCOW2 v3
refcount_ordervalues (0-6), enabling 1 to 64-bit refcounts. Previously only 16-bit was supported, causingUnsupportedRefcountOrdererrors for images created withqemu-img -o refcount_bits=N.Additionally, implement overflow protection and comprehensive unit tests.