2020-05-14 15:35:38 +00:00
|
|
|
package ffiwrapper
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/binary"
|
|
|
|
"io"
|
|
|
|
"os"
|
2020-06-15 18:19:42 +00:00
|
|
|
"syscall"
|
2020-05-14 15:35:38 +00:00
|
|
|
|
|
|
|
"github.com/detailyang/go-fallocate"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
|
|
|
rlepluslazy "github.com/filecoin-project/go-bitfield/rle"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-05-26 08:25:17 +00:00
|
|
|
|
2020-08-17 13:26:18 +00:00
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/fsutil"
|
|
|
|
"github.com/filecoin-project/lotus/extern/sector-storage/storiface"
|
2020-05-14 15:35:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const veryLargeRle = 1 << 20
|
|
|
|
|
|
|
|
// Sectors can be partially unsealed. We support this by appending a small
|
|
|
|
// trailer to each unsealed sector file containing an RLE+ marking which bytes
|
|
|
|
// in a sector are unsealed, and which are not (holes)
|
|
|
|
|
|
|
|
// unsealed sector files internally have this structure
|
|
|
|
// [unpadded (raw) data][rle+][4B LE length fo the rle+ field]
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
type PartialFile struct {
|
2020-05-28 23:33:00 +00:00
|
|
|
maxPiece abi.PaddedPieceSize
|
2020-05-14 15:35:38 +00:00
|
|
|
|
|
|
|
path string
|
|
|
|
allocated rlepluslazy.RLE
|
|
|
|
|
|
|
|
file *os.File
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:08:51 +00:00
|
|
|
func writeTrailer(maxPieceSize int64, w *os.File, r rlepluslazy.RunIterator) error {
|
2020-05-14 15:35:38 +00:00
|
|
|
trailer, err := rlepluslazy.EncodeRuns(r, nil)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("encoding trailer: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:08:51 +00:00
|
|
|
// maxPieceSize == unpadded(sectorSize) == trailer start
|
|
|
|
if _, err := w.Seek(maxPieceSize, io.SeekStart); err != nil {
|
2020-05-14 15:35:38 +00:00
|
|
|
return xerrors.Errorf("seek to trailer start: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rb, err := w.Write(trailer)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("writing trailer data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := binary.Write(w, binary.LittleEndian, uint32(len(trailer))); err != nil {
|
|
|
|
return xerrors.Errorf("writing trailer length: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-05-26 19:08:51 +00:00
|
|
|
return w.Truncate(maxPieceSize + int64(rb) + 4)
|
2020-05-14 15:35:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
func createPartialFile(maxPieceSize abi.PaddedPieceSize, path string) (*PartialFile, error) {
|
2020-08-16 10:40:35 +00:00
|
|
|
f, err := os.OpenFile(path, os.O_RDWR|os.O_CREATE, 0644) // nolint
|
2020-05-14 15:35:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("openning partial file '%s': %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = func() error {
|
|
|
|
err := fallocate.Fallocate(f, 0, int64(maxPieceSize))
|
2020-06-15 18:19:42 +00:00
|
|
|
if errno, ok := err.(syscall.Errno); ok {
|
|
|
|
if errno == syscall.EOPNOTSUPP || errno == syscall.ENOSYS {
|
|
|
|
log.Warnf("could not allocated space, ignoring: %v", errno)
|
|
|
|
err = nil // log and ignore
|
|
|
|
}
|
|
|
|
}
|
2020-05-14 15:35:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("fallocate '%s': %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writeTrailer(int64(maxPieceSize), f, &rlepluslazy.RunSliceIterator{}); err != nil {
|
|
|
|
return xerrors.Errorf("writing trailer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
2020-08-16 10:40:35 +00:00
|
|
|
_ = f.Close()
|
2020-05-14 15:35:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return nil, xerrors.Errorf("close empty partial file: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
return OpenPartialFile(maxPieceSize, path)
|
2020-05-14 15:35:38 +00:00
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
func OpenPartialFile(maxPieceSize abi.PaddedPieceSize, path string) (*PartialFile, error) {
|
2020-08-16 10:40:35 +00:00
|
|
|
f, err := os.OpenFile(path, os.O_RDWR, 0644) // nolint
|
2020-05-14 15:35:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("openning partial file '%s': %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var rle rlepluslazy.RLE
|
|
|
|
err = func() error {
|
|
|
|
st, err := f.Stat()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("stat '%s': %w", path, err)
|
|
|
|
}
|
|
|
|
if st.Size() < int64(maxPieceSize) {
|
|
|
|
return xerrors.Errorf("sector file '%s' was smaller than the sector size %d < %d", path, st.Size(), maxPieceSize)
|
|
|
|
}
|
|
|
|
// read trailer
|
|
|
|
var tlen [4]byte
|
2020-05-18 22:08:11 +00:00
|
|
|
_, err = f.ReadAt(tlen[:], st.Size()-int64(len(tlen)))
|
2020-05-14 15:35:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("reading trailer length: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// sanity-check the length
|
|
|
|
trailerLen := binary.LittleEndian.Uint32(tlen[:])
|
|
|
|
expectLen := int64(trailerLen) + int64(len(tlen)) + int64(maxPieceSize)
|
|
|
|
if expectLen != st.Size() {
|
2020-08-20 04:49:10 +00:00
|
|
|
return xerrors.Errorf("file '%s' has inconsistent length; has %d bytes; expected %d (%d trailer, %d sector data)", path, st.Size(), expectLen, int64(trailerLen)+int64(len(tlen)), maxPieceSize)
|
2020-05-14 15:35:38 +00:00
|
|
|
}
|
|
|
|
if trailerLen > veryLargeRle {
|
|
|
|
log.Warnf("Partial file '%s' has a VERY large trailer with %d bytes", path, trailerLen)
|
|
|
|
}
|
|
|
|
|
|
|
|
trailerStart := st.Size() - int64(len(tlen)) - int64(trailerLen)
|
|
|
|
if trailerStart != int64(maxPieceSize) {
|
|
|
|
return xerrors.Errorf("expected sector size to equal trailer start index")
|
|
|
|
}
|
|
|
|
|
|
|
|
trailerBytes := make([]byte, trailerLen)
|
|
|
|
_, err = f.ReadAt(trailerBytes, trailerStart)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("reading trailer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rle, err = rlepluslazy.FromBuf(trailerBytes)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("decoding trailer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
it, err := rle.RunIterator()
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting trailer run iterator: %w", err)
|
|
|
|
}
|
|
|
|
|
2020-07-30 04:54:28 +00:00
|
|
|
f, err := rlepluslazy.Fill(it)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("filling bitfield: %w", err)
|
|
|
|
}
|
|
|
|
lastSet, err := rlepluslazy.Count(f)
|
2020-05-14 15:35:38 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("finding last set byte index: %w", err)
|
|
|
|
}
|
2020-07-30 04:54:28 +00:00
|
|
|
|
2020-05-14 15:35:38 +00:00
|
|
|
if lastSet > uint64(maxPieceSize) {
|
|
|
|
return xerrors.Errorf("last set byte at index higher than sector size: %d > %d", lastSet, maxPieceSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}()
|
|
|
|
if err != nil {
|
2020-08-16 10:40:35 +00:00
|
|
|
_ = f.Close()
|
2020-05-14 15:35:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
return &PartialFile{
|
2020-05-14 15:35:38 +00:00
|
|
|
maxPiece: maxPieceSize,
|
|
|
|
path: path,
|
|
|
|
allocated: rle,
|
|
|
|
file: f,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
func (pf *PartialFile) Close() error {
|
2020-05-14 15:35:38 +00:00
|
|
|
return pf.file.Close()
|
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
func (pf *PartialFile) Writer(offset storiface.PaddedByteIndex, size abi.PaddedPieceSize) (io.Writer, error) {
|
2020-05-14 15:35:38 +00:00
|
|
|
if _, err := pf.file.Seek(int64(offset), io.SeekStart); err != nil {
|
|
|
|
return nil, xerrors.Errorf("seek piece start: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
have, err := pf.allocated.RunIterator()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
and, err := rlepluslazy.And(have, pieceRun(offset, size))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := rlepluslazy.Count(and)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c > 0 {
|
|
|
|
log.Warnf("getting partial file writer overwriting %d allocated bytes", c)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pf.file, nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
func (pf *PartialFile) MarkAllocated(offset storiface.PaddedByteIndex, size abi.PaddedPieceSize) error {
|
2020-05-14 15:35:38 +00:00
|
|
|
have, err := pf.allocated.RunIterator()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ored, err := rlepluslazy.Or(have, pieceRun(offset, size))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writeTrailer(int64(pf.maxPiece), pf.file, ored); err != nil {
|
|
|
|
return xerrors.Errorf("writing trailer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
func (pf *PartialFile) Free(offset storiface.PaddedByteIndex, size abi.PaddedPieceSize) error {
|
2020-07-03 19:52:31 +00:00
|
|
|
have, err := pf.allocated.RunIterator()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := fsutil.Deallocate(pf.file, int64(offset), int64(size)); err != nil {
|
|
|
|
return xerrors.Errorf("deallocating: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
s, err := rlepluslazy.Subtract(have, pieceRun(offset, size))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := writeTrailer(int64(pf.maxPiece), pf.file, s); err != nil {
|
|
|
|
return xerrors.Errorf("writing trailer: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
func (pf *PartialFile) Reader(offset storiface.PaddedByteIndex, size abi.PaddedPieceSize) (*os.File, error) {
|
2020-05-14 15:35:38 +00:00
|
|
|
if _, err := pf.file.Seek(int64(offset), io.SeekStart); err != nil {
|
|
|
|
return nil, xerrors.Errorf("seek piece start: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
have, err := pf.allocated.RunIterator()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
and, err := rlepluslazy.And(have, pieceRun(offset, size))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
c, err := rlepluslazy.Count(and)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if c != uint64(size) {
|
2020-05-18 22:08:11 +00:00
|
|
|
log.Warnf("getting partial file reader reading %d unallocated bytes", uint64(size)-c)
|
2020-05-14 15:35:38 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return pf.file, nil
|
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
func (pf *PartialFile) Allocated() (rlepluslazy.RunIterator, error) {
|
2020-05-18 22:08:11 +00:00
|
|
|
return pf.allocated.RunIterator()
|
|
|
|
}
|
|
|
|
|
2021-05-19 11:14:16 +00:00
|
|
|
func (pf *PartialFile) HasAllocated(offset storiface.UnpaddedByteIndex, size abi.UnpaddedPieceSize) (bool, error) {
|
2020-07-30 20:03:43 +00:00
|
|
|
have, err := pf.Allocated()
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2020-07-30 20:38:05 +00:00
|
|
|
u, err := rlepluslazy.And(have, pieceRun(offset.Padded(), size.Padded()))
|
2020-07-30 20:03:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
uc, err := rlepluslazy.Count(u)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return abi.PaddedPieceSize(uc) == size.Padded(), nil
|
|
|
|
}
|
|
|
|
|
2020-05-28 23:33:00 +00:00
|
|
|
func pieceRun(offset storiface.PaddedByteIndex, size abi.PaddedPieceSize) rlepluslazy.RunIterator {
|
2020-05-14 15:35:38 +00:00
|
|
|
var runs []rlepluslazy.Run
|
|
|
|
if offset > 0 {
|
|
|
|
runs = append(runs, rlepluslazy.Run{
|
|
|
|
Val: false,
|
|
|
|
Len: uint64(offset),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
runs = append(runs, rlepluslazy.Run{
|
|
|
|
Val: true,
|
|
|
|
Len: uint64(size),
|
|
|
|
})
|
|
|
|
|
|
|
|
return &rlepluslazy.RunSliceIterator{Runs: runs}
|
|
|
|
}
|