Commit 321434a7 authored by Han-Wen Nienhuys's avatar Han-Wen Nienhuys

nodefs: avoid slice creation for LOOKUP locking

parent 0c1f5aa9
......@@ -140,7 +140,7 @@ func (b *rawBridge) Create(input *fuse.CreateIn, name string, out *fuse.CreateOu
return code
}
lockNodes(parent, child)
lockNode2(parent, child)
parent.setEntry(name, child)
b.mu.Lock()
if child.nodeID == 0 {
......@@ -150,7 +150,7 @@ func (b *rawBridge) Create(input *fuse.CreateIn, name string, out *fuse.CreateOu
out.NodeId = child.nodeID
out.Generation = b.nodes[child.nodeID].generation
b.mu.Unlock()
unlockNodes(parent, child)
unlockNode2(parent, child)
if b.options.AttrTimeout != nil {
out.SetAttrTimeout(*b.options.AttrTimeout)
......
......@@ -92,18 +92,20 @@ func newInode(node Node, mode uint32) *Inode {
// locks on inode group.
func sortNodes(ns []*Inode) {
sort.Slice(ns, func(i, j int) bool {
return uintptr(unsafe.Pointer(ns[i])) < uintptr(unsafe.Pointer(ns[j]))
return nodeLess(ns[i], ns[j])
})
}
func nodeLess(a, b *Inode) bool {
return uintptr(unsafe.Pointer(a)) < uintptr(unsafe.Pointer(b))
}
// lockNodes locks group of inodes.
//
// It always lock the inodes in the same order - to avoid deadlocks.
// It also avoids locking an inode more than once, if it was specified multiple times.
// An example when an inode might be given multiple times is if dir/a and dir/b
// are hardlinked to the same inode and the caller needs to take locks on dir children.
//
// It is valid to give nil nodes - those are simply ignored.
func lockNodes(ns ...*Inode) {
sortNodes(ns)
......@@ -117,6 +119,23 @@ func lockNodes(ns ...*Inode) {
}
}
// lockNode2 locks a and b in order consistent with lockNodes.
func lockNode2(a, b *Inode) {
if nodeLess(a, b) {
a.mu.Lock()
b.mu.Lock()
} else {
b.mu.Lock()
a.mu.Lock()
}
}
// unlockNode2 unlocks a and b
func unlockNode2(a, b *Inode) {
a.mu.Unlock()
b.mu.Unlock()
}
// unlockNodes releases locks taken by lockNodes.
func unlockNodes(ns ...*Inode) {
// we don't need to unlock in the same order that was used in lockNodes.
......
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