2016-06-01 19:43:31 -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-05-20 23:48:47 -04:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/md5"
|
|
|
|
"encoding/hex"
|
|
|
|
"io"
|
|
|
|
"path"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/mimedb"
|
2016-07-03 19:58:21 -04:00
|
|
|
"github.com/minio/minio/pkg/objcache"
|
2016-05-20 23:48:47 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
/// Object Operations
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// GetObject - reads an object erasured coded across multiple
|
|
|
|
// disks. Supports additional parameters like offset and length
|
|
|
|
// which is synonymous with HTTP Range requests.
|
|
|
|
//
|
|
|
|
// startOffset indicates the location at which the client requested
|
|
|
|
// object to be read at. length indicates the total length of the
|
|
|
|
// object requested by client.
|
2016-05-28 18:13:15 -04:00
|
|
|
func (xl xlObjects) GetObject(bucket, object string, startOffset int64, length int64, writer io.Writer) error {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2016-05-28 18:13:15 -04:00
|
|
|
return BucketNameInvalid{Bucket: bucket}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
// Verify if object is valid.
|
|
|
|
if !IsValidObjectName(object) {
|
2016-05-28 18:13:15 -04:00
|
|
|
return ObjectNameInvalid{Bucket: bucket, Object: object}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-07-07 04:30:34 -04:00
|
|
|
// Start offset and length cannot be negative.
|
|
|
|
if startOffset < 0 || length < 0 {
|
|
|
|
return toObjectErr(errUnexpected, bucket, object)
|
|
|
|
}
|
2016-07-08 10:46:49 -04:00
|
|
|
// Writer cannot be nil.
|
|
|
|
if writer == nil {
|
|
|
|
return toObjectErr(errUnexpected, bucket, object)
|
|
|
|
}
|
2016-05-25 04:33:39 -04:00
|
|
|
// Lock the object before reading.
|
2016-05-20 23:48:47 -04:00
|
|
|
nsMutex.RLock(bucket, object)
|
|
|
|
defer nsMutex.RUnlock(bucket, object)
|
2016-05-25 04:33:39 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Read metadata associated with the object from all disks.
|
2016-06-17 01:26:18 -04:00
|
|
|
metaArr, errs := xl.readAllXLMetadata(bucket, object)
|
2016-06-27 13:01:09 -04:00
|
|
|
// Do we have read quorum?
|
|
|
|
if !isQuorum(errs, xl.readQuorum) {
|
|
|
|
return toObjectErr(errXLReadQuorum, bucket, object)
|
|
|
|
}
|
2016-05-26 22:55:48 -04:00
|
|
|
|
2016-07-09 16:01:32 -04:00
|
|
|
// If all the disks returned error, we return error.
|
|
|
|
if err := reduceErrs(errs); err != nil {
|
|
|
|
return toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
2016-05-25 19:42:31 -04:00
|
|
|
// List all online disks.
|
2016-06-17 01:26:18 -04:00
|
|
|
onlineDisks, highestVersion, err := xl.listOnlineDisks(metaArr, errs)
|
2016-05-25 19:42:31 -04:00
|
|
|
if err != nil {
|
2016-05-28 18:13:15 -04:00
|
|
|
return toObjectErr(err, bucket, object)
|
2016-05-26 06:15:01 -04:00
|
|
|
}
|
2016-05-26 22:55:48 -04:00
|
|
|
|
2016-06-17 01:26:18 -04:00
|
|
|
// Pick latest valid metadata.
|
|
|
|
var xlMeta xlMetaV1
|
|
|
|
for _, meta := range metaArr {
|
|
|
|
if meta.IsValid() && meta.Stat.Version == highestVersion {
|
|
|
|
xlMeta = meta
|
|
|
|
break
|
2016-05-31 23:23:31 -04:00
|
|
|
}
|
|
|
|
}
|
2016-05-25 19:42:31 -04:00
|
|
|
|
2016-07-08 10:46:49 -04:00
|
|
|
// Reply back invalid range if the input offset and length fall out of range.
|
|
|
|
if startOffset > xlMeta.Stat.Size || length > xlMeta.Stat.Size {
|
|
|
|
return InvalidRange{startOffset, length, xlMeta.Stat.Size}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reply if we have inputs with offset and length.
|
|
|
|
if startOffset+length > xlMeta.Stat.Size {
|
|
|
|
return InvalidRange{startOffset, length, xlMeta.Stat.Size}
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// Get start part index and offset.
|
2016-05-31 23:23:31 -04:00
|
|
|
partIndex, partOffset, err := xlMeta.ObjectToPartOffset(startOffset)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
2016-05-28 18:13:15 -04:00
|
|
|
return toObjectErr(err, bucket, object)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
// Get last part index to read given length.
|
|
|
|
lastPartIndex, _, err := xlMeta.ObjectToPartOffset(startOffset + length - 1)
|
|
|
|
if err != nil {
|
|
|
|
return toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// Collect all the previous erasure infos across the disk.
|
|
|
|
var eInfos []erasureInfo
|
|
|
|
for index := range onlineDisks {
|
2016-06-17 01:26:18 -04:00
|
|
|
eInfos = append(eInfos, metaArr[index].Erasure)
|
2016-06-01 19:43:31 -04:00
|
|
|
}
|
|
|
|
|
2016-07-03 19:58:21 -04:00
|
|
|
// Save the writer.
|
|
|
|
mw := writer
|
|
|
|
|
|
|
|
// Object cache enabled block.
|
2016-07-08 23:34:27 -04:00
|
|
|
if xlMeta.Stat.Size > 0 && xl.objCacheEnabled {
|
2016-07-03 19:58:21 -04:00
|
|
|
// Validate if we have previous cache.
|
|
|
|
cachedBuffer, err := xl.objCache.Open(path.Join(bucket, object))
|
|
|
|
if err == nil { // Cache hit.
|
|
|
|
// Advance the buffer to offset as if it was read.
|
|
|
|
if _, err = cachedBuffer.Seek(startOffset, 0); err != nil { // Seek to the offset.
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Write the requested length.
|
|
|
|
if _, err = io.CopyN(writer, cachedBuffer, length); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
} // Cache miss.
|
|
|
|
// For unknown error, return and error out.
|
|
|
|
if err != objcache.ErrKeyNotFoundInCache {
|
|
|
|
return err
|
|
|
|
} // Cache has not been found, fill the cache.
|
|
|
|
|
|
|
|
// Cache is only set if whole object is being read.
|
|
|
|
if startOffset == 0 && length == xlMeta.Stat.Size {
|
2016-07-05 04:04:50 -04:00
|
|
|
// Proceed to set the cache.
|
2016-07-06 04:29:49 -04:00
|
|
|
var newBuffer io.WriteCloser
|
2016-07-03 19:58:21 -04:00
|
|
|
// Create a new entry in memory of length.
|
|
|
|
newBuffer, err = xl.objCache.Create(path.Join(bucket, object), length)
|
2016-07-05 04:04:50 -04:00
|
|
|
if err == nil {
|
|
|
|
// Create a multi writer to write to both memory and client response.
|
|
|
|
mw = io.MultiWriter(newBuffer, writer)
|
2016-07-06 04:29:49 -04:00
|
|
|
defer newBuffer.Close()
|
2016-07-05 04:04:50 -04:00
|
|
|
}
|
2016-07-08 23:34:27 -04:00
|
|
|
// Ignore error if cache is full, proceed to write the object.
|
2016-07-05 04:04:50 -04:00
|
|
|
if err != nil && err != objcache.ErrCacheFull {
|
2016-07-08 23:34:27 -04:00
|
|
|
// For any other error return here.
|
|
|
|
return toObjectErr(err, bucket, object)
|
2016-07-03 19:58:21 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
totalBytesRead := int64(0)
|
2016-05-31 23:23:31 -04:00
|
|
|
// Read from all parts.
|
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
|
|
|
|
2016-06-21 17:34:11 -04:00
|
|
|
readSize := partSize - partOffset
|
2016-06-22 12:05:03 -04:00
|
|
|
// readSize should be adjusted so that we don't write more data than what was requested.
|
2016-06-21 17:34:11 -04:00
|
|
|
if readSize > (length - totalBytesRead) {
|
|
|
|
readSize = length - totalBytesRead
|
2016-06-19 16:35:26 -04:00
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
|
|
|
|
// Start reading the part name.
|
2016-07-03 19:58:21 -04:00
|
|
|
n, err := erasureReadFile(mw, onlineDisks, bucket, pathJoin(object, partName), partName, eInfos, partOffset, readSize, 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
|
|
|
}
|
|
|
|
|
2016-06-19 16:35:26 -04:00
|
|
|
totalBytesRead += n
|
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
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// GetObjectInfo - reads object metadata and replies back ObjectInfo.
|
2016-05-20 23:48:47 -04:00
|
|
|
func (xl xlObjects) GetObjectInfo(bucket, object string) (ObjectInfo, error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return ObjectInfo{}, BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify if object is valid.
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ObjectInfo{}, ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
|
|
|
nsMutex.RLock(bucket, object)
|
|
|
|
defer nsMutex.RUnlock(bucket, object)
|
|
|
|
info, err := xl.getObjectInfo(bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
return ObjectInfo{}, toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// getObjectInfo - wrapper for reading object metadata and constructs ObjectInfo.
|
2016-05-20 23:48:47 -04:00
|
|
|
func (xl xlObjects) getObjectInfo(bucket, object string) (objInfo ObjectInfo, err error) {
|
2016-05-25 04:33:39 -04:00
|
|
|
var xlMeta xlMetaV1
|
|
|
|
xlMeta, err = xl.readXLMetadata(bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
// Return error.
|
|
|
|
return ObjectInfo{}, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-06-01 19:43:31 -04:00
|
|
|
objInfo = ObjectInfo{
|
|
|
|
IsDir: false,
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: object,
|
|
|
|
Size: xlMeta.Stat.Size,
|
|
|
|
ModTime: xlMeta.Stat.ModTime,
|
|
|
|
MD5Sum: xlMeta.Meta["md5Sum"],
|
|
|
|
ContentType: xlMeta.Meta["content-type"],
|
|
|
|
ContentEncoding: xlMeta.Meta["content-encoding"],
|
|
|
|
}
|
2016-05-25 04:33:39 -04:00
|
|
|
return objInfo, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-06-20 22:11:55 -04:00
|
|
|
func (xl xlObjects) undoRename(srcBucket, srcEntry, dstBucket, dstEntry string, isPart 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
|
|
|
|
if !isPart {
|
|
|
|
srcEntry = retainSlash(srcEntry)
|
|
|
|
dstEntry = retainSlash(dstEntry)
|
|
|
|
}
|
2016-06-17 14:57:51 -04:00
|
|
|
for index, disk := range xl.storageDisks {
|
|
|
|
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
|
|
|
// undoRenameObject - renames back the partially successful rename operations.
|
|
|
|
func (xl xlObjects) undoRenameObject(srcBucket, srcObject, dstBucket, dstObject string, errs []error) {
|
|
|
|
isPart := false
|
|
|
|
xl.undoRename(srcBucket, srcObject, dstBucket, dstObject, isPart, errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// undoRenamePart - renames back the partially successful rename operation.
|
|
|
|
func (xl xlObjects) undoRenamePart(srcBucket, srcPart, dstBucket, dstPart string, errs []error) {
|
|
|
|
isPart := true
|
|
|
|
xl.undoRename(srcBucket, srcPart, dstBucket, dstPart, isPart, errs)
|
|
|
|
}
|
|
|
|
|
|
|
|
// rename - common function that renamePart and renameObject use to rename
|
|
|
|
// the respective underlying storage layer representations.
|
|
|
|
func (xl xlObjects) rename(srcBucket, srcEntry, dstBucket, dstEntry string, isPart bool) error {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Initialize sync waitgroup.
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
|
|
|
// Initialize list of errors.
|
|
|
|
var errs = make([]error, len(xl.storageDisks))
|
|
|
|
|
2016-06-20 22:11:55 -04:00
|
|
|
if !isPart {
|
|
|
|
dstEntry = retainSlash(dstEntry)
|
|
|
|
srcEntry = retainSlash(srcEntry)
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Rename file on all underlying storage disks.
|
|
|
|
for index, disk := range xl.storageDisks {
|
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()
|
2016-06-20 22:11:55 -04:00
|
|
|
err := disk.RenameFile(srcBucket, srcEntry, dstBucket, dstEntry)
|
2016-06-01 19:43:31 -04:00
|
|
|
if err != nil && err != errFileNotFound {
|
2016-05-20 23:48:47 -04:00
|
|
|
errs[index] = err
|
|
|
|
}
|
|
|
|
}(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()
|
|
|
|
|
|
|
|
// We can safely allow RenameFile errors up to len(xl.storageDisks) - xl.writeQuorum
|
|
|
|
// otherwise return failure. Cleanup successful renames.
|
2016-06-17 14:57:51 -04:00
|
|
|
if !isQuorum(errs, xl.writeQuorum) {
|
2016-06-01 19:43:31 -04:00
|
|
|
// Check we have successful read quorum.
|
2016-06-17 14:57:51 -04:00
|
|
|
if isQuorum(errs, xl.readQuorum) {
|
2016-06-01 19:43:31 -04:00
|
|
|
return nil // Return success.
|
|
|
|
} // else - failed to acquire read quorum.
|
2016-06-17 14:57:51 -04:00
|
|
|
// Undo all the partial rename operations.
|
2016-06-20 22:11:55 -04:00
|
|
|
xl.undoRename(srcBucket, srcEntry, dstBucket, dstEntry, isPart, errs)
|
2016-06-02 19:34:15 -04:00
|
|
|
return errXLWriteQuorum
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-06-17 14:57:51 -04:00
|
|
|
// Return on first error, also undo any partially successful rename operations.
|
|
|
|
for _, err := range errs {
|
|
|
|
if err != nil && err != errDiskNotFound {
|
|
|
|
// Undo all the partial rename operations.
|
2016-06-20 22:11:55 -04:00
|
|
|
xl.undoRename(srcBucket, srcEntry, dstBucket, dstEntry, isPart, errs)
|
2016-06-17 14:57:51 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
|
|
|
func (xl xlObjects) renamePart(srcBucket, srcObject, dstBucket, dstObject string) error {
|
|
|
|
isPart := true
|
|
|
|
return xl.rename(srcBucket, srcObject, dstBucket, dstObject, isPart)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
func (xl xlObjects) renameObject(srcBucket, srcObject, dstBucket, dstObject string) error {
|
|
|
|
isPart := false
|
|
|
|
return xl.rename(srcBucket, srcObject, dstBucket, dstObject, isPart)
|
|
|
|
}
|
|
|
|
|
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.
|
2016-07-08 23:34:27 -04:00
|
|
|
func (xl xlObjects) PutObject(bucket string, object string, size int64, data io.Reader, metadata map[string]string) (md5Sum string, err error) {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return "", BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
// Verify bucket exists.
|
|
|
|
if !xl.isBucketExist(bucket) {
|
|
|
|
return "", BucketNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return "", ObjectNameInvalid{
|
|
|
|
Bucket: bucket,
|
|
|
|
Object: object,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No metadata is set, allocate a new one.
|
|
|
|
if metadata == nil {
|
|
|
|
metadata = make(map[string]string)
|
|
|
|
}
|
2016-05-29 03:42:09 -04:00
|
|
|
uniqueID := getUUID()
|
2016-06-28 00:42:33 -04:00
|
|
|
tempErasureObj := path.Join(tmpMetaPrefix, uniqueID, "part.1")
|
2016-06-29 05:28:46 -04:00
|
|
|
minioMetaTmpBucket := path.Join(minioMetaBucket, tmpMetaPrefix)
|
|
|
|
tempObj := uniqueID
|
2016-05-25 19:42:31 -04:00
|
|
|
|
2016-07-08 23:34:27 -04:00
|
|
|
// Lock the object.
|
|
|
|
nsMutex.Lock(bucket, object)
|
|
|
|
defer nsMutex.Unlock(bucket, object)
|
|
|
|
|
2016-05-26 22:55:48 -04:00
|
|
|
// Initialize xl meta.
|
2016-07-08 18:28:09 -04:00
|
|
|
xlMeta := newXLMetaV1(object, xl.dataBlocks, xl.parityBlocks)
|
2016-05-26 22:55:48 -04:00
|
|
|
|
2016-05-31 23:23:31 -04:00
|
|
|
// Read metadata associated with the object from all disks.
|
|
|
|
partsMetadata, errs := xl.readAllXLMetadata(bucket, object)
|
2016-06-27 13:01:09 -04:00
|
|
|
// Do we have write quroum?.
|
|
|
|
if !isQuorum(errs, xl.writeQuorum) {
|
|
|
|
return "", toObjectErr(errXLWriteQuorum, bucket, object)
|
|
|
|
}
|
2016-05-31 23:23:31 -04:00
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// List all online disks.
|
2016-05-31 23:23:31 -04:00
|
|
|
onlineDisks, higherVersion, err := xl.listOnlineDisks(partsMetadata, errs)
|
2016-05-25 19:42:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
2016-05-26 22:55:48 -04:00
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// Increment version only if we have online disks less than configured storage disks.
|
2016-05-25 19:42:31 -04:00
|
|
|
if diskCount(onlineDisks) < len(xl.storageDisks) {
|
|
|
|
higherVersion++
|
|
|
|
}
|
2016-05-26 22:55:48 -04:00
|
|
|
|
2016-07-08 23:34:27 -04:00
|
|
|
var mw io.Writer
|
2016-05-20 23:48:47 -04:00
|
|
|
// Initialize md5 writer.
|
|
|
|
md5Writer := md5.New()
|
|
|
|
|
2016-07-08 23:34:27 -04:00
|
|
|
// Proceed to set the cache.
|
|
|
|
var newBuffer io.WriteCloser
|
|
|
|
|
|
|
|
// If caching is enabled, proceed to set the cache.
|
|
|
|
if size > 0 && xl.objCacheEnabled {
|
|
|
|
// PutObject invalidates any previously cached object in memory.
|
|
|
|
xl.objCache.Delete(path.Join(bucket, object))
|
|
|
|
|
|
|
|
// Create a new entry in memory of size.
|
|
|
|
newBuffer, err = xl.objCache.Create(path.Join(bucket, object), size)
|
|
|
|
if err == nil {
|
|
|
|
// Create a multi writer to write to both memory and client response.
|
|
|
|
mw = io.MultiWriter(newBuffer, md5Writer)
|
|
|
|
}
|
|
|
|
// Ignore error if cache is full, proceed to write the object.
|
|
|
|
if err != nil && err != objcache.ErrCacheFull {
|
|
|
|
// For any other error return here.
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
mw = md5Writer
|
|
|
|
}
|
|
|
|
|
2016-07-01 17:33:28 -04:00
|
|
|
// Limit the reader to its provided size if specified.
|
2016-07-05 04:04:50 -04:00
|
|
|
var limitDataReader io.Reader
|
2016-07-01 17:33:28 -04:00
|
|
|
if size > 0 {
|
|
|
|
// This is done so that we can avoid erroneous clients sending
|
|
|
|
// more data than the set content size.
|
2016-07-05 04:04:50 -04:00
|
|
|
limitDataReader = io.LimitReader(data, size)
|
|
|
|
} else {
|
|
|
|
// else we read till EOF.
|
|
|
|
limitDataReader = data
|
|
|
|
}
|
2016-07-01 17:33:28 -04:00
|
|
|
|
2016-07-05 04:04:50 -04:00
|
|
|
// Tee reader combines incoming data stream and md5, data read from input stream is written to md5.
|
2016-07-08 23:34:27 -04:00
|
|
|
teeReader := io.TeeReader(limitDataReader, mw)
|
2016-06-01 19:43:31 -04:00
|
|
|
|
|
|
|
// Collect all the previous erasure infos across the disk.
|
|
|
|
var eInfos []erasureInfo
|
|
|
|
for range onlineDisks {
|
|
|
|
eInfos = append(eInfos, xlMeta.Erasure)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Erasure code and write across all disks.
|
2016-07-01 17:33:28 -04:00
|
|
|
newEInfos, sizeWritten, err := erasureCreateFile(onlineDisks, minioMetaBucket, tempErasureObj, "part.1", teeReader, eInfos, xl.writeQuorum)
|
2016-06-01 19:43:31 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, minioMetaBucket, tempErasureObj)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-07-01 17:33:28 -04:00
|
|
|
|
|
|
|
// For size == -1, perhaps client is sending in chunked encoding
|
|
|
|
// set the size as size that was actually written.
|
2016-06-02 20:09:47 -04:00
|
|
|
if size == -1 {
|
2016-07-01 17:33:28 -04:00
|
|
|
size = sizeWritten
|
2016-06-02 20:09:47 -04:00
|
|
|
}
|
2016-07-01 17:33:28 -04:00
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Save additional erasureMetadata.
|
|
|
|
modTime := time.Now().UTC()
|
|
|
|
|
|
|
|
newMD5Hex := hex.EncodeToString(md5Writer.Sum(nil))
|
|
|
|
// Update the md5sum if not set with the newly calculated one.
|
|
|
|
if len(metadata["md5Sum"]) == 0 {
|
|
|
|
metadata["md5Sum"] = newMD5Hex
|
|
|
|
}
|
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"] == "" {
|
|
|
|
if objectExt := filepath.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-07-05 04:04:50 -04:00
|
|
|
// Validate if payload is valid.
|
|
|
|
if isSignVerify(data) {
|
|
|
|
if vErr := data.(*signVerifyReader).Verify(); vErr != nil {
|
|
|
|
// Incoming payload wrong, delete the temporary object.
|
|
|
|
xl.deleteObject(minioMetaTmpBucket, tempObj)
|
|
|
|
// Error return.
|
|
|
|
return "", toObjectErr(vErr, bucket, object)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// md5Hex representation.
|
|
|
|
md5Hex := metadata["md5Sum"]
|
|
|
|
if md5Hex != "" {
|
|
|
|
if newMD5Hex != md5Hex {
|
2016-06-01 19:43:31 -04:00
|
|
|
// MD5 mismatch, delete the temporary object.
|
2016-06-29 05:28:46 -04:00
|
|
|
xl.deleteObject(minioMetaTmpBucket, tempObj)
|
2016-06-01 19:43:31 -04:00
|
|
|
// Returns md5 mismatch.
|
2016-05-20 23:48:47 -04:00
|
|
|
return "", BadDigest{md5Hex, newMD5Hex}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if an object is present as one of the parent dir.
|
2016-06-01 19:43:31 -04:00
|
|
|
// -- FIXME. (needs a new kind of lock).
|
2016-05-20 23:48:47 -04:00
|
|
|
if xl.parentDirIsObject(bucket, path.Dir(object)) {
|
|
|
|
return "", toObjectErr(errFileAccessDenied, bucket, object)
|
|
|
|
}
|
|
|
|
|
2016-05-29 03:42:09 -04:00
|
|
|
// Rename if an object already exists to temporary location.
|
|
|
|
newUniqueID := getUUID()
|
2016-06-20 09:18:47 -04:00
|
|
|
if xl.isObject(bucket, object) {
|
2016-06-29 05:28:46 -04:00
|
|
|
err = xl.renameObject(bucket, object, minioMetaTmpBucket, newUniqueID)
|
2016-06-20 09:18:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-26 22:55:48 -04:00
|
|
|
// Fill all the necessary metadata.
|
2016-05-20 23:48:47 -04:00
|
|
|
xlMeta.Meta = metadata
|
|
|
|
xlMeta.Stat.Size = size
|
|
|
|
xlMeta.Stat.ModTime = modTime
|
2016-05-25 19:42:31 -04:00
|
|
|
xlMeta.Stat.Version = higherVersion
|
2016-05-31 23:23:31 -04:00
|
|
|
// Add the final part.
|
2016-06-28 00:42:33 -04:00
|
|
|
xlMeta.AddObjectPart(1, "part.1", newMD5Hex, xlMeta.Stat.Size)
|
2016-05-29 03:42:09 -04:00
|
|
|
|
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 {
|
|
|
|
partsMetadata[index] = xlMeta
|
2016-06-01 19:43:31 -04:00
|
|
|
partsMetadata[index].Erasure = newEInfos[index]
|
2016-05-31 23:23:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Write unique `xl.json` for each disk.
|
2016-06-29 05:28:46 -04:00
|
|
|
if err = xl.writeUniqueXLMetadata(minioMetaTmpBucket, tempObj, partsMetadata); err != nil {
|
2016-05-20 23:48:47 -04:00
|
|
|
return "", toObjectErr(err, bucket, object)
|
|
|
|
}
|
2016-05-29 03:42:09 -04:00
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// Rename the successfully written temporary object to final location.
|
2016-06-29 05:28:46 -04:00
|
|
|
err = xl.renameObject(minioMetaTmpBucket, tempObj, bucket, object)
|
2016-05-29 03:42:09 -04:00
|
|
|
if err != nil {
|
|
|
|
return "", toObjectErr(err, bucket, object)
|
2016-05-28 18:13:15 -04:00
|
|
|
}
|
2016-05-29 03:42:09 -04:00
|
|
|
|
|
|
|
// Delete the temporary object.
|
2016-06-29 05:28:46 -04:00
|
|
|
xl.deleteObject(minioMetaTmpBucket, newUniqueID)
|
2016-05-29 03:42:09 -04:00
|
|
|
|
2016-07-08 23:34:27 -04:00
|
|
|
// Once we have successfully renamed the object, Close the buffer which would
|
|
|
|
// save the object on cache.
|
|
|
|
if size > 0 && xl.objCacheEnabled && newBuffer != nil {
|
|
|
|
newBuffer.Close()
|
|
|
|
}
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Return md5sum, successfully wrote object.
|
|
|
|
return newMD5Hex, nil
|
|
|
|
}
|
|
|
|
|
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.
|
2016-05-20 23:48:47 -04:00
|
|
|
func (xl xlObjects) deleteObject(bucket, object string) error {
|
|
|
|
// Initialize sync waitgroup.
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
|
|
|
// Initialize list of errors.
|
|
|
|
var dErrs = make([]error, len(xl.storageDisks))
|
|
|
|
|
|
|
|
for index, disk := range xl.storageDisks {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
|
|
|
dErrs[index] = errDiskNotFound
|
|
|
|
continue
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
2016-05-25 17:32:49 -04:00
|
|
|
err := cleanupDir(disk, bucket, object)
|
2016-06-17 14:57:51 -04:00
|
|
|
if err != nil && err != errFileNotFound {
|
2016-05-25 17:32:49 -04:00
|
|
|
dErrs[index] = err
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all routines to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-06-17 14:57:51 -04:00
|
|
|
if !isQuorum(dErrs, xl.writeQuorum) {
|
2016-06-02 19:34:15 -04:00
|
|
|
// Return errXLWriteQuorum if errors were more than
|
2016-05-20 23:48:47 -04:00
|
|
|
// allowed write quorum.
|
2016-06-02 19:34:15 -04:00
|
|
|
return errXLWriteQuorum
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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.
|
2016-06-07 14:35:03 -04:00
|
|
|
func (xl xlObjects) DeleteObject(bucket, object string) (err error) {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
if !IsValidObjectName(object) {
|
|
|
|
return ObjectNameInvalid{Bucket: bucket, Object: object}
|
|
|
|
}
|
|
|
|
nsMutex.Lock(bucket, object)
|
|
|
|
defer nsMutex.Unlock(bucket, object)
|
2016-06-07 14:35:03 -04:00
|
|
|
|
2016-06-17 14:57:51 -04:00
|
|
|
// Validate object exists.
|
2016-06-17 01:18:43 -04:00
|
|
|
if !xl.isObject(bucket, object) {
|
|
|
|
return ObjectNotFound{bucket, object}
|
2016-06-17 14:57:51 -04:00
|
|
|
} // else proceed to delete the object.
|
2016-06-17 01:18:43 -04:00
|
|
|
|
2016-06-17 14:57:51 -04:00
|
|
|
// Delete the object on all disks.
|
|
|
|
err = xl.deleteObject(bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
return toObjectErr(err, bucket, object)
|
2016-06-07 14:35:03 -04:00
|
|
|
}
|
|
|
|
|
2016-07-06 13:25:42 -04:00
|
|
|
// Delete from the cache.
|
|
|
|
xl.objCache.Delete(pathJoin(bucket, object))
|
|
|
|
|
2016-06-17 14:57:51 -04:00
|
|
|
// Success.
|
|
|
|
return nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|