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.

87 lines
2.2 KiB

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