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.

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