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.

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