2016-03-28 06:52:38 +02: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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
slashpath "path"
|
|
|
|
"strings"
|
|
|
|
|
2016-05-05 21:51:56 +02:00
|
|
|
"path"
|
|
|
|
|
2016-04-25 08:12:54 +02:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-03-28 06:52:38 +02:00
|
|
|
"github.com/klauspost/reedsolomon"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-05-03 02:42:01 +02:00
|
|
|
// XL erasure metadata file.
|
|
|
|
xlMetaV1File = "file.json"
|
2016-03-28 06:52:38 +02:00
|
|
|
// Maximum erasure blocks.
|
|
|
|
maxErasureBlocks = 16
|
|
|
|
)
|
|
|
|
|
2016-04-19 19:23:20 +02:00
|
|
|
// XL layer structure.
|
|
|
|
type XL struct {
|
2016-04-29 10:29:09 +02:00
|
|
|
ReedSolomon reedsolomon.Encoder // Erasure encoder/decoder.
|
|
|
|
DataBlocks int
|
|
|
|
ParityBlocks int
|
|
|
|
storageDisks []StorageAPI
|
|
|
|
readQuorum int
|
|
|
|
writeQuorum int
|
2016-04-19 19:23:20 +02:00
|
|
|
}
|
|
|
|
|
2016-03-28 06:52:38 +02:00
|
|
|
// newXL instantiate a new XL.
|
|
|
|
func newXL(disks ...string) (StorageAPI, error) {
|
|
|
|
// Initialize XL.
|
|
|
|
xl := &XL{}
|
|
|
|
|
|
|
|
// Verify disks.
|
|
|
|
totalDisks := len(disks)
|
|
|
|
if totalDisks > maxErasureBlocks {
|
2016-04-22 00:33:26 +02:00
|
|
|
return nil, errMaxDisks
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// isEven function to verify if a given number if even.
|
|
|
|
isEven := func(number int) bool {
|
|
|
|
return number%2 == 0
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: verify if this makes sense in future.
|
|
|
|
if !isEven(totalDisks) {
|
2016-04-22 00:33:26 +02:00
|
|
|
return nil, errNumDisks
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate data and parity blocks.
|
|
|
|
dataBlocks, parityBlocks := totalDisks/2, totalDisks/2
|
|
|
|
|
|
|
|
// Initialize reed solomon encoding.
|
|
|
|
rs, err := reedsolomon.New(dataBlocks, parityBlocks)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save the reedsolomon.
|
|
|
|
xl.DataBlocks = dataBlocks
|
|
|
|
xl.ParityBlocks = parityBlocks
|
2016-04-22 00:33:26 +02:00
|
|
|
xl.ReedSolomon = rs
|
2016-03-28 06:52:38 +02:00
|
|
|
|
|
|
|
// Initialize all storage disks.
|
|
|
|
storageDisks := make([]StorageAPI, len(disks))
|
|
|
|
for index, disk := range disks {
|
|
|
|
var err error
|
2016-04-29 23:24:10 +02:00
|
|
|
storageDisks[index], err = newPosix(disk)
|
2016-03-28 06:52:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save all the initialized storage disks.
|
|
|
|
xl.storageDisks = storageDisks
|
|
|
|
|
2016-04-19 21:30:10 +02:00
|
|
|
// Figure out read and write quorum based on number of storage disks.
|
|
|
|
// Read quorum should be always N/2 + 1 (due to Vandermonde matrix
|
|
|
|
// erasure requirements)
|
|
|
|
xl.readQuorum = len(xl.storageDisks)/2 + 1
|
2016-04-22 00:33:26 +02:00
|
|
|
|
2016-04-19 21:30:10 +02:00
|
|
|
// Write quorum is assumed if we have total disks + 3
|
|
|
|
// parity. (Need to discuss this again)
|
|
|
|
xl.writeQuorum = len(xl.storageDisks)/2 + 3
|
2016-04-19 19:23:20 +02:00
|
|
|
if xl.writeQuorum > len(xl.storageDisks) {
|
|
|
|
xl.writeQuorum = len(xl.storageDisks)
|
|
|
|
}
|
|
|
|
|
2016-03-28 06:52:38 +02:00
|
|
|
// Return successfully initialized.
|
|
|
|
return xl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeVol - make a volume.
|
|
|
|
func (xl XL) MakeVol(volume string) error {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2016-05-02 03:42:00 +02:00
|
|
|
|
|
|
|
// Verify if the volume already exists.
|
|
|
|
_, errs := xl.getAllVolumeInfo(volume)
|
|
|
|
|
|
|
|
// Count errors other than errVolumeNotFound, bigger than the allowed
|
|
|
|
// readQuorum, if yes throw an error.
|
|
|
|
errCount := 0
|
|
|
|
for _, err := range errs {
|
|
|
|
if err != nil && err != errVolumeNotFound {
|
|
|
|
errCount++
|
|
|
|
if errCount > xl.readQuorum {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
}).Errorf("%s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
createVolErr := 0
|
|
|
|
volumeExistsErrCnt := 0
|
2016-03-28 06:52:38 +02:00
|
|
|
// Make a volume entry on all underlying storage disks.
|
2016-05-02 03:42:00 +02:00
|
|
|
for _, disk := range xl.storageDisks {
|
2016-03-28 06:52:38 +02:00
|
|
|
if err := disk.MakeVol(volume); err != nil {
|
2016-04-25 08:12:54 +02:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
2016-04-26 22:03:37 +02:00
|
|
|
}).Errorf("MakeVol failed with %s", err)
|
2016-05-02 03:42:00 +02:00
|
|
|
// if volume already exists, count them.
|
2016-04-21 21:57:14 +02:00
|
|
|
if err == errVolumeExists {
|
2016-05-02 03:42:00 +02:00
|
|
|
volumeExistsErrCnt++
|
|
|
|
// Return err if all disks report volume exists.
|
|
|
|
if volumeExistsErrCnt == len(xl.storageDisks) {
|
|
|
|
return errVolumeExists
|
|
|
|
}
|
2016-04-21 21:57:14 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-05-02 03:42:00 +02:00
|
|
|
// Update error counter separately.
|
|
|
|
createVolErr++
|
|
|
|
if createVolErr <= len(xl.storageDisks)-xl.writeQuorum {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return errWriteQuorum
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVol - delete a volume.
|
|
|
|
func (xl XL) DeleteVol(volume string) error {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2016-04-23 01:19:45 +02:00
|
|
|
|
|
|
|
// Collect if all disks report volume not found.
|
2016-05-02 03:42:00 +02:00
|
|
|
var volumeNotFoundErrCnt int
|
2016-04-23 01:19:45 +02:00
|
|
|
|
|
|
|
// Remove a volume entry on all underlying storage disks.
|
2016-05-02 03:42:00 +02:00
|
|
|
for _, disk := range xl.storageDisks {
|
2016-03-28 06:52:38 +02:00
|
|
|
if err := disk.DeleteVol(volume); err != nil {
|
2016-04-25 08:12:54 +02:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
2016-04-26 22:03:37 +02:00
|
|
|
}).Errorf("DeleteVol failed with %s", err)
|
2016-04-21 21:57:14 +02:00
|
|
|
// We ignore error if errVolumeNotFound.
|
|
|
|
if err == errVolumeNotFound {
|
2016-05-02 03:42:00 +02:00
|
|
|
volumeNotFoundErrCnt++
|
2016-04-21 21:57:14 +02:00
|
|
|
continue
|
|
|
|
}
|
2016-03-28 06:52:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-04-23 01:19:45 +02:00
|
|
|
// Return err if all disks report volume not found.
|
2016-05-02 03:42:00 +02:00
|
|
|
if volumeNotFoundErrCnt == len(xl.storageDisks) {
|
2016-04-23 01:19:45 +02:00
|
|
|
return errVolumeNotFound
|
|
|
|
}
|
2016-03-28 06:52:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListVols - list volumes.
|
|
|
|
func (xl XL) ListVols() (volsInfo []VolInfo, err error) {
|
2016-04-21 21:57:14 +02:00
|
|
|
emptyCount := 0
|
|
|
|
// Success vols map carries successful results of ListVols from
|
|
|
|
// each disks.
|
|
|
|
var successVolsMap = make(map[int][]VolInfo)
|
|
|
|
for index, disk := range xl.storageDisks {
|
|
|
|
var vlsInfo []VolInfo
|
|
|
|
vlsInfo, err = disk.ListVols()
|
|
|
|
if err == nil {
|
|
|
|
if len(vlsInfo) == 0 {
|
|
|
|
emptyCount++
|
|
|
|
} else {
|
|
|
|
successVolsMap[index] = vlsInfo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If all list operations resulted in an empty count which is same
|
|
|
|
// as your total storage disks, then it is a valid case return
|
|
|
|
// success with empty vols.
|
|
|
|
if emptyCount == len(xl.storageDisks) {
|
|
|
|
return []VolInfo{}, nil
|
|
|
|
} else if len(successVolsMap) < xl.readQuorum {
|
|
|
|
// If there is data and not empty, then we attempt quorum verification.
|
|
|
|
// Verify if we have enough quorum to list vols.
|
|
|
|
return nil, errReadQuorum
|
|
|
|
}
|
2016-05-01 08:34:43 +02:00
|
|
|
|
|
|
|
var total, free int64
|
|
|
|
// Loop through success vols map and get aggregated usage values.
|
2016-04-21 21:57:14 +02:00
|
|
|
for index := range xl.storageDisks {
|
|
|
|
if _, ok := successVolsMap[index]; ok {
|
|
|
|
volsInfo = successVolsMap[index]
|
2016-05-01 08:34:43 +02:00
|
|
|
free += volsInfo[0].Free
|
|
|
|
total += volsInfo[0].Total
|
2016-04-21 21:57:14 +02:00
|
|
|
}
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
2016-05-01 08:34:43 +02:00
|
|
|
// Save the updated usage values back into the vols.
|
|
|
|
for index := range volsInfo {
|
|
|
|
volsInfo[index].Free = free
|
|
|
|
volsInfo[index].Total = total
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: 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.
|
2016-04-21 21:57:14 +02:00
|
|
|
return volsInfo, nil
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 03:42:00 +02:00
|
|
|
// getAllVolumeInfo - get bucket volume info from all disks.
|
|
|
|
// Returns error slice indicating the failed volume stat operations.
|
|
|
|
func (xl XL) getAllVolumeInfo(volume string) (volsInfo []VolInfo, errs []error) {
|
|
|
|
errs = make([]error, len(xl.storageDisks))
|
|
|
|
volsInfo = make([]VolInfo, len(xl.storageDisks))
|
|
|
|
for index, disk := range xl.storageDisks {
|
|
|
|
volInfo, err := disk.StatVol(volume)
|
|
|
|
if err != nil {
|
|
|
|
errs[index] = err
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
volsInfo[index] = volInfo
|
|
|
|
}
|
|
|
|
return volsInfo, errs
|
|
|
|
}
|
|
|
|
|
|
|
|
// listAllVolumeInfo - list all stat volume info from all disks.
|
|
|
|
// Returns
|
|
|
|
// - stat volume info for all online disks.
|
|
|
|
// - boolean to indicate if healing is necessary.
|
|
|
|
// - error if any.
|
|
|
|
func (xl XL) listAllVolumeInfo(volume string) ([]VolInfo, bool, error) {
|
|
|
|
volsInfo, errs := xl.getAllVolumeInfo(volume)
|
|
|
|
notFoundCount := 0
|
|
|
|
for _, err := range errs {
|
|
|
|
if err == errVolumeNotFound {
|
|
|
|
notFoundCount++
|
|
|
|
// If we have errors with file not found greater than allowed read
|
|
|
|
// quorum we return err as errFileNotFound.
|
|
|
|
if notFoundCount > len(xl.storageDisks)-xl.readQuorum {
|
|
|
|
return nil, false, errVolumeNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Calculate online disk count.
|
|
|
|
onlineDiskCount := 0
|
|
|
|
for index := range errs {
|
|
|
|
if errs[index] == nil {
|
|
|
|
onlineDiskCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var heal bool
|
|
|
|
// If online disks count is lesser than configured disks, most
|
|
|
|
// probably we need to heal the file, additionally verify if the
|
|
|
|
// count is lesser than readQuorum, if not we throw an error.
|
|
|
|
if onlineDiskCount < len(xl.storageDisks) {
|
|
|
|
// Online disks lesser than total storage disks, needs to be
|
|
|
|
// healed. unless we do not have readQuorum.
|
|
|
|
heal = true
|
|
|
|
// Verify if online disks count are lesser than readQuorum
|
|
|
|
// threshold, return an error if yes.
|
|
|
|
if onlineDiskCount < xl.readQuorum {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"onlineDiskCount": onlineDiskCount,
|
|
|
|
"readQuorumCount": xl.readQuorum,
|
|
|
|
}).Errorf("%s", errReadQuorum)
|
|
|
|
return nil, false, errReadQuorum
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return success.
|
|
|
|
return volsInfo, heal, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// healVolume - heals any missing volumes.
|
|
|
|
func (xl XL) healVolume(volume string) error {
|
|
|
|
// Lists volume info for all online disks.
|
|
|
|
volsInfo, heal, err := xl.listAllVolumeInfo(volume)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
}).Errorf("List online disks failed with %s", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !heal {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
// Create volume if missing on online disks.
|
|
|
|
for index, volInfo := range volsInfo {
|
|
|
|
if volInfo.Name != "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// Volinfo name would be an empty string, create it.
|
|
|
|
if err = xl.storageDisks[index].MakeVol(volume); err != nil {
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
}).Errorf("MakeVol failed with error %s", err)
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-03-28 06:52:38 +02:00
|
|
|
// StatVol - get volume stat info.
|
|
|
|
func (xl XL) StatVol(volume string) (volInfo VolInfo, err error) {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return VolInfo{}, errInvalidArgument
|
|
|
|
}
|
2016-05-02 03:42:00 +02:00
|
|
|
volsInfo, heal, err := xl.listAllVolumeInfo(volume)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
}).Errorf("listOnlineVolsInfo failed with %s", err)
|
|
|
|
return VolInfo{}, err
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
2016-04-21 21:57:14 +02:00
|
|
|
|
2016-05-02 03:42:00 +02:00
|
|
|
if heal {
|
|
|
|
go func() {
|
|
|
|
if err = xl.healVolume(volume); err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
}).Errorf("healVolume failed with %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2016-04-21 21:57:14 +02:00
|
|
|
}
|
|
|
|
|
2016-05-01 08:34:43 +02:00
|
|
|
// Loop through all statVols, calculate the actual usage values.
|
|
|
|
var total, free int64
|
2016-05-02 03:42:00 +02:00
|
|
|
for _, volInfo := range volsInfo {
|
|
|
|
free += volInfo.Free
|
|
|
|
total += volInfo.Total
|
2016-05-01 08:34:43 +02:00
|
|
|
}
|
2016-05-02 03:42:00 +02:00
|
|
|
// Filter volsInfo and update the volInfo.
|
|
|
|
volInfo = removeDuplicateVols(volsInfo)[0]
|
2016-05-01 08:34:43 +02:00
|
|
|
volInfo.Free = free
|
|
|
|
volInfo.Total = total
|
2016-04-21 21:57:14 +02:00
|
|
|
return volInfo, nil
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-05 21:51:56 +02:00
|
|
|
// isLeafDirectoryXL - check if a given path is leaf directory. i.e
|
|
|
|
// if it contains file xlMetaV1File
|
|
|
|
func isLeafDirectoryXL(disk StorageAPI, volume, leafPath string) (isLeaf bool) {
|
|
|
|
_, err := disk.StatFile(volume, path.Join(leafPath, xlMetaV1File))
|
|
|
|
return err == nil
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-05 21:51:56 +02:00
|
|
|
// ListDir - return all the entries at the given directory path.
|
|
|
|
// If an entry is a directory it will be returned with a trailing "/".
|
|
|
|
func (xl XL) ListDir(volume, dirPath string) (entries []string, err error) {
|
2016-03-28 06:52:38 +02:00
|
|
|
if !isValidVolname(volume) {
|
2016-05-05 21:51:56 +02:00
|
|
|
return nil, errInvalidArgument
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
2016-05-05 21:51:56 +02:00
|
|
|
// FIXME: need someway to figure out which disk has the latest namespace
|
|
|
|
// so that Listing can be done there. One option is always do Listing from
|
|
|
|
// the "local" disk - if it is down user has to list using another XL server.
|
|
|
|
// This way user knows from which disk he is listing from.
|
2016-04-28 06:09:26 +02:00
|
|
|
|
|
|
|
for _, disk := range xl.storageDisks {
|
2016-05-05 21:51:56 +02:00
|
|
|
if entries, err = disk.ListDir(volume, dirPath); err != nil {
|
|
|
|
continue
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
2016-05-05 21:51:56 +02:00
|
|
|
for i, entry := range entries {
|
|
|
|
if strings.HasSuffix(entry, slashSeparator) && isLeafDirectoryXL(disk, volume, path.Join(dirPath, entry)) {
|
|
|
|
entries[i] = strings.TrimSuffix(entry, slashSeparator)
|
2016-04-26 19:35:39 +02:00
|
|
|
}
|
|
|
|
}
|
2016-05-08 21:05:19 +02:00
|
|
|
// We have list from one of the disks hence break the loop.
|
|
|
|
break
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
2016-05-05 21:51:56 +02:00
|
|
|
return
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Object API.
|
|
|
|
|
|
|
|
// StatFile - stat a file
|
|
|
|
func (xl XL) StatFile(volume, path string) (FileInfo, error) {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return FileInfo{}, errInvalidArgument
|
|
|
|
}
|
|
|
|
if !isValidPath(path) {
|
|
|
|
return FileInfo{}, errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2016-04-22 18:04:17 +02:00
|
|
|
// Acquire read lock.
|
2016-04-29 10:29:09 +02:00
|
|
|
nsMutex.RLock(volume, path)
|
2016-04-26 22:03:37 +02:00
|
|
|
_, metadata, heal, err := xl.listOnlineDisks(volume, path)
|
2016-04-29 10:29:09 +02:00
|
|
|
nsMutex.RUnlock(volume, path)
|
2016-04-22 18:04:17 +02:00
|
|
|
if err != nil {
|
2016-04-25 08:12:54 +02:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-27 02:57:16 +02:00
|
|
|
}).Errorf("listOnlineDisks failed with %s", err)
|
2016-04-22 18:04:17 +02:00
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
|
|
|
|
2016-04-26 22:03:37 +02:00
|
|
|
if heal {
|
2016-04-28 00:10:19 +02:00
|
|
|
// Heal in background safely, since we already have read quorum disks.
|
|
|
|
go func() {
|
|
|
|
if err = xl.healFile(volume, path); err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
|
|
|
}).Errorf("healFile failed with %s", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}()
|
2016-04-22 18:04:17 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Return file info.
|
|
|
|
return FileInfo{
|
|
|
|
Volume: volume,
|
|
|
|
Name: path,
|
2016-04-29 04:27:02 +02:00
|
|
|
Size: metadata.Stat.Size,
|
|
|
|
ModTime: metadata.Stat.ModTime,
|
2016-04-22 18:04:17 +02:00
|
|
|
Mode: os.FileMode(0644),
|
|
|
|
}, nil
|
2016-03-28 06:52:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFile - delete a file
|
|
|
|
func (xl XL) DeleteFile(volume, path string) error {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
if !isValidPath(path) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2016-04-30 03:43:18 +02:00
|
|
|
|
|
|
|
nsMutex.Lock(volume, path)
|
|
|
|
defer nsMutex.Unlock(volume, path)
|
|
|
|
|
2016-04-30 11:52:15 +02:00
|
|
|
errCount := 0
|
|
|
|
// Update meta data file and remove part file
|
2016-03-28 06:52:38 +02:00
|
|
|
for index, disk := range xl.storageDisks {
|
2016-05-06 05:28:22 +02:00
|
|
|
erasureFilePart := slashpath.Join(path, fmt.Sprintf("file.%d", index))
|
|
|
|
err := disk.DeleteFile(volume, erasureFilePart)
|
2016-04-30 11:52:15 +02:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-05-06 05:28:22 +02:00
|
|
|
}).Errorf("DeleteFile failed with %s", err)
|
2016-04-30 03:43:18 +02:00
|
|
|
|
2016-04-30 11:52:15 +02:00
|
|
|
errCount++
|
|
|
|
|
2016-05-06 05:28:22 +02:00
|
|
|
// We can safely allow DeleteFile errors up to len(xl.storageDisks) - xl.writeQuorum
|
2016-04-30 11:52:15 +02:00
|
|
|
// otherwise return failure.
|
|
|
|
if errCount <= len(xl.storageDisks)-xl.writeQuorum {
|
|
|
|
continue
|
2016-04-30 03:43:18 +02:00
|
|
|
}
|
|
|
|
|
2016-04-30 11:52:15 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-06 05:28:22 +02:00
|
|
|
xlMetaV1FilePath := slashpath.Join(path, "file.json")
|
|
|
|
err = disk.DeleteFile(volume, xlMetaV1FilePath)
|
2016-03-28 06:52:38 +02:00
|
|
|
if err != nil {
|
2016-04-25 08:12:54 +02:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 22:03:37 +02:00
|
|
|
}).Errorf("DeleteFile failed with %s", err)
|
2016-04-30 11:52:15 +02:00
|
|
|
|
|
|
|
errCount++
|
|
|
|
|
2016-05-06 05:28:22 +02:00
|
|
|
// We can safely allow DeleteFile errors up to len(xl.storageDisks) - xl.writeQuorum
|
2016-04-30 11:52:15 +02:00
|
|
|
// otherwise return failure.
|
|
|
|
if errCount <= len(xl.storageDisks)-xl.writeQuorum {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-03-28 06:52:38 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-04-30 11:52:15 +02:00
|
|
|
|
2016-05-04 01:10:24 +02:00
|
|
|
// Return success.
|
2016-03-28 06:52:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
2016-04-29 21:17:48 +02:00
|
|
|
|
|
|
|
// RenameFile - rename file.
|
|
|
|
func (xl XL) RenameFile(srcVolume, srcPath, dstVolume, dstPath string) error {
|
2016-04-29 23:24:10 +02:00
|
|
|
// Validate inputs.
|
2016-04-29 21:17:48 +02:00
|
|
|
if !isValidVolname(srcVolume) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
if !isValidPath(srcPath) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
if !isValidVolname(dstVolume) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
if !isValidPath(dstPath) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2016-05-04 01:10:24 +02:00
|
|
|
|
|
|
|
// Hold read lock at source before rename.
|
|
|
|
nsMutex.RLock(srcVolume, srcPath)
|
|
|
|
defer nsMutex.RUnlock(srcVolume, srcPath)
|
|
|
|
|
|
|
|
// Hold write lock at destination before rename.
|
|
|
|
nsMutex.Lock(dstVolume, dstPath)
|
|
|
|
defer nsMutex.Unlock(dstVolume, dstPath)
|
|
|
|
|
2016-05-05 22:03:53 +02:00
|
|
|
errCount := 0
|
2016-05-04 01:10:24 +02:00
|
|
|
for index, disk := range xl.storageDisks {
|
|
|
|
// Make sure to rename all the files only, not directories.
|
|
|
|
srcErasurePartPath := slashpath.Join(srcPath, fmt.Sprintf("file.%d", index))
|
|
|
|
dstErasurePartPath := slashpath.Join(dstPath, fmt.Sprintf("file.%d", index))
|
|
|
|
err := disk.RenameFile(srcVolume, srcErasurePartPath, dstVolume, dstErasurePartPath)
|
|
|
|
if err != nil {
|
2016-04-29 23:24:10 +02:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"srcVolume": srcVolume,
|
2016-05-04 01:10:24 +02:00
|
|
|
"srcPath": srcErasurePartPath,
|
2016-04-29 23:24:10 +02:00
|
|
|
"dstVolume": dstVolume,
|
2016-05-04 01:10:24 +02:00
|
|
|
"dstPath": dstErasurePartPath,
|
2016-04-29 23:24:10 +02:00
|
|
|
}).Errorf("RenameFile failed with %s", err)
|
2016-05-05 22:03:53 +02:00
|
|
|
|
|
|
|
errCount++
|
|
|
|
// We can safely allow RenameFile errors up to len(xl.storageDisks) - xl.writeQuorum
|
|
|
|
// otherwise return failure.
|
|
|
|
if errCount <= len(xl.storageDisks)-xl.writeQuorum {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-04-29 21:17:48 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-05-04 01:10:24 +02:00
|
|
|
srcXLMetaPath := slashpath.Join(srcPath, xlMetaV1File)
|
|
|
|
dstXLMetaPath := slashpath.Join(dstPath, xlMetaV1File)
|
|
|
|
err = disk.RenameFile(srcVolume, srcXLMetaPath, dstVolume, dstXLMetaPath)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"srcVolume": srcVolume,
|
|
|
|
"srcPath": srcXLMetaPath,
|
|
|
|
"dstVolume": dstVolume,
|
|
|
|
"dstPath": dstXLMetaPath,
|
|
|
|
}).Errorf("RenameFile failed with %s", err)
|
2016-05-05 22:03:53 +02:00
|
|
|
|
|
|
|
errCount++
|
|
|
|
// We can safely allow RenameFile errors up to len(xl.storageDisks) - xl.writeQuorum
|
|
|
|
// otherwise return failure.
|
|
|
|
if errCount <= len(xl.storageDisks)-xl.writeQuorum {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:10:24 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = disk.DeleteFile(srcVolume, srcPath)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"srcVolume": srcVolume,
|
|
|
|
"srcPath": srcPath,
|
|
|
|
}).Errorf("DeleteFile failed with %s", err)
|
2016-05-05 22:03:53 +02:00
|
|
|
|
|
|
|
errCount++
|
|
|
|
// We can safely allow RenameFile errors up to len(xl.storageDisks) - xl.writeQuorum
|
|
|
|
// otherwise return failure.
|
|
|
|
if errCount <= len(xl.storageDisks)-xl.writeQuorum {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:10:24 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-04-29 21:17:48 +02:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|