filehandle_map.go 1.7 KB

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