wfs.go 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. package filesys
  2. import (
  3. "context"
  4. "fmt"
  5. "math"
  6. "os"
  7. "path"
  8. "sync"
  9. "time"
  10. "google.golang.org/grpc"
  11. "github.com/chrislusf/seaweedfs/weed/util/grace"
  12. "github.com/seaweedfs/fuse"
  13. "github.com/seaweedfs/fuse/fs"
  14. "github.com/chrislusf/seaweedfs/weed/filesys/meta_cache"
  15. "github.com/chrislusf/seaweedfs/weed/glog"
  16. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  17. "github.com/chrislusf/seaweedfs/weed/util"
  18. "github.com/chrislusf/seaweedfs/weed/util/chunk_cache"
  19. )
  20. type Option struct {
  21. FilerGrpcAddress string
  22. GrpcDialOption grpc.DialOption
  23. FilerMountRootPath string
  24. Collection string
  25. Replication string
  26. TtlSec int32
  27. ChunkSizeLimit int64
  28. CacheDir string
  29. CacheSizeMB int64
  30. DataCenter string
  31. EntryCacheTtl time.Duration
  32. Umask os.FileMode
  33. MountUid uint32
  34. MountGid uint32
  35. MountMode os.FileMode
  36. MountCtime time.Time
  37. MountMtime time.Time
  38. OutsideContainerClusterMode bool // whether the mount runs outside SeaweedFS containers
  39. Cipher bool // whether encrypt data on volume server
  40. }
  41. var _ = fs.FS(&WFS{})
  42. var _ = fs.FSStatfser(&WFS{})
  43. type WFS struct {
  44. option *Option
  45. // contains all open handles, protected by handlesLock
  46. handlesLock sync.Mutex
  47. handles map[uint64]*FileHandle
  48. bufPool sync.Pool
  49. stats statsCache
  50. root fs.Node
  51. fsNodeCache *FsCache
  52. chunkCache *chunk_cache.ChunkCache
  53. metaCache *meta_cache.MetaCache
  54. }
  55. type statsCache struct {
  56. filer_pb.StatisticsResponse
  57. lastChecked int64 // unix time in seconds
  58. }
  59. func NewSeaweedFileSystem(option *Option) *WFS {
  60. wfs := &WFS{
  61. option: option,
  62. handles: make(map[uint64]*FileHandle),
  63. bufPool: sync.Pool{
  64. New: func() interface{} {
  65. return make([]byte, option.ChunkSizeLimit)
  66. },
  67. },
  68. }
  69. cacheUniqueId := util.Md5String([]byte(option.FilerGrpcAddress + option.FilerMountRootPath + util.Version()))[0:4]
  70. cacheDir := path.Join(option.CacheDir, cacheUniqueId)
  71. if option.CacheSizeMB > 0 {
  72. os.MkdirAll(cacheDir, 0755)
  73. wfs.chunkCache = chunk_cache.NewChunkCache(256, cacheDir, option.CacheSizeMB)
  74. grace.OnInterrupt(func() {
  75. wfs.chunkCache.Shutdown()
  76. })
  77. }
  78. wfs.metaCache = meta_cache.NewMetaCache(path.Join(cacheDir, "meta"))
  79. startTime := time.Now()
  80. go meta_cache.SubscribeMetaEvents(wfs.metaCache, wfs, wfs.option.FilerMountRootPath, startTime.UnixNano())
  81. grace.OnInterrupt(func() {
  82. wfs.metaCache.Shutdown()
  83. })
  84. wfs.root = &Dir{name: wfs.option.FilerMountRootPath, wfs: wfs}
  85. wfs.fsNodeCache = newFsCache(wfs.root)
  86. return wfs
  87. }
  88. func (wfs *WFS) Root() (fs.Node, error) {
  89. return wfs.root, nil
  90. }
  91. func (wfs *WFS) AcquireHandle(file *File, uid, gid uint32) (fileHandle *FileHandle) {
  92. fullpath := file.fullpath()
  93. glog.V(4).Infof("%s AcquireHandle uid=%d gid=%d", fullpath, uid, gid)
  94. wfs.handlesLock.Lock()
  95. defer wfs.handlesLock.Unlock()
  96. inodeId := file.fullpath().AsInode()
  97. existingHandle, found := wfs.handles[inodeId]
  98. if found && existingHandle != nil {
  99. return existingHandle
  100. }
  101. fileHandle = newFileHandle(file, uid, gid)
  102. wfs.handles[inodeId] = fileHandle
  103. fileHandle.handle = inodeId
  104. glog.V(4).Infof("%s new fh %d", fullpath, fileHandle.handle)
  105. return
  106. }
  107. func (wfs *WFS) ReleaseHandle(fullpath util.FullPath, handleId fuse.HandleID) {
  108. wfs.handlesLock.Lock()
  109. defer wfs.handlesLock.Unlock()
  110. glog.V(4).Infof("%s ReleaseHandle id %d current handles length %d", fullpath, handleId, len(wfs.handles))
  111. delete(wfs.handles, fullpath.AsInode())
  112. return
  113. }
  114. // Statfs is called to obtain file system metadata. Implements fuse.FSStatfser
  115. func (wfs *WFS) Statfs(ctx context.Context, req *fuse.StatfsRequest, resp *fuse.StatfsResponse) error {
  116. glog.V(4).Infof("reading fs stats: %+v", req)
  117. if wfs.stats.lastChecked < time.Now().Unix()-20 {
  118. err := wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  119. request := &filer_pb.StatisticsRequest{
  120. Collection: wfs.option.Collection,
  121. Replication: wfs.option.Replication,
  122. Ttl: fmt.Sprintf("%ds", wfs.option.TtlSec),
  123. }
  124. glog.V(4).Infof("reading filer stats: %+v", request)
  125. resp, err := client.Statistics(context.Background(), request)
  126. if err != nil {
  127. glog.V(0).Infof("reading filer stats %v: %v", request, err)
  128. return err
  129. }
  130. glog.V(4).Infof("read filer stats: %+v", resp)
  131. wfs.stats.TotalSize = resp.TotalSize
  132. wfs.stats.UsedSize = resp.UsedSize
  133. wfs.stats.FileCount = resp.FileCount
  134. wfs.stats.lastChecked = time.Now().Unix()
  135. return nil
  136. })
  137. if err != nil {
  138. glog.V(0).Infof("filer Statistics: %v", err)
  139. return err
  140. }
  141. }
  142. totalDiskSize := wfs.stats.TotalSize
  143. usedDiskSize := wfs.stats.UsedSize
  144. actualFileCount := wfs.stats.FileCount
  145. // Compute the total number of available blocks
  146. resp.Blocks = totalDiskSize / blockSize
  147. // Compute the number of used blocks
  148. numBlocks := uint64(usedDiskSize / blockSize)
  149. // Report the number of free and available blocks for the block size
  150. resp.Bfree = resp.Blocks - numBlocks
  151. resp.Bavail = resp.Blocks - numBlocks
  152. resp.Bsize = uint32(blockSize)
  153. // Report the total number of possible files in the file system (and those free)
  154. resp.Files = math.MaxInt64
  155. resp.Ffree = math.MaxInt64 - actualFileCount
  156. // Report the maximum length of a name and the minimum fragment size
  157. resp.Namelen = 1024
  158. resp.Frsize = uint32(blockSize)
  159. return nil
  160. }