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.

206 lines
5.2 KiB

7 years ago
5 years ago
5 years ago
6 years ago
4 years ago
4 years ago
5 years ago
4 years ago
5 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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.TieredChunkCache
  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.NewTieredChunkCache(256, cacheDir, option.CacheSizeMB)
  74. }
  75. wfs.metaCache = meta_cache.NewMetaCache(path.Join(cacheDir, "meta"))
  76. startTime := time.Now()
  77. go meta_cache.SubscribeMetaEvents(wfs.metaCache, wfs, wfs.option.FilerMountRootPath, startTime.UnixNano())
  78. grace.OnInterrupt(func() {
  79. wfs.metaCache.Shutdown()
  80. })
  81. wfs.root = &Dir{name: wfs.option.FilerMountRootPath, wfs: wfs}
  82. wfs.fsNodeCache = newFsCache(wfs.root)
  83. return wfs
  84. }
  85. func (wfs *WFS) Root() (fs.Node, error) {
  86. return wfs.root, nil
  87. }
  88. func (wfs *WFS) AcquireHandle(file *File, uid, gid uint32) (fileHandle *FileHandle) {
  89. fullpath := file.fullpath()
  90. glog.V(4).Infof("AcquireHandle %s uid=%d gid=%d", fullpath, uid, gid)
  91. wfs.handlesLock.Lock()
  92. defer wfs.handlesLock.Unlock()
  93. inodeId := file.fullpath().AsInode()
  94. existingHandle, found := wfs.handles[inodeId]
  95. if found && existingHandle != nil {
  96. file.isOpen++
  97. return existingHandle
  98. }
  99. fileHandle = newFileHandle(file, uid, gid)
  100. file.maybeLoadEntry(context.Background())
  101. file.isOpen++
  102. wfs.handles[inodeId] = fileHandle
  103. fileHandle.handle = inodeId
  104. return
  105. }
  106. func (wfs *WFS) ReleaseHandle(fullpath util.FullPath, handleId fuse.HandleID) {
  107. wfs.handlesLock.Lock()
  108. defer wfs.handlesLock.Unlock()
  109. glog.V(4).Infof("%s ReleaseHandle id %d current handles length %d", fullpath, handleId, len(wfs.handles))
  110. delete(wfs.handles, fullpath.AsInode())
  111. return
  112. }
  113. // Statfs is called to obtain file system metadata. Implements fuse.FSStatfser
  114. func (wfs *WFS) Statfs(ctx context.Context, req *fuse.StatfsRequest, resp *fuse.StatfsResponse) error {
  115. glog.V(5).Infof("reading fs stats: %+v", req)
  116. if wfs.stats.lastChecked < time.Now().Unix()-20 {
  117. err := wfs.WithFilerClient(func(client filer_pb.SeaweedFilerClient) error {
  118. request := &filer_pb.StatisticsRequest{
  119. Collection: wfs.option.Collection,
  120. Replication: wfs.option.Replication,
  121. Ttl: fmt.Sprintf("%ds", wfs.option.TtlSec),
  122. }
  123. glog.V(5).Infof("reading filer stats: %+v", request)
  124. resp, err := client.Statistics(context.Background(), request)
  125. if err != nil {
  126. glog.V(0).Infof("reading filer stats %v: %v", request, err)
  127. return err
  128. }
  129. glog.V(5).Infof("read filer stats: %+v", resp)
  130. wfs.stats.TotalSize = resp.TotalSize
  131. wfs.stats.UsedSize = resp.UsedSize
  132. wfs.stats.FileCount = resp.FileCount
  133. wfs.stats.lastChecked = time.Now().Unix()
  134. return nil
  135. })
  136. if err != nil {
  137. glog.V(0).Infof("filer Statistics: %v", err)
  138. return err
  139. }
  140. }
  141. totalDiskSize := wfs.stats.TotalSize
  142. usedDiskSize := wfs.stats.UsedSize
  143. actualFileCount := wfs.stats.FileCount
  144. // Compute the total number of available blocks
  145. resp.Blocks = totalDiskSize / blockSize
  146. // Compute the number of used blocks
  147. numBlocks := uint64(usedDiskSize / blockSize)
  148. // Report the number of free and available blocks for the block size
  149. resp.Bfree = resp.Blocks - numBlocks
  150. resp.Bavail = resp.Blocks - numBlocks
  151. resp.Bsize = uint32(blockSize)
  152. // Report the total number of possible files in the file system (and those free)
  153. resp.Files = math.MaxInt64
  154. resp.Ffree = math.MaxInt64 - actualFileCount
  155. // Report the maximum length of a name and the minimum fragment size
  156. resp.Namelen = 1024
  157. resp.Frsize = uint32(blockSize)
  158. return nil
  159. }