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.

287 lines
7.0 KiB

7 years ago
7 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
7 years ago
5 years ago
5 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
6 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "io"
  5. "os"
  6. "sort"
  7. "time"
  8. "github.com/chrislusf/seaweedfs/weed/filer2"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  11. "github.com/chrislusf/seaweedfs/weed/util"
  12. "github.com/seaweedfs/fuse"
  13. "github.com/seaweedfs/fuse/fs"
  14. )
  15. const blockSize = 512
  16. var _ = fs.Node(&File{})
  17. var _ = fs.NodeOpener(&File{})
  18. var _ = fs.NodeFsyncer(&File{})
  19. var _ = fs.NodeSetattrer(&File{})
  20. var _ = fs.NodeGetxattrer(&File{})
  21. var _ = fs.NodeSetxattrer(&File{})
  22. var _ = fs.NodeRemovexattrer(&File{})
  23. var _ = fs.NodeListxattrer(&File{})
  24. var _ = fs.NodeForgetter(&File{})
  25. type File struct {
  26. Name string
  27. dir *Dir
  28. wfs *WFS
  29. entry *filer_pb.Entry
  30. entryViewCache []filer2.VisibleInterval
  31. isOpen int
  32. reader io.ReaderAt
  33. }
  34. func (file *File) fullpath() util.FullPath {
  35. return util.NewFullPath(file.dir.FullPath(), file.Name)
  36. }
  37. func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error {
  38. glog.V(4).Infof("file Attr %s, open:%v, existing attr: %+v", file.fullpath(), file.isOpen, attr)
  39. if file.isOpen <= 0 {
  40. if err := file.maybeLoadEntry(ctx); err != nil {
  41. return err
  42. }
  43. }
  44. attr.Inode = file.fullpath().AsInode()
  45. attr.Valid = time.Second
  46. attr.Mode = os.FileMode(file.entry.Attributes.FileMode)
  47. attr.Size = filer2.TotalSize(file.entry.Chunks)
  48. if file.isOpen > 0 {
  49. attr.Size = file.entry.Attributes.FileSize
  50. glog.V(4).Infof("file Attr %s, open:%v, size: %d", file.fullpath(), file.isOpen, attr.Size)
  51. }
  52. attr.Crtime = time.Unix(file.entry.Attributes.Crtime, 0)
  53. attr.Mtime = time.Unix(file.entry.Attributes.Mtime, 0)
  54. attr.Gid = file.entry.Attributes.Gid
  55. attr.Uid = file.entry.Attributes.Uid
  56. attr.Blocks = attr.Size/blockSize + 1
  57. attr.BlockSize = uint32(file.wfs.option.ChunkSizeLimit)
  58. return nil
  59. }
  60. func (file *File) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  61. glog.V(4).Infof("file Getxattr %s", file.fullpath())
  62. if err := file.maybeLoadEntry(ctx); err != nil {
  63. return err
  64. }
  65. return getxattr(file.entry, req, resp)
  66. }
  67. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  68. glog.V(4).Infof("file %v open %+v", file.fullpath(), req)
  69. file.isOpen++
  70. handle := file.wfs.AcquireHandle(file, req.Uid, req.Gid)
  71. resp.Handle = fuse.HandleID(handle.handle)
  72. glog.V(3).Infof("%v file open handle id = %d", file.fullpath(), handle.handle)
  73. return handle, nil
  74. }
  75. func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  76. glog.V(3).Infof("%v file setattr %+v, old:%+v", file.fullpath(), req, file.entry.Attributes)
  77. if err := file.maybeLoadEntry(ctx); err != nil {
  78. return err
  79. }
  80. if req.Valid.Size() {
  81. glog.V(3).Infof("%v file setattr set size=%v", file.fullpath(), req.Size)
  82. if req.Size < filer2.TotalSize(file.entry.Chunks) {
  83. // fmt.Printf("truncate %v \n", fullPath)
  84. var chunks []*filer_pb.FileChunk
  85. for _, chunk := range file.entry.Chunks {
  86. int64Size := int64(chunk.Size)
  87. if chunk.Offset+int64Size > int64(req.Size) {
  88. int64Size = int64(req.Size) - chunk.Offset
  89. }
  90. if int64Size > 0 {
  91. chunks = append(chunks, chunk)
  92. }
  93. }
  94. file.entry.Chunks = chunks
  95. file.entryViewCache = nil
  96. file.reader = nil
  97. }
  98. file.entry.Attributes.FileSize = req.Size
  99. }
  100. if req.Valid.Mode() {
  101. file.entry.Attributes.FileMode = uint32(req.Mode)
  102. }
  103. if req.Valid.Uid() {
  104. file.entry.Attributes.Uid = req.Uid
  105. }
  106. if req.Valid.Gid() {
  107. file.entry.Attributes.Gid = req.Gid
  108. }
  109. if req.Valid.Crtime() {
  110. file.entry.Attributes.Crtime = req.Crtime.Unix()
  111. }
  112. if req.Valid.Mtime() {
  113. file.entry.Attributes.Mtime = req.Mtime.Unix()
  114. }
  115. if file.isOpen > 0 {
  116. return nil
  117. }
  118. file.wfs.cacheDelete(file.fullpath())
  119. return file.saveEntry()
  120. }
  121. func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  122. glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name)
  123. if err := file.maybeLoadEntry(ctx); err != nil {
  124. return err
  125. }
  126. if err := setxattr(file.entry, req); err != nil {
  127. return err
  128. }
  129. file.wfs.cacheDelete(file.fullpath())
  130. return file.saveEntry()
  131. }
  132. func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  133. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  134. if err := file.maybeLoadEntry(ctx); err != nil {
  135. return err
  136. }
  137. if err := removexattr(file.entry, req); err != nil {
  138. return err
  139. }
  140. file.wfs.cacheDelete(file.fullpath())
  141. return file.saveEntry()
  142. }
  143. func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  144. glog.V(4).Infof("file Listxattr %s", file.fullpath())
  145. if err := file.maybeLoadEntry(ctx); err != nil {
  146. return err
  147. }
  148. if err := listxattr(file.entry, req, resp); err != nil {
  149. return err
  150. }
  151. return nil
  152. }
  153. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  154. // fsync works at OS level
  155. // write the file chunks to the filerGrpcAddress
  156. glog.V(3).Infof("%s/%s fsync file %+v", file.dir.FullPath(), file.Name, req)
  157. return nil
  158. }
  159. func (file *File) Forget() {
  160. t := util.NewFullPath(file.dir.FullPath(), file.Name)
  161. glog.V(3).Infof("Forget file %s", t)
  162. file.wfs.fsNodeCache.DeleteFsNode(t)
  163. }
  164. func (file *File) maybeLoadEntry(ctx context.Context) error {
  165. if file.entry == nil || file.isOpen <= 0 {
  166. entry, err := file.wfs.maybeLoadEntry(file.dir.FullPath(), file.Name)
  167. if err != nil {
  168. glog.V(3).Infof("maybeLoadEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  169. return err
  170. }
  171. if entry != nil {
  172. file.setEntry(entry)
  173. }
  174. }
  175. return nil
  176. }
  177. func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
  178. sort.Slice(chunks, func(i, j int) bool {
  179. return chunks[i].Mtime < chunks[j].Mtime
  180. })
  181. var newVisibles []filer2.VisibleInterval
  182. for _, chunk := range chunks {
  183. newVisibles = filer2.MergeIntoVisibles(file.entryViewCache, newVisibles, chunk)
  184. t := file.entryViewCache[:0]
  185. file.entryViewCache = newVisibles
  186. newVisibles = t
  187. }
  188. file.reader = nil
  189. glog.V(3).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(file.entry.Chunks), len(chunks))
  190. file.entry.Chunks = append(file.entry.Chunks, chunks...)
  191. }
  192. func (file *File) setEntry(entry *filer_pb.Entry) {
  193. file.entry = entry
  194. file.entryViewCache = filer2.NonOverlappingVisibleIntervals(file.entry.Chunks)
  195. file.reader = nil
  196. }
  197. func (file *File) saveEntry() error {
  198. return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  199. request := &filer_pb.UpdateEntryRequest{
  200. Directory: file.dir.FullPath(),
  201. Entry: file.entry,
  202. }
  203. glog.V(1).Infof("save file entry: %v", request)
  204. _, err := client.UpdateEntry(context.Background(), request)
  205. if err != nil {
  206. glog.V(0).Infof("UpdateEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  207. return fuse.EIO
  208. }
  209. if file.wfs.option.AsyncMetaDataCaching {
  210. file.wfs.metaCache.UpdateEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  211. }
  212. return nil
  213. })
  214. }