weedfs_attr.go 6.6 KB

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