2016-02-06 19:27:59 +01:00
|
|
|
package fusefrontend
|
2016-01-24 13:08:08 +01:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2016-10-25 23:57:30 +02:00
|
|
|
"sync/atomic"
|
2016-11-17 20:32:19 +01:00
|
|
|
"syscall"
|
2016-01-24 13:08:08 +01:00
|
|
|
)
|
|
|
|
|
2016-11-17 20:32:19 +01:00
|
|
|
// DevInoStruct uniquely identifies a backing file through device number and
|
|
|
|
// inode number.
|
|
|
|
type DevInoStruct struct {
|
|
|
|
dev uint64
|
|
|
|
ino uint64
|
|
|
|
}
|
|
|
|
|
2016-11-17 23:40:03 +01:00
|
|
|
// DevInoFromStat fills a new DevInoStruct with the passed Stat_t info
|
2016-11-17 20:32:19 +01:00
|
|
|
func DevInoFromStat(st *syscall.Stat_t) DevInoStruct {
|
|
|
|
// Explicit cast to uint64 to prevent build problems on 32-bit platforms
|
|
|
|
return DevInoStruct{
|
|
|
|
dev: uint64(st.Dev),
|
|
|
|
ino: uint64(st.Ino),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-24 13:08:08 +01:00
|
|
|
func init() {
|
2016-11-17 22:29:45 +01:00
|
|
|
openFileMap.entries = make(map[DevInoStruct]*openFileEntryT)
|
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.
|
2016-11-17 22:29:45 +01:00
|
|
|
var openFileMap openFileMapT
|
2016-01-24 13:08:08 +01:00
|
|
|
|
|
|
|
// wlockMap - usage:
|
|
|
|
// 1) register
|
|
|
|
// 2) lock ... unlock ...
|
|
|
|
// 3) unregister
|
2016-11-17 22:29:45 +01:00
|
|
|
type openFileMapT struct {
|
|
|
|
// opCount counts writeLock.Lock() calls. As every operation that modifies a file should
|
2016-10-25 23:57:30 +02:00
|
|
|
// call it, this effectively serves as a write-operation counter.
|
|
|
|
// The variable is accessed without holding any locks so atomic operations
|
|
|
|
// must be used. It must be the first element of the struct to guarantee
|
|
|
|
// 64-bit alignment.
|
|
|
|
opCount uint64
|
2016-05-29 22:40:05 +02:00
|
|
|
// Protects map access
|
|
|
|
sync.Mutex
|
2016-11-17 22:29:45 +01:00
|
|
|
entries map[DevInoStruct]*openFileEntryT
|
2016-05-29 22:40:05 +02:00
|
|
|
}
|
|
|
|
|
2016-11-17 22:29:45 +01:00
|
|
|
type opCountMutex struct {
|
2016-05-29 22:40:05 +02:00
|
|
|
sync.Mutex
|
2016-11-17 22:29:45 +01:00
|
|
|
// Points to the opCount variable of the parent openFileMapT
|
|
|
|
opCount *uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func (o *opCountMutex) Lock() {
|
|
|
|
o.Mutex.Lock()
|
|
|
|
atomic.AddUint64(o.opCount, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// refCntMutex - mutex with reference count
|
|
|
|
type openFileEntryT struct {
|
2016-05-29 22:40:05 +02:00
|
|
|
// Reference count
|
|
|
|
refCnt int
|
2016-11-17 22:29:45 +01:00
|
|
|
// Write lock for this inode
|
|
|
|
writeLock *opCountMutex
|
|
|
|
// ID is the file ID in the file header.
|
|
|
|
ID []byte
|
|
|
|
IDLock sync.RWMutex
|
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-11-17 22:29:45 +01:00
|
|
|
func (w *openFileMapT) register(di DevInoStruct) *openFileEntryT {
|
2016-05-29 22:40:05 +02:00
|
|
|
w.Lock()
|
|
|
|
defer w.Unlock()
|
|
|
|
|
2016-11-17 22:29:45 +01:00
|
|
|
r := w.entries[di]
|
2016-01-24 13:08:08 +01:00
|
|
|
if r == nil {
|
2016-11-17 22:29:45 +01:00
|
|
|
o := opCountMutex{opCount: &w.opCount}
|
|
|
|
r = &openFileEntryT{writeLock: &o}
|
|
|
|
w.entries[di] = r
|
2016-01-24 13:08:08 +01:00
|
|
|
}
|
2016-05-29 22:40:05 +02:00
|
|
|
r.refCnt++
|
2016-11-17 22:29:45 +01:00
|
|
|
return r
|
2016-01-24 13:08:08 +01:00
|
|
|
}
|
|
|
|
|
2016-11-17 20:32:19 +01:00
|
|
|
// unregister decrements the reference count for "di" and deletes the entry if
|
2016-05-29 22:40:05 +02:00
|
|
|
// the reference count has reached 0.
|
2016-11-17 22:29:45 +01:00
|
|
|
func (w *openFileMapT) unregister(di DevInoStruct) {
|
2016-05-29 22:40:05 +02:00
|
|
|
w.Lock()
|
|
|
|
defer w.Unlock()
|
|
|
|
|
2016-11-17 22:29:45 +01:00
|
|
|
r := w.entries[di]
|
2016-01-24 13:08:08 +01:00
|
|
|
r.refCnt--
|
|
|
|
if r.refCnt == 0 {
|
2016-11-17 22:29:45 +01:00
|
|
|
delete(w.entries, di)
|
2016-01-24 13:08:08 +01:00
|
|
|
}
|
|
|
|
}
|