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.

325 lines
8.3 KiB

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