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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),

### Changed

- Freeing blocks that no snapshot or clone shares takes only the block bitmap lock, once per operation, not the refcount lock and one bitmap lock per block. Snapshots and clones take the bitmap lock while they count a file's blocks.
- Each node keeps the directories it reads in an in-memory index, so a lookup no longer reads and decodes the whole directory. A lookup checks the directory inode's generation on disk before it uses the index, so other nodes' changes show on the next lookup without broadcast. Removing an entry now writes the directory inode before the child is freed. `-dir-index-entries` caps the names held (default 1,048,576, about 100 MB), and `/metrics` counts hits, misses and rebuilds. In one test with two nodes, `ls -l` of 4,000 files went from 65 s to 1 s, a `stat` from 3.8 ms to 0.25 ms, and a create from 14 ms to 6 ms.
- `-format` now writes a superblock with the volume layout and zeroes all metadata regions, even on a device that already holds a filesystem. Mount reads every region offset from the superblock.
- At most 64 nodes mount a volume at once, one per node registry slot.
Expand Down
107 changes: 107 additions & 0 deletions internal/bitmap/bitmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -332,6 +332,113 @@ func (r *Region) releaseReserved(ctx context.Context, blocks []uint64) {
}
}

// HoldAllocated takes the bitmap lock and returns the blocks of blocks that are
// allocated. The lock stays held, so none of them can be freed, until release.
// A block past the end of the volume is an error.
func (r *Region) HoldAllocated(ctx context.Context, blocks []uint64) ([]uint64, func(), error) {
if r.lockMgr != nil {
if err := r.lockMgr.Acquire(ctx, lock.LockTypeBlockBitmap, 0, lock.LockModeExclusive); err != nil {
return nil, nil, fmt.Errorf("failed to acquire bitmap lock: %w", err)
}
}
r.mu.Lock()
release := func() {
r.mu.Unlock()
if r.lockMgr != nil {
r.unlock()
}
}
sectors := make(map[uint64][]byte)
var allocated []uint64
for _, b := range blocks {
if b >= r.totalBlocks {
release()
return nil, nil, fmt.Errorf("block %d: %w", b, ErrBlockOutOfRange)
}
sectorIdx := b / BitsPerSector
data, ok := sectors[sectorIdx]
if !ok {
var err error
if data, err = r.readSector(sectorIdx); err != nil {
release()
return nil, nil, fmt.Errorf("failed to read bitmap sector %d: %w", sectorIdx, err)
}
sectors[sectorIdx] = data
}
bit := b % BitsPerSector
if data[bit/8]&(1<<(bit%8)) != 0 {
allocated = append(allocated, b)
}
}
return allocated, release, nil
}

// FreeIf takes the bitmap lock, asks which of blocks to free, and frees those
// in one hold. It returns the blocks it left allocated. Freed blocks are
// queued for TRIM.
func (r *Region) FreeIf(ctx context.Context, blocks []uint64, which func([]uint64) func(uint64) bool) ([]uint64, error) {
if len(blocks) == 0 {
return nil, nil
}
if r.lockMgr != nil {
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.unlock()
}
r.mu.Lock()
defer r.mu.Unlock()

free := which(blocks)
var kept []uint64
bySector := make(map[uint64][]uint64)
for _, b := range blocks {
switch {
case !free(b):
kept = append(kept, b)
case b >= r.totalBlocks:
logging.Debugf("bitmap.FreeIf: block %d out of range", b)
default:
bySector[b/BitsPerSector] = append(bySector[b/BitsPerSector], b)
}
}
for sectorIdx, sectorBlocks := range bySector {
data, err := r.readSector(sectorIdx)
if err != nil {
return kept, fmt.Errorf("failed to read bitmap sector %d: %w", sectorIdx, err)
}
var freed []uint64
for _, b := range sectorBlocks {
bit := b % BitsPerSector
mask := byte(1) << (bit % 8)
if data[bit/8]&mask == 0 {
log.Printf("[WARN] bitmap: freeing block %d, which is already free", b)
continue
}
data[bit/8] &^= mask
freed = append(freed, b)
}
if len(freed) == 0 {
continue
}
if err := r.writeSector(sectorIdx, data); err != nil {
return kept, fmt.Errorf("failed to write bitmap sector %d: %w", sectorIdx, err)
}
if r.trimMgr != nil {
for _, b := range freed {
r.trimMgr.Queue(b)
}
}
}
return kept, nil
}

// FreeBatch frees blocks in one bitmap lock hold.
func (r *Region) FreeBatch(ctx context.Context, blocks []uint64) error {
_, err := r.FreeIf(ctx, blocks, func([]uint64) func(uint64) bool { return func(uint64) bool { return true } })
return err
}

