Skip to content

Commit

Permalink
Merge pull request #1381 from iain-macdonald/main
Browse files Browse the repository at this point in the history
Protect node.ents and node.entsCached with a mutex in fs/layer/node.go
  • Loading branch information
ktock authored Sep 13, 2023
2 parents 258b4ba + 98e34dd commit 7275d45
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions fs/layer/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,13 @@ func (fs *fs) inodeOfID(id uint32) (uint64, error) {
// node is a filesystem inode abstraction.
type node struct {
fusefs.Inode
fs *fs
id uint32
attr metadata.Attr
fs *fs
id uint32
attr metadata.Attr

ents []fuse.DirEntry
entsCached bool
entsMu sync.Mutex
}

func (n *node) isRootNode() bool {
Expand Down Expand Up @@ -162,9 +164,13 @@ func (n *node) readdir() ([]fuse.DirEntry, syscall.Errno) {
start := time.Now() // set start time
defer commonmetrics.MeasureLatencyInMicroseconds(commonmetrics.NodeReaddir, n.fs.layerDigest, start)

n.entsMu.Lock()
if n.entsCached {
return n.ents, 0
ents := n.ents
n.entsMu.Unlock()
return ents, 0
}
n.entsMu.Unlock()

isRoot := n.isRootNode()

Expand Down Expand Up @@ -228,6 +234,8 @@ func (n *node) readdir() ([]fuse.DirEntry, syscall.Errno) {
sort.Slice(ents, func(i, j int) bool {
return ents[i].Name < ents[j].Name
})
n.entsMu.Lock()
defer n.entsMu.Unlock()
n.ents, n.entsCached = ents, true // cache it

return ents, 0
Expand Down Expand Up @@ -279,6 +287,7 @@ func (n *node) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fu
}

// early return if this entry doesn't exist
n.entsMu.Lock()
if n.entsCached {
var found bool
for _, e := range n.ents {
Expand All @@ -287,9 +296,11 @@ func (n *node) Lookup(ctx context.Context, name string, out *fuse.EntryOut) (*fu
}
}
if !found {
n.entsMu.Unlock()
return nil, syscall.ENOENT
}
}
n.entsMu.Unlock()

id, ce, err := n.fs.r.Metadata().GetChild(n.id, name)
if err != nil {
Expand Down

0 comments on commit 7275d45

Please sign in to comment.