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
4 changes: 4 additions & 0 deletions .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ version: "2"
linters:
default: none
enable:
- errcheck
- govet
- ineffassign
- staticcheck
Expand All @@ -21,6 +22,9 @@ linters:
exclusions:
# Known findings as of 2026-10-24 in code other open PRs are changing; delete each rule once fixed.
rules:
# errcheck is enforced only in packages already cleaned up; widen this as others are.
- path-except: ^(internal/(journal|bitmap|refcount|trim|superblock|storage|node|metrics)|cmd/crctest)/
linters: [errcheck]
- path: ^internal/goclusterfs/fs\.go$
linters: [unused]
text: "errRetryCreate|field sb is unused"
Expand Down
43 changes: 26 additions & 17 deletions internal/bitmap/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func (r *Region) Alloc(ctx context.Context, count int) ([]uint64, error) {
if err := r.lockMgr.Acquire(ctx, lock.LockTypeBlockBitmap, 0, lock.LockModeExclusive); err != nil {
return nil, fmt.Errorf("failed to acquire bitmap lock: %w", err)
}
defer r.lockMgr.Release(lock.LockTypeBlockBitmap, 0)
defer r.unlock()
}

r.mu.Lock()
Expand All @@ -107,11 +107,7 @@ func (r *Region) Alloc(ctx context.Context, count int) ([]uint64, error) {
for sectorIdx := uint64(0); sectorIdx < numSectors && len(allocated) < count; sectorIdx++ {
sectorData, err := r.readSector(sectorIdx)
if err != nil {
// Rollback allocations made so far
for _, blockNum := range allocated {
r.freeBlockInSector(blockNum)
}
return nil, fmt.Errorf("failed to read bitmap sector %d: %w", sectorIdx, err)
return nil, r.rollback(allocated, fmt.Errorf("failed to read bitmap sector %d: %w", sectorIdx, err))
}

modified := false
Expand Down Expand Up @@ -141,27 +137,40 @@ func (r *Region) Alloc(ctx context.Context, count int) ([]uint64, error) {

if modified {
if err := r.writeSector(sectorIdx, sectorData); err != nil {
// Rollback allocations made so far
for _, blockNum := range allocated {
r.freeBlockInSector(blockNum)
}
return nil, fmt.Errorf("failed to write bitmap sector %d: %w", sectorIdx, err)
return nil, r.rollback(allocated, fmt.Errorf("failed to write bitmap sector %d: %w", sectorIdx, err))
}
}
}

if len(allocated) < count {
// Not enough free blocks - rollback
for _, blockNum := range allocated {
r.freeBlockInSector(blockNum)
}
return nil, ErrNoFreeBlocks
return nil, r.rollback(allocated, ErrNoFreeBlocks)
}

log.Printf("[DEBUG] bitmap.Alloc: allocated blocks %v", allocated)
return allocated, nil
}

// rollback frees the blocks a failed Alloc had marked and returns cause, joined
// with an error naming each block it could not free.
func (r *Region) rollback(blocks []uint64, cause error) error {
errs := []error{cause}
for _, b := range blocks {
if err := r.freeBlockInSector(b); err != nil {
errs = append(errs, fmt.Errorf("rolling back block %d: %w", b, err))
}
}
if len(errs) == 1 {
return cause
}
return errors.Join(errs...)
}

func (r *Region) unlock() {
if err := r.lockMgr.Release(lock.LockTypeBlockBitmap, 0); err != nil {
log.Printf("[WARN] bitmap: releasing the bitmap lock failed: %v", err)
}
}

// Free releases a single block back to the free pool.
func (r *Region) Free(ctx context.Context, blockNum uint64) error {
if blockNum >= r.totalBlocks {
Expand All @@ -173,7 +182,7 @@ func (r *Region) Free(ctx context.Context, blockNum uint64) error {
if err := r.lockMgr.Acquire(ctx, lock.LockTypeBlockBitmap, 0, lock.LockModeExclusive); err != nil {
return fmt.Errorf("failed to acquire bitmap lock: %w", err)
}
defer r.lockMgr.Release(lock.LockTypeBlockBitmap, 0)
defer r.unlock()
}

r.mu.Lock()
Expand Down
46 changes: 30 additions & 16 deletions internal/bitmap/bitmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,23 @@ func createTestDevice(tb testing.TB, size int64) (*storage.Device, func()) {
}

if err := f.Truncate(size); err != nil {
f.Close()
os.Remove(testFile)
_ = f.Close()
_ = os.Remove(testFile)
tb.Skipf("Cannot truncate test file: %v", err)
}
f.Close()
if err := f.Close(); err != nil {
tb.Fatal(err)
}

dev, err := storage.Open(testFile)
if err != nil {
os.Remove(testFile)
_ = os.Remove(testFile)
tb.Skipf("O_DIRECT not supported: %v", err)
}

cleanup := func() {
dev.Close()
os.Remove(testFile)
_ = dev.Close()
_ = os.Remove(testFile)
}

return dev, cleanup
Expand Down Expand Up @@ -486,7 +488,7 @@ func TestPersistence(t *testing.T) {
}

testFile := filepath.Join(tmpDir, "test_bitmap_persist.bin")
defer os.Remove(testFile)
defer func() { _ = os.Remove(testFile) }()

// Create and allocate
func() {
Expand All @@ -495,26 +497,32 @@ func TestPersistence(t *testing.T) {
t.Skipf("Cannot create test file: %v", err)
}
if err := f.Truncate(10 << 20); err != nil {
f.Close()
_ = f.Close()
t.Skipf("Cannot truncate: %v", err)
}
f.Close()
if err := f.Close(); err != nil {
t.Fatal(err)
}

dev, err := storage.Open(testFile)
if err != nil {
t.Skipf("O_DIRECT not supported: %v", err)
}
defer dev.Close()
defer func() { _ = dev.Close() }()

var uuid [16]byte
region := NewRegion(dev, 0, 100, nil, uuid)

ctx := context.Background()

// Allocate blocks 0-9 and 50-59
region.Alloc(ctx, 10)
if _, err := region.Alloc(ctx, 10); err != nil {
t.Fatal(err)
}
for i := uint64(50); i < 60; i++ {
region.MarkAllocated(ctx, i)
if err := region.MarkAllocated(ctx, i); err != nil {
t.Fatal(err)
}
}
}()

Expand All @@ -523,7 +531,7 @@ func TestPersistence(t *testing.T) {
if err != nil {
t.Skipf("Cannot reopen: %v", err)
}
defer dev.Close()
defer func() { _ = dev.Close() }()

var uuid [16]byte
region := NewRegion(dev, 0, 100, nil, uuid)
Expand Down Expand Up @@ -621,7 +629,9 @@ func BenchmarkAlloc(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
region.Alloc(ctx, 1)
if _, err := region.Alloc(ctx, 1); err != nil {
b.Fatal(err)
}
}
}

Expand All @@ -639,7 +649,9 @@ func BenchmarkFree(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
region.Free(ctx, blocks[i])
if err := region.Free(ctx, blocks[i]); err != nil {
b.Fatal(err)
}
}
}

Expand All @@ -665,6 +677,8 @@ func BenchmarkIsFree(b *testing.B) {

b.ResetTimer()
for i := 0; i < b.N; i++ {
region.IsFree(uint64(i % 10000))
if _, err := region.IsFree(uint64(i % 10000)); err != nil {
b.Fatal(err)
}
}
}
6 changes: 5 additions & 1 deletion internal/journal/cross_node_recovery.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,11 @@ func ReplayJournal(ctx context.Context, rctx *RecoveryContext, info *node.Failed
if err != nil {
return nil, fmt.Errorf("failed to open journal region: %w", err)
}
defer region.Close()
defer func() {
if err := region.Close(); err != nil {
log.Printf("[Recovery] Closing journal region failed: %v", err)
}
}()
if owner := region.Superblock().NodeUUID; owner != info.UUID {
log.Printf("[Recovery] Journal was written by earlier slot owner %x", owner[:4])
}
Expand Down
18 changes: 11 additions & 7 deletions internal/journal/cross_node_recovery_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@ func testRecoveryContext(t testing.TB) (*RecoveryContext, func()) {
}
testFile := f.Name()
if err := f.Truncate(10 * 1024 * 1024); err != nil {
f.Close()
os.Remove(testFile)
_ = f.Close()
_ = os.Remove(testFile)
t.Fatalf("Failed to truncate: %v", err)
}
f.Close()
if err := f.Close(); err != nil {
t.Fatal(err)
}

// Layout: 0-1MB reserved, 1-2MB bitmap, 2-3MB inodes, 3MB+ data
const (
Expand All @@ -42,7 +44,7 @@ func testRecoveryContext(t testing.TB) (*RecoveryContext, func()) {

dev, err := storage.Open(testFile)
if err != nil {
os.Remove(testFile)
_ = os.Remove(testFile)
t.Skipf("O_DIRECT not supported: %v", err)
}

Expand All @@ -63,8 +65,8 @@ func testRecoveryContext(t testing.TB) (*RecoveryContext, func()) {
}

cleanup := func() {
dev.Close()
os.Remove(testFile)
_ = dev.Close()
_ = os.Remove(testFile)
}

return rctx, cleanup
Expand Down Expand Up @@ -394,7 +396,9 @@ func TestApplyDirRemoveEntry(t *testing.T) {
}
parentIno, _ := rctx.InodeTbl.Read(inode.InodeRoot)
parentIno.LinkCount = 2
rctx.InodeTbl.Write(ctx, parentIno)
if err := rctx.InodeTbl.Write(ctx, parentIno); err != nil {
t.Fatal(err)
}
parentIno, _ = rctx.InodeTbl.Read(inode.InodeRoot)
if err := rctx.DirMgr.InitDirectory(ctx, parentIno, uint64(inode.InodeRoot)); err != nil {
t.Fatalf("Setup: failed to init directory: %v", err)
Expand Down
16 changes: 12 additions & 4 deletions internal/journal/intent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,9 @@ func TestJournalCompleteIntent(t *testing.T) {

// Begin and commit a transaction
txn := j.Begin()
txn.LogInodeCreate(testInodePayload(100))
if err := txn.LogInodeCreate(testInodePayload(100)); err != nil {
t.Fatal(err)
}

intentSeq, err := j.Commit(txn)
if err != nil {
Expand Down Expand Up @@ -126,7 +128,9 @@ func TestRecoveryWithIntentCompletion(t *testing.T) {

// Transaction 1: Commit and complete
txn1 := j1.Begin()
txn1.LogInodeCreate(testInodePayload(100))
if err := txn1.LogInodeCreate(testInodePayload(100)); err != nil {
t.Fatal(err)
}
seq1, err := j1.Commit(txn1)
if err != nil {
t.Fatalf("Commit txn1 failed: %v", err)
Expand All @@ -137,7 +141,9 @@ func TestRecoveryWithIntentCompletion(t *testing.T) {

// Transaction 2: Commit but DON'T complete (simulates crash during execution)
txn2 := j1.Begin()
txn2.LogInodeCreate(testInodePayload(101))
if err := txn2.LogInodeCreate(testInodePayload(101)); err != nil {
t.Fatal(err)
}
seq2, err := j1.Commit(txn2)
if err != nil {
t.Fatalf("Commit txn2 failed: %v", err)
Expand All @@ -146,7 +152,9 @@ func TestRecoveryWithIntentCompletion(t *testing.T) {

// Transaction 3: Commit and complete
txn3 := j1.Begin()
txn3.LogInodeCreate(testInodePayload(102))
if err := txn3.LogInodeCreate(testInodePayload(102)); err != nil {
t.Fatal(err)
}
seq3, err := j1.Commit(txn3)
if err != nil {
t.Fatalf("Commit txn3 failed: %v", err)
Expand Down
Loading
Loading