2016-06-01 19:43:31 -04:00
|
|
|
/*
|
2018-03-27 19:44:45 -04:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017, 2018 Minio, Inc.
|
2016-06-01 19:43:31 -04:00
|
|
|
*
|
|
|
|
* 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-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
import (
|
2018-08-27 05:58:23 -04:00
|
|
|
"bytes"
|
2018-03-14 15:01:47 -04:00
|
|
|
"context"
|
2017-10-24 15:25:42 -04:00
|
|
|
"encoding/hex"
|
2016-05-20 23:48:47 -04:00
|
|
|
"io"
|
|
|
|
"path"
|
2017-01-30 18:44:42 -05:00
|
|
|
"strconv"
|
2016-05-20 23:48:47 -04:00
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2017-10-22 01:30:34 -04:00
|
|
|
"github.com/minio/minio/pkg/hash"
|
2016-05-20 23:48:47 -04:00
|
|
|
"github.com/minio/minio/pkg/mimedb"
|
|
|
|
)
|
|
|
|
|
2016-11-20 19:57:12 -05:00
|
|
|
// list all errors which can be ignored in object operations.
|
2017-02-01 14:16:17 -05:00
|
|
|
var objectOpIgnoredErrs = append(baseIgnoredErrs, errDiskAccessDenied)
|
2016-11-20 19:57:12 -05:00
|
|
|
|
2018-01-29 21:43:13 -05:00
|
|
|
// putObjectDir hints the bottom layer to create a new directory.
|
2018-04-05 18:04:40 -04:00
|
|
|
func (xl xlObjects) putObjectDir(ctx context.Context, bucket, object string, writeQuorum int) error {
|
2018-01-29 21:43:13 -05:00
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
errs := make([]error, len(xl.getDisks()))
|
2018-01-29 21:43:13 -05:00
|
|
|
// Prepare object creation in all disks
|
2018-02-15 20:45:57 -05:00
|
|
|
for index, disk := range xl.getDisks() {
|
2018-01-29 21:43:13 -05:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
|
|
|
if err := disk.MakeVol(pathJoin(bucket, object)); err != nil && err != errVolumeExists {
|
|
|
|
errs[index] = err
|
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
return reduceWriteQuorumErrs(ctx, errs, objectOpIgnoredErrs, writeQuorum)
|
2018-01-29 21:43:13 -05:00
|
|
|
}
|
|
|
|
|
2017-03-07 17:48:56 -05:00
|
|
|
// prepareFile hints the bottom layer to optimize the creation of a new object
|
2018-04-05 18:04:40 -04:00
|
|
|
func (xl xlObjects) prepareFile(ctx context.Context, bucket, object string, size int64, onlineDisks []StorageAPI, blockSize int64, dataBlocks, writeQuorum int) error {
|
2017-03-07 17:48:56 -05:00
|
|
|
pErrs := make([]error, len(onlineDisks))
|
|
|
|
// Calculate the real size of the part in one disk.
|
2018-08-06 18:14:08 -04:00
|
|
|
actualSize := getErasureShardFileSize(blockSize, size, dataBlocks)
|
2017-03-07 17:48:56 -05:00
|
|
|
// Prepare object creation in a all disks
|
|
|
|
for index, disk := range onlineDisks {
|
|
|
|
if disk != nil {
|
|
|
|
if err := disk.PrepareFile(bucket, object, actualSize); err != nil {
|
|
|
|
// Save error to reduce it later
|
|
|
|
pErrs[index] = err
|
|
|
|
// Ignore later access to disk which generated the error
|
|
|
|
onlineDisks[index] = nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
return reduceWriteQuorumErrs(ctx, pErrs, objectOpIgnoredErrs, writeQuorum)
|
2017-03-07 17:48:56 -05:00
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
/// Object Operations
|
|
|
|
|
2016-12-26 19:29:26 -05:00
|
|
|
// CopyObject - copy object source object to destination object.
|
|
|
|
// if source object and destination object are same we only
|
|
|
|
// update metadata.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) CopyObject(ctx context.Context, srcBucket, srcObject, dstBucket, dstObject string, srcInfo ObjectInfo) (oi ObjectInfo, e error) {
|
2018-02-23 18:07:21 -05:00
|
|
|
cpSrcDstSame := isStringEqual(pathJoin(srcBucket, srcObject), pathJoin(dstBucket, dstObject))
|
2018-01-12 23:34:52 -05:00
|
|
|
|
2016-12-26 19:29:26 -05:00
|
|
|
// Read metadata associated with the object from all disks.
|
2018-03-02 20:24:02 -05:00
|
|
|
storageDisks := xl.getDisks()
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
metaArr, errs := readAllXLMetadata(ctx, storageDisks, srcBucket, srcObject)
|
2017-12-22 06:28:13 -05:00
|
|
|
|
|
|
|
// get Quorum for this object
|
2018-07-31 03:23:29 -04:00
|
|
|
readQuorum, writeQuorum, err := objectQuorumFromMeta(ctx, xl, metaArr, errs)
|
2017-12-22 06:28:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return oi, toObjectErr(err, srcBucket, srcObject)
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
if reducedErr := reduceReadQuorumErrs(ctx, errs, objectOpIgnoredErrs, readQuorum); reducedErr != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(reducedErr, srcBucket, srcObject)
|
2016-12-26 19:29:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// List all online disks.
|
2018-03-02 20:24:02 -05:00
|
|
|
_, modTime := listOnlineDisks(storageDisks, metaArr, errs)
|
2016-12-26 19:29:26 -05:00
|
|
|
|
|
|
|
// Pick latest valid metadata.
|
2018-08-17 17:42:04 -04:00
|
|
|
xlMeta, err := pickValidXLMeta(ctx, metaArr, modTime, readQuorum)
|
2016-12-26 19:29:26 -05:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, srcBucket, srcObject)
|
2016-12-26 19:29:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Length of the file to read.
|
|
|
|
length := xlMeta.Stat.Size
|
|
|
|
|
|
|
|
// Check if this request is only metadata update.
|
2018-02-23 18:07:21 -05:00
|
|
|
if cpSrcDstSame {
|
2016-12-26 19:29:26 -05:00
|
|
|
// Update `xl.json` content on each disks.
|
2018-03-02 20:24:02 -05:00
|
|
|
for index := range metaArr {
|
|
|
|
metaArr[index].Meta = srcInfo.UserDefined
|
2018-03-09 13:50:39 -05:00
|
|
|
metaArr[index].Meta["etag"] = srcInfo.ETag
|
2016-12-26 19:29:26 -05:00
|
|
|
}
|
|
|
|
|
2018-03-02 20:24:02 -05:00
|
|
|
var onlineDisks []StorageAPI
|
|
|
|
|
2016-12-26 19:29:26 -05:00
|
|
|
tempObj := mustGetUUID()
|
|
|
|
|
|
|
|
// Write unique `xl.json` for each disk.
|
2018-04-05 18:04:40 -04:00
|
|
|
if onlineDisks, err = writeUniqueXLMetadata(ctx, storageDisks, minioMetaTmpBucket, tempObj, metaArr, writeQuorum); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, srcBucket, srcObject)
|
2016-12-26 19:29:26 -05:00
|
|
|
}
|
|
|
|
// Rename atomically `xl.json` from tmp location to destination for each disk.
|
2018-04-05 18:04:40 -04:00
|
|
|
if _, err = renameXLMetadata(ctx, onlineDisks, minioMetaTmpBucket, tempObj, srcBucket, srcObject, writeQuorum); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, srcBucket, srcObject)
|
2016-12-26 19:29:26 -05:00
|
|
|
}
|
2017-05-14 15:05:51 -04:00
|
|
|
return xlMeta.ToObjectInfo(srcBucket, srcObject), nil
|
2016-12-26 19:29:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize pipe.
|
|
|
|
pipeReader, pipeWriter := io.Pipe()
|
|
|
|
|
|
|
|
go func() {
|
2017-02-24 12:20:40 -05:00
|
|
|
var startOffset int64 // Read the whole file.
|
2018-04-05 18:04:40 -04:00
|
|
|
if gerr := xl.getObject(ctx, srcBucket, srcObject, startOffset, length, pipeWriter, srcInfo.ETag); gerr != nil {
|
2016-12-26 19:29:26 -05:00
|
|
|
pipeWriter.CloseWithError(toObjectErr(gerr, srcBucket, srcObject))
|
|
|
|
return
|
|
|
|
}
|
2018-06-28 19:02:02 -04:00
|
|
|
pipeWriter.Close() // Close writer explicitly signaling we wrote all data.
|
2016-12-26 19:29:26 -05:00
|
|
|
}()
|
|
|
|
|
2017-10-22 01:30:34 -04:00
|
|
|
hashReader, err := hash.NewReader(pipeReader, length, "", "")
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return oi, toObjectErr(err, dstBucket, dstObject)
|
2017-10-22 01:30:34 -04:00
|
|
|
}
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
objInfo, err := xl.putObject(ctx, dstBucket, dstObject, hashReader, srcInfo.UserDefined)
|
2016-12-26 19:29:26 -05:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, dstBucket, dstObject)
|
2016-12-26 19:29:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Explicitly close the reader.
|
|
|
|
pipeReader.Close()
|
|
|
|
|
|
|
|
return objInfo, nil
|
|
|
|
}
|
|
|
|
|
2018-08-27 05:58:23 -04:00
|
|
|
func (xl xlObjects) GetObjectNInfo(ctx context.Context, bucket, object string, rs *HTTPRangeSpec) (objInfo ObjectInfo, reader io.ReadCloser, err error) {
|
|
|
|
|
|
|
|
// Acquire lock
|
|
|
|
lock := xl.nsMutex.NewNSLock(bucket, object)
|
|
|
|
if err = lock.GetRLock(globalObjectTimeout); err != nil {
|
|
|
|
return objInfo, nil, err
|
|
|
|
}
|
|
|
|
objReader := &GetObjectReader{
|
|
|
|
lock: lock,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkGetObjArgs(ctx, bucket, object); err != nil {
|
|
|
|
return objInfo, objReader, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if hasSuffix(object, slashSeparator) {
|
|
|
|
if !xl.isObjectDir(bucket, object) {
|
|
|
|
return objInfo, objReader, toObjectErr(errFileNotFound, bucket, object)
|
|
|
|
}
|
|
|
|
var e error
|
|
|
|
if objInfo, e = xl.getObjectInfoDir(ctx, bucket, object); e != nil {
|
|
|
|
return objInfo, objReader, toObjectErr(e, bucket, object)
|
|
|
|
}
|
|
|
|
objReader.pr = bytes.NewBuffer(nil)
|
|
|
|
return objInfo, objReader, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
objInfo, err = xl.getObjectInfo(ctx, bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
return objInfo, objReader, toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
|
|
|
startOffset, readLength := int64(0), objInfo.Size
|
|
|
|
if rs != nil {
|
|
|
|
startOffset, readLength = rs.GetOffsetLength(objInfo.Size)
|
|
|
|
}
|
|
|
|
|
|
|
|
pr, pw := io.Pipe()
|
|
|
|
objReader.pr = pr
|
|
|
|
go func() {
|
|
|
|
err := xl.getObject(ctx, bucket, object, startOffset, readLength, pw, "")
|
|
|
|
pw.CloseWithError(err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
return objInfo, objReader, nil
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// GetObject - reads an object erasured coded across multiple
|
|
|
|
// disks. Supports additional parameters like offset and length
|
2016-12-26 19:29:26 -05:00
|
|
|
// which are synonymous with HTTP Range requests.
|
2016-06-01 19:43:31 -04:00
|
|
|
//
|
2016-12-26 19:29:26 -05:00
|
|
|
// startOffset indicates the starting read location of the object.
|
|
|
|
// length indicates the total length of the object.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) GetObject(ctx context.Context, bucket, object string, startOffset int64, length int64, writer io.Writer, etag string) error {
|
2018-01-12 23:34:52 -05:00
|
|
|
// Lock the object before reading.
|
|
|
|
objectLock := xl.nsMutex.NewNSLock(bucket, object)
|
|
|
|
if err := objectLock.GetRLock(globalObjectTimeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer objectLock.RUnlock()
|
2018-04-05 18:04:40 -04:00
|
|
|
return xl.getObject(ctx, bucket, object, startOffset, length, writer, etag)
|
2018-01-12 23:34:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// getObject wrapper for xl GetObject
|
2018-04-05 18:04:40 -04:00
|
|
|
func (xl xlObjects) getObject(ctx context.Context, bucket, object string, startOffset int64, length int64, writer io.Writer, etag string) error {
|
2018-01-12 23:34:52 -05:00
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
if err := checkGetObjArgs(ctx, bucket, object); err != nil {
|
2016-12-02 02:15:17 -05:00
|
|
|
return err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-12-21 14:29:32 -05:00
|
|
|
|
|
|
|
// Start offset cannot be negative.
|
|
|
|
if startOffset < 0 {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errUnexpected)
|
|
|
|
return errUnexpected
|
2016-07-07 04:30:34 -04:00
|
|
|
}
|
2016-12-21 14:29:32 -05:00
|
|
|
|
2016-07-08 10:46:49 -04:00
|
|
|
// Writer cannot be nil.
|
|
|
|
if writer == nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errUnexpected)
|
|
|
|
return errUnexpected
|
2016-07-08 10:46:49 -04:00
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2018-01-29 21:43:13 -05:00
|
|
|
// If its a directory request, we return an empty body.
|
|
|
|
if hasSuffix(object, slashSeparator) {
|
|
|
|
_, err := writer.Write([]byte(""))
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return toObjectErr(err, bucket, object)
|
2018-01-29 21:43:13 -05:00
|
|
|
}
|
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Read metadata associated with the object from all disks.
|
2018-04-05 18:04:40 -04:00
|
|
|
metaArr, errs := readAllXLMetadata(ctx, xl.getDisks(), bucket, object)
|
2017-12-22 06:28:13 -05:00
|
|
|
|
|
|
|
// get Quorum for this object
|
2018-07-31 03:23:29 -04:00
|
|
|
readQuorum, _, err := objectQuorumFromMeta(ctx, xl, metaArr, errs)
|
2017-12-22 06:28:13 -05:00
|
|
|
if err != nil {
|
|
|
|
return toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
if reducedErr := reduceReadQuorumErrs(ctx, errs, objectOpIgnoredErrs, readQuorum); reducedErr != nil {
|
2016-07-19 22:24:32 -04:00
|
|
|
return toObjectErr(reducedErr, bucket, object)
|
2016-07-09 16:01:32 -04:00
|
|
|
}
|
|
|
|
|
2016-05-25 19:42:31 -04:00
|
|
|
// List all online disks.
|
2018-02-15 20:45:57 -05:00
|
|
|
onlineDisks, modTime := listOnlineDisks(xl.getDisks(), metaArr, errs)
|
2016-05-26 22:55:48 -04:00
|
|
|
|
2016-06-17 01:26:18 -04:00
|
|
|
// Pick latest valid metadata.
|
2018-08-17 17:42:04 -04:00
|
|
|
xlMeta, err := pickValidXLMeta(ctx, metaArr, modTime, readQuorum)
|
2016-11-20 23:56:44 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-07-25 01:49:27 -04:00
|
|
|
|
|
|
|
// Reorder online disks based on erasure distribution order.
|
2017-02-24 12:20:40 -05:00
|
|
|
onlineDisks = shuffleDisks(onlineDisks, xlMeta.Erasure.Distribution)
|
2016-07-25 01:49:27 -04:00
|
|
|
|
|
|
|
// Reorder parts metadata based on erasure distribution order.
|
2017-02-24 12:20:40 -05:00
|
|
|
metaArr = shufflePartsMetadata(metaArr, xlMeta.Erasure.Distribution)
|
2016-05-25 19:42:31 -04:00
|
|
|
|
2016-12-21 14:29:32 -05:00
|
|
|
// For negative length read everything.
|
|
|
|
if length < 0 {
|
|
|
|
length = xlMeta.Stat.Size - startOffset
|
2016-07-08 10:46:49 -04:00
|
|
|
}
|
|
|
|
|
2016-12-21 14:29:32 -05:00
|
|
|
// Reply back invalid range if the input offset and length fall out of range.
|
|
|
|
if startOffset > xlMeta.Stat.Size || startOffset+length > xlMeta.Stat.Size {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, InvalidRange{startOffset, length, xlMeta.Stat.Size})
|
|
|
|
return InvalidRange{startOffset, length, xlMeta.Stat.Size}
|
2016-07-08 10:46:49 -04:00
|
|
|
}
|
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// Get start part index and offset.
|
2018-04-05 18:04:40 -04:00
|
|
|
partIndex, partOffset, err := xlMeta.ObjectToPartOffset(ctx, startOffset)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
return InvalidRange{startOffset, length, xlMeta.Stat.Size}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2017-01-27 13:51:02 -05:00
|
|
|
// Calculate endOffset according to length
|
|
|
|
endOffset := startOffset
|
|
|
|
if length > 0 {
|
|
|
|
endOffset += length - 1
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// Get last part index to read given length.
|
2018-04-05 18:04:40 -04:00
|
|
|
lastPartIndex, _, err := xlMeta.ObjectToPartOffset(ctx, endOffset)
|
2016-06-19 16:35:26 -04:00
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
return InvalidRange{startOffset, length, xlMeta.Stat.Size}
|
2016-06-19 16:35:26 -04:00
|
|
|
}
|
|
|
|
|
2017-02-24 12:20:40 -05:00
|
|
|
var totalBytesRead int64
|
2018-08-24 02:35:37 -04:00
|
|
|
erasure, err := NewErasure(ctx, xlMeta.Erasure.DataBlocks, xlMeta.Erasure.ParityBlocks, xlMeta.Erasure.BlockSize)
|
2017-08-14 21:08:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return toObjectErr(err, bucket, object)
|
|
|
|
}
|
2018-08-06 18:14:08 -04:00
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
for ; partIndex <= lastPartIndex; partIndex++ {
|
2016-06-21 17:34:11 -04:00
|
|
|
if length == totalBytesRead {
|
|
|
|
break
|
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
// Save the current part name and size.
|
|
|
|
partName := xlMeta.Parts[partIndex].Name
|
|
|
|
partSize := xlMeta.Parts[partIndex].Size
|
2016-06-22 12:05:03 -04:00
|
|
|
|
2018-08-06 18:14:08 -04:00
|
|
|
partLength := partSize - partOffset
|
|
|
|
// partLength should be adjusted so that we don't write more data than what was requested.
|
|
|
|
if partLength > (length - totalBytesRead) {
|
|
|
|
partLength = length - totalBytesRead
|
2016-06-19 16:35:26 -04:00
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-07-16 11:35:30 -04:00
|
|
|
// Get the checksums of the current part.
|
2018-08-06 18:14:08 -04:00
|
|
|
bitrotReaders := make([]*bitrotReader, len(onlineDisks))
|
|
|
|
for index, disk := range onlineDisks {
|
2017-08-14 21:08:42 -04:00
|
|
|
if disk == OfflineDisk {
|
2016-07-25 01:49:27 -04:00
|
|
|
continue
|
|
|
|
}
|
2017-08-14 21:08:42 -04:00
|
|
|
checksumInfo := metaArr[index].Erasure.GetChecksumInfo(partName)
|
2018-08-06 18:14:08 -04:00
|
|
|
endOffset := getErasureShardFileEndOffset(partOffset, partLength, partSize, xlMeta.Erasure.BlockSize, xlMeta.Erasure.DataBlocks)
|
|
|
|
bitrotReaders[index] = newBitrotReader(disk, bucket, pathJoin(object, partName), checksumInfo.Algorithm, endOffset, checksumInfo.Hash)
|
2016-07-16 11:35:30 -04:00
|
|
|
}
|
|
|
|
|
2018-08-24 02:35:37 -04:00
|
|
|
err := erasure.Decode(ctx, writer, bitrotReaders, partOffset, partLength, partSize)
|
2016-05-31 23:23:31 -04:00
|
|
|
if err != nil {
|
2016-07-08 23:34:27 -04:00
|
|
|
return toObjectErr(err, bucket, object)
|
2016-05-31 23:23:31 -04:00
|
|
|
}
|
2018-08-06 18:14:08 -04:00
|
|
|
for i, r := range bitrotReaders {
|
|
|
|
if r == nil {
|
|
|
|
onlineDisks[i] = OfflineDisk
|
|
|
|
}
|
|
|
|
}
|
2016-07-28 05:20:34 -04:00
|
|
|
// Track total bytes read from disk and written to the client.
|
2018-08-06 18:14:08 -04:00
|
|
|
totalBytesRead += partLength
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-06-22 12:05:03 -04:00
|
|
|
// partOffset will be valid only for the first part, hence reset it to 0 for
|
|
|
|
// the remaining parts.
|
2016-05-31 23:23:31 -04:00
|
|
|
partOffset = 0
|
2016-06-01 19:43:31 -04:00
|
|
|
} // End of read all parts loop.
|
2016-05-31 23:23:31 -04:00
|
|
|
|
|
|
|
// Return success.
|
2016-05-28 18:13:15 -04:00
|
|
|
return nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2018-01-29 21:43:13 -05:00
|
|
|
// getObjectInfoDir - This getObjectInfo is specific to object directory lookup.
|
2018-04-05 18:04:40 -04:00
|
|
|
func (xl xlObjects) getObjectInfoDir(ctx context.Context, bucket, object string) (oi ObjectInfo, err error) {
|
2018-01-29 21:43:13 -05:00
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
errs := make([]error, len(xl.getDisks()))
|
2018-01-29 21:43:13 -05:00
|
|
|
// Prepare object creation in a all disks
|
2018-02-15 20:45:57 -05:00
|
|
|
for index, disk := range xl.getDisks() {
|
2018-01-29 21:43:13 -05:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
|
|
|
if _, err := disk.StatVol(pathJoin(bucket, object)); err != nil {
|
|
|
|
// Since we are re-purposing StatVol, an object which
|
|
|
|
// is a directory if it doesn't exist should be
|
|
|
|
// returned as errFileNotFound instead, convert
|
|
|
|
// the error right here accordingly.
|
|
|
|
if err == errVolumeNotFound {
|
|
|
|
err = errFileNotFound
|
|
|
|
} else if err == errVolumeAccessDenied {
|
|
|
|
err = errFileAccessDenied
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save error to reduce it later
|
|
|
|
errs[index] = err
|
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
readQuorum := len(xl.getDisks()) / 2
|
2018-04-05 18:04:40 -04:00
|
|
|
return dirObjectInfo(bucket, object, 0, map[string]string{}), reduceReadQuorumErrs(ctx, errs, objectOpIgnoredErrs, readQuorum)
|
2018-01-29 21:43:13 -05:00
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// GetObjectInfo - reads object metadata and replies back ObjectInfo.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) GetObjectInfo(ctx context.Context, bucket, object string) (oi ObjectInfo, e error) {
|
2018-01-12 23:34:52 -05:00
|
|
|
// Lock the object before reading.
|
|
|
|
objectLock := xl.nsMutex.NewNSLock(bucket, object)
|
|
|
|
if err := objectLock.GetRLock(globalObjectTimeout); err != nil {
|
|
|
|
return oi, err
|
|
|
|
}
|
|
|
|
defer objectLock.RUnlock()
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
if err := checkGetObjArgs(ctx, bucket, object); err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2018-01-29 21:43:13 -05:00
|
|
|
if hasSuffix(object, slashSeparator) {
|
2018-05-08 22:08:21 -04:00
|
|
|
if !xl.isObjectDir(bucket, object) {
|
|
|
|
return oi, toObjectErr(errFileNotFound, bucket, object)
|
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
if oi, e = xl.getObjectInfoDir(ctx, bucket, object); e != nil {
|
2018-02-02 03:34:15 -05:00
|
|
|
return oi, toObjectErr(e, bucket, object)
|
|
|
|
}
|
|
|
|
return oi, nil
|
2018-01-29 21:43:13 -05:00
|
|
|
}
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
info, err := xl.getObjectInfo(ctx, bucket, object)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return oi, toObjectErr(err, bucket, object)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2018-01-29 21:43:13 -05:00
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// getObjectInfo - wrapper for reading object metadata and constructs ObjectInfo.
|
2018-04-05 18:04:40 -04:00
|
|
|
func (xl xlObjects) getObjectInfo(ctx context.Context, bucket, object string) (objInfo ObjectInfo, err error) {
|
2018-02-01 13:47:49 -05:00
|
|
|
// Read metadata associated with the object from all disks.
|
2018-04-05 18:04:40 -04:00
|
|
|
metaArr, errs := readAllXLMetadata(ctx, xl.getDisks(), bucket, object)
|
2018-02-01 13:47:49 -05:00
|
|
|
|
|
|
|
// get Quorum for this object
|
2018-07-31 03:23:29 -04:00
|
|
|
readQuorum, _, err := objectQuorumFromMeta(ctx, xl, metaArr, errs)
|
2018-02-01 13:47:49 -05:00
|
|
|
if err != nil {
|
2018-04-11 20:15:42 -04:00
|
|
|
return objInfo, err
|
2018-02-01 13:47:49 -05:00
|
|
|
}
|
2018-01-29 21:43:13 -05:00
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
if reducedErr := reduceReadQuorumErrs(ctx, errs, objectOpIgnoredErrs, readQuorum); reducedErr != nil {
|
2018-04-11 20:15:42 -04:00
|
|
|
return objInfo, reducedErr
|
2018-02-01 13:47:49 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// List all the file commit ids from parts metadata.
|
|
|
|
modTimes := listObjectModtimes(metaArr, errs)
|
|
|
|
|
|
|
|
// Reduce list of UUIDs to a single common value.
|
|
|
|
modTime, _ := commonTime(modTimes)
|
|
|
|
|
|
|
|
// Pick latest valid metadata.
|
2018-08-17 17:42:04 -04:00
|
|
|
xlMeta, err := pickValidXLMeta(ctx, metaArr, modTime, readQuorum)
|
2016-05-25 04:33:39 -04:00
|
|
|
if err != nil {
|
2018-02-01 13:47:49 -05:00
|
|
|
return objInfo, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-09-09 01:38:18 -04:00
|
|
|
|
2018-03-01 14:37:57 -05:00
|
|
|
return xlMeta.ToObjectInfo(bucket, object), nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-12-26 19:29:26 -05:00
|
|
|
func undoRename(disks []StorageAPI, srcBucket, srcEntry, dstBucket, dstEntry string, isDir bool, errs []error) {
|
2016-06-17 14:57:51 -04:00
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
// Undo rename object on disks where RenameFile succeeded.
|
2016-06-20 22:11:55 -04:00
|
|
|
|
|
|
|
// If srcEntry/dstEntry are objects then add a trailing slash to copy
|
|
|
|
// over all the parts inside the object directory
|
2016-12-26 19:29:26 -05:00
|
|
|
if isDir {
|
2016-06-20 22:11:55 -04:00
|
|
|
srcEntry = retainSlash(srcEntry)
|
|
|
|
dstEntry = retainSlash(dstEntry)
|
|
|
|
}
|
2016-07-12 01:53:54 -04:00
|
|
|
for index, disk := range disks {
|
2016-06-17 14:57:51 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Undo rename object in parallel.
|
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
|
|
|
if errs[index] != nil {
|
|
|
|
return
|
|
|
|
}
|
2016-06-20 22:11:55 -04:00
|
|
|
_ = disk.RenameFile(dstBucket, dstEntry, srcBucket, srcEntry)
|
2016-06-17 14:57:51 -04:00
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2016-06-20 22:11:55 -04:00
|
|
|
// rename - common function that renamePart and renameObject use to rename
|
|
|
|
// the respective underlying storage layer representations.
|
2018-04-11 20:15:42 -04:00
|
|
|
func rename(ctx context.Context, disks []StorageAPI, srcBucket, srcEntry, dstBucket, dstEntry string, isDir bool, writeQuorum int, ignoredErr []error) ([]StorageAPI, error) {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Initialize sync waitgroup.
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
|
|
|
// Initialize list of errors.
|
2016-07-12 01:53:54 -04:00
|
|
|
var errs = make([]error, len(disks))
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-12-26 19:29:26 -05:00
|
|
|
if isDir {
|
2016-06-20 22:11:55 -04:00
|
|
|
dstEntry = retainSlash(dstEntry)
|
|
|
|
srcEntry = retainSlash(srcEntry)
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Rename file on all underlying storage disks.
|
2016-07-12 01:53:54 -04:00
|
|
|
for index, disk := range disks {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
|
|
|
errs[index] = errDiskNotFound
|
|
|
|
continue
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
2018-04-11 20:15:42 -04:00
|
|
|
if err := disk.RenameFile(srcBucket, srcEntry, dstBucket, dstEntry); err != nil {
|
|
|
|
if !IsErrIgnored(err, ignoredErr...) {
|
|
|
|
errs[index] = err
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// Wait for all renames to finish.
|
2016-05-20 23:48:47 -04:00
|
|
|
wg.Wait()
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
// We can safely allow RenameFile errors up to len(xl.getDisks()) - writeQuorum
|
2016-05-20 23:48:47 -04:00
|
|
|
// otherwise return failure. Cleanup successful renames.
|
2018-04-05 18:04:40 -04:00
|
|
|
err := reduceWriteQuorumErrs(ctx, errs, objectOpIgnoredErrs, writeQuorum)
|
2018-04-10 12:36:37 -04:00
|
|
|
if err == errXLWriteQuorum {
|
2016-06-17 14:57:51 -04:00
|
|
|
// Undo all the partial rename operations.
|
2016-12-26 19:29:26 -05:00
|
|
|
undoRename(disks, srcBucket, srcEntry, dstBucket, dstEntry, isDir, errs)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2017-06-14 20:14:27 -04:00
|
|
|
return evalDisks(disks, errs), err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-06-20 22:11:55 -04:00
|
|
|
// renamePart - renames a part of the source object to the destination
|
|
|
|
// across all disks in parallel. Additionally if we have errors and do
|
|
|
|
// not have a readQuorum partially renamed files are renamed back to
|
|
|
|
// its proper location.
|
2018-04-05 18:04:40 -04:00
|
|
|
func renamePart(ctx context.Context, disks []StorageAPI, srcBucket, srcPart, dstBucket, dstPart string, quorum int) ([]StorageAPI, error) {
|
2016-12-26 19:29:26 -05:00
|
|
|
isDir := false
|
2018-04-11 20:15:42 -04:00
|
|
|
return rename(ctx, disks, srcBucket, srcPart, dstBucket, dstPart, isDir, quorum, []error{errFileNotFound})
|
|
|
|
}
|
|
|
|
|
|
|
|
// renameObjectDir - renames all source objects directories to destination
|
|
|
|
// object directories across all disks in parallel. Additionally if we have
|
|
|
|
// errors and do not have a readQuorum partially renamed files are renamed
|
|
|
|
// back to its proper location.
|
|
|
|
func renameObjectDir(ctx context.Context, disks []StorageAPI, srcBucket, srcObject, dstBucket, dstObject string, quorum int) ([]StorageAPI, error) {
|
|
|
|
isDir := true
|
|
|
|
return rename(ctx, disks, srcBucket, srcObject, dstBucket, dstObject, isDir, quorum, []error{errFileNotFound, errFileAccessDenied})
|
2016-06-20 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// renameObject - renames all source objects to destination object
|
|
|
|
// across all disks in parallel. Additionally if we have errors and do
|
|
|
|
// not have a readQuorum partially renamed files are renamed back to
|
|
|
|
// its proper location.
|
2018-04-05 18:04:40 -04:00
|
|
|
func renameObject(ctx context.Context, disks []StorageAPI, srcBucket, srcObject, dstBucket, dstObject string, quorum int) ([]StorageAPI, error) {
|
2016-12-26 19:29:26 -05:00
|
|
|
isDir := true
|
2018-04-11 20:15:42 -04:00
|
|
|
return rename(ctx, disks, srcBucket, srcObject, dstBucket, dstObject, isDir, quorum, []error{errFileNotFound})
|
2016-06-20 22:11:55 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// PutObject - creates an object upon reading from the input stream
|
|
|
|
// until EOF, erasure codes the data across all disk and additionally
|
|
|
|
// writes `xl.json` which carries the necessary metadata for future
|
|
|
|
// object operations.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) PutObject(ctx context.Context, bucket string, object string, data *hash.Reader, metadata map[string]string) (objInfo ObjectInfo, err error) {
|
2018-02-09 18:19:30 -05:00
|
|
|
// Validate put object input args.
|
2018-04-05 18:04:40 -04:00
|
|
|
if err = checkPutObjectArgs(ctx, bucket, object, xl, data.Size()); err != nil {
|
2018-02-09 18:19:30 -05:00
|
|
|
return ObjectInfo{}, err
|
|
|
|
}
|
2018-01-12 23:34:52 -05:00
|
|
|
// Lock the object.
|
|
|
|
objectLock := xl.nsMutex.NewNSLock(bucket, object)
|
|
|
|
if err := objectLock.GetLock(globalObjectTimeout); err != nil {
|
|
|
|
return objInfo, err
|
|
|
|
}
|
|
|
|
defer objectLock.Unlock()
|
2018-04-05 18:04:40 -04:00
|
|
|
return xl.putObject(ctx, bucket, object, data, metadata)
|
2018-01-12 23:34:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// putObject wrapper for xl PutObject
|
2018-04-05 18:04:40 -04:00
|
|
|
func (xl xlObjects) putObject(ctx context.Context, bucket string, object string, data *hash.Reader, metadata map[string]string) (objInfo ObjectInfo, err error) {
|
2018-01-29 21:43:13 -05:00
|
|
|
uniqueID := mustGetUUID()
|
|
|
|
tempObj := uniqueID
|
|
|
|
|
|
|
|
// No metadata is set, allocate a new one.
|
|
|
|
if metadata == nil {
|
|
|
|
metadata = make(map[string]string)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get parity and data drive count based on storage class metadata
|
2018-02-15 20:45:57 -05:00
|
|
|
dataDrives, parityDrives := getRedundancyCount(metadata[amzStorageClass], len(xl.getDisks()))
|
2018-01-29 21:43:13 -05:00
|
|
|
|
|
|
|
// we now know the number of blocks this object needs for data and parity.
|
|
|
|
// writeQuorum is dataBlocks + 1
|
|
|
|
writeQuorum := dataDrives + 1
|
|
|
|
|
|
|
|
// Delete temporary object in the event of failure.
|
|
|
|
// If PutObject succeeded there would be no temporary
|
|
|
|
// object to delete.
|
2018-08-29 16:36:19 -04:00
|
|
|
defer xl.deleteObject(ctx, minioMetaTmpBucket, tempObj, writeQuorum, false)
|
2018-01-29 21:43:13 -05:00
|
|
|
|
2017-01-20 19:33:01 -05:00
|
|
|
// This is a special case with size as '0' and object ends with
|
|
|
|
// a slash separator, we treat it like a valid operation and
|
|
|
|
// return success.
|
2017-09-19 15:40:27 -04:00
|
|
|
if isObjectDir(object, data.Size()) {
|
2017-02-21 22:43:44 -05:00
|
|
|
// Check if an object is present as one of the parent dir.
|
|
|
|
// -- FIXME. (needs a new kind of lock).
|
2017-05-09 17:32:24 -04:00
|
|
|
// -- FIXME (this also causes performance issue when disks are down).
|
2018-04-05 18:04:40 -04:00
|
|
|
if xl.parentDirIsObject(ctx, bucket, path.Dir(object)) {
|
|
|
|
return ObjectInfo{}, toObjectErr(errFileAccessDenied, bucket, object)
|
2017-02-21 22:43:44 -05:00
|
|
|
}
|
2018-01-29 21:43:13 -05:00
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
if err = xl.putObjectDir(ctx, minioMetaTmpBucket, tempObj, writeQuorum); err != nil {
|
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
2018-01-29 21:43:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Rename the successfully written temporary object to final location.
|
2018-04-11 20:15:42 -04:00
|
|
|
if _, err = renameObjectDir(ctx, xl.getDisks(), minioMetaTmpBucket, tempObj, bucket, object, writeQuorum); err != nil {
|
2018-01-29 21:43:13 -05:00
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
2017-09-19 15:40:27 -04:00
|
|
|
return dirObjectInfo(bucket, object, data.Size(), metadata), nil
|
2017-01-20 19:33:01 -05:00
|
|
|
}
|
2017-02-21 22:43:44 -05:00
|
|
|
|
|
|
|
// Validate put object input args.
|
2018-04-05 18:04:40 -04:00
|
|
|
if err = checkPutObjectArgs(ctx, bucket, object, xl, data.Size()); err != nil {
|
2016-12-02 02:15:17 -05:00
|
|
|
return ObjectInfo{}, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2017-02-21 22:43:44 -05:00
|
|
|
|
2017-10-06 12:38:01 -04:00
|
|
|
// Validate input data size and it can never be less than zero.
|
|
|
|
if data.Size() < 0 {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, errInvalidArgument)
|
|
|
|
return ObjectInfo{}, toObjectErr(errInvalidArgument)
|
2017-10-06 12:38:01 -04:00
|
|
|
}
|
|
|
|
|
2017-02-21 22:43:44 -05:00
|
|
|
// Check if an object is present as one of the parent dir.
|
|
|
|
// -- FIXME. (needs a new kind of lock).
|
2017-05-09 17:32:24 -04:00
|
|
|
// -- FIXME (this also causes performance issue when disks are down).
|
2018-04-05 18:04:40 -04:00
|
|
|
if xl.parentDirIsObject(ctx, bucket, path.Dir(object)) {
|
|
|
|
return ObjectInfo{}, toObjectErr(errFileAccessDenied, bucket, object)
|
2017-02-21 22:43:44 -05:00
|
|
|
}
|
|
|
|
|
2017-09-19 15:40:27 -04:00
|
|
|
// Limit the reader to its provided size if specified.
|
|
|
|
var reader io.Reader = data
|
2016-10-02 18:51:49 -04:00
|
|
|
|
2017-01-30 18:44:42 -05:00
|
|
|
// Initialize parts metadata
|
2018-02-15 20:45:57 -05:00
|
|
|
partsMetadata := make([]xlMetaV1, len(xl.getDisks()))
|
2017-01-30 18:44:42 -05:00
|
|
|
|
2017-12-22 06:28:13 -05:00
|
|
|
xlMeta := newXLMetaV1(object, dataDrives, parityDrives)
|
2016-07-13 14:56:25 -04:00
|
|
|
|
2017-01-30 18:44:42 -05:00
|
|
|
// Initialize xl meta.
|
|
|
|
for index := range partsMetadata {
|
|
|
|
partsMetadata[index] = xlMeta
|
|
|
|
}
|
|
|
|
|
|
|
|
// Order disks according to erasure distribution
|
2018-02-15 20:45:57 -05:00
|
|
|
onlineDisks := shuffleDisks(xl.getDisks(), partsMetadata[0].Erasure.Distribution)
|
2016-06-01 19:43:31 -04:00
|
|
|
|
2017-01-30 18:44:42 -05:00
|
|
|
// Total size of the written object
|
2017-02-24 12:20:40 -05:00
|
|
|
var sizeWritten int64
|
2017-01-30 18:44:42 -05:00
|
|
|
|
2018-08-24 02:35:37 -04:00
|
|
|
erasure, err := NewErasure(ctx, xlMeta.Erasure.DataBlocks, xlMeta.Erasure.ParityBlocks, xlMeta.Erasure.BlockSize)
|
2017-08-14 21:08:42 -04:00
|
|
|
if err != nil {
|
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
|
|
|
}
|
2017-10-06 12:38:01 -04:00
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
// Fetch buffer for I/O, returns from the pool if not allocates a new one and returns.
|
2018-06-13 14:55:12 -04:00
|
|
|
var buffer []byte
|
|
|
|
switch size := data.Size(); {
|
|
|
|
case size == 0:
|
|
|
|
buffer = make([]byte, 1) // Allocate atleast a byte to reach EOF
|
|
|
|
case size < blockSizeV1:
|
|
|
|
// No need to allocate fully blockSizeV1 buffer if the incoming data is smaller.
|
|
|
|
buffer = make([]byte, size, 2*size)
|
|
|
|
default:
|
|
|
|
buffer = xl.bp.Get()
|
|
|
|
defer xl.bp.Put(buffer)
|
|
|
|
}
|
2017-10-06 12:38:01 -04:00
|
|
|
|
2018-08-06 18:14:08 -04:00
|
|
|
if len(buffer) > int(xlMeta.Erasure.BlockSize) {
|
|
|
|
buffer = buffer[:xlMeta.Erasure.BlockSize]
|
|
|
|
}
|
|
|
|
|
2017-01-30 18:44:42 -05:00
|
|
|
// Read data and split into parts - similar to multipart mechanism
|
|
|
|
for partIdx := 1; ; partIdx++ {
|
|
|
|
// Compute part name
|
|
|
|
partName := "part." + strconv.Itoa(partIdx)
|
|
|
|
// Compute the path of current part
|
2017-02-21 22:43:44 -05:00
|
|
|
tempErasureObj := pathJoin(uniqueID, partName)
|
2017-01-30 18:44:42 -05:00
|
|
|
|
2017-10-22 01:30:34 -04:00
|
|
|
// Calculate the size of the current part.
|
2017-01-31 18:34:49 -05:00
|
|
|
var curPartSize int64
|
2018-04-05 18:04:40 -04:00
|
|
|
curPartSize, err = calculatePartSizeFromIdx(ctx, data.Size(), globalPutPartSize, partIdx)
|
2017-01-31 18:34:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
|
|
|
}
|
2017-01-30 18:44:42 -05:00
|
|
|
|
2017-02-24 12:20:40 -05:00
|
|
|
// Hint the filesystem to pre-allocate one continuous large block.
|
|
|
|
// This is only an optimization.
|
2017-10-22 01:30:34 -04:00
|
|
|
var curPartReader io.Reader
|
2017-01-30 18:44:42 -05:00
|
|
|
if curPartSize > 0 {
|
2018-08-06 18:14:08 -04:00
|
|
|
pErr := xl.prepareFile(ctx, minioMetaTmpBucket, tempErasureObj, curPartSize, onlineDisks, xlMeta.Erasure.BlockSize, xlMeta.Erasure.DataBlocks, writeQuorum)
|
2017-03-07 17:48:56 -05:00
|
|
|
if pErr != nil {
|
|
|
|
return ObjectInfo{}, toObjectErr(pErr, bucket, object)
|
2016-10-29 15:44:44 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-22 01:30:34 -04:00
|
|
|
if curPartSize < data.Size() {
|
|
|
|
curPartReader = io.LimitReader(reader, curPartSize)
|
|
|
|
} else {
|
|
|
|
curPartReader = reader
|
|
|
|
}
|
|
|
|
|
2018-08-06 18:14:08 -04:00
|
|
|
writers := make([]*bitrotWriter, len(onlineDisks))
|
|
|
|
for i, disk := range onlineDisks {
|
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
writers[i] = newBitrotWriter(disk, minioMetaTmpBucket, tempErasureObj, DefaultBitrotAlgorithm)
|
|
|
|
}
|
2018-08-24 02:35:37 -04:00
|
|
|
n, erasureErr := erasure.Encode(ctx, curPartReader, writers, buffer, erasure.dataBlocks+1)
|
2017-01-30 18:44:42 -05:00
|
|
|
if erasureErr != nil {
|
|
|
|
return ObjectInfo{}, toObjectErr(erasureErr, minioMetaTmpBucket, tempErasureObj)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Should return IncompleteBody{} error when reader has fewer bytes
|
|
|
|
// than specified in request header.
|
2018-08-06 18:14:08 -04:00
|
|
|
if n < curPartSize {
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, IncompleteBody{})
|
|
|
|
return ObjectInfo{}, IncompleteBody{}
|
2017-01-30 18:44:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the total written size
|
2018-08-06 18:14:08 -04:00
|
|
|
sizeWritten += n
|
2017-08-14 21:08:42 -04:00
|
|
|
|
2018-08-06 18:14:08 -04:00
|
|
|
for i, w := range writers {
|
|
|
|
if w == nil {
|
|
|
|
onlineDisks[i] = nil
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
partsMetadata[i].AddObjectPart(partIdx, partName, "", n)
|
|
|
|
partsMetadata[i].Erasure.AddChecksumInfo(ChecksumInfo{partName, DefaultBitrotAlgorithm, w.Sum()})
|
2017-01-31 18:34:49 -05:00
|
|
|
}
|
|
|
|
|
2017-10-22 01:30:34 -04:00
|
|
|
// We wrote everything, break out.
|
|
|
|
if sizeWritten == data.Size() {
|
2017-01-30 18:44:42 -05:00
|
|
|
break
|
|
|
|
}
|
2016-07-18 22:06:48 -04:00
|
|
|
}
|
2016-07-01 17:33:28 -04:00
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Save additional erasureMetadata.
|
2017-03-18 14:28:41 -04:00
|
|
|
modTime := UTCNow()
|
2017-10-24 15:25:42 -04:00
|
|
|
metadata["etag"] = hex.EncodeToString(data.MD5Current())
|
2016-06-01 19:43:31 -04:00
|
|
|
|
2016-06-17 00:42:02 -04:00
|
|
|
// Guess content-type from the extension if possible.
|
2016-05-20 23:48:47 -04:00
|
|
|
if metadata["content-type"] == "" {
|
2016-08-17 16:26:08 -04:00
|
|
|
if objectExt := path.Ext(object); objectExt != "" {
|
2016-06-17 00:42:02 -04:00
|
|
|
if content, ok := mimedb.DB[strings.ToLower(strings.TrimPrefix(objectExt, "."))]; ok {
|
|
|
|
metadata["content-type"] = content.ContentType
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-20 09:18:47 -04:00
|
|
|
if xl.isObject(bucket, object) {
|
2017-02-21 22:43:44 -05:00
|
|
|
// Rename if an object already exists to temporary location.
|
|
|
|
newUniqueID := mustGetUUID()
|
|
|
|
|
|
|
|
// Delete successfully renamed object.
|
2018-08-29 16:36:19 -04:00
|
|
|
defer xl.deleteObject(ctx, minioMetaTmpBucket, newUniqueID, writeQuorum, false)
|
2016-10-20 01:52:03 -04:00
|
|
|
|
2016-07-27 14:57:08 -04:00
|
|
|
// NOTE: Do not use online disks slice here.
|
|
|
|
// The reason is that existing object should be purged
|
|
|
|
// regardless of `xl.json` status and rolled back in case of errors.
|
2018-04-05 18:04:40 -04:00
|
|
|
_, err = renameObject(ctx, xl.getDisks(), bucket, object, minioMetaTmpBucket, newUniqueID, writeQuorum)
|
2016-06-20 09:18:47 -04:00
|
|
|
if err != nil {
|
2016-09-02 15:18:35 -04:00
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
2016-06-20 09:18:47 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-26 22:55:48 -04:00
|
|
|
// Fill all the necessary metadata.
|
2016-06-01 19:43:31 -04:00
|
|
|
// Update `xl.json` content on each disks.
|
2016-05-31 23:23:31 -04:00
|
|
|
for index := range partsMetadata {
|
2017-01-30 18:44:42 -05:00
|
|
|
partsMetadata[index].Meta = metadata
|
2017-09-19 15:40:27 -04:00
|
|
|
partsMetadata[index].Stat.Size = sizeWritten
|
2017-01-30 18:44:42 -05:00
|
|
|
partsMetadata[index].Stat.ModTime = modTime
|
2016-05-31 23:23:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write unique `xl.json` for each disk.
|
2018-04-05 18:04:40 -04:00
|
|
|
if onlineDisks, err = writeUniqueXLMetadata(ctx, onlineDisks, minioMetaTmpBucket, tempObj, partsMetadata, writeQuorum); err != nil {
|
2016-09-02 15:18:35 -04:00
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-29 03:42:09 -04:00
|
|
|
|
2018-03-27 19:44:45 -04:00
|
|
|
// Deny if WORM is enabled
|
|
|
|
if globalWORMEnabled {
|
|
|
|
if xl.isObject(bucket, object) {
|
2018-04-05 18:04:40 -04:00
|
|
|
return ObjectInfo{}, ObjectAlreadyExists{Bucket: bucket, Object: object}
|
2018-03-27 19:44:45 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// Rename the successfully written temporary object to final location.
|
2018-04-05 18:04:40 -04:00
|
|
|
if _, err = renameObject(ctx, onlineDisks, minioMetaTmpBucket, tempObj, bucket, object, writeQuorum); err != nil {
|
2016-09-02 15:18:35 -04:00
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
2016-05-28 18:13:15 -04:00
|
|
|
}
|
2016-05-29 03:42:09 -04:00
|
|
|
|
2017-01-30 18:44:42 -05:00
|
|
|
// Object info is the same in all disks, so we can pick the first meta
|
|
|
|
// of the first disk
|
|
|
|
xlMeta = partsMetadata[0]
|
|
|
|
|
2016-09-02 15:18:35 -04:00
|
|
|
objInfo = ObjectInfo{
|
|
|
|
IsDir: false,
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: object,
|
|
|
|
Size: xlMeta.Stat.Size,
|
|
|
|
ModTime: xlMeta.Stat.ModTime,
|
2017-05-14 15:05:51 -04:00
|
|
|
ETag: xlMeta.Meta["etag"],
|
2016-09-02 15:18:35 -04:00
|
|
|
ContentType: xlMeta.Meta["content-type"],
|
|
|
|
ContentEncoding: xlMeta.Meta["content-encoding"],
|
|
|
|
UserDefined: xlMeta.Meta,
|
|
|
|
}
|
2017-01-16 22:23:43 -05:00
|
|
|
|
|
|
|
// Success, return object info.
|
2016-09-02 15:18:35 -04:00
|
|
|
return objInfo, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// deleteObject - wrapper for delete object, deletes an object from
|
|
|
|
// all the disks in parallel, including `xl.json` associated with the
|
|
|
|
// object.
|
2018-08-29 16:36:19 -04:00
|
|
|
func (xl xlObjects) deleteObject(ctx context.Context, bucket, object string, writeQuorum int, isDir bool) error {
|
|
|
|
var disks []StorageAPI
|
2018-01-29 21:43:13 -05:00
|
|
|
var err error
|
2018-02-20 18:33:26 -05:00
|
|
|
|
2018-08-29 16:36:19 -04:00
|
|
|
tmpObj := mustGetUUID()
|
|
|
|
if bucket == minioMetaTmpBucket {
|
|
|
|
tmpObj = object
|
|
|
|
disks = xl.getDisks()
|
|
|
|
} else {
|
|
|
|
if isDir {
|
|
|
|
disks, err = renameObjectDir(ctx, xl.getDisks(), bucket, object, minioMetaTmpBucket, tmpObj, writeQuorum)
|
|
|
|
} else {
|
|
|
|
disks, err = renameObject(ctx, xl.getDisks(), bucket, object, minioMetaTmpBucket, tmpObj, writeQuorum)
|
2018-01-29 21:43:13 -05:00
|
|
|
}
|
2018-03-26 19:39:28 -04:00
|
|
|
if err != nil {
|
2018-08-29 16:36:19 -04:00
|
|
|
return toObjectErr(err, bucket, object)
|
2018-03-26 19:39:28 -04:00
|
|
|
}
|
2017-12-22 06:28:13 -05:00
|
|
|
}
|
|
|
|
|
2018-08-29 16:36:19 -04:00
|
|
|
// Initialize sync waitgroup.
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Initialize list of errors.
|
2018-08-29 16:36:19 -04:00
|
|
|
var dErrs = make([]error, len(disks))
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2018-08-29 16:36:19 -04:00
|
|
|
for index, disk := range disks {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
dErrs[index] = errDiskNotFound
|
2016-06-02 19:34:15 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
wg.Add(1)
|
2018-02-20 18:33:26 -05:00
|
|
|
go func(index int, disk StorageAPI, isDir bool) {
|
2016-05-20 23:48:47 -04:00
|
|
|
defer wg.Done()
|
2018-03-26 19:39:28 -04:00
|
|
|
var e error
|
2018-02-20 18:33:26 -05:00
|
|
|
if isDir {
|
|
|
|
// DeleteFile() simply tries to remove a directory
|
|
|
|
// and will succeed only if that directory is empty.
|
2018-08-29 16:36:19 -04:00
|
|
|
e = disk.DeleteFile(minioMetaTmpBucket, tmpObj)
|
2018-02-20 18:33:26 -05:00
|
|
|
} else {
|
2018-08-29 16:36:19 -04:00
|
|
|
e = cleanupDir(ctx, disk, minioMetaTmpBucket, tmpObj)
|
2018-02-20 18:33:26 -05:00
|
|
|
}
|
2018-04-10 12:36:37 -04:00
|
|
|
if e != nil && e != errVolumeNotFound {
|
2018-03-26 19:39:28 -04:00
|
|
|
dErrs[index] = e
|
2016-05-25 17:32:49 -04:00
|
|
|
}
|
2018-02-20 18:33:26 -05:00
|
|
|
}(index, disk, isDir)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all routines to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
2018-03-26 19:39:28 -04:00
|
|
|
// return errors if any during deletion
|
2018-04-05 18:04:40 -04:00
|
|
|
return reduceWriteQuorumErrs(ctx, dErrs, objectOpIgnoredErrs, writeQuorum)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// DeleteObject - deletes an object, this call doesn't necessary reply
|
|
|
|
// any error as it is not necessary for the handler to reply back a
|
|
|
|
// response to the client request.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) DeleteObject(ctx context.Context, bucket, object string) (err error) {
|
2018-01-12 23:34:52 -05:00
|
|
|
// Acquire a write lock before deleting the object.
|
|
|
|
objectLock := xl.nsMutex.NewNSLock(bucket, object)
|
|
|
|
if perr := objectLock.GetLock(globalOperationTimeout); perr != nil {
|
|
|
|
return perr
|
|
|
|
}
|
|
|
|
defer objectLock.Unlock()
|
|
|
|
|
2018-04-05 18:04:40 -04:00
|
|
|
if err = checkDelObjArgs(ctx, bucket, object); err != nil {
|
2016-12-02 02:15:17 -05:00
|
|
|
return err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2018-01-29 21:43:13 -05:00
|
|
|
if hasSuffix(object, slashSeparator) {
|
|
|
|
// Delete the object on all disks.
|
2018-08-29 16:36:19 -04:00
|
|
|
if err = xl.deleteObject(ctx, bucket, object, len(xl.getDisks())/2+1, true); err != nil {
|
2018-01-29 21:43:13 -05:00
|
|
|
return toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-29 16:36:19 -04:00
|
|
|
// Read metadata associated with the object from all disks.
|
|
|
|
partsMetadata, errs := readAllXLMetadata(ctx, xl.getDisks(), bucket, object)
|
|
|
|
|
|
|
|
// get Quorum for this object
|
|
|
|
_, writeQuorum, err := objectQuorumFromMeta(ctx, xl, partsMetadata, errs)
|
|
|
|
if err != nil {
|
|
|
|
return toObjectErr(err, bucket, object)
|
|
|
|
}
|
2016-06-17 01:18:43 -04:00
|
|
|
|
2016-06-17 14:57:51 -04:00
|
|
|
// Delete the object on all disks.
|
2018-08-29 16:36:19 -04:00
|
|
|
if err = xl.deleteObject(ctx, bucket, object, writeQuorum, false); err != nil {
|
2016-06-17 14:57:51 -04:00
|
|
|
return toObjectErr(err, bucket, object)
|
2016-06-07 14:35:03 -04:00
|
|
|
}
|
|
|
|
|
2016-06-17 14:57:51 -04:00
|
|
|
// Success.
|
|
|
|
return nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2018-02-09 18:19:30 -05:00
|
|
|
|
|
|
|
// ListObjectsV2 lists all blobs in bucket filtered by prefix
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) ListObjectsV2(ctx context.Context, bucket, prefix, continuationToken, delimiter string, maxKeys int, fetchOwner bool, startAfter string) (result ListObjectsV2Info, err error) {
|
2018-07-01 00:22:45 -04:00
|
|
|
marker := continuationToken
|
|
|
|
if marker == "" {
|
|
|
|
marker = startAfter
|
|
|
|
}
|
|
|
|
|
|
|
|
loi, err := xl.ListObjects(ctx, bucket, prefix, marker, delimiter, maxKeys)
|
2018-02-09 18:19:30 -05:00
|
|
|
if err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
listObjectsV2Info := ListObjectsV2Info{
|
|
|
|
IsTruncated: loi.IsTruncated,
|
|
|
|
ContinuationToken: continuationToken,
|
|
|
|
NextContinuationToken: loi.NextMarker,
|
|
|
|
Objects: loi.Objects,
|
|
|
|
Prefixes: loi.Prefixes,
|
|
|
|
}
|
|
|
|
return listObjectsV2Info, err
|
|
|
|
}
|