2016-04-22 13:24:04 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2016-04-20 11:32:47 -04:00
|
|
|
|
2016-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-04-20 11:32:47 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
import (
|
2016-06-22 15:55:23 -04:00
|
|
|
"bytes"
|
2018-04-05 18:04:40 -04:00
|
|
|
"context"
|
2016-05-31 23:23:31 -04:00
|
|
|
"io"
|
2018-09-25 15:39:46 -04:00
|
|
|
"strings"
|
2016-05-31 23:23:31 -04:00
|
|
|
|
|
|
|
"github.com/klauspost/reedsolomon"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2016-05-31 23:23:31 -04:00
|
|
|
)
|
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
// getDataBlockLen - get length of data blocks from encoded blocks.
|
|
|
|
func getDataBlockLen(enBlocks [][]byte, dataBlocks int) int {
|
2016-05-29 18:38:14 -04:00
|
|
|
size := 0
|
2016-06-22 15:55:23 -04:00
|
|
|
// Figure out the data block length.
|
|
|
|
for _, block := range enBlocks[:dataBlocks] {
|
2016-05-29 18:38:14 -04:00
|
|
|
size += len(block)
|
|
|
|
}
|
2016-06-22 15:55:23 -04:00
|
|
|
return size
|
|
|
|
}
|
|
|
|
|
|
|
|
// Writes all the data blocks from encoded blocks until requested
|
|
|
|
// outSize length. Provides a way to skip bytes until the offset.
|
2018-04-05 18:04:40 -04:00
|
|
|
func writeDataBlocks(ctx context.Context, dst io.Writer, enBlocks [][]byte, dataBlocks int, offset int64, length int64) (int64, error) {
|
2016-07-07 04:30:34 -04:00
|
|
|
// Offset and out size cannot be negative.
|
2016-07-20 04:30:30 -04:00
|
|
|
if offset < 0 || length < 0 {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errUnexpected)
|
|
|
|
return 0, errUnexpected
|
2016-07-07 04:30:34 -04:00
|
|
|
}
|
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
// Do we have enough blocks?
|
|
|
|
if len(enBlocks) < dataBlocks {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, reedsolomon.ErrTooFewShards)
|
|
|
|
return 0, reedsolomon.ErrTooFewShards
|
2016-05-29 18:38:14 -04:00
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-06-22 15:55:23 -04:00
|
|
|
// Do we have enough data?
|
2016-07-20 04:30:30 -04:00
|
|
|
if int64(getDataBlockLen(enBlocks, dataBlocks)) < length {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, reedsolomon.ErrShortData)
|
|
|
|
return 0, reedsolomon.ErrShortData
|
2016-06-22 15:55:23 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Counter to decrement total left to write.
|
2016-07-20 04:30:30 -04:00
|
|
|
write := length
|
2016-06-22 15:55:23 -04:00
|
|
|
|
|
|
|
// Counter to increment total written.
|
2017-02-24 12:20:40 -05:00
|
|
|
var totalWritten int64
|
2016-06-22 15:55:23 -04:00
|
|
|
|
|
|
|
// Write all data blocks to dst.
|
|
|
|
for _, block := range enBlocks[:dataBlocks] {
|
|
|
|
// Skip blocks until we have reached our offset.
|
2016-07-20 04:30:30 -04:00
|
|
|
if offset >= int64(len(block)) {
|
2016-06-22 15:55:23 -04:00
|
|
|
// Decrement offset.
|
2016-07-20 04:30:30 -04:00
|
|
|
offset -= int64(len(block))
|
2016-06-22 15:55:23 -04:00
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
// Skip until offset.
|
2016-07-20 04:30:30 -04:00
|
|
|
block = block[offset:]
|
2016-06-22 15:55:23 -04:00
|
|
|
|
|
|
|
// Reset the offset for next iteration to read everything
|
|
|
|
// from subsequent blocks.
|
2016-07-20 04:30:30 -04:00
|
|
|
offset = 0
|
2016-06-22 15:55:23 -04:00
|
|
|
}
|
|
|
|
// We have written all the blocks, write the last remaining block.
|
|
|
|
if write < int64(len(block)) {
|
|
|
|
n, err := io.Copy(dst, bytes.NewReader(block[:write]))
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return 0, err
|
2016-06-22 15:55:23 -04:00
|
|
|
}
|
|
|
|
totalWritten += n
|
|
|
|
break
|
|
|
|
}
|
|
|
|
// Copy the block.
|
|
|
|
n, err := io.Copy(dst, bytes.NewReader(block))
|
|
|
|
if err != nil {
|
2018-09-25 15:39:46 -04:00
|
|
|
if !strings.Contains(err.Error(), "read/write on closed pipe") {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return 0, err
|
2016-05-29 18:38:14 -04:00
|
|
|
}
|
2016-06-22 15:55:23 -04:00
|
|
|
|
|
|
|
// Decrement output size.
|
|
|
|
write -= n
|
|
|
|
|
|
|
|
// Increment written.
|
|
|
|
totalWritten += n
|
2016-05-06 19:25:08 -04:00
|
|
|
}
|
2016-06-22 15:55:23 -04:00
|
|
|
|
|
|
|
// Success.
|
|
|
|
return totalWritten, nil
|
2016-05-06 19:25:08 -04:00
|
|
|
}
|
2018-08-06 18:14:08 -04:00
|
|
|
|
|
|
|
// Returns shard-file size.
|
|
|
|
func getErasureShardFileSize(blockSize int64, totalLength int64, dataBlocks int) int64 {
|
|
|
|
shardSize := ceilFrac(int64(blockSize), int64(dataBlocks))
|
|
|
|
numShards := totalLength / int64(blockSize)
|
|
|
|
lastBlockSize := totalLength % int64(blockSize)
|
|
|
|
lastShardSize := ceilFrac(lastBlockSize, int64(dataBlocks))
|
|
|
|
return shardSize*numShards + lastShardSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns the endOffset till which bitrotReader should read data using disk.ReadFile()
|
|
|
|
// partOffset, partLength and partSize are values of the object's part file.
|
|
|
|
func getErasureShardFileEndOffset(partOffset int64, partLength int64, partSize int64, erasureBlockSize int64, dataBlocks int) int64 {
|
|
|
|
shardSize := ceilFrac(erasureBlockSize, int64(dataBlocks))
|
|
|
|
shardFileSize := getErasureShardFileSize(erasureBlockSize, partSize, dataBlocks)
|
|
|
|
endShard := (partOffset + int64(partLength)) / erasureBlockSize
|
|
|
|
endOffset := endShard*shardSize + shardSize
|
|
|
|
if endOffset > shardFileSize {
|
|
|
|
endOffset = shardFileSize
|
|
|
|
}
|
|
|
|
return endOffset
|
|
|
|
}
|