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.

360 lines
8.5 KiB

7 years ago
5 years ago
4 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
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)
  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 file.isOpen > 0 {
  90. file.wfs.handlesLock.Lock()
  91. fileHandle := file.wfs.handles[file.Id()]
  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(entry.Chunks))
  100. if req.Size < filer.FileSize(entry) {
  101. // fmt.Printf("truncate %v \n", fullPath)
  102. var chunks []*filer_pb.FileChunk
  103. var truncatedChunks []*filer_pb.FileChunk
  104. for _, chunk := range 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. entry.Chunks = chunks
  120. }
  121. entry.Attributes.FileSize = req.Size
  122. file.dirtyMetadata = true
  123. }
  124. if req.Valid.Mode() {
  125. entry.Attributes.FileMode = uint32(req.Mode)
  126. file.dirtyMetadata = true
  127. }
  128. if req.Valid.Uid() {
  129. entry.Attributes.Uid = req.Uid
  130. file.dirtyMetadata = true
  131. }
  132. if req.Valid.Gid() {
  133. entry.Attributes.Gid = req.Gid
  134. file.dirtyMetadata = true
  135. }
  136. if req.Valid.Crtime() {
  137. entry.Attributes.Crtime = req.Crtime.Unix()
  138. file.dirtyMetadata = true
  139. }
  140. if req.Valid.Mtime() {
  141. 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(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.ReleaseHandle(t, fuse.HandleID(t.AsInode()))
  198. }
  199. func (file *File) maybeLoadEntry(ctx context.Context) (entry *filer_pb.Entry, err error) {
  200. file.wfs.handlesLock.Lock()
  201. handle, found := file.wfs.handles[file.Id()]
  202. file.wfs.handlesLock.Unlock()
  203. entry = file.entry
  204. if found {
  205. glog.V(4).Infof("maybeLoadEntry found opened file %s/%s: %v %v", file.dir.FullPath(), file.Name, handle.f.entry, entry)
  206. entry = handle.f.entry
  207. }
  208. if entry != nil {
  209. if len(entry.HardLinkId) == 0 {
  210. // only always reload hard link
  211. return entry, nil
  212. }
  213. }
  214. entry, err = file.wfs.maybeLoadEntry(file.dir.FullPath(), file.Name)
  215. if err != nil {
  216. glog.V(3).Infof("maybeLoadEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  217. return entry, err
  218. }
  219. if entry != nil {
  220. // file.entry = entry
  221. } else {
  222. glog.Warningf("maybeLoadEntry not found entry %s/%s: %v", file.dir.FullPath(), file.Name, err)
  223. }
  224. return entry, nil
  225. }
  226. func lessThan(a, b *filer_pb.FileChunk) bool {
  227. if a.Mtime == b.Mtime {
  228. return a.Fid.FileKey < b.Fid.FileKey
  229. }
  230. return a.Mtime < b.Mtime
  231. }
  232. func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
  233. // find the earliest incoming chunk
  234. newChunks := chunks
  235. earliestChunk := newChunks[0]
  236. for i := 1; i < len(newChunks); i++ {
  237. if lessThan(earliestChunk, newChunks[i]) {
  238. earliestChunk = newChunks[i]
  239. }
  240. }
  241. entry := file.getEntry()
  242. if entry == nil {
  243. return
  244. }
  245. // pick out-of-order chunks from existing chunks
  246. for _, chunk := range entry.Chunks {
  247. if lessThan(earliestChunk, chunk) {
  248. chunks = append(chunks, chunk)
  249. }
  250. }
  251. // sort incoming chunks
  252. sort.Slice(chunks, func(i, j int) bool {
  253. return lessThan(chunks[i], chunks[j])
  254. })
  255. glog.V(4).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(entry.Chunks), len(chunks))
  256. entry.Chunks = append(entry.Chunks, newChunks...)
  257. }
  258. func (file *File) saveEntry(entry *filer_pb.Entry) error {
  259. return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  260. file.wfs.mapPbIdFromLocalToFiler(entry)
  261. defer file.wfs.mapPbIdFromFilerToLocal(entry)
  262. request := &filer_pb.UpdateEntryRequest{
  263. Directory: file.dir.FullPath(),
  264. Entry: entry,
  265. Signatures: []int32{file.wfs.signature},
  266. }
  267. glog.V(4).Infof("save file entry: %v", request)
  268. _, err := client.UpdateEntry(context.Background(), request)
  269. if err != nil {
  270. glog.Errorf("UpdateEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  271. return fuse.EIO
  272. }
  273. file.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  274. return nil
  275. })
  276. }
  277. func (file *File) getEntry() *filer_pb.Entry {
  278. return file.entry
  279. }