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.

119 lines
2.1 KiB

3 years ago
3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/glog"
  4. "github.com/chrislusf/seaweedfs/weed/util"
  5. "sync"
  6. )
  7. type InodeToPath struct {
  8. sync.RWMutex
  9. nextInodeId uint64
  10. inode2path map[uint64]*InodeEntry
  11. path2inode map[util.FullPath]uint64
  12. }
  13. type InodeEntry struct {
  14. util.FullPath
  15. nlookup uint64
  16. }
  17. func NewInodeToPath() *InodeToPath {
  18. return &InodeToPath{
  19. inode2path: make(map[uint64]*InodeEntry),
  20. path2inode: make(map[util.FullPath]uint64),
  21. nextInodeId: 2, // the root inode id is 1
  22. }
  23. }
  24. func (i *InodeToPath) Lookup(path util.FullPath) uint64 {
  25. if path == "/" {
  26. return 1
  27. }
  28. i.Lock()
  29. defer i.Unlock()
  30. inode, found := i.path2inode[path]
  31. if !found {
  32. inode = i.nextInodeId
  33. i.nextInodeId++
  34. i.path2inode[path] = inode
  35. i.inode2path[inode] = &InodeEntry{path, 1}
  36. } else {
  37. i.inode2path[inode].nlookup++
  38. }
  39. return inode
  40. }
  41. func (i *InodeToPath) GetInode(path util.FullPath) uint64 {
  42. if path == "/" {
  43. return 1
  44. }
  45. i.Lock()
  46. defer i.Unlock()
  47. inode, found := i.path2inode[path]
  48. if !found {
  49. glog.Fatalf("GetInode unknown inode %d", inode)
  50. }
  51. return inode
  52. }
  53. func (i *InodeToPath) GetPath(inode uint64) util.FullPath {
  54. if inode == 1 {
  55. return "/"
  56. }
  57. i.RLock()
  58. defer i.RUnlock()
  59. path, found := i.inode2path[inode]
  60. if !found {
  61. glog.Fatalf("not found inode %d", inode)
  62. }
  63. return path.FullPath
  64. }
  65. func (i *InodeToPath) HasPath(path util.FullPath) bool {
  66. if path == "/" {
  67. return true
  68. }
  69. i.RLock()
  70. defer i.RUnlock()
  71. _, found := i.path2inode[path]
  72. return found
  73. }
  74. func (i *InodeToPath) HasInode(inode uint64) bool {
  75. if inode == 1 {
  76. return true
  77. }
  78. i.RLock()
  79. defer i.RUnlock()
  80. _, found := i.inode2path[inode]
  81. return found
  82. }
  83. func (i *InodeToPath) RemovePath(path util.FullPath) {
  84. if path == "/" {
  85. return
  86. }
  87. i.Lock()
  88. defer i.Unlock()
  89. inode, found := i.path2inode[path]
  90. if found {
  91. delete(i.path2inode, path)
  92. delete(i.inode2path, inode)
  93. }
  94. }
  95. func (i *InodeToPath) Forget(inode, nlookup uint64) {
  96. if inode == 1 {
  97. return
  98. }
  99. i.Lock()
  100. defer i.Unlock()
  101. path, found := i.inode2path[inode]
  102. if found {
  103. path.nlookup -= nlookup
  104. if path.nlookup <= 0 {
  105. delete(i.path2inode, path.FullPath)
  106. delete(i.inode2path, inode)
  107. }
  108. }
  109. }