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.

315 lines
7.9 KiB

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