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.

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