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.

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