Skip to content

Zero the old tail when writing the block right after the end - #131

Merged
eafer merged 1 commit into
linux-apfs:masterfrom
ozzycattt:zero-tail-next-block
Oct 16, 2026
Merged

eafer merged 1 commit into
linux-apfs:masterfrom
ozzycattt:zero-tail-next-block

Conversation

@ozzycattt

@ozzycattt ozzycattt commented Oct 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes the stale data from #130.

apfs_dstream_get_new_block() zeroes the stale tail of the last block when a write goes past the end, but the check is dstream_blks < dsblock, so it misses a write to the block right after the last one. For a 5000-byte file dstream_blks is 2, and a write to block 2 skips the zeroing:

write 8192 bytes, fsync(), truncate to 5000, write 4 bytes at 9000
-> bytes 5000-8191 still have the old data

Writes that start further away (block 3 and up) were fine, and so were writes inside the last block itself, which go through apfs_zero_bh_tail(). The patch changes the check to <=.

Tested on a Raspberry Pi 4 (kernel 6.18, 4 KiB pages):

  • Truncation and write cases, each comparing the whole file and checked again after a remount:

    • 8192 + fsync, truncate 5000, write at 9000 (the failing case),
    • write at 6000 (same block),
    • write at 8192 and 12288,
    • truncate to 4096 (block boundary),
    • 3000-byte file truncated to 1000 with a write at 4100,
    • 8 MiB file truncated to 976832 with 162554 bytes written at 981262 (the case from the stress test).

    All match and apfsck is clean.

  • One 7-minute round of my stress test, same seed as the failing run (about 15k operations), together with Keep the name from disk when preparing the first hard link #132 for the hard link problem: 0 mismatches out of 3179 files, apfsck clean, fsck_apfs -n -W clean. Without the two patches the same round gave one bad file and the apfsck warning.

apfs_dstream_get_new_block() zeroes the stale tail of the last block when
a write goes past the end of the data stream, but the check misses the
case where the new block comes right after the last one. For a 5000-byte
file dstream_blks is 2, so a write to block 2 doesn't trigger it. If the
file was cut in the middle of its last block after that block reached the
disk, the old bytes after the new end come back once the file grows:

  write 8192 bytes, fsync(), truncate to 5000, write 4 bytes at 9000
  -> bytes 5000-8191 still have the old data

Writes that start further away (block 3 and up) were fine, and so were
writes inside the last block itself, which go through apfs_zero_bh_tail().

Use "<=" so that the next block is covered too. For appends that start at
the old end, the last block is already full by the time the next block is
reached, so apfs_zero_dstream_tail() returns right away.

Reported in linux-apfs#130.

Signed-off-by: ozzycattt <ozzycattt@users.noreply.github.com>
@eafer

eafer commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

Thanks, that's very helpful.

@eafer

eafer commented Oct 16, 2026 •

Copy link
Copy Markdown
Member

It's scary that my xfstests runs somehow missed something so simple. I'll have to think of new tests for stuff like this.

@ozzycattt

Copy link
Copy Markdown
Contributor Author

Glad it helped. It only showed up because my script compares every file byte by byte after a remount, against what it wrote. I can share the script if it's useful for new tests.

@eafer

eafer commented Oct 16, 2026

Copy link
Copy Markdown
Member

Sure, if you don't mind.

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