Revert "Kill the mutexes, don't seem to be required"

This reverts commit 1ba41be529d306db81e13ecf55dc45922120cfa6.
This commit is contained in:
Alex Yatskov 2015-07-06 17:42:37 +09:00
parent cf4d7330f3
commit db22e54fa3
3 changed files with 43 additions and 3 deletions

23
dir.go
View File

@ -26,6 +26,7 @@ import (
"errors" "errors"
"os" "os"
"path" "path"
"sync"
"bazil.org/fuse" "bazil.org/fuse"
"bazil.org/fuse/fs" "bazil.org/fuse/fs"
@ -42,16 +43,20 @@ type verDir struct {
node *verNode node *verNode
inode uint64 inode uint64
parent *verDir parent *verDir
mutex sync.Mutex
} }
func newVerDir(node *verNode, parent *verDir) *verDir { func newVerDir(node *verNode, parent *verDir) *verDir {
dirs := make(map[string]*verDir) dirs := make(map[string]*verDir)
files := make(map[string]*verFile) 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 { func (vd *verDir) version() error {
vd.mutex.Lock()
defer vd.mutex.Unlock()
if vd.node.flags&NodeFlagNew == NodeFlagNew { if vd.node.flags&NodeFlagNew == NodeFlagNew {
return nil return nil
} }
@ -80,7 +85,9 @@ func (vd *verDir) createDir(name string) (*verDir, error) {
node := newVerNode(childPath, vd.node.ver, nil, NodeFlagDir|NodeFlagNew) node := newVerNode(childPath, vd.node.ver, nil, NodeFlagDir|NodeFlagNew)
dir := newVerDir(node, vd) dir := newVerDir(node, vd)
vd.mutex.Lock()
vd.dirs[name] = dir vd.dirs[name] = dir
vd.mutex.Unlock()
node.ver.meta.createNode(node.path) node.ver.meta.createNode(node.path)
return dir, nil return dir, nil
@ -100,13 +107,18 @@ func (vd *verDir) createFile(name string, flags fuse.OpenFlags, mode os.FileMode
return nil, nil, 0, err return nil, nil, 0, err
} }
vd.mutex.Lock()
vd.files[name] = file vd.files[name] = file
vd.mutex.Unlock()
node.ver.meta.createNode(node.path) node.ver.meta.createNode(node.path)
return file, handle, id, nil return file, handle, id, nil
} }
func (vd *verDir) removeDir(name string) error { func (vd *verDir) removeDir(name string) error {
vd.mutex.Lock()
defer vd.mutex.Unlock()
if err := vd.version(); err != nil { if err := vd.version(); err != nil {
return err return err
} }
@ -125,6 +137,9 @@ func (vd *verDir) removeDir(name string) error {
} }
func (vd *verDir) removeFile(name string) error { func (vd *verDir) removeFile(name string) error {
vd.mutex.Lock()
defer vd.mutex.Unlock()
if err := vd.version(); err != nil { if err := vd.version(); err != nil {
return err return err
} }
@ -193,6 +208,9 @@ func (vd *verDir) Remove(ctx context.Context, req *fuse.RemoveRequest) error {
// NodeRequestLookuper // NodeRequestLookuper
func (vd *verDir) Lookup(ctx context.Context, name string) (fs.Node, error) { 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 { if dir, ok := vd.dirs[name]; ok {
return dir, nil return dir, nil
} }
@ -206,6 +224,9 @@ func (vd *verDir) Lookup(ctx context.Context, name string) (fs.Node, error) {
// HandleReadDirAller // HandleReadDirAller
func (vd *verDir) ReadDirAll(ctx context.Context) ([]fuse.Dirent, error) { 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}} entries := []fuse.Dirent{{Inode: vd.inode, Name: ".", Type: fuse.DT_Dir}}
if vd.parent != nil { if vd.parent != nil {
entry := fuse.Dirent{Inode: vd.parent.inode, Name: "..", Type: fuse.DT_Dir} entry := fuse.Dirent{Inode: vd.parent.inode, Name: "..", Type: fuse.DT_Dir}

13
file.go
View File

@ -24,6 +24,7 @@ package main
import ( import (
"os" "os"
"sync"
"bazil.org/fuse" "bazil.org/fuse"
"bazil.org/fuse/fs" "bazil.org/fuse/fs"
@ -41,13 +42,17 @@ type verFile struct {
inode uint64 inode uint64
parent *verDir parent *verDir
handles handleMap handles handleMap
mutex sync.Mutex
} }
func newVerFile(node *verNode, parent *verDir) *verFile { 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 { func (vf *verFile) version() error {
vf.mutex.Lock()
defer vf.mutex.Unlock()
if vf.node.flags&NodeFlagNew == NodeFlagNew { if vf.node.flags&NodeFlagNew == NodeFlagNew {
return nil return nil
} }
@ -80,13 +85,17 @@ func (vf *verFile) open(flags fuse.OpenFlags, mode os.FileMode) (*verFileHandle,
id := allocHandleId() id := allocHandleId()
verHandle := &verFileHandle{vf, path, handle} verHandle := &verFileHandle{vf, path, handle}
vf.mutex.Lock()
vf.handles[id] = verHandle vf.handles[id] = verHandle
vf.mutex.Unlock()
return verHandle, id, nil return verHandle, id, nil
} }
func (vf *verFile) release(handle fuse.HandleID) { func (vf *verFile) release(handle fuse.HandleID) {
vf.mutex.Lock()
delete(vf.handles, handle) delete(vf.handles, handle)
vf.mutex.Unlock()
} }
// Node // Node
@ -119,6 +128,8 @@ func (vf *verFile) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.O
// NodeFsyncer // NodeFsyncer
func (vf *verFile) Fsync(ctx context.Context, req *fuse.FsyncRequest) error { 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() return vf.handles[req.Handle].handle.Sync()
} }

10
meta.go
View File

@ -28,6 +28,7 @@ import (
"os" "os"
"path" "path"
"strings" "strings"
"sync"
) )
// //
@ -42,10 +43,11 @@ type verMeta struct {
path string path string
deleted map[string]bool deleted map[string]bool
modified bool modified bool
mutex sync.Mutex
} }
func newVerMeta(path string) (*verMeta, error) { 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 { if err := meta.load(); err != nil {
return nil, err return nil, err
} }
@ -68,12 +70,18 @@ func (m *verMeta) filter(nodes verNodeMap) {
} }
func (m *verMeta) removeNode(path string) { func (m *verMeta) removeNode(path string) {
m.mutex.Lock()
m.deleted[path] = true m.deleted[path] = true
m.mutex.Unlock()
m.modified = true m.modified = true
} }
func (m *verMeta) createNode(path string) { func (m *verMeta) createNode(path string) {
m.mutex.Lock()
m.deleted[path] = false m.deleted[path] = false
m.mutex.Unlock()
m.modified = true m.modified = true
} }