Repository navigation
Zero the old tail when writing the block right after the end - #131
Merged
Merged
Conversation
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>
This was referenced Oct 15, 2026
Member
|
Thanks, that's very helpful. |
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. |
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. |
Member
|
Sure, if you don't mind. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 isdstream_blks < dsblock, so it misses a write to the block right after the last one. For a 5000-byte filedstream_blksis 2, and a write to block 2 skips the zeroing: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:
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 -Wclean. Without the two patches the same round gave one bad file and the apfsck warning.