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.

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