Skip to content

virtio/fs/macos: fix fallocate semantics#597

Open
slp wants to merge 1 commit intocontainers:mainfrom
slp:fix-fallocate
Open

virtio/fs/macos: fix fallocate semantics#597
slp wants to merge 1 commit intocontainers:mainfrom
slp:fix-fallocate

Conversation

@slp
Copy link
Collaborator

@slp slp commented Mar 20, 2026

The translation of Linux's fallocate semantics to macOS was badly broken. While we can't fully preserve the original semantics, since macOS doesn't have posix_fallocate and F_PREALLOCATE doesn't allow to allocate arbitrary ranges, we can do something close enough to work fine for the vast majority of guest applications.

The translation of Linux's fallocate semantics to macOS was badly
broken. While we can't fully preserve the original semantics,
since macOS doesn't have posix_fallocate and F_PREALLOCATE doesn't
allow to allocate arbitrary ranges, we can do something close
enough to work fine for the vast majority of guest applications.

Signed-off-by: Sergio Lopez <slp@redhat.com>
@slp
Copy link
Collaborator Author

slp commented Mar 20, 2026

/gemini review

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

The pull request significantly improves the fallocate semantics for macOS by correctly translating Linux fallocate flags to their closest macOS equivalents. The implementation now properly handles LINUX_FALLOC_FL_ALLOCATE_RANGE and LINUX_FALLOC_FL_PUNCH_HOLE flags, including the LINUX_FALLOC_FL_KEEP_SIZE modifier. Error handling for unsupported flags and invalid flag combinations is also correctly implemented, making the virtio-fs behavior more robust and aligned with Linux expectations on macOS.

@slp
Copy link
Collaborator Author

slp commented Mar 25, 2026

@mtjhrc @dorindabassey @mz-pdm PTAL. If you can't test on macOS, a visual inspection is still appreciated.

@JAORMX
Copy link

JAORMX commented Mar 26, 2026

@slp I'm still getting acquainted with the codebase. I ran this through Claude, so take it with a grain of salt, but it poitned out the following:

in the keep_size path, st.st_blocks * st.st_blksize overestimates disk usage. st_blocks is in 512-byte units per POSIX, not st_blksize units. On APFS (st_blksize = 4096) this inflates the value by 8x, causing needed allocations to be skipped. Should be st.st_blocks * 512.

let new_length = (offset + length) as i64;

if keep_size {
// Check the number of allocate blocks instead of the file size.
Copy link
Collaborator

Choose a reason for hiding this comment

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

s/allocate/allocated/ ?

@mz-pdm
Copy link
Collaborator

mz-pdm commented Mar 26, 2026

@slp I'm still getting acquainted with the codebase. I ran this through Claude, so take it with a grain of salt, but it poitned out the following:

in the keep_size path, st.st_blocks * st.st_blksize overestimates disk usage. st_blocks is in 512-byte units per POSIX, not st_blksize units. On APFS (st_blksize = 4096) this inflates the value by 8x, causing needed allocations to be skipped. Should be st.st_blocks * 512.

According to https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man2/fstat.2.html, Claude is right. st_blksize is the optimal I/O block size and st_blocks is the number of 512-byte units.

Other than that (and the possible typo in the comment), the PR looks good to me (but I know nothing about the topic, just using available documentation).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants