2022-02-14 11:14:34 +08:00
|
|
|
package mount
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2022-08-27 07:41:37 +08:00
|
|
|
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
|
2022-02-14 11:14:34 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
type FileHandleToInode struct {
|
|
|
|
sync.RWMutex
|
|
|
|
nextFh FileHandleId
|
|
|
|
inode2fh map[uint64]*FileHandle
|
|
|
|
fh2inode map[FileHandleId]uint64
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewFileHandleToInode() *FileHandleToInode {
|
|
|
|
return &FileHandleToInode{
|
|
|
|
inode2fh: make(map[uint64]*FileHandle),
|
|
|
|
fh2inode: make(map[FileHandleId]uint64),
|
|
|
|
nextFh: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-14 14:50:44 +08:00
|
|
|
func (i *FileHandleToInode) GetFileHandle(fh FileHandleId) *FileHandle {
|
|
|
|
i.RLock()
|
|
|
|
defer i.RUnlock()
|
|
|
|
inode, found := i.fh2inode[fh]
|
|
|
|
if found {
|
|
|
|
return i.inode2fh[inode]
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-14 17:36:10 +08:00
|
|
|
func (i *FileHandleToInode) FindFileHandle(inode uint64) (fh *FileHandle, found bool) {
|
|
|
|
i.RLock()
|
|
|
|
defer i.RUnlock()
|
|
|
|
fh, found = i.inode2fh[inode]
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-14 14:50:44 +08:00
|
|
|
func (i *FileHandleToInode) AcquireFileHandle(wfs *WFS, inode uint64, entry *filer_pb.Entry) *FileHandle {
|
2022-02-14 11:14:34 +08:00
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
fh, found := i.inode2fh[inode]
|
|
|
|
if !found {
|
2022-02-14 14:50:44 +08:00
|
|
|
fh = newFileHandle(wfs, i.nextFh, inode, entry)
|
2022-02-14 11:14:34 +08:00
|
|
|
i.nextFh++
|
|
|
|
i.inode2fh[inode] = fh
|
|
|
|
i.fh2inode[fh.fh] = inode
|
|
|
|
} else {
|
|
|
|
fh.counter++
|
|
|
|
}
|
2022-12-05 15:33:05 +08:00
|
|
|
if fh.GetEntry() != entry {
|
2022-08-27 07:41:37 +08:00
|
|
|
fh.SetEntry(entry)
|
|
|
|
}
|
2022-02-14 11:14:34 +08:00
|
|
|
return fh
|
|
|
|
}
|
|
|
|
|
|
|
|
func (i *FileHandleToInode) ReleaseByInode(inode uint64) {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
fh, found := i.inode2fh[inode]
|
|
|
|
if found {
|
|
|
|
fh.counter--
|
|
|
|
if fh.counter <= 0 {
|
|
|
|
delete(i.inode2fh, inode)
|
|
|
|
delete(i.fh2inode, fh.fh)
|
2023-01-03 15:20:45 +08:00
|
|
|
fh.ReleaseHandle()
|
2022-02-14 11:14:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (i *FileHandleToInode) ReleaseByHandle(fh FileHandleId) {
|
|
|
|
i.Lock()
|
|
|
|
defer i.Unlock()
|
|
|
|
inode, found := i.fh2inode[fh]
|
|
|
|
if found {
|
|
|
|
fhHandle, fhFound := i.inode2fh[inode]
|
|
|
|
if !fhFound {
|
|
|
|
delete(i.fh2inode, fh)
|
|
|
|
} else {
|
|
|
|
fhHandle.counter--
|
|
|
|
if fhHandle.counter <= 0 {
|
|
|
|
delete(i.inode2fh, inode)
|
|
|
|
delete(i.fh2inode, fhHandle.fh)
|
2023-01-03 15:20:45 +08:00
|
|
|
fhHandle.ReleaseHandle()
|
2022-02-14 11:14:34 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|