2021-08-18 21:35:22 -04:00
|
|
|
//go:build linux && !s390x && !arm && !386
|
2020-08-24 15:11:20 -04:00
|
|
|
// +build linux,!s390x,!arm,!386
|
2015-10-17 19:45:42 -04:00
|
|
|
|
2023-07-24 12:30:19 -04:00
|
|
|
// Copyright (c) 2015-2023 MinIO, Inc.
|
2021-04-18 15:41:13 -04:00
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2015-10-17 19:45:42 -04:00
|
|
|
|
|
|
|
package disk
|
|
|
|
|
|
|
|
import (
|
2022-08-16 10:13:49 -04:00
|
|
|
"bufio"
|
2024-02-26 14:34:50 -05:00
|
|
|
"errors"
|
2019-08-13 05:58:43 -04:00
|
|
|
"fmt"
|
2024-02-26 14:34:50 -05:00
|
|
|
"io"
|
2022-08-16 10:13:49 -04:00
|
|
|
"os"
|
2023-09-06 15:37:57 -04:00
|
|
|
"path/filepath"
|
2022-08-16 10:13:49 -04:00
|
|
|
"strconv"
|
|
|
|
"strings"
|
2015-10-17 19:45:42 -04:00
|
|
|
"syscall"
|
2022-08-16 10:13:49 -04:00
|
|
|
|
2023-07-24 12:30:19 -04:00
|
|
|
"github.com/prometheus/procfs/blockdevice"
|
2022-08-16 10:13:49 -04:00
|
|
|
"golang.org/x/sys/unix"
|
2015-10-17 19:45:42 -04:00
|
|
|
)
|
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
// GetInfo returns total and free bytes available in a directory, e.g. `/`.
|
2023-07-31 18:20:48 -04:00
|
|
|
func GetInfo(path string, firstTime bool) (info Info, err error) {
|
2015-10-17 19:45:42 -04:00
|
|
|
s := syscall.Statfs_t{}
|
|
|
|
err = syscall.Statfs(path, &s)
|
|
|
|
if err != nil {
|
2016-01-25 02:03:38 -05:00
|
|
|
return Info{}, err
|
2015-10-17 19:45:42 -04:00
|
|
|
}
|
2020-08-24 15:11:20 -04:00
|
|
|
reservedBlocks := s.Bfree - s.Bavail
|
2017-09-25 21:46:19 -04:00
|
|
|
info = Info{
|
2021-05-24 12:28:19 -04:00
|
|
|
Total: uint64(s.Frsize) * (s.Blocks - reservedBlocks),
|
|
|
|
Free: uint64(s.Frsize) * s.Bavail,
|
|
|
|
Files: s.Files,
|
|
|
|
Ffree: s.Ffree,
|
|
|
|
//nolint:unconvert
|
2021-03-15 09:02:09 -04:00
|
|
|
FSType: getFSType(int64(s.Type)),
|
2017-09-25 21:46:19 -04:00
|
|
|
}
|
2022-08-16 10:13:49 -04:00
|
|
|
|
|
|
|
st := syscall.Stat_t{}
|
|
|
|
err = syscall.Stat(path, &st)
|
|
|
|
if err != nil {
|
|
|
|
return Info{}, err
|
|
|
|
}
|
|
|
|
//nolint:unconvert
|
|
|
|
devID := uint64(st.Dev) // Needed to support multiple GOARCHs
|
|
|
|
info.Major = unix.Major(devID)
|
|
|
|
info.Minor = unix.Minor(devID)
|
2023-07-24 12:30:19 -04:00
|
|
|
|
|
|
|
// Check for overflows.
|
|
|
|
// https://github.com/minio/minio/issues/8035
|
|
|
|
// XFS can show wrong values at times error out
|
|
|
|
// in such scenarios.
|
|
|
|
if info.Free > info.Total {
|
|
|
|
return info, fmt.Errorf("detected free space (%d) > total drive space (%d), fs corruption at (%s). please run 'fsck'", info.Free, info.Total, path)
|
|
|
|
}
|
|
|
|
info.Used = info.Total - info.Free
|
|
|
|
|
2023-07-31 18:20:48 -04:00
|
|
|
if firstTime {
|
|
|
|
bfs, err := blockdevice.NewDefaultFS()
|
|
|
|
if err == nil {
|
2023-09-06 15:37:57 -04:00
|
|
|
devName := ""
|
2023-07-31 18:20:48 -04:00
|
|
|
diskstats, _ := bfs.ProcDiskstats()
|
|
|
|
for _, dstat := range diskstats {
|
|
|
|
// ignore all loop devices
|
|
|
|
if strings.HasPrefix(dstat.DeviceName, "loop") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
if dstat.MajorNumber == info.Major && dstat.MinorNumber == info.Minor {
|
2023-09-06 15:37:57 -04:00
|
|
|
devName = dstat.DeviceName
|
2023-07-31 18:20:48 -04:00
|
|
|
break
|
|
|
|
}
|
2023-07-24 12:30:19 -04:00
|
|
|
}
|
2023-09-06 15:37:57 -04:00
|
|
|
if devName != "" {
|
|
|
|
info.Name = devName
|
|
|
|
qst, err := bfs.SysBlockDeviceQueueStats(devName)
|
|
|
|
if err != nil { // Mostly not found error
|
|
|
|
// Check if there is a parent device:
|
|
|
|
// e.g. if the mount is based on /dev/nvme0n1p1, let's calculate the
|
|
|
|
// real device name (nvme0n1) to get its sysfs information
|
|
|
|
parentDevPath, e := os.Readlink("/sys/class/block/" + devName)
|
|
|
|
if e == nil {
|
|
|
|
parentDev := filepath.Base(filepath.Dir(parentDevPath))
|
|
|
|
qst, err = bfs.SysBlockDeviceQueueStats(parentDev)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err == nil {
|
2023-09-11 17:48:54 -04:00
|
|
|
info.NRRequests = qst.NRRequests
|
2023-09-06 15:37:57 -04:00
|
|
|
rot := qst.Rotational == 1 // Rotational is '1' if the device is HDD
|
|
|
|
info.Rotational = &rot
|
|
|
|
}
|
|
|
|
}
|
2023-07-24 12:30:19 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-25 02:03:38 -05:00
|
|
|
return info, nil
|
2015-10-17 19:45:42 -04:00
|
|
|
}
|
2022-08-16 10:13:49 -04:00
|
|
|
|
2024-02-26 14:34:50 -05:00
|
|
|
// GetDriveStats returns IO stats of the drive by its major:minor
|
|
|
|
func GetDriveStats(major, minor uint32) (iostats IOStats, err error) {
|
|
|
|
return readDriveStats(fmt.Sprintf("/sys/dev/block/%v:%v/stat", major, minor))
|
|
|
|
}
|
2022-08-16 10:13:49 -04:00
|
|
|
|
2024-02-26 14:34:50 -05:00
|
|
|
func readDriveStats(statsFile string) (iostats IOStats, err error) {
|
|
|
|
stats, err := readStat(statsFile)
|
2022-08-16 10:13:49 -04:00
|
|
|
if err != nil {
|
2024-02-26 14:34:50 -05:00
|
|
|
return IOStats{}, err
|
2022-08-16 10:13:49 -04:00
|
|
|
}
|
2024-02-26 14:34:50 -05:00
|
|
|
if len(stats) < 11 {
|
|
|
|
return IOStats{}, fmt.Errorf("found invalid format while reading %v", statsFile)
|
|
|
|
}
|
|
|
|
// refer https://www.kernel.org/doc/Documentation/block/stat.txt
|
|
|
|
iostats = IOStats{
|
|
|
|
ReadIOs: stats[0],
|
|
|
|
ReadMerges: stats[1],
|
|
|
|
ReadSectors: stats[2],
|
|
|
|
ReadTicks: stats[3],
|
|
|
|
WriteIOs: stats[4],
|
|
|
|
WriteMerges: stats[5],
|
|
|
|
WriteSectors: stats[6],
|
|
|
|
WriteTicks: stats[7],
|
|
|
|
CurrentIOs: stats[8],
|
|
|
|
TotalTicks: stats[9],
|
|
|
|
ReqTicks: stats[10],
|
|
|
|
}
|
|
|
|
// as per the doc, only 11 fields are guaranteed
|
|
|
|
// only set if available
|
|
|
|
if len(stats) > 14 {
|
|
|
|
iostats.DiscardIOs = stats[11]
|
|
|
|
iostats.DiscardMerges = stats[12]
|
|
|
|
iostats.DiscardSectors = stats[13]
|
|
|
|
iostats.DiscardTicks = stats[14]
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2022-08-16 10:13:49 -04:00
|
|
|
|
2024-02-26 14:34:50 -05:00
|
|
|
func readStat(fileName string) (stats []uint64, err error) {
|
|
|
|
file, err := os.Open(fileName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2022-08-16 10:13:49 -04:00
|
|
|
}
|
2024-02-26 14:34:50 -05:00
|
|
|
defer file.Close()
|
2022-08-16 10:13:49 -04:00
|
|
|
|
2024-02-26 14:34:50 -05:00
|
|
|
s, err := bufio.NewReader(file).ReadString('\n')
|
|
|
|
if err != nil && !errors.Is(err, io.EOF) {
|
2022-08-16 10:13:49 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2024-02-26 14:34:50 -05:00
|
|
|
statLine := strings.TrimSpace(s)
|
|
|
|
for _, token := range strings.Fields(statLine) {
|
|
|
|
ui64, err := strconv.ParseUint(token, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
stats = append(stats, ui64)
|
|
|
|
}
|
2022-08-16 10:13:49 -04:00
|
|
|
|
2024-02-26 14:34:50 -05:00
|
|
|
return stats, nil
|
2022-08-16 10:13:49 -04:00
|
|
|
}
|