diff options
author | Jakob Unterwurzacher | 2016-01-24 13:08:08 +0100 |
---|---|---|
committer | Jakob Unterwurzacher | 2016-01-24 13:08:08 +0100 |
commit | 2f32114bd356c2124d75fb1879ff230fc1f0ca1b (patch) | |
tree | 07c09e6926882bbb859e0c5999860a90833a10ee /pathfs_frontend/write_lock.go | |
parent | dac9f71089b1f20493b29edcec848ab188944990 (diff) |
Add per-inode write mutex
At the moment, FUSE writes to a single file are serialized by the kernel.
However, it is unclear if this is guaranteed behaviour or may change
in the future.
This patch adds our own per-inode write lock to rule out races regardless
of kernel behavoir.
Diffstat (limited to 'pathfs_frontend/write_lock.go')
-rw-r--r-- | pathfs_frontend/write_lock.go | 66 |
1 files changed, 66 insertions, 0 deletions
diff --git a/pathfs_frontend/write_lock.go b/pathfs_frontend/write_lock.go new file mode 100644 index 0000000..0704eb6 --- /dev/null +++ b/pathfs_frontend/write_lock.go @@ -0,0 +1,66 @@ +package pathfs_frontend + +import ( + "sync" +) + +func init() { + wlock.m = make(map[uint64]*refCntMutex) +} + +// wlock - serializes write accesses to each file (identified by inode number) +// Writing partial blocks means we have to do read-modify-write cycles. We +// really don't want concurrent writes there. +// Concurrent full-block writes could actually be allowed, but are not to +// keep the locking simple. +var wlock wlockMap + +// wlockMap - usage: +// 1) register +// 2) lock ... unlock ... +// 3) unregister +type wlockMap struct { + mapMutex sync.RWMutex + m map[uint64]*refCntMutex +} + +func (w *wlockMap) register(ino uint64) { + w.mapMutex.Lock() + r := w.m[ino] + if r == nil { + r = &refCntMutex{} + w.m[ino] = r + } + r.refCnt++ // this must happen inside the mapMutex lock + w.mapMutex.Unlock() +} + +func (w *wlockMap) unregister(ino uint64) { + w.mapMutex.Lock() + r := w.m[ino] + r.refCnt-- + if r.refCnt == 0 { + delete(w.m, ino) + } + w.mapMutex.Unlock() +} + +func (w *wlockMap) lock(ino uint64) { + w.mapMutex.RLock() + r := w.m[ino] + w.mapMutex.RUnlock() + r.Lock() // this can take a long time - execute outside the mapMutex lock +} + +func (w *wlockMap) unlock(ino uint64) { + w.mapMutex.RLock() + r := w.m[ino] + w.mapMutex.RUnlock() + r.Unlock() +} + +// refCntMutex - mutex with reference count +type refCntMutex struct { + sync.Mutex + refCnt int +} |