2016-04-30 02:52:17 +02:00
|
|
|
/*
|
2017-01-18 21:24:34 +01:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2016-04-30 02:52:17 +02: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-19 01:23:42 +02:00
|
|
|
package cmd
|
2016-04-30 02:52:17 +02:00
|
|
|
|
2016-05-25 10:33:39 +02:00
|
|
|
import (
|
|
|
|
"sync"
|
2016-11-23 03:18:22 +01:00
|
|
|
|
|
|
|
humanize "github.com/dustin/go-humanize"
|
2017-11-25 20:58:29 +01:00
|
|
|
"github.com/minio/minio/pkg/errors"
|
2016-05-25 10:33:39 +02:00
|
|
|
)
|
2016-04-30 02:52:17 +02:00
|
|
|
|
2016-05-30 00:38:14 +02:00
|
|
|
const (
|
|
|
|
// Block size used for all internal operations version 1.
|
2016-11-23 03:18:22 +01:00
|
|
|
blockSizeV1 = 10 * humanize.MiByte
|
2016-06-25 12:03:27 +02:00
|
|
|
|
|
|
|
// Staging buffer read size for all internal operations version 1.
|
2016-11-23 03:18:22 +01:00
|
|
|
readSizeV1 = 1 * humanize.MiByte
|
2016-07-22 02:31:14 +02:00
|
|
|
|
|
|
|
// Buckets meta prefix.
|
|
|
|
bucketMetaPrefix = "buckets"
|
2017-04-11 04:51:23 +02:00
|
|
|
|
2017-05-14 21:05:51 +02:00
|
|
|
// ETag (hex encoded md5sum) of empty string.
|
|
|
|
emptyETag = "d41d8cd98f00b204e9800998ecf8427e"
|
2016-05-30 00:38:14 +02:00
|
|
|
)
|
|
|
|
|
2016-10-10 08:03:10 +02:00
|
|
|
// Global object layer mutex, used for safely updating object layer.
|
2017-03-03 10:07:45 +01:00
|
|
|
var globalObjLayerMutex *sync.RWMutex
|
2016-10-10 08:03:10 +02:00
|
|
|
|
|
|
|
// Global object layer, only accessed by newObjectLayerFn().
|
|
|
|
var globalObjectAPI ObjectLayer
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
// Initialize this once per server initialization.
|
2017-03-03 10:07:45 +01:00
|
|
|
globalObjLayerMutex = &sync.RWMutex{}
|
2016-10-10 08:03:10 +02:00
|
|
|
}
|
|
|
|
|
2017-01-17 02:05:00 +01:00
|
|
|
// Check if the disk is remote.
|
|
|
|
func isRemoteDisk(disk StorageAPI) bool {
|
|
|
|
_, ok := disk.(*networkStorage)
|
|
|
|
return ok
|
|
|
|
}
|
|
|
|
|
2017-01-21 01:33:01 +01:00
|
|
|
// Checks if the object is a directory, this logic uses
|
|
|
|
// if size == 0 and object ends with slashSeparator then
|
|
|
|
// returns true.
|
|
|
|
func isObjectDir(object string, size int64) bool {
|
2017-02-16 23:52:14 +01:00
|
|
|
return hasSuffix(object, slashSeparator) && size == 0
|
2017-01-21 01:33:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Converts just bucket, object metadata into ObjectInfo datatype.
|
|
|
|
func dirObjectInfo(bucket, object string, size int64, metadata map[string]string) ObjectInfo {
|
|
|
|
// 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-05-14 21:05:51 +02:00
|
|
|
etag := metadata["etag"]
|
|
|
|
delete(metadata, "etag")
|
|
|
|
if etag == "" {
|
|
|
|
etag = emptyETag
|
2017-04-11 04:51:23 +02:00
|
|
|
}
|
|
|
|
|
2017-01-21 01:33:01 +01:00
|
|
|
return ObjectInfo{
|
|
|
|
Bucket: bucket,
|
|
|
|
Name: object,
|
2017-03-18 19:28:41 +01:00
|
|
|
ModTime: UTCNow(),
|
2017-01-21 01:33:01 +01:00
|
|
|
ContentType: "application/octet-stream",
|
|
|
|
IsDir: true,
|
|
|
|
Size: size,
|
2017-05-14 21:05:51 +02:00
|
|
|
ETag: etag,
|
2017-01-21 01:33:01 +01:00
|
|
|
UserDefined: metadata,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-10-20 04:59:48 +02:00
|
|
|
// House keeping code for FS/XL and distributed Minio setup.
|
|
|
|
func houseKeeping(storageDisks []StorageAPI) error {
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
|
|
|
// Initialize errs to collect errors inside go-routine.
|
|
|
|
var errs = make([]error, len(storageDisks))
|
|
|
|
|
|
|
|
// Initialize all disks in parallel.
|
|
|
|
for index, disk := range storageDisks {
|
2016-10-18 21:49:24 +02:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2017-01-17 02:05:00 +01:00
|
|
|
// Skip remote disks.
|
|
|
|
if isRemoteDisk(disk) {
|
2016-10-20 04:59:48 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
// Indicate this wait group is done.
|
|
|
|
defer wg.Done()
|
|
|
|
|
|
|
|
// Cleanup all temp entries upon start.
|
2016-11-20 23:25:43 +01:00
|
|
|
err := cleanupDir(disk, minioMetaTmpBucket, "")
|
2016-10-20 04:59:48 +02:00
|
|
|
if err != nil {
|
2017-11-25 20:58:29 +01:00
|
|
|
if !errors.IsErrIgnored(errors.Cause(err), errDiskNotFound, errVolumeNotFound, errFileNotFound) {
|
2016-10-20 04:59:48 +02:00
|
|
|
errs[index] = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all cleanup to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// Return upon first error.
|
|
|
|
for _, err := range errs {
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-11-20 23:25:43 +01:00
|
|
|
return toObjectErr(err, minioMetaTmpBucket, "*")
|
2016-05-31 01:51:59 +02:00
|
|
|
}
|
2016-10-20 04:59:48 +02:00
|
|
|
|
|
|
|
// Return success here.
|
2016-05-31 01:51:59 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-02 10:49:46 +02:00
|
|
|
// Depending on the disk type network or local, initialize storage API.
|
2017-04-12 00:44:27 +02:00
|
|
|
func newStorageAPI(endpoint Endpoint) (storage StorageAPI, err error) {
|
|
|
|
if endpoint.IsLocal {
|
|
|
|
return newPosix(endpoint.Path)
|
2016-06-02 10:49:46 +02:00
|
|
|
}
|
2017-04-12 00:44:27 +02:00
|
|
|
|
|
|
|
return newStorageRPC(endpoint), nil
|
2016-06-02 10:49:46 +02:00
|
|
|
}
|
|
|
|
|
2016-11-24 05:05:04 +01:00
|
|
|
var initMetaVolIgnoredErrs = append(baseIgnoredErrs, errVolumeExists)
|
2016-11-21 10:46:55 +01:00
|
|
|
|
2016-07-02 10:59:28 +02:00
|
|
|
// Initializes meta volume on all input storage disks.
|
|
|
|
func initMetaVolume(storageDisks []StorageAPI) error {
|
2016-05-06 00:00:03 +02:00
|
|
|
// This happens for the first time, but keep this here since this
|
|
|
|
// is the only place where it can be made expensive optimizing all
|
|
|
|
// other calls. Create minio meta volume, if it doesn't exist yet.
|
2016-05-25 10:33:39 +02:00
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
|
|
|
// Initialize errs to collect errors inside go-routine.
|
|
|
|
var errs = make([]error, len(storageDisks))
|
|
|
|
|
|
|
|
// Initialize all disks in parallel.
|
|
|
|
for index, disk := range storageDisks {
|
2016-06-03 01:34:15 +02:00
|
|
|
if disk == nil {
|
2016-07-10 23:38:15 +02:00
|
|
|
// Ignore create meta volume on disks which are not found.
|
2016-06-03 01:34:15 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-05-25 10:33:39 +02:00
|
|
|
wg.Add(1)
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
// Indicate this wait group is done.
|
|
|
|
defer wg.Done()
|
|
|
|
|
2016-09-07 05:31:50 +02:00
|
|
|
// Attempt to create `.minio.sys`.
|
2016-05-25 10:33:39 +02:00
|
|
|
err := disk.MakeVol(minioMetaBucket)
|
2016-07-02 10:59:28 +02:00
|
|
|
if err != nil {
|
2017-11-25 20:58:29 +01:00
|
|
|
if !errors.IsErrIgnored(err, initMetaVolIgnoredErrs...) {
|
2016-07-02 10:59:28 +02:00
|
|
|
errs[index] = err
|
2016-11-26 03:11:50 +01:00
|
|
|
return
|
2016-07-02 10:59:28 +02:00
|
|
|
}
|
2016-11-20 23:25:43 +01:00
|
|
|
}
|
|
|
|
err = disk.MakeVol(minioMetaTmpBucket)
|
|
|
|
if err != nil {
|
2017-11-25 20:58:29 +01:00
|
|
|
if !errors.IsErrIgnored(err, initMetaVolIgnoredErrs...) {
|
2016-11-20 23:25:43 +01:00
|
|
|
errs[index] = err
|
2016-11-26 03:11:50 +01:00
|
|
|
return
|
2016-11-20 23:25:43 +01:00
|
|
|
}
|
2016-05-20 11:22:22 +02:00
|
|
|
}
|
2016-11-22 22:15:06 +01:00
|
|
|
err = disk.MakeVol(minioMetaMultipartBucket)
|
|
|
|
if err != nil {
|
2017-11-25 20:58:29 +01:00
|
|
|
if !errors.IsErrIgnored(err, initMetaVolIgnoredErrs...) {
|
2016-11-22 22:15:06 +01:00
|
|
|
errs[index] = err
|
2016-11-26 03:11:50 +01:00
|
|
|
return
|
2016-11-22 22:15:06 +01:00
|
|
|
}
|
|
|
|
}
|
2016-07-02 10:59:28 +02:00
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all cleanup to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
// Return upon first error.
|
|
|
|
for _, err := range errs {
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return toObjectErr(err, minioMetaBucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return success here.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-08 19:15:34 +02:00
|
|
|
// Cleanup a directory recursively.
|
|
|
|
func cleanupDir(storage StorageAPI, volume, dirPath string) error {
|
|
|
|
var delFunc func(string) error
|
|
|
|
// Function to delete entries recursively.
|
|
|
|
delFunc = func(entryPath string) error {
|
2017-02-16 23:52:14 +01:00
|
|
|
if !hasSuffix(entryPath, slashSeparator) {
|
2016-07-12 10:01:47 +02:00
|
|
|
// Delete the file entry.
|
2017-11-25 20:58:29 +01:00
|
|
|
return errors.Trace(storage.DeleteFile(volume, entryPath))
|
2016-05-08 19:15:34 +02:00
|
|
|
}
|
2016-07-12 10:01:47 +02:00
|
|
|
|
2016-05-08 19:15:34 +02:00
|
|
|
// If it's a directory, list and call delFunc() for each entry.
|
|
|
|
entries, err := storage.ListDir(volume, entryPath)
|
2016-07-12 10:01:47 +02:00
|
|
|
// If entryPath prefix never existed, safe to ignore.
|
|
|
|
if err == errFileNotFound {
|
|
|
|
return nil
|
|
|
|
} else if err != nil { // For any other errors fail.
|
2017-11-25 20:58:29 +01:00
|
|
|
return errors.Trace(err)
|
2016-07-12 10:01:47 +02:00
|
|
|
} // else on success..
|
2016-06-14 10:39:40 +02:00
|
|
|
|
2016-07-12 10:01:47 +02:00
|
|
|
// Recurse and delete all other entries.
|
|
|
|
for _, entry := range entries {
|
|
|
|
if err = delFunc(pathJoin(entryPath, entry)); err != nil {
|
2016-05-08 19:15:34 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-06-03 01:34:15 +02:00
|
|
|
err := delFunc(retainSlash(pathJoin(dirPath)))
|
|
|
|
return err
|
2016-05-08 19:15:34 +02:00
|
|
|
}
|