From e4169f1bbe614523f576a6cd45bb04e028b6a6f3 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Fri, 13 Nov 2026 12:41:35 -0600 Subject: [PATCH 1/9] fix(fs): remove a name only while it names the inode the lookup found Unlink and rmdir looked up the child, then removed the name and freed the child by that lookup. If another node removed the name and made a new one in between, they removed the new name and freed with stale extents. The removal now checks the child under the directory lock: a name that points to another inode returns ENOENT, and a changed child is looked up again. The free uses what the inode maps when it is freed. Name errors map to EEXIST and ENOENT. Co-Authored-By: Claude Opus 5.5 --- internal/goclusterfs/fs.go | 78 ++++++++++++++++++++++++++++---------- 1 file changed, 59 insertions(+), 19 deletions(-) diff --git a/internal/goclusterfs/fs.go b/internal/goclusterfs/fs.go index 50f1e979..36637b9b 100644 --- a/internal/goclusterfs/fs.go +++ b/internal/goclusterfs/fs.go @@ -784,8 +784,8 @@ func (f *FS) freeSlotGeneration(num uint32) uint64 { // afterCommit runs between an operation's journal commit and its first change; tests stop there as a crash would. var afterCommit func(op string) -// retryClaim runs try again, after a jittered backoff, while another node takes the inode slot it picked first. -func retryClaim(op, name string, try func() syscall.Errno) syscall.Errno { +// retryRace runs try again, after a jittered backoff, while it returns EAGAIN for losing a race with another node. +func retryRace(op, name string, try func() syscall.Errno) syscall.Errno { for attempt := 0; attempt < MaxCreateRetries; attempt++ { errno := try() if errno != syscall.EAGAIN { @@ -793,13 +793,24 @@ func retryClaim(op, name string, try func() syscall.Errno) syscall.Errno { } baseDelay := time.Millisecond * time.Duration(1< Date: Fri, 13 Nov 2026 12:41:35 -0600 Subject: [PATCH 2/9] fix(journal): replay a mkdir whole, and finish a create whose claim landed A replayed mkdir now gets "." and ".." and raises the parent's link count, under the parent's inode lock. A create that crashed after claiming its inode used to leave it unnamed: replay now names it when the logging node wrote it last, and no more often than a create does before naming it. Co-Authored-By: Claude Opus 5.5 --- internal/inode/inode.go | 38 ++++++++++++++ internal/journal/cross_node_recovery.go | 69 ++++++++++++++++++++----- 2 files changed, 94 insertions(+), 13 deletions(-) diff --git a/internal/inode/inode.go b/internal/inode/inode.go index a1f2d7c3..388aeafa 100644 --- a/internal/inode/inode.go +++ b/internal/inode/inode.go @@ -239,6 +239,44 @@ func (t *Table) lockedWrite(ctx context.Context, inode *Inode) (func(), error) { return t.writeWithTree(inode) } +// Update applies change to inode num and writes it back, holding the inode lock from the read to the write. +func (t *Table) Update(ctx context.Context, num uint32, change func(*Inode) error) error { + committed, err := t.lockedUpdate(ctx, num, change) + if committed != nil { + committed() + } + return err +} + +func (t *Table) lockedUpdate(ctx context.Context, num uint32, change func(*Inode) error) (func(), error) { + if uint64(num) >= t.totalInodes { + return nil, ErrInodeOutOfRange + } + if t.lockMgr != nil { + if err := t.lockMgr.Acquire(ctx, lock.LockTypeInode, uint64(num), lock.LockModeExclusive); err != nil { + return nil, fmt.Errorf("failed to acquire inode lock: %w", err) + } + defer t.lockMgr.Release(lock.LockTypeInode, uint64(num)) + } + unlockSector, err := t.lockSector(num) + if err != nil { + return nil, err + } + defer unlockSector() + + t.mu.Lock() + defer t.mu.Unlock() + + ino, err := t.readInodeUnlocked(num) + if err != nil { + return nil, err + } + if err := change(ino); err != nil { + return nil, err + } + return t.writeWithTree(ino) +} + // WriteWithLockHeld writes an inode to disk without acquiring the distributed lock. // Use this when the caller already holds the inode lock (e.g., in directory operations). // This prevents deadlock from trying to acquire the same lock twice. diff --git a/internal/journal/cross_node_recovery.go b/internal/journal/cross_node_recovery.go index 59e7054f..148ee374 100644 --- a/internal/journal/cross_node_recovery.go +++ b/internal/journal/cross_node_recovery.go @@ -126,7 +126,7 @@ type txnApplyResult struct { // applyTransaction applies a single recovered transaction idempotently. func applyTransaction(ctx context.Context, rctx *RecoveryContext, txn *RecoveredTxn) (*txnApplyResult, error) { result := &txnApplyResult{} - r := newTxnReplay() + r := newTxnReplay(txn.Header.OwnerNodeUUID) for _, entry := range txn.Entries { applied, err := applyEntry(ctx, rctx, r, entry) @@ -147,12 +147,13 @@ func applyTransaction(ctx context.Context, rctx *RecoveryContext, txn *Recovered // node's. A peer can take a dead node's locks before its journal is replayed, so each record checks that what it // changes is still as the operation left it. type txnReplay struct { + owner [16]byte created map[uint64]bool wrote map[uint32]uint64 } -func newTxnReplay() *txnReplay { - return &txnReplay{created: make(map[uint64]bool), wrote: make(map[uint32]uint64)} +func newTxnReplay(owner [16]byte) *txnReplay { + return &txnReplay{owner: owner, created: make(map[uint64]bool), wrote: make(map[uint32]uint64)} } // unchanged reports whether ino is in use and was last written before a record logged its MetaGeneration as gen, or @@ -194,7 +195,8 @@ func applyEntry(ctx context.Context, rctx *RecoveryContext, r *txnReplay, entry } } -// applyInodeCreate claims the inode's slot if it is still free and nobody used it since the create was logged. +// applyInodeCreate claims the inode's slot if it is still free and nobody used it since the create was logged, or +// takes over the create's own claim if it landed before the crash. func applyInodeCreate(ctx context.Context, rctx *RecoveryContext, r *txnReplay, payload []byte) (bool, error) { p, err := DeserializeInodeCreate(payload) if err != nil { @@ -205,13 +207,49 @@ func applyInodeCreate(ctx context.Context, rctx *RecoveryContext, r *txnReplay, return free.MetaGeneration == p.MetaGeneration }) if errors.Is(err, inode.ErrInodeInUse) { - log.Printf("[Recovery] InodeCreate: slot %d is in use or was used since, skipping", p.InodeNum) - return false, nil + ino, err = r.ownClaim(rctx, p) + if ino == nil && err == nil { + log.Printf("[Recovery] InodeCreate: slot %d is in use or was used since, skipping", p.InodeNum) + return false, nil + } } if err != nil { return false, fmt.Errorf("failed to claim inode %d: %w", p.InodeNum, err) } + if ino.Mode == 0 { + if err := initCreated(ctx, rctx, ino, p); err != nil { + return false, fmt.Errorf("failed to set up inode %d: %w", p.InodeNum, err) + } + } + r.created[p.InodeNum] = true + r.wrote[ino.InodeNum] = ino.MetaGeneration + + log.Printf("[Recovery] InodeCreate: created inode %d", p.InodeNum) + return true, nil +} + +// ownClaim returns the slot's inode if the create's own claim landed and nothing wrote the inode since: the logging +// node wrote it last, and at most as often as a create does before adding the name. +func (r *txnReplay) ownClaim(rctx *RecoveryContext, p *InodeCreatePayload) (*inode.Inode, error) { + ino, err := rctx.InodeTbl.Read(uint32(p.InodeNum)) + if err != nil { + return nil, err + } + writes := ino.MetaGeneration - p.MetaGeneration + maxWrites := uint64(2) // the claim, then the attributes + if uint16(p.Mode)&inode.S_IFMT == inode.S_IFDIR { + maxWrites = 3 // the claim, "." and "..", then the attributes + } + if ino.Magic != inode.InodeMagic || ino.LastModifierUUID != r.owner || writes < 1 || writes > maxWrites || + ino.Mode != 0 && ino.Mode != uint16(p.Mode) { + return nil, nil + } + return ino, nil +} + +// initCreated gives a newly claimed inode the logged attributes, and a directory its "." and ".." entries. +func initCreated(ctx context.Context, rctx *RecoveryContext, ino *inode.Inode, p *InodeCreatePayload) error { ino.Mode = uint16(p.Mode) ino.UID = p.UID ino.GID = p.GID @@ -219,14 +257,11 @@ func applyInodeCreate(ctx context.Context, rctx *RecoveryContext, r *txnReplay, ino.Atime = now ino.Mtime = now ino.Ctime = now - if err := rctx.InodeTbl.Write(ctx, ino); err != nil { - return false, fmt.Errorf("failed to write inode %d: %w", p.InodeNum, err) + if !ino.IsDir() { + return rctx.InodeTbl.Write(ctx, ino) } - r.created[p.InodeNum] = true - r.wrote[ino.InodeNum] = ino.MetaGeneration - - log.Printf("[Recovery] InodeCreate: created inode %d", p.InodeNum) - return true, nil + ino.LinkCount = 2 + return rctx.DirMgr.InitDirectory(ctx, ino, p.ParentInodeNum) } // applyInodeUpdate updates an inode's metadata. @@ -315,6 +350,14 @@ func applyDirAddEntry(ctx context.Context, rctx *RecoveryContext, r *txnReplay, case err != nil: return false, fmt.Errorf("failed to add entry %q: %w", p.Name, err) } + if p.EntryType == DirEntryDirectory { + if err := rctx.InodeTbl.Update(ctx, uint32(p.ParentInodeNum), func(parent *inode.Inode) error { + parent.LinkCount++ + return nil + }); err != nil { + return true, fmt.Errorf("failed to count the link from %q in parent %d: %w", p.Name, p.ParentInodeNum, err) + } + } log.Printf("[Recovery] DirAddEntry: added entry %q -> inode %d in parent %d", p.Name, p.ChildInodeNum, p.ParentInodeNum) From f32bc564a6b104b1fafc32e66b3ca23890aac551 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Fri, 13 Nov 2026 12:41:35 -0600 Subject: [PATCH 3/9] test: unlink racing a peer's recreate, replayed mkdir and claims, and name errors Co-Authored-By: Claude Opus 5.5 --- internal/goclusterfs/name_order_test.go | 170 ++++++++++++++++++- internal/journal/cross_node_recovery_test.go | 100 ++++++++--- tests/test-manifest.yaml | 38 ++++- 3 files changed, 272 insertions(+), 36 deletions(-) diff --git a/internal/goclusterfs/name_order_test.go b/internal/goclusterfs/name_order_test.go index b4b9a599..fdfcf962 100644 --- a/internal/goclusterfs/name_order_test.go +++ b/internal/goclusterfs/name_order_test.go @@ -3,13 +3,13 @@ package goclusterfs import ( "context" "errors" + "syscall" "testing" "time" "github.com/hanwen/go-fuse/v2/fuse" "github.com/supporttools/GoClusterFS/internal/dir" - "github.com/supporttools/GoClusterFS/internal/fsck" "github.com/supporttools/GoClusterFS/internal/inode" ) @@ -45,18 +45,40 @@ func expectOneNamePerInode(t *testing.T, fsys *FS) map[string]uint64 { return names } -// A create or mkdir crashes between claiming and naming its inode; a peer's file created before replay keeps its own inode. -func TestCrashBetweenClaimAndNameLeavesNoSharedName(t *testing.T) { +// expectEmptyDir fails unless directory num has "." and ".." entries and the link count of an empty directory. +func expectEmptyDir(t *testing.T, fsys *FS, num uint64, parent uint32) { + t.Helper() + ctx := context.Background() + ino, err := fsys.inodeTbl.Read(uint32(num)) + if err != nil { + t.Fatal(err) + } + if !ino.IsDir() || ino.LinkCount != 2 { + t.Errorf("inode %d: mode %#o, link count %d; want an empty directory", num, ino.Mode, ino.LinkCount) + } + if self, _, err := fsys.dirMgr.Lookup(ctx, ino, "."); err != nil || self != num { + t.Errorf("%q in inode %d: %d, %v", ".", num, self, err) + } + if up, _, err := fsys.dirMgr.Lookup(ctx, ino, ".."); err != nil || up != uint64(parent) { + t.Errorf("%q in inode %d: %d, %v; want %d", "..", num, up, err, parent) + } +} + +// A create or mkdir crashes between claiming and naming its inode, and a peer creates a file before replay. Replay +// names the claimed inode, and the peer's file keeps an inode of its own. +func TestCrashBetweenClaimAndNameReplaysTheName(t *testing.T) { ctx := context.Background() for _, tc := range []struct { crashPoint string create func(fsys *FS) + isDir bool }{ - {"create: inode claimed", func(fsys *FS) { fsys.Create(ctx, "crashed", 0, 0o644, &fuse.EntryOut{}) }}, - {"mkdir: inode claimed", func(fsys *FS) { fsys.Mkdir(ctx, "crashed", 0o755, &fuse.EntryOut{}) }}, + {"create: inode claimed", func(fsys *FS) { fsys.Create(ctx, "crashed", 0, 0o644, &fuse.EntryOut{}) }, false}, + {"mkdir: inode claimed", func(fsys *FS) { fsys.Mkdir(ctx, "crashed", 0o755, &fuse.EntryOut{}) }, true}, } { t.Run(tc.crashPoint, func(t *testing.T) { fsys, path, offset, size := journaledFS(t) + links := readRoot(t, fsys).LinkCount crashed := crashAfterCommit(t, fsys, tc.crashPoint) go tc.create(fsys) <-crashed @@ -65,18 +87,148 @@ func TestCrashBetweenClaimAndNameLeavesNoSharedName(t *testing.T) { createFile(t, openFormattedFS(t, path), "peer.txt") remounted := remountAndReplay(t, path, offset, size) - if _, ok := expectOneNamePerInode(t, remounted)["peer.txt"]; !ok { + names := expectOneNamePerInode(t, remounted) + if _, ok := names["peer.txt"]; !ok { t.Error("the peer's file is gone after replay") } - for _, p := range runFsck(t, remounted, fsck.DefaultOptions()).Problems { - if p.Severity == fsck.Error { - t.Errorf("fsck after replay: %s", p.String()) + num, ok := names["crashed"] + if !ok { + t.Fatal("replay did not name the claimed inode") + } + if tc.isDir { + expectEmptyDir(t, remounted, num, inode.InodeRoot) + if got := readRoot(t, remounted).LinkCount; got != links+1 { + t.Errorf("root link count %d after replay, want %d", got, links+1) + } + } + expectCleanFsck(t, remounted) + }) + } +} + +// A mkdir stops after its commit and the process dies: replay makes the directory with "." and "..", and counts its +// ".." in the parent's link count. +func TestMkdirReplaysAfterCrash(t *testing.T) { + fsys, path, offset, size := journaledFS(t) + links := readRoot(t, fsys).LinkCount + crashed := crashAfterCommit(t, fsys, "mkdir") + go fsys.Mkdir(context.Background(), "made", 0o755, &fuse.EntryOut{}) + <-crashed + + remounted := remountAndReplay(t, path, offset, size) + num, ok := expectOneNamePerInode(t, remounted)["made"] + if !ok { + t.Fatal("replay did not add the directory") + } + expectEmptyDir(t, remounted, num, inode.InodeRoot) + if got := readRoot(t, remounted).LinkCount; got != links+1 { + t.Errorf("root link count %d after replay, want %d", got, links+1) + } + expectCleanFsck(t, remounted) +} + +// Node A looks up a name to remove it, and node B removes the name and makes a new one before A removes it. A must +// not remove or free B's new inode by what its lookup saw. +func TestRemoveRacesPeerRecreate(t *testing.T) { + ctx := context.Background() + for _, tc := range []struct { + name string + isDir bool + reuse bool + want syscall.Errno + }{ + {"unlink, new file in another slot", false, false, syscall.ENOENT}, + {"unlink, new file in the same slot", false, true, 0}, + {"rmdir, new directory in another slot", true, false, syscall.ENOENT}, + } { + t.Run(tc.name, func(t *testing.T) { + a, path, _, _ := journaledFS(t) + b := openFormattedFS(t, path) + remove := func(fsys *FS) syscall.Errno { + if tc.isDir { + return fsys.RootDir().Rmdir(ctx, "x") + } + return fsys.RootDir().Unlink(ctx, "x") + } + recreate := func() { + if !tc.reuse { + createFile(t, b, "filler") } + if tc.isDir { + newSubdir(t, b, "x") + } else { + writeAt(t, createFile(t, b, "x"), pattern(2, 2*BlockSize), 0) + } + } + if tc.isDir { + newSubdir(t, a, "x") + } else { + writeAt(t, createFile(t, a, "x"), pattern(1, BlockSize), 0) } + + afterCommit = func(op string) { + if op != "unlink" { + return + } + afterCommit = nil + if errno := remove(b); errno != 0 { + t.Fatalf("B's remove: %v", errno) + } + recreate() + } + t.Cleanup(func() { afterCommit = nil }) + + if errno := remove(a); errno != tc.want { + t.Errorf("A's remove = %v, want %v", errno, tc.want) + } + if _, kept := expectOneNamePerInode(t, b)["x"]; kept != (tc.want != 0) { + t.Errorf("x exists after A's remove: %v", kept) + } + expectCleanFsck(t, b) }) } } +// A create or mkdir whose name is taken, or whose directory another node removed, reports EEXIST or ENOENT and gives +// back the inode it claimed. +func TestCreateNameErrors(t *testing.T) { + ctx := context.Background() + fsys := openFormattedFS(t, newFormattedImage(t)) + createFile(t, fsys, "taken") + gone := newSubdir(t, fsys, "gone") + if errno := fsys.Rmdir(ctx, "gone"); errno != 0 { + t.Fatalf("rmdir: %v", errno) + } + for _, tc := range []struct { + parent uint32 + name string + want syscall.Errno + }{ + {inode.InodeRoot, "taken", syscall.EEXIST}, + {gone.inodeNum, "late", syscall.ENOENT}, + } { + for _, mkdir := range []bool{false, true} { + slot, err := fsys.inodeTbl.FindFreeSlot() + if err != nil { + t.Fatal(err) + } + var errno syscall.Errno + if mkdir { + _, errno = fsys.makeDirOnce(ctx, &fsys.Inode, tc.parent, tc.name, 0o755, &fuse.EntryOut{}) + } else { + _, errno = fsys.createFileOnce(ctx, &fsys.Inode, tc.parent, tc.name, 0o644, &fuse.EntryOut{}) + } + if errno != tc.want { + t.Errorf("mkdir %v, %q in inode %d: %v, want %v", mkdir, tc.name, tc.parent, errno, tc.want) + } + if ino, err := fsys.inodeTbl.Read(slot); err != nil || ino.Magic != 0 { + t.Errorf("mkdir %v, %q: slot %d still in use after the failure (%v)", mkdir, tc.name, slot, err) + } + } + } + expectCleanFsck(t, fsys) +} + // A peer that changes an entry while RemoveIf checks it waits for the directory lock. func TestRemoveIfHoldsDirectoryLockWhileChecking(t *testing.T) { ctx := context.Background() diff --git a/internal/journal/cross_node_recovery_test.go b/internal/journal/cross_node_recovery_test.go index d0e5e187..5ac3bef5 100644 --- a/internal/journal/cross_node_recovery_test.go +++ b/internal/journal/cross_node_recovery_test.go @@ -50,7 +50,7 @@ func testRecoveryContext(t testing.TB) (*RecoveryContext, func()) { t.Skipf("O_DIRECT not supported: %v", err) } - nodeUUID := [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} + nodeUUID := testNodeUUID bm := bitmap.NewRegion(dev, bitmapOffset, totalBlocks, nil, nodeUUID) inodeTbl := inode.NewTable(dev, inodeOffset, inodeCount*inode.InodeSize, nil, nodeUUID) @@ -79,7 +79,7 @@ func TestApplyInodeCreate(t *testing.T) { rctx, cleanup := testRecoveryContext(t) defer cleanup() ctx := context.Background() - r := newTxnReplay() + r := newTxnReplay([16]byte{}) // Create a payload for inode creation // Note: Use inode number < 100 since test context only creates 100 inodes @@ -132,7 +132,7 @@ func TestApplyInodeUpdate(t *testing.T) { rctx, cleanup := testRecoveryContext(t) defer cleanup() ctx := context.Background() - r := newTxnReplay() + r := newTxnReplay([16]byte{}) // First, create an inode to update // Note: Use inode number < 100 since test context only creates 100 inodes @@ -168,7 +168,7 @@ func TestApplyInodeDelete(t *testing.T) { rctx, cleanup := testRecoveryContext(t) defer cleanup() ctx := context.Background() - r := newTxnReplay() + r := newTxnReplay([16]byte{}) // First, create an inode to delete // Note: Use inode number < 100 since test context only creates 100 inodes @@ -280,7 +280,7 @@ func TestApplyDirAddEntry(t *testing.T) { rctx, cleanup := testRecoveryContext(t) defer cleanup() ctx := context.Background() - r := newTxnReplay() + r := newTxnReplay([16]byte{}) // Create a parent directory inode parentPayload := &InodeCreatePayload{ @@ -361,7 +361,7 @@ func TestApplyDirRemoveEntry(t *testing.T) { rctx, cleanup := testRecoveryContext(t) defer cleanup() ctx := context.Background() - r := newTxnReplay() + r := newTxnReplay([16]byte{}) // Setup: Create parent dir, child inode, and add entry parentPayload := &InodeCreatePayload{ @@ -438,7 +438,7 @@ func TestApplyExtentAdd(t *testing.T) { rctx, cleanup := testRecoveryContext(t) defer cleanup() ctx := context.Background() - r := newTxnReplay() + r := newTxnReplay([16]byte{}) // Create an inode to attach the extent to inodePayload := &InodeCreatePayload{ @@ -496,7 +496,7 @@ func TestApplyExtentAdd(t *testing.T) { } // A second replay finds the inode written since the write was logged. - applied, err = applyExtentAdd(ctx, rctx, newTxnReplay(), extentPayload.Serialize()) + applied, err = applyExtentAdd(ctx, rctx, newTxnReplay([16]byte{}), extentPayload.Serialize()) if err != nil { t.Fatalf("applyExtentAdd (idempotent) failed: %v", err) } @@ -512,7 +512,7 @@ func TestApplyExtentAddIntoTree(t *testing.T) { rctx, cleanup := testRecoveryContext(t) defer cleanup() ctx := context.Background() - r := newTxnReplay() + r := newTxnReplay([16]byte{}) if _, err := applyInodeCreate(ctx, rctx, r, (&InodeCreatePayload{InodeNum: 51, Mode: 0644}).Serialize()); err != nil { t.Fatalf("Setup: failed to create inode: %v", err) @@ -540,7 +540,7 @@ func TestApplyExtentAddIntoTree(t *testing.T) { } payload := (&ExtentAddPayload{InodeNum: 51, FileBlock: 6, DiskBlock: lost, Length: 1, MetaGeneration: ino.MetaGeneration}).Serialize() for attempt, wantApplied := range []bool{true, false} { - applied, err := applyExtentAdd(ctx, rctx, newTxnReplay(), payload) + applied, err := applyExtentAdd(ctx, rctx, newTxnReplay([16]byte{}), payload) if err != nil { t.Fatalf("replay %d: %v", attempt, err) } @@ -650,14 +650,14 @@ func TestApplyExtentFreeCutsUnchangedInode(t *testing.T) { ino := newDataInode(t, rctx, 50, 1, 2, 3) stale := &ExtentFreePayload{InodeNum: 50, MetaGeneration: ino.MetaGeneration - 1, Size: 1 << 20} - if applied, err := applyExtentFree(ctx, rctx, newTxnReplay(), stale.Serialize()); err != nil || applied { + if applied, err := applyExtentFree(ctx, rctx, newTxnReplay([16]byte{}), stale.Serialize()); err != nil || applied { t.Fatalf("applyExtentFree logged before a later inode write = %v, %v; want false, nil", applied, err) } free := (&ExtentFreePayload{InodeNum: 50, MetaGeneration: ino.MetaGeneration, Size: 1 << 20}).Serialize() - if applied, err := applyExtentFree(ctx, rctx, newTxnReplay(), free); err != nil || !applied { + if applied, err := applyExtentFree(ctx, rctx, newTxnReplay([16]byte{}), free); err != nil || !applied { t.Fatalf("applyExtentFree = %v, %v; want true, nil", applied, err) } - if applied, err := applyExtentFree(ctx, rctx, newTxnReplay(), free); err != nil || applied { + if applied, err := applyExtentFree(ctx, rctx, newTxnReplay([16]byte{}), free); err != nil || applied { t.Fatalf("second applyExtentFree = %v, %v; want false, nil", applied, err) } @@ -719,7 +719,7 @@ func TestPayloadsWithoutGenerations(t *testing.T) { func recoveredTxn(records ...interface { Serialize() []byte }) *RecoveredTxn { - txn := &RecoveredTxn{} + txn := &RecoveredTxn{Header: &TxnHeader{}} for _, rec := range records { var op TxnOpType switch rec.(type) { @@ -745,14 +745,8 @@ func recoveredTxn(records ...interface { func newRootDir(t *testing.T, rctx *RecoveryContext) { t.Helper() ctx := context.Background() - if _, err := applyInodeCreate(ctx, rctx, newTxnReplay(), (&InodeCreatePayload{InodeNum: uint64(inode.InodeRoot), Mode: uint32(inode.S_IFDIR | 0755)}).Serialize()); err != nil { - t.Fatal(err) - } - root, err := rctx.InodeTbl.Read(inode.InodeRoot) - if err != nil { - t.Fatal(err) - } - if err := rctx.DirMgr.InitDirectory(ctx, root, uint64(inode.InodeRoot)); err != nil { + root := &InodeCreatePayload{InodeNum: uint64(inode.InodeRoot), Mode: uint32(inode.S_IFDIR | 0755), ParentInodeNum: uint64(inode.InodeRoot)} + if _, err := applyInodeCreate(ctx, rctx, newTxnReplay([16]byte{}), root.Serialize()); err != nil { t.Fatal(err) } } @@ -934,6 +928,64 @@ func TestReplayedCreateSkipsUsedSlot(t *testing.T) { } } +var testNodeUUID = [16]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16} + +// A create's claim lands before the crash: replay adds the name if the logging node wrote the inode last, and no more +// often than a create does before naming it. Nothing else can reach an unnamed inode. +func TestReplayedCreateFinishesOwnClaim(t *testing.T) { + for _, tc := range []struct { + name string + owner [16]byte + writes int + named bool + }{ + {"claimed", testNodeUUID, 0, true}, + {"claimed and set up", testNodeUUID, 1, true}, + {"claimed by another node", [16]byte{}, 0, false}, + {"written since", testNodeUUID, 2, false}, + } { + t.Run(tc.name, func(t *testing.T) { + rctx, cleanup := testRecoveryContext(t) + defer cleanup() + ctx := context.Background() + newRootDir(t, rctx) + free, err := rctx.InodeTbl.Read(50) + if err != nil { + t.Fatal(err) + } + ino, err := rctx.InodeTbl.ClaimSlot(ctx, 50) + if err != nil { + t.Fatal(err) + } + for range tc.writes { + ino.Mode = inode.S_IFREG | 0644 + if err := rctx.InodeTbl.Write(ctx, ino); err != nil { + t.Fatal(err) + } + } + + create := recoveredTxn( + &InodeCreatePayload{InodeNum: 50, Mode: uint32(inode.S_IFREG | 0644), ParentInodeNum: uint64(inode.InodeRoot), MetaGeneration: free.MetaGeneration}, + &DirAddEntryPayload{ParentInodeNum: uint64(inode.InodeRoot), ChildInodeNum: 50, EntryType: DirEntryRegular, Name: "made"}, + ) + create.Header.OwnerNodeUUID = tc.owner + if _, err := applyTransaction(ctx, rctx, create); err != nil { + t.Fatal(err) + } + num, err := lookupRoot(t, rctx, "made") + if tc.named && (err != nil || num != 50) { + t.Errorf("entry after replay: %d, %v; want inode 50", num, err) + } + if !tc.named && !errors.Is(err, dir.ErrNotFound) { + t.Errorf("replay named a slot it cannot tell is the create's own: %d, %v", num, err) + } + if ino, err := rctx.InodeTbl.Read(50); tc.named && (err != nil || ino.Mode != inode.S_IFREG|0644) { + t.Errorf("inode 50 after replay: mode %#o, %v", ino.Mode, err) + } + }) + } +} + func TestReplayedCreateFinishesUnclaimedSlot(t *testing.T) { rctx, cleanup := testRecoveryContext(t) defer cleanup() @@ -976,7 +1028,7 @@ func TestApplyExtentAddSkipsChangedInode(t *testing.T) { if err := rctx.InodeTbl.Write(ctx, ino); err != nil { t.Fatal(err) } - if applied, err := applyExtentAdd(ctx, rctx, newTxnReplay(), write); err != nil || applied { + if applied, err := applyExtentAdd(ctx, rctx, newTxnReplay([16]byte{}), write); err != nil || applied { t.Fatalf("replay after a truncate = %v, %v; want false, nil", applied, err) } if ino, err := rctx.InodeTbl.Read(50); err != nil || ino.Size != 0 || ino.ExtentCount != 0 { @@ -989,7 +1041,7 @@ func TestApplyDirEntrySkipsRemovedParent(t *testing.T) { rctx, cleanup := testRecoveryContext(t) defer cleanup() ctx := context.Background() - r := newTxnReplay() + r := newTxnReplay([16]byte{}) free, err := rctx.InodeTbl.Read(51) if err != nil { t.Fatal(err) diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index 02c6ecb8..a714d142 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -976,14 +976,46 @@ scenarios: go: internal/goclusterfs/replay_guard_test.go::TestRmdirFreesDirectoryInode - id: crash-between-claim-and-name - name: A crash between claiming a new inode and naming it leaves no shared name - description: A create or mkdir stops after claiming its inode and before adding the name, and a peer creates a file before replay; the peer's file gets its own inode, no two names share an inode, and fsck finds no errors + name: Replay names an inode whose create crashed between the claim and the name + description: A create or mkdir stops after claiming its inode and before adding the name, and a peer creates a file before replay; replay names the claimed inode, a directory gets "." and "..", the peer's file keeps its own inode, and fsck is clean priority: high environments: - go: internal/goclusterfs/name_order_test.go::TestCrashBetweenClaimAndNameLeavesNoSharedName + go: internal/goclusterfs/name_order_test.go::TestCrashBetweenClaimAndNameReplaysTheName docker: TODO # needs a pause between the claim and the name, which only the Go test hook gives vm: TODO # a power failure cannot be timed into the window between the claim and the name without that hook + - id: mkdir-replays-after-crash + name: Replay of an interrupted mkdir makes a complete directory + description: A mkdir stops after its commit and the process dies; replay makes the directory with "." and "..", raises the parent's link count, and fsck is clean + priority: high + environments: + go: internal/goclusterfs/name_order_test.go::TestMkdirReplaysAfterCrash + docker: TODO # needs a pause between commit and the claim, which only the Go test hook gives + vm: TODO # a power failure cannot be timed into the window between commit and the claim without that hook + + - id: remove-races-peer-recreate + name: Unlink and rmdir do not remove a name another node made since their lookup + description: Node B removes a name and makes a new one between node A's lookup and its removal; A returns ENOENT when the new name has another inode, removes the new file whole when it reuses the slot, and fsck is clean + priority: high + environments: + go: internal/goclusterfs/name_order_test.go::TestRemoveRacesPeerRecreate + docker: TODO # needs a pause between one node's lookup and its removal, which only the Go test hook gives + vm: TODO # the race window is too short to hit on purpose without that hook + + - id: create-name-errors + name: A failed name add reports EEXIST or ENOENT + description: Create and mkdir into a taken name return EEXIST, into a removed directory ENOENT, and give back the claimed inode + priority: go_only + environments: + go: internal/goclusterfs/name_order_test.go::TestCreateNameErrors + + - id: replayed-create-finishes-own-claim + name: Replay names a create's own claim only while nothing wrote it since + description: The logging node claimed the slot and wrote it at most as often as a create does; replay adds the name, and leaves a slot another node wrote, or wrote more often, alone + priority: go_only + environments: + go: internal/journal/cross_node_recovery_test.go::TestReplayedCreateFinishesOwnClaim + - id: remove-if-holds-directory-lock name: RemoveIf checks an entry under the directory lock description: A peer node removes the entry while RemoveIf checks it; the peer waits for the directory lock and finds the entry gone From 365193ce80cc072993d613d4249abf77a63a3d47 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Fri, 13 Nov 2026 12:42:26 -0600 Subject: [PATCH 4/9] test(fs): unlink racing a peer that puts a directory in the same slot Co-Authored-By: Claude Opus 5.5 --- internal/goclusterfs/name_order_test.go | 26 +++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/internal/goclusterfs/name_order_test.go b/internal/goclusterfs/name_order_test.go index fdfcf962..a8ae5740 100644 --- a/internal/goclusterfs/name_order_test.go +++ b/internal/goclusterfs/name_order_test.go @@ -132,20 +132,22 @@ func TestMkdirReplaysAfterCrash(t *testing.T) { func TestRemoveRacesPeerRecreate(t *testing.T) { ctx := context.Background() for _, tc := range []struct { - name string - isDir bool - reuse bool - want syscall.Errno + name string + isDir bool + newDir bool + reuse bool + want syscall.Errno }{ - {"unlink, new file in another slot", false, false, syscall.ENOENT}, - {"unlink, new file in the same slot", false, true, 0}, - {"rmdir, new directory in another slot", true, false, syscall.ENOENT}, + {"unlink, new file in another slot", false, false, false, syscall.ENOENT}, + {"unlink, new file in the same slot", false, false, true, 0}, + {"unlink, new directory in the same slot", false, true, true, syscall.EISDIR}, + {"rmdir, new directory in another slot", true, true, false, syscall.ENOENT}, } { t.Run(tc.name, func(t *testing.T) { a, path, _, _ := journaledFS(t) b := openFormattedFS(t, path) - remove := func(fsys *FS) syscall.Errno { - if tc.isDir { + remove := func(fsys *FS, isDir bool) syscall.Errno { + if isDir { return fsys.RootDir().Rmdir(ctx, "x") } return fsys.RootDir().Unlink(ctx, "x") @@ -154,7 +156,7 @@ func TestRemoveRacesPeerRecreate(t *testing.T) { if !tc.reuse { createFile(t, b, "filler") } - if tc.isDir { + if tc.newDir { newSubdir(t, b, "x") } else { writeAt(t, createFile(t, b, "x"), pattern(2, 2*BlockSize), 0) @@ -171,14 +173,14 @@ func TestRemoveRacesPeerRecreate(t *testing.T) { return } afterCommit = nil - if errno := remove(b); errno != 0 { + if errno := remove(b, tc.isDir); errno != 0 { t.Fatalf("B's remove: %v", errno) } recreate() } t.Cleanup(func() { afterCommit = nil }) - if errno := remove(a); errno != tc.want { + if errno := remove(a, tc.isDir); errno != tc.want { t.Errorf("A's remove = %v, want %v", errno, tc.want) } if _, kept := expectOneNamePerInode(t, b)["x"]; kept != (tc.want != 0) { From e67febac3716f330cbad616f7ec393f5e84869f7 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Fri, 13 Nov 2026 12:56:00 -0600 Subject: [PATCH 5/9] fix(dir): report a directory whose inode is already free as removed Co-Authored-By: Claude Opus 5.5 --- internal/dir/dir.go | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/internal/dir/dir.go b/internal/dir/dir.go index 6913e1e1..8e8b88a2 100644 --- a/internal/dir/dir.go +++ b/internal/dir/dir.go @@ -180,6 +180,9 @@ func (m *Manager) lookupUnlocked(ctx context.Context, dirIno *inode.Inode, name // Add adds a new entry to a directory. // Uses an optimistic retry pattern to handle concurrent modifications from other nodes. func (m *Manager) Add(ctx context.Context, dirIno *inode.Inode, name string, childInode uint64, fileType uint8) error { + if dirIno.Magic != inode.InodeMagic { + return ErrRemoved + } if !dirIno.IsDir() { return ErrNotDirectory } @@ -318,6 +321,9 @@ func (m *Manager) Remove(ctx context.Context, dirIno *inode.Inode, name string) // RemoveIf removes name if ok accepts the inode it names; ok runs under the directory lock. func (m *Manager) RemoveIf(ctx context.Context, dirIno *inode.Inode, name string, ok func(child uint64) bool) (bool, error) { + if dirIno.Magic != inode.InodeMagic { + return false, ErrRemoved + } if !dirIno.IsDir() { return false, ErrNotDirectory } From bd1835306b4099f17be79dc920de91fbe753bfcb Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Fri, 13 Nov 2026 12:56:00 -0600 Subject: [PATCH 6/9] test(journal): build the root with newRootDir now that replay sets up directories Co-Authored-By: Claude Opus 5.5 --- internal/journal/cross_node_recovery_test.go | 49 ++------------------ 1 file changed, 4 insertions(+), 45 deletions(-) diff --git a/internal/journal/cross_node_recovery_test.go b/internal/journal/cross_node_recovery_test.go index 5ac3bef5..95d029b8 100644 --- a/internal/journal/cross_node_recovery_test.go +++ b/internal/journal/cross_node_recovery_test.go @@ -282,32 +282,7 @@ func TestApplyDirAddEntry(t *testing.T) { ctx := context.Background() r := newTxnReplay([16]byte{}) - // Create a parent directory inode - parentPayload := &InodeCreatePayload{ - InodeNum: uint64(inode.InodeRoot), - Mode: uint32(inode.S_IFDIR | 0755), - UID: 0, - GID: 0, - } - if _, err := applyInodeCreate(ctx, rctx, r, parentPayload.Serialize()); err != nil { - t.Fatalf("Setup: failed to create parent inode: %v", err) - } - - // Update link count for parent - parentIno, err := rctx.InodeTbl.Read(inode.InodeRoot) - if err != nil { - t.Fatalf("Setup: failed to read parent inode: %v", err) - } - parentIno.LinkCount = 2 - if err := rctx.InodeTbl.Write(ctx, parentIno); err != nil { - t.Fatalf("Setup: failed to write parent inode: %v", err) - } - - // Initialize the directory structure - parentIno, _ = rctx.InodeTbl.Read(inode.InodeRoot) - if err := rctx.DirMgr.InitDirectory(ctx, parentIno, uint64(inode.InodeRoot)); err != nil { - t.Fatalf("Setup: failed to init directory: %v", err) - } + newRootDir(t, rctx) // Create a child inode childPayload := &InodeCreatePayload{ @@ -337,7 +312,7 @@ func TestApplyDirAddEntry(t *testing.T) { } // Verify the entry was added - parentIno, _ = rctx.InodeTbl.Read(inode.InodeRoot) + parentIno, _ := rctx.InodeTbl.Read(inode.InodeRoot) childNum, _, err := rctx.DirMgr.Lookup(ctx, parentIno, "testfile.txt") if err != nil { t.Fatalf("Failed to lookup added entry: %v", err) @@ -363,23 +338,7 @@ func TestApplyDirRemoveEntry(t *testing.T) { ctx := context.Background() r := newTxnReplay([16]byte{}) - // Setup: Create parent dir, child inode, and add entry - parentPayload := &InodeCreatePayload{ - InodeNum: uint64(inode.InodeRoot), - Mode: uint32(inode.S_IFDIR | 0755), - } - if _, err := applyInodeCreate(ctx, rctx, r, parentPayload.Serialize()); err != nil { - t.Fatalf("Setup: failed to create parent inode: %v", err) - } - parentIno, _ := rctx.InodeTbl.Read(inode.InodeRoot) - parentIno.LinkCount = 2 - if err := rctx.InodeTbl.Write(ctx, parentIno); err != nil { - t.Fatal(err) - } - parentIno, _ = rctx.InodeTbl.Read(inode.InodeRoot) - if err := rctx.DirMgr.InitDirectory(ctx, parentIno, uint64(inode.InodeRoot)); err != nil { - t.Fatalf("Setup: failed to init directory: %v", err) - } + newRootDir(t, rctx) childPayload := &InodeCreatePayload{InodeNum: 20, Mode: 0644} if _, err := applyInodeCreate(ctx, rctx, r, childPayload.Serialize()); err != nil { @@ -417,7 +376,7 @@ func TestApplyDirRemoveEntry(t *testing.T) { } // Verify the entry was removed - parentIno, _ = rctx.InodeTbl.Read(inode.InodeRoot) + parentIno, _ := rctx.InodeTbl.Read(inode.InodeRoot) _, _, err = rctx.DirMgr.Lookup(ctx, parentIno, "toremove.txt") if err != dir.ErrNotFound { t.Errorf("Entry should be removed, but Lookup returned err=%v", err) From 0a69b2759993a8668cdb6c93550b02d5fb557d2d Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Fri, 13 Nov 2026 13:09:26 -0600 Subject: [PATCH 7/9] test(fs): unlink frees blocks an open handle wrote after the name was removed Co-Authored-By: Claude Opus 5.5 --- internal/goclusterfs/fs.go | 3 +++ internal/goclusterfs/name_order_test.go | 19 +++++++++++++++++++ tests/test-manifest.yaml | 7 +++++++ 3 files changed, 29 insertions(+) diff --git a/internal/goclusterfs/fs.go b/internal/goclusterfs/fs.go index 36637b9b..c36ab3fd 100644 --- a/internal/goclusterfs/fs.go +++ b/internal/goclusterfs/fs.go @@ -1467,6 +1467,9 @@ func (d *Dir) removeEntryOnce(ctx context.Context, name string, isDir bool) sysc case !removed: return syscall.ENOENT } + if afterCommit != nil { + afterCommit("unlink: name removed") + } if willFreeInode { // An open handle may write the file after the name is gone, so free what the inode maps when it is freed. diff --git a/internal/goclusterfs/name_order_test.go b/internal/goclusterfs/name_order_test.go index a8ae5740..581fc796 100644 --- a/internal/goclusterfs/name_order_test.go +++ b/internal/goclusterfs/name_order_test.go @@ -191,6 +191,25 @@ func TestRemoveRacesPeerRecreate(t *testing.T) { } } +// An open handle writes the file after unlink removes its name: the free gives back what the inode maps then. +func TestUnlinkFreesBlocksWrittenAfterNameRemoved(t *testing.T) { + fsys, _, _, _ := journaledFS(t) + victim := createFile(t, fsys, "victim") + writeAt(t, victim, pattern(1, BlockSize), 0) + afterCommit = func(op string) { + if op == "unlink: name removed" { + afterCommit = nil + writeAt(t, victim, pattern(2, BlockSize), BlockSize) + } + } + t.Cleanup(func() { afterCommit = nil }) + + if errno := fsys.RootDir().Unlink(context.Background(), "victim"); errno != 0 { + t.Fatalf("unlink: %v", errno) + } + expectCleanFsck(t, fsys) +} + // A create or mkdir whose name is taken, or whose directory another node removed, reports EEXIST or ENOENT and gives // back the inode it claimed. func TestCreateNameErrors(t *testing.T) { diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index a714d142..7c803d26 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -1002,6 +1002,13 @@ scenarios: docker: TODO # needs a pause between one node's lookup and its removal, which only the Go test hook gives vm: TODO # the race window is too short to hit on purpose without that hook + - id: unlink-frees-blocks-written-after-name-removed + name: Unlink frees blocks written after its name was removed + description: An open handle writes a new block between unlink removing the name and freeing the inode; the free gives back every block, and fsck is clean + priority: go_only + environments: + go: internal/goclusterfs/name_order_test.go::TestUnlinkFreesBlocksWrittenAfterNameRemoved + - id: create-name-errors name: A failed name add reports EEXIST or ENOENT description: Create and mkdir into a taken name return EEXIST, into a removed directory ENOENT, and give back the claimed inode From 94223c154b0f2312727676a84ea7ebb5413785a5 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Fri, 13 Nov 2026 13:12:19 -0600 Subject: [PATCH 8/9] fix(fs): let unlink proceed when another node only wrote the file Every write raises a file's generation, so unlink of a file another node kept writing ran out of retries with EAGAIN. A file now only has to keep its type and link count; a directory still has to be unchanged, since an entry added to it changes its generation. Co-Authored-By: Claude Opus 5.5 --- internal/goclusterfs/fs.go | 10 ++++- internal/goclusterfs/name_order_test.go | 51 +++++++++++++++++++++++++ tests/test-manifest.yaml | 14 +++++++ 3 files changed, 74 insertions(+), 1 deletion(-) diff --git a/internal/goclusterfs/fs.go b/internal/goclusterfs/fs.go index c36ab3fd..3fcec01c 100644 --- a/internal/goclusterfs/fs.go +++ b/internal/goclusterfs/fs.go @@ -1318,6 +1318,14 @@ func (d *Dir) removeEntry(ctx context.Context, name string, isDir bool) syscall. return retryRace("Remove", name, func() syscall.Errno { return d.removeEntryOnce(ctx, name, isDir) }) } +// sameForRemoval reports whether cur still matches the removal's checks: a file's writes don't count, a directory's new entries do. +func sameForRemoval(was, cur *inode.Inode) bool { + if cur.Magic != inode.InodeMagic || cur.Mode&inode.S_IFMT != was.Mode&inode.S_IFMT || cur.LinkCount != was.LinkCount { + return false + } + return !was.IsDir() || cur.MetaGeneration == was.MetaGeneration +} + // removeEntryOnce returns EAGAIN when another node changes the child between the lookup and the removal. func (d *Dir) removeEntryOnce(ctx context.Context, name string, isDir bool) syscall.Errno { // Invalidate parent directory cache before modifying @@ -1454,7 +1462,7 @@ func (d *Dir) removeEntryOnce(ctx context.Context, name string, isDir bool) sysc } cur, err := d.fs.inodeTbl.Read(uint32(child)) readFailed = err != nil - changed = err == nil && cur.MetaGeneration != childIno.MetaGeneration + changed = err == nil && !sameForRemoval(childIno, cur) return err == nil && !changed }) switch { diff --git a/internal/goclusterfs/name_order_test.go b/internal/goclusterfs/name_order_test.go index 581fc796..0421f757 100644 --- a/internal/goclusterfs/name_order_test.go +++ b/internal/goclusterfs/name_order_test.go @@ -191,6 +191,57 @@ func TestRemoveRacesPeerRecreate(t *testing.T) { } } +// Another node writes the file each time unlink looks it up: the unlink still succeeds and frees every block. +func TestUnlinkSucceedsWhilePeerWrites(t *testing.T) { + a, path, _, _ := journaledFS(t) + b := openFormattedFS(t, path) + victim := createFile(t, a, "victim") + writer := &File{fs: b, inodeNum: victim.inodeNum} + writes := 0 + afterCommit = func(op string) { + if op == "unlink" { + writes++ + writeAt(t, writer, pattern(byte(writes), BlockSize), int64(writes)*BlockSize) + } + } + t.Cleanup(func() { afterCommit = nil }) + + if errno := a.RootDir().Unlink(context.Background(), "victim"); errno != 0 { + t.Fatalf("unlink while another node writes: %v", errno) + } + expectCleanFsck(t, b) +} + +// Another node adds a file to a directory between rmdir's emptiness check and its removal: rmdir reports ENOTEMPTY. +func TestRmdirRacesPeerAddingEntry(t *testing.T) { + a, path, _, _ := journaledFS(t) + b := openFormattedFS(t, path) + sub := newSubdir(t, a, "sub") + afterCommit = func(op string) { + if op != "unlink" { + return + } + afterCommit = nil + subIno, err := b.inodeTbl.Read(sub.inodeNum) + if err != nil { + t.Fatal(err) + } + inner := newRegularFile(t, b) + if err := b.dirMgr.Add(context.Background(), subIno, "inner", uint64(inner.inodeNum), dir.DT_REG); err != nil { + t.Fatal(err) + } + } + t.Cleanup(func() { afterCommit = nil }) + + if errno := a.RootDir().Rmdir(context.Background(), "sub"); errno != syscall.ENOTEMPTY { + t.Errorf("rmdir = %v, want %v", errno, syscall.ENOTEMPTY) + } + if _, ok := expectOneNamePerInode(t, b)["sub"]; !ok { + t.Error("the directory lost its name") + } + expectCleanFsck(t, b) +} + // An open handle writes the file after unlink removes its name: the free gives back what the inode maps then. func TestUnlinkFreesBlocksWrittenAfterNameRemoved(t *testing.T) { fsys, _, _, _ := journaledFS(t) diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index 7c803d26..15474398 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -1002,6 +1002,20 @@ scenarios: docker: TODO # needs a pause between one node's lookup and its removal, which only the Go test hook gives vm: TODO # the race window is too short to hit on purpose without that hook + - id: unlink-succeeds-while-peer-writes + name: Unlink succeeds while another node writes the file + description: Another node writes the file every time unlink looks it up; the unlink succeeds, every block is freed, and fsck is clean + priority: go_only + environments: + go: internal/goclusterfs/name_order_test.go::TestUnlinkSucceedsWhilePeerWrites + + - id: rmdir-races-peer-adding-entry + name: Rmdir keeps a directory another node adds an entry to + description: Another node adds an entry to the directory between rmdir's emptiness check and its removal; rmdir returns ENOTEMPTY and the directory keeps its name + priority: go_only + environments: + go: internal/goclusterfs/name_order_test.go::TestRmdirRacesPeerAddingEntry + - id: unlink-frees-blocks-written-after-name-removed name: Unlink frees blocks written after its name was removed description: An open handle writes a new block between unlink removing the name and freeing the inode; the free gives back every block, and fsck is clean From ba7840ec622fc8ceb329682a236655d77a766a26 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Fri, 13 Nov 2026 14:19:18 -0600 Subject: [PATCH 9/9] fix(inode): release the Update lock through t.unlock, as #95 does Co-Authored-By: Claude Opus 5.5 --- internal/inode/inode.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/internal/inode/inode.go b/internal/inode/inode.go index 7ee9b4fa..aafb0f60 100644 --- a/internal/inode/inode.go +++ b/internal/inode/inode.go @@ -257,7 +257,7 @@ func (t *Table) lockedUpdate(ctx context.Context, num uint32, change func(*Inode if err := t.lockMgr.Acquire(ctx, lock.LockTypeInode, uint64(num), lock.LockModeExclusive); err != nil { return nil, fmt.Errorf("failed to acquire inode lock: %w", err) } - defer t.lockMgr.Release(lock.LockTypeInode, uint64(num)) + defer t.unlock(lock.LockTypeInode, uint64(num)) } unlockSector, err := t.lockSector(num) if err != nil {