From c85c092189df44e9a88ad30c1f91cfc7fbeb4ccb Mon Sep 17 00:00:00 2001
From: Jakob Unterwurzacher
Date: Sun, 5 May 2024 22:05:08 +0200
Subject: inomap: incorporate spillBit into the spillNext start value

This avoids the manual "| spillBit" logic.
---
 internal/inomap/inomap.go      | 21 ++++++++++-----------
 internal/inomap/inomap_test.go |  5 +++++
 2 files changed, 15 insertions(+), 11 deletions(-)

(limited to 'internal')

diff --git a/internal/inomap/inomap.go b/internal/inomap/inomap.go
index 0f7ade3..1c012d9 100644
--- a/internal/inomap/inomap.go
+++ b/internal/inomap/inomap.go
@@ -3,8 +3,8 @@
 //
 // Format of the returned inode numbers:
 //
-//	[spill bit = 0][15 bit namespace id][48 bit passthru inode number]
-//	[spill bit = 1][63 bit spill inode number                        ]
+//	[spill bit = 0][15 bit namespace id][48 bit passthru inode number] = 64 bit translated inode number
+//	[spill bit = 1][63 bit counter                                   ] = 64 bit spill inode number
 //
 // Each (Dev, Tag) tuple gets a namespace id assigned. The original inode
 // number is then passed through in the lower 48 bits.
@@ -16,6 +16,7 @@ package inomap
 
 import (
 	"log"
+	"math"
 	"sync"
 	"syscall"
 
@@ -27,10 +28,8 @@ const (
 	maxNamespaceId = 1<<15 - 1
 	// max value of 48 bit passthru inode number
 	maxPassthruIno = 1<<48 - 1
-	// max value of 63 bit spill inode number
-	maxSpillIno = 1<<63 - 1
-	// bit 63 is used as the spill bit
-	spillBit = 1 << 63
+	// the spill inode number space starts at 0b10000...0.
+	spillSpaceStart = 1 << 63
 )
 
 // InoMap stores the maps using for inode number translation.
@@ -57,7 +56,7 @@ func New(rootDev uint64) *InoMap {
 		namespaceMap:  make(map[namespaceData]uint16),
 		namespaceNext: 0,
 		spillMap:      make(map[QIno]uint64),
-		spillNext:     0,
+		spillNext:     spillSpaceStart,
 	}
 	if rootDev > 0 {
 		// Reserve namespace 0 for rootDev
@@ -74,18 +73,18 @@ func (m *InoMap) spill(in QIno) (out uint64) {
 
 	out, found := m.spillMap[in]
 	if found {
-		return out | spillBit
+		return out
 	}
-	if m.spillNext >= maxSpillIno {
+	if m.spillNext == math.MaxUint64 {
 		log.Panicf("spillMap overflow: spillNext = 0x%x", m.spillNext)
 	}
 	out = m.spillNext
 	m.spillNext++
 	m.spillMap[in] = out
-	return out | spillBit
+	return out
 }
 
-// Translate maps the passed-in (device, inode) pair to a unique inode number.
+// Translate maps the passed-in (device, tag, inode) tuple to a unique inode number.
 func (m *InoMap) Translate(in QIno) (out uint64) {
 	m.Lock()
 	defer m.Unlock()
diff --git a/internal/inomap/inomap_test.go b/internal/inomap/inomap_test.go
index 9ec2932..430ec94 100644
--- a/internal/inomap/inomap_test.go
+++ b/internal/inomap/inomap_test.go
@@ -5,6 +5,11 @@ import (
 	"testing"
 )
 
+const (
+	// bit 63 is used as the spill bit
+	spillBit = 1 << 63
+)
+
 func TestTranslate(t *testing.T) {
 	m := New(0)
 	q := QIno{Ino: 1}
-- 
cgit v1.2.3