weedfs_attr.go 5.7 KB

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