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-08-18 19:23:42 -04:00
|
|
|
package cmd
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
"sync"
|
2017-11-25 14:58:29 -05:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/errors"
|
2016-05-20 23:48:47 -04:00
|
|
|
)
|
|
|
|
|
2016-11-23 23:05:04 -05:00
|
|
|
// list all errors that can be ignore in a bucket operation.
|
|
|
|
var bucketOpIgnoredErrs = append(baseIgnoredErrs, errDiskAccessDenied)
|
|
|
|
|
2016-11-20 19:57:12 -05:00
|
|
|
// list all errors that can be ignored in a bucket metadata operation.
|
|
|
|
var bucketMetadataOpIgnoredErrs = append(bucketOpIgnoredErrs, errVolumeNotFound)
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
/// Bucket operations
|
|
|
|
|
|
|
|
// MakeBucket - make a bucket.
|
2017-06-01 12:43:20 -04:00
|
|
|
func (xl xlObjects) MakeBucketWithLocation(bucket, location string) error {
|
2018-01-12 23:34:52 -05:00
|
|
|
bucketLock := xl.nsMutex.NewNSLock(bucket, "")
|
|
|
|
if err := bucketLock.GetLock(globalObjectTimeout); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer bucketLock.Unlock()
|
2016-05-20 23:48:47 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2017-11-25 14:58:29 -05:00
|
|
|
return errors.Trace(BucketNameInvalid{Bucket: bucket})
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize sync waitgroup.
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
|
|
|
// Initialize list of errors.
|
|
|
|
var dErrs = make([]error, len(xl.storageDisks))
|
|
|
|
|
|
|
|
// Make a volume entry on all underlying storage disks.
|
|
|
|
for index, disk := range xl.storageDisks {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
dErrs[index] = errors.Trace(errDiskNotFound)
|
2016-06-02 19:34:15 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
wg.Add(1)
|
|
|
|
// Make a volume inside a go-routine.
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := disk.MakeVol(bucket)
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
dErrs[index] = errors.Trace(err)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all make vol to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-12-22 06:28:13 -05:00
|
|
|
writeQuorum := len(xl.storageDisks)/2 + 1
|
|
|
|
err := reduceWriteQuorumErrs(dErrs, bucketOpIgnoredErrs, writeQuorum)
|
2017-11-25 14:58:29 -05:00
|
|
|
if errors.Cause(err) == errXLWriteQuorum {
|
2016-06-17 14:57:51 -04:00
|
|
|
// Purge successfully created buckets if we don't have writeQuorum.
|
2016-11-16 19:42:23 -05:00
|
|
|
undoMakeBucket(xl.storageDisks, bucket)
|
2016-06-17 14:57:51 -04:00
|
|
|
}
|
2017-02-01 14:16:17 -05:00
|
|
|
return toObjectErr(err, bucket)
|
2016-06-17 14:57:51 -04:00
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
|
2016-07-21 03:27:08 -04:00
|
|
|
func (xl xlObjects) undoDeleteBucket(bucket string) {
|
|
|
|
// Initialize sync waitgroup.
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
// Undo previous make bucket entry on all underlying storage disks.
|
|
|
|
for index, disk := range xl.storageDisks {
|
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
// Delete a bucket inside a go-routine.
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
|
|
|
_ = disk.MakeVol(bucket)
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all make vol to finish.
|
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
2016-06-17 14:57:51 -04:00
|
|
|
// undo make bucket operation upon quorum failure.
|
2016-11-16 19:42:23 -05:00
|
|
|
func undoMakeBucket(storageDisks []StorageAPI, bucket string) {
|
2016-06-17 14:57:51 -04:00
|
|
|
// Initialize sync waitgroup.
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
// Undo previous make bucket entry on all underlying storage disks.
|
2016-11-16 19:42:23 -05:00
|
|
|
for index, disk := range storageDisks {
|
2016-06-17 14:57:51 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
wg.Add(1)
|
|
|
|
// Delete a bucket inside a go-routine.
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
|
|
|
_ = disk.DeleteVol(bucket)
|
|
|
|
}(index, disk)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-06-17 14:57:51 -04:00
|
|
|
|
|
|
|
// Wait for all make vol to finish.
|
|
|
|
wg.Wait()
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-06-01 19:43:31 -04:00
|
|
|
// getBucketInfo - returns the BucketInfo from one of the load balanced disks.
|
2016-05-26 06:15:01 -04:00
|
|
|
func (xl xlObjects) getBucketInfo(bucketName string) (bucketInfo BucketInfo, err error) {
|
2017-04-12 18:38:35 -04:00
|
|
|
var bucketErrs []error
|
2016-07-21 03:27:08 -04:00
|
|
|
for _, disk := range xl.getLoadBalancedDisks() {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
2017-04-12 18:38:35 -04:00
|
|
|
bucketErrs = append(bucketErrs, errDiskNotFound)
|
2016-06-02 19:34:15 -04:00
|
|
|
continue
|
|
|
|
}
|
2017-04-12 18:38:35 -04:00
|
|
|
volInfo, serr := disk.StatVol(bucketName)
|
|
|
|
if serr == nil {
|
2016-07-08 01:10:27 -04:00
|
|
|
bucketInfo = BucketInfo{
|
|
|
|
Name: volInfo.Name,
|
|
|
|
Created: volInfo.Created,
|
2016-06-02 19:34:15 -04:00
|
|
|
}
|
2016-07-08 01:10:27 -04:00
|
|
|
return bucketInfo, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2017-11-25 14:58:29 -05:00
|
|
|
err = errors.Trace(serr)
|
2016-07-08 01:10:27 -04:00
|
|
|
// For any reason disk went offline continue and pick the next one.
|
2017-11-25 14:58:29 -05:00
|
|
|
if errors.IsErrIgnored(err, bucketMetadataOpIgnoredErrs...) {
|
2017-04-12 18:38:35 -04:00
|
|
|
bucketErrs = append(bucketErrs, err)
|
2016-07-08 01:10:27 -04:00
|
|
|
continue
|
2016-06-01 19:43:31 -04:00
|
|
|
}
|
2017-04-12 18:38:35 -04:00
|
|
|
// Any error which cannot be ignored, we return quickly.
|
|
|
|
return BucketInfo{}, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2017-04-12 18:38:35 -04:00
|
|
|
// If all our errors were ignored, then we try to
|
|
|
|
// reduce to one error based on read quorum.
|
|
|
|
// `nil` is deliberately passed for ignoredErrs
|
|
|
|
// because these errors were already ignored.
|
2017-12-22 06:28:13 -05:00
|
|
|
readQuorum := len(xl.storageDisks) / 2
|
|
|
|
return BucketInfo{}, reduceReadQuorumErrs(bucketErrs, nil, readQuorum)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// GetBucketInfo - returns BucketInfo for a bucket.
|
2017-06-21 22:53:09 -04:00
|
|
|
func (xl xlObjects) GetBucketInfo(bucket string) (bi BucketInfo, e error) {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
2017-06-21 22:53:09 -04:00
|
|
|
return bi, BucketNameInvalid{Bucket: bucket}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-08-31 14:39:08 -04:00
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
bucketInfo, err := xl.getBucketInfo(bucket)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
2017-06-21 22:53:09 -04:00
|
|
|
return bi, toObjectErr(err, bucket)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
return bucketInfo, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// listBuckets - returns list of all buckets from a disk picked at random.
|
|
|
|
func (xl xlObjects) listBuckets() (bucketsInfo []BucketInfo, err error) {
|
2016-07-21 03:27:08 -04:00
|
|
|
for _, disk := range xl.getLoadBalancedDisks() {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
var volsInfo []VolInfo
|
|
|
|
volsInfo, err = disk.ListVols()
|
|
|
|
if err == nil {
|
|
|
|
// NOTE: The assumption here is that volumes across all disks in
|
|
|
|
// readQuorum have consistent view i.e they all have same number
|
|
|
|
// of buckets. This is essentially not verified since healing
|
|
|
|
// should take care of this.
|
|
|
|
var bucketsInfo []BucketInfo
|
|
|
|
for _, volInfo := range volsInfo {
|
2017-02-16 17:52:14 -05:00
|
|
|
if isReservedOrInvalidBucket(volInfo.Name) {
|
2016-07-24 01:51:12 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
bucketsInfo = append(bucketsInfo, BucketInfo{
|
|
|
|
Name: volInfo.Name,
|
|
|
|
Created: volInfo.Created,
|
|
|
|
})
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-07-24 21:08:15 -04:00
|
|
|
// For buckets info empty, loop once again to check
|
2016-11-16 19:42:23 -05:00
|
|
|
// if we have, can happen if disks were down.
|
2016-07-24 21:08:15 -04:00
|
|
|
if len(bucketsInfo) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
return bucketsInfo, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2017-11-25 14:58:29 -05:00
|
|
|
err = errors.Trace(err)
|
2016-07-08 01:10:27 -04:00
|
|
|
// Ignore any disks not found.
|
2017-11-25 14:58:29 -05:00
|
|
|
if errors.IsErrIgnored(err, bucketMetadataOpIgnoredErrs...) {
|
2016-07-08 01:10:27 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-06-01 19:43:31 -04:00
|
|
|
break
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
return nil, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// ListBuckets - lists all the buckets, sorted by its name.
|
2016-05-20 23:48:47 -04:00
|
|
|
func (xl xlObjects) ListBuckets() ([]BucketInfo, error) {
|
|
|
|
bucketInfos, err := xl.listBuckets()
|
|
|
|
if err != nil {
|
|
|
|
return nil, toObjectErr(err)
|
|
|
|
}
|
2016-05-24 20:48:58 -04:00
|
|
|
// Sort by bucket name before returning.
|
2016-05-20 23:48:47 -04:00
|
|
|
sort.Sort(byBucketName(bucketInfos))
|
|
|
|
return bucketInfos, nil
|
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// DeleteBucket - deletes a bucket.
|
2016-05-20 23:48:47 -04:00
|
|
|
func (xl xlObjects) DeleteBucket(bucket string) error {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Collect if all disks report volume not found.
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
var dErrs = make([]error, len(xl.storageDisks))
|
|
|
|
|
|
|
|
// Remove a volume entry on all underlying storage disks.
|
|
|
|
for index, disk := range xl.storageDisks {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
dErrs[index] = errors.Trace(errDiskNotFound)
|
2016-06-02 19:34:15 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
wg.Add(1)
|
|
|
|
// Delete volume inside a go-routine.
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
2016-07-12 04:01:47 -04:00
|
|
|
// Attempt to delete bucket.
|
2016-05-20 23:48:47 -04:00
|
|
|
err := disk.DeleteVol(bucket)
|
2016-07-12 04:01:47 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
dErrs[index] = errors.Trace(err)
|
2016-07-12 04:01:47 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Cleanup all the previously incomplete multiparts.
|
2016-11-22 16:15:06 -05:00
|
|
|
err = cleanupDir(disk, minioMetaMultipartBucket, bucket)
|
2016-08-25 12:39:01 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
if errors.Cause(err) == errVolumeNotFound {
|
2016-08-25 12:39:01 -04:00
|
|
|
return
|
|
|
|
}
|
2016-05-20 23:48:47 -04:00
|
|
|
dErrs[index] = err
|
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all the delete vols to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-12-22 06:28:13 -05:00
|
|
|
writeQuorum := len(xl.storageDisks)/2 + 1
|
|
|
|
err := reduceWriteQuorumErrs(dErrs, bucketOpIgnoredErrs, writeQuorum)
|
2017-11-25 14:58:29 -05:00
|
|
|
if errors.Cause(err) == errXLWriteQuorum {
|
2016-07-21 03:27:08 -04:00
|
|
|
xl.undoDeleteBucket(bucket)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2017-02-01 14:16:17 -05:00
|
|
|
return toObjectErr(err, bucket)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|