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.

281 lines
6.7 KiB

7 years ago
7 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/seaweedfs/fuse"
  12. "github.com/seaweedfs/fuse/fs"
  13. )
  14. const blockSize = 512
  15. var _ = fs.Node(&File{})
  16. var _ = fs.NodeOpener(&File{})
  17. var _ = fs.NodeFsyncer(&File{})
  18. var _ = fs.NodeSetattrer(&File{})
  19. var _ = fs.NodeGetxattrer(&File{})
  20. var _ = fs.NodeSetxattrer(&File{})
  21. var _ = fs.NodeRemovexattrer(&File{})
  22. var _ = fs.NodeListxattrer(&File{})
  23. var _ = fs.NodeForgetter(&File{})
  24. type File struct {
  25. Name string
  26. dir *Dir
  27. wfs *WFS
  28. entry *filer_pb.Entry
  29. entryViewCache []filer2.VisibleInterval
  30. isOpen int
  31. reader io.ReadSeeker
  32. }
  33. func (file *File) fullpath() filer2.FullPath {
  34. return filer2.NewFullPath(file.dir.Path, file.Name)
  35. }
  36. func (file *File) Attr(ctx context.Context, attr *fuse.Attr) error {
  37. glog.V(4).Infof("file Attr %s, open:%v, existing attr: %+v", file.fullpath(), file.isOpen, attr)
  38. if file.isOpen <= 0 {
  39. if err := file.maybeLoadEntry(ctx); err != nil {
  40. return err
  41. }
  42. }
  43. attr.Inode = file.fullpath().AsInode()
  44. attr.Valid = time.Second
  45. attr.Mode = os.FileMode(file.entry.Attributes.FileMode)
  46. attr.Size = filer2.TotalSize(file.entry.Chunks)
  47. if file.isOpen > 0 {
  48. attr.Size = file.entry.Attributes.FileSize
  49. glog.V(4).Infof("file Attr %s, open:%v, size: %d", file.fullpath(), file.isOpen, attr.Size)
  50. }
  51. attr.Crtime = time.Unix(file.entry.Attributes.Crtime, 0)
  52. attr.Mtime = time.Unix(file.entry.Attributes.Mtime, 0)
  53. attr.Gid = file.entry.Attributes.Gid
  54. attr.Uid = file.entry.Attributes.Uid
  55. attr.Blocks = attr.Size/blockSize + 1
  56. attr.BlockSize = uint32(file.wfs.option.ChunkSizeLimit)
  57. return nil
  58. }
  59. func (file *File) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  60. glog.V(4).Infof("file Getxattr %s", file.fullpath())
  61. if err := file.maybeLoadEntry(ctx); err != nil {
  62. return err
  63. }
  64. return getxattr(file.entry, req, resp)
  65. }
  66. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  67. glog.V(4).Infof("file %v open %+v", file.fullpath(), req)
  68. file.isOpen++
  69. handle := file.wfs.AcquireHandle(file, req.Uid, req.Gid)
  70. resp.Handle = fuse.HandleID(handle.handle)
  71. glog.V(3).Infof("%v file open handle id = %d", file.fullpath(), handle.handle)
  72. return handle, nil
  73. }
  74. func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  75. glog.V(3).Infof("%v file setattr %+v, old:%+v", file.fullpath(), req, file.entry.Attributes)
  76. if err := file.maybeLoadEntry(ctx); err != nil {
  77. return err
  78. }
  79. if req.Valid.Size() {
  80. glog.V(3).Infof("%v file setattr set size=%v", file.fullpath(), req.Size)
  81. if req.Size < filer2.TotalSize(file.entry.Chunks) {
  82. // fmt.Printf("truncate %v \n", fullPath)
  83. var chunks []*filer_pb.FileChunk
  84. for _, chunk := range file.entry.Chunks {
  85. int64Size := int64(chunk.Size)
  86. if chunk.Offset+int64Size > int64(req.Size) {
  87. int64Size = int64(req.Size) - chunk.Offset
  88. }
  89. if int64Size > 0 {
  90. chunks = append(chunks, chunk)
  91. }
  92. }
  93. file.entry.Chunks = chunks
  94. file.entryViewCache = nil
  95. file.reader = nil
  96. }
  97. file.entry.Attributes.FileSize = req.Size
  98. }
  99. if req.Valid.Mode() {
  100. file.entry.Attributes.FileMode = uint32(req.Mode)
  101. }
  102. if req.Valid.Uid() {
  103. file.entry.Attributes.Uid = req.Uid
  104. }
  105. if req.Valid.Gid() {
  106. file.entry.Attributes.Gid = req.Gid
  107. }
  108. if req.Valid.Crtime() {
  109. file.entry.Attributes.Crtime = req.Crtime.Unix()
  110. }
  111. if req.Valid.Mtime() {
  112. file.entry.Attributes.Mtime = req.Mtime.Unix()
  113. }
  114. if file.isOpen > 0 {
  115. return nil
  116. }
  117. file.wfs.cacheDelete(file.fullpath())
  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. file.wfs.cacheDelete(file.fullpath())
  129. return file.saveEntry()
  130. }
  131. func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  132. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  133. if err := file.maybeLoadEntry(ctx); err != nil {
  134. return err
  135. }
  136. if err := removexattr(file.entry, req); err != nil {
  137. return err
  138. }
  139. file.wfs.cacheDelete(file.fullpath())
  140. return file.saveEntry()
  141. }
  142. func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  143. glog.V(4).Infof("file Listxattr %s", file.fullpath())
  144. if err := file.maybeLoadEntry(ctx); err != nil {
  145. return err
  146. }
  147. if err := listxattr(file.entry, req, resp); err != nil {
  148. return err
  149. }
  150. return nil
  151. }
  152. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  153. // fsync works at OS level
  154. // write the file chunks to the filerGrpcAddress
  155. glog.V(3).Infof("%s/%s fsync file %+v", file.dir.Path, file.Name, req)
  156. return nil
  157. }
  158. func (file *File) Forget() {
  159. glog.V(3).Infof("Forget file %s/%s", file.dir.Path, file.Name)
  160. file.wfs.forgetNode(filer2.NewFullPath(file.dir.Path, file.Name))
  161. }
  162. func (file *File) maybeLoadEntry(ctx context.Context) error {
  163. if file.entry == nil || file.isOpen <= 0 {
  164. entry, err := file.wfs.maybeLoadEntry(file.dir.Path, file.Name)
  165. if err != nil {
  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(3).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(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.Path,
  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.Path, file.Name, err)
  204. return fuse.EIO
  205. }
  206. return nil
  207. })
  208. }