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.

229 lines
5.8 KiB

3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
3 years ago
  1. package mount
  2. import (
  3. "github.com/chrislusf/seaweedfs/weed/filer"
  4. "github.com/chrislusf/seaweedfs/weed/glog"
  5. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  6. "github.com/hanwen/go-fuse/v2/fuse"
  7. "os"
  8. "syscall"
  9. "time"
  10. )
  11. func (wfs *WFS) GetAttr(cancel <-chan struct{}, input *fuse.GetAttrIn, out *fuse.AttrOut) (code fuse.Status) {
  12. if input.NodeId == 1 {
  13. wfs.setRootAttr(out)
  14. return fuse.OK
  15. }
  16. _, _, entry, status := wfs.maybeReadEntry(input.NodeId)
  17. if status == fuse.OK {
  18. out.AttrValid = 1
  19. wfs.setAttrByPbEntry(&out.Attr, input.NodeId, entry)
  20. return status
  21. } else {
  22. if fh, found := wfs.fhmap.FindFileHandle(input.NodeId); found {
  23. out.AttrValid = 1
  24. wfs.setAttrByPbEntry(&out.Attr, input.NodeId, fh.entry)
  25. out.Nlink = 0
  26. return fuse.OK
  27. }
  28. }
  29. return status
  30. }
  31. func (wfs *WFS) SetAttr(cancel <-chan struct{}, input *fuse.SetAttrIn, out *fuse.AttrOut) (code fuse.Status) {
  32. if wfs.IsOverQuota {
  33. return fuse.Status(syscall.ENOSPC)
  34. }
  35. path, fh, entry, status := wfs.maybeReadEntry(input.NodeId)
  36. if status != fuse.OK {
  37. return status
  38. }
  39. if size, ok := input.GetSize(); ok {
  40. glog.V(4).Infof("%v setattr set size=%v chunks=%d", path, size, len(entry.Chunks))
  41. if size < filer.FileSize(entry) {
  42. // fmt.Printf("truncate %v \n", fullPath)
  43. var chunks []*filer_pb.FileChunk
  44. var truncatedChunks []*filer_pb.FileChunk
  45. for _, chunk := range entry.Chunks {
  46. int64Size := int64(chunk.Size)
  47. if chunk.Offset+int64Size > int64(size) {
  48. // this chunk is truncated
  49. int64Size = int64(size) - chunk.Offset
  50. if int64Size > 0 {
  51. chunks = append(chunks, chunk)
  52. glog.V(4).Infof("truncated chunk %+v from %d to %d\n", chunk.GetFileIdString(), chunk.Size, int64Size)
  53. chunk.Size = uint64(int64Size)
  54. } else {
  55. glog.V(4).Infof("truncated whole chunk %+v\n", chunk.GetFileIdString())
  56. truncatedChunks = append(truncatedChunks, chunk)
  57. }
  58. }
  59. }
  60. // set the new chunks and reset entry cache
  61. entry.Chunks = chunks
  62. if fh != nil {
  63. fh.entryViewCache = nil
  64. }
  65. }
  66. entry.Attributes.Mtime = time.Now().Unix()
  67. entry.Attributes.FileSize = size
  68. }
  69. if mode, ok := input.GetMode(); ok {
  70. // glog.V(4).Infof("setAttr mode %o", mode)
  71. entry.Attributes.FileMode = chmod(entry.Attributes.FileMode, mode)
  72. }
  73. if uid, ok := input.GetUID(); ok {
  74. entry.Attributes.Uid = uid
  75. }
  76. if gid, ok := input.GetGID(); ok {
  77. entry.Attributes.Gid = gid
  78. }
  79. if mtime, ok := input.GetMTime(); ok {
  80. entry.Attributes.Mtime = mtime.Unix()
  81. }
  82. if atime, ok := input.GetATime(); ok {
  83. entry.Attributes.Mtime = atime.Unix()
  84. }
  85. entry.Attributes.Mtime = time.Now().Unix()
  86. out.AttrValid = 1
  87. wfs.setAttrByPbEntry(&out.Attr, input.NodeId, entry)
  88. if fh != nil {
  89. fh.dirtyMetadata = true
  90. return fuse.OK
  91. }
  92. return wfs.saveEntry(path, entry)
  93. }
  94. func (wfs *WFS) setRootAttr(out *fuse.AttrOut) {
  95. now := uint64(time.Now().Unix())
  96. out.AttrValid = 119
  97. out.Ino = 1
  98. setBlksize(&out.Attr, blockSize)
  99. out.Uid = wfs.option.MountUid
  100. out.Gid = wfs.option.MountGid
  101. out.Mtime = now
  102. out.Ctime = now
  103. out.Atime = now
  104. out.Mode = toSyscallType(os.ModeDir) | uint32(wfs.option.MountMode)
  105. out.Nlink = 1
  106. }
  107. func (wfs *WFS) setAttrByPbEntry(out *fuse.Attr, inode uint64, entry *filer_pb.Entry) {
  108. out.Ino = inode
  109. out.Size = filer.FileSize(entry)
  110. out.Blocks = (out.Size + blockSize - 1) / blockSize
  111. setBlksize(out, blockSize)
  112. out.Mtime = uint64(entry.Attributes.Mtime)
  113. out.Ctime = uint64(entry.Attributes.Mtime)
  114. out.Atime = uint64(entry.Attributes.Mtime)
  115. out.Mode = toSyscallMode(os.FileMode(entry.Attributes.FileMode))
  116. if entry.HardLinkCounter > 0 {
  117. out.Nlink = uint32(entry.HardLinkCounter)
  118. } else {
  119. out.Nlink = 1
  120. }
  121. out.Uid = entry.Attributes.Uid
  122. out.Gid = entry.Attributes.Gid
  123. out.Rdev = entry.Attributes.Rdev
  124. }
  125. func (wfs *WFS) setAttrByFilerEntry(out *fuse.Attr, inode uint64, entry *filer.Entry) {
  126. out.Ino = inode
  127. out.Size = entry.FileSize
  128. out.Blocks = (out.Size + blockSize - 1) / blockSize
  129. setBlksize(out, blockSize)
  130. out.Atime = uint64(entry.Attr.Mtime.Unix())
  131. out.Mtime = uint64(entry.Attr.Mtime.Unix())
  132. out.Ctime = uint64(entry.Attr.Mtime.Unix())
  133. out.Mode = toSyscallMode(entry.Attr.Mode)
  134. if entry.HardLinkCounter > 0 {
  135. out.Nlink = uint32(entry.HardLinkCounter)
  136. } else {
  137. out.Nlink = 1
  138. }
  139. out.Uid = entry.Attr.Uid
  140. out.Gid = entry.Attr.Gid
  141. out.Rdev = entry.Attr.Rdev
  142. }
  143. func (wfs *WFS) outputPbEntry(out *fuse.EntryOut, inode uint64, entry *filer_pb.Entry) {
  144. out.NodeId = inode
  145. out.Generation = 1
  146. out.EntryValid = 1
  147. out.AttrValid = 1
  148. wfs.setAttrByPbEntry(&out.Attr, inode, entry)
  149. }
  150. func (wfs *WFS) outputFilerEntry(out *fuse.EntryOut, inode uint64, entry *filer.Entry) {
  151. out.NodeId = inode
  152. out.Generation = 1
  153. out.EntryValid = 1
  154. out.AttrValid = 1
  155. wfs.setAttrByFilerEntry(&out.Attr, inode, entry)
  156. }
  157. func chmod(existing uint32, mode uint32) uint32 {
  158. return existing&^07777 | mode&07777
  159. }
  160. func toSyscallMode(mode os.FileMode) uint32 {
  161. return toSyscallType(mode) | uint32(mode)
  162. }
  163. func toSyscallType(mode os.FileMode) uint32 {
  164. switch mode & os.ModeType {
  165. case os.ModeDir:
  166. return syscall.S_IFDIR
  167. case os.ModeSymlink:
  168. return syscall.S_IFLNK
  169. case os.ModeNamedPipe:
  170. return syscall.S_IFIFO
  171. case os.ModeSocket:
  172. return syscall.S_IFSOCK
  173. case os.ModeDevice:
  174. return syscall.S_IFBLK
  175. case os.ModeCharDevice:
  176. return syscall.S_IFCHR
  177. default:
  178. return syscall.S_IFREG
  179. }
  180. }
  181. func toOsFileType(mode uint32) os.FileMode {
  182. switch mode & (syscall.S_IFMT & 0xffff) {
  183. case syscall.S_IFDIR:
  184. return os.ModeDir
  185. case syscall.S_IFLNK:
  186. return os.ModeSymlink
  187. case syscall.S_IFIFO:
  188. return os.ModeNamedPipe
  189. case syscall.S_IFSOCK:
  190. return os.ModeSocket
  191. case syscall.S_IFBLK:
  192. return os.ModeDevice
  193. case syscall.S_IFCHR:
  194. return os.ModeCharDevice
  195. default:
  196. return 0
  197. }
  198. }
  199. func toOsFileMode(mode uint32) os.FileMode {
  200. return toOsFileType(mode) | os.FileMode(mode&07777)
  201. }