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.

362 lines
8.7 KiB

7 years ago
5 years ago
4 years ago
5 years ago
5 years ago
5 years ago
4 years ago
4 years ago
4 years ago
7 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
5 years ago
4 years ago
6 years ago
6 years ago
4 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. "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.NodeIdentifier(&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. isOpen int
  31. dirtyMetadata bool
  32. id uint64
  33. }
  34. func (file *File) fullpath() util.FullPath {
  35. return util.NewFullPath(file.dir.FullPath(), file.Name)
  36. }
  37. func (file *File) Id() uint64 {
  38. return file.id
  39. }
  40. func (file *File) Attr(ctx context.Context, attr *fuse.Attr) (err error) {
  41. glog.V(4).Infof("file Attr %s, open:%v existing:%v", file.fullpath(), file.isOpen, attr)
  42. entry, err := file.maybeLoadEntry(ctx)
  43. if err != nil {
  44. return err
  45. }
  46. if entry == nil {
  47. return fuse.ENOENT
  48. }
  49. attr.Inode = file.Id()
  50. attr.Valid = time.Second
  51. attr.Mode = os.FileMode(entry.Attributes.FileMode)
  52. attr.Size = filer.FileSize(entry)
  53. if file.isOpen > 0 {
  54. attr.Size = entry.Attributes.FileSize
  55. glog.V(4).Infof("file Attr %s, open:%v, size: %d", file.fullpath(), file.isOpen, attr.Size)
  56. }
  57. attr.Crtime = time.Unix(entry.Attributes.Crtime, 0)
  58. attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  59. attr.Gid = entry.Attributes.Gid
  60. attr.Uid = entry.Attributes.Uid
  61. attr.Blocks = attr.Size/blockSize + 1
  62. attr.BlockSize = uint32(file.wfs.option.ChunkSizeLimit)
  63. if entry.HardLinkCounter > 0 {
  64. attr.Nlink = uint32(entry.HardLinkCounter)
  65. }
  66. return nil
  67. }
  68. func (file *File) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  69. // glog.V(4).Infof("file Getxattr %s", file.fullpath())
  70. entry, err := file.maybeLoadEntry(ctx)
  71. if err != nil {
  72. return err
  73. }
  74. return getxattr(entry, req, resp)
  75. }
  76. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  77. glog.V(4).Infof("file %v open %+v", file.fullpath(), req)
  78. handle := file.wfs.AcquireHandle(file, req.Uid, req.Gid, req.Flags&fuse.OpenWriteOnly > 0)
  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. entry, err := file.maybeLoadEntry(ctx)
  86. if err != nil {
  87. return err
  88. }
  89. if req.Valid.Size() {
  90. glog.V(4).Infof("%v file setattr set size=%v chunks=%d", file.fullpath(), req.Size, len(entry.Chunks))
  91. if req.Size < filer.FileSize(entry) {
  92. // fmt.Printf("truncate %v \n", fullPath)
  93. var chunks []*filer_pb.FileChunk
  94. var truncatedChunks []*filer_pb.FileChunk
  95. for _, chunk := range entry.Chunks {
  96. int64Size := int64(chunk.Size)
  97. if chunk.Offset+int64Size > int64(req.Size) {
  98. // this chunk is truncated
  99. int64Size = int64(req.Size) - chunk.Offset
  100. if int64Size > 0 {
  101. chunks = append(chunks, chunk)
  102. glog.V(4).Infof("truncated chunk %+v from %d to %d\n", chunk.GetFileIdString(), chunk.Size, int64Size)
  103. chunk.Size = uint64(int64Size)
  104. } else {
  105. glog.V(4).Infof("truncated whole chunk %+v\n", chunk.GetFileIdString())
  106. truncatedChunks = append(truncatedChunks, chunk)
  107. }
  108. }
  109. }
  110. entry.Chunks = chunks
  111. }
  112. entry.Attributes.FileSize = req.Size
  113. file.dirtyMetadata = true
  114. }
  115. if req.Valid.Mode() && entry.Attributes.FileMode != uint32(req.Mode) {
  116. entry.Attributes.FileMode = uint32(req.Mode)
  117. file.dirtyMetadata = true
  118. }
  119. if req.Valid.Uid() && entry.Attributes.Uid != req.Uid {
  120. entry.Attributes.Uid = req.Uid
  121. file.dirtyMetadata = true
  122. }
  123. if req.Valid.Gid() && entry.Attributes.Gid != req.Gid {
  124. entry.Attributes.Gid = req.Gid
  125. file.dirtyMetadata = true
  126. }
  127. if req.Valid.Crtime() {
  128. entry.Attributes.Crtime = req.Crtime.Unix()
  129. file.dirtyMetadata = true
  130. }
  131. if req.Valid.Mtime() && entry.Attributes.Mtime != req.Mtime.Unix() {
  132. entry.Attributes.Mtime = req.Mtime.Unix()
  133. file.dirtyMetadata = true
  134. }
  135. if req.Valid.Handle() {
  136. // fmt.Printf("file handle => %d\n", req.Handle)
  137. }
  138. if file.isOpen > 0 {
  139. return nil
  140. }
  141. if !file.dirtyMetadata {
  142. return nil
  143. }
  144. return file.saveEntry(entry)
  145. }
  146. func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  147. glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name)
  148. entry, err := file.maybeLoadEntry(ctx)
  149. if err != nil {
  150. return err
  151. }
  152. if err := setxattr(entry, req); err != nil {
  153. return err
  154. }
  155. file.dirtyMetadata = true
  156. if file.isOpen > 0 {
  157. return nil
  158. }
  159. return file.saveEntry(entry)
  160. }
  161. func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  162. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  163. entry, err := file.maybeLoadEntry(ctx)
  164. if err != nil {
  165. return err
  166. }
  167. if err := removexattr(entry, req); err != nil {
  168. return err
  169. }
  170. file.dirtyMetadata = true
  171. if file.isOpen > 0 {
  172. return nil
  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.ReleaseHandle(t, fuse.HandleID(t.AsInode()))
  197. }
  198. func (file *File) maybeLoadEntry(ctx context.Context) (entry *filer_pb.Entry, err error) {
  199. file.wfs.handlesLock.Lock()
  200. handle, found := file.wfs.handles[file.Id()]
  201. file.wfs.handlesLock.Unlock()
  202. entry = file.entry
  203. if found {
  204. // glog.V(4).Infof("maybeLoadEntry found opened file %s/%s", file.dir.FullPath(), file.Name)
  205. entry = handle.f.entry
  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.entry = 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. entry := file.getEntry()
  241. if entry == nil {
  242. return
  243. }
  244. // pick out-of-order chunks from existing chunks
  245. for _, chunk := range entry.Chunks {
  246. if lessThan(earliestChunk, chunk) {
  247. chunks = append(chunks, chunk)
  248. }
  249. }
  250. // sort incoming chunks
  251. sort.Slice(chunks, func(i, j int) bool {
  252. return lessThan(chunks[i], chunks[j])
  253. })
  254. glog.V(4).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(entry.Chunks), len(chunks))
  255. entry.Chunks = append(entry.Chunks, newChunks...)
  256. }
  257. func (file *File) saveEntry(entry *filer_pb.Entry) error {
  258. return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  259. file.wfs.mapPbIdFromLocalToFiler(entry)
  260. defer file.wfs.mapPbIdFromFilerToLocal(entry)
  261. request := &filer_pb.CreateEntryRequest{
  262. Directory: file.dir.FullPath(),
  263. Entry: entry,
  264. Signatures: []int32{file.wfs.signature},
  265. }
  266. glog.V(4).Infof("save file entry: %v", request)
  267. _, err := client.CreateEntry(context.Background(), request)
  268. if err != nil {
  269. glog.Errorf("UpdateEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  270. return fuse.EIO
  271. }
  272. file.wfs.metaCache.InsertEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  273. file.dirtyMetadata = false
  274. return nil
  275. })
  276. }
  277. func (file *File) getEntry() *filer_pb.Entry {
  278. return file.entry
  279. }