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.

234 lines
6.3 KiB

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