Skip to content

Commit

Permalink
inomap: export NextSpillIno()
Browse files Browse the repository at this point in the history
This will be used in reverse mode. Switch to atomic increment to avoid
a "nextSpillInoUnlocked" helper.
  • Loading branch information
rfjakob committed May 5, 2024
1 parent c85c092 commit 5a2d461
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 6 deletions.
22 changes: 16 additions & 6 deletions internal/inomap/inomap.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"log"
"math"
"sync"
"sync/atomic"
"syscall"

"github.com/rfjakob/gocryptfs/v2/internal/tlog"
Expand Down Expand Up @@ -68,19 +69,28 @@ func New(rootDev uint64) *InoMap {

var spillWarn sync.Once

// NextSpillIno returns a fresh inode number from the spill pool without adding it to
// spillMap.
// Reverse mode NextSpillIno() for gocryptfs.longname.*.name files where a stable
// mapping is not needed.
func (m *InoMap) NextSpillIno() (out uint64) {
if m.spillNext == math.MaxUint64 {
log.Panicf("spillMap overflow: spillNext = 0x%x", m.spillNext)
}
return atomic.AddUint64(&m.spillNext, 1) - 1
}

func (m *InoMap) spill(in QIno) (out uint64) {
spillWarn.Do(func() { tlog.Warn.Printf("InoMap: opening spillMap for %v", in) })
spillWarn.Do(func() { tlog.Warn.Printf("InoMap: opening spillMap for %#v", in) })

out, found := m.spillMap[in]
if found {
return out
}
if m.spillNext == math.MaxUint64 {
log.Panicf("spillMap overflow: spillNext = 0x%x", m.spillNext)
}
out = m.spillNext
m.spillNext++

out = m.NextSpillIno()
m.spillMap[in] = out

return out
}

Expand Down
8 changes: 8 additions & 0 deletions internal/inomap/inomap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,21 @@ func TestSpill(t *testing.T) {
if out1&spillBit == 0 {
t.Error("spill bit not set")
}
if out1 != spillSpaceStart {
t.Errorf("unexpected first spill inode number %d", out1)
}
out2 := m.Translate(q)
if out2&spillBit == 0 {
t.Error("spill bit not set")
}
if out1 != out2 {
t.Errorf("unstable mapping: %d vs %d", out1, out2)
}
q.Ino = maxPassthruIno + 2
out3 := m.Translate(q)
if out3 != out1+1 {
t.Errorf("unexpected 2nd spill inode number %d", out1)
}
}

// TestUniqueness checks that unique (Dev, Flags, Ino) tuples get unique inode
Expand Down

0 comments on commit 5a2d461

Please sign in to comment.