2016-03-28 00:52:38 -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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
import (
|
2016-06-19 16:35:26 -04:00
|
|
|
"bytes"
|
2016-05-31 23:23:31 -04:00
|
|
|
"encoding/hex"
|
|
|
|
"errors"
|
2016-06-19 16:35:26 -04:00
|
|
|
"io"
|
2016-06-01 19:43:31 -04:00
|
|
|
|
|
|
|
"github.com/klauspost/reedsolomon"
|
2016-05-31 23:23:31 -04:00
|
|
|
)
|
2016-03-28 00:52:38 -04:00
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// erasureReadFile - read bytes from erasure coded files and writes to given writer.
|
|
|
|
// Erasure coded files are read block by block as per given erasureInfo and data chunks
|
|
|
|
// are decoded into a data block. Data block is trimmed for given offset and length,
|
|
|
|
// then written to given writer. This function also supports bit-rot detection by
|
|
|
|
// verifying checksum of individual block's checksum.
|
|
|
|
func erasureReadFile(writer io.Writer, disks []StorageAPI, volume string, path string, partName string, eInfos []erasureInfo, offset int64, length int64) (int64, error) {
|
|
|
|
// Total bytes written to writer
|
|
|
|
bytesWritten := int64(0)
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// Gather previously calculated block checksums.
|
|
|
|
blockCheckSums := metaPartBlockChecksums(disks, eInfos, partName)
|
|
|
|
|
|
|
|
// Pick one erasure info.
|
2016-06-02 19:34:15 -04:00
|
|
|
eInfo := pickValidErasureInfo(eInfos)
|
2016-06-01 19:43:31 -04:00
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// Get block info for given offset, length and block size.
|
|
|
|
startBlock, bytesToSkip, endBlock := getBlockInfo(offset, length, eInfo.BlockSize)
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-06-21 00:40:10 -04:00
|
|
|
// Data chunk size on each block.
|
|
|
|
chunkSize := eInfo.BlockSize / int64(eInfo.DataBlocks)
|
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
for block := startBlock; block <= endBlock; block++ {
|
2016-05-31 23:23:31 -04:00
|
|
|
// Allocate encoded blocks up to storage disks.
|
2016-06-01 19:43:31 -04:00
|
|
|
enBlocks := make([][]byte, len(disks))
|
2016-05-31 23:23:31 -04:00
|
|
|
|
|
|
|
// Counter to keep success data blocks.
|
|
|
|
var successDataBlocksCount = 0
|
|
|
|
var noReconstruct bool // Set for no reconstruction.
|
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// Keep how many bytes are read for this block.
|
2016-06-21 00:40:10 -04:00
|
|
|
// In most cases, last block in the file is shorter than chunkSize
|
2016-06-19 16:35:26 -04:00
|
|
|
lastReadSize := int64(0)
|
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Read from all the disks.
|
2016-06-01 19:43:31 -04:00
|
|
|
for index, disk := range disks {
|
|
|
|
blockIndex := eInfo.Distribution[index] - 1
|
|
|
|
if !isValidBlock(disks, volume, path, toDiskIndex(blockIndex, eInfo.Distribution), blockCheckSums) {
|
2016-05-31 23:23:31 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-19 16:35:26 -04:00
|
|
|
|
|
|
|
// Initialize chunk slice and fill the data from each parts.
|
2016-06-21 00:40:10 -04:00
|
|
|
enBlocks[blockIndex] = make([]byte, chunkSize)
|
2016-06-19 16:35:26 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Read the necessary blocks.
|
2016-06-21 00:40:10 -04:00
|
|
|
n, err := disk.ReadFile(volume, path, block*chunkSize, enBlocks[blockIndex])
|
2016-05-31 23:23:31 -04:00
|
|
|
if err != nil {
|
|
|
|
enBlocks[blockIndex] = nil
|
2016-06-21 00:40:10 -04:00
|
|
|
} else if n < chunkSize {
|
|
|
|
// As the data we got is smaller than chunk size, keep only required chunk slice
|
2016-06-19 16:35:26 -04:00
|
|
|
enBlocks[blockIndex] = append([]byte{}, enBlocks[blockIndex][:n]...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remember bytes read at first time.
|
|
|
|
if lastReadSize == 0 {
|
|
|
|
lastReadSize = n
|
|
|
|
}
|
|
|
|
|
|
|
|
// If bytes read is not equal to bytes read lastly, treat it as corrupted chunk.
|
|
|
|
if n != lastReadSize {
|
|
|
|
return bytesWritten, errXLDataCorrupt
|
2016-05-31 23:23:31 -04:00
|
|
|
}
|
2016-06-19 16:35:26 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Verify if we have successfully read all the data blocks.
|
2016-06-01 19:43:31 -04:00
|
|
|
if blockIndex < eInfo.DataBlocks && enBlocks[blockIndex] != nil {
|
2016-05-31 23:23:31 -04:00
|
|
|
successDataBlocksCount++
|
|
|
|
// Set when we have all the data blocks and no
|
|
|
|
// reconstruction is needed, so that we can avoid
|
|
|
|
// erasure reconstruction.
|
2016-06-01 19:43:31 -04:00
|
|
|
noReconstruct = successDataBlocksCount == eInfo.DataBlocks
|
2016-05-31 23:23:31 -04:00
|
|
|
if noReconstruct {
|
|
|
|
// Break out we have read all the data blocks.
|
|
|
|
break
|
|
|
|
}
|
2016-05-28 18:13:15 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-19 15:30:10 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Verify if reconstruction is needed, proceed with reconstruction.
|
|
|
|
if !noReconstruct {
|
2016-06-01 19:43:31 -04:00
|
|
|
err := decodeData(enBlocks, eInfo.DataBlocks, eInfo.ParityBlocks)
|
2016-05-31 23:23:31 -04:00
|
|
|
if err != nil {
|
2016-06-19 16:35:26 -04:00
|
|
|
return bytesWritten, err
|
2016-05-31 23:23:31 -04:00
|
|
|
}
|
2016-05-28 18:13:15 -04:00
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
|
|
|
|
// Get data blocks from encoded blocks.
|
2016-06-19 16:35:26 -04:00
|
|
|
dataBlocks, err := getDataBlocks(enBlocks, eInfo.DataBlocks, int(lastReadSize)*eInfo.DataBlocks)
|
2016-05-28 18:13:15 -04:00
|
|
|
if err != nil {
|
2016-06-19 16:35:26 -04:00
|
|
|
return bytesWritten, err
|
2016-05-29 18:38:14 -04:00
|
|
|
}
|
2016-05-24 10:44:32 -04:00
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// Keep required bytes into buf.
|
|
|
|
buf := dataBlocks
|
2016-05-29 18:38:14 -04:00
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// If this is start block, skip unwanted bytes.
|
|
|
|
if block == startBlock {
|
|
|
|
buf = append([]byte{}, dataBlocks[bytesToSkip:]...)
|
|
|
|
}
|
2016-05-29 18:38:14 -04:00
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// If this is end block, retain only required bytes.
|
|
|
|
if block == endBlock {
|
|
|
|
buf = append([]byte{}, buf[:length-bytesWritten]...)
|
|
|
|
}
|
2016-05-29 18:38:14 -04:00
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// Copy data blocks.
|
|
|
|
var n int64
|
|
|
|
n, err = io.Copy(writer, bytes.NewReader(buf))
|
|
|
|
bytesWritten += int64(n)
|
|
|
|
if err != nil {
|
|
|
|
return bytesWritten, err
|
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
}
|
2016-06-19 16:35:26 -04:00
|
|
|
|
|
|
|
return bytesWritten, nil
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
2016-06-02 04:49:46 -04:00
|
|
|
|
|
|
|
// PartObjectChecksum - returns the checksum for the part name from the checksum slice.
|
|
|
|
func (e erasureInfo) PartObjectChecksum(partName string) checkSumInfo {
|
|
|
|
for _, checksum := range e.Checksum {
|
|
|
|
if checksum.Name == partName {
|
|
|
|
return checksum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return checkSumInfo{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// xlMetaPartBlockChecksums - get block checksums for a given part.
|
|
|
|
func metaPartBlockChecksums(disks []StorageAPI, eInfos []erasureInfo, partName string) (blockCheckSums []checkSumInfo) {
|
|
|
|
for index := range disks {
|
2016-06-02 19:34:15 -04:00
|
|
|
if eInfos[index].IsValid() {
|
|
|
|
// Save the read checksums for a given part.
|
|
|
|
blockCheckSums = append(blockCheckSums, eInfos[index].PartObjectChecksum(partName))
|
|
|
|
} else {
|
|
|
|
blockCheckSums = append(blockCheckSums, checkSumInfo{})
|
|
|
|
}
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
return blockCheckSums
|
|
|
|
}
|
|
|
|
|
|
|
|
// Takes block index and block distribution to get the disk index.
|
2016-06-05 07:25:15 -04:00
|
|
|
func toDiskIndex(blockIdx int, distribution []int) int {
|
2016-06-02 04:49:46 -04:00
|
|
|
// Find out the right disk index for the input block index.
|
|
|
|
for index, blockIndex := range distribution {
|
2016-06-05 07:25:15 -04:00
|
|
|
if blockIndex-1 == blockIdx {
|
|
|
|
return index
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
}
|
2016-06-05 07:25:15 -04:00
|
|
|
return -1
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// isValidBlock - calculates the checksum hash for the block and
|
|
|
|
// validates if its correct returns true for valid cases, false otherwise.
|
2016-06-05 07:25:15 -04:00
|
|
|
func isValidBlock(disks []StorageAPI, volume, path string, diskIndex int, blockCheckSums []checkSumInfo) (ok bool) {
|
|
|
|
ok = false
|
2016-06-02 04:49:46 -04:00
|
|
|
// Unknown block index requested, treat it as error.
|
|
|
|
if diskIndex == -1 {
|
2016-06-05 07:25:15 -04:00
|
|
|
return ok
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
// Disk is not present, treat entire block to be non existent.
|
|
|
|
if disks[diskIndex] == nil {
|
2016-06-05 07:25:15 -04:00
|
|
|
return ok
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
// Read everything for a given block and calculate hash.
|
|
|
|
hashWriter := newHash(blockCheckSums[diskIndex].Algorithm)
|
|
|
|
hashBytes, err := hashSum(disks[diskIndex], volume, path, hashWriter)
|
|
|
|
if err != nil {
|
2016-06-05 07:25:15 -04:00
|
|
|
return ok
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
2016-06-05 07:25:15 -04:00
|
|
|
ok = hex.EncodeToString(hashBytes) == blockCheckSums[diskIndex].Hash
|
|
|
|
return ok
|
2016-06-02 04:49:46 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// decodeData - decode encoded blocks.
|
|
|
|
func decodeData(enBlocks [][]byte, dataBlocks, parityBlocks int) error {
|
|
|
|
rs, err := reedsolomon.New(dataBlocks, parityBlocks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = rs.Reconstruct(enBlocks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Verify reconstructed blocks (parity).
|
|
|
|
ok, err := rs.Verify(enBlocks)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
// Blocks cannot be reconstructed, corrupted data.
|
|
|
|
err = errors.New("Verification failed after reconstruction, data likely corrupted.")
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|