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
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"io"
|
2016-05-14 04:57:04 -04:00
|
|
|
"sync"
|
2016-03-28 00:52:38 -04:00
|
|
|
)
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// ReadFile - decoded erasure coded file.
|
2016-05-24 10:44:32 -04:00
|
|
|
func (e erasure) ReadFile(volume, path string, startOffset int64, totalSize int64) (io.ReadCloser, error) {
|
2016-03-28 00:52:38 -04:00
|
|
|
// Input validation.
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return nil, errInvalidArgument
|
|
|
|
}
|
|
|
|
if !isValidPath(path) {
|
|
|
|
return nil, errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2016-05-24 10:44:32 -04:00
|
|
|
var rwg = &sync.WaitGroup{}
|
2016-05-25 19:42:31 -04:00
|
|
|
var errs = make([]error, len(e.storageDisks))
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
readers := make([]io.ReadCloser, len(e.storageDisks))
|
|
|
|
for index, disk := range e.storageDisks {
|
2016-05-25 19:42:31 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-24 10:44:32 -04:00
|
|
|
rwg.Add(1)
|
2016-05-20 23:48:47 -04:00
|
|
|
go func(index int, disk StorageAPI) {
|
2016-05-24 10:44:32 -04:00
|
|
|
defer rwg.Done()
|
2016-05-20 23:48:47 -04:00
|
|
|
offset := int64(0)
|
|
|
|
if reader, err := disk.ReadFile(volume, path, offset); err == nil {
|
|
|
|
readers[index] = reader
|
2016-05-25 19:42:31 -04:00
|
|
|
} else {
|
|
|
|
errs[index] = err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
}(index, disk)
|
2016-04-22 12:04:17 -04:00
|
|
|
}
|
|
|
|
|
2016-05-24 10:44:32 -04:00
|
|
|
// Wait for all readers.
|
|
|
|
rwg.Wait()
|
2016-03-28 00:52:38 -04:00
|
|
|
|
2016-05-25 19:42:31 -04:00
|
|
|
// For any errors in reader, we should just error out.
|
|
|
|
for _, err := range errs {
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Initialize pipe.
|
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
go func() {
|
2016-05-24 10:44:32 -04:00
|
|
|
var totalLeft = totalSize
|
2016-05-20 23:48:47 -04:00
|
|
|
// Read until EOF.
|
2016-05-24 10:44:32 -04:00
|
|
|
for totalLeft > 0 {
|
|
|
|
// Figure out the right blockSize as it was encoded
|
|
|
|
// before.
|
|
|
|
var curBlockSize int64
|
|
|
|
if erasureBlockSize < totalLeft {
|
|
|
|
curBlockSize = erasureBlockSize
|
|
|
|
} else {
|
|
|
|
curBlockSize = totalLeft
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
// Calculate the current encoded block size.
|
2016-05-24 10:44:32 -04:00
|
|
|
curEncBlockSize := getEncodedBlockLen(curBlockSize, e.DataBlocks)
|
2016-05-20 23:48:47 -04:00
|
|
|
enBlocks := make([][]byte, len(e.storageDisks))
|
2016-05-24 10:44:32 -04:00
|
|
|
// Read all the readers.
|
2016-03-28 00:52:38 -04:00
|
|
|
for index, reader := range readers {
|
2016-04-20 11:32:47 -04:00
|
|
|
// Initialize shard slice and fill the data from each parts.
|
|
|
|
enBlocks[index] = make([]byte, curEncBlockSize)
|
2016-03-28 00:52:38 -04:00
|
|
|
if reader == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
// Read the necessary blocks.
|
2016-05-24 10:44:32 -04:00
|
|
|
_, rErr := io.ReadFull(reader, enBlocks[index])
|
2016-05-20 23:48:47 -04:00
|
|
|
if rErr != nil && rErr != io.ErrUnexpectedEOF {
|
|
|
|
readers[index].Close()
|
|
|
|
readers[index] = nil
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Check blocks if they are all zero in length.
|
|
|
|
if checkBlockSize(enBlocks) == 0 {
|
2016-04-25 02:12:54 -04:00
|
|
|
pipeWriter.CloseWithError(errDataCorrupt)
|
2016-03-28 00:52:38 -04:00
|
|
|
return
|
|
|
|
}
|
2016-04-19 15:30:10 -04:00
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Verify the blocks.
|
2016-05-20 23:48:47 -04:00
|
|
|
ok, err := e.ReedSolomon.Verify(enBlocks)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 15:30:10 -04:00
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Verification failed, blocks require reconstruction.
|
|
|
|
if !ok {
|
2016-04-20 11:32:47 -04:00
|
|
|
for index, reader := range readers {
|
|
|
|
if reader == nil {
|
|
|
|
// Reconstruct expects missing blocks to be nil.
|
|
|
|
enBlocks[index] = nil
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
err = e.ReedSolomon.Reconstruct(enBlocks)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Verify reconstructed blocks again.
|
2016-05-20 23:48:47 -04:00
|
|
|
ok, err = e.ReedSolomon.Verify(enBlocks)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !ok {
|
|
|
|
// Blocks cannot be reconstructed, corrupted data.
|
|
|
|
err = errors.New("Verification failed after reconstruction, data likely corrupted.")
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-04-19 15:30:10 -04:00
|
|
|
|
2016-05-06 19:25:08 -04:00
|
|
|
// Get all the data blocks.
|
2016-05-24 10:44:32 -04:00
|
|
|
dataBlocks := getDataBlocks(enBlocks, e.DataBlocks, int(curBlockSize))
|
2016-05-06 19:25:08 -04:00
|
|
|
|
2016-05-24 10:44:32 -04:00
|
|
|
// Verify if the offset is right for the block, if not move to the next block.
|
2016-05-06 19:25:08 -04:00
|
|
|
if startOffset > 0 {
|
|
|
|
startOffset = startOffset - int64(len(dataBlocks))
|
2016-05-08 18:39:24 -04:00
|
|
|
// Start offset is greater than or equal to zero, skip the dataBlocks.
|
|
|
|
if startOffset >= 0 {
|
2016-05-24 10:44:32 -04:00
|
|
|
totalLeft = totalLeft - erasureBlockSize
|
2016-05-06 19:25:08 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-05-24 20:48:58 -04:00
|
|
|
// Now get back the remaining offset if startOffset is negative.
|
2016-05-06 19:25:08 -04:00
|
|
|
startOffset = startOffset + int64(len(dataBlocks))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write safely the necessary blocks.
|
|
|
|
_, err = pipeWriter.Write(dataBlocks[int(startOffset):])
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
|
|
|
pipeWriter.CloseWithError(err)
|
|
|
|
return
|
|
|
|
}
|
2016-04-19 15:30:10 -04:00
|
|
|
|
2016-05-24 22:25:51 -04:00
|
|
|
// Reset dataBlocks to relenquish memory.
|
|
|
|
dataBlocks = nil
|
|
|
|
|
2016-05-06 19:25:08 -04:00
|
|
|
// Reset offset to '0' to read rest of the blocks.
|
|
|
|
startOffset = int64(0)
|
2016-05-24 10:44:32 -04:00
|
|
|
|
|
|
|
// Save what's left after reading erasureBlockSize.
|
|
|
|
totalLeft = totalLeft - erasureBlockSize
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cleanly end the pipe after a successful decoding.
|
|
|
|
pipeWriter.Close()
|
|
|
|
|
|
|
|
// Cleanly close all the underlying data readers.
|
|
|
|
for _, reader := range readers {
|
|
|
|
if reader == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
reader.Close()
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Return the pipe for the top level caller to start reading.
|
|
|
|
return pipeReader, nil
|
|
|
|
}
|