2021-08-16 17:14:14 +02:00
|
|
|
// inomap translates (Dev, Tag, Ino) tuples to unique uint64
|
2020-04-19 21:57:53 +02:00
|
|
|
// 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 ]
|
|
|
|
//
|
2021-08-16 17:14:14 +02:00
|
|
|
// Each (Dev, Tag) tuple gets a namespace id assigned. The original inode
|
2020-04-19 21:57:53 +02:00
|
|
|
// number is then passed through in the lower 48 bits.
|
|
|
|
//
|
|
|
|
// If namespace ids are exhaused, or the original id is larger than 48 bits,
|
2021-08-16 17:14:14 +02:00
|
|
|
// the whole (Dev, Tag, Ino) tuple gets mapped in the spill map, and the
|
2020-04-19 21:57:53 +02:00
|
|
|
// 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"
|
2021-08-16 17:13:14 +02:00
|
|
|
|
2021-08-23 15:05:15 +02:00
|
|
|
"github.com/rfjakob/gocryptfs/v2/internal/tlog"
|
2019-11-16 21:35:26 +01:00
|
|
|
)
|
|
|
|
|
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-06-11 23:38:29 +02:00
|
|
|
// namespaceMap 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.
|
2021-09-10 17:17:16 +02:00
|
|
|
// Inode numbers on device `rootDev` will be passed through as-is.
|
|
|
|
// If `rootDev` is zero, the first Translate() call decides the effective
|
|
|
|
// rootDev.
|
|
|
|
func New(rootDev uint64) *InoMap {
|
|
|
|
m := &InoMap{
|
2020-04-19 21:57:53 +02:00
|
|
|
namespaceMap: make(map[namespaceData]uint16),
|
|
|
|
namespaceNext: 0,
|
|
|
|
spillMap: make(map[QIno]uint64),
|
|
|
|
spillNext: 0,
|
|
|
|
}
|
2021-09-10 17:17:16 +02:00
|
|
|
if rootDev > 0 {
|
|
|
|
// Reserve namespace 0 for rootDev
|
|
|
|
m.namespaceMap[namespaceData{rootDev, 0}] = 0
|
|
|
|
m.namespaceNext = 1
|
|
|
|
}
|
|
|
|
return m
|
2020-04-19 21:57:53 +02:00
|
|
|
}
|
|
|
|
|
2021-08-16 17:13:14 +02:00
|
|
|
var spillWarn sync.Once
|
|
|
|
|
2020-04-19 21:57:53 +02:00
|
|
|
func (m *InoMap) spill(in QIno) (out uint64) {
|
2021-08-16 17:13:14 +02:00
|
|
|
spillWarn.Do(func() { tlog.Warn.Printf("InoMap: opening spillMap for %v", in) })
|
|
|
|
|
2020-04-19 21:57:53 +02:00
|
|
|
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-06-11 23:38:29 +02:00
|
|
|
// TranslateStat translates (device, ino) pair contained in "st" into a unique
|
|
|
|
// inode number and overwrites the ino in "st" with it.
|
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)
|
|
|
|
}
|