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.

79 lines
1.4 KiB

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