Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ linters:
# Known findings as of 2026-10-24 in code other open PRs are changing; delete each rule once fixed.
rules:
# errcheck still has findings under these paths; delete a path's rule once it is clean.
- path: ^internal/goclusterfs/
linters: [errcheck]

- path: ^cmd/mount/
linters: [errcheck]

Expand Down
22 changes: 11 additions & 11 deletions internal/goclusterfs/crash_frees_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestUnlinkCrashBeforeInodeFreeKeepsCloneBlocks(t *testing.T) {
if err != nil {
t.Fatal(err)
}
t.Cleanup(func() { otherDev.Close() })
t.Cleanup(func() { _ = otherDev.Close() })
slot := int(snapFSNodes.Add(1)-1) % 64
var otherUUID [16]byte
copy(otherUUID[:], "inodeholder12345")
Expand All @@ -103,7 +103,7 @@ func TestUnlinkCrashBeforeInodeFreeKeepsCloneBlocks(t *testing.T) {
t.Fatal(err)
}

go sub.Unlink(ctx, "doomed.bin")
go func() { _ = sub.Unlink(ctx, "doomed.bin") }()
for deadline := time.Now().Add(10 * time.Second); ; time.Sleep(10 * time.Millisecond) {
subIno, err := fsys.inodeTbl.Read(sub.inodeNum)
if err != nil {
Expand All @@ -121,7 +121,7 @@ func TestUnlinkCrashBeforeInodeFreeKeepsCloneBlocks(t *testing.T) {
t.Fatalf("inode %d freed while another node holds its lock (magic %#x, err %v)", f.inodeNum, ino.Magic, err)
}
expectRefcounts(t, fsys, blocks, 2)
fsys.dev.Close()
_ = fsys.dev.Close()
if err := other.Release(lock.LockTypeInode, uint64(f.inodeNum)); err != nil {
t.Fatal(err)
}
Expand All @@ -141,7 +141,7 @@ func TestUnlinkCrashAfterInodeFreeKeepsCloneBlocks(t *testing.T) {
blocks := cloneShares(t, fsys, f, 2)

crashed := crashAfterCommit(t, fsys, "unlink: inode freed")
go sub.Unlink(context.Background(), "doomed.bin")
go func() { _ = sub.Unlink(context.Background(), "doomed.bin") }()
<-crashed

remounted := remountAndReplay(t, path, offset, size)
Expand All @@ -161,7 +161,7 @@ func TestUnlinkCrashAfterBlockFreeKeepsReusedBlock(t *testing.T) {
other := createFile(t, fsys, "other.bin")

crashed := crashAfterCommit(t, fsys, "unlink: blocks freed")
go sub.Unlink(context.Background(), "doomed.bin")
go func() { _ = sub.Unlink(context.Background(), "doomed.bin") }()
<-crashed

data := pattern(9, 4*BlockSize)
Expand Down Expand Up @@ -207,7 +207,7 @@ func TestFailedWriteCrashKeepsReusedBlock(t *testing.T) {
if err := writeImageRange(path, offset, journalAtFree); err != nil {
t.Fatal(err)
}
fsys.dev.Close()
_ = fsys.dev.Close()

remounted := remountAndReplay(t, path, offset, size)
ino, err := remounted.inodeTbl.Read(f.inodeNum)
Expand All @@ -226,7 +226,7 @@ func readImageRange(path string, off, n uint64) ([]byte, error) {
if err != nil {
return nil, err
}
defer f.Close()
defer func() { _ = f.Close() }()
b := make([]byte, n)
_, err = f.ReadAt(b, int64(off))
return b, err
Expand All @@ -237,9 +237,9 @@ func writeImageRange(path string, off uint64, b []byte) error {
if err != nil {
return err
}
defer f.Close()
if _, err := f.WriteAt(b, int64(off)); err != nil {
return err
_, err = f.WriteAt(b, int64(off))
if err == nil {
err = f.Sync()
}
return f.Sync()
return errors.Join(err, f.Close())
}
2 changes: 1 addition & 1 deletion internal/goclusterfs/extent_tree_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func openTreeFS(t *testing.T, path string) *FS {
if err != nil {
t.Fatalf("open %s: %v", path, err)
}
t.Cleanup(func() { dev.Close() })
t.Cleanup(func() { _ = dev.Close() })

var nodeUUID [16]byte
copy(nodeUUID[:], "testnode12345678")
Expand Down
89 changes: 53 additions & 36 deletions internal/goclusterfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,17 @@ func (f *FS) lockInode(num uint32) func() {
return mu.Unlock
}

// release releases a cluster lock and logs a failure; kind names the resource, such as "inode".
func (f *FS) release(lockType lock.LockType, id uint64, kind string) {
err := f.lockMgr.Release(lockType, id)
switch {
case errors.Is(err, lock.ErrNotLockOwner):
log.Printf("[ERROR] released the lock on %s %d that this node does not hold (double release or lock bookkeeping bug)", kind, id)
case err != nil:
log.Printf("[WARN] releasing the lock on %s %d failed: %v", kind, id, err)
}
}

// updateInode re-reads an inode, applies update, and writes it back, starting
// over when another node changed the inode's extent tree in between.
func (f *FS) updateInode(ctx context.Context, num uint32, update func(*inode.Inode) error) (*inode.Inode, error) {
Expand Down Expand Up @@ -391,7 +402,7 @@ func (f *FS) updateUnderInodeLock(ctx context.Context, num uint32, update func(*
if err := f.lockMgr.Acquire(ctx, lock.LockTypeInode, uint64(num), lock.LockModeExclusive); err != nil {
return nil, fmt.Errorf("failed to acquire inode lock: %w", err)
}
defer f.lockMgr.Release(lock.LockTypeInode, uint64(num))
defer f.release(lock.LockTypeInode, uint64(num), "inode")
}

ino, err := f.inodeTbl.Read(num)
Expand Down Expand Up @@ -758,7 +769,7 @@ func (f *FS) unmapped(num uint32, blocks []uint64) []uint64 {
return unused
}

func (f *FS) freeUnused(ctx context.Context, num uint32, blocks []uint64) {
func (f *FS) freeUnused(ctx context.Context, num uint32, blocks ...uint64) {
for _, b := range blocks {
if err := f.extentMgr.FreePhysicalBlock(ctx, b); err != nil {
log.Printf("[WARN] inode %d: freeing unused block %d failed: %v", num, b, err)
Expand All @@ -772,11 +783,19 @@ func (f *FS) dropNewInode(ctx context.Context, ino *inode.Inode) {
if err != nil {
log.Printf("[WARN] inode %d: blocks of the failed new inode unknown, they may leak: %v", ino.InodeNum, err)
}
if err := f.inodeTbl.Free(ctx, ino.InodeNum); err != nil {
log.Printf("[WARN] inode %d: freeing the failed new inode failed: %v", ino.InodeNum, err)
if !f.freeNewInode(ctx, ino.InodeNum) {
return
}
f.freeUnused(ctx, ino.InodeNum, f.unmapped(ino.InodeNum, blocks))
f.freeUnused(ctx, ino.InodeNum, f.unmapped(ino.InodeNum, blocks)...)
}

// freeNewInode frees an inode that a failed operation claimed before anything referenced it.
func (f *FS) freeNewInode(ctx context.Context, num uint32) bool {
if err := f.inodeTbl.Free(ctx, num); err != nil {
log.Printf("[WARN] inode %d: freeing the failed new inode failed: %v", num, err)
return false
}
return true
}

// testHookBeforeMkdirInodeWrite, when set by a test, fails a mkdir's inode write with its error.
Expand Down Expand Up @@ -957,7 +976,7 @@ func (f *FS) createFileOnce(ctx context.Context, parent *fs.Inode, parentNum uin
childIno.Ctime = now
if err := f.inodeTbl.Write(ctx, childIno); err != nil {
logging.Debugf("Create %q: Write inode %d failed: %v", name, childIno.InodeNum, err)
f.inodeTbl.Free(ctx, childIno.InodeNum)
f.freeNewInode(ctx, childIno.InodeNum)
return nil, syscall.EIO
}
if afterCommit != nil {
Expand All @@ -966,7 +985,7 @@ func (f *FS) createFileOnce(ctx context.Context, parent *fs.Inode, parentNum uin

if err := f.dirMgr.Add(ctx, dirIno, name, uint64(inodeNum), dir.DT_REG); err != nil {
logging.Debugf("Create %q: dirMgr.Add failed: %v", name, err)
f.inodeTbl.Free(ctx, childIno.InodeNum)
f.freeNewInode(ctx, childIno.InodeNum)
return nil, nameErrno(err)
}
f.cacheInode(childIno)
Expand Down Expand Up @@ -1525,7 +1544,9 @@ func (d *Dir) removeEntryOnce(ctx context.Context, name string, isDir bool) sysc
afterCommit("unlink: inode freed")
}
sfbBacked, sfbSlot := freed.IsSFBBacked(), freed.InlineExtents[0].PhysicalBlock
d.fs.extentMgr.Delete(ctx, freed)
if err := d.fs.extentMgr.Delete(ctx, freed); err != nil {
log.Printf("[WARN] removeEntry %q: freeing the blocks of inode %d failed, some may leak: %v", name, childInodeNum, err)
}
if sfbBacked {
d.fs.freeSFBSlot(ctx, sfbSlot)
}
Expand Down Expand Up @@ -1793,9 +1814,7 @@ func (f *File) waitOutReplay(ctx context.Context, physBlock uint64) error {
log.Printf("[WARN] Write inode %d: waiting for block %d after its holder's replay failed: %v", f.inodeNum, physBlock, err)
return syscall.EIO
}
if err := f.fs.lockMgr.ReleaseExtent(physBlock); err != nil {
logging.Debugf("Write inode %d: releasing block %d after the replay wait failed: %v", f.inodeNum, physBlock, err)
}
f.fs.release(lock.LockTypeExtent, physBlock, "block")
return errRetryWrite
}

Expand Down Expand Up @@ -1869,22 +1888,22 @@ func (f *File) allocateBlockLocked(ctx context.Context, ino *inode.Inode, blockN
treeCtx, releaseTree, err := f.fs.extentMgr.ReserveTreeBlocks(ctx, ino)
if err != nil {
logging.Debugf("Write inode %d: reserving extent-tree blocks failed: %v", f.inodeNum, err)
f.fs.bitmapMgr.Free(ctx, ext.PhysicalBlock)
f.fs.freeUnused(ctx, f.inodeNum, ext.PhysicalBlock)
return nil, nil, nil, allocErrno(err)
}

if err := f.fs.lockMgr.Acquire(ctx, lock.LockTypeInode, uint64(f.inodeNum), lock.LockModeExclusive); err != nil {
f.fs.bitmapMgr.Free(ctx, ext.PhysicalBlock)
f.fs.freeUnused(ctx, f.inodeNum, ext.PhysicalBlock)
releaseTree()
return nil, nil, nil, syscall.EIO
}
var fresh *inode.Inode
fail := func(errno error) (*inode.Inode, *extent.Extent, func(), error) {
f.fs.lockMgr.Release(lock.LockTypeInode, uint64(f.inodeNum))
f.fs.release(lock.LockTypeInode, uint64(f.inodeNum), "inode")
if fresh != nil {
f.fs.extentMgr.DiscardPending(ctx, fresh)
}
f.fs.bitmapMgr.Free(ctx, ext.PhysicalBlock)
f.fs.freeUnused(ctx, f.inodeNum, ext.PhysicalBlock)
releaseTree()
return nil, nil, nil, errno
}
Expand Down Expand Up @@ -1921,7 +1940,7 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i
var releaseReservations []func()
releaseInodeLock := func() {
if lockHeld && f.fs.lockMgr != nil {
f.fs.lockMgr.Release(lock.LockTypeInode, uint64(f.inodeNum))
f.fs.release(lock.LockTypeInode, uint64(f.inodeNum), "inode")
lockHeld = false
}
}
Expand Down Expand Up @@ -2142,7 +2161,7 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i
dropCopy := func() {
releaseInodeLock()
f.fs.extentMgr.DiscardPending(ctx, ino)
f.fs.extentMgr.FreePhysicalBlock(ctx, cowResult.NewPhysBlock)
f.fs.freeUnused(ctx, f.inodeNum, cowResult.NewPhysBlock)
}
if beforeCoWCommit != nil {
beforeCoWCommit()
Expand Down Expand Up @@ -2248,9 +2267,7 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i
inPlaceLockHeld := false
releaseInPlaceLock := func() {
if inPlaceLockHeld {
if err := f.fs.lockMgr.Release(lock.LockTypeInode, uint64(f.inodeNum)); err != nil {
logging.Debugf("Write inode %d: releasing the shared inode lock failed: %v", f.inodeNum, err)
}
f.fs.release(lock.LockTypeInode, uint64(f.inodeNum), "inode")
inPlaceLockHeld = false
}
}
Expand Down Expand Up @@ -2305,7 +2322,7 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i
if err := f.fs.zeroBlock(physBlock); err != nil {
logging.Debugf("Write inode %d: zeroing block %d failed: %v", f.inodeNum, physBlock, err)
if extentLockHeld {
f.fs.lockMgr.ReleaseExtent(physBlock)
f.fs.release(lock.LockTypeExtent, physBlock, "block")
}
releaseInPlaceLock()
return 0, syscall.EIO
Expand Down Expand Up @@ -2344,7 +2361,7 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i
logging.Debugf("Write inode %d: WriteAt offset %d failed: %v", f.inodeNum, alignedOffset, err)
storage.PutBuffer(buf)
if extentLockHeld {
f.fs.lockMgr.ReleaseExtent(physBlock)
f.fs.release(lock.LockTypeExtent, physBlock, "block")
}
releaseInPlaceLock()
return 0, syscall.EIO
Expand All @@ -2358,7 +2375,7 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i

// Release extent lock after completing write to this block
if extentLockHeld {
f.fs.lockMgr.ReleaseExtent(physBlock)
f.fs.release(lock.LockTypeExtent, physBlock, "block")
}
releaseInPlaceLock()

Expand Down Expand Up @@ -2508,7 +2525,7 @@ func (f *File) writeInternal(ctx context.Context, data []byte, offset uint64) (i
testHookBeforeFreeingUnused()
}
if !conflict {
f.fs.freeUnused(ctx, f.inodeNum, unused)
f.fs.freeUnused(ctx, f.inodeNum, unused...)
cowDone = cowDone[:committedCoW]
return syscall.EIO
}
Expand Down Expand Up @@ -2608,7 +2625,7 @@ func (f *FS) freeSFBSlot(ctx context.Context, packed uint64) {
log.Printf("[WARN] Free SFB slot %d:%d: container lock failed: %v", container, slot, err)
return
}
defer f.lockMgr.Release(lock.LockTypeSFBContainer, container)
defer f.release(lock.LockTypeSFBContainer, container, "SFB container")
}

buf := storage.GetBuffer(storage.SectorSize)
Expand Down Expand Up @@ -2662,7 +2679,7 @@ func (f *File) migrateToExtent(ctx context.Context, ino *inode.Inode) error {
_, err = f.fs.dev.WriteAt(buf.Bytes()[:BlockSize], int64(f.fs.dataOffset+newBlock*BlockSize))
storage.PutBuffer(buf)
if err != nil {
_ = f.fs.bitmapMgr.Free(ctx, newBlock)
f.fs.freeUnused(ctx, f.inodeNum, newBlock)
return fmt.Errorf("failed to write migrated data: %w", err)
}

Expand All @@ -2677,14 +2694,14 @@ func (f *File) migrateToExtent(ctx context.Context, ino *inode.Inode) error {
NewBlock: newBlock,
}); err != nil {
f.fs.journal.Abort(txn)
_ = f.fs.bitmapMgr.Free(ctx, newBlock)
f.fs.freeUnused(ctx, f.inodeNum, newBlock)
return err
}
// === COMMIT POINT ===
var commitErr error
intentSeq, commitErr = f.fs.journal.Commit(txn)
if commitErr != nil {
_ = f.fs.bitmapMgr.Free(ctx, newBlock)
f.fs.freeUnused(ctx, f.inodeNum, newBlock)
return commitErr
}
defer f.fs.closeIntent(intentSeq)
Expand All @@ -2698,7 +2715,7 @@ func (f *File) migrateToExtent(ctx context.Context, ino *inode.Inode) error {
if err := f.fs.inodeTbl.Write(ctx, ino); err != nil {
// Only a conflict proves the inode on disk does not map newBlock.
if errors.Is(err, extent.ErrTreeConflict) {
_ = f.fs.bitmapMgr.Free(ctx, newBlock)
f.fs.freeUnused(ctx, f.inodeNum, newBlock)
}
return retryOnTreeConflict(err)
}
Expand Down Expand Up @@ -2883,7 +2900,9 @@ func (f *File) setattr(ctx context.Context, fh fs.FileHandle, in *fuse.SetAttrIn
if errors.Is(err, extent.ErrTreeConflict) {
// Nothing was applied, so close the intent before the retry logs a new one.
if f.fs.journal != nil && intentSeq > 0 {
f.fs.journal.CompleteIntent(intentSeq)
if err := f.fs.journal.CompleteIntent(intentSeq); err != nil {
log.Printf("[WARN] Setattr inode %d: closing intent %d before the retry failed: %v", f.inodeNum, intentSeq, err)
}
}
return 0, err
}
Expand Down Expand Up @@ -2956,9 +2975,7 @@ func (f *File) allocate(ctx context.Context, off uint64, size uint64, mode uint3
defer func() {
if !committed {
f.fs.extentMgr.DiscardPending(ctx, ino)
for _, b := range newBlocks {
f.fs.extentMgr.FreePhysicalBlock(ctx, b)
}
f.fs.freeUnused(ctx, f.inodeNum, newBlocks...)
}
}()

Expand Down Expand Up @@ -3032,7 +3049,7 @@ func (f *File) allocate(ctx context.Context, off uint64, size uint64, mode uint3
logging.Debugf("Allocate inode %d: lock failed: %v", f.inodeNum, err)
return syscall.EIO, nil
}
defer f.fs.lockMgr.Release(lock.LockTypeInode, uint64(f.inodeNum))
defer f.fs.release(lock.LockTypeInode, uint64(f.inodeNum), "inode")

// ino holds the new extents, so keep it only if no node wrote the inode since it was read.
fresh, err := f.fs.inodeTbl.Read(f.inodeNum)
Expand Down Expand Up @@ -3333,7 +3350,7 @@ func (f *File) createSnapshot(ctx context.Context, name string) syscall.Errno {
data.Mtime, data.Ctime = src.Mtime, src.Ctime
data.Flags |= inode.FlagSnapshotData
if err := f.fs.inodeTbl.Write(ctx, data); err != nil {
f.fs.inodeTbl.Free(ctx, data.InodeNum)
f.fs.freeNewInode(ctx, data.InodeNum)
log.Printf("[snapshot] Failed to write data inode %d: %v", data.InodeNum, err)
return syscall.EIO
}
Expand All @@ -3342,7 +3359,7 @@ func (f *File) createSnapshot(ctx context.Context, name string) syscall.Errno {
data.InodeNum, src.AllocatedBlocks, src.Size, snapshot.FlagReadOnly)
if err != nil {
log.Printf("[snapshot] Failed to add '%s' to the registry: %v", name, err)
f.fs.inodeTbl.Free(ctx, data.InodeNum)
f.fs.freeNewInode(ctx, data.InodeNum)
if errors.Is(err, snapshot.ErrSnapshotExists) {
return syscall.EEXIST
}
Expand Down
Loading
Loading