weedfs_attr.go 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  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, true)
  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.GetEntry(), true)
  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.GetChunks()))
  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.GetChunks() {
  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.entryChunkGroup.SetChunks(chunks)
  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. size, includeSize := input.GetSize()
  103. if includeSize {
  104. out.Attr.Size = size
  105. }
  106. wfs.setAttrByPbEntry(&out.Attr, input.NodeId, entry, !includeSize)
  107. if fh != nil {
  108. fh.dirtyMetadata = true
  109. return fuse.OK
  110. }
  111. return wfs.saveEntry(path, entry)
  112. }
  113. func (wfs *WFS) setRootAttr(out *fuse.AttrOut) {
  114. now := uint64(time.Now().Unix())
  115. out.AttrValid = 119
  116. out.Ino = 1
  117. setBlksize(&out.Attr, blockSize)
  118. out.Uid = wfs.option.MountUid
  119. out.Gid = wfs.option.MountGid
  120. out.Mtime = now
  121. out.Ctime = now
  122. out.Atime = now
  123. out.Mode = toSyscallType(os.ModeDir) | uint32(wfs.option.MountMode)
  124. out.Nlink = 1
  125. }
  126. func (wfs *WFS) setAttrByPbEntry(out *fuse.Attr, inode uint64, entry *filer_pb.Entry, calculateSize bool) {
  127. out.Ino = inode
  128. out.Blocks = (out.Size + blockSize - 1) / blockSize
  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.Mtime = uint64(entry.Attributes.Mtime)
  143. out.Ctime = uint64(entry.Attributes.Mtime)
  144. out.Atime = uint64(entry.Attributes.Mtime)
  145. out.Mode = toSyscallMode(os.FileMode(entry.Attributes.FileMode))
  146. if entry.HardLinkCounter > 0 {
  147. out.Nlink = uint32(entry.HardLinkCounter)
  148. } else {
  149. out.Nlink = 1
  150. }
  151. out.Uid = entry.Attributes.Uid
  152. out.Gid = entry.Attributes.Gid
  153. out.Rdev = entry.Attributes.Rdev
  154. }
  155. func (wfs *WFS) setAttrByFilerEntry(out *fuse.Attr, inode uint64, entry *filer.Entry) {
  156. out.Ino = inode
  157. out.Size = entry.FileSize
  158. if entry.Mode&os.ModeSymlink != 0 {
  159. out.Size = uint64(len(entry.SymlinkTarget))
  160. }
  161. out.Blocks = (out.Size + blockSize - 1) / blockSize
  162. setBlksize(out, blockSize)
  163. out.Atime = uint64(entry.Attr.Mtime.Unix())
  164. out.Mtime = uint64(entry.Attr.Mtime.Unix())
  165. out.Ctime = uint64(entry.Attr.Mtime.Unix())
  166. out.Mode = toSyscallMode(entry.Attr.Mode)
  167. if entry.HardLinkCounter > 0 {
  168. out.Nlink = uint32(entry.HardLinkCounter)
  169. } else {
  170. out.Nlink = 1
  171. }
  172. out.Uid = entry.Attr.Uid
  173. out.Gid = entry.Attr.Gid
  174. out.Rdev = entry.Attr.Rdev
  175. }
  176. func (wfs *WFS) outputPbEntry(out *fuse.EntryOut, inode uint64, entry *filer_pb.Entry) {
  177. out.NodeId = inode
  178. out.Generation = 1
  179. out.EntryValid = 1
  180. out.AttrValid = 1
  181. wfs.setAttrByPbEntry(&out.Attr, inode, entry, true)
  182. }
  183. func (wfs *WFS) outputFilerEntry(out *fuse.EntryOut, inode uint64, entry *filer.Entry) {
  184. out.NodeId = inode
  185. out.Generation = 1
  186. out.EntryValid = 1
  187. out.AttrValid = 1
  188. wfs.setAttrByFilerEntry(&out.Attr, inode, entry)
  189. }
  190. func chmod(existing uint32, mode uint32) uint32 {
  191. return existing&^07777 | mode&07777
  192. }
  193. func toSyscallMode(mode os.FileMode) uint32 {
  194. return toSyscallType(mode) | uint32(mode)
  195. }
  196. func toSyscallType(mode os.FileMode) uint32 {
  197. switch mode & os.ModeType {
  198. case os.ModeDir:
  199. return syscall.S_IFDIR
  200. case os.ModeSymlink:
  201. return syscall.S_IFLNK
  202. case os.ModeNamedPipe:
  203. return syscall.S_IFIFO
  204. case os.ModeSocket:
  205. return syscall.S_IFSOCK
  206. case os.ModeDevice:
  207. return syscall.S_IFBLK
  208. case os.ModeCharDevice:
  209. return syscall.S_IFCHR
  210. default:
  211. return syscall.S_IFREG
  212. }
  213. }
  214. func toOsFileType(mode uint32) os.FileMode {
  215. switch mode & (syscall.S_IFMT & 0xffff) {
  216. case syscall.S_IFDIR:
  217. return os.ModeDir
  218. case syscall.S_IFLNK:
  219. return os.ModeSymlink
  220. case syscall.S_IFIFO:
  221. return os.ModeNamedPipe
  222. case syscall.S_IFSOCK:
  223. return os.ModeSocket
  224. case syscall.S_IFBLK:
  225. return os.ModeDevice
  226. case syscall.S_IFCHR:
  227. return os.ModeCharDevice
  228. default:
  229. return 0
  230. }
  231. }
  232. func toOsFileMode(mode uint32) os.FileMode {
  233. return toOsFileType(mode) | os.FileMode(mode&07777)
  234. }