2016-06-01 19:43:31 -04:00
|
|
|
/*
|
2019-04-09 14:39:42 -04:00
|
|
|
* MinIO Cloud Storage, (C) 2016 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-03-14 15:01:47 -04:00
|
|
|
"context"
|
2016-05-20 23:48:47 -04:00
|
|
|
"sort"
|
|
|
|
"sync"
|
2017-11-25 14:58:29 -05:00
|
|
|
|
2019-03-05 13:42:32 -05:00
|
|
|
"github.com/minio/minio-go/pkg/s3utils"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-04-24 18:53:30 -04:00
|
|
|
"github.com/minio/minio/pkg/policy"
|
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.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) MakeBucketWithLocation(ctx context.Context, bucket, location string) error {
|
2016-05-20 23:48:47 -04:00
|
|
|
// Verify if bucket is valid.
|
2019-03-05 13:42:32 -05:00
|
|
|
if err := s3utils.CheckValidBucketNameStrict(bucket); err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
return BucketNameInvalid{Bucket: bucket}
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize sync waitgroup.
|
|
|
|
var wg = &sync.WaitGroup{}
|
|
|
|
|
|
|
|
// Initialize list of errors.
|
2018-02-15 20:45:57 -05:00
|
|
|
var dErrs = make([]error, len(xl.getDisks()))
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
// Make a volume entry on all underlying storage disks.
|
2018-02-15 20:45:57 -05:00
|
|
|
for index, disk := range xl.getDisks() {
|
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)
|
|
|
|
// Make a volume inside a go-routine.
|
|
|
|
go func(index int, disk StorageAPI) {
|
|
|
|
defer wg.Done()
|
|
|
|
err := disk.MakeVol(bucket)
|
|
|
|
if err != nil {
|
2018-04-05 18:04:40 -04:00
|
|
|
if err != errVolumeExists {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
|
|
|
dErrs[index] = err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all make vol to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
2018-02-15 20:45:57 -05:00
|
|
|
writeQuorum := len(xl.getDisks())/2 + 1
|
2018-04-05 18:04:40 -04:00
|
|
|
err := reduceWriteQuorumErrs(ctx, dErrs, bucketOpIgnoredErrs, writeQuorum)
|
2018-04-10 12:36:37 -04:00
|
|
|
if err == errXLWriteQuorum {
|
2016-06-17 14:57:51 -04:00
|
|
|
// Purge successfully created buckets if we don't have writeQuorum.
|
2018-02-15 20:45:57 -05:00
|
|
|
undoMakeBucket(xl.getDisks(), 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.
|
2018-02-15 20:45:57 -05:00
|
|
|
for index, disk := range xl.getDisks() {
|
2016-07-21 03:27:08 -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.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.
|
2018-04-05 18:04:40 -04:00
|
|
|
func (xl xlObjects) getBucketInfo(ctx context.Context, 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 {
|
2019-02-13 07:59:36 -05:00
|
|
|
return BucketInfo(volInfo), nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
err = serr
|
2016-07-08 01:10:27 -04:00
|
|
|
// For any reason disk went offline continue and pick the next one.
|
2018-04-10 12:36:37 -04:00
|
|
|
if 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.
|
2018-02-15 20:45:57 -05:00
|
|
|
readQuorum := len(xl.getDisks()) / 2
|
2018-04-05 18:04:40 -04:00
|
|
|
return BucketInfo{}, reduceReadQuorumErrs(ctx, 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.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) GetBucketInfo(ctx context.Context, bucket string) (bi BucketInfo, e error) {
|
2018-02-09 18:19:30 -05:00
|
|
|
bucketLock := xl.nsMutex.NewNSLock(bucket, "")
|
|
|
|
if e := bucketLock.GetRLock(globalObjectTimeout); e != nil {
|
|
|
|
return bi, e
|
|
|
|
}
|
|
|
|
defer bucketLock.RUnlock()
|
2018-04-05 18:04:40 -04:00
|
|
|
bucketInfo, err := xl.getBucketInfo(ctx, 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.
|
2018-04-05 18:04:40 -04:00
|
|
|
func (xl xlObjects) listBuckets(ctx context.Context) (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 {
|
2019-03-05 13:42:32 -05:00
|
|
|
if isReservedOrInvalidBucket(volInfo.Name, true) {
|
2016-07-24 01:51:12 -04:00
|
|
|
continue
|
|
|
|
}
|
2019-02-13 07:59:36 -05:00
|
|
|
bucketsInfo = append(bucketsInfo, BucketInfo(volInfo))
|
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
|
|
|
}
|
2018-04-05 18:04:40 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2016-07-08 01:10:27 -04:00
|
|
|
// Ignore any disks not found.
|
2018-04-10 12:36:37 -04:00
|
|
|
if 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.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) ListBuckets(ctx context.Context) ([]BucketInfo, error) {
|
2018-04-05 18:04:40 -04:00
|
|
|
bucketInfos, err := xl.listBuckets(ctx)
|
2016-05-20 23:48:47 -04:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-02-05 20:58:48 -05:00
|
|
|
// Dangling buckets should be handled appropriately, in this following situation
|
|
|
|
// we actually have quorum error to be `nil` but we have some disks where
|
|
|
|
// the bucket delete returned `errVolumeNotEmpty` but this is not correct
|
|
|
|
// can only happen if there are dangling objects in a bucket. Under such
|
|
|
|
// a situation we simply attempt a full delete of the bucket including
|
|
|
|
// the dangling objects. All of this happens under a lock and there
|
|
|
|
// is no way a user can create buckets and sneak in objects into namespace,
|
|
|
|
// so it is safer to do.
|
|
|
|
func deleteDanglingBucket(ctx context.Context, storageDisks []StorageAPI, dErrs []error, bucket string) {
|
|
|
|
for index, err := range dErrs {
|
|
|
|
if err == errVolumeNotEmpty {
|
|
|
|
// Attempt to delete bucket again.
|
|
|
|
if derr := storageDisks[index].DeleteVol(bucket); derr == errVolumeNotEmpty {
|
|
|
|
_ = cleanupDir(ctx, storageDisks[index], bucket, "")
|
|
|
|
|
|
|
|
_ = storageDisks[index].DeleteVol(bucket)
|
|
|
|
|
|
|
|
// Cleanup all the previously incomplete multiparts.
|
|
|
|
_ = cleanupDir(ctx, storageDisks[index], minioMetaMultipartBucket, bucket)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// DeleteBucket - deletes a bucket.
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) DeleteBucket(ctx context.Context, bucket string) error {
|
2018-02-09 18:19:30 -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
|
|
|
// Collect if all disks report volume not found.
|
|
|
|
var wg = &sync.WaitGroup{}
|
2018-02-15 20:45:57 -05:00
|
|
|
var dErrs = make([]error, len(xl.getDisks()))
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
// Remove a volume entry on all underlying storage disks.
|
2019-02-05 20:58:48 -05:00
|
|
|
storageDisks := xl.getDisks()
|
|
|
|
for index, disk := range storageDisks {
|
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)
|
|
|
|
// 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 {
|
2018-04-05 18:04:40 -04:00
|
|
|
dErrs[index] = err
|
2016-07-12 04:01:47 -04:00
|
|
|
return
|
|
|
|
}
|
2019-02-05 20:58:48 -05:00
|
|
|
|
2016-07-12 04:01:47 -04:00
|
|
|
// Cleanup all the previously incomplete multiparts.
|
2018-04-05 18:04:40 -04:00
|
|
|
err = cleanupDir(ctx, disk, minioMetaMultipartBucket, bucket)
|
2019-02-05 20:58:48 -05:00
|
|
|
if err != nil && err != errVolumeNotFound {
|
2016-05-20 23:48:47 -04:00
|
|
|
dErrs[index] = err
|
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all the delete vols to finish.
|
|
|
|
wg.Wait()
|
2018-02-15 20:45:57 -05:00
|
|
|
|
|
|
|
writeQuorum := len(xl.getDisks())/2 + 1
|
2018-04-05 18:04:40 -04:00
|
|
|
err := reduceWriteQuorumErrs(ctx, dErrs, bucketOpIgnoredErrs, writeQuorum)
|
2018-04-10 12:36:37 -04:00
|
|
|
if err == errXLWriteQuorum {
|
2016-07-21 03:27:08 -04:00
|
|
|
xl.undoDeleteBucket(bucket)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2018-02-09 18:19:30 -05:00
|
|
|
if err != nil {
|
|
|
|
return toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
|
2019-02-05 20:58:48 -05:00
|
|
|
// If we reduce quorum to nil, means we have deleted buckets properly
|
|
|
|
// on some servers in quorum, we should look for volumeNotEmpty errors
|
|
|
|
// and delete those buckets as well.
|
|
|
|
deleteDanglingBucket(ctx, storageDisks, dErrs, bucket)
|
|
|
|
|
2018-02-09 18:19:30 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetBucketPolicy sets policy on bucket
|
2018-04-24 18:53:30 -04:00
|
|
|
func (xl xlObjects) SetBucketPolicy(ctx context.Context, bucket string, policy *policy.Policy) error {
|
2018-10-09 17:00:01 -04:00
|
|
|
return savePolicyConfig(ctx, xl, bucket, policy)
|
2018-02-09 18:19:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketPolicy will get policy on bucket
|
2018-04-24 18:53:30 -04:00
|
|
|
func (xl xlObjects) GetBucketPolicy(ctx context.Context, bucket string) (*policy.Policy, error) {
|
2018-06-27 02:59:48 -04:00
|
|
|
return getPolicyConfig(xl, bucket)
|
2018-02-09 18:19:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBucketPolicy deletes all policies on bucket
|
2018-03-14 15:01:47 -04:00
|
|
|
func (xl xlObjects) DeleteBucketPolicy(ctx context.Context, bucket string) error {
|
2018-04-24 18:53:30 -04:00
|
|
|
return removePolicyConfig(ctx, xl, bucket)
|
2018-02-09 18:19:30 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// IsNotificationSupported returns whether bucket notification is applicable for this layer.
|
|
|
|
func (xl xlObjects) IsNotificationSupported() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2018-12-05 17:03:42 -05:00
|
|
|
// IsListenBucketSupported returns whether listen bucket notification is applicable for this layer.
|
|
|
|
func (xl xlObjects) IsListenBucketSupported() bool {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2019-01-05 17:16:43 -05:00
|
|
|
// IsEncryptionSupported returns whether server side encryption is implemented for this layer.
|
2018-02-09 18:19:30 -05:00
|
|
|
func (xl xlObjects) IsEncryptionSupported() bool {
|
|
|
|
return true
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2018-09-27 23:36:17 -04:00
|
|
|
|
|
|
|
// IsCompressionSupported returns whether compression is applicable for this layer.
|
|
|
|
func (xl xlObjects) IsCompressionSupported() bool {
|
|
|
|
return true
|
|
|
|
}
|