From 33332f601660600a785e72cb59785ab4639bc212 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 16:53:12 -0600 Subject: [PATCH 01/11] test(history): describe the stale-open and torn-read rules as they are Co-Authored-By: Claude Opus 5.5 --- tests/integration/history/history.go | 2 +- tests/test-manifest.yaml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/integration/history/history.go b/tests/integration/history/history.go index 95f2eb57..46e4f076 100644 --- a/tests/integration/history/history.go +++ b/tests/integration/history/history.go @@ -214,7 +214,7 @@ func Check(ops []Op, r Rules) []Violation { case o.Result == Failed: out = append(out, Violation{What: o.Kind.String() + " failed", Op: o, Allowed: "success, ENOENT or EEXIST", History: []Op{o}}) case o.Result == Stale && !churned(o, logs[o.Name], r): - out = append(out, Violation{What: "open failed with ESTALE while its name kept its file", Op: o, Allowed: "ESTALE only when the name lost its file at least twice during the call", History: []Op{o}}) + out = append(out, Violation{What: "open failed with ESTALE while its name kept its file", Op: o, Allowed: "ESTALE only when the name lost its file twice within the cache window and the call, once during the call", History: []Op{o}}) case o.Kind == Write && o.Result == Exists: out = append(out, Violation{What: "open with O_CREAT and no O_EXCL failed with EEXIST", Op: o, Allowed: "the file opened", History: []Op{o}}) case o.Result != OK && o.Result != NotFound && o.Result != Exists: diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index 6619a51c..d35519e8 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -4685,7 +4685,7 @@ scenarios: - id: history-checker-torn-read name: Coherence checker flags torn reads - description: A read that mixes values mid-sector, or with no write on its node overlapping, is a violation + description: A torn read is a violation when no write on the reader's node overlapped it, when a split falls off an 8-byte boundary outside one mixed word, or when a piece holds a value the read may not see priority: go_only environments: go: history/history_test.go::TestTornRead From 1b5975d712f472456b50cd408f747f0950bae26d Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 14:40:29 -0600 Subject: [PATCH 02/11] feat(lock): expose a lock's record, so callers can take several locks of one type in order Co-Authored-By: Claude Opus 5.5 --- internal/lock/lock.go | 5 +++++ internal/lock/type_records_test.go | 24 ++++++++++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/internal/lock/lock.go b/internal/lock/lock.go index c896bbbb..05fc7d8c 100644 --- a/internal/lock/lock.go +++ b/internal/lock/lock.go @@ -890,6 +890,11 @@ func (m *Manager) checkLockOrder(key lockKey, recordID, gid uint64) error { return nil } +// Record returns the record a lock maps to; take several locks of one type in increasing record order. +func (m *Manager) Record(lockType LockType, resourceID uint64) uint64 { + return m.hashToRecord(lockType, resourceID) +} + // hashToRecord maps a lock to a record position. func (m *Manager) hashToRecord(lockType LockType, resourceID uint64) uint64 { // Simple hash combining type and resource ID diff --git a/internal/lock/type_records_test.go b/internal/lock/type_records_test.go index 4c7be0a4..a63cc8ae 100644 --- a/internal/lock/type_records_test.go +++ b/internal/lock/type_records_test.go @@ -1,6 +1,7 @@ package lock import ( + "cmp" "errors" "fmt" "maps" @@ -172,6 +173,29 @@ func TestLocksOfOneTypeAreTakenInRecordOrder(t *testing.T) { release(t, m, LockTypeInode, low) } +func TestLocksSortedByRecordAreAcquiredTogether(t *testing.T) { + const region = layoutRows * RowSize + dev, cleanup := createTestDevice(t, region) + defer cleanup() + m := newTestNode(dev, 0, region, 1) + + ids := []uint64{2, 900, 37, 4096, 11, 513, 64, 7} + for id := uint64(100); len(ids) < 10; id++ { + if m.Record(LockTypeInode, id) == m.Record(LockTypeInode, ids[0]) { + ids = append(ids, id) + } + } + slices.SortFunc(ids, func(a, b uint64) int { + return cmp.Or(cmp.Compare(m.Record(LockTypeInode, a), m.Record(LockTypeInode, b)), cmp.Compare(a, b)) + }) + for _, id := range ids { + requireAcquired(t, m, LockTypeInode, id, LockModeExclusive) + } + for _, id := range slices.Backward(ids) { + release(t, m, LockTypeInode, id) + } +} + func TestAcquireRefusesUnknownLockType(t *testing.T) { const region = layoutRows * RowSize dev, cleanup := createTestDevice(t, region) From b8d02b51c1d1b3c8bd11103617a245322a2d9cc1 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 14:46:13 -0600 Subject: [PATCH 03/11] feat(dir): rename an entry within a directory or between two Rename takes the stripes of every directory whose entries change, the volume lock when a directory changes parent, and the inode locks in lock-record order. Under them it rechecks both names, the types, an emptied target and the move-into-itself case. It runs the caller's commit, then writes both parent inodes before any chunk. A directory that changes parent gets its ".." rewritten and both link counts adjusted. A full new parent grows by an empty block first. Co-Authored-By: Claude Opus 5.5 --- internal/dir/dir.go | 24 ++- internal/dir/index.go | 11 +- internal/dir/rename.go | 414 ++++++++++++++++++++++++++++++++++++ internal/dir/rename_test.go | 281 ++++++++++++++++++++++++ 4 files changed, 722 insertions(+), 8 deletions(-) create mode 100644 internal/dir/rename.go create mode 100644 internal/dir/rename_test.go diff --git a/internal/dir/dir.go b/internal/dir/dir.go index 91a917fb..f5fb7ccf 100644 --- a/internal/dir/dir.go +++ b/internal/dir/dir.go @@ -573,6 +573,20 @@ func removeFromChunk(chunk []byte, name string) bool { // as it needs to allocate blocks (bitmap lock) before acquiring the inode lock. // Returns errRetryAdd if a concurrent modification is detected and the caller should retry. func (m *Manager) addToNewBlock(ctx context.Context, dirIno *inode.Inode, entry *DirEntry) error { + return m.appendBlock(ctx, dirIno, + func(next uint64) (*inode.Inode, bool, error) { + return m.addToExistingChunk(dirIno.InodeNum, entry, next) + }, + func(data []byte) { + // The entry takes the first chunk (DirChunkSize) to keep RecLen in uint16 range + entry.RecLen = uint16(DirChunkSize) + serializeEntry(entry, data) + }) +} + +// appendBlock maps a new block, filled by fill, as directory dirIno's next one. Under the directory lock, settled +// may report that another goroutine or node made the block unneeded. +func (m *Manager) appendBlock(ctx context.Context, dirIno *inode.Inode, settled func(nextBlock uint64) (*inode.Inode, bool, error), fill func(data []byte)) error { // Calculate next block number based on current (possibly stale) size nextBlock := extent.BlocksForSize(dirIno.Size) @@ -622,11 +636,11 @@ func (m *Manager) addToNewBlock(ctx context.Context, dirIno *inode.Inode, entry } // Another node may have added this name, or blocks with room for it, while the block was allocated. - freshIno, added, err := m.addToExistingChunk(dirIno.InodeNum, entry, nextBlock) + freshIno, done, err := settled(nextBlock) if err != nil { return err } - if added { + if done { dirIno.Size = freshIno.Size return nil } @@ -647,12 +661,8 @@ func (m *Manager) addToNewBlock(ctx context.Context, dirIno *inode.Inode, entry return fmt.Errorf("failed to check extent: %w", extErr) } - // The entry takes the first chunk (DirChunkSize) to keep RecLen in uint16 range - entry.RecLen = uint16(DirChunkSize) - - // Write the entry data to the allocated physical block data := make([]byte, BlockSize) - serializeEntry(entry, data) + fill(data) if err := m.writeBlock(physBlock, data); err != nil { return err diff --git a/internal/dir/index.go b/internal/dir/index.go index 39345812..11dc930f 100644 --- a/internal/dir/index.go +++ b/internal/dir/index.go @@ -264,6 +264,15 @@ func (m *Manager) physicalBlock(dirIno *inode.Inode, block uint64) (uint64, erro // editChunk writes the directory inode before the chunk, so no node can read the change under the old generation. func (m *Manager) editChunk(dirIno *inode.Inode, x *index, at uint32, edit func(chunk []byte) bool, update func(names map[string]slot)) (changed bool, err error) { + return m.changeChunk(dirIno, x, at, edit, update, func() error { return m.inodeTbl.WriteWithLockHeld(dirIno) }) +} + +// rewriteChunk edits a chunk of a directory whose inode the caller already wrote under the same locks. +func (m *Manager) rewriteChunk(dirIno *inode.Inode, x *index, at uint32, edit func(chunk []byte) bool, update func(names map[string]slot)) (changed bool, err error) { + return m.changeChunk(dirIno, x, at, edit, update, func() error { return nil }) +} + +func (m *Manager) changeChunk(dirIno *inode.Inode, x *index, at uint32, edit func(chunk []byte) bool, update func(names map[string]slot), writeInode func() error) (changed bool, err error) { physBlock, err := m.physicalBlock(dirIno, uint64(at/chunksPerBlock)) if err != nil { return false, err @@ -277,7 +286,7 @@ func (m *Manager) editChunk(dirIno *inode.Inode, x *index, at uint32, edit func( m.indexes.drop(x.num) return false, nil } - if err := m.inodeTbl.WriteWithLockHeld(dirIno); err != nil { + if err := writeInode(); err != nil { m.indexes.drop(x.num) return false, fmt.Errorf("failed to write directory inode: %w", err) } diff --git a/internal/dir/rename.go b/internal/dir/rename.go new file mode 100644 index 00000000..14b6306e --- /dev/null +++ b/internal/dir/rename.go @@ -0,0 +1,414 @@ +package dir + +import ( + "cmp" + "context" + "encoding/binary" + "errors" + "fmt" + "slices" + "time" + + "github.com/supporttools/GoClusterFS/internal/extent" + "github.com/supporttools/GoClusterFS/internal/inode" + "github.com/supporttools/GoClusterFS/internal/lock" +) + +var ( + // ErrRaced means a name the caller looked up changed before the rename took its locks; look up again and retry. + ErrRaced = errors.New("a name changed since it was looked up") + ErrIsDir = errors.New("is a directory") + ErrIntoSelf = errors.New("cannot move a directory into itself") + errNeedsRoom = errors.New("the new parent has no room for the name") +) + +// RenameOp is a rename the caller looked up without locks. +type RenameOp struct { + OldParent, NewParent uint32 + OldName, NewName string + Child uint64 // what OldName named at the lookup + ChildIsDir bool + Target uint64 // what NewName named at the lookup, or 0 + TargetIsDir bool + NoReplace bool + Commit func(*Renamed) error // runs under every lock before anything changes; an error changes nothing +} + +// Renamed is what a rename changes, read under its locks before the change. +type Renamed struct { + OldParent, NewParent *inode.Inode + Child *inode.Inode + Target *inode.Inode // nil when NewName was free + ChildType uint8 + TargetType uint8 + MovesDir bool // a directory changes parent: its ".." and both link counts change +} + +// Rename points NewName at Child and removes OldName. It replaces Target, which the caller frees or drops a link from. +func (m *Manager) Rename(ctx context.Context, op *RenameOp) (*Renamed, error) { + if op.OldName == "" || op.NewName == "" { + return nil, ErrNameEmpty + } + if len(op.NewName) > MaxNameLen { + return nil, ErrNameTooLong + } + for attempt := 0; ; attempt++ { + r, err := m.renameLocked(ctx, op) + if !errors.Is(err, errNeedsRoom) { + return r, err + } + if attempt >= MaxAddRetries { + return nil, fmt.Errorf("directory %d kept filling while a rename waited for room", op.NewParent) + } + if err := m.grow(ctx, op.NewParent, entrySize(&DirEntry{NameLen: uint8(len(op.NewName))})); err != nil { + return nil, err + } + } +} + +func (m *Manager) renameLocked(ctx context.Context, op *RenameOp) (*Renamed, error) { + dirs := []uint32{op.OldParent, op.NewParent} + if op.ChildIsDir { + dirs = append(dirs, uint32(op.Child)) + } + if op.Target != 0 && op.TargetIsDir { + dirs = append(dirs, uint32(op.Target)) + } + defer m.lockStripes(dirs)() + + movesDir := op.ChildIsDir && op.OldParent != op.NewParent + if m.lockMgr != nil { + if movesDir { + // Two moves that each pass the ancestor check could together form a cycle; the volume lock runs them one at a time. + if err := m.lockMgr.AcquireVolume(ctx, lock.LockModeExclusive); err != nil { + return nil, fmt.Errorf("failed to acquire the volume lock: %w", err) + } + defer func() { _ = m.lockMgr.ReleaseVolume() }() + } + inodes := []uint32{op.OldParent, op.NewParent, uint32(op.Child)} + if op.Target != 0 { + inodes = append(inodes, uint32(op.Target)) + } + unlock, err := m.lockInodes(ctx, inodes) + if err != nil { + return nil, err + } + defer unlock() + } + + p, err := m.checkRename(op, movesDir) + if err != nil || p == nil { + return nil, err + } + if err := op.Commit(p.r); err != nil { + return nil, err + } + return p.r, m.applyRename(op, p) +} + +// renamePlan is what checkRename found under the locks: where each name is, and the chunk with room for NewName. +type renamePlan struct { + r *Renamed + oldX, newX *index + child, named slot + at uint32 +} + +// checkRename rereads what op's lookups found; a nil plan means both names already name the child. +func (m *Manager) checkRename(op *RenameOp, movesDir bool) (*renamePlan, error) { + oldDir, err := m.liveDirLocked(op.OldParent) + if errors.Is(err, ErrRemoved) { + return nil, ErrNotFound + } + if err != nil { + return nil, err + } + newDir := oldDir + if op.NewParent != op.OldParent { + if newDir, err = m.liveDirLocked(op.NewParent); errors.Is(err, ErrRemoved) { + return nil, ErrNotFound + } else if err != nil { + return nil, err + } + } + oldX, err := m.indexOf(oldDir) + if err != nil { + return nil, err + } + newX, err := m.indexOf(newDir) + if err != nil { + return nil, err + } + + cs, found, _ := oldX.find(oldDir.MetaGeneration, op.OldName) + switch { + case !found: + return nil, ErrNotFound + case cs.child != op.Child || (cs.ftype == DT_DIR) != op.ChildIsDir: + return nil, ErrRaced + } + ts, replacing, _ := newX.find(newDir.MetaGeneration, op.NewName) + switch { + case replacing && ts.child == cs.child: + return nil, nil + case replacing && op.NoReplace: + return nil, ErrExists + case replacing != (op.Target != 0), replacing && (ts.child != op.Target || (ts.ftype == DT_DIR) != op.TargetIsDir): + return nil, ErrRaced + case replacing && op.ChildIsDir && !op.TargetIsDir: + return nil, ErrNotDirectory + case replacing && !op.ChildIsDir && op.TargetIsDir: + return nil, ErrIsDir + } + + r := &Renamed{OldParent: oldDir, NewParent: newDir, ChildType: cs.ftype, MovesDir: movesDir} + p := &renamePlan{r: r, oldX: oldX, newX: newX, child: cs, named: ts} + if r.Child, err = m.liveInode(op.Child, op.ChildIsDir); err != nil { + return nil, err + } + if replacing { + r.TargetType = ts.ftype + if r.Target, err = m.liveInode(op.Target, op.TargetIsDir); err != nil { + return nil, err + } + if op.TargetIsDir { + if empty, err := m.emptyLocked(r.Target); err != nil || !empty { + return nil, cmp.Or(err, ErrNotEmpty) + } + } + } + if movesDir { + if inside, err := m.isAncestor(uint32(op.Child), op.NewParent); err != nil || inside { + return nil, cmp.Or(err, ErrIntoSelf) + } + } + if !replacing { + var ok bool + if p.at, ok = newX.chunkWithRoom(entrySize(&DirEntry{NameLen: uint8(len(op.NewName))})); !ok { + return nil, errNeedsRoom + } + } + return p, nil +} + +// applyRename writes both parents before any chunk, so a reader that sees a changed entry sees a changed generation. +func (m *Manager) applyRename(op *RenameOp, p *renamePlan) error { + r, oldDir, newDir := p.r, p.r.OldParent, p.r.NewParent + if r.MovesDir { + oldDir.LinkCount-- + newDir.LinkCount++ + } + if r.Target != nil && op.TargetIsDir { + newDir.LinkCount-- + } + now := time.Now().UnixNano() + for _, d := range slices.Compact([]*inode.Inode{oldDir, newDir}) { + d.Mtime, d.Ctime = now, now + if err := m.inodeTbl.WriteWithLockHeld(d); err != nil { + m.indexes.drop(oldDir.InodeNum) + m.indexes.drop(newDir.InodeNum) + return fmt.Errorf("failed to write directory inode %d: %w", d.InodeNum, err) + } + } + if r.MovesDir { + r.Child.Ctime = now + childX, err := m.indexOf(r.Child) + if err != nil { + return err + } + dots, found, _ := childX.find(r.Child.MetaGeneration, "..") + if !found { + return fmt.Errorf("%w: directory %d has no \"..\"", ErrCorrupted, r.Child.InodeNum) + } + if _, err := m.editChunk(r.Child, childX, dots.chunk, + func(chunk []byte) bool { return pointName(chunk, "..", uint64(newDir.InodeNum), DT_DIR) }, + func(names map[string]slot) { + names[".."] = slot{child: uint64(newDir.InodeNum), chunk: dots.chunk, ftype: DT_DIR} + }); err != nil { + return err + } + } + + entry := &DirEntry{InodeNum: op.Child, NameLen: uint8(len(op.NewName)), FileType: r.ChildType, Name: op.NewName} + entry.RecLen = entrySize(entry) + at, place := p.at, func(chunk []byte) bool { return addToChunk(chunk, entry) } + if r.Target != nil { + at, place = p.named.chunk, func(chunk []byte) bool { return pointName(chunk, op.NewName, op.Child, r.ChildType) } + } + named := slot{child: op.Child, chunk: at, ftype: r.ChildType} + oldX, newX, cs := p.oldX, p.newX, p.child + if oldDir == newDir && cs.chunk == at { + _, err := m.rewriteChunk(newDir, newX, at, + func(chunk []byte) bool { return place(chunk) && removeFromChunk(chunk, op.OldName) }, + func(names map[string]slot) { names[op.NewName] = named; delete(names, op.OldName) }) + return err + } + if _, err := m.rewriteChunk(newDir, newX, at, place, func(names map[string]slot) { names[op.NewName] = named }); err != nil { + return err + } + _, err := m.rewriteChunk(oldDir, oldX, cs.chunk, + func(chunk []byte) bool { return removeFromChunk(chunk, op.OldName) }, + func(names map[string]slot) { delete(names, op.OldName) }) + return err +} + +// lockStripes takes each local stripe of dirs once, in stripe order, since a stripe is not reentrant. +func (m *Manager) lockStripes(dirs []uint32) func() { + stripes := make([]uint32, 0, len(dirs)) + for _, d := range dirs { + stripes = append(stripes, d%dirStripes) + } + slices.Sort(stripes) + stripes = slices.Compact(stripes) + for _, s := range stripes { + m.dirMu[s].Lock() + } + return func() { + for _, s := range slices.Backward(stripes) { + m.dirMu[s].Unlock() + } + } +} + +// lockInodes takes exclusive inode locks on nums in lock-record order, so nodes never wait on each other in a cycle. +func (m *Manager) lockInodes(ctx context.Context, nums []uint32) (func(), error) { + slices.SortFunc(nums, func(a, b uint32) int { + return cmp.Or(cmp.Compare(m.lockMgr.Record(lock.LockTypeInode, uint64(a)), m.lockMgr.Record(lock.LockTypeInode, uint64(b))), cmp.Compare(a, b)) + }) + nums = slices.Compact(nums) + held := 0 + unlock := func() { + for _, n := range slices.Backward(nums[:held]) { + m.unlock(n) + } + } + for _, n := range nums { + if err := m.lockMgr.Acquire(ctx, lock.LockTypeInode, uint64(n), lock.LockModeExclusive); err != nil { + unlock() + return nil, fmt.Errorf("failed to acquire the lock of inode %d: %w", n, err) + } + held++ + } + return unlock, nil +} + +// liveInode rereads num under its lock; a different type or a freed slot means the name changed since the lookup. +func (m *Manager) liveInode(num uint64, isDir bool) (*inode.Inode, error) { + ino, err := m.inodeTbl.Read(uint32(num)) + if err != nil { + return nil, fmt.Errorf("failed to read inode %d: %w", num, err) + } + if ino.Magic != inode.InodeMagic || ino.IsDir() != isDir { + return nil, ErrRaced + } + return ino, nil +} + +func (m *Manager) emptyLocked(dirIno *inode.Inode) (bool, error) { + x, err := m.indexOf(dirIno) + if err != nil { + return false, err + } + x.mu.RLock() + defer x.mu.RUnlock() + for name := range x.names { + if name != "." && name != ".." { + return false, nil + } + } + return true, nil +} + +// isAncestor reports whether directory dir is of or above it; the volume lock keeps every ".." on the way still. +func (m *Manager) isAncestor(dir, of uint32) (bool, error) { + cur := of + for range 1 << 16 { + if cur == dir { + return true, nil + } + if cur == inode.InodeRoot { + return false, nil + } + parent, err := m.parentOf(cur) + if err != nil { + return false, err + } + cur = parent + } + return false, fmt.Errorf("%w: no path to the root from directory %d", ErrCorrupted, of) +} + +// parentOf reads the ".." of directory num, which InitDirectory puts in its first chunk. +func (m *Manager) parentOf(num uint32) (uint32, error) { + ino, err := m.inodeTbl.Read(num) + if err != nil { + return 0, fmt.Errorf("failed to read directory inode %d: %w", num, err) + } + if ino.Magic != inode.InodeMagic || !ino.IsDir() { + return 0, fmt.Errorf("%w: inode %d on the way to the root is not a directory", ErrCorrupted, num) + } + physBlock, err := m.physicalBlock(ino, 0) + if err != nil { + return 0, err + } + chunk, err := m.readChunk(physBlock, 0) + if err != nil { + return 0, err + } + var parent uint64 + walkChunk(chunk, func(_ int, e *DirEntry) bool { + if e.InodeNum != 0 && e.Name == ".." { + parent = e.InodeNum + return true + } + return false + }) + if parent == 0 { + return 0, fmt.Errorf("%w: directory %d has no \"..\"", ErrCorrupted, num) + } + return uint32(parent), nil +} + +// pointName makes the live entry name in chunk name child, of type ftype. +func pointName(chunk []byte, name string, child uint64, ftype uint8) bool { + pointed := false + walkChunk(chunk, func(offset int, e *DirEntry) bool { + if e.InodeNum == 0 || e.Name != name { + return false + } + binary.LittleEndian.PutUint64(chunk[offset:], child) + chunk[offset+11] = ftype + pointed = true + return true + }) + return pointed +} + +// grow gives directory num an empty block unless it already has a chunk with room for need. +func (m *Manager) grow(ctx context.Context, num uint32, need uint16) error { + defer m.LockDir(num)() + for attempt := 0; attempt < MaxAddRetries; attempt++ { + fresh, err := m.liveDirLocked(num) + if err != nil { + return err + } + err = m.appendBlock(ctx, fresh, + func(next uint64) (*inode.Inode, bool, error) { + cur, err := m.liveDirLocked(num) + if err != nil || extent.BlocksForSize(cur.Size) <= next { + return cur, false, err + } + x, err := m.indexOf(cur) + if err != nil { + return nil, false, err + } + _, ok := x.chunkWithRoom(need) + return cur, ok, nil + }, + func([]byte) {}) + if !errors.Is(err, errRetryAdd) { + return err + } + } + return fmt.Errorf("directory %d kept changing while it grew", num) +} diff --git a/internal/dir/rename_test.go b/internal/dir/rename_test.go new file mode 100644 index 00000000..7323f8f3 --- /dev/null +++ b/internal/dir/rename_test.go @@ -0,0 +1,281 @@ +package dir + +import ( + "context" + "errors" + "fmt" + "strings" + "testing" + + "github.com/supporttools/GoClusterFS/internal/inode" +) + +func newNode(t *testing.T, m *Manager, mode uint16) *inode.Inode { + t.Helper() + ctx := context.Background() + num, err := m.inodeTbl.FindFreeSlot() + if err != nil { + t.Fatal(err) + } + ino, err := m.inodeTbl.ClaimSlot(ctx, num) + if err != nil { + t.Fatal(err) + } + ino.Mode = mode + ino.LinkCount = 1 + if err := m.inodeTbl.Write(ctx, ino); err != nil { + t.Fatal(err) + } + return ino +} + +func newFileIn(t *testing.T, m *Manager, parent *inode.Inode, name string) *inode.Inode { + t.Helper() + ino := newNode(t, m, inode.S_IFREG|0o644) + if err := m.Add(context.Background(), parent, name, uint64(ino.InodeNum), DT_REG); err != nil { + t.Fatalf("add %s: %v", name, err) + } + return ino +} + +// newDirIn makes a directory whose ".." names parent, and names it in parent. +func newDirIn(t *testing.T, m *Manager, parent *inode.Inode, name string) *inode.Inode { + t.Helper() + ctx := context.Background() + num, err := m.inodeTbl.FindFreeSlot() + if err != nil { + t.Fatal(err) + } + ino, err := m.inodeTbl.ClaimSlot(ctx, num) + if err != nil { + t.Fatal(err) + } + ino.Mode, ino.LinkCount = inode.S_IFDIR|0o755, 2 + if err := m.InitDirectory(ctx, ino, uint64(parent.InodeNum)); err != nil { + t.Fatal(err) + } + if err := m.Add(ctx, parent, name, uint64(ino.InodeNum), DT_DIR); err != nil { + t.Fatalf("add %s: %v", name, err) + } + return ino +} + +// renameOp builds the op a caller would after looking both names up. +func renameOp(t *testing.T, m *Manager, oldParent *inode.Inode, oldName string, newParent *inode.Inode, newName string) *RenameOp { + t.Helper() + ctx := context.Background() + child, ctype, err := m.Lookup(ctx, oldParent, oldName) + if err != nil { + t.Fatalf("lookup %s: %v", oldName, err) + } + op := &RenameOp{OldParent: oldParent.InodeNum, NewParent: newParent.InodeNum, OldName: oldName, NewName: newName, + Child: child, ChildIsDir: ctype == DT_DIR, Commit: func(*Renamed) error { return nil }} + if target, ttype, err := m.Lookup(ctx, newParent, newName); err == nil { + op.Target, op.TargetIsDir = target, ttype == DT_DIR + } + return op +} + +func reread(t *testing.T, m *Manager, ino *inode.Inode) *inode.Inode { + t.Helper() + fresh, err := m.inodeTbl.Read(ino.InodeNum) + if err != nil { + t.Fatal(err) + } + return fresh +} + +func expectName(t *testing.T, m *Manager, parent *inode.Inode, name string, want uint64) { + t.Helper() + got, err := lookupChild(m, reread(t, m, parent), name) + switch { + case want == 0 && !errors.Is(err, ErrNotFound): + t.Errorf("%q in directory %d: %d, %v; want no such name", name, parent.InodeNum, got, err) + case want != 0 && (err != nil || got != want): + t.Errorf("%q in directory %d: %d, %v; want inode %d", name, parent.InodeNum, got, err, want) + } +} + +func TestRenameInOneDirectory(t *testing.T) { + nodes, dirIno := newNodes(t, 1) + m := nodes[0] + f := newFileIn(t, m, dirIno, "a") + before := diskGen(t, m, dirIno.InodeNum) + + var committed []*Renamed + op := renameOp(t, m, dirIno, "a", dirIno, "b") + op.Commit = func(r *Renamed) error { committed = append(committed, r); return nil } + r, err := m.Rename(context.Background(), op) + if err != nil { + t.Fatal(err) + } + if len(committed) != 1 || r.Target != nil || r.MovesDir || r.OldParent.MetaGeneration <= before { + t.Errorf("committed %d times, target %v, moves dir %v, parent generation %d (was %d)", len(committed), r.Target, r.MovesDir, r.OldParent.MetaGeneration, before) + } + expectName(t, m, dirIno, "a", 0) + expectName(t, m, dirIno, "b", uint64(f.InodeNum)) +} + +func TestRenameReplacesATarget(t *testing.T) { + nodes, dirIno := newNodes(t, 1) + m := nodes[0] + a, b := newFileIn(t, m, dirIno, "a"), newFileIn(t, m, dirIno, "b") + + r, err := m.Rename(context.Background(), renameOp(t, m, dirIno, "a", dirIno, "b")) + if err != nil { + t.Fatal(err) + } + if r.Target == nil || r.Target.InodeNum != b.InodeNum || r.TargetType != DT_REG { + t.Errorf("replaced %+v, want inode %d", r.Target, b.InodeNum) + } + expectName(t, m, dirIno, "a", 0) + expectName(t, m, dirIno, "b", uint64(a.InodeNum)) + + newFileIn(t, m, dirIno, "c") + noReplace := renameOp(t, m, dirIno, "b", dirIno, "c") + noReplace.NoReplace = true + noReplace.Commit = func(*Renamed) error { t.Error("committed a rename NOREPLACE refused"); return nil } + if _, err := m.Rename(context.Background(), noReplace); !errors.Is(err, ErrExists) { + t.Errorf("rename over c with NOREPLACE: %v, want ErrExists", err) + } + expectName(t, m, dirIno, "b", uint64(a.InodeNum)) +} + +func TestRenameMovesADirectoryBetweenParents(t *testing.T) { + nodes, root := newNodes(t, 1) + m := nodes[0] + p1, p2 := newDirIn(t, m, root, "p1"), newDirIn(t, m, root, "p2") + d := newDirIn(t, m, p1, "d") + links1, links2 := reread(t, m, p1).LinkCount, reread(t, m, p2).LinkCount + + r, err := m.Rename(context.Background(), renameOp(t, m, reread(t, m, p1), "d", reread(t, m, p2), "e")) + if err != nil { + t.Fatal(err) + } + if !r.MovesDir { + t.Error("a move between parents did not count as moving a directory") + } + expectName(t, m, p1, "d", 0) + expectName(t, m, p2, "e", uint64(d.InodeNum)) + if parent, err := m.parentOf(d.InodeNum); err != nil || parent != p2.InodeNum { + t.Errorf("\"..\" of the moved directory: %d, %v; want %d", parent, err, p2.InodeNum) + } + expectName(t, m, d, "..", uint64(p2.InodeNum)) + if got1, got2 := reread(t, m, p1).LinkCount, reread(t, m, p2).LinkCount; got1 != links1-1 || got2 != links2+1 { + t.Errorf("link counts %d and %d, want %d and %d", got1, got2, links1-1, links2+1) + } +} + +func TestRenameRefusesAMoveIntoItself(t *testing.T) { + nodes, root := newNodes(t, 1) + m := nodes[0] + p := newDirIn(t, m, root, "p") + child := newDirIn(t, m, p, "child") + grandchild := newDirIn(t, m, child, "grandchild") + + for _, into := range []*inode.Inode{child, grandchild} { + op := renameOp(t, m, reread(t, m, root), "p", reread(t, m, into), "moved") + op.Commit = func(*Renamed) error { t.Error("committed a move into the directory's own subtree"); return nil } + if _, err := m.Rename(context.Background(), op); !errors.Is(err, ErrIntoSelf) { + t.Errorf("move p into directory %d: %v, want ErrIntoSelf", into.InodeNum, err) + } + } + expectName(t, m, root, "p", uint64(p.InodeNum)) +} + +func TestRenameChecksTargetTypes(t *testing.T) { + nodes, root := newNodes(t, 1) + m := nodes[0] + file := newFileIn(t, m, root, "file") + newDirIn(t, m, root, "dir") + full := newDirIn(t, m, root, "full") + newFileIn(t, m, full, "x") + empty := newDirIn(t, m, root, "empty") + + for _, c := range []struct { + from, to string + want error + }{ + {"dir", "file", ErrNotDirectory}, + {"file", "dir", ErrIsDir}, + {"dir", "full", ErrNotEmpty}, + } { + op := renameOp(t, m, reread(t, m, root), c.from, reread(t, m, root), c.to) + if _, err := m.Rename(context.Background(), op); !errors.Is(err, c.want) { + t.Errorf("rename %s over %s: %v, want %v", c.from, c.to, err, c.want) + } + } + expectName(t, m, root, "file", uint64(file.InodeNum)) + + links := reread(t, m, root).LinkCount + r, err := m.Rename(context.Background(), renameOp(t, m, reread(t, m, root), "dir", reread(t, m, root), "empty")) + if err != nil { + t.Fatalf("rename dir over an empty directory: %v", err) + } + if r.Target.InodeNum != empty.InodeNum || reread(t, m, root).LinkCount != links-1 { + t.Errorf("replaced inode %d, parent links %d; want inode %d, %d links", r.Target.InodeNum, reread(t, m, root).LinkCount, empty.InodeNum, links-1) + } +} + +func TestRenameAfterAStaleLookupChangesNothing(t *testing.T) { + nodes, dirIno := newNodes(t, 1) + m := nodes[0] + f := newFileIn(t, m, dirIno, "a") + op := renameOp(t, m, dirIno, "a", dirIno, "b") + newFileIn(t, m, dirIno, "b") + if _, err := m.Rename(context.Background(), op); !errors.Is(err, ErrRaced) { + t.Errorf("rename after b appeared: %v, want ErrRaced", err) + } + + op = renameOp(t, m, dirIno, "a", dirIno, "c") + op.Commit = func(*Renamed) error { return errors.New("journal full") } + if _, err := m.Rename(context.Background(), op); err == nil || !strings.Contains(err.Error(), "journal full") { + t.Errorf("rename with a failing commit: %v", err) + } + expectName(t, m, dirIno, "a", uint64(f.InodeNum)) + expectName(t, m, dirIno, "c", 0) +} + +// The other node's index follows the rename on its next lookup, since both generations change first. +func TestRenameIsSeenByAnotherNode(t *testing.T) { + nodes, dirIno := newNodes(t, 2) + a, b := nodes[0], nodes[1] + f := newFileIn(t, a, dirIno, "old") + expectName(t, b, dirIno, "old", uint64(f.InodeNum)) + expectName(t, b, dirIno, "new", 0) + + if _, err := a.Rename(context.Background(), renameOp(t, a, reread(t, a, dirIno), "old", reread(t, a, dirIno), "new")); err != nil { + t.Fatal(err) + } + expectName(t, b, dirIno, "old", 0) + expectName(t, b, dirIno, "new", uint64(f.InodeNum)) +} + +func TestRenameGrowsAFullNewParent(t *testing.T) { + nodes, root := newNodes(t, 1) + m := nodes[0] + from, to := newDirIn(t, m, root, "from"), newDirIn(t, m, root, "to") + f := newFileIn(t, m, from, "moving") + long := strings.Repeat("n", MaxNameLen-4) + for i := 0; ; i++ { + x, err := m.indexOf(reread(t, m, to)) + if err != nil { + t.Fatal(err) + } + if _, ok := x.chunkWithRoom(entrySize(&DirEntry{NameLen: MaxNameLen})); !ok { + break + } + mustAdd(t, m, reread(t, m, to), fmt.Sprintf("%s%04d", long, i), uint64(f.InodeNum)) + } + blocks := reread(t, m, to).Size / BlockSize + + name := strings.Repeat("m", MaxNameLen) + if _, err := m.Rename(context.Background(), renameOp(t, m, reread(t, m, from), "moving", reread(t, m, to), name)); err != nil { + t.Fatal(err) + } + if got := reread(t, m, to).Size / BlockSize; got != blocks+1 { + t.Errorf("the full parent has %d blocks after the rename, want %d", got, blocks+1) + } + expectName(t, m, to, name, uint64(f.InodeNum)) + expectName(t, m, from, "moving", 0) +} From 682720e146cd6b60062b2b2606e2ead751045d9b Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 14:49:42 -0600 Subject: [PATCH 04/11] feat(journal): log and replay a rename DirRename now carries both parents' generations, the moved inode's generation and type, and the replaced target, so replay finishes a committed rename: the new name ends up naming the moved inode, the old name goes, a moved directory's ".." follows, and each parent's link count changes only while the parent is as the rename logged it. If another node took the new name since, the old name stays and the target's own records in the transaction leave it alone. Co-Authored-By: Claude Opus 5.5 --- internal/dir/rename.go | 32 +++++ internal/dir/rename_test.go | 18 +++ internal/journal/cross_node_recovery.go | 151 +++++++++++++++++++++--- internal/journal/entries.go | 72 +++++------ internal/journal/transaction_test.go | 31 ++--- 5 files changed, 234 insertions(+), 70 deletions(-) diff --git a/internal/dir/rename.go b/internal/dir/rename.go index 14b6306e..90a0310c 100644 --- a/internal/dir/rename.go +++ b/internal/dir/rename.go @@ -412,3 +412,35 @@ func (m *Manager) grow(ctx context.Context, num uint32, need uint16) error { } return fmt.Errorf("directory %d kept changing while it grew", num) } + +// PointIf makes name in dirIno name child, of type ftype, if ok accepts the inode it names now; ok runs under the directory lock. +func (m *Manager) PointIf(ctx context.Context, dirIno *inode.Inode, name string, child uint64, ftype uint8, ok func(cur uint64) bool) (bool, error) { + if dirIno.Magic != inode.InodeMagic { + return false, ErrRemoved + } + defer m.LockDir(dirIno.InodeNum)() + if m.lockMgr != nil { + if err := m.lockMgr.Acquire(ctx, lock.LockTypeInode, uint64(dirIno.InodeNum), lock.LockModeExclusive); err != nil { + return false, fmt.Errorf("failed to acquire directory lock: %w", err) + } + defer m.unlock(dirIno.InodeNum) + } + fresh, err := m.liveDirLocked(dirIno.InodeNum) + if err != nil { + return false, err + } + x, err := m.indexOf(fresh) + if err != nil { + return false, err + } + s, found, _ := x.find(fresh.MetaGeneration, name) + if !found { + return false, ErrNotFound + } + if !ok(s.child) { + return false, nil + } + return m.editChunk(fresh, x, s.chunk, + func(chunk []byte) bool { return pointName(chunk, name, child, ftype) }, + func(names map[string]slot) { names[name] = slot{child: child, chunk: s.chunk, ftype: ftype} }) +} diff --git a/internal/dir/rename_test.go b/internal/dir/rename_test.go index 7323f8f3..f7557e31 100644 --- a/internal/dir/rename_test.go +++ b/internal/dir/rename_test.go @@ -279,3 +279,21 @@ func TestRenameGrowsAFullNewParent(t *testing.T) { expectName(t, m, to, name, uint64(f.InodeNum)) expectName(t, m, from, "moving", 0) } + +func TestPointIfOnlyMovesANameThatStillNamesTheExpectedInode(t *testing.T) { + nodes, dirIno := newNodes(t, 1) + m := nodes[0] + a, b := newFileIn(t, m, dirIno, "a"), newNode(t, m, inode.S_IFREG|0o644) + ctx := context.Background() + if pointed, err := m.PointIf(ctx, reread(t, m, dirIno), "a", uint64(b.InodeNum), DT_REG, func(cur uint64) bool { return cur == 999 }); err != nil || pointed { + t.Errorf("PointIf with a refusing check: %v, %v", pointed, err) + } + expectName(t, m, dirIno, "a", uint64(a.InodeNum)) + if pointed, err := m.PointIf(ctx, reread(t, m, dirIno), "a", uint64(b.InodeNum), DT_REG, func(cur uint64) bool { return cur == uint64(a.InodeNum) }); err != nil || !pointed { + t.Errorf("PointIf: %v, %v", pointed, err) + } + expectName(t, m, dirIno, "a", uint64(b.InodeNum)) + if _, err := m.PointIf(ctx, reread(t, m, dirIno), "missing", uint64(b.InodeNum), DT_REG, func(uint64) bool { return true }); !errors.Is(err, ErrNotFound) { + t.Errorf("PointIf of a missing name: %v, want ErrNotFound", err) + } +} diff --git a/internal/journal/cross_node_recovery.go b/internal/journal/cross_node_recovery.go index a5138dce..0bdf8e35 100644 --- a/internal/journal/cross_node_recovery.go +++ b/internal/journal/cross_node_recovery.go @@ -166,10 +166,19 @@ type txnReplay struct { owner [16]byte created map[uint64]bool wrote map[uint32]uint64 + keep map[uint64]bool // targets a replayed rename left named, so its records must not drop or free them } func newTxnReplay(owner [16]byte) *txnReplay { - return &txnReplay{owner: owner, created: make(map[uint64]bool), wrote: make(map[uint32]uint64)} + return &txnReplay{owner: owner, created: make(map[uint64]bool), wrote: make(map[uint32]uint64), keep: make(map[uint64]bool)} +} + +// kept reports whether a rename earlier in this transaction left inode num named, and logs why op skips it. +func (r *txnReplay) kept(op string, num uint64) bool { + if r.keep[num] { + log.Printf("[Recovery] %s: the rename did not replace inode %d, which keeps its name, skipping", op, num) + } + return r.keep[num] } // unchanged reports whether ino is in use and was last written before a record logged its MetaGeneration as gen, or @@ -196,6 +205,8 @@ func applyEntry(ctx context.Context, rctx *RecoveryContext, r *txnReplay, entry return applyLinkDrop(ctx, rctx, r, entry.Payload) case TxnOpDirAddEntry: return applyDirAddEntry(ctx, rctx, r, entry.Payload) + case TxnOpDirRename: + return applyDirRename(ctx, rctx, r, entry.Payload) case TxnOpDirRemoveEntry: return applyDirRemoveEntry(ctx, rctx, entry.Payload) case TxnOpBlockAlloc: @@ -315,6 +326,9 @@ func applyInodeDelete(ctx context.Context, rctx *RecoveryContext, r *txnReplay, if err != nil { return false, err } + if r.kept("InodeDelete", uint64(p.InodeNum)) { + return false, nil + } freed, err := rctx.InodeTbl.FreeIf(ctx, uint32(p.InodeNum), func(ino *inode.Inode) bool { return r.unchanged(ino, p.MetaGeneration) @@ -331,7 +345,10 @@ func applyInodeDelete(ctx context.Context, rctx *RecoveryContext, r *txnReplay, return true, nil } -var errLinkDropDone = errors.New("link drop already applied or overtaken") +var ( + errLinkDropDone = errors.New("link drop already applied or overtaken") + errParentChanged = errors.New("the parent changed since the rename logged it") +) // applyLinkDrop redoes a lost link drop; at zero links the file is listed as an orphan, or freed if nobody holds it. func applyLinkDrop(ctx context.Context, rctx *RecoveryContext, r *txnReplay, payload []byte) (bool, error) { @@ -339,6 +356,9 @@ func applyLinkDrop(ctx context.Context, rctx *RecoveryContext, r *txnReplay, pay if err != nil { return false, err } + if r.kept("LinkDrop", uint64(p.InodeNum)) { + return false, nil + } num := uint32(p.InodeNum) var holders uint64 if rctx.OrphanHolders != nil { @@ -424,20 +444,8 @@ func applyDirAddEntry(ctx context.Context, rctx *RecoveryContext, r *txnReplay, return false, err } - var fileType uint8 - switch p.EntryType { - case DirEntryRegular: - fileType = dir.DT_REG - case DirEntryDirectory: - fileType = dir.DT_DIR - case DirEntrySymlink: - fileType = dir.DT_LNK - default: - fileType = dir.DT_UNKNOWN - } - // Add checks that the parent is live and the name is free under the directory lock. - err = rctx.DirMgr.Add(ctx, parentIno, p.Name, p.ChildInodeNum, fileType) + err = rctx.DirMgr.Add(ctx, parentIno, p.Name, p.ChildInodeNum, fileTypeOf(p.EntryType)) switch { case errors.Is(err, dir.ErrExists): log.Printf("[Recovery] DirAddEntry: entry %q already exists in parent %d, skipping", p.Name, p.ParentInodeNum) @@ -462,6 +470,116 @@ func applyDirAddEntry(ctx context.Context, rctx *RecoveryContext, r *txnReplay, return true, nil } +func fileTypeOf(t DirEntryType) uint8 { + switch t { + case DirEntryRegular: + return dir.DT_REG + case DirEntryDirectory: + return dir.DT_DIR + case DirEntrySymlink: + return dir.DT_LNK + } + return dir.DT_UNKNOWN +} + +// applyDirRename finishes a committed rename: the new name ends up naming the child and the old name is gone, unless +// another node took the new name since, in which case the old name stays and the target keeps its name. +func applyDirRename(ctx context.Context, rctx *RecoveryContext, r *txnReplay, payload []byte) (bool, error) { + p, err := DeserializeDirRename(payload) + if err != nil { + return false, err + } + placed, err := placeRenamed(ctx, rctx, r, p) + if err != nil || !placed { + if p.TargetInodeNum != 0 { + r.keep[p.TargetInodeNum] = true + } + return false, err + } + if oldParent, ok, err := liveDir(rctx, p.OldParentInodeNum); err != nil { + return true, err + } else if ok { + if _, err := rctx.DirMgr.RemoveIf(ctx, oldParent, p.OldName, func(cur uint64) bool { return cur == p.InodeNum }); err != nil && !errors.Is(err, dir.ErrNotFound) && !errors.Is(err, dir.ErrRemoved) { + return true, fmt.Errorf("failed to remove the old name %q: %w", p.OldName, err) + } + } + if p.EntryType == DirEntryDirectory && p.OldParentInodeNum != p.NewParentInodeNum { + if child, ok, err := liveDir(rctx, p.InodeNum); err != nil { + return true, err + } else if ok { + if _, err := rctx.DirMgr.PointIf(ctx, child, "..", p.NewParentInodeNum, dir.DT_DIR, func(cur uint64) bool { return cur == p.OldParentInodeNum }); err != nil && !errors.Is(err, dir.ErrNotFound) && !errors.Is(err, dir.ErrRemoved) { + return true, fmt.Errorf("failed to point the \"..\" of directory %d at %d: %w", p.InodeNum, p.NewParentInodeNum, err) + } + } + } + log.Printf("[Recovery] DirRename: %q in %d is %q in %d", p.OldName, p.OldParentInodeNum, p.NewName, p.NewParentInodeNum) + return true, nil +} + +// placeRenamed makes the new name name the child, if it is free, names the child, or still names the logged target. +// Each parent's link count changes first, and only while the parent is as the rename logged it. +func placeRenamed(ctx context.Context, rctx *RecoveryContext, r *txnReplay, p *DirRenamePayload) (bool, error) { + newParent, ok, err := liveDir(rctx, p.NewParentInodeNum) + if !ok { + return false, err + } + cur, _, err := rctx.DirMgr.Lookup(ctx, newParent, p.NewName) + switch { + case err == nil && cur == p.InodeNum: + return true, nil + case err == nil && (p.TargetInodeNum == 0 || cur != p.TargetInodeNum): + log.Printf("[Recovery] DirRename: %q in %d names inode %d since, skipping", p.NewName, p.NewParentInodeNum, cur) + return false, nil + case err != nil && !errors.Is(err, dir.ErrNotFound): + return false, fmt.Errorf("failed to look up %q in %d: %w", p.NewName, p.NewParentInodeNum, err) + } + + movesDir := p.EntryType == DirEntryDirectory && p.OldParentInodeNum != p.NewParentInodeNum + links := map[uint64]int{} + gens := map[uint64]uint64{p.OldParentInodeNum: p.OldParentGen, p.NewParentInodeNum: p.NewParentGen} + if movesDir { + links[p.OldParentInodeNum]-- + links[p.NewParentInodeNum]++ + } + if p.TargetInodeNum != 0 && p.TargetType == DirEntryDirectory { + links[p.NewParentInodeNum]-- + } + for num, delta := range links { + if delta == 0 { + continue + } + var wrote bool + if err := rctx.InodeTbl.Update(ctx, uint32(num), func(parent *inode.Inode) error { + if wrote = r.unchanged(parent, gens[num]); !wrote { + return errParentChanged + } + parent.LinkCount = uint32(int64(parent.LinkCount) + int64(delta)) + return nil + }); err != nil && !errors.Is(err, errParentChanged) { + return false, fmt.Errorf("failed to update the link count of directory %d: %w", num, err) + } + if wrote { + if parent, err := rctx.InodeTbl.Read(uint32(num)); err == nil { + r.wrote[uint32(num)] = parent.MetaGeneration + } + } + } + + if errors.Is(err, dir.ErrNotFound) { + err = rctx.DirMgr.Add(ctx, newParent, p.NewName, p.InodeNum, fileTypeOf(p.EntryType)) + if errors.Is(err, dir.ErrExists) || errors.Is(err, dir.ErrRemoved) { + log.Printf("[Recovery] DirRename: %q in %d was taken or removed meanwhile, skipping", p.NewName, p.NewParentInodeNum) + return false, nil + } + return err == nil, err + } + pointed, err := rctx.DirMgr.PointIf(ctx, newParent, p.NewName, p.InodeNum, fileTypeOf(p.EntryType), func(cur uint64) bool { return cur == p.TargetInodeNum }) + if errors.Is(err, dir.ErrNotFound) || errors.Is(err, dir.ErrRemoved) { + return false, nil + } + return pointed, err +} + // liveDir reads the inode num and reports whether it is a directory in use. Another node may have removed it since // the record was logged. func liveDir(rctx *RecoveryContext, num uint64) (*inode.Inode, bool, error) { @@ -691,6 +809,9 @@ func applyExtentFree(ctx context.Context, rctx *RecoveryContext, r *txnReplay, p if err != nil { return false, err } + if r.kept("ExtentFree", uint64(p.InodeNum)) { + return false, nil + } return updateExtents(ctx, rctx, r, uint32(p.InodeNum), func(ino *inode.Inode) (bool, error) { if !r.unchanged(ino, p.MetaGeneration) { log.Printf("[Recovery] ExtentFree: inode %d is free or was written since, skipping", p.InodeNum) diff --git a/internal/journal/entries.go b/internal/journal/entries.go index 6b0c3e44..f9f4b2b0 100644 --- a/internal/journal/entries.go +++ b/internal/journal/entries.go @@ -361,66 +361,66 @@ type DirRenamePayload struct { NewParentInodeNum uint64 InodeNum uint64 EntryType DirEntryType - OldNameLen uint8 - NewNameLen uint8 - Reserved [5]byte + TargetType DirEntryType + Flags uint32 + OldParentGen uint64 // each MetaGeneration as read under the rename's locks, before it changed anything + NewParentGen uint64 + MetaGeneration uint64 + TargetInodeNum uint64 // 0 when the new name was free + TargetGen uint64 OldName string NewName string } +const dirRenameHeader = 80 + // Serialize converts the payload to bytes. func (p *DirRenamePayload) Serialize() []byte { - oldNameLen := len(p.OldName) - newNameLen := len(p.NewName) - if oldNameLen > 255 { - oldNameLen = 255 - } - if newNameLen > 255 { - newNameLen = 255 - } - - paddedOldNameLen := (oldNameLen + 3) &^ 3 - paddedNewNameLen := (newNameLen + 3) &^ 3 - - buf := make([]byte, 32+paddedOldNameLen+paddedNewNameLen) + oldName, newName := p.OldName[:min(len(p.OldName), 255)], p.NewName[:min(len(p.NewName), 255)] + paddedOld := (len(oldName) + 3) &^ 3 + buf := make([]byte, dirRenameHeader+paddedOld+(len(newName)+3)&^3) binary.LittleEndian.PutUint64(buf[0:8], p.OldParentInodeNum) binary.LittleEndian.PutUint64(buf[8:16], p.NewParentInodeNum) binary.LittleEndian.PutUint64(buf[16:24], p.InodeNum) buf[24] = byte(p.EntryType) - buf[25] = uint8(oldNameLen) - buf[26] = uint8(newNameLen) - // Reserved at 27-31 - - offset := 32 - copy(buf[offset:], p.OldName[:oldNameLen]) - offset += paddedOldNameLen - copy(buf[offset:], p.NewName[:newNameLen]) - + buf[25] = uint8(len(oldName)) + buf[26] = uint8(len(newName)) + buf[27] = byte(p.TargetType) + binary.LittleEndian.PutUint32(buf[28:32], p.Flags) + binary.LittleEndian.PutUint64(buf[32:40], p.OldParentGen) + binary.LittleEndian.PutUint64(buf[40:48], p.NewParentGen) + binary.LittleEndian.PutUint64(buf[48:56], p.MetaGeneration) + binary.LittleEndian.PutUint64(buf[56:64], p.TargetInodeNum) + binary.LittleEndian.PutUint64(buf[64:72], p.TargetGen) + copy(buf[dirRenameHeader:], oldName) + copy(buf[dirRenameHeader+paddedOld:], newName) return buf } // DeserializeDirRename parses a DirRenamePayload from bytes. func DeserializeDirRename(buf []byte) (*DirRenamePayload, error) { - if len(buf) < 32 { + if len(buf) < dirRenameHeader { return nil, ErrTxnTooSmall } - oldNameLen := int(buf[25]) - newNameLen := int(buf[26]) - paddedOldNameLen := (oldNameLen + 3) &^ 3 - - if len(buf) < 32+paddedOldNameLen+newNameLen { + oldLen, newLen := int(buf[25]), int(buf[26]) + paddedOld := (oldLen + 3) &^ 3 + if len(buf) < dirRenameHeader+paddedOld+newLen { return nil, ErrTxnTooSmall } - return &DirRenamePayload{ OldParentInodeNum: binary.LittleEndian.Uint64(buf[0:8]), NewParentInodeNum: binary.LittleEndian.Uint64(buf[8:16]), InodeNum: binary.LittleEndian.Uint64(buf[16:24]), EntryType: DirEntryType(buf[24]), - OldNameLen: buf[25], - NewNameLen: buf[26], - OldName: string(buf[32 : 32+oldNameLen]), - NewName: string(buf[32+paddedOldNameLen : 32+paddedOldNameLen+newNameLen]), + TargetType: DirEntryType(buf[27]), + Flags: binary.LittleEndian.Uint32(buf[28:32]), + OldParentGen: binary.LittleEndian.Uint64(buf[32:40]), + NewParentGen: binary.LittleEndian.Uint64(buf[40:48]), + MetaGeneration: binary.LittleEndian.Uint64(buf[48:56]), + TargetInodeNum: binary.LittleEndian.Uint64(buf[56:64]), + TargetGen: binary.LittleEndian.Uint64(buf[64:72]), + OldName: string(buf[dirRenameHeader : dirRenameHeader+oldLen]), + NewName: string(buf[dirRenameHeader+paddedOld : dirRenameHeader+paddedOld+newLen]), }, nil } diff --git a/internal/journal/transaction_test.go b/internal/journal/transaction_test.go index 1d03f4d8..797cc322 100644 --- a/internal/journal/transaction_test.go +++ b/internal/journal/transaction_test.go @@ -1,6 +1,7 @@ package journal import ( + "strings" "testing" ) @@ -362,30 +363,22 @@ func TestDirRenamePayloadRoundTrip(t *testing.T) { NewParentInodeNum: 20, InodeNum: 100, EntryType: DirEntryDirectory, + TargetType: DirEntryDirectory, + Flags: 1, + OldParentGen: 7, + NewParentGen: 8, + MetaGeneration: 9, + TargetInodeNum: 200, + TargetGen: 11, OldName: "oldname", - NewName: "newname", + NewName: strings.Repeat("n", 255), } - - data := original.Serialize() - restored, err := DeserializeDirRename(data) + restored, err := DeserializeDirRename(original.Serialize()) if err != nil { t.Fatalf("DeserializeDirRename failed: %v", err) } - - if restored.OldParentInodeNum != original.OldParentInodeNum { - t.Errorf("OldParentInodeNum = %d, want %d", restored.OldParentInodeNum, original.OldParentInodeNum) - } - if restored.NewParentInodeNum != original.NewParentInodeNum { - t.Errorf("NewParentInodeNum = %d, want %d", restored.NewParentInodeNum, original.NewParentInodeNum) - } - if restored.InodeNum != original.InodeNum { - t.Errorf("InodeNum = %d, want %d", restored.InodeNum, original.InodeNum) - } - if restored.OldName != original.OldName { - t.Errorf("OldName = %q, want %q", restored.OldName, original.OldName) - } - if restored.NewName != original.NewName { - t.Errorf("NewName = %q, want %q", restored.NewName, original.NewName) + if *restored != *original { + t.Errorf("round trip: got %+v, want %+v", restored, original) } } From 69cbbc6d2f828a82888f73cc6accbf1c8778b54a Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 14:52:30 -0600 Subject: [PATCH 05/11] feat(fs): rename files and directories Rename and RENAME_NOREPLACE now work within a directory and between directories; RENAME_EXCHANGE and RENAME_WHITEOUT give EINVAL. A replaced file loses its link through the same path unlink uses, so a peer that has it open keeps it until its last close; a replaced empty directory is freed. The journal records the rename and what it does to the target before anything changes. Co-Authored-By: Claude Opus 5.5 --- internal/goclusterfs/rename.go | 234 ++++++++++++++++++++++++++ internal/goclusterfs/rename_test.go | 250 ++++++++++++++++++++++++++++ 2 files changed, 484 insertions(+) create mode 100644 internal/goclusterfs/rename.go create mode 100644 internal/goclusterfs/rename_test.go diff --git a/internal/goclusterfs/rename.go b/internal/goclusterfs/rename.go new file mode 100644 index 00000000..ce73065a --- /dev/null +++ b/internal/goclusterfs/rename.go @@ -0,0 +1,234 @@ +package goclusterfs + +import ( + "context" + "errors" + "log" + "syscall" + + "github.com/hanwen/go-fuse/v2/fs" + + "github.com/supporttools/GoClusterFS/internal/dir" + "github.com/supporttools/GoClusterFS/internal/inode" + "github.com/supporttools/GoClusterFS/internal/journal" + "github.com/supporttools/GoClusterFS/internal/node" +) + +const renameNoReplace = 1 // RENAME_NOREPLACE; RENAME_EXCHANGE and RENAME_WHITEOUT are refused + +var ( + _ fs.NodeRenamer = (*Dir)(nil) + _ fs.NodeRenamer = (*FS)(nil) +) + +// Rename moves name to newName in newParent; go-fuse moves its own node after a success. +func (d *Dir) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno { + return d.fs.rename(detach(ctx), d.inodeNum, name, newParent, newName, flags) +} + +// Rename moves name in the root to newName in newParent. +func (f *FS) Rename(ctx context.Context, name string, newParent fs.InodeEmbedder, newName string, flags uint32) syscall.Errno { + return f.rename(detach(ctx), inode.InodeRoot, name, newParent, newName, flags) +} + +func (f *FS) rename(ctx context.Context, oldParent uint32, oldName string, newParentNode fs.InodeEmbedder, newName string, flags uint32) syscall.Errno { + if flags&^renameNoReplace != 0 { + return syscall.EINVAL + } + var newParent uint32 + switch p := newParentNode.(type) { + case *Dir: + newParent = p.inodeNum + case *FS: + newParent = inode.InodeRoot + default: + return syscall.EXDEV + } + if oldParent == newParent && oldName == newName { + return 0 + } + return retryRace("Rename", oldName, func() syscall.Errno { + return f.renameOnce(ctx, oldParent, oldName, newParent, newName, flags) + }) +} + +func (f *FS) renameOnce(ctx context.Context, oldParent uint32, oldName string, newParent uint32, newName string, flags uint32) syscall.Errno { + oldDir, err := f.inodeTbl.Read(oldParent) + if err != nil { + return syscall.EIO + } + newDir, err := f.inodeTbl.Read(newParent) + if err != nil { + return syscall.EIO + } + child, ctype, err := f.dirMgr.Lookup(ctx, oldDir, oldName) + if err != nil { + return nameErrno(err) + } + op := &dir.RenameOp{OldParent: oldParent, NewParent: newParent, OldName: oldName, NewName: newName, + Child: child, ChildIsDir: ctype == dir.DT_DIR, NoReplace: flags&renameNoReplace != 0} + target, ttype, err := f.dirMgr.Lookup(ctx, newDir, newName) + switch { + case err == nil && target == child: + return 0 + case err == nil: + op.Target, op.TargetIsDir = target, ttype == dir.DT_DIR + case !errors.Is(err, dir.ErrNotFound): + return nameErrno(err) + } + + // A file losing its last link is listed as an orphan before anything changes, as unlink does. + var live uint64 + var targetIno *inode.Inode + if op.Target != 0 && !op.TargetIsDir && !op.NoReplace { + if targetIno, err = f.inodeTbl.Read(uint32(op.Target)); err != nil { + return syscall.EIO + } + if targetIno.LinkCount == 1 { + if live, err = f.reserveOrphan(targetIno); errors.Is(err, errOrphansFull) { + log.Printf("[WARN] rename over %q: %v", newName, err) + return syscall.EBUSY + } else if err != nil { + return syscall.EIO + } + } + } + reserved := live != 0 + defer func() { + if reserved && f.orphanReg != nil { + o := orphanOf(targetIno) + _ = f.orphanReg.DropOrphans(func(x node.Orphan) bool { return x == o }) + } + }() + + var intentSeq uint64 + op.Commit = func(r *dir.Renamed) error { + if f.journal == nil { + return nil + } + seq, err := f.logRename(op, r, flags) + intentSeq = seq + if err == nil && afterCommit != nil { + afterCommit("rename") + } + return err + } + r, err := f.dirMgr.Rename(ctx, op) + if intentSeq != 0 { + defer f.closeIntent(intentSeq) + } + if err != nil { + return renameErrno(err) + } + if r == nil { + return 0 + } + if afterCommit != nil { + afterCommit("rename: names changed") + } + for _, ino := range []*inode.Inode{r.OldParent, r.NewParent, r.Child} { + f.invalidateInode(ino.InodeNum) + if f.cacheMgr != nil { + f.cacheMgr.BroadcastInvalidateInode(ino.InodeNum, ino.DataGeneration, ino.MetaGeneration) + } + } + + switch { + case r.Target == nil: + case op.TargetIsDir: + f.freeReplacedDir(ctx, r.Target) + default: + if err := f.dropLink(ctx, r.Target.InodeNum, live); err != nil { + log.Printf("[WARN] rename over %q: link of inode %d not dropped: %v", newName, r.Target.InodeNum, err) + } else { + reserved = false + } + } + return 0 +} + +// logRename commits the rename and what it does to the target, before any of it reaches the device. +func (f *FS) logRename(op *dir.RenameOp, r *dir.Renamed, flags uint32) (uint64, error) { + txn := f.journal.Begin() + payload := &journal.DirRenamePayload{ + OldParentInodeNum: uint64(op.OldParent), + NewParentInodeNum: uint64(op.NewParent), + InodeNum: op.Child, + EntryType: entryTypeOf(r.ChildType), + Flags: flags, + OldParentGen: r.OldParent.MetaGeneration, + NewParentGen: r.NewParent.MetaGeneration, + MetaGeneration: r.Child.MetaGeneration, + OldName: op.OldName, + NewName: op.NewName, + } + if r.Target != nil { + payload.TargetInodeNum, payload.TargetGen, payload.TargetType = uint64(r.Target.InodeNum), r.Target.MetaGeneration, entryTypeOf(r.TargetType) + } + err := txn.LogDirRename(payload) + switch { + case err != nil || r.Target == nil: + case op.TargetIsDir: + if err = logExtentFree(txn, r.Target, 0); err == nil { + err = txn.LogInodeDelete(&journal.InodeDeletePayload{InodeNum: uint64(r.Target.InodeNum), MetaGeneration: r.Target.MetaGeneration}) + } + default: + err = txn.LogLinkDrop(&journal.LinkDropPayload{InodeNum: uint64(r.Target.InodeNum), MetaGeneration: r.Target.MetaGeneration}) + } + if err != nil { + f.journal.Abort(txn) + return 0, err + } + return f.journal.Commit(txn) +} + +// freeReplacedDir frees an empty directory a rename replaced, unless it changed since the rename checked it. +func (f *FS) freeReplacedDir(ctx context.Context, target *inode.Inode) { + var freed *inode.Inode + ok, err := f.inodeTbl.FreeIf(ctx, target.InodeNum, func(ino *inode.Inode) bool { + freed = ino + return ino.MetaGeneration == target.MetaGeneration + }) + switch { + case err != nil: + log.Printf("[WARN] rename: replaced directory inode %d not freed, keeping its blocks: %v", target.InodeNum, err) + case !ok: + log.Printf("[WARN] rename: replaced directory inode %d changed after it lost its name, keeping it", target.InodeNum) + default: + f.invalidateInode(target.InodeNum) + if err := f.extentMgr.Delete(ctx, freed); err != nil { + log.Printf("[WARN] rename: freeing the blocks of replaced directory %d failed, some may leak: %v", target.InodeNum, err) + } + if afterCommit != nil { + afterCommit("rename: target freed") + } + } +} + +func entryTypeOf(ftype uint8) journal.DirEntryType { + switch ftype { + case dir.DT_DIR: + return journal.DirEntryDirectory + case dir.DT_LNK: + return journal.DirEntrySymlink + } + return journal.DirEntryRegular +} + +func renameErrno(err error) syscall.Errno { + switch { + case errors.Is(err, dir.ErrRaced): + return syscall.EAGAIN + case errors.Is(err, dir.ErrNotEmpty): + return syscall.ENOTEMPTY + case errors.Is(err, dir.ErrNotDirectory): + return syscall.ENOTDIR + case errors.Is(err, dir.ErrIsDir): + return syscall.EISDIR + case errors.Is(err, dir.ErrIntoSelf): + return syscall.EINVAL + case errors.Is(err, dir.ErrNameTooLong): + return syscall.ENAMETOOLONG + } + return nameErrno(err) +} diff --git a/internal/goclusterfs/rename_test.go b/internal/goclusterfs/rename_test.go new file mode 100644 index 00000000..c7d7f038 --- /dev/null +++ b/internal/goclusterfs/rename_test.go @@ -0,0 +1,250 @@ +package goclusterfs + +import ( + "context" + "errors" + "syscall" + "testing" + + "github.com/hanwen/go-fuse/v2/fs" + + "github.com/supporttools/GoClusterFS/internal/dir" + "github.com/supporttools/GoClusterFS/internal/inode" +) + +func fileIn(t *testing.T, fsys *FS, parent uint32, name string) *File { + t.Helper() + ctx := context.Background() + f := newRegularFile(t, fsys) + if _, err := fsys.updateInode(ctx, f.inodeNum, func(ino *inode.Inode) error { ino.LinkCount = 1; return nil }); err != nil { + t.Fatal(err) + } + parentIno, err := fsys.inodeTbl.Read(parent) + if err != nil { + t.Fatal(err) + } + if err := fsys.dirMgr.Add(ctx, parentIno, name, uint64(f.inodeNum), dir.DT_REG); err != nil { + t.Fatal(err) + } + return fileNode(fsys, f.inodeNum) +} + +// named returns what name in directory parent names, or 0. +func named(t *testing.T, fsys *FS, parent uint32, name string) uint32 { + t.Helper() + parentIno, err := fsys.inodeTbl.Read(parent) + if err != nil { + t.Fatal(err) + } + child, _, err := fsys.dirMgr.Lookup(context.Background(), parentIno, name) + if errors.Is(err, dir.ErrNotFound) { + return 0 + } + if err != nil { + t.Fatalf("lookup %q in %d: %v", name, parent, err) + } + return uint32(child) +} + +func links(t *testing.T, fsys *FS, num uint32) uint32 { + t.Helper() + return readInode(t, fsys, num).LinkCount +} + +func TestRenameKeepsTheFileAndItsData(t *testing.T) { + fsys := openFormattedFS(t, newFormattedImage(t)) + f := createFile(t, fsys, "a") + writeAt(t, f, pattern(1, 3*4096), 0) + if errno := fsys.Rename(context.Background(), "a", fsys, "b", 0); errno != 0 { + t.Fatalf("rename: %v", errno) + } + if got := named(t, fsys, inode.InodeRoot, "a"); got != 0 { + t.Errorf("a still names inode %d", got) + } + if got := named(t, fsys, inode.InodeRoot, "b"); got != f.inodeNum { + t.Errorf("b names inode %d, want %d", got, f.inodeNum) + } + checkAt(t, fileNode(fsys, f.inodeNum), pattern(1, 3*4096), 0) + expectNoProblems(t, fsys, "after the rename") +} + +func TestRenameOverAFileFreesIt(t *testing.T) { + fsys := openFormattedFS(t, newFormattedImage(t)) + a, b := createFile(t, fsys, "a"), createFile(t, fsys, "b") + writeAt(t, a, pattern(1, 4096), 0) + writeAt(t, b, pattern(2, 2*BlockSize), 0) + blocks := []uint64{physicalBlock(t, fsys, b.inodeNum, 0), physicalBlock(t, fsys, b.inodeNum, 1)} + + if errno := fsys.Rename(context.Background(), "a", fsys, "b", 0); errno != 0 { + t.Fatalf("rename: %v", errno) + } + if got := named(t, fsys, inode.InodeRoot, "b"); got != a.inodeNum { + t.Errorf("b names inode %d, want %d", got, a.inodeNum) + } + if ino := readInode(t, fsys, b.inodeNum); ino.Magic != 0 { + t.Errorf("the replaced file's inode is still in use: magic %#x", ino.Magic) + } + expectAllocated(t, fsys, blocks, false) + expectNoProblems(t, fsys, "after the rename over a file") +} + +// A rename over a file a peer has open leaves it to that peer until its last close, as unlink does. +func TestRenameOverAFileAPeerHasOpen(t *testing.T) { + path := newFormattedImage(t) + cluster := newFakeCluster(0, 1) + nodeA := cluster.join(t, withBridge(openFormattedFS(t, path)), 0) + nodeB := cluster.join(t, openFormattedFS(t, path), 1) + ctx := context.Background() + held := createOpen(t, nodeA, "b") + writeAt(t, held, pattern(1, 4096), 0) + a := createFile(t, nodeB, "a") + + if errno := nodeB.Rename(ctx, "a", nodeB, "b", 0); errno != 0 { + t.Fatalf("node B renames a over b: %v", errno) + } + if got := named(t, nodeB, inode.InodeRoot, "b"); got != a.inodeNum { + t.Errorf("b names inode %d, want %d", got, a.inodeNum) + } + if ino := readInode(t, nodeA, held.inodeNum); ino.Magic != inode.InodeMagic || ino.LinkCount != 0 || ino.Holders != 1 { + t.Fatalf("replaced file: magic %#x, links %d, holders %#b; want an orphan node A holds", ino.Magic, ino.LinkCount, ino.Holders) + } + writeAt(t, held, pattern(2, 4096), 0) + checkAt(t, held, pattern(2, 4096), 0) + if errno := held.Release(ctx, nil); errno != 0 { + t.Fatalf("release: %v", errno) + } + if ino := readInode(t, nodeA, held.inodeNum); ino.Magic != 0 { + t.Errorf("after node A's last close: magic %#x; want the slot free", ino.Magic) + } +} + +func TestRenameMovesADirectory(t *testing.T) { + fsys := openFormattedFS(t, newFormattedImage(t)) + if err := fsys.InitRoot(context.Background()); err != nil { + t.Fatal(err) + } + p1, p2 := newSubdir(t, fsys, "p1"), newSubdir(t, fsys, "p2") + d := newDirIn(t, fsys, p1.inodeNum, "d") + before1, before2 := links(t, fsys, p1.inodeNum), links(t, fsys, p2.inodeNum) + + if errno := p1.Rename(context.Background(), "d", p2, "e", 0); errno != 0 { + t.Fatalf("rename: %v", errno) + } + if named(t, fsys, p1.inodeNum, "d") != 0 || named(t, fsys, p2.inodeNum, "e") != d.inodeNum { + t.Errorf("after the move: d in p1 names %d, e in p2 names %d; want 0 and %d", named(t, fsys, p1.inodeNum, "d"), named(t, fsys, p2.inodeNum, "e"), d.inodeNum) + } + if got := named(t, fsys, d.inodeNum, ".."); got != p2.inodeNum { + t.Errorf("\"..\" of the moved directory names %d, want %d", got, p2.inodeNum) + } + if got1, got2 := links(t, fsys, p1.inodeNum), links(t, fsys, p2.inodeNum); got1 != before1-1 || got2 != before2+1 { + t.Errorf("link counts %d and %d, want %d and %d", got1, got2, before1-1, before2+1) + } + expectNoProblems(t, fsys, "after the directory moved") +} + +func TestRenameErrors(t *testing.T) { + fsys := openFormattedFS(t, newFormattedImage(t)) + ctx := context.Background() + if err := fsys.InitRoot(ctx); err != nil { + t.Fatal(err) + } + createFile(t, fsys, "file") + createFile(t, fsys, "other") + p := newSubdir(t, fsys, "p") + newDirIn(t, fsys, p.inodeNum, "child") + newSubdir(t, fsys, "empty") + full := newSubdir(t, fsys, "full") + fileIn(t, fsys, full.inodeNum, "x") + + for _, c := range []struct { + from string + to string + into *Dir + flags uint32 + want syscall.Errno + describe string + }{ + {"file", "other", nil, 2, syscall.EINVAL, "RENAME_EXCHANGE"}, + {"file", "other", nil, renameNoReplace, syscall.EEXIST, "NOREPLACE over a file"}, + {"missing", "x", nil, 0, syscall.ENOENT, "a missing name"}, + {"file", "empty", nil, 0, syscall.EISDIR, "a file over a directory"}, + {"p", "file", nil, 0, syscall.ENOTDIR, "a directory over a file"}, + {"empty", "full", nil, 0, syscall.ENOTEMPTY, "a directory over a non-empty one"}, + {"p", "inside", &Dir{fs: fsys, inodeNum: named(t, fsys, p.inodeNum, "child")}, 0, syscall.EINVAL, "a directory into itself"}, + } { + var into fs.InodeEmbedder = fsys + if c.into != nil { + into = c.into + } + if errno := fsys.Rename(ctx, c.from, into, c.to, c.flags); errno != c.want { + t.Errorf("rename of %s: %v, want %v", c.describe, errno, c.want) + } + } + expectNoProblems(t, fsys, "after the refused renames") +} + +func TestRenameReplayFinishesARenameOverAFile(t *testing.T) { + fsys, path, offset, size := journaledFS(t) + ctx := context.Background() + a, b := createFile(t, fsys, "a"), createFile(t, fsys, "b") + writeAt(t, b, pattern(2, BlockSize), 0) + block := physicalBlock(t, fsys, b.inodeNum, 0) + + crashed := crashAfterCommit(t, fsys, "rename") + go func() { _ = fsys.Rename(ctx, "a", fsys, "b", 0) }() + <-crashed + + remounted := remountAndReplay(t, path, offset, size) + if named(t, remounted, inode.InodeRoot, "a") != 0 || named(t, remounted, inode.InodeRoot, "b") != a.inodeNum { + t.Errorf("after replay: a names %d, b names %d; want 0 and %d", named(t, remounted, inode.InodeRoot, "a"), named(t, remounted, inode.InodeRoot, "b"), a.inodeNum) + } + if ino := readInode(t, remounted, b.inodeNum); ino.Magic != 0 { + t.Errorf("the replaced file's inode after replay: magic %#x; want free", ino.Magic) + } + expectAllocated(t, remounted, []uint64{block}, false) + expectCleanFsck(t, remounted) +} + +func TestRenameReplayMovesADirectory(t *testing.T) { + fsys, path, offset, size := journaledFS(t) + ctx := context.Background() + p1, p2 := newSubdir(t, fsys, "p1"), newSubdir(t, fsys, "p2") + d := newDirIn(t, fsys, p1.inodeNum, "d") + before1, before2 := links(t, fsys, p1.inodeNum), links(t, fsys, p2.inodeNum) + + crashed := crashAfterCommit(t, fsys, "rename") + go func() { _ = p1.Rename(ctx, "d", p2, "e", 0) }() + <-crashed + + remounted := remountAndReplay(t, path, offset, size) + if named(t, remounted, p1.inodeNum, "d") != 0 || named(t, remounted, p2.inodeNum, "e") != d.inodeNum { + t.Errorf("after replay: d in p1 names %d, e in p2 names %d", named(t, remounted, p1.inodeNum, "d"), named(t, remounted, p2.inodeNum, "e")) + } + if got := named(t, remounted, d.inodeNum, ".."); got != p2.inodeNum { + t.Errorf("\"..\" after replay names %d, want %d", got, p2.inodeNum) + } + if got1, got2 := links(t, remounted, p1.inodeNum), links(t, remounted, p2.inodeNum); got1 != before1-1 || got2 != before2+1 { + t.Errorf("link counts after replay %d and %d, want %d and %d", got1, got2, before1-1, before2+1) + } + expectCleanFsck(t, remounted) +} + +// A crash after the names changed but before the target lost its link: replay drops the link once. +func TestRenameReplayDropsTheTargetAfterTheNamesChanged(t *testing.T) { + fsys, path, offset, size := journaledFS(t) + ctx := context.Background() + a, b := createFile(t, fsys, "a"), createFile(t, fsys, "b") + + crashed := crashAfterCommit(t, fsys, "rename: names changed") + go func() { _ = fsys.Rename(ctx, "a", fsys, "b", 0) }() + <-crashed + + remounted := remountAndReplay(t, path, offset, size) + if named(t, remounted, inode.InodeRoot, "b") != a.inodeNum || named(t, remounted, inode.InodeRoot, "a") != 0 { + t.Errorf("after replay: a names %d, b names %d", named(t, remounted, inode.InodeRoot, "a"), named(t, remounted, inode.InodeRoot, "b")) + } + if ino := readInode(t, remounted, b.inodeNum); ino.Magic != 0 { + t.Errorf("the replaced file's inode after replay: magic %#x, links %d; want free", ino.Magic, ino.LinkCount) + } + expectCleanFsck(t, remounted) +} From 0c786ee79e8a13674a87fba92d8875dfbea53c8a Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 14:53:44 -0600 Subject: [PATCH 06/11] test(fs): cut power at every write of a rename Each of five renames, in one directory, between directories, over a file, a directory moving between parents, and a directory over an empty one, ends with exactly one of its two names after replay, and fsck finds nothing but leaks. Co-Authored-By: Claude Opus 5.5 --- internal/goclusterfs/rename_power_cut_test.go | 128 ++++++++++++++++++ 1 file changed, 128 insertions(+) create mode 100644 internal/goclusterfs/rename_power_cut_test.go diff --git a/internal/goclusterfs/rename_power_cut_test.go b/internal/goclusterfs/rename_power_cut_test.go new file mode 100644 index 00000000..f0e57637 --- /dev/null +++ b/internal/goclusterfs/rename_power_cut_test.go @@ -0,0 +1,128 @@ +package goclusterfs + +import ( + "context" + "testing" + + "github.com/supporttools/GoClusterFS/internal/dir" + "github.com/supporttools/GoClusterFS/internal/inode" +) + +// expectOneName fails unless exactly one of the two names names child: a rename either happened or did not. +func expectOneName(t *testing.T, fsys *FS, oldParent uint32, oldName string, newParent uint32, newName string, child uint32) (moved bool) { + t.Helper() + atOld, atNew := named(t, fsys, oldParent, oldName) == child, named(t, fsys, newParent, newName) == child + if atOld == atNew { + t.Fatalf("inode %d: named by the old name %v, by the new name %v; want exactly one", child, atOld, atNew) + } + return atNew +} + +// expectDirLinks checks that a directory counts 2 links plus one per subdirectory. +func expectDirLinks(t *testing.T, fsys *FS, num uint32) { + t.Helper() + entries, err := fsys.dirMgr.List(context.Background(), readInode(t, fsys, num)) + if err != nil { + t.Fatal(err) + } + want := uint32(2) + for _, e := range entries { + if e.FileType == dir.DT_DIR && e.Name != "." && e.Name != ".." { + want++ + } + } + if got := links(t, fsys, num); got != want { + t.Errorf("directory %d counts %d links, want %d", num, got, want) + } +} + +func TestPowerCutDuringRenameInOneDirectory(t *testing.T) { + data := pattern(4, 4096) + var f *File + runPowerCuts(t, powerCutCase{ + setup: func(t *testing.T, fsys *FS) { + f = createFile(t, fsys, "a") + writeAt(t, f, data, 0) + }, + op: func(fsys *FS, _ *power) { _ = fsys.Rename(context.Background(), "a", fsys, "b", 0) }, + check: func(t *testing.T, fsys *FS) { + expectOneName(t, fsys, inode.InodeRoot, "a", inode.InodeRoot, "b", f.inodeNum) + checkAt(t, fileNode(fsys, f.inodeNum), data, 0) + }, + }) +} + +func TestPowerCutDuringRenameBetweenDirectories(t *testing.T) { + var p1, p2 *Dir + var f *File + runPowerCuts(t, powerCutCase{ + setup: func(t *testing.T, fsys *FS) { + p1, p2 = newSubdir(t, fsys, "p1"), newSubdir(t, fsys, "p2") + f = fileIn(t, fsys, p1.inodeNum, "a") + }, + op: func(fsys *FS, _ *power) { _ = p1.Rename(context.Background(), "a", p2, "b", 0) }, + check: func(t *testing.T, fsys *FS) { + expectOneName(t, fsys, p1.inodeNum, "a", p2.inodeNum, "b", f.inodeNum) + }, + }) +} + +func TestPowerCutDuringRenameOverAFile(t *testing.T) { + var a, b *File + runPowerCuts(t, powerCutCase{ + setup: func(t *testing.T, fsys *FS) { + a, b = createFile(t, fsys, "a"), createFile(t, fsys, "b") + writeAt(t, b, pattern(2, BlockSize), 0) + }, + op: func(fsys *FS, _ *power) { _ = fsys.Rename(context.Background(), "a", fsys, "b", 0) }, + check: func(t *testing.T, fsys *FS) { + if !expectOneName(t, fsys, inode.InodeRoot, "a", inode.InodeRoot, "b", a.inodeNum) { + if got := named(t, fsys, inode.InodeRoot, "b"); got != b.inodeNum { + t.Errorf("the rename did not happen, but b names inode %d, not %d", got, b.inodeNum) + } + } + }, + }) +} + +func TestPowerCutDuringDirectoryMove(t *testing.T) { + var p1, p2, d *Dir + runPowerCuts(t, powerCutCase{ + setup: func(t *testing.T, fsys *FS) { + p1, p2 = newSubdir(t, fsys, "p1"), newSubdir(t, fsys, "p2") + d = newDirIn(t, fsys, p1.inodeNum, "d") + }, + op: func(fsys *FS, _ *power) { _ = p1.Rename(context.Background(), "d", p2, "e", 0) }, + check: func(t *testing.T, fsys *FS) { + parent := p1.inodeNum + if expectOneName(t, fsys, p1.inodeNum, "d", p2.inodeNum, "e", d.inodeNum) { + parent = p2.inodeNum + } + if got := named(t, fsys, d.inodeNum, ".."); got != parent { + t.Errorf("\"..\" of the moved directory names %d, want %d", got, parent) + } + expectDirLinks(t, fsys, p1.inodeNum) + expectDirLinks(t, fsys, p2.inodeNum) + }, + }) +} + +func TestPowerCutDuringRenameOverAnEmptyDirectory(t *testing.T) { + var p1, p2, d, e *Dir + runPowerCuts(t, powerCutCase{ + setup: func(t *testing.T, fsys *FS) { + p1, p2 = newSubdir(t, fsys, "p1"), newSubdir(t, fsys, "p2") + d, e = newDirIn(t, fsys, p1.inodeNum, "d"), newDirIn(t, fsys, p2.inodeNum, "e") + }, + op: func(fsys *FS, _ *power) { _ = p1.Rename(context.Background(), "d", p2, "e", 0) }, + check: func(t *testing.T, fsys *FS) { + if !expectOneName(t, fsys, p1.inodeNum, "d", p2.inodeNum, "e", d.inodeNum) { + if got := named(t, fsys, p2.inodeNum, "e"); got != e.inodeNum { + t.Errorf("the rename did not happen, but e names inode %d, not %d", got, e.inodeNum) + } + } + expectDirLinks(t, fsys, p1.inodeNum) + expectDirLinks(t, fsys, p2.inodeNum) + }, + }) +} From 38c7519190af0a0f9d1807d0844afef4f2803af6 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 14:55:10 -0600 Subject: [PATCH 07/11] test: rename through the mount, and fail when mv fails Co-Authored-By: Claude Opus 5.5 --- tests/docker/scripts/run-tests.sh | 5 +- tests/integration/integration_test.go | 69 +++++++++++++++++---------- tests/vm/scripts/run-vm-tests.sh | 5 +- 3 files changed, 49 insertions(+), 30 deletions(-) diff --git a/tests/docker/scripts/run-tests.sh b/tests/docker/scripts/run-tests.sh index 46402eb8..25c6afb0 100644 --- a/tests/docker/scripts/run-tests.sh +++ b/tests/docker/scripts/run-tests.sh @@ -890,9 +890,8 @@ test_rename_file() { # Attempt to rename if ! exec_on_node "$node" mv "/mnt/goclusterfs/$oldname" "/mnt/goclusterfs/$newname" 2>/dev/null; then - # Rename not implemented - this is expected for now - log "SKIP: Rename operation not yet implemented in GoClusterFS" - return 0 + log "ERROR: mv $oldname $newname failed" + return 1 fi # If rename succeeded, verify old name doesn't exist diff --git a/tests/integration/integration_test.go b/tests/integration/integration_test.go index 90c9e1bb..d90616c2 100644 --- a/tests/integration/integration_test.go +++ b/tests/integration/integration_test.go @@ -5,12 +5,15 @@ import ( "bytes" "crypto/md5" "crypto/rand" + "errors" "fmt" "os" "path/filepath" "strings" "syscall" "testing" + + "golang.org/x/sys/unix" ) // TestMountUnmount verifies basic mount and unmount functionality. @@ -724,46 +727,64 @@ func TestAppendFile(t *testing.T) { t.Log("Append file test passed") } -// TestRename tests renaming files. -// Note: Rename is not yet implemented in GoClusterFS - this test verifies the expected error. +// TestRename renames a file, renames it over another file, moves a directory between parents, and checks +// RENAME_NOREPLACE, all through the mount. func TestRename(t *testing.T) { - mfs := NewMountedFS(t, 20) + mfs := NewMountedFS(t, 64) if err := mfs.FormatAndMount(false); err != nil { t.Fatalf("FormatAndMount failed: %v", err) } - - // Create a file content := []byte("Rename test content") if err := mfs.WriteFile("oldname.txt", content); err != nil { t.Fatalf("WriteFile failed: %v", err) } - - // Rename it - currently not implemented, so expect failure - oldPath := mfs.Path("oldname.txt") - newPath := mfs.Path("newname.txt") - err := os.Rename(oldPath, newPath) - if err != nil { - // Rename not implemented - this is expected for now - t.Logf("Rename not implemented (expected): %v", err) - t.Skip("Rename operation not yet implemented in GoClusterFS") + if err := os.Rename(mfs.Path("oldname.txt"), mfs.Path("newname.txt")); err != nil { + t.Fatalf("rename: %v", err) } - - // Old name should not exist if mfs.Exists("oldname.txt") { - t.Error("Old name should not exist after rename") + t.Error("the old name still exists after the rename") } + expectContent(t, mfs, "newname.txt", content) - // New name should exist with same content - readBack, err := mfs.ReadFile("newname.txt") - if err != nil { - t.Fatalf("ReadFile new name failed: %v", err) + if err := mfs.WriteFile("victim.txt", []byte("replaced")); err != nil { + t.Fatal(err) } + if err := os.Rename(mfs.Path("newname.txt"), mfs.Path("victim.txt")); err != nil { + t.Fatalf("rename over a file: %v", err) + } + expectContent(t, mfs, "victim.txt", content) - if !bytes.Equal(content, readBack) { - t.Error("Content changed after rename") + for _, d := range []string{"a", "b", "a/sub"} { + if err := os.Mkdir(mfs.Path(d), 0o755); err != nil { + t.Fatal(err) + } + } + if err := mfs.WriteFile("a/sub/f", []byte("inside")); err != nil { + t.Fatal(err) + } + if err := os.Rename(mfs.Path("a/sub"), mfs.Path("b/moved")); err != nil { + t.Fatalf("move a directory between parents: %v", err) + } + expectContent(t, mfs, "b/moved/f", []byte("inside")) + if mfs.Exists("a/sub") { + t.Error("a/sub still exists after the move") + } + if dots, parent := inodeNumber(t, mfs, "b/moved/.."), inodeNumber(t, mfs, "b"); dots != parent { + t.Errorf("\"..\" of the moved directory is inode %d, want b's %d", dots, parent) } - t.Log("Rename test passed") + err := unix.Renameat2(unix.AT_FDCWD, mfs.Path("victim.txt"), unix.AT_FDCWD, mfs.Path("b/moved/f"), unix.RENAME_NOREPLACE) + if !errors.Is(err, unix.EEXIST) { + t.Errorf("RENAME_NOREPLACE over an existing name: %v, want EEXIST", err) + } + expectContent(t, mfs, "b/moved/f", []byte("inside")) + + if err := mfs.Unmount(); err != nil { + t.Fatal(err) + } + if code, out := runFsck(t, mfs.ImagePath); code != 0 { + t.Errorf("fsck after the renames: exit %d:\n%s", code, strings.Join(problemLines(out), "\n")) + } } // TestCopyFile tests copying files within the filesystem. diff --git a/tests/vm/scripts/run-vm-tests.sh b/tests/vm/scripts/run-vm-tests.sh index 0674f0d6..88f851d4 100755 --- a/tests/vm/scripts/run-vm-tests.sh +++ b/tests/vm/scripts/run-vm-tests.sh @@ -2466,9 +2466,8 @@ test_rename_file() { # Attempt to rename if ! ssh_run "$vm_id" "mv /mnt/goclusterfs/$oldname /mnt/goclusterfs/$newname" 2>/dev/null; then - # Rename not implemented - this is expected for now - log_info "SKIP: Rename operation not yet implemented in GoClusterFS" - return 0 + log_error "mv $oldname $newname failed" + return 1 fi # If rename succeeded, verify old name doesn't exist From bb875b8c2a2517f6a68b5db34a5294cb02919c37 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 14:55:59 -0600 Subject: [PATCH 08/11] docs: rename is supported; track its tests in the manifest Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + CLAUDE.md | 2 +- tests/test-manifest.yaml | 156 ++++++++++++++++++++++++++++++++++++++- 3 files changed, 157 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a473e1ba..8c4f934b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -59,6 +59,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - `-cache-inodes`, `-cache-extents`, `-cache-negative` and `-broadcast-max-peers` set the cache and broadcast table sizes, and `/metrics` reports each limit next to its size. - A node that hears no broadcasts from a node the registry shows mounted, for 3 `-broadcast-heartbeat` intervals (90 seconds by default), logs a warning naming it, reports `broadcast-peers` as degraded on `/health`, and counts it in `goclusterfs_broadcast_silent_peers`. A network that silently drops multicast was invisible before. +- Rename of files and directories, in one directory or between two, with `RENAME_NOREPLACE`. A rename over a file or an empty directory replaces it; a file another node has open stays readable there until its last close. `RENAME_EXCHANGE` and `RENAME_WHITEOUT` fail with `EINVAL`. ### Changed diff --git a/CLAUDE.md b/CLAUDE.md index 394f4555..4b295a7a 100644 --- a/CLAUDE.md +++ b/CLAUDE.md @@ -99,7 +99,7 @@ Clone paths are absolute from the filesystem root, and clones can only go in the - `goclusterfs-fsck -repair` fixes only block refcounts, sharing that older versions left uncounted, leaked blocks, inodes no directory names, snapshot registry sectors that fail their checksum, and the superblock checksum. It reports every other problem without fixing it. - The inode count is set at format time and cannot change. Every `statfs` (`df`) reads the whole inode table to count free inodes: 50 MB on a 200 GB device at the default ratio. - SFB is disabled: writes never allocate SFB slots, so every file uses full 1 MB blocks. `internal/sfb` container I/O is broken (unaligned header buffers, no data offset) and stays until it is fixed or removed. -- No rename, symlinks, or hard links. +- No symlinks or hard links. Rename supports `RENAME_NOREPLACE`; `RENAME_EXCHANGE` and `RENAME_WHITEOUT` fail with `EINVAL`. ## Testing diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index d35519e8..a373662f 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -210,7 +210,7 @@ scenarios: - id: rename-file name: Rename file - description: Rename a file within directory + description: Rename a file, rename it over another file, move a directory between parents with its ".." following, and refuse RENAME_NOREPLACE over an existing name; fsck is clean afterwards (Docker and VM rename one file) priority: high environments: go: integration_test.go::TestRename @@ -1646,6 +1646,160 @@ scenarios: environments: go: internal/goclusterfs/power_cut_test.go::TestPowerCutDuringSnapshotDelete + - id: dir-renameinonedirectory + name: Directory rename within one directory + description: A rename in one directory commits once and moves the name + priority: go_only + environments: + go: internal/dir/rename_test.go::TestRenameInOneDirectory + + - id: dir-renamereplacesatarget + name: Directory rename over a target and NOREPLACE + description: A rename replaces a target file; NOREPLACE over an existing name changes nothing + priority: go_only + environments: + go: internal/dir/rename_test.go::TestRenameReplacesATarget + + - id: dir-renamemovesadirectorybetweenparents + name: Directory rename moves a directory + description: A directory moved between parents gets its ".." rewritten and both link counts adjusted + priority: go_only + environments: + go: internal/dir/rename_test.go::TestRenameMovesADirectoryBetweenParents + + - id: dir-renamerefusesamoveintoitself + name: Directory rename refuses a move into itself + description: Moving a directory into its own subtree fails before anything changes + priority: go_only + environments: + go: internal/dir/rename_test.go::TestRenameRefusesAMoveIntoItself + + - id: dir-renamecheckstargettypes + name: Directory rename checks target types + description: A directory over a file, a file over a directory, and a directory over a non-empty one are refused; a directory over an empty one replaces it + priority: go_only + environments: + go: internal/dir/rename_test.go::TestRenameChecksTargetTypes + + - id: dir-renameafterastalelookupchangesnothing + name: Directory rename rechecks its lookups + description: A rename whose names changed since the lookup, or whose commit fails, changes nothing + priority: go_only + environments: + go: internal/dir/rename_test.go::TestRenameAfterAStaleLookupChangesNothing + + - id: dir-renameisseenbyanothernode + name: Directory rename is seen by another node + description: Another node's index sees the rename on its next lookup + priority: go_only + environments: + go: internal/dir/rename_test.go::TestRenameIsSeenByAnotherNode + + - id: dir-renamegrowsafullnewparent + name: Directory rename grows a full new parent + description: A rename into a directory with no room gives it a new block first + priority: go_only + environments: + go: internal/dir/rename_test.go::TestRenameGrowsAFullNewParent + + - id: dir-pointifonlymovesanamethatstillnamestheex + name: Directory PointIf checks the current inode + description: PointIf points a name at another inode only while it names the expected one + priority: go_only + environments: + go: internal/dir/rename_test.go::TestPointIfOnlyMovesANameThatStillNamesTheExpectedInode + + - id: fs-renamekeepsthefileanditsdata + name: Rename keeps the file and its data + description: A renamed file keeps its inode and data; fsck is clean + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameKeepsTheFileAndItsData + + - id: fs-renameoverafilefreesit + name: Rename over a file frees it + description: The replaced file's inode and blocks are freed + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameOverAFileFreesIt + + - id: fs-renameoverafileapeerhasopen + name: Rename over a file a peer has open + description: The replaced file stays an orphan the peer holds until its last close + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameOverAFileAPeerHasOpen + + - id: fs-renamemovesadirectory + name: Rename moves a directory between parents + description: The moved directory's ".." and both parents' link counts follow + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameMovesADirectory + + - id: fs-renameerrors + name: Rename errors + description: EXCHANGE, NOREPLACE, a missing name, type mismatches, a non-empty target and a move into itself give the right errno + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameErrors + + - id: fs-renamereplayfinishesarenameoverafile + name: Rename replay finishes a rename over a file + description: A crash right after the commit ends with the new name and the target freed + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameReplayFinishesARenameOverAFile + + - id: fs-renamereplaymovesadirectory + name: Rename replay moves a directory + description: A crash right after the commit of a directory move ends with the new name, ".." and link counts right + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameReplayMovesADirectory + + - id: fs-renamereplaydropsthetargetafterthenamesc + name: Rename replay drops the target once + description: A crash after the names changed frees the target exactly once + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameReplayDropsTheTargetAfterTheNamesChanged + + - id: rename-power-renameinonedirectory + name: Power failure during a rename in one directory + description: Power fails at each write; exactly one of the two names names the file, and fsck finds only leaks + priority: go_only + environments: + go: internal/goclusterfs/rename_power_cut_test.go::TestPowerCutDuringRenameInOneDirectory + + - id: rename-power-renamebetweendirectories + name: Power failure during a rename between directories + description: Power fails at each write; exactly one of the two names names the file + priority: go_only + environments: + go: internal/goclusterfs/rename_power_cut_test.go::TestPowerCutDuringRenameBetweenDirectories + + - id: rename-power-renameoverafile + name: Power failure during a rename over a file + description: Power fails at each write; the file has one name, and the target keeps its name only if the rename did not happen + priority: go_only + environments: + go: internal/goclusterfs/rename_power_cut_test.go::TestPowerCutDuringRenameOverAFile + + - id: rename-power-directorymove + name: Power failure during a directory move + description: Power fails at each write; the directory has one name, its ".." names that parent, and link counts are right + priority: go_only + environments: + go: internal/goclusterfs/rename_power_cut_test.go::TestPowerCutDuringDirectoryMove + + - id: rename-power-renameoveranemptydirectory + name: Power failure during a rename over an empty directory + description: Power fails at each write; one name, and link counts are right + priority: go_only + environments: + go: internal/goclusterfs/rename_power_cut_test.go::TestPowerCutDuringRenameOverAnEmptyDirectory + - id: clone-power-failure-go name: Power failure at each write of a clone description: Power fails at each device write of a clone; the source keeps its data and the clone is either gone or a full copy, and fsck finds only leaks From 43bfbeb6cd41ea36740f08d505d80514c1f82510 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 17:11:12 -0600 Subject: [PATCH 09/11] fix(journal): leave a finished rename alone when later operations changed its names renameOnce releases its locks before it completes its intent, so a later rename, rename back or unlink could change the names before a crash, and replay then redid the rename over them: two names for one link, a name for a freed inode, or a rename undone. A rename writes each parent, and a moved directory, once under its locks, so replay now skips the names when any of them is freed or more than one write past the generation the rename logged. The CHANGELOG notes that older nodes replaying this journal skip the rename but drop the target's link, so every node must be upgraded before anyone renames. Co-Authored-By: Claude Opus 5.5 --- CHANGELOG.md | 1 + internal/goclusterfs/rename_test.go | 83 +++++++++++++++++++++++++ internal/journal/cross_node_recovery.go | 23 +++++++ tests/test-manifest.yaml | 14 +++++ 4 files changed, 121 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8c4f934b..0cc67ff7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - Older binaries read a newer node's waiting mark in a lock record as a held lock. - Older binaries free a file unlinked while open at once, even when a newer node still has it open. - Older binaries cannot replay a journal this version wrote, because they do not know its link-drop records. After a crash, let this version replay the journal before you downgrade. + - Older binaries skip rename records when they replay a journal this version wrote, but still apply the replaced file's link drop, which can free a file whose name remains. Upgrade every node before anyone renames. - Run `goclusterfs-fsck -repair` before the first mount after upgrading an image that has snapshots or clones. Unmount the device on every node first. - Older versions marked a file's blocks shared, on snapshot or clone, without counting the owners. Until `goclusterfs-fsck -repair` counts them, this version never frees such blocks, so deleting those files leaks their space, and it refuses to snapshot or clone them (`ENOTSUP`). In-place writes to an old clone still change its source. - Snapshots now record the birth generation of the file they were taken of, so a later file that reuses the inode number cannot list, clone or delete them. Snapshots older versions made do not record it, and some captured no data, so after the upgrade they are orphans: `getfattr -n user.snapshots` no longer lists them, `GET /api/v1/snapshots` lists them with `orphan: true`, and `DELETE /api/v1/snapshots/` without `path` deletes them. Older binaries keep the birth generation in inode bytes 196–203 only while they do not rewrite the inode, so upgrade all nodes together. diff --git a/internal/goclusterfs/rename_test.go b/internal/goclusterfs/rename_test.go index c7d7f038..e86e8416 100644 --- a/internal/goclusterfs/rename_test.go +++ b/internal/goclusterfs/rename_test.go @@ -248,3 +248,86 @@ func TestRenameReplayDropsTheTargetAfterTheNamesChanged(t *testing.T) { } expectCleanFsck(t, remounted) } + +// crashAfterOvertaken runs overtake once the rename has changed its names and released its locks, then crashes with +// the rename's intent still open, as a crash before its completion record reaches the device would. +func crashAfterOvertaken(t *testing.T, fsys *FS, overtake func()) <-chan struct{} { + t.Helper() + crashed := make(chan struct{}) + fired := false + afterCommit = func(got string) { + if got != "rename: names changed" || fired { + return + } + fired = true + overtake() + if _, err := fsys.journal.Checkpoint(); err != nil { + t.Errorf("checkpoint: %v", err) + } + close(crashed) + select {} + } + t.Cleanup(func() { afterCommit = nil }) + return crashed +} + +// Replay must not redo a rename that finished and that later operations changed. +func TestRenameReplayLeavesAnOvertakenRenameAlone(t *testing.T) { + for _, c := range []struct { + describe string + overtake func(ctx context.Context, fsys *FS) syscall.Errno + want map[string]bool // which root names name the file after replay + }{ + {"renamed again", func(ctx context.Context, fsys *FS) syscall.Errno { return fsys.Rename(ctx, "b", fsys, "c", 0) }, map[string]bool{"c": true}}, + {"renamed back", func(ctx context.Context, fsys *FS) syscall.Errno { return fsys.Rename(ctx, "b", fsys, "a", 0) }, map[string]bool{"a": true}}, + {"unlinked", func(ctx context.Context, fsys *FS) syscall.Errno { return fsys.Unlink(ctx, "b") }, map[string]bool{}}, + } { + t.Run(c.describe, func(t *testing.T) { + fsys, path, offset, size := journaledFS(t) + ctx := context.Background() + a := createFile(t, fsys, "a") + crashed := crashAfterOvertaken(t, fsys, func() { + if errno := c.overtake(ctx, fsys); errno != 0 { + t.Errorf("the overtaking operation: %v", errno) + } + }) + go func() { _ = fsys.Rename(ctx, "a", fsys, "b", 0) }() + <-crashed + + remounted := remountAndReplay(t, path, offset, size) + for _, name := range []string{"a", "b", "c"} { + if got := named(t, remounted, inode.InodeRoot, name) == a.inodeNum; got != c.want[name] { + t.Errorf("after replay, %q names the file: %v, want %v", name, got, c.want[name]) + } + } + expectCleanFsck(t, remounted) + }) + } +} + +func TestRenameReplayLeavesADirectoryMovedBackAlone(t *testing.T) { + fsys, path, offset, size := journaledFS(t) + ctx := context.Background() + p1, p2 := newSubdir(t, fsys, "p1"), newSubdir(t, fsys, "p2") + d := newDirIn(t, fsys, p1.inodeNum, "d") + before1, before2 := links(t, fsys, p1.inodeNum), links(t, fsys, p2.inodeNum) + crashed := crashAfterOvertaken(t, fsys, func() { + if errno := p2.Rename(ctx, "e", p1, "d", 0); errno != 0 { + t.Errorf("move back: %v", errno) + } + }) + go func() { _ = p1.Rename(ctx, "d", p2, "e", 0) }() + <-crashed + + remounted := remountAndReplay(t, path, offset, size) + if named(t, remounted, p1.inodeNum, "d") != d.inodeNum || named(t, remounted, p2.inodeNum, "e") != 0 { + t.Errorf("after replay: d in p1 names %d, e in p2 names %d; want %d and 0", named(t, remounted, p1.inodeNum, "d"), named(t, remounted, p2.inodeNum, "e"), d.inodeNum) + } + if got := named(t, remounted, d.inodeNum, ".."); got != p1.inodeNum { + t.Errorf("\"..\" after replay names %d, want %d", got, p1.inodeNum) + } + if got1, got2 := links(t, remounted, p1.inodeNum), links(t, remounted, p2.inodeNum); got1 != before1 || got2 != before2 { + t.Errorf("link counts after replay %d and %d, want %d and %d", got1, got2, before1, before2) + } + expectCleanFsck(t, remounted) +} diff --git a/internal/journal/cross_node_recovery.go b/internal/journal/cross_node_recovery.go index 0bdf8e35..dfd0a5b4 100644 --- a/internal/journal/cross_node_recovery.go +++ b/internal/journal/cross_node_recovery.go @@ -489,6 +489,9 @@ func applyDirRename(ctx context.Context, rctx *RecoveryContext, r *txnReplay, pa if err != nil { return false, err } + if done, err := renameOvertaken(rctx, p); err != nil || done { + return false, err + } placed, err := placeRenamed(ctx, rctx, r, p) if err != nil || !placed { if p.TargetInodeNum != 0 { @@ -516,6 +519,26 @@ func applyDirRename(ctx context.Context, rctx *RecoveryContext, r *txnReplay, pa return true, nil } +// renameOvertaken reports whether an inode the rename writes once under its locks is freed or more than one write past +// the generation it logged. Another operation took the lock after the rename finished, so its names must stay. +func renameOvertaken(rctx *RecoveryContext, p *DirRenamePayload) (bool, error) { + logged := map[uint64]uint64{p.OldParentInodeNum: p.OldParentGen, p.NewParentInodeNum: p.NewParentGen} + if p.EntryType == DirEntryDirectory && p.OldParentInodeNum != p.NewParentInodeNum { + logged[p.InodeNum] = p.MetaGeneration + } + for num, gen := range logged { + ino, err := rctx.InodeTbl.Read(uint32(num)) + if err != nil { + return false, fmt.Errorf("failed to read inode %d: %w", num, err) + } + if ino.Magic != inode.InodeMagic || ino.MetaGeneration > gen+1 { + log.Printf("[Recovery] DirRename: inode %d changed after the rename of %q to %q finished, skipping", num, p.OldName, p.NewName) + return true, nil + } + } + return false, nil +} + // placeRenamed makes the new name name the child, if it is free, names the child, or still names the logged target. // Each parent's link count changes first, and only while the parent is as the rename logged it. func placeRenamed(ctx context.Context, rctx *RecoveryContext, r *txnReplay, p *DirRenamePayload) (bool, error) { diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index a373662f..e0bedef1 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -1765,6 +1765,20 @@ scenarios: environments: go: internal/goclusterfs/rename_test.go::TestRenameReplayDropsTheTargetAfterTheNamesChanged + - id: fs-renamereplayleavesanovertakenrenamealone + name: Rename replay leaves an overtaken rename alone + description: A crash with a finished rename's intent open, after a later rename, rename back or unlink changed its names, leaves the later names as they are + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameReplayLeavesAnOvertakenRenameAlone + + - id: fs-renamereplayleavesadirectorymovedbackalone + name: Rename replay leaves a directory moved back alone + description: A directory moved and moved back before the crash keeps its old name, "..", and both parents' link counts after replay + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameReplayLeavesADirectoryMovedBackAlone + - id: rename-power-renameinonedirectory name: Power failure during a rename in one directory description: Power fails at each write; exactly one of the two names names the file, and fsck finds only leaks From ce07912e1f6e889deeb88670f021113e7b3de0a6 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 17:13:07 -0600 Subject: [PATCH 10/11] fix(fs): free a symlink a rename replaces at once, as unlink will No node holds a symlink open, so listing it as an orphan only delays its free until peers let go. Co-Authored-By: Claude Opus 5.5 --- internal/goclusterfs/rename.go | 5 +++-- internal/goclusterfs/rename_test.go | 32 +++++++++++++++++++++++++++++ tests/test-manifest.yaml | 7 +++++++ 3 files changed, 42 insertions(+), 2 deletions(-) diff --git a/internal/goclusterfs/rename.go b/internal/goclusterfs/rename.go index ce73065a..d671eb53 100644 --- a/internal/goclusterfs/rename.go +++ b/internal/goclusterfs/rename.go @@ -77,10 +77,11 @@ func (f *FS) renameOnce(ctx context.Context, oldParent uint32, oldName string, n return nameErrno(err) } - // A file losing its last link is listed as an orphan before anything changes, as unlink does. + // A file losing its last link is listed as an orphan before anything changes, as unlink does. No node holds a + // symlink open, so a replaced symlink is freed at once. var live uint64 var targetIno *inode.Inode - if op.Target != 0 && !op.TargetIsDir && !op.NoReplace { + if op.Target != 0 && !op.TargetIsDir && !op.NoReplace && ttype != dir.DT_LNK { if targetIno, err = f.inodeTbl.Read(uint32(op.Target)); err != nil { return syscall.EIO } diff --git a/internal/goclusterfs/rename_test.go b/internal/goclusterfs/rename_test.go index e86e8416..ef017b30 100644 --- a/internal/goclusterfs/rename_test.go +++ b/internal/goclusterfs/rename_test.go @@ -331,3 +331,35 @@ func TestRenameReplayLeavesADirectoryMovedBackAlone(t *testing.T) { } expectCleanFsck(t, remounted) } + +// No node can hold a symlink open, so a rename over one frees it at once instead of listing it as an orphan. +func TestRenameOverASymlinkFreesItAtOnce(t *testing.T) { + path := newFormattedImage(t) + cluster := newFakeCluster(0, 1) + fsys := cluster.join(t, openFormattedFS(t, path), 1) + ctx := context.Background() + a := createFile(t, fsys, "a") + link := newRegularFile(t, fsys) + if _, err := fsys.updateInode(ctx, link.inodeNum, func(ino *inode.Inode) error { + ino.Mode, ino.LinkCount = inode.S_IFLNK|0o777, 1 + return nil + }); err != nil { + t.Fatal(err) + } + if err := fsys.dirMgr.Add(ctx, readInode(t, fsys, inode.InodeRoot), "l", uint64(link.inodeNum), dir.DT_LNK); err != nil { + t.Fatal(err) + } + + if errno := fsys.Rename(ctx, "a", fsys, "l", 0); errno != 0 { + t.Fatalf("rename over the symlink: %v", errno) + } + if got := named(t, fsys, inode.InodeRoot, "l"); got != a.inodeNum { + t.Errorf("l names inode %d, want %d", got, a.inodeNum) + } + if ino := readInode(t, fsys, link.inodeNum); ino.Magic != 0 { + t.Errorf("the replaced symlink: magic %#x, holders %#b; want it freed at once", ino.Magic, ino.Holders) + } + if got := cluster.list(1); len(got) != 0 { + t.Errorf("orphan list %v after a rename over a symlink, want it empty", got) + } +} diff --git a/tests/test-manifest.yaml b/tests/test-manifest.yaml index e0bedef1..a06febad 100644 --- a/tests/test-manifest.yaml +++ b/tests/test-manifest.yaml @@ -1779,6 +1779,13 @@ scenarios: environments: go: internal/goclusterfs/rename_test.go::TestRenameReplayLeavesADirectoryMovedBackAlone + - id: fs-renameoverasymlinkfreesitatonce + name: Rename over a symlink frees it at once + description: With a peer mounted, a rename over a symlink frees it at once instead of listing it as an orphan, since no node holds a symlink open + priority: go_only + environments: + go: internal/goclusterfs/rename_test.go::TestRenameOverASymlinkFreesItAtOnce + - id: rename-power-renameinonedirectory name: Power failure during a rename in one directory description: Power fails at each write; exactly one of the two names names the file, and fsck finds only leaks From 8cd1aeb32902555f1509d4a878bf606c15ecca84 Mon Sep 17 00:00:00 2001 From: Matthew Mattox Date: Mon, 16 Nov 2026 17:31:03 -0600 Subject: [PATCH 11/11] test(fs): run the rename power-cut tests in parallel Each uses its own image and its own closures, and the hooks they could touch are only set by sequential tests, which Go runs first. Under -race with coverage on .68 their wall time fell from 184 s to 91 s. Co-Authored-By: Claude Opus 5.5 --- internal/goclusterfs/rename_power_cut_test.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/internal/goclusterfs/rename_power_cut_test.go b/internal/goclusterfs/rename_power_cut_test.go index f0e57637..e2a7e00b 100644 --- a/internal/goclusterfs/rename_power_cut_test.go +++ b/internal/goclusterfs/rename_power_cut_test.go @@ -37,6 +37,7 @@ func expectDirLinks(t *testing.T, fsys *FS, num uint32) { } func TestPowerCutDuringRenameInOneDirectory(t *testing.T) { + t.Parallel() data := pattern(4, 4096) var f *File runPowerCuts(t, powerCutCase{ @@ -53,6 +54,7 @@ func TestPowerCutDuringRenameInOneDirectory(t *testing.T) { } func TestPowerCutDuringRenameBetweenDirectories(t *testing.T) { + t.Parallel() var p1, p2 *Dir var f *File runPowerCuts(t, powerCutCase{ @@ -68,6 +70,7 @@ func TestPowerCutDuringRenameBetweenDirectories(t *testing.T) { } func TestPowerCutDuringRenameOverAFile(t *testing.T) { + t.Parallel() var a, b *File runPowerCuts(t, powerCutCase{ setup: func(t *testing.T, fsys *FS) { @@ -86,6 +89,7 @@ func TestPowerCutDuringRenameOverAFile(t *testing.T) { } func TestPowerCutDuringDirectoryMove(t *testing.T) { + t.Parallel() var p1, p2, d *Dir runPowerCuts(t, powerCutCase{ setup: func(t *testing.T, fsys *FS) { @@ -108,6 +112,7 @@ func TestPowerCutDuringDirectoryMove(t *testing.T) { } func TestPowerCutDuringRenameOverAnEmptyDirectory(t *testing.T) { + t.Parallel() var p1, p2, d, e *Dir runPowerCuts(t, powerCutCase{ setup: func(t *testing.T, fsys *FS) {