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.

261 lines
7.1 KiB

7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
5 years ago
4 years ago
4 years ago
6 years ago
4 years ago
6 years ago
5 years ago
4 years ago
4 years ago
5 years ago
6 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
4 years ago
6 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "io"
  6. "math"
  7. "net/http"
  8. "os"
  9. "time"
  10. "github.com/seaweedfs/fuse"
  11. "github.com/seaweedfs/fuse/fs"
  12. "github.com/chrislusf/seaweedfs/weed/filer2"
  13. "github.com/chrislusf/seaweedfs/weed/glog"
  14. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  15. )
  16. type FileHandle struct {
  17. // cache file has been written to
  18. dirtyPages *ContinuousDirtyPages
  19. contentType string
  20. handle uint64
  21. f *File
  22. RequestId fuse.RequestID // unique ID for request
  23. NodeId fuse.NodeID // file or directory the request is about
  24. Uid uint32 // user ID of process making request
  25. Gid uint32 // group ID of process making request
  26. }
  27. func newFileHandle(file *File, uid, gid uint32) *FileHandle {
  28. fh := &FileHandle{
  29. f: file,
  30. dirtyPages: newDirtyPages(file),
  31. Uid: uid,
  32. Gid: gid,
  33. }
  34. if fh.f.entry != nil {
  35. fh.f.entry.Attributes.FileSize = filer2.FileSize(fh.f.entry)
  36. }
  37. return fh
  38. }
  39. var _ = fs.Handle(&FileHandle{})
  40. // var _ = fs.HandleReadAller(&FileHandle{})
  41. var _ = fs.HandleReader(&FileHandle{})
  42. var _ = fs.HandleFlusher(&FileHandle{})
  43. var _ = fs.HandleWriter(&FileHandle{})
  44. var _ = fs.HandleReleaser(&FileHandle{})
  45. func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
  46. glog.V(4).Infof("%s read fh %d: [%d,%d) size %d resp.Data len=%d cap=%d", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(req.Size), req.Size, len(resp.Data), cap(resp.Data))
  47. buff := resp.Data[:cap(resp.Data)]
  48. if req.Size > cap(resp.Data) {
  49. // should not happen
  50. buff = make([]byte, req.Size)
  51. }
  52. totalRead, err := fh.readFromChunks(buff, req.Offset)
  53. if err == nil {
  54. maxStop := fh.readFromDirtyPages(buff, req.Offset)
  55. totalRead = max(maxStop - req.Offset, totalRead)
  56. }
  57. if err != nil {
  58. glog.Errorf("file handle read %s: %v", fh.f.fullpath(), err)
  59. return fuse.EIO
  60. }
  61. totalRead = min(int64(len(buff)), totalRead)
  62. resp.Data = buff[:totalRead]
  63. return err
  64. }
  65. func (fh *FileHandle) readFromDirtyPages(buff []byte, startOffset int64) (maxStop int64) {
  66. return fh.dirtyPages.ReadDirtyData(buff, startOffset)
  67. }
  68. func (fh *FileHandle) readFromChunks(buff []byte, offset int64) (int64, error) {
  69. fileSize := int64(filer2.FileSize(fh.f.entry))
  70. if fileSize == 0 {
  71. glog.V(1).Infof("empty fh %v", fh.f.fullpath())
  72. return 0, io.EOF
  73. }
  74. var chunkResolveErr error
  75. if fh.f.entryViewCache == nil {
  76. fh.f.entryViewCache, chunkResolveErr = filer2.NonOverlappingVisibleIntervals(filer2.LookupFn(fh.f.wfs), fh.f.entry.Chunks)
  77. if chunkResolveErr != nil {
  78. return 0, fmt.Errorf("fail to resolve chunk manifest: %v", chunkResolveErr)
  79. }
  80. fh.f.reader = nil
  81. }
  82. if fh.f.reader == nil {
  83. chunkViews := filer2.ViewFromVisibleIntervals(fh.f.entryViewCache, 0, math.MaxInt32)
  84. fh.f.reader = filer2.NewChunkReaderAtFromClient(fh.f.wfs, chunkViews, fh.f.wfs.chunkCache, fileSize)
  85. }
  86. totalRead, err := fh.f.reader.ReadAt(buff, offset)
  87. if err == io.EOF {
  88. err = nil
  89. }
  90. if err != nil {
  91. glog.Errorf("file handle read %s: %v", fh.f.fullpath(), err)
  92. }
  93. // glog.V(0).Infof("file handle read %s [%d,%d] %d : %v", fh.f.fullpath(), offset, offset+int64(totalRead), totalRead, err)
  94. return int64(totalRead), err
  95. }
  96. // Write to the file handle
  97. func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  98. // write the request to volume servers
  99. data := make([]byte, len(req.Data))
  100. copy(data, req.Data)
  101. fh.f.entry.Attributes.FileSize = uint64(max(req.Offset+int64(len(data)), int64(fh.f.entry.Attributes.FileSize)))
  102. glog.V(5).Infof("%v write [%d,%d)", fh.f.fullpath(), req.Offset, req.Offset+int64(len(req.Data)))
  103. chunks, err := fh.dirtyPages.AddPage(req.Offset, data)
  104. if err != nil {
  105. glog.Errorf("%v write fh %d: [%d,%d): %v", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(len(data)), err)
  106. return fuse.EIO
  107. }
  108. resp.Size = len(data)
  109. if req.Offset == 0 {
  110. // detect mime type
  111. fh.contentType = http.DetectContentType(data)
  112. fh.f.dirtyMetadata = true
  113. }
  114. if len(chunks) > 0 {
  115. fh.f.addChunks(chunks)
  116. fh.f.dirtyMetadata = true
  117. }
  118. return nil
  119. }
  120. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  121. glog.V(4).Infof("Release %v fh %d", fh.f.fullpath(), fh.handle)
  122. fh.f.isOpen--
  123. if fh.f.isOpen <= 0 {
  124. fh.dirtyPages.releaseResource()
  125. fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
  126. }
  127. fh.f.entryViewCache = nil
  128. fh.f.reader = nil
  129. return nil
  130. }
  131. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  132. // fflush works at fh level
  133. // send the data to the OS
  134. glog.V(5).Infof("Flush %s fh %d %v", fh.f.fullpath(), fh.handle, req)
  135. chunks, err := fh.dirtyPages.FlushToStorage()
  136. if err != nil {
  137. glog.Errorf("flush %s: %v", fh.f.fullpath(), err)
  138. return fuse.EIO
  139. }
  140. if len(chunks) > 0 {
  141. fh.f.addChunks(chunks)
  142. fh.f.dirtyMetadata = true
  143. }
  144. if !fh.f.dirtyMetadata {
  145. return nil
  146. }
  147. err = fh.f.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  148. if fh.f.entry.Attributes != nil {
  149. fh.f.entry.Attributes.Mime = fh.contentType
  150. if fh.f.entry.Attributes.Uid == 0 {
  151. fh.f.entry.Attributes.Uid = req.Uid
  152. }
  153. if fh.f.entry.Attributes.Gid == 0 {
  154. fh.f.entry.Attributes.Gid = req.Gid
  155. }
  156. if fh.f.entry.Attributes.Crtime == 0 {
  157. fh.f.entry.Attributes.Crtime = time.Now().Unix()
  158. }
  159. fh.f.entry.Attributes.Mtime = time.Now().Unix()
  160. fh.f.entry.Attributes.FileMode = uint32(os.FileMode(fh.f.entry.Attributes.FileMode) &^ fh.f.wfs.option.Umask)
  161. fh.f.entry.Attributes.Collection = fh.dirtyPages.collection
  162. fh.f.entry.Attributes.Replication = fh.dirtyPages.replication
  163. }
  164. request := &filer_pb.CreateEntryRequest{
  165. Directory: fh.f.dir.FullPath(),
  166. Entry: fh.f.entry,
  167. }
  168. glog.V(4).Infof("%s set chunks: %v", fh.f.fullpath(), len(fh.f.entry.Chunks))
  169. for i, chunk := range fh.f.entry.Chunks {
  170. glog.V(4).Infof("%s chunks %d: %v [%d,%d)", fh.f.fullpath(), i, chunk.GetFileIdString(), chunk.Offset, chunk.Offset+int64(chunk.Size))
  171. }
  172. chunks, garbages := filer2.CompactFileChunks(filer2.LookupFn(fh.f.wfs), fh.f.entry.Chunks)
  173. chunks, manifestErr := filer2.MaybeManifestize(fh.f.wfs.saveDataAsChunk(fh.f.dir.FullPath()), chunks)
  174. if manifestErr != nil {
  175. // not good, but should be ok
  176. glog.V(0).Infof("MaybeManifestize: %v", manifestErr)
  177. }
  178. fh.f.entry.Chunks = chunks
  179. // fh.f.entryViewCache = nil
  180. // special handling of one chunk md5
  181. if len(chunks) == 1 {
  182. }
  183. if err := filer_pb.CreateEntry(client, request); err != nil {
  184. glog.Errorf("fh flush create %s: %v", fh.f.fullpath(), err)
  185. return fmt.Errorf("fh flush create %s: %v", fh.f.fullpath(), err)
  186. }
  187. fh.f.wfs.metaCache.InsertEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  188. fh.f.wfs.deleteFileChunks(garbages)
  189. for i, chunk := range garbages {
  190. glog.V(4).Infof("garbage %s chunks %d: %v [%d,%d)", fh.f.fullpath(), i, chunk.GetFileIdString(), chunk.Offset, chunk.Offset+int64(chunk.Size))
  191. }
  192. return nil
  193. })
  194. if err == nil {
  195. fh.f.dirtyMetadata = false
  196. }
  197. if err != nil {
  198. glog.Errorf("%v fh %d flush: %v", fh.f.fullpath(), fh.handle, err)
  199. return fuse.EIO
  200. }
  201. return nil
  202. }