From 1ff2c07417588a41b0c155342b74f82345e980f7 Mon Sep 17 00:00:00 2001 From: Sridharan Kuppa Date: Mon, 26 Oct 2020 14:05:19 -0700 Subject: [PATCH] Fixing the creation of multiple inode creation for the same name when MkDir or CreateFile executed simultaneously. 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 | 8 ++++---- 2 files changed, 4 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 9070fb54..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) @@ -1049,14 +1049,14 @@ func (fs *Goofys) MkDir( parent := fs.getInodeOrDie(op.Parent) fs.mu.RUnlock() + parent.mu.Lock() + // ignore op.Mode for now inode, err := parent.MkDir(op.Name) if err != nil { return err } - parent.mu.Lock() - fs.mu.Lock() defer fs.mu.Unlock() fs.insertInode(parent, inode)