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.

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