From db22e54fa3a4a9326778f1acc15d1178d3bbe11b Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Mon, 6 Jul 2015 17:42:37 +0900 Subject: [PATCH] Revert "Kill the mutexes, don't seem to be required" This reverts commit 1ba41be529d306db81e13ecf55dc45922120cfa6. --- dir.go | 23 ++++++++++++++++++++++- file.go | 13 ++++++++++++- meta.go | 10 +++++++++- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/dir.go b/dir.go index 755e5b3..35da465 100644 --- a/dir.go +++ b/dir.go @@ -26,6 +26,7 @@ import ( "errors" "os" "path" + "sync" "bazil.org/fuse" "bazil.org/fuse/fs" @@ -42,16 +43,20 @@ type verDir struct { node *verNode inode uint64 parent *verDir + mutex sync.Mutex } func newVerDir(node *verNode, parent *verDir) *verDir { dirs := make(map[string]*verDir) files := make(map[string]*verFile) - return &verDir{dirs, files, node, allocInode(), parent} + return &verDir{dirs, files, node, allocInode(), parent, sync.Mutex{}} } func (vd *verDir) version() error { + vd.mutex.Lock() + defer vd.mutex.Unlock() + if vd.node.flags&NodeFlagNew == NodeFlagNew { return nil } @@ -80,7 +85,9 @@ func (vd *verDir) createDir(name string) (*verDir, error) { node := newVerNode(childPath, vd.node.ver, nil, NodeFlagDir|NodeFlagNew) dir := newVerDir(node, vd) + vd.mutex.Lock() vd.dirs[name] = dir + vd.mutex.Unlock() node.ver.meta.createNode(node.path) return dir, nil @@ -100,13 +107,18 @@ func (vd *verDir) createFile(name string, flags fuse.OpenFlags, mode os.FileMode return nil, nil, 0, err } + vd.mutex.Lock() vd.files[name] = file + vd.mutex.Unlock() node.ver.meta.createNode(node.path) return file, handle, id, nil } func (vd *verDir) removeDir(name string) error { + vd.mutex.Lock() + defer vd.mutex.Unlock() + if err := vd.version(); err != nil { return err } @@ -125,6 +137,9 @@ func (vd *verDir) removeDir(name string) error { } func (vd *verDir) removeFile(name string) error { + vd.mutex.Lock() + defer vd.mutex.Unlock() + if err := vd.version(); err != nil { return err } @@ -193,6 +208,9 @@ func (vd *verDir) Remove(ctx context.Context, req *fuse.RemoveRequest) error { // NodeRequestLookuper func (vd *verDir) Lookup(ctx context.Context, name string) (fs.Node, error) { + vd.mutex.Lock() + defer vd.mutex.Unlock() + if dir, ok := vd.dirs[name]; ok { return dir, nil } @@ -206,6 +224,9 @@ func (vd *verDir) Lookup(ctx context.Context, name string) (fs.Node, error) { // HandleReadDirAller func (vd *verDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { + vd.mutex.Lock() + defer vd.mutex.Unlock() + entries := []fuse.Dirent{{Inode: vd.inode, Name: ".", Type: fuse.DT_Dir}} if vd.parent != nil { entry := fuse.Dirent{Inode: vd.parent.inode, Name: "..", Type: fuse.DT_Dir} diff --git a/file.go b/file.go index 696c4df..77ae9c2 100644 --- a/file.go +++ b/file.go @@ -24,6 +24,7 @@ package main import ( "os" + "sync" "bazil.org/fuse" "bazil.org/fuse/fs" @@ -41,13 +42,17 @@ type verFile struct { inode uint64 parent *verDir handles handleMap + mutex sync.Mutex } func newVerFile(node *verNode, parent *verDir) *verFile { - return &verFile{node, allocInode(), parent, make(handleMap)} + return &verFile{node, allocInode(), parent, make(handleMap), sync.Mutex{}} } func (vf *verFile) version() error { + vf.mutex.Lock() + defer vf.mutex.Unlock() + if vf.node.flags&NodeFlagNew == NodeFlagNew { return nil } @@ -80,13 +85,17 @@ func (vf *verFile) open(flags fuse.OpenFlags, mode os.FileMode) (*verFileHandle, id := allocHandleId() verHandle := &verFileHandle{vf, path, handle} + vf.mutex.Lock() vf.handles[id] = verHandle + vf.mutex.Unlock() return verHandle, id, nil } func (vf *verFile) release(handle fuse.HandleID) { + vf.mutex.Lock() delete(vf.handles, handle) + vf.mutex.Unlock() } // Node @@ -119,6 +128,8 @@ func (vf *verFile) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.O // NodeFsyncer func (vf *verFile) Fsync(ctx context.Context, req *fuse.FsyncRequest) error { + vf.mutex.Lock() + defer vf.mutex.Unlock() return vf.handles[req.Handle].handle.Sync() } diff --git a/meta.go b/meta.go index d3f9bbc..4ebbb8a 100644 --- a/meta.go +++ b/meta.go @@ -28,6 +28,7 @@ import ( "os" "path" "strings" + "sync" ) // @@ -42,10 +43,11 @@ type verMeta struct { path string deleted map[string]bool modified bool + mutex sync.Mutex } func newVerMeta(path string) (*verMeta, error) { - meta := &verMeta{path, make(map[string]bool), false} + meta := &verMeta{path, make(map[string]bool), false, sync.Mutex{}} if err := meta.load(); err != nil { return nil, err } @@ -68,12 +70,18 @@ func (m *verMeta) filter(nodes verNodeMap) { } func (m *verMeta) removeNode(path string) { + m.mutex.Lock() m.deleted[path] = true + m.mutex.Unlock() + m.modified = true } func (m *verMeta) createNode(path string) { + m.mutex.Lock() m.deleted[path] = false + m.mutex.Unlock() + m.modified = true }