Skip to content

fix(storage): fall back when the kernel does not report STATX_DIOALIGN - #10

Merged
BowMonk merged 2 commits into
BrainEchoLab:devfrom
sdiebolt:fix/7-statx-dioalign-fallback
Oct 2, 2026
Merged

BowMonk merged 2 commits into
BrainEchoLab:devfrom
sdiebolt:fix/7-statx-dioalign-fallback

Conversation

@sdiebolt

@sdiebolt sdiebolt commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #7.

Problem

LinuxFileIO::queryAlignment() threw when statx(2) came back without STATX_DIOALIGN:

queryAlignment: kernel does not report STATX_DIOALIGN (requires Linux 6.1+)

So storage could not open a file on a pre-6.1 kernel, or on a filesystem that does not implement the query — BTRFS among them, which is where this was hit in Effusive when enabling RF/storage saving.

Change

  • queryAlignment() now falls back instead of throwing: page-sized alignment for both file offset/length and buffer address, with a normal-level warning.
  • The filesystem case that genuinely cannot work is unchanged: a filesystem that reports the query but zeroes dio_offset_align/dio_mem_align (tmpfs) still throws, since O_DIRECT is not actually honored there.
  • The statx struct and resolution moved to src/storage/Linux/linux_statx.h as Storage::detail, so the missing-mask path can be tested directly.
  • Stale comments in LinuxFileIO.h and Storage/README.md that claimed the constructor throws whenever the kernel cannot report a value are updated.

Bug found while writing the test

A pre-existing division by zero, unrelated to the missing STATX_DIOALIGN mask: when dio_offset_align was 0 and stx_blksize was larger, stx_blksize % stx_dio_offset_align divided by zero. That is a SIGFPE on tmpfs or any other filesystem that accepts O_DIRECT without enforcing alignment. The zero case is now passed through for the caller to reject, which is what the surrounding code intended.

Test

tests/linux_alignment_check.cpp (new, wired into the Storage ctest suite, Linux only, no file I/O) drives resolveDioAlignment() across: a 512e drive, a block size that is not a multiple of the minimum alignment, a missing STATX_BASIC_STATS, a tmpfs-style zero report, the no-STATX_DIOALIGN page fallback with and without a usable block size, and an empty mask.

Validation run:

cmake -S echoframe/cpp/libs/Storage -B /tmp/ef-storage-align-build -DCMAKE_BUILD_TYPE=Release
cmake --build /tmp/ef-storage-align-build -j"$(nproc)"
ctest --test-dir /tmp/ef-storage-align-build --output-on-failure
# 100% tests passed: write_stats_check, write_count_check, linux_alignment_check

queryAlignment() threw when statx came back without STATX_DIOALIGN, so
storage could not open a file on a pre-6.1 kernel or a filesystem that
does not implement the query (BTRFS, among others). It now falls back to
stx_blksize, or the page size when the kernel did not report that either,
and warns. The page size is a multiple of every sector size such a
filesystem uses, so a buffer aligned to it satisfies any of them.

Moving the statx struct and the resolution into linux_statx.h lets
linux_alignment_check drive the mask directly, since a modern kernel
cannot produce the one this fix is about. Writing that check turned up a
division by zero: a filesystem that reports the query but zeroes
dio_offset_align (tmpfs) had stx_blksize % stx_dio_offset_align evaluated
with a zero divisor whenever stx_blksize was larger. The zero case is now
passed through for the caller to reject, as before.
@sdiebolt
sdiebolt marked this pull request as ready for review September 30, 2026 15:55
@BowMonk
BowMonk merged commit c4b0717 into BrainEchoLab:dev Oct 2, 2026
4 checks passed
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.

2 participants