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.

198 lines
5.4 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. return fuse.OK
  121. }
  122. isEarlyTerminated := false
  123. dirPath := wfs.inodeToPath.GetPath(input.NodeId)
  124. var dirEntry fuse.DirEntry
  125. if input.Offset == 0 && !isPlusMode {
  126. dirEntry.Ino = input.NodeId
  127. dirEntry.Name = "."
  128. dirEntry.Mode = toSystemMode(os.ModeDir)
  129. out.AddDirEntry(dirEntry)
  130. parentDir, _ := dirPath.DirAndName()
  131. parentInode := wfs.inodeToPath.GetInode(util.FullPath(parentDir))
  132. dirEntry.Ino = parentInode
  133. dirEntry.Name = ".."
  134. dirEntry.Mode = toSystemMode(os.ModeDir)
  135. out.AddDirEntry(dirEntry)
  136. }
  137. processEachEntryFn := func(entry *filer.Entry, isLast bool) bool {
  138. dirEntry.Name = entry.Name()
  139. inode := wfs.inodeToPath.GetInode(dirPath.Child(dirEntry.Name))
  140. dirEntry.Ino = inode
  141. dirEntry.Mode = toSystemMode(entry.Mode)
  142. if !isPlusMode {
  143. if !out.AddDirEntry(dirEntry) {
  144. isEarlyTerminated = true
  145. return false
  146. }
  147. } else {
  148. entryOut := out.AddDirLookupEntry(dirEntry)
  149. if entryOut == nil {
  150. isEarlyTerminated = true
  151. return false
  152. }
  153. wfs.outputFilerEntry(entryOut, inode, entry)
  154. }
  155. dh.lastEntryName = entry.Name()
  156. return true
  157. }
  158. if err := meta_cache.EnsureVisited(wfs.metaCache, wfs, dirPath); err != nil {
  159. glog.Errorf("dir ReadDirAll %s: %v", dirPath, err)
  160. return fuse.EIO
  161. }
  162. listErr := wfs.metaCache.ListDirectoryEntries(context.Background(), dirPath, dh.lastEntryName, false, int64(math.MaxInt32), func(entry *filer.Entry) bool {
  163. return processEachEntryFn(entry, false)
  164. })
  165. if listErr != nil {
  166. glog.Errorf("list meta cache: %v", listErr)
  167. return fuse.EIO
  168. }
  169. if !isEarlyTerminated {
  170. dh.isFinished = true
  171. }
  172. return fuse.OK
  173. }