Skip to content

fix(snapshot): copy shared blocks under the exclusive inode lock - #46

Merged
mattmattox merged 3 commits into
mainfrom
fix-cow-copy-under-lock
Oct 25, 2026
Merged

mattmattox merged 3 commits into
mainfrom
fix-cow-copy-under-lock

Conversation

@mattmattox

Copy link
Copy Markdown
Contributor

Fixes the three-node lost write found in #42's review, which predates #42. Node A writes a block in place after its re-check, node B's snapshot raises the block's count, and node C copies the block before A's data lands. C's commit check compares MetaGeneration, which in-place writes don't change, so A's acknowledged write was lost.

The new test fails on #42's code with only its hooks added ("data mismatch at offset 0") and passes 10 of 10 with -race here.

I/O per write, measured on .68:

Write Before #42 With #42 This PR
4KB in place 16 22 (+38%) 22
64KB in place 46 52 (+13%) 52
4KB copy-on-write 46.8 46.8 40.8

This PR leaves in-place cost unchanged, and copy-on-write writes no longer re-check the refcount. The 1MB copy now happens under the exclusive inode lock, so it blocks the file's other writers while it runs.

Tested on .68: lint, unit -race, integration -short (90 passed, 17 skipped).

🤖 Generated with Claude Code

mattmattox and others added 3 commits October 24, 2026 18:55
A write that copies a shared block read the old block before it took the
inode lock. An in-place write on another node could still be landing in
that block, since the block became shared only after it checked, and the
copy's commit check compared MetaGeneration, which in-place writes do not
change. The acknowledged write was lost.

The copy is now mapped before the lock, as allocating it may take bitmap
and tree blocks, and its data is read and written under the exclusive
inode lock, which waits for in-place writes holding the shared one.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A write that copied a shared block and then failed before any inode
write left the copy allocated with nothing pointing at it. Free it on
that path. A failed inode write may still have reached the disk, so
copies it carried stay allocated, as before.

CopyOnWrite is only used by the extent tests now, so it moves to a test
file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@mattmattox
mattmattox merged commit 0fca640 into main Oct 25, 2026
8 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.

1 participant