Thanks to visit codestin.com
Credit goes to github.com

Skip to content

block: qcow: Support variable refcount widths - #7633

Merged
rbradford merged 3 commits into
cloud-hypervisor:mainfrom
weltling:qcow-refcount-order
Jan 25, 2026
Merged

rbradford merged 3 commits into
cloud-hypervisor:mainfrom
weltling:qcow-refcount-order

Conversation

@weltling

Copy link
Copy Markdown
Member

Add support for variable QCOW2 refcount widths.

Support all QCOW2 v3 refcount_order values (0-6), enabling 1 to 64-bit refcounts. Previously only 16-bit was supported, causing UnsupportedRefcountOrder errors for images created with qemu-img -o refcount_bits=N.

Additionally, implement overflow protection and comprehensive unit tests.

@weltling
weltling requested a review from a team as a code owner January 24, 2026 09:18
@weltling
weltling force-pushed the qcow-refcount-order branch from 19cf38b to bbbe287 Compare January 24, 2026 10:12
Comment thread block/src/qcow/qcow_raw_file.rs Outdated
type RefcountWriter = fn(&mut RawFile, &[u64]) -> io::Result<()>;

/// Trait for byte-aligned refcount types (8, 16, 32, 64 bits).
trait RefcountBytes: Sized + Copy {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Isn't this just a generic BE file access trait - maybe it could have a more generic name?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I renamed it to BeUint and removed the _be affix from the methods. Thanks

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Filed a followup in #7637. 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]>
@weltling
weltling force-pushed the qcow-refcount-order branch from bbbe287 to 8a60d45 Compare January 24, 2026 13:11

@rbradford rbradford left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still lgtm (although I didn't dive into deep QCOW2 detail)

@rbradford
rbradford added this pull request to the merge queue Jan 25, 2026
Merged via the queue into cloud-hypervisor:main with commit a6aecad Jan 25, 2026
43 checks passed
@weltling

Copy link
Copy Markdown
Member Author

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 :)

@weltling
weltling deleted the qcow-refcount-order branch January 25, 2026 11:55
@github-project-automation github-project-automation Bot moved this from 🆕 New to ✅ Done in Cloud Hypervisor Roadmap Feb 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: ✅ Done

Development

Successfully merging this pull request may close these issues.

3 participants