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

Skip to content

block: Query actual DIO alignment for file-backed images - #7767

Merged
rbradford merged 5 commits into
cloud-hypervisor:mainfrom
weltling:block-file-dio-align
Mar 2, 2026
Merged

rbradford merged 5 commits into
cloud-hypervisor:mainfrom
weltling:block-file-dio-align

Conversation

@weltling

@weltling weltling commented Feb 27, 2026

Copy link
Copy Markdown
Member

Follow-up to #7735.

DiskTopology::probe() returned a hardcoded 512 for regular files, causing O_DIRECT failures on volumes with larger block sizes (e.g. 4K).

This PR queries the real alignment via statx(STATX_DIOALIGN) (Linux >= 6.1) and automatically detects whether the file was opened with O_DIRECT via fcntl(F_GETFL).

An integration test boots a VM with a file backed disk on a 4K sector filesystem (loop device + ext4) and verifies the guest sees the correct logical sector size and that DIO I/O succeeds.

There is also a small refactoring of create_loop_device() in the integration test helpers to fix a race where parallel tests could claim the same loop device.

Additionally, this PR contains an integration test for the block device alignment path from #7735.

@weltling
weltling requested a review from a team as a code owner February 27, 2026 23:36
@weltling
weltling force-pushed the block-file-dio-align branch 3 times, most recently from 49b12d3 to e1d22e9 Compare February 28, 2026 00:04
@rbradford
rbradford added this pull request to the merge queue Feb 28, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Feb 28, 2026
@rbradford
rbradford added this pull request to the merge queue Feb 28, 2026
@github-merge-queue
github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Feb 28, 2026
@rbradford

Copy link
Copy Markdown
Member

@weltling I think there are some cargo formatting issues kicking it out of merge queue

@weltling

Copy link
Copy Markdown
Member Author

@weltling I think there are some cargo formatting issues kicking it out of merge queue

Thanks for the review, @rbradford! Yeah. Must have started failing today of all days :)

I filed #7770 to fix the nightly issue, will rebase this one then.

Thanks

@rbradford

Copy link
Copy Markdown
Member

I've been reflecting on this PR a bit over the weekend. I'm not sure we should use both methods for detection. What does the new method do that the old one doesn't? And the old one will continue to work. I think it's fine to depend on Linux 6.1 for this functionality and just stick with the new one if there is some reason why it's better.

Comment thread block/src/lib.rs
@@ -1161,8 +1161,83 @@ impl DiskTopology {
Ok(block_size)
}

/// Query the O_DIRECT alignment requirement for a regular file.

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.

Your commit message and comment should give a rationale why statx(STATX_DIOALIGN) is better and preferred over the fstatvfs approach

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.

Done, updated the commit message and the comment. Thanks

@weltling
weltling force-pushed the block-file-dio-align branch from e1d22e9 to 1dbc56a Compare March 1, 2026 21:32
@weltling

weltling commented Mar 1, 2026

Copy link
Copy Markdown
Member Author

I've been reflecting on this PR a bit over the weekend. I'm not sure we should use both methods for detection. What does the new method do that the old one doesn't? And the old one will continue to work. I think it's fine to depend on Linux 6.1 for this functionality and just stick with the new one if there is some reason why it's better.

Agreed, dropped the fstatvfs fallback entirely. Since we're targeting modern cloud infrastructure, depending on Linux >= 6.1 for statx(STATX_DIOALIGN) is reasonable. My initial intention was to have a wider fallback, but zooming in, f_bsize is often just a preferred size like 4k which defeats the purpose of precise detection.

Keeping the fstatvfs path would add complexity without providing the same precision. I've removed the fstatvfs fallabck, updated the comments and rebased to the latest main now.

Thanks

Comment thread block/src/lib.rs Outdated
}
}

debug!("DIO alignment query failed, falling back to default {SECTOR_SIZE}");

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: s/DIO/O_DIRECT/

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.

Fixed, thanks.

weltling added 5 commits March 2, 2026 16:40
DiskTopology::probe() returned a hardcoded 512 for regular files,
causing O_DIRECT failures on volumes with larger block sizes
(e.g. 4K).

Use statx(STATX_DIOALIGN) (Linux >= 6.1) to query the real per file
DIO memory and offset alignment. Unlike fstatvfs().f_bsize, which
only returns the filesystem preferred I/O block size,
STATX_DIOALIGN reports the true DIO constraints accounting for the
filesystem, underlying block device, and any stacking (loop, dm,
etc.).

Signed-off-by: Anatol Belski <[email protected]>
Test valid power of two alignment, layout compatibility,
direct helper coverage, and O_DIRECT write/read roundtrip.

Signed-off-by: Anatol Belski <[email protected]>
Move LOOP_CTL_GET_FREE + open + LOOP_CONFIGURE into the retry loop
so each attempt requests a fresh free device number.  Previously, a
parallel test could claim the same device between GET_FREE and
CONFIGURE, and retrying the same stale number would always fail with
EBUSY.

Signed-off-by: Anatol Belski <[email protected]>
Verify that DiskTopology::probe() returns the correct DIO alignment
for a regular file on a 4k sector filesystem.  The test creates a
loop device with --sector-size 4096, formats ext4, places a raw disk
image on it, and boots a VM with direct=on.  Asserts that the guest
sees a 4096 byte logical sector and that a DIO write/read roundtrip
succeeds.

Signed-off-by: Anatol Belski <[email protected]>
Boot a VM with a 4k sector loop device passed with direct=on and
image_type=raw.  Assert that the guest sees a 4096 byte logical
sector and that a DIO write/read roundtrip at 4096 byte alignment
succeeds.

Signed-off-by: Anatol Belski <[email protected]>
@weltling
weltling force-pushed the block-file-dio-align branch from 1dbc56a to faa1803 Compare March 2, 2026 15:48
@rbradford
rbradford added this pull request to the merge queue Mar 2, 2026
Merged via the queue into cloud-hypervisor:main with commit aae7594 Mar 2, 2026
37 checks passed
@weltling
weltling deleted the block-file-dio-align branch March 2, 2026 21:14
@rbradford rbradford moved this to ✅ Done in Cloud Hypervisor Roadmap Apr 25, 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.

2 participants