2019-12-16 13:49:58 +00:00
|
|
|
package sectorbuilder
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
)
|
|
|
|
|
2019-12-16 18:49:32 +00:00
|
|
|
type dataType string
|
2019-12-16 13:49:58 +00:00
|
|
|
|
|
|
|
const (
|
2019-12-16 18:49:32 +00:00
|
|
|
dataCache dataType = "cache"
|
|
|
|
dataStaging dataType = "staging"
|
|
|
|
dataSealed dataType = "sealed"
|
|
|
|
dataUnsealed dataType = "unsealed"
|
2019-12-16 13:49:58 +00:00
|
|
|
)
|
|
|
|
|
2019-12-16 18:49:32 +00:00
|
|
|
var overheadMul = map[dataType]uint64{ // * sectorSize
|
2019-12-16 13:49:58 +00:00
|
|
|
dataCache: 11, // TODO: check if true for 32G sectors
|
|
|
|
dataStaging: 1,
|
|
|
|
dataSealed: 1,
|
|
|
|
dataUnsealed: 1,
|
|
|
|
}
|
|
|
|
|
|
|
|
type fs struct {
|
|
|
|
path string
|
|
|
|
|
|
|
|
// in progress actions
|
|
|
|
|
2019-12-16 18:49:32 +00:00
|
|
|
reserved map[dataType]uint64
|
2019-12-16 13:49:58 +00:00
|
|
|
|
|
|
|
lk sync.Mutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func openFs(dir string) *fs {
|
|
|
|
return &fs{
|
2019-12-16 18:49:32 +00:00
|
|
|
path: dir,
|
|
|
|
reserved: map[dataType]uint64{},
|
2019-12-16 13:49:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fs) init() error {
|
2019-12-16 18:49:32 +00:00
|
|
|
for _, dir := range []string{f.path,
|
|
|
|
f.pathFor(dataCache),
|
|
|
|
f.pathFor(dataStaging),
|
|
|
|
f.pathFor(dataSealed),
|
|
|
|
f.pathFor(dataUnsealed)} {
|
2019-12-16 13:49:58 +00:00
|
|
|
if err := os.Mkdir(dir, 0755); err != nil {
|
|
|
|
if os.IsExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-12-16 18:49:32 +00:00
|
|
|
func (f *fs) pathFor(typ dataType) string {
|
|
|
|
_, found := overheadMul[typ]
|
|
|
|
if !found {
|
|
|
|
panic("unknown data path requested")
|
|
|
|
}
|
2019-12-16 13:49:58 +00:00
|
|
|
|
2019-12-16 18:49:32 +00:00
|
|
|
return filepath.Join(f.path, string(typ))
|
2019-12-16 13:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fs) reservedBytes() int64 {
|
|
|
|
var out int64
|
|
|
|
for _, r := range f.reserved {
|
|
|
|
out += int64(r)
|
|
|
|
}
|
|
|
|
return out
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fs) reserve(typ dataType, size uint64) error {
|
|
|
|
f.lk.Lock()
|
|
|
|
defer f.lk.Unlock()
|
|
|
|
|
|
|
|
var fsstat syscall.Statfs_t
|
|
|
|
|
2019-12-16 18:49:32 +00:00
|
|
|
if err := syscall.Statfs(f.pathFor(typ), &fsstat); err != nil {
|
2019-12-16 13:49:58 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-17 13:47:08 +00:00
|
|
|
fsavail := int64(fsstat.Bavail) * int64(fsstat.Bsize)
|
2019-12-16 13:49:58 +00:00
|
|
|
|
2019-12-17 13:47:08 +00:00
|
|
|
avail := fsavail - f.reservedBytes()
|
2019-12-16 13:49:58 +00:00
|
|
|
|
|
|
|
need := overheadMul[typ] * size
|
|
|
|
|
|
|
|
if int64(need) > avail {
|
2019-12-17 13:47:08 +00:00
|
|
|
return xerrors.Errorf("not enough space in '%s', need %s, available %s (fs: %s, reserved: %s)",
|
|
|
|
f.path,
|
|
|
|
types.NewInt(need).SizeStr(),
|
|
|
|
types.NewInt(uint64(avail)).SizeStr(),
|
|
|
|
types.NewInt(uint64(fsavail)).SizeStr(),
|
2019-12-17 13:47:26 +00:00
|
|
|
types.NewInt(uint64(f.reservedBytes())).SizeStr())
|
2019-12-16 13:49:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
f.reserved[typ] += need
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (f *fs) free(typ dataType, sectorSize uint64) {
|
|
|
|
f.lk.Lock()
|
|
|
|
defer f.lk.Unlock()
|
|
|
|
|
|
|
|
f.reserved[typ] -= overheadMul[typ] * sectorSize
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|