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

Create a inode => openedFile mapping.

parent 80362c9c
...@@ -72,16 +72,27 @@ func (me *fileSystemMount) unregisterFileHandle(node *inode, handle uint64) *ope ...@@ -72,16 +72,27 @@ func (me *fileSystemMount) unregisterFileHandle(node *inode, handle uint64) *ope
obj := me.openFiles.Forget(handle) obj := me.openFiles.Forget(handle)
opened := (*openedFile)(unsafe.Pointer(obj)) opened := (*openedFile)(unsafe.Pointer(obj))
node.OpenCountMutex.Lock() node.OpenFilesMutex.Lock()
defer node.OpenCountMutex.Unlock() defer node.OpenFilesMutex.Unlock()
node.OpenCount--
idx := -1
for i, v := range node.OpenFiles {
if v == opened {
idx = i
break
}
}
l := len(node.OpenFiles)
node.OpenFiles[idx] = node.OpenFiles[l-1]
node.OpenFiles = node.OpenFiles[:l-1]
return opened return opened
} }
func (me *fileSystemMount) registerFileHandle(node *inode, dir rawDir, f File, flags uint32) (uint64, *openedFile) { func (me *fileSystemMount) registerFileHandle(node *inode, dir rawDir, f File, flags uint32) (uint64, *openedFile) {
node.OpenCountMutex.Lock() node.OpenFilesMutex.Lock()
defer node.OpenCountMutex.Unlock() defer node.OpenFilesMutex.Unlock()
b := &openedFile{ b := &openedFile{
dir: dir, dir: dir,
file: f, file: f,
...@@ -96,7 +107,7 @@ func (me *fileSystemMount) registerFileHandle(node *inode, dir rawDir, f File, f ...@@ -96,7 +107,7 @@ func (me *fileSystemMount) registerFileHandle(node *inode, dir rawDir, f File, f
f = withFlags.File f = withFlags.File
} }
node.OpenCount++ node.OpenFiles = append(node.OpenFiles, b)
handle := me.openFiles.Register(&b.Handled) handle := me.openFiles.Register(&b.Handled)
return handle, b return handle, b
} }
...@@ -116,8 +127,8 @@ type inode struct { ...@@ -116,8 +127,8 @@ type inode struct {
NodeId uint64 NodeId uint64
// Number of open files and its protection. // Number of open files and its protection.
OpenCountMutex sync.Mutex OpenFilesMutex sync.Mutex
OpenCount int OpenFiles []*openedFile
// treeLock is a pointer to me.mount.treeLock; we need store // treeLock is a pointer to me.mount.treeLock; we need store
// this mutex separately, since unmount may set me.mount = nil // this mutex separately, since unmount may set me.mount = nil
...@@ -168,9 +179,9 @@ func (me *inode) canUnmount() bool { ...@@ -168,9 +179,9 @@ func (me *inode) canUnmount() bool {
} }
} }
me.OpenCountMutex.Lock() me.OpenFilesMutex.Lock()
defer me.OpenCountMutex.Unlock() defer me.OpenFilesMutex.Unlock()
return me.OpenCount == 0 return len(me.OpenFiles) == 0
} }
// Must be called with treeLock held // Must be called with treeLock held
...@@ -411,10 +422,10 @@ func (me *FileSystemConnector) considerDropInode(n *inode) { ...@@ -411,10 +422,10 @@ func (me *FileSystemConnector) considerDropInode(n *inode) {
defer n.treeLock.Unlock() defer n.treeLock.Unlock()
} }
n.OpenCountMutex.Lock() n.OpenFilesMutex.Lock()
defer n.OpenCountMutex.Unlock() defer n.OpenFilesMutex.Unlock()
dropInode := n.LookupCount <= 0 && len(n.Children) == 0 && dropInode := n.LookupCount <= 0 && len(n.Children) == 0 &&
n.OpenCount <= 0 && n != me.rootNode && n.mountPoint == nil len(n.OpenFiles) <= 0 && n != me.rootNode && n.mountPoint == nil
if dropInode { if dropInode {
n.setParent(nil) n.setParent(nil)
if n != me.rootNode { if n != me.rootNode {
......
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