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.

279 lines
6.8 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
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(4).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(4).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(4).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. return file.saveEntry()
  119. }
  120. func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  121. glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name)
  122. if err := file.maybeLoadEntry(ctx); err != nil {
  123. return err
  124. }
  125. if err := setxattr(file.entry, req); err != nil {
  126. return err
  127. }
  128. return file.saveEntry()
  129. }
  130. func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  131. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  132. if err := file.maybeLoadEntry(ctx); err != nil {
  133. return err
  134. }
  135. if err := removexattr(file.entry, req); err != nil {
  136. return err
  137. }
  138. return file.saveEntry()
  139. }
  140. func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  141. glog.V(4).Infof("file Listxattr %s", file.fullpath())
  142. if err := file.maybeLoadEntry(ctx); err != nil {
  143. return err
  144. }
  145. if err := listxattr(file.entry, req, resp); err != nil {
  146. return err
  147. }
  148. return nil
  149. }
  150. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  151. // fsync works at OS level
  152. // write the file chunks to the filerGrpcAddress
  153. glog.V(4).Infof("%s/%s fsync file %+v", file.dir.FullPath(), file.Name, req)
  154. return nil
  155. }
  156. func (file *File) Forget() {
  157. t := util.NewFullPath(file.dir.FullPath(), file.Name)
  158. glog.V(4).Infof("Forget file %s", t)
  159. file.wfs.fsNodeCache.DeleteFsNode(t)
  160. }
  161. func (file *File) maybeLoadEntry(ctx context.Context) error {
  162. if file.entry == nil || file.isOpen <= 0 {
  163. entry, err := file.wfs.maybeLoadEntry(file.dir.FullPath(), file.Name)
  164. if err != nil {
  165. glog.V(3).Infof("maybeLoadEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  166. return err
  167. }
  168. if entry != nil {
  169. file.setEntry(entry)
  170. }
  171. }
  172. return nil
  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. file.reader = nil
  186. glog.V(4).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(file.entry.Chunks), len(chunks))
  187. file.entry.Chunks = append(file.entry.Chunks, chunks...)
  188. }
  189. func (file *File) setEntry(entry *filer_pb.Entry) {
  190. file.entry = entry
  191. file.entryViewCache, _ = filer2.NonOverlappingVisibleIntervals(filer2.LookupFn(file.wfs), file.entry.Chunks)
  192. file.reader = nil
  193. }
  194. func (file *File) saveEntry() error {
  195. return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  196. request := &filer_pb.UpdateEntryRequest{
  197. Directory: file.dir.FullPath(),
  198. Entry: file.entry,
  199. }
  200. glog.V(1).Infof("save file entry: %v", request)
  201. _, err := client.UpdateEntry(context.Background(), request)
  202. if err != nil {
  203. glog.V(0).Infof("UpdateEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  204. return fuse.EIO
  205. }
  206. file.wfs.metaCache.UpdateEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  207. return nil
  208. })
  209. }