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.

340 lines
8.3 KiB

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