2016-02-06 19:27:59 +01:00
|
|
|
package fusefrontend
|
2016-01-24 13:08:08 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2016-05-29 22:40:05 +02:00
|
|
|
wlock.inodeLocks = make(map[uint64]*refCntMutex)
|
2016-01-24 13:08:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2016-05-29 22:40:05 +02:00
|
|
|
// Protects map access
|
|
|
|
sync.Mutex
|
|
|
|
inodeLocks map[uint64]*refCntMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
// refCntMutex - mutex with reference count
|
|
|
|
type refCntMutex struct {
|
|
|
|
// Write lock for this inode
|
|
|
|
sync.Mutex
|
|
|
|
// Reference count
|
|
|
|
refCnt int
|
2016-01-24 13:08:08 +01:00
|
|
|
}
|
|
|
|
|
2016-05-29 22:40:05 +02:00
|
|
|
// register creates an entry for "ino", or incrementes the reference count
|
|
|
|
// if the entry already exists.
|
2016-01-24 13:08:08 +01:00
|
|
|
func (w *wlockMap) register(ino uint64) {
|
2016-05-29 22:40:05 +02:00
|
|
|
w.Lock()
|
|
|
|
defer w.Unlock()
|
|
|
|
|
|
|
|
r := w.inodeLocks[ino]
|
2016-01-24 13:08:08 +01:00
|
|
|
if r == nil {
|
|
|
|
r = &refCntMutex{}
|
2016-05-29 22:40:05 +02:00
|
|
|
w.inodeLocks[ino] = r
|
2016-01-24 13:08:08 +01:00
|
|
|
}
|
2016-05-29 22:40:05 +02:00
|
|
|
r.refCnt++
|
2016-01-24 13:08:08 +01:00
|
|
|
}
|
|
|
|
|
2016-05-29 22:40:05 +02:00
|
|
|
// unregister decrements the reference count for "ino" and deletes the entry if
|
|
|
|
// the reference count has reached 0.
|
2016-01-24 13:08:08 +01:00
|
|
|
func (w *wlockMap) unregister(ino uint64) {
|
2016-05-29 22:40:05 +02:00
|
|
|
w.Lock()
|
|
|
|
defer w.Unlock()
|
|
|
|
|
|
|
|
r := w.inodeLocks[ino]
|
2016-01-24 13:08:08 +01:00
|
|
|
r.refCnt--
|
|
|
|
if r.refCnt == 0 {
|
2016-05-29 22:40:05 +02:00
|
|
|
delete(w.inodeLocks, ino)
|
2016-01-24 13:08:08 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-29 22:40:05 +02:00
|
|
|
// lock retrieves the entry for "ino" and locks it.
|
2016-01-24 13:08:08 +01:00
|
|
|
func (w *wlockMap) lock(ino uint64) {
|
2016-05-29 22:40:05 +02:00
|
|
|
w.Lock()
|
|
|
|
r := w.inodeLocks[ino]
|
|
|
|
w.Unlock()
|
|
|
|
// this can take a long time - execute outside the wlockMap lock
|
|
|
|
r.Lock()
|
2016-01-24 13:08:08 +01:00
|
|
|
}
|
|
|
|
|
2016-05-29 22:40:05 +02:00
|
|
|
// unlock retrieves the entry for "ino" and unlocks it.
|
2016-01-24 13:08:08 +01:00
|
|
|
func (w *wlockMap) unlock(ino uint64) {
|
2016-05-29 22:40:05 +02:00
|
|
|
w.Lock()
|
|
|
|
r := w.inodeLocks[ino]
|
|
|
|
w.Unlock()
|
2016-01-24 13:08:08 +01:00
|
|
|
r.Unlock()
|
|
|
|
}
|