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.

314 lines
7.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
4 years ago
4 years ago
4 years ago
7 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
6 years ago
4 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
4 years ago
4 years ago
  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(5).Infof("%v file setattr %+v", file.fullpath(), req)
  78. if err := file.maybeLoadEntry(ctx); err != nil {
  79. return err
  80. }
  81. if file.isOpen > 0 {
  82. file.wfs.handlesLock.Lock()
  83. fileHandle := file.wfs.handles[file.fullpath().AsInode()]
  84. file.wfs.handlesLock.Unlock()
  85. if fileHandle != nil {
  86. fileHandle.Lock()
  87. defer fileHandle.Unlock()
  88. }
  89. }
  90. if req.Valid.Size() {
  91. glog.V(4).Infof("%v file setattr set size=%v chunks=%d", file.fullpath(), req.Size, len(file.entry.Chunks))
  92. if req.Size < filer2.TotalSize(file.entry.Chunks) {
  93. // fmt.Printf("truncate %v \n", fullPath)
  94. var chunks []*filer_pb.FileChunk
  95. var truncatedChunks []*filer_pb.FileChunk
  96. for _, chunk := range file.entry.Chunks {
  97. int64Size := int64(chunk.Size)
  98. if chunk.Offset+int64Size > int64(req.Size) {
  99. // this chunk is truncated
  100. int64Size = int64(req.Size) - chunk.Offset
  101. if int64Size > 0 {
  102. chunks = append(chunks, chunk)
  103. glog.V(4).Infof("truncated chunk %+v from %d to %d\n", chunk.GetFileIdString(), chunk.Size, int64Size)
  104. chunk.Size = uint64(int64Size)
  105. } else {
  106. glog.V(4).Infof("truncated whole chunk %+v\n", chunk.GetFileIdString())
  107. truncatedChunks = append(truncatedChunks, chunk)
  108. }
  109. }
  110. }
  111. file.wfs.deleteFileChunks(truncatedChunks)
  112. file.entry.Chunks = chunks
  113. file.entryViewCache = nil
  114. file.reader = nil
  115. }
  116. file.entry.Attributes.FileSize = req.Size
  117. file.dirtyMetadata = true
  118. }
  119. if req.Valid.Mode() {
  120. file.entry.Attributes.FileMode = uint32(req.Mode)
  121. file.dirtyMetadata = true
  122. }
  123. if req.Valid.Uid() {
  124. file.entry.Attributes.Uid = req.Uid
  125. file.dirtyMetadata = true
  126. }
  127. if req.Valid.Gid() {
  128. file.entry.Attributes.Gid = req.Gid
  129. file.dirtyMetadata = true
  130. }
  131. if req.Valid.Crtime() {
  132. file.entry.Attributes.Crtime = req.Crtime.Unix()
  133. file.dirtyMetadata = true
  134. }
  135. if req.Valid.Mtime() {
  136. file.entry.Attributes.Mtime = req.Mtime.Unix()
  137. file.dirtyMetadata = true
  138. }
  139. if req.Valid.Handle() {
  140. // fmt.Printf("file handle => %d\n", req.Handle)
  141. }
  142. if file.isOpen > 0 {
  143. return nil
  144. }
  145. if !file.dirtyMetadata {
  146. return nil
  147. }
  148. return file.saveEntry()
  149. }
  150. func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  151. glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name)
  152. if err := file.maybeLoadEntry(ctx); err != nil {
  153. return err
  154. }
  155. if err := setxattr(file.entry, req); err != nil {
  156. return err
  157. }
  158. return file.saveEntry()
  159. }
  160. func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  161. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  162. if err := file.maybeLoadEntry(ctx); err != nil {
  163. return err
  164. }
  165. if err := removexattr(file.entry, req); err != nil {
  166. return err
  167. }
  168. return file.saveEntry()
  169. }
  170. func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  171. glog.V(4).Infof("file Listxattr %s", file.fullpath())
  172. if err := file.maybeLoadEntry(ctx); err != nil {
  173. return err
  174. }
  175. if err := listxattr(file.entry, req, resp); err != nil {
  176. return err
  177. }
  178. return nil
  179. }
  180. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  181. // fsync works at OS level
  182. // write the file chunks to the filerGrpcAddress
  183. glog.V(4).Infof("%s/%s fsync file %+v", file.dir.FullPath(), file.Name, req)
  184. return nil
  185. }
  186. func (file *File) Forget() {
  187. t := util.NewFullPath(file.dir.FullPath(), file.Name)
  188. glog.V(5).Infof("Forget file %s", t)
  189. file.wfs.fsNodeCache.DeleteFsNode(t)
  190. }
  191. func (file *File) maybeLoadEntry(ctx context.Context) error {
  192. if file.entry == nil || file.isOpen <= 0 {
  193. entry, err := file.wfs.maybeLoadEntry(file.dir.FullPath(), file.Name)
  194. if err != nil {
  195. glog.V(3).Infof("maybeLoadEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  196. return err
  197. }
  198. if entry != nil {
  199. file.setEntry(entry)
  200. }
  201. }
  202. return nil
  203. }
  204. func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
  205. sort.Slice(chunks, func(i, j int) bool {
  206. return chunks[i].Fid.FileKey < chunks[j].Fid.FileKey
  207. })
  208. var newVisibles []filer2.VisibleInterval
  209. for _, chunk := range chunks {
  210. newVisibles = filer2.MergeIntoVisibles(file.entryViewCache, newVisibles, chunk)
  211. t := file.entryViewCache[:0]
  212. file.entryViewCache = newVisibles
  213. newVisibles = t
  214. }
  215. file.reader = nil
  216. glog.V(4).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(file.entry.Chunks), len(chunks))
  217. file.entry.Chunks = append(file.entry.Chunks, chunks...)
  218. }
  219. func (file *File) setEntry(entry *filer_pb.Entry) {
  220. file.entry = entry
  221. file.entryViewCache, _ = filer2.NonOverlappingVisibleIntervals(filer2.LookupFn(file.wfs), file.entry.Chunks)
  222. file.reader = nil
  223. }
  224. func (file *File) saveEntry() error {
  225. return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  226. request := &filer_pb.UpdateEntryRequest{
  227. Directory: file.dir.FullPath(),
  228. Entry: file.entry,
  229. }
  230. glog.V(4).Infof("save file entry: %v", request)
  231. _, err := client.UpdateEntry(context.Background(), request)
  232. if err != nil {
  233. glog.Errorf("UpdateEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  234. return fuse.EIO
  235. }
  236. file.wfs.metaCache.UpdateEntry(context.Background(), filer2.FromPbEntry(request.Directory, request.Entry))
  237. return nil
  238. })
  239. }