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.

385 lines
9.2 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
6 years ago
5 years ago
5 years ago
  1. package filesys
  2. import (
  3. "context"
  4. "io"
  5. "os"
  6. "sort"
  7. "sync"
  8. "time"
  9. "github.com/seaweedfs/fuse"
  10. "github.com/seaweedfs/fuse/fs"
  11. "github.com/chrislusf/seaweedfs/weed/filer"
  12. "github.com/chrislusf/seaweedfs/weed/glog"
  13. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  14. "github.com/chrislusf/seaweedfs/weed/util"
  15. )
  16. const blockSize = 512
  17. var _ = fs.Node(&File{})
  18. var _ = fs.NodeOpener(&File{})
  19. var _ = fs.NodeFsyncer(&File{})
  20. var _ = fs.NodeSetattrer(&File{})
  21. var _ = fs.NodeGetxattrer(&File{})
  22. var _ = fs.NodeSetxattrer(&File{})
  23. var _ = fs.NodeRemovexattrer(&File{})
  24. var _ = fs.NodeListxattrer(&File{})
  25. var _ = fs.NodeForgetter(&File{})
  26. type File struct {
  27. Name string
  28. dir *Dir
  29. wfs *WFS
  30. entry *filer_pb.Entry
  31. entryLock sync.RWMutex
  32. entryViewCache []filer.VisibleInterval
  33. isOpen int
  34. reader io.ReaderAt
  35. dirtyMetadata bool
  36. }
  37. func (file *File) fullpath() util.FullPath {
  38. return util.NewFullPath(file.dir.FullPath(), file.Name)
  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 := file.getEntry()
  43. if file.isOpen <= 0 || entry == nil {
  44. if entry, err = file.maybeLoadEntry(ctx); err != nil {
  45. return err
  46. }
  47. }
  48. if entry == nil {
  49. return fuse.ENOENT
  50. }
  51. // attr.Inode = file.fullpath().AsInode()
  52. attr.Valid = time.Second
  53. attr.Mode = os.FileMode(entry.Attributes.FileMode)
  54. attr.Size = filer.FileSize(entry)
  55. if file.isOpen > 0 {
  56. attr.Size = entry.Attributes.FileSize
  57. glog.V(4).Infof("file Attr %s, open:%v, size: %d", file.fullpath(), file.isOpen, attr.Size)
  58. }
  59. attr.Crtime = time.Unix(entry.Attributes.Crtime, 0)
  60. attr.Mtime = time.Unix(entry.Attributes.Mtime, 0)
  61. attr.Gid = entry.Attributes.Gid
  62. attr.Uid = entry.Attributes.Uid
  63. attr.Blocks = attr.Size/blockSize + 1
  64. attr.BlockSize = uint32(file.wfs.option.ChunkSizeLimit)
  65. if entry.HardLinkCounter > 0 {
  66. attr.Nlink = uint32(entry.HardLinkCounter)
  67. }
  68. return nil
  69. }
  70. func (file *File) Getxattr(ctx context.Context, req *fuse.GetxattrRequest, resp *fuse.GetxattrResponse) error {
  71. glog.V(4).Infof("file Getxattr %s", file.fullpath())
  72. entry, err := file.maybeLoadEntry(ctx)
  73. if err != nil {
  74. return err
  75. }
  76. return getxattr(entry, req, resp)
  77. }
  78. func (file *File) Open(ctx context.Context, req *fuse.OpenRequest, resp *fuse.OpenResponse) (fs.Handle, error) {
  79. glog.V(4).Infof("file %v open %+v", file.fullpath(), req)
  80. handle := file.wfs.AcquireHandle(file, req.Uid, req.Gid)
  81. resp.Handle = fuse.HandleID(handle.handle)
  82. glog.V(4).Infof("%v file open handle id = %d", file.fullpath(), handle.handle)
  83. return handle, nil
  84. }
  85. func (file *File) Setattr(ctx context.Context, req *fuse.SetattrRequest, resp *fuse.SetattrResponse) error {
  86. glog.V(4).Infof("%v file setattr %+v", file.fullpath(), req)
  87. entry, err := file.maybeLoadEntry(ctx)
  88. if err != nil {
  89. return err
  90. }
  91. if file.isOpen > 0 {
  92. file.wfs.handlesLock.Lock()
  93. fileHandle := file.wfs.handles[file.fullpath().AsInode()]
  94. file.wfs.handlesLock.Unlock()
  95. if fileHandle != nil {
  96. fileHandle.Lock()
  97. defer fileHandle.Unlock()
  98. }
  99. }
  100. if req.Valid.Size() {
  101. glog.V(4).Infof("%v file setattr set size=%v chunks=%d", file.fullpath(), req.Size, len(entry.Chunks))
  102. if req.Size < filer.FileSize(entry) {
  103. // fmt.Printf("truncate %v \n", fullPath)
  104. var chunks []*filer_pb.FileChunk
  105. var truncatedChunks []*filer_pb.FileChunk
  106. for _, chunk := range entry.Chunks {
  107. int64Size := int64(chunk.Size)
  108. if chunk.Offset+int64Size > int64(req.Size) {
  109. // this chunk is truncated
  110. int64Size = int64(req.Size) - chunk.Offset
  111. if int64Size > 0 {
  112. chunks = append(chunks, chunk)
  113. glog.V(4).Infof("truncated chunk %+v from %d to %d\n", chunk.GetFileIdString(), chunk.Size, int64Size)
  114. chunk.Size = uint64(int64Size)
  115. } else {
  116. glog.V(4).Infof("truncated whole chunk %+v\n", chunk.GetFileIdString())
  117. truncatedChunks = append(truncatedChunks, chunk)
  118. }
  119. }
  120. }
  121. entry.Chunks = chunks
  122. file.entryViewCache, _ = filer.NonOverlappingVisibleIntervals(file.wfs.LookupFn(), chunks)
  123. file.reader = nil
  124. }
  125. entry.Attributes.FileSize = req.Size
  126. file.dirtyMetadata = true
  127. }
  128. if req.Valid.Mode() {
  129. entry.Attributes.FileMode = uint32(req.Mode)
  130. file.dirtyMetadata = true
  131. }
  132. if req.Valid.Uid() {
  133. entry.Attributes.Uid = req.Uid
  134. file.dirtyMetadata = true
  135. }
  136. if req.Valid.Gid() {
  137. entry.Attributes.Gid = req.Gid
  138. file.dirtyMetadata = true
  139. }
  140. if req.Valid.Crtime() {
  141. entry.Attributes.Crtime = req.Crtime.Unix()
  142. file.dirtyMetadata = true
  143. }
  144. if req.Valid.Mtime() {
  145. entry.Attributes.Mtime = req.Mtime.Unix()
  146. file.dirtyMetadata = true
  147. }
  148. if req.Valid.Handle() {
  149. // fmt.Printf("file handle => %d\n", req.Handle)
  150. }
  151. if file.isOpen > 0 {
  152. return nil
  153. }
  154. if !file.dirtyMetadata {
  155. return nil
  156. }
  157. return file.saveEntry(entry)
  158. }
  159. func (file *File) Setxattr(ctx context.Context, req *fuse.SetxattrRequest) error {
  160. glog.V(4).Infof("file Setxattr %s: %s", file.fullpath(), req.Name)
  161. entry, err := file.maybeLoadEntry(ctx)
  162. if err != nil {
  163. return err
  164. }
  165. if err := setxattr(entry, req); err != nil {
  166. return err
  167. }
  168. return file.saveEntry(entry)
  169. }
  170. func (file *File) Removexattr(ctx context.Context, req *fuse.RemovexattrRequest) error {
  171. glog.V(4).Infof("file Removexattr %s: %s", file.fullpath(), req.Name)
  172. entry, err := file.maybeLoadEntry(ctx)
  173. if err != nil {
  174. return err
  175. }
  176. if err := removexattr(entry, req); err != nil {
  177. return err
  178. }
  179. return file.saveEntry(entry)
  180. }
  181. func (file *File) Listxattr(ctx context.Context, req *fuse.ListxattrRequest, resp *fuse.ListxattrResponse) error {
  182. glog.V(4).Infof("file Listxattr %s", file.fullpath())
  183. entry, err := file.maybeLoadEntry(ctx)
  184. if err != nil {
  185. return err
  186. }
  187. if err := listxattr(entry, req, resp); err != nil {
  188. return err
  189. }
  190. return nil
  191. }
  192. func (file *File) Fsync(ctx context.Context, req *fuse.FsyncRequest) error {
  193. // fsync works at OS level
  194. // write the file chunks to the filerGrpcAddress
  195. glog.V(4).Infof("%s/%s fsync file %+v", file.dir.FullPath(), file.Name, req)
  196. return nil
  197. }
  198. func (file *File) Forget() {
  199. t := util.NewFullPath(file.dir.FullPath(), file.Name)
  200. glog.V(4).Infof("Forget file %s", t)
  201. file.wfs.fsNodeCache.DeleteFsNode(t)
  202. }
  203. func (file *File) maybeLoadEntry(ctx context.Context) (entry *filer_pb.Entry, err error) {
  204. entry = file.getEntry()
  205. if file.isOpen > 0 {
  206. return entry, nil
  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. if entry.Attributes == nil {
  221. entry.Attributes = &filer_pb.FuseAttributes{}
  222. }
  223. file.setEntry(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. // add to entry view cache
  259. for _, chunk := range chunks {
  260. file.entryViewCache = filer.MergeIntoVisibles(file.entryViewCache, chunk)
  261. }
  262. file.reader = nil
  263. glog.V(4).Infof("%s existing %d chunks adds %d more", file.fullpath(), len(entry.Chunks), len(chunks))
  264. entry.Chunks = append(entry.Chunks, newChunks...)
  265. }
  266. func (file *File) setEntry(entry *filer_pb.Entry) {
  267. file.entryLock.Lock()
  268. defer file.entryLock.Unlock()
  269. file.entry = entry
  270. file.entryViewCache, _ = filer.NonOverlappingVisibleIntervals(file.wfs.LookupFn(), entry.Chunks)
  271. file.reader = nil
  272. }
  273. func (file *File) clearEntry() {
  274. file.entryLock.Lock()
  275. defer file.entryLock.Unlock()
  276. file.entry = nil
  277. file.entryViewCache = nil
  278. file.reader = nil
  279. }
  280. func (file *File) saveEntry(entry *filer_pb.Entry) error {
  281. return file.wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  282. file.wfs.mapPbIdFromLocalToFiler(entry)
  283. defer file.wfs.mapPbIdFromFilerToLocal(entry)
  284. request := &filer_pb.UpdateEntryRequest{
  285. Directory: file.dir.FullPath(),
  286. Entry: entry,
  287. Signatures: []int32{file.wfs.signature},
  288. }
  289. glog.V(4).Infof("save file entry: %v", request)
  290. _, err := client.UpdateEntry(context.Background(), request)
  291. if err != nil {
  292. glog.Errorf("UpdateEntry file %s/%s: %v", file.dir.FullPath(), file.Name, err)
  293. return fuse.EIO
  294. }
  295. file.wfs.metaCache.UpdateEntry(context.Background(), filer.FromPbEntry(request.Directory, request.Entry))
  296. return nil
  297. })
  298. }
  299. func (file *File) getEntry() *filer_pb.Entry {
  300. file.entryLock.RLock()
  301. defer file.entryLock.RUnlock()
  302. return file.entry
  303. }