Commit d415f3e7 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

Use memory addresses as node ids.

Saves a mutex and a map lookup.
parent 990f3c11
...@@ -7,6 +7,7 @@ import ( ...@@ -7,6 +7,7 @@ import (
"path/filepath" "path/filepath"
"strings" "strings"
"sync" "sync"
"unsafe"
) )
type mountData struct { type mountData struct {
...@@ -158,7 +159,7 @@ type FileSystemConnector struct { ...@@ -158,7 +159,7 @@ type FileSystemConnector struct {
// Invariants: see the verify() method. // Invariants: see the verify() method.
inodeMap map[uint64]*inode inodeMap map[uint64]*inode
nextFreeInode uint64 rootNode *inode
// Open files/directories. // Open files/directories.
fileLock sync.RWMutex fileLock sync.RWMutex
...@@ -173,7 +174,7 @@ func (me *FileSystemConnector) DebugString() string { ...@@ -173,7 +174,7 @@ func (me *FileSystemConnector) DebugString() string {
me.fileLock.RLock() me.fileLock.RLock()
defer me.fileLock.RUnlock() defer me.fileLock.RUnlock()
root := me.inodeMap[FUSE_ROOT_ID] root := me.rootNode
return fmt.Sprintf("Mounts %20d\nFiles %20d\nInodes %20d\n", return fmt.Sprintf("Mounts %20d\nFiles %20d\nInodes %20d\n",
root.totalMountCount(), root.totalMountCount(),
len(me.openFiles), len(me.inodeMap)) len(me.openFiles), len(me.inodeMap))
...@@ -233,12 +234,12 @@ func (me *FileSystemConnector) verify() { ...@@ -233,12 +234,12 @@ func (me *FileSystemConnector) verify() {
if v.NodeId != k { if v.NodeId != k {
panic(fmt.Sprintf("nodeid mismatch %v %v", v, k)) panic(fmt.Sprintf("nodeid mismatch %v %v", v, k))
} }
if v.Parent == nil && v.NodeId != FUSE_ROOT_ID { if v.Parent == nil && v != me.rootNode {
hiddenOpen += v.OpenCount hiddenOpen += v.OpenCount
} }
} }
root := me.inodeMap[FUSE_ROOT_ID] root := me.rootNode
root.verify() root.verify()
open := root.totalOpenCount() open := root.totalOpenCount()
...@@ -249,11 +250,14 @@ func (me *FileSystemConnector) verify() { ...@@ -249,11 +250,14 @@ func (me *FileSystemConnector) verify() {
} }
} }
func (me *FileSystemConnector) newInode() *inode { func (me *FileSystemConnector) newInode(root bool) *inode {
data := new(inode) data := new(inode)
data.NodeId = me.nextFreeInode if root {
me.nextFreeInode++ data.NodeId = FUSE_ROOT_ID
me.rootNode = data
} else {
data.NodeId = uint64(uintptr(unsafe.Pointer(data)))
}
me.inodeMap[data.NodeId] = data me.inodeMap[data.NodeId] = data
return data return data
...@@ -267,7 +271,7 @@ func (me *FileSystemConnector) lookupUpdate(parent *inode, name string, isDir bo ...@@ -267,7 +271,7 @@ func (me *FileSystemConnector) lookupUpdate(parent *inode, name string, isDir bo
data, ok := parent.Children[name] data, ok := parent.Children[name]
if !ok { if !ok {
data = me.newInode() data = me.newInode(false)
data.Name = name data.Name = name
data.setParent(parent) data.setParent(parent)
if isDir { if isDir {
...@@ -279,14 +283,11 @@ func (me *FileSystemConnector) lookupUpdate(parent *inode, name string, isDir bo ...@@ -279,14 +283,11 @@ func (me *FileSystemConnector) lookupUpdate(parent *inode, name string, isDir bo
} }
func (me *FileSystemConnector) getInodeData(nodeid uint64) *inode { func (me *FileSystemConnector) getInodeData(nodeid uint64) *inode {
me.lock.RLock() if nodeid == FUSE_ROOT_ID {
defer me.lock.RUnlock() return me.rootNode
val := me.inodeMap[nodeid]
if val == nil {
panic(fmt.Sprintf("inode %v unknown", nodeid))
} }
return val
return (*inode)(unsafe.Pointer(uintptr(nodeid)))
} }
func (me *FileSystemConnector) forgetUpdate(nodeId uint64, forgetCount int) { func (me *FileSystemConnector) forgetUpdate(nodeId uint64, forgetCount int) {
...@@ -351,7 +352,7 @@ func (me *FileSystemConnector) findInode(fullPath string) *inode { ...@@ -351,7 +352,7 @@ func (me *FileSystemConnector) findInode(fullPath string) *inode {
me.lock.RLock() me.lock.RLock()
defer me.lock.RUnlock() defer me.lock.RUnlock()
node := me.inodeMap[FUSE_ROOT_ID] node := me.rootNode
for i, component := range comps { for i, component := range comps {
if len(component) == 0 { if len(component) == 0 {
continue continue
...@@ -374,9 +375,7 @@ func EmptyFileSystemConnector() (out *FileSystemConnector) { ...@@ -374,9 +375,7 @@ func EmptyFileSystemConnector() (out *FileSystemConnector) {
out.inodeMap = make(map[uint64]*inode) out.inodeMap = make(map[uint64]*inode)
out.openFiles = make(map[uint64]interface{}) out.openFiles = make(map[uint64]interface{})
out.nextFreeInode = FUSE_ROOT_ID rootData := out.newInode(true)
rootData := out.newInode()
rootData.NodeId = FUSE_ROOT_ID
rootData.Type = ModeToType(S_IFDIR) rootData.Type = ModeToType(S_IFDIR)
rootData.Children = make(map[string]*inode, initDirSize) rootData.Children = make(map[string]*inode, initDirSize)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment