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.

85 lines
2.2 KiB

3 years ago
  1. package mount
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/glog"
  6. "github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
  7. "github.com/hanwen/go-fuse/v2/fs"
  8. "github.com/hanwen/go-fuse/v2/fuse"
  9. "math"
  10. "os"
  11. "syscall"
  12. "time"
  13. )
  14. const blockSize = 512
  15. var _ = fs.NodeStatfser(&WFS{})
  16. type statsCache struct {
  17. filer_pb.StatisticsResponse
  18. lastChecked int64 // unix time in seconds
  19. }
  20. func (wfs *WFS) Statfs(ctx context.Context, out *fuse.StatfsOut) syscall.Errno {
  21. glog.V(4).Infof("reading fs stats")
  22. if wfs.stats.lastChecked < time.Now().Unix()-20 {
  23. err := wfs.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  24. request := &filer_pb.StatisticsRequest{
  25. Collection: wfs.option.Collection,
  26. Replication: wfs.option.Replication,
  27. Ttl: fmt.Sprintf("%ds", wfs.option.TtlSec),
  28. DiskType: string(wfs.option.DiskType),
  29. }
  30. glog.V(4).Infof("reading filer stats: %+v", request)
  31. resp, err := client.Statistics(context.Background(), request)
  32. if err != nil {
  33. glog.V(0).Infof("reading filer stats %v: %v", request, err)
  34. return err
  35. }
  36. glog.V(4).Infof("read filer stats: %+v", resp)
  37. wfs.stats.TotalSize = resp.TotalSize
  38. wfs.stats.UsedSize = resp.UsedSize
  39. wfs.stats.FileCount = resp.FileCount
  40. wfs.stats.lastChecked = time.Now().Unix()
  41. return nil
  42. })
  43. if err != nil {
  44. glog.V(0).Infof("filer Statistics: %v", err)
  45. return fs.ToErrno(os.ErrInvalid)
  46. }
  47. }
  48. totalDiskSize := wfs.stats.TotalSize
  49. usedDiskSize := wfs.stats.UsedSize
  50. actualFileCount := wfs.stats.FileCount
  51. // Compute the total number of available blocks
  52. out.Blocks = totalDiskSize / blockSize
  53. // Compute the number of used blocks
  54. numBlocks := uint64(usedDiskSize / blockSize)
  55. // Report the number of free and available blocks for the block size
  56. out.Bfree = out.Blocks - numBlocks
  57. out.Bavail = out.Blocks - numBlocks
  58. out.Bsize = uint32(blockSize)
  59. // Report the total number of possible files in the file system (and those free)
  60. out.Files = math.MaxInt64
  61. out.Ffree = math.MaxInt64 - actualFileCount
  62. // Report the maximum length of a name and the minimum fragment size
  63. out.NameLen = 1024
  64. out.Frsize = uint32(blockSize)
  65. return fs.OK
  66. }