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.

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