You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
1.7 KiB

3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  4. "sync"
  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. fh.entry = entry
  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.Release()
  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.Release()
  76. }
  77. }
  78. }
  79. }