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
6 changes: 5 additions & 1 deletion cmd/mount/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -274,8 +274,12 @@ func main() {
}

// Refcounts stay on without snapshots: blocks shared earlier still need copy-on-write.
refcountReg := refcount.NewRegion(dev, refcountOffset, totalBlocks, lockMgr)
refcountReg := refcount.NewRegion(dev, refcountOffset, sb.RefCountTableSize, totalBlocks, lockMgr)
log.Printf("Refcount: region at %dMB (%d blocks)", refcountOffset/(1024*1024), totalBlocks)
if covered := refcount.BlocksForSize(sb.RefCountTableSize); totalBlocks > covered {
log.Printf("[WARN] Refcount: region covers blocks 0-%d of %d; snapshots and clones of files with data in higher blocks fail",
covered-1, totalBlocks)
}

// Create filesystem with journal, TRIM, SFB, and refcount for crash consistency
gfs := goclusterfs.NewWithComponents(dev, bm, inodeTbl, extentMgr, dirMgr, cacheMgr, j, trimMgr, sfbMgr, refcountReg, lockMgr, nodeUUID, dataOffset)
Expand Down
8 changes: 4 additions & 4 deletions internal/extent/extent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1186,7 +1186,7 @@ func createTestSetupWithRefcount(tb testing.TB) (*Manager, *refcount.Region, *in
refcountSize := uint64(1 << 20) // 1MB for refcounts

bm := bitmap.NewRegion(dev, bitmapOffset, 1000, nil, uuid) // 1000 blocks
refcountReg := refcount.NewRegion(dev, refcountOffset, refcountSize, nil)
refcountReg := refcount.NewRegion(dev, refcountOffset, refcountSize, 1000, nil)
mgr := NewManager(dev, bm, nil, 2<<20)

ino := &inode.Inode{
Expand Down Expand Up @@ -1511,7 +1511,7 @@ func listExtents(tb testing.TB, mgr *Manager, ino *inode.Inode) []*Extent {

func TestFreeKeepsSharedBlocks(t *testing.T) {
env := newTreeEnv(t, 0)
refs := refcount.NewRegion(env.dev, treeTestInodeOffset+512<<10, treeTestBlocks, nil)
refs := refcount.NewRegion(env.dev, treeTestInodeOffset+512<<10, refcount.SizeForBlocks(treeTestBlocks), treeTestBlocks, nil)
env.mgr.SetRefcounts(refs)
initial := env.bm.FreeCount()

Expand Down Expand Up @@ -1574,7 +1574,7 @@ func legacyShared(t *testing.T, env *treeEnv, refs *refcount.Region, count uint3

func TestUncountedSharingIsNeverFreed(t *testing.T) {
env := newTreeEnv(t, 0)
refs := refcount.NewRegion(env.dev, treeTestInodeOffset+512<<10, treeTestBlocks, nil)
refs := refcount.NewRegion(env.dev, treeTestInodeOffset+512<<10, refcount.SizeForBlocks(treeTestBlocks), treeTestBlocks, nil)
env.mgr.SetRefcounts(refs)
truncate := func(ino *inode.Inode) {
t.Helper()
Expand Down Expand Up @@ -1616,7 +1616,7 @@ func TestUncountedSharingIsNeverFreed(t *testing.T) {
// inode write may land, so the old block is released once.
func TestConcurrentInlineCopyOnWrite(t *testing.T) {
env := newTreeEnv(t, 0)
refs := refcount.NewRegion(env.dev, treeTestInodeOffset+512<<10, treeTestBlocks, nil)
refs := refcount.NewRegion(env.dev, treeTestInodeOffset+512<<10, refcount.SizeForBlocks(treeTestBlocks), treeTestBlocks, nil)
env.mgr.SetRefcounts(refs)

src := env.newInode()
Expand Down
2 changes: 1 addition & 1 deletion internal/fsck/check_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func fuzzImage(tb testing.TB) (*storage.Device, []fuzzTarget) {
tbl := inode.NewTable(dev, fuzzInodes, 4096*inode.InodeSize, nil, uuid)
mgr := extent.NewManager(dev, bm, nil, fuzzDataOffset)
tbl.SetTreeFlusher(mgr)
refs := refcount.NewRegion(dev, fuzzRefcounts, fuzzBlocks, nil)
refs := refcount.NewRegion(dev, fuzzRefcounts, refcount.SizeForBlocks(fuzzBlocks), fuzzBlocks, nil)
mgr.SetRefcounts(refs)
dirs := dir.NewManager(dev, tbl, mgr, nil, fuzzDataOffset)

Expand Down
10 changes: 9 additions & 1 deletion internal/goclusterfs/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -3305,6 +3305,14 @@ func (f *FS) shareInto(ctx context.Context, srcNum uint32, dst *inode.Inode, sti
if uncounted {
return errUncountedSharing
}
// Refuse before marking src shared, so its writes stay off the copy-on-write path.
covered, err := f.extentMgr.DataBlocks(src)
if err != nil {
return err
}
if err := f.refcountReg.CheckCovered(covered); err != nil {
return err
}
// A snapshot's data inode is marked shared when it is created and never written again.
if src.Flags&inode.FlagSnapshotData == 0 {
if src, err = f.updateInode(ctx, srcNum, f.markShared); err != nil {
Expand Down Expand Up @@ -3344,7 +3352,7 @@ func (f *FS) shareInto(ctx context.Context, srcNum uint32, dst *inode.Inode, sti

// sharingErrno maps a failure to share a file's blocks to an errno.
func sharingErrno(err error) syscall.Errno {
if errors.Is(err, errUncountedSharing) {
if errors.Is(err, errUncountedSharing) || errors.Is(err, refcount.ErrBlockPastRegion) {
return syscall.ENOTSUP
}
return syscall.EIO
Expand Down
2 changes: 1 addition & 1 deletion internal/goclusterfs/fsck_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func openFormattedFS(t *testing.T, path string) *FS {
inodeTbl := inode.NewTable(dev, sb.InodeTableOffset, sb.InodeCount*inode.InodeSize, locks, nodeUUID)
extentMgr := extent.NewManager(dev, bm, locks, sb.DataRegionOffset)
inodeTbl.SetTreeFlusher(extentMgr)
refs := refcount.NewRegion(dev, sb.RefCountTableOffset, sb.TotalBlocks, locks)
refs := refcount.NewRegion(dev, sb.RefCountTableOffset, sb.RefCountTableSize, sb.TotalBlocks, locks)
dirMgr := dir.NewManager(dev, inodeTbl, extentMgr, locks, sb.DataRegionOffset)
fsys := NewWithComponents(dev, bm, inodeTbl, extentMgr, dirMgr, nil, nil, nil, nil, refs, locks, nodeUUID, sb.DataRegionOffset)
if err := fsys.InitSnapshots(ctx, sb.SnapshotRegOffset, sb.SnapshotRegSize, locks, slot); err != nil {
Expand Down
39 changes: 38 additions & 1 deletion internal/goclusterfs/snapshot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func openSnapshotFS(t *testing.T, path string) *FS {
inodeTbl := inode.NewTable(dev, snapFSInodeOffset, 4096*inode.InodeSize, locks, nodeUUID)
extentMgr := extent.NewManager(dev, bm, locks, snapFSDataOffset)
inodeTbl.SetTreeFlusher(extentMgr)
refs := refcount.NewRegion(dev, snapFSRefcountOffset, snapFSDataBlocks, locks)
refs := refcount.NewRegion(dev, snapFSRefcountOffset, refcount.SizeForBlocks(snapFSDataBlocks), snapFSDataBlocks, locks)
dirMgr := dir.NewManager(dev, inodeTbl, extentMgr, locks, snapFSDataOffset)
fsys := NewWithComponents(dev, bm, inodeTbl, extentMgr, dirMgr, nil, nil, nil, nil, refs, locks, nodeUUID, snapFSDataOffset)
if err := fsys.InitSnapshots(context.Background(), snapFSRegistryOffset, snapFSRegistrySize, locks, slot); err != nil {
Expand Down Expand Up @@ -298,6 +298,43 @@ func TestSnapshotRefusesUncountedSharing(t *testing.T) {
checkFile(t, f, pattern(1, BlockSize))
}

// On a volume larger than its refcount region, blocks past the region cannot
// be shared. Refusing must leave the file as it was, off the copy-on-write path.
func TestSnapshotRefusesBlocksPastRefcountRegion(t *testing.T) {
fsys := openSnapshotFS(t, newSnapshotFSImage(t))
ctx := context.Background()
if err := fsys.InitRoot(ctx); err != nil {
t.Fatal(err)
}
noEntries := refcount.NewRegion(fsys.dev, snapFSRefcountOffset, 0, snapFSDataBlocks, fsys.lockMgr)
fsys.refcountReg = noEntries
fsys.extentMgr.SetRefcounts(noEntries)

f := newRegularFile(t, fsys)
writeAt(t, f, pattern(1, 2*BlockSize), 0)

if errno := f.Setxattr(ctx, "user.snapshot", []byte("high"), 0); errno != syscall.ENOTSUP {
t.Fatalf("snapshot of blocks past the refcount region: errno %v, want ENOTSUP", errno)
}
if errno := f.Setxattr(ctx, "user.clone", []byte("/high-clone"), 0); errno != syscall.ENOTSUP {
t.Fatalf("clone of blocks past the refcount region: errno %v, want ENOTSUP", errno)
}
ino, err := fsys.inodeTbl.Read(f.inodeNum)
if err != nil {
t.Fatal(err)
}
if ino.InlineExtents[0].Flags&extent.FlagShared != 0 || ino.Flags&inode.FlagHasSnapshot != 0 {
t.Fatalf("refused share marked the file shared: extent flags %#x, inode flags %#x", ino.InlineExtents[0].Flags, ino.Flags)
}

free := fsys.bitmapMgr.FreeCount()
writeAt(t, f, pattern(2, 4096), 10)
if got := fsys.bitmapMgr.FreeCount(); got != free {
t.Fatalf("write after a refused snapshot allocated %d blocks, want 0", free-got)
}
checkFile(t, f, overwritten(pattern(1, 2*BlockSize), 10, pattern(2, 4096)))
}

// A new mount cleans up a snapshot a crash left half created or half deleted,
// and the file keeps its blocks.
func TestSnapshotRecoveryAfterCrash(t *testing.T) {
Expand Down
61 changes: 57 additions & 4 deletions internal/refcount/refcount.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ var (
ErrRefCountUnderflow = errors.New("reference count underflow")
ErrInvalidChecksum = errors.New("refcount sector checksum mismatch")
ErrAlreadyZero = errors.New("reference count already zero")
ErrBlockPastRegion = errors.New("block has no refcount entry: the refcount region is too small for this volume")
)

// Entry represents a single reference count entry.
Expand Down Expand Up @@ -107,6 +108,7 @@ type Region struct {
mu sync.RWMutex
dev *storage.Device
offset uint64 // RefCount region start offset on device
capacity uint64 // Blocks the region has entries for
totalBlocks uint64 // Total number of data blocks tracked
lockMgr *lock.Manager // For distributed locking (optional)
}
Expand All @@ -116,12 +118,17 @@ type Region struct {
// Parameters:
// - dev: The storage device
// - offset: Byte offset where refcount region starts on device
// - size: Size of the refcount region in bytes; nothing past it is touched
// - totalBlocks: Total number of data blocks to track
// - lockMgr: Lock manager for distributed locking (can be nil for single-node)
func NewRegion(dev *storage.Device, offset, totalBlocks uint64, lockMgr *lock.Manager) *Region {
//
// Blocks past the region's capacity read as having a single owner, and any
// change to them fails with ErrBlockPastRegion.
func NewRegion(dev *storage.Device, offset, size, totalBlocks uint64, lockMgr *lock.Manager) *Region {
return &Region{
dev: dev,
offset: offset,
capacity: BlocksForSize(size),
totalBlocks: totalBlocks,
lockMgr: lockMgr,
}
Expand Down Expand Up @@ -376,6 +383,17 @@ func (r *Region) updateBatch(ctx context.Context, blockNums []uint64, fn func(i
sectors := make(map[uint64][]byte)
dirty := make(map[uint64]bool)
for i, blockNum := range blockNums {
if blockNum >= r.capacity {
entry := Entry{}
changed, err := fn(i, &entry)
if err != nil {
return err
}
if changed && entry != (Entry{}) {
return r.pastRegion(blockNum)
}
continue
}
sectorIdx := blockNum / EntriesPerSector
if _, ok := sectors[sectorIdx]; !ok {
data, err := r.readSector(sectorIdx)
Expand Down Expand Up @@ -409,7 +427,8 @@ func (r *Region) Scan(ctx context.Context) (stats ScanStats, err error) {
r.mu.RLock()
defer r.mu.RUnlock()

numSectors := (r.totalBlocks + EntriesPerSector - 1) / EntriesPerSector
covered := r.coveredBlocks()
numSectors := (covered + EntriesPerSector - 1) / EntriesPerSector

for sectorIdx := uint64(0); sectorIdx < numSectors; sectorIdx++ {
sectorData, err := r.readSector(sectorIdx)
Expand All @@ -420,7 +439,7 @@ func (r *Region) Scan(ctx context.Context) (stats ScanStats, err error) {
baseBlock := sectorIdx * EntriesPerSector
for entryIdx := uint64(0); entryIdx < EntriesPerSector; entryIdx++ {
blockNum := baseBlock + entryIdx
if blockNum >= r.totalBlocks {
if blockNum >= covered {
break
}

Expand All @@ -441,6 +460,7 @@ func (r *Region) Scan(ctx context.Context) (stats ScanStats, err error) {
}
}

stats.FreeBlocks += r.totalBlocks - covered
stats.TotalBlocks = r.totalBlocks
return stats, nil
}
Expand All @@ -460,8 +480,30 @@ func (r *Region) TotalBlocks() uint64 {
return r.totalBlocks
}

// CheckCovered returns an ErrBlockPastRegion error for the first block the
// region has no entry for.
func (r *Region) CheckCovered(blockNums []uint64) error {
for _, blockNum := range blockNums {
if blockNum >= r.capacity {
return r.pastRegion(blockNum)
}
}
return nil
}

func (r *Region) coveredBlocks() uint64 {
return min(r.totalBlocks, r.capacity)
}

func (r *Region) pastRegion(blockNum uint64) error {
return fmt.Errorf("%w: block %d, region covers blocks 0-%d", ErrBlockPastRegion, blockNum, r.capacity-1)
}

// readEntry reads a single entry from disk.
func (r *Region) readEntry(blockNum uint64) (Entry, error) {
if blockNum >= r.capacity {
return Entry{}, nil
}
sectorIdx := blockNum / EntriesPerSector
entryIdx := blockNum % EntriesPerSector

Expand All @@ -475,6 +517,12 @@ func (r *Region) readEntry(blockNum uint64) (Entry, error) {

// writeEntry writes a single entry to disk.
func (r *Region) writeEntry(blockNum uint64, entry Entry) error {
if blockNum >= r.capacity {
if entry == (Entry{}) {
return nil
}
return r.pastRegion(blockNum)
}
sectorIdx := blockNum / EntriesPerSector
entryIdx := blockNum % EntriesPerSector

Expand Down Expand Up @@ -581,13 +629,18 @@ func SizeForBlocks(totalBlocks uint64) uint64 {
return numSectors * SectorSize
}

// BlocksForSize returns how many blocks a refcount region of size bytes has entries for.
func BlocksForSize(size uint64) uint64 {
return size / SectorSize * EntriesPerSector
}

// Initialize initializes the refcount region with all counts set to 0.
// This should be called during filesystem format.
func (r *Region) Initialize(ctx context.Context) error {
r.mu.Lock()
defer r.mu.Unlock()

numSectors := (r.totalBlocks + EntriesPerSector - 1) / EntriesPerSector
numSectors := (r.coveredBlocks() + EntriesPerSector - 1) / EntriesPerSector

// Create an empty sector (all zeros)
emptyData := make([]byte, BytesPerSector)
Expand Down
Loading
Loading