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.

258 lines
7.0 KiB

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