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.

211 lines
5.5 KiB

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