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.

286 lines
7.5 KiB

7 years ago
7 years ago
7 years ago
7 years ago
4 years ago
4 years ago
5 years ago
4 years ago
6 years ago
4 years ago
4 years ago
4 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. "sync"
  10. "time"
  11. "github.com/seaweedfs/fuse"
  12. "github.com/seaweedfs/fuse/fs"
  13. "github.com/chrislusf/seaweedfs/weed/filer"
  14. "github.com/chrislusf/seaweedfs/weed/glog"
  15. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  16. )
  17. type FileHandle struct {
  18. // cache file has been written to
  19. dirtyPages *ContinuousDirtyPages
  20. contentType string
  21. handle uint64
  22. sync.RWMutex
  23. f *File
  24. RequestId fuse.RequestID // unique ID for request
  25. NodeId fuse.NodeID // file or directory the request is about
  26. Uid uint32 // user ID of process making request
  27. Gid uint32 // group ID of process making request
  28. }
  29. func newFileHandle(file *File, uid, gid uint32) *FileHandle {
  30. fh := &FileHandle{
  31. f: file,
  32. dirtyPages: newDirtyPages(file),
  33. Uid: uid,
  34. Gid: gid,
  35. }
  36. if fh.f.entry != nil {
  37. fh.f.entry.Attributes.FileSize = filer.FileSize(fh.f.entry)
  38. }
  39. return fh
  40. }
  41. var _ = fs.Handle(&FileHandle{})
  42. // var _ = fs.HandleReadAller(&FileHandle{})
  43. var _ = fs.HandleReader(&FileHandle{})
  44. var _ = fs.HandleFlusher(&FileHandle{})
  45. var _ = fs.HandleWriter(&FileHandle{})
  46. var _ = fs.HandleReleaser(&FileHandle{})
  47. func (fh *FileHandle) Read(ctx context.Context, req *fuse.ReadRequest, resp *fuse.ReadResponse) error {
  48. 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))
  49. fh.RLock()
  50. defer fh.RUnlock()
  51. if req.Size <= 0 {
  52. return nil
  53. }
  54. buff := resp.Data[:cap(resp.Data)]
  55. if req.Size > cap(resp.Data) {
  56. // should not happen
  57. buff = make([]byte, req.Size)
  58. }
  59. totalRead, err := fh.readFromChunks(buff, req.Offset)
  60. if err == nil {
  61. maxStop := fh.readFromDirtyPages(buff, req.Offset)
  62. totalRead = max(maxStop-req.Offset, totalRead)
  63. }
  64. if err != nil {
  65. glog.Warningf("file handle read %s %d: %v", fh.f.fullpath(), totalRead, err)
  66. return fuse.EIO
  67. }
  68. if totalRead > int64(len(buff)) {
  69. 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)
  70. totalRead = min(int64(len(buff)), totalRead)
  71. }
  72. // resp.Data = buff[:totalRead]
  73. resp.Data = buff
  74. return err
  75. }
  76. func (fh *FileHandle) readFromDirtyPages(buff []byte, startOffset int64) (maxStop int64) {
  77. maxStop = fh.dirtyPages.ReadDirtyDataAt(buff, startOffset)
  78. return
  79. }
  80. func (fh *FileHandle) readFromChunks(buff []byte, offset int64) (int64, error) {
  81. fileSize := int64(filer.FileSize(fh.f.entry))
  82. if fileSize == 0 {
  83. glog.V(1).Infof("empty fh %v", fh.f.fullpath())
  84. return 0, io.EOF
  85. }
  86. var chunkResolveErr error
  87. if fh.f.entryViewCache == nil {
  88. fh.f.entryViewCache, chunkResolveErr = filer.NonOverlappingVisibleIntervals(filer.LookupFn(fh.f.wfs), fh.f.entry.Chunks)
  89. if chunkResolveErr != nil {
  90. return 0, fmt.Errorf("fail to resolve chunk manifest: %v", chunkResolveErr)
  91. }
  92. fh.f.reader = nil
  93. }
  94. if fh.f.reader == nil {
  95. chunkViews := filer.ViewFromVisibleIntervals(fh.f.entryViewCache, 0, math.MaxInt64)
  96. fh.f.reader = filer.NewChunkReaderAtFromClient(fh.f.wfs, chunkViews, fh.f.wfs.chunkCache, fileSize)
  97. }
  98. totalRead, err := fh.f.reader.ReadAt(buff, offset)
  99. if err == io.EOF {
  100. err = nil
  101. }
  102. if err != nil {
  103. glog.Errorf("file handle read %s: %v", fh.f.fullpath(), err)
  104. }
  105. glog.V(4).Infof("file handle read %s [%d,%d] %d : %v", fh.f.fullpath(), offset, offset+int64(totalRead), totalRead, err)
  106. return int64(totalRead), err
  107. }
  108. // Write to the file handle
  109. func (fh *FileHandle) Write(ctx context.Context, req *fuse.WriteRequest, resp *fuse.WriteResponse) error {
  110. fh.Lock()
  111. defer fh.Unlock()
  112. // write the request to volume servers
  113. data := make([]byte, len(req.Data))
  114. copy(data, req.Data)
  115. fh.f.entry.Attributes.FileSize = uint64(max(req.Offset+int64(len(data)), int64(fh.f.entry.Attributes.FileSize)))
  116. glog.V(4).Infof("%v write [%d,%d) %d", fh.f.fullpath(), req.Offset, req.Offset+int64(len(req.Data)), len(req.Data))
  117. fh.dirtyPages.AddPage(req.Offset, data)
  118. resp.Size = len(data)
  119. if req.Offset == 0 {
  120. // detect mime type
  121. fh.contentType = http.DetectContentType(data)
  122. fh.f.dirtyMetadata = true
  123. }
  124. fh.f.dirtyMetadata = true
  125. return nil
  126. }
  127. func (fh *FileHandle) Release(ctx context.Context, req *fuse.ReleaseRequest) error {
  128. glog.V(4).Infof("Release %v fh %d", fh.f.fullpath(), fh.handle)
  129. fh.Lock()
  130. defer fh.Unlock()
  131. fh.f.isOpen--
  132. if fh.f.isOpen < 0 {
  133. glog.V(0).Infof("Release reset %s open count %d => %d", fh.f.Name, fh.f.isOpen, 0)
  134. fh.f.isOpen = 0
  135. return nil
  136. }
  137. if fh.f.isOpen == 0 {
  138. fh.doFlush(ctx, req.Header)
  139. fh.f.wfs.ReleaseHandle(fh.f.fullpath(), fuse.HandleID(fh.handle))
  140. }
  141. return nil
  142. }
  143. func (fh *FileHandle) Flush(ctx context.Context, req *fuse.FlushRequest) error {
  144. fh.Lock()
  145. defer fh.Unlock()
  146. return fh.doFlush(ctx, req.Header)
  147. }
  148. func (fh *FileHandle) doFlush(ctx context.Context, header fuse.Header) error {
  149. // flush works at fh level
  150. // send the data to the OS
  151. glog.V(4).Infof("doFlush %s fh %d", fh.f.fullpath(), fh.handle)
  152. fh.dirtyPages.saveExistingPagesToStorage()
  153. var err error
  154. go func() {
  155. for t := range fh.dirtyPages.chunkSaveErrChan {
  156. if t != nil {
  157. err = t
  158. }
  159. }
  160. }()
  161. fh.dirtyPages.writeWaitGroup.Wait()
  162. if err != nil {
  163. return err
  164. }
  165. if !fh.f.dirtyMetadata {
  166. return nil
  167. }
  168. err = fh.f.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  169. if fh.f.entry.Attributes != nil {
  170. fh.f.entry.Attributes.Mime = fh.contentType
  171. if fh.f.entry.Attributes.Uid == 0 {
  172. fh.f.entry.Attributes.Uid = header.Uid
  173. }
  174. if fh.f.entry.Attributes.Gid == 0 {
  175. fh.f.entry.Attributes.Gid = header.Gid
  176. }
  177. if fh.f.entry.Attributes.Crtime == 0 {
  178. fh.f.entry.Attributes.Crtime = time.Now().Unix()
  179. }
  180. fh.f.entry.Attributes.Mtime = time.Now().Unix()
  181. fh.f.entry.Attributes.FileMode = uint32(os.FileMode(fh.f.entry.Attributes.FileMode) &^ fh.f.wfs.option.Umask)
  182. fh.f.entry.Attributes.Collection = fh.dirtyPages.collection
  183. fh.f.entry.Attributes.Replication = fh.dirtyPages.replication
  184. }
  185. request := &filer_pb.CreateEntryRequest{
  186. Directory: fh.f.dir.FullPath(),
  187. Entry: fh.f.entry,
  188. Signatures: []int32{fh.f.wfs.signature},
  189. }
  190. glog.V(4).Infof("%s set chunks: %v", fh.f.fullpath(), len(fh.f.entry.Chunks))
  191. for i, chunk := range fh.f.entry.Chunks {
  192. glog.V(4).Infof("%s chunks %d: %v [%d,%d)", fh.f.fullpath(), i, chunk.GetFileIdString(), chunk.Offset, chunk.Offset+int64(chunk.Size))
  193. }
  194. manifestChunks, nonManifestChunks := filer.SeparateManifestChunks(fh.f.entry.Chunks)
  195. chunks, _ := filer.CompactFileChunks(filer.LookupFn(fh.f.wfs), nonManifestChunks)
  196. chunks, manifestErr := filer.MaybeManifestize(fh.f.wfs.saveDataAsChunk(fh.f.dir.FullPath()), chunks)
  197. if manifestErr != nil {
  198. // not good, but should be ok
  199. glog.V(0).Infof("MaybeManifestize: %v", manifestErr)
  200. }
  201. fh.f.entry.Chunks = append(chunks, manifestChunks...)
  202. fh.f.entryViewCache = nil
  203. fh.f.wfs.mapPbIdFromLocalToFiler(request.Entry)
  204. defer fh.f.wfs.mapPbIdFromFilerToLocal(request.Entry)
  205. if err := filer_pb.CreateEntry(client, request); err != nil {
  206. glog.Errorf("fh flush create %s: %v", fh.f.fullpath(), err)
  207. return fmt.Errorf("fh flush create %s: %v", fh.f.fullpath(), err)
  208. }
  209. fh.f.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  210. return nil
  211. })
  212. if err == nil {
  213. fh.f.dirtyMetadata = false
  214. }
  215. if err != nil {
  216. glog.Errorf("%v fh %d flush: %v", fh.f.fullpath(), fh.handle, err)
  217. return fuse.EIO
  218. }
  219. return nil
  220. }