You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
201 lines
5.4 KiB
201 lines
5.4 KiB
![]()
9 years ago
|
/*
|
||
|
* 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"
|
||
|
"fmt"
|
||
|
"io"
|
||
|
slashpath "path"
|
||
![]()
8 years ago
|
|
||
|
"github.com/Sirupsen/logrus"
|
||
![]()
9 years ago
|
)
|
||
|
|
||
|
// ReadFile - read file
|
||
|
func (xl XL) ReadFile(volume, path string, offset int64) (io.ReadCloser, error) {
|
||
|
// Input validation.
|
||
|
if !isValidVolname(volume) {
|
||
|
return nil, errInvalidArgument
|
||
|
}
|
||
|
if !isValidPath(path) {
|
||
|
return nil, errInvalidArgument
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
// Acquire a read lock.
|
||
![]()
8 years ago
|
nsMutex.RLock(volume, path)
|
||
![]()
8 years ago
|
onlineDisks, metadata, heal, err := xl.listOnlineDisks(volume, path)
|
||
![]()
8 years ago
|
nsMutex.RUnlock(volume, path)
|
||
![]()
8 years ago
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
![]()
8 years ago
|
}).Errorf("Get readable disks failed with %s", err)
|
||
![]()
8 years ago
|
return nil, err
|
||
|
}
|
||
|
|
||
![]()
8 years ago
|
if heal {
|
||
![]()
8 years ago
|
// Heal in background safely, since we already have read
|
||
|
// quorum disks. Let the reads continue.
|
||
|
go func() {
|
||
|
if err = xl.healFile(volume, path); err != nil {
|
||
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
|
}).Errorf("healFile failed with %s", err)
|
||
|
return
|
||
|
}
|
||
|
}()
|
||
![]()
8 years ago
|
}
|
||
|
|
||
![]()
8 years ago
|
// Acquire read lock again.
|
||
![]()
8 years ago
|
nsMutex.RLock(volume, path)
|
||
![]()
8 years ago
|
readers := make([]io.ReadCloser, len(xl.storageDisks))
|
||
![]()
8 years ago
|
for index, disk := range onlineDisks {
|
||
![]()
8 years ago
|
if disk == nil {
|
||
|
continue
|
||
|
}
|
||
![]()
8 years ago
|
erasurePart := slashpath.Join(path, fmt.Sprintf("file.%d", index))
|
||
![]()
8 years ago
|
// If disk.ReadFile returns error and we don't have read quorum it will be taken care as
|
||
|
// ReedSolomon.Reconstruct() will fail later.
|
||
|
var reader io.ReadCloser
|
||
|
if reader, err = disk.ReadFile(volume, erasurePart, offset); err == nil {
|
||
|
readers[index] = reader
|
||
![]()
9 years ago
|
}
|
||
|
}
|
||
![]()
8 years ago
|
nsMutex.RUnlock(volume, path)
|
||
![]()
9 years ago
|
|
||
|
// Initialize pipe.
|
||
|
pipeReader, pipeWriter := io.Pipe()
|
||
|
go func() {
|
||
![]()
8 years ago
|
var totalLeft = metadata.Stat.Size
|
||
![]()
9 years ago
|
// Read until the totalLeft.
|
||
|
for totalLeft > 0 {
|
||
|
// Figure out the right blockSize as it was encoded before.
|
||
![]()
8 years ago
|
var curBlockSize int64
|
||
|
if metadata.Erasure.BlockSize < totalLeft {
|
||
|
curBlockSize = metadata.Erasure.BlockSize
|
||
![]()
9 years ago
|
} else {
|
||
![]()
8 years ago
|
curBlockSize = totalLeft
|
||
![]()
9 years ago
|
}
|
||
|
// Calculate the current encoded block size.
|
||
![]()
8 years ago
|
curEncBlockSize := getEncodedBlockLen(curBlockSize, metadata.Erasure.DataBlocks)
|
||
![]()
8 years ago
|
enBlocks := make([][]byte, len(xl.storageDisks))
|
||
![]()
9 years ago
|
// Loop through all readers and read.
|
||
|
for index, reader := range readers {
|
||
![]()
8 years ago
|
// Initialize shard slice and fill the data from each parts.
|
||
|
enBlocks[index] = make([]byte, curEncBlockSize)
|
||
![]()
9 years ago
|
if reader == nil {
|
||
|
continue
|
||
|
}
|
||
|
_, err = io.ReadFull(reader, enBlocks[index])
|
||
|
if err != nil && err != io.ErrUnexpectedEOF {
|
||
![]()
8 years ago
|
readers[index] = nil
|
||
![]()
9 years ago
|
}
|
||
|
}
|
||
|
|
||
|
// Check blocks if they are all zero in length.
|
||
|
if checkBlockSize(enBlocks) == 0 {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
![]()
8 years ago
|
}).Errorf("%s", errDataCorrupt)
|
||
![]()
8 years ago
|
pipeWriter.CloseWithError(errDataCorrupt)
|
||
![]()
9 years ago
|
return
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
// Verify the blocks.
|
||
|
var ok bool
|
||
|
ok, err = xl.ReedSolomon.Verify(enBlocks)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
![]()
8 years ago
|
}).Errorf("ReedSolomon verify failed with %s", err)
|
||
![]()
9 years ago
|
pipeWriter.CloseWithError(err)
|
||
|
return
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
// Verification failed, blocks require reconstruction.
|
||
|
if !ok {
|
||
![]()
8 years ago
|
for index, reader := range readers {
|
||
|
if reader == nil {
|
||
|
// Reconstruct expects missing blocks to be nil.
|
||
|
enBlocks[index] = nil
|
||
|
}
|
||
|
}
|
||
![]()
9 years ago
|
err = xl.ReedSolomon.Reconstruct(enBlocks)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
![]()
8 years ago
|
}).Errorf("ReedSolomon reconstruct failed with %s", err)
|
||
![]()
9 years ago
|
pipeWriter.CloseWithError(err)
|
||
|
return
|
||
|
}
|
||
|
// Verify reconstructed blocks again.
|
||
|
ok, err = xl.ReedSolomon.Verify(enBlocks)
|
||
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
![]()
8 years ago
|
}).Errorf("ReedSolomon verify failed with %s", err)
|
||
![]()
9 years ago
|
pipeWriter.CloseWithError(err)
|
||
|
return
|
||
|
}
|
||
|
if !ok {
|
||
|
// Blocks cannot be reconstructed, corrupted data.
|
||
|
err = errors.New("Verification failed after reconstruction, data likely corrupted.")
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
![]()
8 years ago
|
}).Errorf("%s", err)
|
||
![]()
9 years ago
|
pipeWriter.CloseWithError(err)
|
||
|
return
|
||
|
}
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
// Join the decoded blocks.
|
||
![]()
8 years ago
|
err = xl.ReedSolomon.Join(pipeWriter, enBlocks, int(curBlockSize))
|
||
![]()
9 years ago
|
if err != nil {
|
||
![]()
8 years ago
|
log.WithFields(logrus.Fields{
|
||
|
"volume": volume,
|
||
|
"path": path,
|
||
![]()
8 years ago
|
}).Errorf("ReedSolomon joining decoded blocks failed with %s", err)
|
||
![]()
9 years ago
|
pipeWriter.CloseWithError(err)
|
||
|
return
|
||
|
}
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
// Save what's left after reading erasureBlockSize.
|
||
![]()
8 years ago
|
totalLeft = totalLeft - metadata.Erasure.BlockSize
|
||
![]()
9 years ago
|
}
|
||
![]()
8 years ago
|
|
||
![]()
9 years ago
|
// Cleanly end the pipe after a successful decoding.
|
||
|
pipeWriter.Close()
|
||
|
|
||
|
// Cleanly close all the underlying data readers.
|
||
|
for _, reader := range readers {
|
||
![]()
8 years ago
|
if reader == nil {
|
||
|
continue
|
||
|
}
|
||
![]()
9 years ago
|
reader.Close()
|
||
|
}
|
||
|
}()
|
||
|
|
||
|
// Return the pipe for the top level caller to start reading.
|
||
|
return pipeReader, nil
|
||
|
}
|