From d714e442a930b78929f459c7fe9006f5349434ff Mon Sep 17 00:00:00 2001 From: Sridharan Kuppa Date: Tue, 27 Oct 2020 11:52:52 -0700 Subject: [PATCH] Get parent lock before creating inode from `Goofys.MkDir` or `Goofys.CreateFile` to avoid creating a duplicate inode. Remove get lock from `Inode.Create` and `Inode.MkDir` since it lock already acquired by the caller. --- internal/dir.go | 6 ------ internal/goofys.go | 12 ++++++++---- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/internal/dir.go b/internal/dir.go index 687674f0..e40bcdce 100644 --- a/internal/dir.go +++ b/internal/dir.go @@ -905,9 +905,6 @@ func (parent *Inode) Create( fs := parent.fs - parent.mu.Lock() - defer parent.mu.Unlock() - now := time.Now() inode = NewInode(fs, parent, &name) inode.Attributes = InodeAttributes{ @@ -948,9 +945,6 @@ func (parent *Inode) MkDir( return } - parent.mu.Lock() - defer parent.mu.Unlock() - inode = NewInode(fs, parent, &name) inode.ToDir() inode.touch() diff --git a/internal/goofys.go b/internal/goofys.go index dd64ce61..2546ea11 100644 --- a/internal/goofys.go +++ b/internal/goofys.go @@ -1013,10 +1013,10 @@ func (fs *Goofys) CreateFile( parent := fs.getInodeOrDie(op.Parent) fs.mu.RUnlock() - inode, fh := parent.Create(op.Name, op.Metadata) - parent.mu.Lock() + inode, fh := parent.Create(op.Name, op.Metadata) + fs.mu.Lock() defer fs.mu.Unlock() fs.insertInode(parent, inode) @@ -1046,8 +1046,10 @@ func (fs *Goofys) MkDir( op *fuseops.MkDirOp) (err error) { fs.mu.RLock() - defer fs.mu.RUnlock() parent := fs.getInodeOrDie(op.Parent) + fs.mu.RUnlock() + + parent.mu.Lock() // ignore op.Mode for now inode, err := parent.MkDir(op.Name) @@ -1055,8 +1057,10 @@ func (fs *Goofys) MkDir( return err } - parent.mu.Lock() + fs.mu.Lock() + defer fs.mu.Unlock() fs.insertInode(parent, inode) + parent.mu.Unlock() op.Entry.Child = inode.Id