2020-04-19 21:57:53 +02:00
|
|
|
// inomap translates (Dev, Flags, Ino) tuples to unique uint64
|
|
|
|
// inode numbers.
|
|
|
|
//
|
|
|
|
// 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 ]
|
|
|
|
//
|
|
|
|
// Each (Dev, Flags) tuple gets a namespace id assigned. The original inode
|
|
|
|
// number is then passed through in the lower 48 bits.
|
|
|
|
//
|
|
|
|
// If namespace ids are exhaused, or the original id is larger than 48 bits,
|
|
|
|
// the whole (Dev, Flags, Ino) tuple gets mapped in the spill map, and the
|
|
|
|
// spill bit is set to 1.
|
2020-04-12 17:15:03 +02:00
|
|
|
package inomap
|
2019-11-16 21:35:26 +01:00
|
|
|
|
|
|
|
import (
|
2020-04-19 21:57:53 +02:00
|
|
|
"log"
|
2019-11-16 21:35:26 +01:00
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2020-04-19 21:57:53 +02:00
|
|
|
const (
|
2020-04-19 22:09:21 +02:00
|
|
|
// max value of 15 bit namespace id
|
2020-04-19 21:57:53 +02:00
|
|
|
maxNamespaceId = 1<<15 - 1
|
2020-04-19 22:09:21 +02:00
|
|
|
// max value of 48 bit passthru inode number
|
2020-04-19 21:57:53 +02:00
|
|
|
maxPassthruIno = 1<<48 - 1
|
2020-04-19 22:09:21 +02:00
|
|
|
// max value of 63 bit spill inode number
|
|
|
|
maxSpillIno = 1<<63 - 1
|
2020-05-03 20:21:11 +02:00
|
|
|
// bit 63 is used as the spill bit
|
|
|
|
spillBit = 1 << 63
|
2020-04-19 21:57:53 +02:00
|
|
|
)
|
2019-11-16 21:35:26 +01:00
|
|
|
|
2020-04-19 21:57:53 +02:00
|
|
|
// InoMap stores the maps using for inode number translation.
|
|
|
|
// See package comment for details.
|
2020-04-12 17:15:03 +02:00
|
|
|
type InoMap struct {
|
2019-11-16 21:35:26 +01:00
|
|
|
sync.Mutex
|
2020-04-19 21:57:53 +02:00
|
|
|
// namespaces keeps the mapping of (Dev,Flags) tuples to
|
2020-04-19 22:09:21 +02:00
|
|
|
// 15-bit identifiers (stored in an uint16 with the high bit always zero)
|
2020-04-19 21:57:53 +02:00
|
|
|
namespaceMap map[namespaceData]uint16
|
|
|
|
// spillNext is the next free namespace number in the namespaces map
|
|
|
|
namespaceNext uint16
|
|
|
|
// spill is used once the namespaces map is full
|
|
|
|
spillMap map[QIno]uint64
|
|
|
|
// spillNext is the next free inode number in the spill map
|
|
|
|
spillNext uint64
|
2019-11-16 21:35:26 +01:00
|
|
|
}
|
|
|
|
|
2020-04-12 17:15:03 +02:00
|
|
|
// New returns a new InoMap.
|
2020-04-19 21:57:53 +02:00
|
|
|
func New() *InoMap {
|
2020-04-12 17:15:03 +02:00
|
|
|
return &InoMap{
|
2020-04-19 21:57:53 +02:00
|
|
|
namespaceMap: make(map[namespaceData]uint16),
|
|
|
|
namespaceNext: 0,
|
|
|
|
spillMap: make(map[QIno]uint64),
|
|
|
|
spillNext: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *InoMap) spill(in QIno) (out uint64) {
|
|
|
|
out, found := m.spillMap[in]
|
|
|
|
if found {
|
2020-05-03 20:21:11 +02:00
|
|
|
return out | spillBit
|
2020-04-19 21:57:53 +02:00
|
|
|
}
|
|
|
|
if m.spillNext >= maxSpillIno {
|
|
|
|
log.Panicf("spillMap overflow: spillNext = 0x%x", m.spillNext)
|
2019-11-16 21:35:26 +01:00
|
|
|
}
|
2020-04-19 21:57:53 +02:00
|
|
|
out = m.spillNext
|
|
|
|
m.spillNext++
|
|
|
|
m.spillMap[in] = out
|
2020-05-03 20:21:11 +02:00
|
|
|
return out | spillBit
|
2019-11-16 21:35:26 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Translate maps the passed-in (device, inode) pair to a unique inode number.
|
2020-04-12 17:15:03 +02:00
|
|
|
func (m *InoMap) Translate(in QIno) (out uint64) {
|
2019-11-16 21:35:26 +01:00
|
|
|
m.Lock()
|
|
|
|
defer m.Unlock()
|
2020-04-19 21:57:53 +02:00
|
|
|
|
|
|
|
if in.Ino > maxPassthruIno {
|
2020-05-03 15:22:10 +02:00
|
|
|
out = m.spill(in)
|
|
|
|
return out
|
2020-04-19 21:57:53 +02:00
|
|
|
}
|
|
|
|
ns, found := m.namespaceMap[in.namespaceData]
|
|
|
|
// Use existing namespace
|
|
|
|
if found {
|
2020-05-03 15:22:10 +02:00
|
|
|
out = uint64(ns)<<48 | in.Ino
|
|
|
|
return out
|
2020-04-19 21:57:53 +02:00
|
|
|
}
|
|
|
|
// No free namespace slots?
|
|
|
|
if m.namespaceNext >= maxNamespaceId {
|
2020-05-03 15:22:10 +02:00
|
|
|
out = m.spill(in)
|
|
|
|
return out
|
2019-11-16 21:35:26 +01:00
|
|
|
}
|
2020-04-19 21:57:53 +02:00
|
|
|
ns = m.namespaceNext
|
|
|
|
m.namespaceNext++
|
|
|
|
m.namespaceMap[in.namespaceData] = ns
|
2020-05-03 15:22:10 +02:00
|
|
|
out = uint64(ns)<<48 | in.Ino
|
|
|
|
return out
|
2019-11-16 21:35:26 +01:00
|
|
|
}
|
|
|
|
|
2020-05-10 00:25:49 +02:00
|
|
|
// TranslateStat translates the inode number contained in "st" if necessary.
|
2019-11-16 21:35:26 +01:00
|
|
|
// Convience wrapper around Translate().
|
2020-04-12 17:15:03 +02:00
|
|
|
func (m *InoMap) TranslateStat(st *syscall.Stat_t) {
|
2019-11-16 21:35:26 +01:00
|
|
|
in := QInoFromStat(st)
|
|
|
|
st.Ino = m.Translate(in)
|
|
|
|
}
|