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.

268 lines
7.4 KiB

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