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.

361 lines
8.8 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
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/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:%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, _ = filer.NonOverlappingVisibleIntervals(file.wfs.LookupFn(), chunks)
  118. file.reader = nil
  119. }
  120. file.entry.Attributes.FileSize = req.Size
  121. file.dirtyMetadata = true
  122. }
  123. if req.Valid.Mode() {
  124. file.entry.Attributes.FileMode = uint32(req.Mode)
  125. file.dirtyMetadata = true
  126. }
  127. if req.Valid.Uid() {
  128. file.entry.Attributes.Uid = req.Uid
  129. file.dirtyMetadata = true
  130. }
  131. if req.Valid.Gid() {
  132. file.entry.Attributes.Gid = req.Gid
  133. file.dirtyMetadata = true
  134. }
  135. if req.Valid.Crtime() {
  136. file.entry.Attributes.Crtime = req.Crtime.Unix()
  137. file.dirtyMetadata = true
  138. }
  139. if req.Valid.Mtime() {
  140. file.entry.Attributes.Mtime = req.Mtime.Unix()
  141. file.dirtyMetadata = true
  142. }
  143. if req.Valid.Handle() {
  144. // fmt.Printf("file handle => %d\n", req.Handle)
  145. }
  146. if file.isOpen > 0 {
  147. return nil
  148. }
  149. if !file.dirtyMetadata {
  150. return nil
  151. }
  152. return file.saveEntry(file.entry)
  153. }
  154. func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  155. glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name)
  156. entry, err := file.maybeLoadEntry(ctx)
  157. if err != nil {
  158. return err
  159. }
  160. if err := setxattr(entry, req); err != nil {
  161. return err
  162. }
  163. return file.saveEntry(entry)
  164. }
  165. func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  166. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  167. entry, err := file.maybeLoadEntry(ctx)
  168. if err != nil {
  169. return err
  170. }
  171. if err := removexattr(entry, req); err != nil {
  172. return err
  173. }
  174. return file.saveEntry(entry)
  175. }
  176. func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  177. glog.V(4).Infof("file Listxattr %s", file.fullpath())
  178. entry, err := file.maybeLoadEntry(ctx)
  179. if err != nil {
  180. return err
  181. }
  182. if err := listxattr(entry, req, resp); err != nil {
  183. return err
  184. }
  185. return nil
  186. }
  187. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  188. // fsync works at OS level
  189. // write the file chunks to the filerGrpcAddress
  190. glog.V(4).Infof("%s/%s fsync file %+v", file.dir.FullPath(), file.Name, req)
  191. return nil
  192. }
  193. func (file *File) Forget() {
  194. t := util.NewFullPath(file.dir.FullPath(), file.Name)
  195. glog.V(4).Infof("Forget file %s", t)
  196. file.wfs.fsNodeCache.DeleteFsNode(t)
  197. }
  198. func (file *File) maybeLoadEntry(ctx context.Context) (entry *filer_pb.Entry, err error) {
  199. entry = file.entry
  200. if file.isOpen > 0 {
  201. return entry, nil
  202. }
  203. if entry != nil {
  204. if len(entry.HardLinkId) == 0 {
  205. // only always reload hard link
  206. return entry, nil
  207. }
  208. }
  209. entry, err = file.wfs.maybeLoadEntry(file.dir.FullPath(), file.Name)
  210. if err != nil {
  211. glog.V(3).Infof("maybeLoadEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  212. return entry, err
  213. }
  214. if entry != nil {
  215. file.setEntry(entry)
  216. } else {
  217. glog.Warningf("maybeLoadEntry not found entry %s/%s: %v", file.dir.FullPath(), file.Name, err)
  218. }
  219. return entry, nil
  220. }
  221. func lessThan(a, b *filer_pb.FileChunk) bool {
  222. if a.Mtime == b.Mtime {
  223. return a.Fid.FileKey < b.Fid.FileKey
  224. }
  225. return a.Mtime < b.Mtime
  226. }
  227. func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
  228. // find the earliest incoming chunk
  229. newChunks := chunks
  230. earliestChunk := newChunks[0]
  231. for i := 1; i < len(newChunks); i++ {
  232. if lessThan(earliestChunk, newChunks[i]) {
  233. earliestChunk = newChunks[i]
  234. }
  235. }
  236. // pick out-of-order chunks from existing chunks
  237. for _, chunk := range file.entry.Chunks {
  238. if lessThan(earliestChunk, chunk) {
  239. chunks = append(chunks, chunk)
  240. }
  241. }
  242. // sort incoming chunks
  243. sort.Slice(chunks, func(i, j int) bool {
  244. return lessThan(chunks[i], chunks[j])
  245. })
  246. // add to entry view cache
  247. for _, chunk := range chunks {
  248. file.entryViewCache = filer.MergeIntoVisibles(file.entryViewCache, chunk)
  249. }
  250. file.reader = nil
  251. glog.V(4).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(file.entry.Chunks), len(chunks))
  252. file.entry.Chunks = append(file.entry.Chunks, newChunks...)
  253. }
  254. func (file *File) setEntry(entry *filer_pb.Entry) {
  255. file.entry = entry
  256. file.entryViewCache, _ = filer.NonOverlappingVisibleIntervals(file.wfs.LookupFn(), entry.Chunks)
  257. file.reader = nil
  258. }
  259. func (file *File) clearEntry() {
  260. file.entry = nil
  261. file.entryViewCache = nil
  262. file.reader = nil
  263. }
  264. func (file *File) saveEntry(entry *filer_pb.Entry) error {
  265. return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  266. file.wfs.mapPbIdFromLocalToFiler(entry)
  267. defer file.wfs.mapPbIdFromFilerToLocal(entry)
  268. request := &filer_pb.UpdateEntryRequest{
  269. Directory: file.dir.FullPath(),
  270. Entry: entry,
  271. Signatures: []int32{file.wfs.signature},
  272. }
  273. glog.V(4).Infof("save file entry: %v", request)
  274. _, err := client.UpdateEntry(context.Background(), request)
  275. if err != nil {
  276. glog.Errorf("UpdateEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  277. return fuse.EIO
  278. }
  279. file.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  280. return nil
  281. })
  282. }