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 (
|
|
|
|
"sort"
|
|
|
|
"sync"
|
|
|
|
)
|
|
|
|
|
|
|
|
/// Bucket operations
|
|
|
|
|
|
|
|
// MakeBucket - make a bucket.
|
|
|
|
func (xl xlObjects) MakeBucket(bucket string) error {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMutex.Lock(bucket, "")
|
|
|
|
defer nsMutex.Unlock(bucket, "")
|
|
|
|
|
|
|
|
// Err counters.
|
|
|
|
createVolErr := 0 // Count generic create vol errs.
|
|
|
|
volumeExistsErrCnt := 0 // Count all errVolumeExists errs.
|
|
|
|
|
|
|
|
// 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 {
|
|
|
|
dErrs[index] = errDiskNotFound
|
|
|
|
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 {
|
|
|
|
dErrs[index] = err
|
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all make vol to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-05-24 20:48:58 -04:00
|
|
|
// Look for specific errors and count them to be verified later.
|
2016-05-20 23:48:47 -04:00
|
|
|
for _, err := range dErrs {
|
|
|
|
if err == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// if volume already exists, count them.
|
|
|
|
if err == errVolumeExists {
|
|
|
|
volumeExistsErrCnt++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update error counter separately.
|
|
|
|
createVolErr++
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return err if all disks report volume exists.
|
2016-06-02 19:34:15 -04:00
|
|
|
if volumeExistsErrCnt > len(xl.storageDisks)-xl.readQuorum {
|
2016-05-20 23:48:47 -04:00
|
|
|
return toObjectErr(errVolumeExists, bucket)
|
|
|
|
} else if createVolErr > len(xl.storageDisks)-xl.writeQuorum {
|
2016-06-02 19:34:15 -04:00
|
|
|
// Return errXLWriteQuorum if errors were more than allowed write quorum.
|
|
|
|
return toObjectErr(errXLWriteQuorum, bucket)
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
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) {
|
2016-06-01 19:43:31 -04:00
|
|
|
for _, disk := range xl.getLoadBalancedQuorumDisks() {
|
2016-06-02 19:34:15 -04:00
|
|
|
if disk == nil {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
var volInfo VolInfo
|
|
|
|
volInfo, err = disk.StatVol(bucketName)
|
2016-06-01 19:43:31 -04:00
|
|
|
if err != nil {
|
2016-06-03 01:49:27 -04:00
|
|
|
// For any reason disk went offline continue and pick the next one.
|
2016-06-02 19:34:15 -04:00
|
|
|
if err == errDiskNotFound {
|
|
|
|
continue
|
|
|
|
}
|
2016-06-01 19:43:31 -04:00
|
|
|
return BucketInfo{}, err
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-06-01 19:43:31 -04:00
|
|
|
bucketInfo = BucketInfo{
|
|
|
|
Name: volInfo.Name,
|
|
|
|
Created: volInfo.Created,
|
|
|
|
}
|
|
|
|
break
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-06-01 19:43:31 -04:00
|
|
|
return bucketInfo, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Checks whether bucket exists.
|
2016-05-26 06:15:01 -04:00
|
|
|
func (xl xlObjects) isBucketExist(bucket string) bool {
|
|
|
|
nsMutex.RLock(bucket, "")
|
|
|
|
defer nsMutex.RUnlock(bucket, "")
|
|
|
|
|
2016-05-20 23:48:47 -04:00
|
|
|
// Check whether bucket exists.
|
2016-05-26 06:15:01 -04:00
|
|
|
_, err := xl.getBucketInfo(bucket)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
|
|
|
if err == errVolumeNotFound {
|
|
|
|
return false
|
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
errorIf(err, "Stat failed on bucket "+bucket+".")
|
2016-05-20 23:48:47 -04:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2016-05-26 06:15:01 -04:00
|
|
|
// GetBucketInfo - returns BucketInfo for a bucket.
|
2016-05-20 23:48:47 -04:00
|
|
|
func (xl xlObjects) GetBucketInfo(bucket string) (BucketInfo, error) {
|
|
|
|
// Verify if bucket is valid.
|
|
|
|
if !IsValidBucketName(bucket) {
|
|
|
|
return BucketInfo{}, BucketNameInvalid{Bucket: bucket}
|
|
|
|
}
|
|
|
|
nsMutex.RLock(bucket, "")
|
|
|
|
defer nsMutex.RUnlock(bucket, "")
|
2016-05-26 06:15:01 -04:00
|
|
|
bucketInfo, err := xl.getBucketInfo(bucket)
|
2016-05-20 23:48:47 -04:00
|
|
|
if err != nil {
|
|
|
|
return BucketInfo{}, toObjectErr(err, bucket)
|
|
|
|
}
|
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-06-01 19:43:31 -04:00
|
|
|
for _, disk := range xl.getLoadBalancedQuorumDisks() {
|
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()
|
2016-06-03 01:49:27 -04:00
|
|
|
// Ignore any disks not found.
|
|
|
|
if err == errDiskNotFound {
|
|
|
|
continue
|
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
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 {
|
|
|
|
// StorageAPI can send volume names which are incompatible
|
|
|
|
// with buckets, handle it and skip them.
|
|
|
|
if !IsValidBucketName(volInfo.Name) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
bucketsInfo = append(bucketsInfo, BucketInfo{
|
|
|
|
Name: volInfo.Name,
|
|
|
|
Created: volInfo.Created,
|
|
|
|
})
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
2016-05-26 06:15:01 -04:00
|
|
|
return bucketsInfo, nil
|
2016-05-20 23:48:47 -04:00
|
|
|
}
|
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}
|
|
|
|
}
|
|
|
|
|
|
|
|
nsMutex.Lock(bucket, "")
|
2016-05-28 05:43:01 -04:00
|
|
|
defer nsMutex.Unlock(bucket, "")
|
2016-05-20 23:48:47 -04:00
|
|
|
|
|
|
|
// Collect if all disks report volume not found.
|
|
|
|
var volumeNotFoundErrCnt int
|
|
|
|
|
|
|
|
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 {
|
|
|
|
dErrs[index] = errDiskNotFound
|
|
|
|
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()
|
|
|
|
err := disk.DeleteVol(bucket)
|
|
|
|
if err != nil {
|
|
|
|
dErrs[index] = err
|
|
|
|
}
|
|
|
|
}(index, disk)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for all the delete vols to finish.
|
|
|
|
wg.Wait()
|
|
|
|
|
2016-05-24 20:48:58 -04:00
|
|
|
// Count the errors for known errors, return quickly if we found
|
|
|
|
// an unknown error.
|
2016-05-20 23:48:47 -04:00
|
|
|
for _, err := range dErrs {
|
|
|
|
if err != nil {
|
|
|
|
// We ignore error if errVolumeNotFound or errDiskNotFound
|
|
|
|
if err == errVolumeNotFound || err == errDiskNotFound {
|
|
|
|
volumeNotFoundErrCnt++
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-24 20:48:58 -04:00
|
|
|
// Return errVolumeNotFound if all disks report volume not found.
|
2016-05-20 23:48:47 -04:00
|
|
|
if volumeNotFoundErrCnt == len(xl.storageDisks) {
|
|
|
|
return toObjectErr(errVolumeNotFound, bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|