filehandle.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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(2).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. var chunkResolveErr error
  72. if fh.f.entryViewCache == nil {
  73. fh.f.entryViewCache, chunkResolveErr = filer2.NonOverlappingVisibleIntervals(filer2.LookupFn(fh.f.wfs), fh.f.entry.Chunks)
  74. if chunkResolveErr != nil {
  75. return 0, fmt.Errorf("fail to resolve chunk manifest: %v", chunkResolveErr)
  76. }
  77. fh.f.reader = nil
  78. }
  79. if fh.f.reader == nil {
  80. chunkViews := filer2.ViewFromVisibleIntervals(fh.f.entryViewCache, 0, math.MaxInt32)
  81. fh.f.reader = filer2.NewChunkReaderAtFromClient(fh.f.wfs, chunkViews, fh.f.wfs.chunkCache)
  82. }
  83. totalRead, err := fh.f.reader.ReadAt(buff, offset)
  84. if err == io.EOF {
  85. err = nil
  86. }
  87. if err != nil {
  88. glog.Errorf("file handle read %s: %v", fh.f.fullpath(), err)
  89. }
  90. // glog.V(0).Infof("file handle read %s [%d,%d] %d : %v", fh.f.fullpath(), offset, offset+int64(totalRead), totalRead, err)
  91. return int64(totalRead), err
  92. }
  93. // Write to the file handle
  94. func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  95. // write the request to volume servers
  96. data := make([]byte, len(req.Data))
  97. copy(data, req.Data)
  98. fh.f.entry.Attributes.FileSize = uint64(max(req.Offset+int64(len(data)), int64(fh.f.entry.Attributes.FileSize)))
  99. glog.V(2).Infof("%v write [%d,%d)", fh.f.fullpath(), req.Offset, req.Offset+int64(len(req.Data)))
  100. chunks, err := fh.dirtyPages.AddPage(req.Offset, data)
  101. if err != nil {
  102. glog.Errorf("%v write fh %d: [%d,%d): %v", fh.f.fullpath(), fh.handle, req.Offset, req.Offset+int64(len(data)), err)
  103. return fuse.EIO
  104. }
  105. resp.Size = len(data)
  106. if req.Offset == 0 {
  107. // detect mime type
  108. fh.contentType = http.DetectContentType(data)
  109. fh.f.dirtyMetadata = true
  110. }
  111. if len(chunks) > 0 {
  112. fh.f.addChunks(chunks)
  113. fh.f.dirtyMetadata = true
  114. }
  115. return nil
  116. }
  117. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  118. glog.V(4).Infof("%v release fh %d", fh.f.fullpath(), fh.handle)
  119. fh.f.isOpen--
  120. if fh.f.isOpen <= 0 {
  121. fh.dirtyPages.releaseResource()
  122. fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
  123. }
  124. fh.f.entryViewCache = nil
  125. fh.f.reader = nil
  126. return nil
  127. }
  128. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  129. // fflush works at fh level
  130. // send the data to the OS
  131. glog.V(4).Infof("%s fh %d flush %v", fh.f.fullpath(), fh.handle, req)
  132. chunks, err := fh.dirtyPages.FlushToStorage()
  133. if err != nil {
  134. glog.Errorf("flush %s: %v", fh.f.fullpath(), err)
  135. return fuse.EIO
  136. }
  137. if len(chunks) > 0 {
  138. fh.f.addChunks(chunks)
  139. fh.f.dirtyMetadata = true
  140. }
  141. if !fh.f.dirtyMetadata {
  142. return nil
  143. }
  144. err = fh.f.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  145. if fh.f.entry.Attributes != nil {
  146. fh.f.entry.Attributes.Mime = fh.contentType
  147. if fh.f.entry.Attributes.Uid == 0 {
  148. fh.f.entry.Attributes.Uid = req.Uid
  149. }
  150. if fh.f.entry.Attributes.Gid == 0 {
  151. fh.f.entry.Attributes.Gid = req.Gid
  152. }
  153. if fh.f.entry.Attributes.Crtime == 0 {
  154. fh.f.entry.Attributes.Crtime = time.Now().Unix()
  155. }
  156. fh.f.entry.Attributes.Mtime = time.Now().Unix()
  157. fh.f.entry.Attributes.FileMode = uint32(os.FileMode(fh.f.entry.Attributes.FileMode) &^ fh.f.wfs.option.Umask)
  158. fh.f.entry.Attributes.Collection = fh.dirtyPages.collection
  159. fh.f.entry.Attributes.Replication = fh.dirtyPages.replication
  160. }
  161. request := &filer_pb.CreateEntryRequest{
  162. Directory: fh.f.dir.FullPath(),
  163. Entry: fh.f.entry,
  164. }
  165. glog.V(4).Infof("%s set chunks: %v", fh.f.fullpath(), len(fh.f.entry.Chunks))
  166. for i, chunk := range fh.f.entry.Chunks {
  167. glog.V(4).Infof("%s chunks %d: %v [%d,%d)", fh.f.fullpath(), i, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
  168. }
  169. chunks, garbages := filer2.CompactFileChunks(filer2.LookupFn(fh.f.wfs), fh.f.entry.Chunks)
  170. chunks, manifestErr := filer2.MaybeManifestize(fh.f.wfs.saveDataAsChunk(fh.f.dir.FullPath()), chunks)
  171. if manifestErr != nil {
  172. // not good, but should be ok
  173. glog.V(0).Infof("MaybeManifestize: %v", manifestErr)
  174. }
  175. fh.f.entry.Chunks = chunks
  176. // fh.f.entryViewCache = nil
  177. // special handling of one chunk md5
  178. if len(chunks) == 1 {
  179. }
  180. if err := filer_pb.CreateEntry(client, request); err != nil {
  181. glog.Errorf("fh flush create %s: %v", fh.f.fullpath(), err)
  182. return fmt.Errorf("fh flush create %s: %v", fh.f.fullpath(), err)
  183. }
  184. fh.f.wfs.metaCache.InsertEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  185. fh.f.wfs.deleteFileChunks(garbages)
  186. for i, chunk := range garbages {
  187. glog.V(4).Infof("garbage %s chunks %d: %v [%d,%d)", fh.f.fullpath(), i, chunk.FileId, chunk.Offset, chunk.Offset+int64(chunk.Size))
  188. }
  189. return nil
  190. })
  191. if err == nil {
  192. fh.f.dirtyMetadata = false
  193. }
  194. if err != nil {
  195. glog.Errorf("%v fh %d flush: %v", fh.f.fullpath(), fh.handle, err)
  196. return fuse.EIO
  197. }
  198. return nil
  199. }