diff --git a/.golangci.yml b/.golangci.yml index 67d65707..1c54c0b6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -23,7 +23,7 @@ linters: # 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|lock|inode|extent)|cmd/crctest)/ + - path-except: ^(internal/(journal|bitmap|refcount|trim|superblock|storage|node|metrics|lock|inode|extent|snapshot)|cmd/crctest)/ linters: [errcheck] - path: ^internal/goclusterfs/fs\.go$ linters: [unused] diff --git a/internal/snapshot/snapshot.go b/internal/snapshot/snapshot.go index 3c6f0cc0..2ccf468f 100644 --- a/internal/snapshot/snapshot.go +++ b/internal/snapshot/snapshot.go @@ -18,6 +18,7 @@ import ( "errors" "fmt" "iter" + "log" "sync" "time" @@ -271,6 +272,18 @@ func NewRegistry(dev *storage.Device, offset, size uint64, lockMgr *lock.Manager } } +// unlock releases the registry lock. ErrNotLockOwner means a double release: +// the extra one may have dropped another holder's lock early. +func (r *Registry) unlock() { + err := r.lockMgr.Release(lock.LockTypeSnapshot, 0) + switch { + case errors.Is(err, lock.ErrNotLockOwner): + log.Printf("[ERROR] snapshot registry: released the registry lock that this node does not hold (double release or lock bookkeeping bug)") + case err != nil: + log.Printf("[WARN] snapshot registry: releasing the registry lock failed: %v", err) + } +} + // Create creates a new snapshot descriptor. // // Parameters: @@ -297,7 +310,7 @@ func (r *Registry) Create(ctx context.Context, name string, targetInode uint32, if err := r.lockMgr.Acquire(ctx, lock.LockTypeSnapshot, 0, lock.LockModeExclusive); err != nil { return nil, fmt.Errorf("failed to acquire snapshot lock: %w", err) } - defer r.lockMgr.Release(lock.LockTypeSnapshot, 0) + defer r.unlock() } r.mu.Lock() @@ -435,7 +448,7 @@ func (r *Registry) UpdateParentID(ctx context.Context, snapshotID, newParentID u if err := r.lockMgr.Acquire(ctx, lock.LockTypeSnapshot, 0, lock.LockModeExclusive); err != nil { return fmt.Errorf("failed to acquire snapshot lock: %w", err) } - defer r.lockMgr.Release(lock.LockTypeSnapshot, 0) + defer r.unlock() } r.mu.Lock() @@ -481,7 +494,7 @@ func (r *Registry) updateState(ctx context.Context, snapshotID uint64, newState, if err := r.lockMgr.Acquire(ctx, lock.LockTypeSnapshot, 0, lock.LockModeExclusive); err != nil { return fmt.Errorf("failed to acquire snapshot lock: %w", err) } - defer r.lockMgr.Release(lock.LockTypeSnapshot, 0) + defer r.unlock() } r.mu.Lock() @@ -515,7 +528,7 @@ func (r *Registry) SetRefCountJobID(ctx context.Context, snapshotID, jobID uint6 if err := r.lockMgr.Acquire(ctx, lock.LockTypeSnapshot, 0, lock.LockModeExclusive); err != nil { return fmt.Errorf("failed to acquire snapshot lock: %w", err) } - defer r.lockMgr.Release(lock.LockTypeSnapshot, 0) + defer r.unlock() } r.mu.Lock() @@ -549,7 +562,7 @@ func (r *Registry) Remove(ctx context.Context, snapshotID uint64) error { if err := r.lockMgr.Acquire(ctx, lock.LockTypeSnapshot, 0, lock.LockModeExclusive); err != nil { return fmt.Errorf("failed to acquire snapshot lock: %w", err) } - defer r.lockMgr.Release(lock.LockTypeSnapshot, 0) + defer r.unlock() } r.mu.Lock() diff --git a/internal/snapshot/snapshot_test.go b/internal/snapshot/snapshot_test.go index f1a6b500..1c0dffa9 100644 --- a/internal/snapshot/snapshot_test.go +++ b/internal/snapshot/snapshot_test.go @@ -29,21 +29,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 @@ -310,8 +312,12 @@ func TestRegistryGetByName(t *testing.T) { ctx := context.Background() // Create snapshots - registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly) - registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly) + if _, err := registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil { + t.Fatal(err) + } + if _, err := registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly); err != nil { + t.Fatal(err) + } // Get by name desc, err := registry.GetByName(ctx, "snap2") @@ -364,9 +370,15 @@ func TestRegistryList(t *testing.T) { ctx := context.Background() // Create several snapshots - registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly) - registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly) - registry.Create(ctx, "hidden", 300, InvalidSnapshotID, 3000, 150, 3072, FlagHidden) + if _, err := registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil { + t.Fatal(err) + } + if _, err := registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly); err != nil { + t.Fatal(err) + } + if _, err := registry.Create(ctx, "hidden", 300, InvalidSnapshotID, 3000, 150, 3072, FlagHidden); err != nil { + t.Fatal(err) + } // List should not include hidden snapshots, err := registry.List(ctx) @@ -386,9 +398,15 @@ func TestRegistryListForInode(t *testing.T) { ctx := context.Background() // Create snapshots for different inodes - registry.Create(ctx, "snap1-a", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly) - registry.Create(ctx, "snap1-b", 100, InvalidSnapshotID, 1001, 51, 1025, FlagReadOnly) - registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly) + if _, err := registry.Create(ctx, "snap1-a", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil { + t.Fatal(err) + } + if _, err := registry.Create(ctx, "snap1-b", 100, InvalidSnapshotID, 1001, 51, 1025, FlagReadOnly); err != nil { + t.Fatal(err) + } + if _, err := registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly); err != nil { + t.Fatal(err) + } // List for inode 100 snapshots, err := registry.ListForInode(ctx, 100) @@ -449,7 +467,9 @@ func TestRegistryDelete(t *testing.T) { // Create and activate a snapshot desc, _ := registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly) - registry.UpdateState(ctx, desc.SnapshotID, StateActive) + if err := registry.UpdateState(ctx, desc.SnapshotID, StateActive); err != nil { + t.Fatal(err) + } // Delete (mark for deletion) err := registry.Delete(ctx, desc.SnapshotID) @@ -480,8 +500,12 @@ func TestRegistryRemove(t *testing.T) { // Create, activate, and delete a snapshot desc, _ := registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly) - registry.UpdateState(ctx, desc.SnapshotID, StateActive) - registry.Delete(ctx, desc.SnapshotID) + if err := registry.UpdateState(ctx, desc.SnapshotID, StateActive); err != nil { + t.Fatal(err) + } + if err := registry.Delete(ctx, desc.SnapshotID); err != nil { + t.Fatal(err) + } // Remove permanently err := registry.Remove(ctx, desc.SnapshotID) @@ -510,7 +534,9 @@ func TestRegistryRemoveNotDeleting(t *testing.T) { // Create an active snapshot desc, _ := registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly) - registry.UpdateState(ctx, desc.SnapshotID, StateActive) + if err := registry.UpdateState(ctx, desc.SnapshotID, StateActive); err != nil { + t.Fatal(err) + } // Try to remove without deleting first err := registry.Remove(ctx, desc.SnapshotID) @@ -551,8 +577,12 @@ func TestRegistryInitialize(t *testing.T) { ctx := context.Background() // Create some snapshots - registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly) - registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly) + if _, err := registry.Create(ctx, "snap1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil { + t.Fatal(err) + } + if _, err := registry.Create(ctx, "snap2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly); err != nil { + t.Fatal(err) + } // Initialize should clear everything err := registry.Initialize(ctx) @@ -580,17 +610,25 @@ func TestRegistryScan(t *testing.T) { // Create snapshots in various states desc1, _ := registry.Create(ctx, "active1", 100, InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly) - registry.UpdateState(ctx, desc1.SnapshotID, StateActive) + if err := registry.UpdateState(ctx, desc1.SnapshotID, StateActive); err != nil { + t.Fatal(err) + } desc2, _ := registry.Create(ctx, "active2", 200, InvalidSnapshotID, 2000, 100, 2048, FlagReadOnly) - registry.UpdateState(ctx, desc2.SnapshotID, StateActive) + if err := registry.UpdateState(ctx, desc2.SnapshotID, StateActive); err != nil { + t.Fatal(err) + } _, _ = registry.Create(ctx, "creating", 300, InvalidSnapshotID, 3000, 75, 1536, FlagReadOnly) // Leave in creating state desc4, _ := registry.Create(ctx, "deleting", 400, InvalidSnapshotID, 4000, 25, 512, FlagReadOnly) - registry.UpdateState(ctx, desc4.SnapshotID, StateActive) - registry.Delete(ctx, desc4.SnapshotID) + if err := registry.UpdateState(ctx, desc4.SnapshotID, StateActive); err != nil { + t.Fatal(err) + } + if err := registry.Delete(ctx, desc4.SnapshotID); err != nil { + t.Fatal(err) + } // Scan stats, err := registry.Scan(ctx) @@ -676,7 +714,7 @@ func TestRegistryPersistence(t *testing.T) { } testFile := filepath.Join(tmpDir, "test_snapshot_persist.bin") - defer os.Remove(testFile) + defer func() { _ = os.Remove(testFile) }() var snapshotID uint64 @@ -687,16 +725,18 @@ func TestRegistryPersistence(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() }() registry := NewRegistry(dev, 0, 10*SectorSize, nil) ctx := context.Background() @@ -708,7 +748,9 @@ func TestRegistryPersistence(t *testing.T) { } snapshotID = desc.SnapshotID - registry.UpdateState(ctx, snapshotID, StateActive) + if err := registry.UpdateState(ctx, snapshotID, StateActive); err != nil { + t.Fatal(err) + } }() // Reopen and verify @@ -716,13 +758,15 @@ func TestRegistryPersistence(t *testing.T) { if err != nil { t.Skipf("Cannot reopen: %v", err) } - defer dev.Close() + defer func() { _ = dev.Close() }() registry := NewRegistry(dev, 0, 10*SectorSize, nil) ctx := context.Background() // Scan to rebuild state - registry.Scan(ctx) + if _, err := registry.Scan(ctx); err != nil { + t.Fatal(err) + } // Retrieve desc, err := registry.Get(ctx, snapshotID) @@ -864,8 +908,10 @@ func BenchmarkCreate(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - registry.Create(ctx, fmt.Sprintf("snap%d", i), uint32(100+i), - InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly) + if _, err := registry.Create(ctx, fmt.Sprintf("snap%d", i), uint32(100+i), + InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil { + b.Fatal(err) + } } } @@ -886,7 +932,9 @@ func BenchmarkGet(b *testing.B) { b.ResetTimer() for i := 0; i < b.N; i++ { - registry.Get(ctx, ids[i%100]) + if _, err := registry.Get(ctx, ids[i%100]); err != nil { + b.Fatal(err) + } } } @@ -899,13 +947,17 @@ func BenchmarkList(b *testing.B) { // Pre-create snapshots for i := 0; i < 100; i++ { - registry.Create(ctx, fmt.Sprintf("snap%d", i), uint32(100+i), - InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly) + if _, err := registry.Create(ctx, fmt.Sprintf("snap%d", i), uint32(100+i), + InvalidSnapshotID, 1000, 50, 1024, FlagReadOnly); err != nil { + b.Fatal(err) + } } b.ResetTimer() for i := 0; i < b.N; i++ { - registry.List(ctx) + if _, err := registry.List(ctx); err != nil { + b.Fatal(err) + } } } @@ -1051,7 +1103,9 @@ func TestRegistryIDsAfterOlderImagesAndSlotReuse(t *testing.T) { continue } var n int - fmt.Sscanf(d.GetName(), "old-%d", &n) + if _, err := fmt.Sscanf(d.GetName(), "old-%d", &n); err != nil { + t.Fatal(err) + } d.SnapshotID = uint64(n + 1) if err := old.writeDescriptor(slot, d); err != nil { t.Fatal(err) diff --git a/internal/snapshot/worker_test.go b/internal/snapshot/worker_test.go index f4681725..93f5e2a5 100644 --- a/internal/snapshot/worker_test.go +++ b/internal/snapshot/worker_test.go @@ -39,26 +39,28 @@ func setupTestDevice(t *testing.T) (*storage.Device, string, func()) { devPath := filepath.Join(tmpDir, "test.img") f, err := os.Create(devPath) if err != nil { - os.RemoveAll(tmpDir) + _ = os.RemoveAll(tmpDir) t.Fatalf("Failed to create device file: %v", err) } if err := f.Truncate(testDeviceSize); err != nil { - f.Close() - os.RemoveAll(tmpDir) + _ = f.Close() + _ = os.RemoveAll(tmpDir) t.Fatalf("Failed to truncate device: %v", err) } - f.Close() + if err := f.Close(); err != nil { + t.Fatal(err) + } dev, err := storage.Open(devPath) if err != nil { - os.RemoveAll(tmpDir) + _ = os.RemoveAll(tmpDir) t.Fatalf("Failed to open device: %v", err) } cleanup := func() { - dev.Close() - os.RemoveAll(tmpDir) + _ = dev.Close() + _ = os.RemoveAll(tmpDir) } return dev, devPath, cleanup