filehandle_map.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. package mount
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/util"
  4. "sync"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  6. )
  7. type FileHandleToInode struct {
  8. sync.RWMutex
  9. inode2fh map[uint64]*FileHandle
  10. fh2inode map[FileHandleId]uint64
  11. }
  12. func NewFileHandleToInode() *FileHandleToInode {
  13. return &FileHandleToInode{
  14. inode2fh: make(map[uint64]*FileHandle),
  15. fh2inode: make(map[FileHandleId]uint64),
  16. }
  17. }
  18. func (i *FileHandleToInode) GetFileHandle(fh FileHandleId) *FileHandle {
  19. i.RLock()
  20. defer i.RUnlock()
  21. inode, found := i.fh2inode[fh]
  22. if found {
  23. return i.inode2fh[inode]
  24. }
  25. return nil
  26. }
  27. func (i *FileHandleToInode) FindFileHandle(inode uint64) (fh *FileHandle, found bool) {
  28. i.RLock()
  29. defer i.RUnlock()
  30. fh, found = i.inode2fh[inode]
  31. return
  32. }
  33. func (i *FileHandleToInode) AcquireFileHandle(wfs *WFS, inode uint64, entry *filer_pb.Entry) *FileHandle {
  34. i.Lock()
  35. defer i.Unlock()
  36. fh, found := i.inode2fh[inode]
  37. if !found {
  38. fh = newFileHandle(wfs, FileHandleId(util.RandomUint64()), inode, entry)
  39. i.inode2fh[inode] = fh
  40. i.fh2inode[fh.fh] = inode
  41. } else {
  42. fh.counter++
  43. }
  44. if fh.GetEntry().GetEntry() != entry {
  45. fh.SetEntry(entry)
  46. }
  47. return fh
  48. }
  49. func (i *FileHandleToInode) ReleaseByInode(inode uint64) {
  50. i.Lock()
  51. defer i.Unlock()
  52. fh, found := i.inode2fh[inode]
  53. if found {
  54. fh.counter--
  55. if fh.counter <= 0 {
  56. delete(i.inode2fh, inode)
  57. delete(i.fh2inode, fh.fh)
  58. fh.ReleaseHandle()
  59. }
  60. }
  61. }
  62. func (i *FileHandleToInode) ReleaseByHandle(fh FileHandleId) {
  63. i.Lock()
  64. defer i.Unlock()
  65. inode, found := i.fh2inode[fh]
  66. if found {
  67. fhHandle, fhFound := i.inode2fh[inode]
  68. if !fhFound {
  69. delete(i.fh2inode, fh)
  70. } else {
  71. fhHandle.counter--
  72. if fhHandle.counter <= 0 {
  73. delete(i.inode2fh, inode)
  74. delete(i.fh2inode, fhHandle.fh)
  75. fhHandle.ReleaseHandle()
  76. }
  77. }
  78. }
  79. }