From 9417614a8e58f1a654a826154c9c6c12d49a908e Mon Sep 17 00:00:00 2001 From: Anis Elleuch Date: Tue, 27 Sep 2016 20:46:38 +0100 Subject: [PATCH] Recalculate free minimum disk space (#2788) * Fix calculating free space disk by using blocks available for unprivileged user * Use fixed minimal free disk space instead of percentage --- cmd/posix.go | 38 ++++++++++++++++++++------------------ pkg/disk/stat_nix.go | 2 +- 2 files changed, 21 insertions(+), 19 deletions(-) diff --git a/cmd/posix.go b/cmd/posix.go index 8bf5bec3b..ef767339a 100644 --- a/cmd/posix.go +++ b/cmd/posix.go @@ -33,15 +33,17 @@ import ( ) const ( - fsMinSpacePercent = 5 - maxAllowedIOError = 5 + fsMinFreeSpace = 1024 * 1024 * 1024 + fsMinFreeInodesPercent = 5 + maxAllowedIOError = 5 ) // posix - implements StorageAPI interface. type posix struct { - ioErrCount int32 // ref: https://golang.org/pkg/sync/atomic/#pkg-note-BUG - diskPath string - minFreeDisk int64 + ioErrCount int32 // ref: https://golang.org/pkg/sync/atomic/#pkg-note-BUG + diskPath string + minFreeSpace int64 + minFreeInodes int64 } var errFaultyDisk = errors.New("Faulty disk") @@ -100,8 +102,9 @@ func newPosix(diskPath string) (StorageAPI, error) { return nil, err } fs := &posix{ - diskPath: diskPath, - minFreeDisk: fsMinSpacePercent, // Minimum 5% disk should be free. + diskPath: diskPath, + minFreeSpace: fsMinFreeSpace, + minFreeInodes: fsMinFreeInodesPercent, } st, err := os.Stat(preparePath(diskPath)) if err != nil { @@ -132,16 +135,15 @@ func getDiskInfo(diskPath string) (di disk.Info, err error) { } // checkDiskFree verifies if disk path has sufficient minimum free disk space and files. -func checkDiskFree(diskPath string, minFreeDisk int64) (err error) { - di, err := getDiskInfo(diskPath) +func (s posix) checkDiskFree() (err error) { + di, err := getDiskInfo(s.diskPath) if err != nil { return err } - // Remove 5% from total space for cumulative disk - // space used for journalling, inodes etc. - availableDiskSpace := (float64(di.Free) / (float64(di.Total) - (0.05 * float64(di.Total)))) * 100 - if int64(availableDiskSpace) <= minFreeDisk { + // Remove 5% from free space for cumulative disk space used for journalling, inodes etc. + availableDiskSpace := float64(di.Free) * 0.95 + if int64(availableDiskSpace) <= s.minFreeSpace { return errDiskFull } @@ -150,8 +152,8 @@ func checkDiskFree(diskPath string, minFreeDisk int64) (err error) { // Allow for the available disk to be separately validate and we will validate inodes only if // total inodes are provided by the underlying filesystem. if di.Files != 0 { - availableFiles := (float64(di.Ffree) / (float64(di.Files) - (0.05 * float64(di.Files)))) * 100 - if int64(availableFiles) <= minFreeDisk { + availableFiles := 100 * float64(di.Ffree) / float64(di.Files) + if int64(availableFiles) <= s.minFreeInodes { return errDiskFull } } @@ -191,7 +193,7 @@ func (s *posix) MakeVol(volume string) (err error) { } // Validate if disk is free. - if err = checkDiskFree(s.diskPath, s.minFreeDisk); err != nil { + if err = s.checkDiskFree(); err != nil { return err } @@ -539,7 +541,7 @@ func (s *posix) AppendFile(volume, path string, buf []byte) (err error) { } // Validate if disk is free. - if err = checkDiskFree(s.diskPath, s.minFreeDisk); err != nil { + if err = s.checkDiskFree(); err != nil { return err } @@ -744,7 +746,7 @@ func (s *posix) RenameFile(srcVolume, srcPath, dstVolume, dstPath string) (err e } // Validate if disk is free. - if err = checkDiskFree(s.diskPath, s.minFreeDisk); err != nil { + if err = s.checkDiskFree(); err != nil { return err } diff --git a/pkg/disk/stat_nix.go b/pkg/disk/stat_nix.go index fcff64be4..1c1a58a01 100644 --- a/pkg/disk/stat_nix.go +++ b/pkg/disk/stat_nix.go @@ -31,7 +31,7 @@ func GetInfo(path string) (info Info, err error) { } info = Info{} info.Total = int64(s.Bsize) * int64(s.Blocks) - info.Free = int64(s.Bsize) * int64(s.Bfree) + info.Free = int64(s.Bsize) * int64(s.Bavail) info.Files = int64(s.Files) info.Ffree = int64(s.Ffree) info.FSType, err = getFSType(path)