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.

262 lines
7.1 KiB

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