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.

351 lines
8.3 KiB

7 years ago
5 years ago
4 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
5 years ago
5 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "os"
  5. "sort"
  6. "time"
  7. "github.com/seaweedfs/fuse"
  8. "github.com/seaweedfs/fuse/fs"
  9. "github.com/chrislusf/seaweedfs/weed/filer"
  10. "github.com/chrislusf/seaweedfs/weed/glog"
  11. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. const blockSize = 512
  15. var _ = fs.Node(&File{})
  16. var _ = fs.NodeOpener(&File{})
  17. var _ = fs.NodeFsyncer(&File{})
  18. var _ = fs.NodeSetattrer(&File{})
  19. var _ = fs.NodeGetxattrer(&File{})
  20. var _ = fs.NodeSetxattrer(&File{})
  21. var _ = fs.NodeRemovexattrer(&File{})
  22. var _ = fs.NodeListxattrer(&File{})
  23. var _ = fs.NodeForgetter(&File{})
  24. type File struct {
  25. Name string
  26. dir *Dir
  27. wfs *WFS
  28. entry *filer_pb.Entry
  29. isOpen int
  30. dirtyMetadata bool
  31. }
  32. func (file *File) fullpath() util.FullPath {
  33. return util.NewFullPath(file.dir.FullPath(), file.Name)
  34. }
  35. func (file *File) Attr(ctx context.Context, attr *fuse.Attr) (err error) {
  36. glog.V(4).Infof("file Attr %s, open:%v existing:%v", file.fullpath(), file.isOpen, attr)
  37. entry := file.getEntry()
  38. if file.isOpen <= 0 || entry == nil {
  39. if entry, err = file.maybeLoadEntry(ctx); err != nil {
  40. return err
  41. }
  42. }
  43. if entry == nil {
  44. return fuse.ENOENT
  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. entry, 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(entry.Chunks))
  97. if req.Size < filer.FileSize(entry) {
  98. // fmt.Printf("truncate %v \n", fullPath)
  99. var chunks []*filer_pb.FileChunk
  100. var truncatedChunks []*filer_pb.FileChunk
  101. for _, chunk := range 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. entry.Chunks = chunks
  117. }
  118. entry.Attributes.FileSize = req.Size
  119. file.dirtyMetadata = true
  120. }
  121. if req.Valid.Mode() {
  122. entry.Attributes.FileMode = uint32(req.Mode)
  123. file.dirtyMetadata = true
  124. }
  125. if req.Valid.Uid() {
  126. entry.Attributes.Uid = req.Uid
  127. file.dirtyMetadata = true
  128. }
  129. if req.Valid.Gid() {
  130. entry.Attributes.Gid = req.Gid
  131. file.dirtyMetadata = true
  132. }
  133. if req.Valid.Crtime() {
  134. entry.Attributes.Crtime = req.Crtime.Unix()
  135. file.dirtyMetadata = true
  136. }
  137. if req.Valid.Mtime() {
  138. 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(entry)
  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. entry, err := file.maybeLoadEntry(ctx)
  155. if err != nil {
  156. return err
  157. }
  158. if err := setxattr(entry, req); err != nil {
  159. return err
  160. }
  161. return file.saveEntry(entry)
  162. }
  163. func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  164. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  165. entry, err := file.maybeLoadEntry(ctx)
  166. if err != nil {
  167. return err
  168. }
  169. if err := removexattr(entry, req); err != nil {
  170. return err
  171. }
  172. return file.saveEntry(entry)
  173. }
  174. func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  175. glog.V(4).Infof("file Listxattr %s", file.fullpath())
  176. entry, err := file.maybeLoadEntry(ctx)
  177. if err != nil {
  178. return err
  179. }
  180. if err := listxattr(entry, req, resp); err != nil {
  181. return err
  182. }
  183. return nil
  184. }
  185. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  186. // fsync works at OS level
  187. // write the file chunks to the filerGrpcAddress
  188. glog.V(4).Infof("%s/%s fsync file %+v", file.dir.FullPath(), file.Name, req)
  189. return nil
  190. }
  191. func (file *File) Forget() {
  192. t := util.NewFullPath(file.dir.FullPath(), file.Name)
  193. glog.V(4).Infof("Forget file %s", t)
  194. file.wfs.fsNodeCache.DeleteFsNode(t)
  195. file.wfs.ReleaseHandle(t, 0)
  196. }
  197. func (file *File) maybeLoadEntry(ctx context.Context) (entry *filer_pb.Entry, err error) {
  198. entry = file.getEntry()
  199. if file.isOpen > 0 {
  200. return entry, nil
  201. }
  202. if entry != nil {
  203. if len(entry.HardLinkId) == 0 {
  204. // only always reload hard link
  205. return entry, nil
  206. }
  207. }
  208. entry, err = file.wfs.maybeLoadEntry(file.dir.FullPath(), file.Name)
  209. if err != nil {
  210. glog.V(3).Infof("maybeLoadEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  211. return entry, err
  212. }
  213. if entry != nil {
  214. // file.entry = entry
  215. } else {
  216. glog.Warningf("maybeLoadEntry not found entry %s/%s: %v", file.dir.FullPath(), file.Name, err)
  217. }
  218. return entry, nil
  219. }
  220. func lessThan(a, b *filer_pb.FileChunk) bool {
  221. if a.Mtime == b.Mtime {
  222. return a.Fid.FileKey < b.Fid.FileKey
  223. }
  224. return a.Mtime < b.Mtime
  225. }
  226. func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
  227. // find the earliest incoming chunk
  228. newChunks := chunks
  229. earliestChunk := newChunks[0]
  230. for i := 1; i < len(newChunks); i++ {
  231. if lessThan(earliestChunk, newChunks[i]) {
  232. earliestChunk = newChunks[i]
  233. }
  234. }
  235. entry := file.getEntry()
  236. if entry == nil {
  237. return
  238. }
  239. // pick out-of-order chunks from existing chunks
  240. for _, chunk := range entry.Chunks {
  241. if lessThan(earliestChunk, chunk) {
  242. chunks = append(chunks, chunk)
  243. }
  244. }
  245. // sort incoming chunks
  246. sort.Slice(chunks, func(i, j int) bool {
  247. return lessThan(chunks[i], chunks[j])
  248. })
  249. glog.V(4).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(entry.Chunks), len(chunks))
  250. entry.Chunks = append(entry.Chunks, newChunks...)
  251. }
  252. func (file *File) saveEntry(entry *filer_pb.Entry) error {
  253. return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  254. file.wfs.mapPbIdFromLocalToFiler(entry)
  255. defer file.wfs.mapPbIdFromFilerToLocal(entry)
  256. request := &filer_pb.UpdateEntryRequest{
  257. Directory: file.dir.FullPath(),
  258. Entry: entry,
  259. Signatures: []int32{file.wfs.signature},
  260. }
  261. glog.V(4).Infof("save file entry: %v", request)
  262. _, err := client.UpdateEntry(context.Background(), request)
  263. if err != nil {
  264. glog.Errorf("UpdateEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  265. return fuse.EIO
  266. }
  267. file.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  268. return nil
  269. })
  270. }
  271. func (file *File) getEntry() *filer_pb.Entry {
  272. return file.entry
  273. }