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.

213 lines
5.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
6 years ago
6 years ago
5 years ago
6 years ago
6 years ago
7 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "mime"
  6. "path"
  7. "time"
  8. "github.com/gabriel-vasile/mimetype"
  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. return &FileHandle{
  29. f: file,
  30. dirtyPages: newDirtyPages(file),
  31. Uid: uid,
  32. Gid: gid,
  33. }
  34. }
  35. var _ = fs.Handle(&FileHandle{})
  36. // var _ = fs.HandleReadAller(&FileHandle{})
  37. var _ = fs.HandleReader(&FileHandle{})
  38. var _ = fs.HandleFlusher(&FileHandle{})
  39. var _ = fs.HandleWriter(&FileHandle{})
  40. var _ = fs.HandleReleaser(&FileHandle{})
  41. func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
  42. glog.V(4).Infof("%s read fh %d: [%d,%d)", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(req.Size))
  43. buff := make([]byte, req.Size)
  44. totalRead, err := fh.readFromChunks(ctx, buff, req.Offset)
  45. if err == nil {
  46. dirtyOffset, dirtySize := fh.readFromDirtyPages(ctx, buff, req.Offset)
  47. if totalRead+req.Offset < dirtyOffset+int64(dirtySize) {
  48. totalRead = dirtyOffset + int64(dirtySize) - req.Offset
  49. }
  50. }
  51. resp.Data = buff[:totalRead]
  52. if err != nil {
  53. glog.Errorf("file handle read %s: %v", fh.f.fullpath(), err)
  54. }
  55. return err
  56. }
  57. func (fh *FileHandle) readFromDirtyPages(ctx context.Context, buff []byte, startOffset int64) (offset int64, size int) {
  58. return fh.dirtyPages.ReadDirtyData(ctx, buff, startOffset)
  59. }
  60. func (fh *FileHandle) readFromChunks(ctx context.Context, buff []byte, offset int64) (int64, error) {
  61. // this value should come from the filer instead of the old f
  62. if len(fh.f.entry.Chunks) == 0 {
  63. glog.V(1).Infof("empty fh %v/%v", fh.f.dir.Path, fh.f.Name)
  64. return 0, nil
  65. }
  66. if fh.f.entryViewCache == nil {
  67. fh.f.entryViewCache = filer2.NonOverlappingVisibleIntervals(fh.f.entry.Chunks)
  68. }
  69. chunkViews := filer2.ViewFromVisibleIntervals(fh.f.entryViewCache, offset, len(buff))
  70. totalRead, err := filer2.ReadIntoBuffer(ctx, fh.f.wfs, fh.f.fullpath(), buff, chunkViews, offset)
  71. if err != nil {
  72. glog.Errorf("file handle read %s: %v", fh.f.fullpath(), err)
  73. }
  74. return totalRead, err
  75. }
  76. // Write to the file handle
  77. func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  78. // write the request to volume servers
  79. fh.f.entry.Attributes.FileSize = uint64(max(req.Offset+int64(len(req.Data)), int64(fh.f.entry.Attributes.FileSize)))
  80. chunks, err := fh.dirtyPages.AddPage(ctx, req.Offset, req.Data)
  81. if err != nil {
  82. glog.Errorf("%+v/%v write fh %d: [%d,%d): %v", fh.f.dir.Path, fh.f.Name, fh.handle, req.Offset, req.Offset+int64(len(req.Data)), err)
  83. return fmt.Errorf("write %s/%s at [%d,%d): %v", fh.f.dir.Path, fh.f.Name, req.Offset, req.Offset+int64(len(req.Data)), err)
  84. }
  85. resp.Size = len(req.Data)
  86. if req.Offset == 0 {
  87. // detect mime type
  88. detectedMIME := mimetype.Detect(req.Data)
  89. fh.contentType = detectedMIME.String()
  90. if ext := path.Ext(fh.f.Name); ext != detectedMIME.Extension() {
  91. fh.contentType = mime.TypeByExtension(ext)
  92. }
  93. fh.dirtyMetadata = true
  94. }
  95. if len(chunks) > 0 {
  96. fh.f.addChunks(chunks)
  97. fh.dirtyMetadata = true
  98. }
  99. return nil
  100. }
  101. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  102. glog.V(4).Infof("%v release fh %d", fh.f.fullpath(), fh.handle)
  103. fh.f.isOpen--
  104. if fh.f.isOpen <= 0 {
  105. fh.dirtyPages.releaseResource()
  106. fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
  107. }
  108. return nil
  109. }
  110. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  111. // fflush works at fh level
  112. // send the data to the OS
  113. glog.V(4).Infof("%s fh %d flush %v", fh.f.fullpath(), fh.handle, req)
  114. chunks, err := fh.dirtyPages.FlushToStorage(ctx)
  115. if err != nil {
  116. glog.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  117. return fmt.Errorf("flush %s/%s: %v", fh.f.dir.Path, fh.f.Name, err)
  118. }
  119. fh.f.addChunks(chunks)
  120. if len(chunks) > 0 {
  121. fh.dirtyMetadata = true
  122. }
  123. if !fh.dirtyMetadata {
  124. return nil
  125. }
  126. err = fh.f.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  127. if fh.f.entry.Attributes != nil {
  128. fh.f.entry.Attributes.Mime = fh.contentType
  129. fh.f.entry.Attributes.Uid = req.Uid
  130. fh.f.entry.Attributes.Gid = req.Gid
  131. fh.f.entry.Attributes.Mtime = time.Now().Unix()
  132. fh.f.entry.Attributes.Crtime = time.Now().Unix()
  133. fh.f.entry.Attributes.FileMode = uint32(0777 &^ fh.f.wfs.option.Umask)
  134. }
  135. request := &filer_pb.CreateEntryRequest{
  136. Directory: fh.f.dir.Path,
  137. Entry: fh.f.entry,
  138. }
  139. glog.V(3).Infof("%s/%s set chunks: %v", fh.f.dir.Path, fh.f.Name, len(fh.f.entry.Chunks))
  140. for i, chunk := range fh.f.entry.Chunks {
  141. glog.V(3).Infof("%s/%s chunks %d: %v [%d,%d)", fh.f.dir.Path, fh.f.Name, i, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
  142. }
  143. chunks, garbages := filer2.CompactFileChunks(fh.f.entry.Chunks)
  144. fh.f.entry.Chunks = chunks
  145. // fh.f.entryViewCache = nil
  146. if _, err := client.CreateEntry(ctx, request); err != nil {
  147. glog.Errorf("update fh: %v", err)
  148. return fmt.Errorf("update fh: %v", err)
  149. }
  150. fh.f.wfs.deleteFileChunks(ctx, garbages)
  151. for i, chunk := range garbages {
  152. glog.V(3).Infof("garbage %s/%s chunks %d: %v [%d,%d)", fh.f.dir.Path, fh.f.Name, i, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
  153. }
  154. return nil
  155. })
  156. if err == nil {
  157. fh.dirtyMetadata = false
  158. }
  159. return err
  160. }