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.

275 lines
6.5 KiB

7 years ago
7 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
  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 int
  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 <= 0 {
  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 > 0 {
  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++
  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. int64Size := int64(chunk.Size)
  83. if chunk.Offset+int64Size > int64(req.Size) {
  84. int64Size = int64(req.Size) - chunk.Offset
  85. }
  86. if int64Size > 0 {
  87. chunks = append(chunks, chunk)
  88. }
  89. }
  90. file.entry.Chunks = chunks
  91. file.entryViewCache = nil
  92. }
  93. file.entry.Attributes.FileSize = req.Size
  94. }
  95. if req.Valid.Mode() {
  96. file.entry.Attributes.FileMode = uint32(req.Mode)
  97. }
  98. if req.Valid.Uid() {
  99. file.entry.Attributes.Uid = req.Uid
  100. }
  101. if req.Valid.Gid() {
  102. file.entry.Attributes.Gid = req.Gid
  103. }
  104. if req.Valid.Crtime() {
  105. file.entry.Attributes.Crtime = req.Crtime.Unix()
  106. }
  107. if req.Valid.Mtime() {
  108. file.entry.Attributes.Mtime = req.Mtime.Unix()
  109. }
  110. if file.isOpen > 0 {
  111. return nil
  112. }
  113. file.wfs.cacheDelete(file.fullpath())
  114. return file.saveEntry(ctx)
  115. }
  116. func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  117. glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name)
  118. if err := file.maybeLoadEntry(ctx); err != nil {
  119. return err
  120. }
  121. if err := setxattr(file.entry, req); err != nil {
  122. return err
  123. }
  124. file.wfs.cacheDelete(file.fullpath())
  125. return file.saveEntry(ctx)
  126. }
  127. func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  128. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  129. if err := file.maybeLoadEntry(ctx); err != nil {
  130. return err
  131. }
  132. if err := removexattr(file.entry, req); err != nil {
  133. return err
  134. }
  135. file.wfs.cacheDelete(file.fullpath())
  136. return file.saveEntry(ctx)
  137. }
  138. func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  139. glog.V(4).Infof("file Listxattr %s", file.fullpath())
  140. if err := file.maybeLoadEntry(ctx); err != nil {
  141. return err
  142. }
  143. if err := listxattr(file.entry, req, resp); err != nil {
  144. return err
  145. }
  146. return nil
  147. }
  148. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  149. // fsync works at OS level
  150. // write the file chunks to the filerGrpcAddress
  151. glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
  152. return nil
  153. }
  154. func (file *File) Forget() {
  155. glog.V(3).Infof("Forget file %s/%s", file.dir.Path, file.Name)
  156. file.wfs.forgetNode(filer2.NewFullPath(file.dir.Path, file.Name))
  157. }
  158. func (file *File) maybeLoadEntry(ctx context.Context) error {
  159. if file.entry == nil || file.isOpen <= 0 {
  160. entry, err := file.wfs.maybeLoadEntry(ctx, file.dir.Path, file.Name)
  161. if err != nil {
  162. return err
  163. }
  164. if entry != nil {
  165. file.setEntry(entry)
  166. }
  167. }
  168. return nil
  169. }
  170. func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
  171. sort.Slice(chunks, func(i, j int) bool {
  172. return chunks[i].Mtime < chunks[j].Mtime
  173. })
  174. var newVisibles []filer2.VisibleInterval
  175. for _, chunk := range chunks {
  176. newVisibles = filer2.MergeIntoVisibles(file.entryViewCache, newVisibles, chunk)
  177. t := file.entryViewCache[:0]
  178. file.entryViewCache = newVisibles
  179. newVisibles = t
  180. }
  181. glog.V(3).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(file.entry.Chunks), len(chunks))
  182. file.entry.Chunks = append(file.entry.Chunks, chunks...)
  183. }
  184. func (file *File) setEntry(entry *filer_pb.Entry) {
  185. file.entry = entry
  186. file.entryViewCache = filer2.NonOverlappingVisibleIntervals(file.entry.Chunks)
  187. }
  188. func (file *File) saveEntry(ctx context.Context) error {
  189. return file.wfs.WithFilerClient(ctx, func(client filer_pb.SeaweedFilerClient) error {
  190. request := &filer_pb.UpdateEntryRequest{
  191. Directory: file.dir.Path,
  192. Entry: file.entry,
  193. }
  194. glog.V(1).Infof("save file entry: %v", request)
  195. _, err := client.UpdateEntry(ctx, request)
  196. if err != nil {
  197. glog.V(0).Infof("UpdateEntry file %s/%s: %v", file.dir.Path, file.Name, err)
  198. return fuse.EIO
  199. }
  200. return nil
  201. })
  202. }