// flipBits marks each block allocated (or free) whose bit says otherwise, under
// the bitmap lock, and returns the blocks it changed, even on error.
func (r *Region) flipBits(ctx context.Context, blocks []uint64, allocated bool) ([]uint64, error) {
Expand Down
47 changes: 47 additions & 0 deletions internal/bitmap/bitmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"
"os"
"path/filepath"
"slices"
"testing"

"github.com/supporttools/GoClusterFS/internal/storage"
Expand Down Expand Up @@ -682,3 +683,49 @@ func BenchmarkIsFree(b *testing.B) {
}
}
}

// FreeIf frees the blocks the caller picks, keeps the rest, and skips blocks
// already free; HoldAllocated reports only allocated blocks.
func TestFreeIfAndHoldAllocated(t *testing.T) {
dev, cleanup := createTestDevice(t, 10<<20)
defer cleanup()
var uuid [16]byte
region := NewRegion(dev, 0, 1000, nil, uuid)
ctx := context.Background()
blocks, err := region.Alloc(ctx, 4)
if err != nil {
t.Fatal(err)
}
if err := region.Free(ctx, blocks[3]); err != nil {
t.Fatal(err)
}

allocated, release, err := region.HoldAllocated(ctx, blocks)
if err != nil {
t.Fatal(err)
}
release()
if want := blocks[:3]; !slices.Equal(allocated, want) {
t.Errorf("HoldAllocated = %v, want %v", allocated, want)
}

pick := map[uint64]bool{blocks[0]: true, blocks[2]: true, blocks[3]: true}
kept, err := region.FreeIf(ctx, blocks, func([]uint64) func(uint64) bool {
return func(b uint64) bool { return pick[b] }
})
if err != nil {
t.Fatal(err)
}
if want := []uint64{blocks[1]}; !slices.Equal(kept, want) {
t.Errorf("FreeIf kept %v, want %v", kept, want)
}
for i, b := range blocks {
free, err := region.IsFree(b)
if err != nil {
t.Fatal(err)
}
if want := i != 1; free != want {
t.Errorf("block %d free = %v, want %v", b, free, want)
}
}
}
3 changes: 3 additions & 0 deletions internal/extent/extent.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,9 @@ type Manager struct {
// snapshot or clone goes back to the bitmap only after its last owner frees it.
func (m *Manager) SetRefcounts(refs *refcount.Region) {
m.refs = refs
if refs != nil {
refs.SetAllocationGuard(m.bitmap)
}
}

// NewManager creates a new extent manager. dataOffset is the device offset of
Expand Down
4 changes: 2 additions & 2 deletions internal/extent/extent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1566,7 +1566,7 @@ func TestFreeKeepsSharedBlocks(t *testing.T) {
if err != nil {
t.Fatal(err)
}
if err := refs.Share(env.ctx, blocks); err != nil {
if _, err := refs.Share(env.ctx, blocks); err != nil {
t.Fatal(err)
}

Expand Down Expand Up @@ -1663,7 +1663,7 @@ func TestConcurrentInlineCopyOnWrite(t *testing.T) {
t.Fatal(err)
}
env.write(src)
if err := refs.Share(env.ctx, []uint64{old}); err != nil {
if _, err := refs.Share(env.ctx, []uint64{old}); err != nil {
t.Fatal(err)
}
snap := env.newInode()
Expand Down
52 changes: 33 additions & 19 deletions internal/extent/tree_edit.go
Original file line number Diff line number Diff line change
Expand Up @@ -742,27 +742,41 @@ func (m *Manager) freeBlocks(ctx context.Context, blocks []uint64) {
m.deferred = nil
m.deferredMu.Unlock()

if m.refs != nil && len(blocks) > 0 {
free, err := m.refs.Release(ctx, blocks)
switch {
case errors.Is(err, lock.ErrDeadlockRisk):
m.deferFree(blocks...)
return
case err != nil:
log.Printf("[extent] release %d blocks: %v; leaving them allocated", len(blocks), err)
return
}
blocks = free
if len(blocks) == 0 {
return
}
uncounted := func([]uint64) func(uint64) bool { return func(uint64) bool { return true } }
if m.refs != nil {
uncounted = m.refs.Uncounted
}
// Only Share raises a count, and only under the bitmap lock, so a count of 0
// read under that lock stays 0 while the block is freed.
counted, err := m.bitmap.FreeIf(ctx, blocks, uncounted)
switch {
case errors.Is(err, lock.ErrDeadlockRisk):
m.deferFree(blocks...)
return
case err != nil:
log.Printf("[extent] free %d blocks: %v; leaving the rest allocated", len(blocks), err)
return
case len(counted) == 0:
return
}

for _, b := range blocks {
err := m.bitmap.Free(ctx, b)
switch {
case errors.Is(err, lock.ErrDeadlockRisk):
m.deferFree(b)
case err != nil:
log.Printf("[extent] free block %d: %v", b, err)
}
free, err := m.refs.Release(ctx, counted)
switch {
case errors.Is(err, lock.ErrDeadlockRisk):
m.deferFree(counted...)
return
case err != nil:
log.Printf("[extent] release %d blocks: %v; leaving them allocated", len(counted), err)
return
}
switch err := m.bitmap.FreeBatch(ctx, free); {
case errors.Is(err, lock.ErrDeadlockRisk):
m.deferFree(free...)
case err != nil:
log.Printf("[extent] free %d released blocks: %v", len(free), err)
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/fsck/check_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,8 @@ func fuzzImage(tb testing.TB) (*storage.Device, []fuzzTarget) {

blocks, err := mgr.DataBlocks(plain)
must(err)
must(refs.Share(ctx, blocks))
_, err = refs.Share(ctx, blocks)
must(err)
clone := newFile("clone")
must(mgr.CopyExtentsForClone(ctx, plain, clone))
clone.Size = plain.Size
Expand Down
34 changes: 18 additions & 16 deletions internal/goclusterfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3213,31 +3213,33 @@ func (f *FS) shareSource(ctx context.Context, srcNum uint32) (*inode.Inode, []ui
if err := f.refcountReg.CheckCovered(blocks); err != nil {
return nil, nil, err
}
if err := f.refcountReg.Share(ctx, blocks); err != nil {
counted, err := f.refcountReg.Share(ctx, blocks)
if err != nil {
return nil, nil, err
}
marked, err := src, error(nil)
// A snapshot's data inode is marked shared when it is created and never written again.
if src.Flags&inode.FlagSnapshotData != 0 {
return src, blocks, nil
}
if beforeShareMark != nil {
beforeShareMark()
}
// Setxattr holds lockInode(srcNum).
marked, err := f.updateUnderInodeLock(ctx, srcNum, func(ino *inode.Inode) error {
if duringShareMark != nil {
duringShareMark()
if src.Flags&inode.FlagSnapshotData == 0 {
if beforeShareMark != nil {
beforeShareMark()
}
return f.markShared(ino)
})
// Setxattr holds lockInode(srcNum).
marked, err = f.updateUnderInodeLock(ctx, srcNum, func(ino *inode.Inode) error {
if duringShareMark != nil {
duringShareMark()
}
return f.markShared(ino)
})
}
var now []uint64
if err == nil {
now, err = f.extentMgr.DataBlocks(marked)
}
if err == nil && slices.Equal(now, blocks) {
return marked, blocks, nil
// Share skips blocks already freed, so a file that lost blocks is shared again.
if err == nil && slices.Equal(now, blocks) && len(counted) == len(blocks) {
return marked, counted, nil
}
f.extentMgr.ReleaseBlocks(ctx, blocks)
f.extentMgr.ReleaseBlocks(ctx, counted)
if err != nil {
return nil, nil, err
}
Expand Down
14 changes: 9 additions & 5 deletions internal/goclusterfs/fsck_reclaim_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func leakBlock(t *testing.T, fsys *FS) {
}

// deleteWithTimedOutFree removes name as Unlink does, but its block free gives
// up on a refcount lock another node holds, as it does after the 30s lock-wait
// up on the bitmap and refcount locks another node holds, as it does after the 30s lock-wait
// limit. Unlink detaches from its caller's context, so it would wait the full 30s.
func deleteWithTimedOutFree(t *testing.T, fsys *FS, name string) {
t.Helper()
Expand Down Expand Up @@ -63,16 +63,20 @@ func deleteWithTimedOutFree(t *testing.T, fsys *FS, name string) {
copy(otherUUID[:], "othernode1234567")
otherUUID[15] = byte(slot)
other := lock.NewManager(fsys.dev, sb.LockBitmapOffset, sb.LockBitmapSize, otherUUID, slot)
if err := other.Acquire(ctx, lock.LockTypeRefCount, 0, lock.LockModeExclusive); err != nil {
t.Fatal(err)
for _, typ := range []lock.LockType{lock.LockTypeRefCount, lock.LockTypeBlockBitmap} {
if err := other.Acquire(ctx, typ, 0, lock.LockModeExclusive); err != nil {
t.Fatal(err)
}
}
expired, cancel := context.WithCancel(ctx)
cancel()
if err := fsys.extentMgr.Delete(expired, ino); err != nil {
t.Fatalf("delete %s: %v", name, err)
}
if err := other.Release(lock.LockTypeRefCount, 0); err != nil {
t.Fatal(err)
for _, typ := range []lock.LockType{lock.LockTypeBlockBitmap, lock.LockTypeRefCount} {
if err := other.Release(typ, 0); err != nil {
t.Fatal(err)
}
}
if err := fsys.inodeTbl.Free(ctx, ino.InodeNum); err != nil {
t.Fatal(err)
Expand Down
Loading
Loading