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.

77 lines
1.5 KiB

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) AcquireFileHandle(wfs *WFS, inode uint64, entry *filer_pb.Entry) *FileHandle {
  29. i.Lock()
  30. defer i.Unlock()
  31. fh, found := i.inode2fh[inode]
  32. if !found {
  33. fh = newFileHandle(wfs, i.nextFh, inode, entry)
  34. i.nextFh++
  35. i.inode2fh[inode] = fh
  36. i.fh2inode[fh.fh] = inode
  37. } else {
  38. fh.counter++
  39. }
  40. return fh
  41. }
  42. func (i *FileHandleToInode) ReleaseByInode(inode uint64) {
  43. i.Lock()
  44. defer i.Unlock()
  45. fh, found := i.inode2fh[inode]
  46. if found {
  47. fh.counter--
  48. if fh.counter <= 0 {
  49. delete(i.inode2fh, inode)
  50. delete(i.fh2inode, fh.fh)
  51. }
  52. }
  53. }
  54. func (i *FileHandleToInode) ReleaseByHandle(fh FileHandleId) {
  55. i.Lock()
  56. defer i.Unlock()
  57. inode, found := i.fh2inode[fh]
  58. if found {
  59. fhHandle, fhFound := i.inode2fh[inode]
  60. if !fhFound {
  61. delete(i.fh2inode, fh)
  62. } else {
  63. fhHandle.counter--
  64. if fhHandle.counter <= 0 {
  65. delete(i.inode2fh, inode)
  66. delete(i.fh2inode, fhHandle.fh)
  67. }
  68. }
  69. }
  70. }