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.

363 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
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.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. if file.wfs.option.ReadOnly {
  82. return fuse.EPERM
  83. }
  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.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(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. if file.wfs.option.ReadOnly {
  157. return fuse.EPERM
  158. }
  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. if file.wfs.option.ReadOnly {
  171. return fuse.EPERM
  172. }
  173. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  174. entry, err := file.maybeLoadEntry(ctx)
  175. if err != nil {
  176. return err
  177. }
  178. if err := removexattr(entry, req); err != nil {
  179. return err
  180. }
  181. return file.saveEntry(entry)
  182. }
  183. func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  184. glog.V(4).Infof("file Listxattr %s", file.fullpath())
  185. entry, err := file.maybeLoadEntry(ctx)
  186. if err != nil {
  187. return err
  188. }
  189. if err := listxattr(entry, req, resp); err != nil {
  190. return err
  191. }
  192. return nil
  193. }
  194. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  195. // fsync works at OS level
  196. // write the file chunks to the filerGrpcAddress
  197. glog.V(4).Infof("%s/%s fsync file %+v", file.dir.FullPath(), file.Name, req)
  198. return nil
  199. }
  200. func (file *File) Forget() {
  201. t := util.NewFullPath(file.dir.FullPath(), file.Name)
  202. glog.V(4).Infof("Forget file %s", t)
  203. file.wfs.fsNodeCache.DeleteFsNode(t)
  204. file.wfs.ReleaseHandle(t, 0)
  205. }
  206. func (file *File) maybeLoadEntry(ctx context.Context) (entry *filer_pb.Entry, err error) {
  207. entry = file.getEntry()
  208. if file.isOpen > 0 {
  209. return entry, nil
  210. }
  211. if entry != nil {
  212. if len(entry.HardLinkId) == 0 {
  213. // only always reload hard link
  214. return entry, nil
  215. }
  216. }
  217. entry, err = file.wfs.maybeLoadEntry(file.dir.FullPath(), file.Name)
  218. if err != nil {
  219. glog.V(3).Infof("maybeLoadEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  220. return entry, err
  221. }
  222. if entry != nil {
  223. file.entry = entry
  224. } else {
  225. glog.Warningf("maybeLoadEntry not found entry %s/%s: %v", file.dir.FullPath(), file.Name, err)
  226. }
  227. return entry, nil
  228. }
  229. func lessThan(a, b *filer_pb.FileChunk) bool {
  230. if a.Mtime == b.Mtime {
  231. return a.Fid.FileKey < b.Fid.FileKey
  232. }
  233. return a.Mtime < b.Mtime
  234. }
  235. func (file *File) addChunks(chunks []*filer_pb.FileChunk) {
  236. // find the earliest incoming chunk
  237. newChunks := chunks
  238. earliestChunk := newChunks[0]
  239. for i := 1; i < len(newChunks); i++ {
  240. if lessThan(earliestChunk, newChunks[i]) {
  241. earliestChunk = newChunks[i]
  242. }
  243. }
  244. entry := file.getEntry()
  245. if entry == nil {
  246. return
  247. }
  248. // pick out-of-order chunks from existing chunks
  249. for _, chunk := range entry.Chunks {
  250. if lessThan(earliestChunk, chunk) {
  251. chunks = append(chunks, chunk)
  252. }
  253. }
  254. // sort incoming chunks
  255. sort.Slice(chunks, func(i, j int) bool {
  256. return lessThan(chunks[i], chunks[j])
  257. })
  258. glog.V(4).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(entry.Chunks), len(chunks))
  259. entry.Chunks = append(entry.Chunks, newChunks...)
  260. }
  261. func (file *File) saveEntry(entry *filer_pb.Entry) error {
  262. return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  263. file.wfs.mapPbIdFromLocalToFiler(entry)
  264. defer file.wfs.mapPbIdFromFilerToLocal(entry)
  265. request := &filer_pb.UpdateEntryRequest{
  266. Directory: file.dir.FullPath(),
  267. Entry: entry,
  268. Signatures: []int32{file.wfs.signature},
  269. }
  270. glog.V(4).Infof("save file entry: %v", request)
  271. _, err := client.UpdateEntry(context.Background(), request)
  272. if err != nil {
  273. glog.Errorf("UpdateEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  274. return fuse.EIO
  275. }
  276. file.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  277. return nil
  278. })
  279. }
  280. func (file *File) getEntry() *filer_pb.Entry {
  281. return file.entry
  282. }