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.

280 lines
6.6 KiB

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