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.

203 lines
5.5 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "context"
  4. "github.com/chrislusf/seaweedfs/weed/filer"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/mount/meta_cache"
  7. "github.com/chrislusf/seaweedfs/weed/util"
  8. "github.com/hanwen/go-fuse/v2/fuse"
  9. "math"
  10. "os"
  11. "sync"
  12. )
  13. type DirectoryHandleId uint64
  14. type DirectoryHandle struct {
  15. isFinished bool
  16. lastEntryName string
  17. }
  18. type DirectoryHandleToInode struct {
  19. // shares the file handle id sequencer with FileHandleToInode{nextFh}
  20. sync.Mutex
  21. dir2inode map[DirectoryHandleId]*DirectoryHandle
  22. }
  23. func NewDirectoryHandleToInode() *DirectoryHandleToInode {
  24. return &DirectoryHandleToInode{
  25. dir2inode: make(map[DirectoryHandleId]*DirectoryHandle),
  26. }
  27. }
  28. func (wfs *WFS) AcquireDirectoryHandle() (DirectoryHandleId, *DirectoryHandle) {
  29. wfs.fhmap.Lock()
  30. fh := wfs.fhmap.nextFh
  31. wfs.fhmap.nextFh++
  32. wfs.fhmap.Unlock()
  33. wfs.dhmap.Lock()
  34. defer wfs.dhmap.Unlock()
  35. dh := &DirectoryHandle{
  36. isFinished: false,
  37. lastEntryName: "",
  38. }
  39. wfs.dhmap.dir2inode[DirectoryHandleId(fh)] = dh
  40. return DirectoryHandleId(fh), dh
  41. }
  42. func (wfs *WFS) GetDirectoryHandle(dhid DirectoryHandleId) *DirectoryHandle {
  43. wfs.dhmap.Lock()
  44. defer wfs.dhmap.Unlock()
  45. if dh, found := wfs.dhmap.dir2inode[dhid]; found {
  46. return dh
  47. }
  48. dh := &DirectoryHandle{
  49. isFinished: false,
  50. lastEntryName: "",
  51. }
  52. wfs.dhmap.dir2inode[dhid] = dh
  53. return dh
  54. }
  55. func (wfs *WFS) ReleaseDirectoryHandle(dhid DirectoryHandleId) {
  56. wfs.dhmap.Lock()
  57. defer wfs.dhmap.Unlock()
  58. delete(wfs.dhmap.dir2inode, dhid)
  59. }
  60. // Directory handling
  61. /** Open directory
  62. *
  63. * Unless the 'default_permissions' mount option is given,
  64. * this method should check if opendir is permitted for this
  65. * directory. Optionally opendir may also return an arbitrary
  66. * filehandle in the fuse_file_info structure, which will be
  67. * passed to readdir, releasedir and fsyncdir.
  68. */
  69. func (wfs *WFS) OpenDir(cancel <-chan struct{}, input *fuse.OpenIn, out *fuse.OpenOut) (code fuse.Status) {
  70. if !wfs.inodeToPath.HasInode(input.NodeId) {
  71. return fuse.ENOENT
  72. }
  73. dhid, _ := wfs.AcquireDirectoryHandle()
  74. out.Fh = uint64(dhid)
  75. return fuse.OK
  76. }
  77. /** Release directory
  78. *
  79. * If the directory has been removed after the call to opendir, the
  80. * path parameter will be NULL.
  81. */
  82. func (wfs *WFS) ReleaseDir(input *fuse.ReleaseIn) {
  83. wfs.ReleaseDirectoryHandle(DirectoryHandleId(input.Fh))
  84. }
  85. /** Synchronize directory contents
  86. *
  87. * If the directory has been removed after the call to opendir, the
  88. * path parameter will be NULL.
  89. *
  90. * If the datasync parameter is non-zero, then only the user data
  91. * should be flushed, not the meta data
  92. */
  93. func (wfs *WFS) FsyncDir(cancel <-chan struct{}, input *fuse.FsyncIn) (code fuse.Status) {
  94. return fuse.OK
  95. }
  96. /** Read directory
  97. *
  98. * The filesystem may choose between two modes of operation:
  99. *
  100. * 1) The readdir implementation ignores the offset parameter, and
  101. * passes zero to the filler function's offset. The filler
  102. * function will not return '1' (unless an error happens), so the
  103. * whole directory is read in a single readdir operation.
  104. *
  105. * 2) The readdir implementation keeps track of the offsets of the
  106. * directory entries. It uses the offset parameter and always
  107. * passes non-zero offset to the filler function. When the buffer
  108. * is full (or an error happens) the filler function will return
  109. * '1'.
  110. */
  111. func (wfs *WFS) ReadDir(cancel <-chan struct{}, input *fuse.ReadIn, out *fuse.DirEntryList) (code fuse.Status) {
  112. return wfs.doReadDirectory(input, out, false)
  113. }
  114. func (wfs *WFS) ReadDirPlus(cancel <-chan struct{}, input *fuse.ReadIn, out *fuse.DirEntryList) (code fuse.Status) {
  115. return wfs.doReadDirectory(input, out, true)
  116. }
  117. func (wfs *WFS) doReadDirectory(input *fuse.ReadIn, out *fuse.DirEntryList, isPlusMode bool) fuse.Status {
  118. dh := wfs.GetDirectoryHandle(DirectoryHandleId(input.Fh))
  119. if dh.isFinished {
  120. if input.Offset == 0 {
  121. dh.isFinished = false
  122. dh.lastEntryName = ""
  123. } else {
  124. return fuse.OK
  125. }
  126. }
  127. isEarlyTerminated := false
  128. dirPath := wfs.inodeToPath.GetPath(input.NodeId)
  129. var dirEntry fuse.DirEntry
  130. if input.Offset == 0 && !isPlusMode {
  131. dirEntry.Ino = input.NodeId
  132. dirEntry.Name = "."
  133. dirEntry.Mode = toSystemMode(os.ModeDir)
  134. out.AddDirEntry(dirEntry)
  135. parentDir, _ := dirPath.DirAndName()
  136. parentInode := wfs.inodeToPath.GetInode(util.FullPath(parentDir))
  137. dirEntry.Ino = parentInode
  138. dirEntry.Name = ".."
  139. dirEntry.Mode = toSystemMode(os.ModeDir)
  140. out.AddDirEntry(dirEntry)
  141. }
  142. processEachEntryFn := func(entry *filer.Entry, isLast bool) bool {
  143. dirEntry.Name = entry.Name()
  144. inode := wfs.inodeToPath.GetInode(dirPath.Child(dirEntry.Name))
  145. dirEntry.Ino = inode
  146. dirEntry.Mode = toSystemMode(entry.Mode)
  147. if !isPlusMode {
  148. if !out.AddDirEntry(dirEntry) {
  149. isEarlyTerminated = true
  150. return false
  151. }
  152. } else {
  153. entryOut := out.AddDirLookupEntry(dirEntry)
  154. if entryOut == nil {
  155. isEarlyTerminated = true
  156. return false
  157. }
  158. wfs.outputFilerEntry(entryOut, inode, entry)
  159. }
  160. dh.lastEntryName = entry.Name()
  161. return true
  162. }
  163. if err := meta_cache.EnsureVisited(wfs.metaCache, wfs, dirPath); err != nil {
  164. glog.Errorf("dir ReadDirAll %s: %v", dirPath, err)
  165. return fuse.EIO
  166. }
  167. listErr := wfs.metaCache.ListDirectoryEntries(context.Background(), dirPath, dh.lastEntryName, false, int64(math.MaxInt32), func(entry *filer.Entry) bool {
  168. return processEachEntryFn(entry, false)
  169. })
  170. if listErr != nil {
  171. glog.Errorf("list meta cache: %v", listErr)
  172. return fuse.EIO
  173. }
  174. if !isEarlyTerminated {
  175. dh.isFinished = true
  176. }
  177. return fuse.OK
  178. }