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.

237 lines
5.9 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
  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. if input.NodeId == 1 {
  73. wfs.option.MountMode = os.FileMode(chmod(uint32(wfs.option.MountMode), mode))
  74. }
  75. }
  76. if uid, ok := input.GetUID(); ok {
  77. entry.Attributes.Uid = uid
  78. if input.NodeId == 1 {
  79. wfs.option.MountUid = uid
  80. }
  81. }
  82. if gid, ok := input.GetGID(); ok {
  83. entry.Attributes.Gid = gid
  84. if input.NodeId == 1 {
  85. wfs.option.MountGid = gid
  86. }
  87. }
  88. if atime, ok := input.GetATime(); ok {
  89. entry.Attributes.Mtime = atime.Unix()
  90. }
  91. if mtime, ok := input.GetMTime(); ok {
  92. entry.Attributes.Mtime = mtime.Unix()
  93. }
  94. out.AttrValid = 1
  95. wfs.setAttrByPbEntry(&out.Attr, input.NodeId, entry)
  96. if fh != nil {
  97. fh.dirtyMetadata = true
  98. return fuse.OK
  99. }
  100. return wfs.saveEntry(path, entry)
  101. }
  102. func (wfs *WFS) setRootAttr(out *fuse.AttrOut) {
  103. now := uint64(time.Now().Unix())
  104. out.AttrValid = 119
  105. out.Ino = 1
  106. setBlksize(&out.Attr, blockSize)
  107. out.Uid = wfs.option.MountUid
  108. out.Gid = wfs.option.MountGid
  109. out.Mtime = now
  110. out.Ctime = now
  111. out.Atime = now
  112. out.Mode = toSyscallType(os.ModeDir) | uint32(wfs.option.MountMode)
  113. out.Nlink = 1
  114. }
  115. func (wfs *WFS) setAttrByPbEntry(out *fuse.Attr, inode uint64, entry *filer_pb.Entry) {
  116. out.Ino = inode
  117. out.Size = filer.FileSize(entry)
  118. out.Blocks = (out.Size + blockSize - 1) / blockSize
  119. setBlksize(out, blockSize)
  120. out.Mtime = uint64(entry.Attributes.Mtime)
  121. out.Ctime = uint64(entry.Attributes.Mtime)
  122. out.Atime = uint64(entry.Attributes.Mtime)
  123. out.Mode = toSyscallMode(os.FileMode(entry.Attributes.FileMode))
  124. if entry.HardLinkCounter > 0 {
  125. out.Nlink = uint32(entry.HardLinkCounter)
  126. } else {
  127. out.Nlink = 1
  128. }
  129. out.Uid = entry.Attributes.Uid
  130. out.Gid = entry.Attributes.Gid
  131. out.Rdev = entry.Attributes.Rdev
  132. }
  133. func (wfs *WFS) setAttrByFilerEntry(out *fuse.Attr, inode uint64, entry *filer.Entry) {
  134. out.Ino = inode
  135. out.Size = entry.FileSize
  136. out.Blocks = (out.Size + blockSize - 1) / blockSize
  137. setBlksize(out, blockSize)
  138. out.Atime = uint64(entry.Attr.Mtime.Unix())
  139. out.Mtime = uint64(entry.Attr.Mtime.Unix())
  140. out.Ctime = uint64(entry.Attr.Mtime.Unix())
  141. out.Mode = toSyscallMode(entry.Attr.Mode)
  142. if entry.HardLinkCounter > 0 {
  143. out.Nlink = uint32(entry.HardLinkCounter)
  144. } else {
  145. out.Nlink = 1
  146. }
  147. out.Uid = entry.Attr.Uid
  148. out.Gid = entry.Attr.Gid
  149. out.Rdev = entry.Attr.Rdev
  150. }
  151. func (wfs *WFS) outputPbEntry(out *fuse.EntryOut, inode uint64, entry *filer_pb.Entry) {
  152. out.NodeId = inode
  153. out.Generation = 1
  154. out.EntryValid = 1
  155. out.AttrValid = 1
  156. wfs.setAttrByPbEntry(&out.Attr, inode, entry)
  157. }
  158. func (wfs *WFS) outputFilerEntry(out *fuse.EntryOut, inode uint64, entry *filer.Entry) {
  159. out.NodeId = inode
  160. out.Generation = 1
  161. out.EntryValid = 1
  162. out.AttrValid = 1
  163. wfs.setAttrByFilerEntry(&out.Attr, inode, entry)
  164. }
  165. func chmod(existing uint32, mode uint32) uint32 {
  166. return existing&^07777 | mode&07777
  167. }
  168. func toSyscallMode(mode os.FileMode) uint32 {
  169. return toSyscallType(mode) | uint32(mode)
  170. }
  171. func toSyscallType(mode os.FileMode) uint32 {
  172. switch mode & os.ModeType {
  173. case os.ModeDir:
  174. return syscall.S_IFDIR
  175. case os.ModeSymlink:
  176. return syscall.S_IFLNK
  177. case os.ModeNamedPipe:
  178. return syscall.S_IFIFO
  179. case os.ModeSocket:
  180. return syscall.S_IFSOCK
  181. case os.ModeDevice:
  182. return syscall.S_IFBLK
  183. case os.ModeCharDevice:
  184. return syscall.S_IFCHR
  185. default:
  186. return syscall.S_IFREG
  187. }
  188. }
  189. func toOsFileType(mode uint32) os.FileMode {
  190. switch mode & (syscall.S_IFMT & 0xffff) {
  191. case syscall.S_IFDIR:
  192. return os.ModeDir
  193. case syscall.S_IFLNK:
  194. return os.ModeSymlink
  195. case syscall.S_IFIFO:
  196. return os.ModeNamedPipe
  197. case syscall.S_IFSOCK:
  198. return os.ModeSocket
  199. case syscall.S_IFBLK:
  200. return os.ModeDevice
  201. case syscall.S_IFCHR:
  202. return os.ModeCharDevice
  203. default:
  204. return 0
  205. }
  206. }
  207. func toOsFileMode(mode uint32) os.FileMode {
  208. return toOsFileType(mode) | os.FileMode(mode&07777)
  209. }