Skip to content

Apple allocate can succeed after partial F_PREALLOCATE #1

Closed

Description

@AptS-1547

Problem

The Apple implementation inherited through rustix::fs::fallocate can report success after F_ALLOCATECONTIG reserves only part of the requested space. The implementation checks only the fcntl return code and ignores fstore_t.fst_bytesalloc, then extends the logical EOF with ftruncate.

This violates FileExt::allocate / AsyncFileExt::allocate: later writes are not guaranteed to succeed for lack of disk space.

Consumer report: AsterCommunity/AsterDrive#623.

Reproduction evidence

On macOS/APFS:

  • 128 MiB request with ample free space: inherited implementation returned Ok(()), but only 4,046,848 bytes were physically allocated after sync_all().
  • 200 MiB request on a disposable APFS volume with 132,771,840 bytes available: inherited implementation returned Ok(()), partially allocated 99,115,008 bytes, and set logical EOF to 209,715,200 bytes.
  • Direct F_ALLOCATEALL for 128 MiB returned fst_bytesalloc = 134,217,728 and fully reserved the file.
  • Direct F_ALLOCATEALL for 200 MiB on the limited volume returned ENOSPC, fst_bytesalloc = 0, and left logical and allocated sizes at zero.

Control evidence:

  • Linux fallocate fully reserves normal requests and fails without partial allocation on a size-limited tmpfs.
  • Windows FileAllocationInfo fully reserves normal requests and returns ERROR_DISK_FULL without partial allocation on a size-limited NTFS VHDX.

Required behavior

  • On Apple platforms, use all-or-nothing allocation and validate fst_bytesalloc before extending logical EOF.
  • Preserve sparse-file recovery: request only the physical bytes not already allocated.
  • Share the corrected backend between sync and async traits.
  • Add a normal APFS regression and an ENOSPC test using a disposable size-limited APFS image in CI.
  • Preserve the existing feature matrix and MSRV.

Activity

  1. added
    bug
    on Sep 18, 2026
  2. self-assigned this
    on Sep 18, 2026
  3. AptS-1547 commented on Sep 18, 2026

    @AptS-1547
    MemberAuthor

    Resolution report and consumer integration

    This issue started as an AsterDrive upload failure, but direct-library and native-filesystem tests showed that the broken contract belonged in the filesystem abstraction. The complete resolution is now merged in #2 and consumed by AsterDrive in AsterCommunity/AsterDrive#626.

    Discovery path

    1. AsterDrive upload Init repeatedly failed after FileExt::allocate returned success because only roughly 2-5 MiB of 117-263 MiB requests appeared physically allocated.
    2. The destination storage policy had ample capacity; the failing filesystem was the local staging volume on APFS.
    3. Direct calls to the inherited library reproduced the mismatch independently of AsterDrive.
    4. A size-limited APFS image demonstrated the dangerous case: the contiguous-allocation path could partially reserve space, return success, and then extend logical EOF.
    5. Direct F_ALLOCATEALL control probes demonstrated the required behavior: a feasible request reserved the full amount, while an oversized request returned ENOSPC with no reservation and no EOF extension on a new file.
    6. Native Linux and Windows controls showed that their supported allocation paths already provided the expected success/failure boundary.

    Root cause

    F_ALLOCATECONTIG is useful as a best-effort request for contiguous extents, but Apple explicitly reports the actual completed amount through fstore_t.fst_bytesalloc. Treating only the fcntl return code as proof of complete reservation was incorrect. Extending EOF afterward converted partial physical work into an apparently complete sparse file.

    The correction therefore could not be “ignore st_blocks in AsterDrive.” That would merely hide the false success. The allocation layer had to establish and test the complete physical-reservation postcondition itself.

    Implemented contract

    #2 introduced the following behavior:

    • New Apple extents use F_ALLOCATEALL and validate the returned fst_bytesalloc before logical EOF is extended.
    • Existing sparse files are recovered by locating and materializing their actual holes rather than subtracting an unrelated total allocated-block count.
    • Existing bytes and file cursor position are preserved.
    • High-offset extents cannot be mistaken for coverage of low-offset holes.
    • Allocation remains idempotent when physical blocks already cover the requested range, while logical EOF is still extended when required.
    • Non-Apple supported backends continue to invoke their native allocation primitive instead of incorrectly short-circuiting from total block accounting.
    • Async Tokio, fs-err Tokio, async-std, and Smol integrations run the synchronous native work on the appropriate blocking pool.
    • New empty Apple reservations are all-or-nothing. Existing sparse-file recovery may have partial side effects before a later ENOSPC, but it never returns false success; the consumer can retain, fail, retry, or clean that session explicitly.

    Evidence matrix

    Platform Native validation CI / build validation
    macOS / APFS Complete normal reservation; disposable limited-volume ENOSPC; allocation rounding; existing sparse holes; high-offset data; cursor/data preservation; blocking-pool isolation Native macOS tests, Apple preallocation contract job, macOS coverage and Clippy
    Linux Normal native allocation and limited-tmpfs ENOSPC in Docker Ubuntu build/test/coverage/Clippy plus affected GNU, musl, Android, and architecture cross checks
    Windows / NTFS Normal native reservation and limited-VHDX ERROR_DISK_FULL in a Windows VM Windows build/test/coverage/Clippy plus GNU and MSVC cross checks
    Rust compatibility Sync-only public contract Rust 1.75 MSRV check plus pinned Rust 1.95 development/CI toolchain and feature matrix

    The final PR head 8698896abcfb6b641f2ede685255efc8f8a07c0e passed all required CI, CodeRabbit's final incremental review produced no new actionable comments, and the squash merge is 913ed30598119c184f49f18e5b339fc98869fff2.

    Consumer result

    AsterDrive#626 pins aster-fs to that exact merge revision. Its staging path remains inside tokio::task::spawn_blocking and preserves the product semantics around HTTP 507, failed-Init cleanup, restart recovery, chunk data, cancel, and completion.

    Consumer validation includes 2/2 staging unit tests, 14/14 staging integration tests, 11/11 staged-upload tests, a dedicated 128 MiB APFS Init regression, full test compilation, Clippy with warnings denied, documentation validation, formatting, and whitespace checks.

    This closes the loop from a user-visible upload 500 to a directly tested cross-platform allocation contract, rather than papering over APFS block accounting in the consumer.

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

Metadata

Metadata

Assignees

Labels

bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions