file.go 7.5 KB

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