2016-03-28 00:52:38 -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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
slashpath "path"
|
2016-04-28 04:48:57 -04:00
|
|
|
"sort"
|
2016-03-28 00:52:38 -04:00
|
|
|
"strings"
|
2016-04-19 13:23:20 -04:00
|
|
|
"sync"
|
2016-03-28 00:52:38 -04:00
|
|
|
|
2016-04-25 02:12:54 -04:00
|
|
|
"github.com/Sirupsen/logrus"
|
2016-03-28 00:52:38 -04:00
|
|
|
"github.com/klauspost/reedsolomon"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Part metadata file.
|
|
|
|
metadataFile = "part.json"
|
|
|
|
// Maximum erasure blocks.
|
|
|
|
maxErasureBlocks = 16
|
|
|
|
)
|
|
|
|
|
2016-04-19 13:23:20 -04:00
|
|
|
// XL layer structure.
|
|
|
|
type XL struct {
|
|
|
|
ReedSolomon reedsolomon.Encoder // Erasure encoder/decoder.
|
|
|
|
DataBlocks int
|
|
|
|
ParityBlocks int
|
|
|
|
storageDisks []StorageAPI
|
2016-04-19 15:30:10 -04:00
|
|
|
nameSpaceLockMap map[nameSpaceParam]*nameSpaceLock
|
2016-04-19 13:23:20 -04:00
|
|
|
nameSpaceLockMapMutex *sync.Mutex
|
|
|
|
readQuorum int
|
|
|
|
writeQuorum int
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// lockNS - locks the given resource, using a previously allocated
|
|
|
|
// name space lock or initializing a new one.
|
|
|
|
func (xl XL) lockNS(volume, path string, readLock bool) {
|
2016-04-19 13:23:20 -04:00
|
|
|
xl.nameSpaceLockMapMutex.Lock()
|
|
|
|
defer xl.nameSpaceLockMapMutex.Unlock()
|
|
|
|
|
|
|
|
param := nameSpaceParam{volume, path}
|
|
|
|
nsLock, found := xl.nameSpaceLockMap[param]
|
|
|
|
if !found {
|
2016-04-19 15:30:10 -04:00
|
|
|
nsLock = newNSLock()
|
2016-04-19 13:23:20 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
if readLock {
|
2016-04-19 13:23:20 -04:00
|
|
|
nsLock.RLock()
|
|
|
|
} else {
|
|
|
|
nsLock.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
xl.nameSpaceLockMap[param] = nsLock
|
|
|
|
}
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// unlockNS - unlocks any previously acquired read or write locks.
|
|
|
|
func (xl XL) unlockNS(volume, path string, readLock bool) {
|
2016-04-19 13:23:20 -04:00
|
|
|
xl.nameSpaceLockMapMutex.Lock()
|
|
|
|
defer xl.nameSpaceLockMapMutex.Unlock()
|
|
|
|
|
|
|
|
param := nameSpaceParam{volume, path}
|
|
|
|
if nsLock, found := xl.nameSpaceLockMap[param]; found {
|
2016-04-19 15:30:10 -04:00
|
|
|
if readLock {
|
2016-04-19 13:23:20 -04:00
|
|
|
nsLock.RUnlock()
|
|
|
|
} else {
|
|
|
|
nsLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
if nsLock.InUse() {
|
|
|
|
xl.nameSpaceLockMap[param] = nsLock
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-03-28 00:52:38 -04: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-21 18:33:26 -04:00
|
|
|
return nil, errMaxDisks
|
2016-03-28 00:52:38 -04: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-21 18:33:26 -04:00
|
|
|
return nil, errNumDisks
|
2016-03-28 00:52:38 -04: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-21 18:33:26 -04:00
|
|
|
xl.ReedSolomon = rs
|
2016-03-28 00:52:38 -04:00
|
|
|
|
|
|
|
// Initialize all storage disks.
|
|
|
|
storageDisks := make([]StorageAPI, len(disks))
|
|
|
|
for index, disk := range disks {
|
|
|
|
var err error
|
|
|
|
storageDisks[index], err = newFS(disk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save all the initialized storage disks.
|
|
|
|
xl.storageDisks = storageDisks
|
|
|
|
|
2016-04-19 15:30:10 -04:00
|
|
|
// Initialize name space lock map.
|
|
|
|
xl.nameSpaceLockMap = make(map[nameSpaceParam]*nameSpaceLock)
|
2016-04-19 13:23:20 -04:00
|
|
|
xl.nameSpaceLockMapMutex = &sync.Mutex{}
|
2016-04-19 15:30:10 -04: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-21 18:33:26 -04:00
|
|
|
|
2016-04-19 15:30:10 -04: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 13:23:20 -04:00
|
|
|
if xl.writeQuorum > len(xl.storageDisks) {
|
|
|
|
xl.writeQuorum = len(xl.storageDisks)
|
|
|
|
}
|
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Return successfully initialized.
|
|
|
|
return xl, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeVol - make a volume.
|
|
|
|
func (xl XL) MakeVol(volume string) error {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2016-04-22 19:19:45 -04:00
|
|
|
// Collect if all disks report volume exists.
|
|
|
|
var volumeExistsMap = make(map[int]struct{})
|
2016-03-28 00:52:38 -04:00
|
|
|
// Make a volume entry on all underlying storage disks.
|
2016-04-22 19:19:45 -04:00
|
|
|
for index, disk := range xl.storageDisks {
|
2016-03-28 00:52:38 -04:00
|
|
|
if err := disk.MakeVol(volume); err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("MakeVol failed with %s", err)
|
2016-04-21 15:57:14 -04:00
|
|
|
// We ignore error if errVolumeExists and creating a volume again.
|
|
|
|
if err == errVolumeExists {
|
2016-04-22 19:19:45 -04:00
|
|
|
volumeExistsMap[index] = struct{}{}
|
2016-04-21 15:57:14 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-04-22 19:19:45 -04:00
|
|
|
// Return err if all disks report volume exists.
|
|
|
|
if len(volumeExistsMap) == len(xl.storageDisks) {
|
|
|
|
return errVolumeExists
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVol - delete a volume.
|
|
|
|
func (xl XL) DeleteVol(volume string) error {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
2016-04-22 19:19:45 -04:00
|
|
|
|
|
|
|
// Collect if all disks report volume not found.
|
|
|
|
var volumeNotFoundMap = make(map[int]struct{})
|
|
|
|
|
|
|
|
// Remove a volume entry on all underlying storage disks.
|
|
|
|
for index, disk := range xl.storageDisks {
|
2016-03-28 00:52:38 -04:00
|
|
|
if err := disk.DeleteVol(volume); err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("DeleteVol failed with %s", err)
|
2016-04-21 15:57:14 -04:00
|
|
|
// We ignore error if errVolumeNotFound.
|
|
|
|
if err == errVolumeNotFound {
|
2016-04-22 19:19:45 -04:00
|
|
|
volumeNotFoundMap[index] = struct{}{}
|
2016-04-21 15:57:14 -04:00
|
|
|
continue
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2016-04-22 19:19:45 -04:00
|
|
|
// Return err if all disks report volume not found.
|
|
|
|
if len(volumeNotFoundMap) == len(xl.storageDisks) {
|
|
|
|
return errVolumeNotFound
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ListVols - list volumes.
|
|
|
|
func (xl XL) ListVols() (volsInfo []VolInfo, err error) {
|
2016-04-21 15:57:14 -04: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
|
|
|
|
}
|
|
|
|
// Loop through success vols map and return the first value.
|
|
|
|
for index := range xl.storageDisks {
|
|
|
|
if _, ok := successVolsMap[index]; ok {
|
|
|
|
volsInfo = successVolsMap[index]
|
|
|
|
break
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
2016-04-21 15:57:14 -04:00
|
|
|
return volsInfo, nil
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// StatVol - get volume stat info.
|
|
|
|
func (xl XL) StatVol(volume string) (volInfo VolInfo, err error) {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return VolInfo{}, errInvalidArgument
|
|
|
|
}
|
2016-04-21 15:57:14 -04:00
|
|
|
var statVols []VolInfo
|
|
|
|
volumeNotFoundErrCnt := 0
|
|
|
|
for _, disk := range xl.storageDisks {
|
|
|
|
volInfo, err = disk.StatVol(volume)
|
|
|
|
if err == nil {
|
|
|
|
// Collect all the successful attempts to verify quorum
|
|
|
|
// subsequently.
|
|
|
|
statVols = append(statVols, volInfo)
|
|
|
|
} else if err == errVolumeNotFound {
|
|
|
|
// Count total amount of volume not found errors.
|
|
|
|
volumeNotFoundErrCnt++
|
2016-04-25 02:12:54 -04:00
|
|
|
} else if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("StatVol failed with %s", err)
|
2016-04-25 02:12:54 -04:00
|
|
|
return VolInfo{}, err
|
2016-04-21 15:57:14 -04:00
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
2016-04-21 15:57:14 -04:00
|
|
|
|
|
|
|
// If volume not found err count is same as total storage disks, we
|
|
|
|
// really don't have the bucket, report a valid error.
|
|
|
|
if volumeNotFoundErrCnt == len(xl.storageDisks) {
|
|
|
|
return VolInfo{}, errVolumeNotFound
|
|
|
|
} else if len(statVols) < xl.readQuorum {
|
|
|
|
// If one of the disks have bucket we need to validate if we
|
|
|
|
// have read quorum, if not fail.
|
|
|
|
return VolInfo{}, errReadQuorum
|
|
|
|
}
|
|
|
|
|
|
|
|
// If successful remove all the duplicates and keep the latest one.
|
|
|
|
volInfo = removeDuplicateVols(statVols)[0]
|
|
|
|
return volInfo, nil
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// isLeafDirectory - check if a given path is leaf directory. i.e
|
|
|
|
// there are no more directories inside it. Erasure code backend
|
|
|
|
// format it means that the parent directory is the actual object name.
|
2016-04-28 20:32:46 -04:00
|
|
|
func isLeafDirectory(disk StorageAPI, volume, leafPath string) (isLeaf bool) {
|
2016-04-19 23:05:38 -04:00
|
|
|
var markerPath string
|
2016-04-28 20:32:46 -04:00
|
|
|
var xlListCount = 1000 // Count page.
|
2016-03-28 00:52:38 -04:00
|
|
|
for {
|
2016-04-28 20:32:46 -04:00
|
|
|
fileInfos, eof, err := disk.ListFiles(volume, leafPath, markerPath, false, xlListCount)
|
2016-04-25 02:12:54 -04:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"leafPath": leafPath,
|
|
|
|
"markerPath": markerPath,
|
|
|
|
"recursive": false,
|
2016-04-28 20:32:46 -04:00
|
|
|
"count": xlListCount,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("ListFiles failed with %s", err)
|
2016-03-28 00:52:38 -04:00
|
|
|
break
|
|
|
|
}
|
2016-04-28 20:32:46 -04:00
|
|
|
for _, fileInfo := range fileInfos {
|
|
|
|
if fileInfo.Mode.IsDir() {
|
|
|
|
// Directory found, not a leaf directory, return right here.
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
if eof {
|
|
|
|
break
|
|
|
|
}
|
2016-04-19 23:05:38 -04:00
|
|
|
// MarkerPath to get the next set of files.
|
2016-04-28 20:32:46 -04:00
|
|
|
markerPath = fileInfos[len(fileInfos)-1].Name
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
// Exhausted all the entries, no directories found must be leaf
|
|
|
|
// return right here.
|
2016-04-28 20:32:46 -04:00
|
|
|
return true
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// extractMetadata - extract file metadata.
|
2016-04-28 20:32:46 -04:00
|
|
|
func extractMetadata(disk StorageAPI, volume, path string) (fileMetadata, error) {
|
2016-03-28 00:52:38 -04:00
|
|
|
metadataFilePath := slashpath.Join(path, metadataFile)
|
|
|
|
// We are not going to read partial data from metadata file,
|
|
|
|
// read the whole file always.
|
|
|
|
offset := int64(0)
|
|
|
|
metadataReader, err := disk.ReadFile(volume, metadataFilePath, offset)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": metadataFilePath,
|
|
|
|
"offset": offset,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("ReadFile failed with %s", err)
|
2016-04-21 18:33:26 -04:00
|
|
|
return nil, err
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
// Close metadata reader.
|
|
|
|
defer metadataReader.Close()
|
|
|
|
|
2016-04-21 18:33:26 -04:00
|
|
|
metadata, err := fileMetadataDecode(metadataReader)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": metadataFilePath,
|
|
|
|
"offset": offset,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("fileMetadataDecode failed with %s", err)
|
2016-04-21 18:33:26 -04:00
|
|
|
return nil, err
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
2016-04-21 18:33:26 -04:00
|
|
|
return metadata, nil
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
|
2016-04-21 18:33:26 -04:00
|
|
|
// Extract file info from paths.
|
2016-04-28 20:32:46 -04:00
|
|
|
func extractFileInfo(disk StorageAPI, volume, path string) (FileInfo, error) {
|
2016-04-21 18:33:26 -04:00
|
|
|
fileInfo := FileInfo{}
|
|
|
|
fileInfo.Volume = volume
|
|
|
|
fileInfo.Name = path
|
2016-04-19 13:23:20 -04:00
|
|
|
|
2016-04-28 20:32:46 -04:00
|
|
|
metadata, err := extractMetadata(disk, volume, path)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("extractMetadata failed with %s", err)
|
2016-04-21 18:33:26 -04:00
|
|
|
return FileInfo{}, err
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
2016-04-22 12:04:17 -04:00
|
|
|
fileSize, err := metadata.GetSize()
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("GetSize failed with %s", err)
|
2016-04-21 18:33:26 -04:00
|
|
|
return FileInfo{}, err
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
2016-04-22 12:04:17 -04:00
|
|
|
fileModTime, err := metadata.GetModTime()
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("GetModTime failed with %s", err)
|
2016-04-21 18:33:26 -04:00
|
|
|
return FileInfo{}, err
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
2016-04-21 18:33:26 -04:00
|
|
|
fileInfo.Size = fileSize
|
|
|
|
fileInfo.Mode = os.FileMode(0644)
|
|
|
|
fileInfo.ModTime = fileModTime
|
|
|
|
return fileInfo, nil
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// byFileInfoName is a collection satisfying sort.Interface.
|
|
|
|
type byFileInfoName []FileInfo
|
|
|
|
|
|
|
|
func (d byFileInfoName) Len() int { return len(d) }
|
|
|
|
func (d byFileInfoName) Swap(i, j int) { d[i], d[j] = d[j], d[i] }
|
|
|
|
func (d byFileInfoName) Less(i, j int) bool { return d[i].Name < d[j].Name }
|
|
|
|
|
|
|
|
// ListFiles files at prefix.
|
|
|
|
func (xl XL) ListFiles(volume, prefix, marker string, recursive bool, count int) (filesInfo []FileInfo, eof bool, err error) {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return nil, true, errInvalidArgument
|
|
|
|
}
|
2016-04-28 00:09:26 -04:00
|
|
|
|
|
|
|
// TODO: Fix: If readQuorum is met, its assumed that disks are in consistent file list.
|
|
|
|
// exclude disks those are not in consistent file list and check count of remaining disks
|
|
|
|
// are met readQuorum.
|
|
|
|
|
|
|
|
// Treat empty file list specially
|
|
|
|
emptyCount := 0
|
|
|
|
errCount := 0
|
|
|
|
successCount := 0
|
|
|
|
|
|
|
|
var firstFilesInfo []FileInfo
|
|
|
|
var firstEOF bool
|
|
|
|
var firstErr error
|
|
|
|
|
|
|
|
for _, disk := range xl.storageDisks {
|
2016-04-28 20:32:46 -04:00
|
|
|
if filesInfo, eof, err = listFiles(disk, volume, prefix, marker, recursive, count); err == nil {
|
2016-04-28 00:09:26 -04:00
|
|
|
// we need to return first successful result
|
|
|
|
if firstFilesInfo == nil {
|
|
|
|
firstFilesInfo = filesInfo
|
|
|
|
firstEOF = eof
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(filesInfo) == 0 {
|
|
|
|
emptyCount++
|
|
|
|
} else {
|
|
|
|
successCount++
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if firstErr == nil {
|
|
|
|
firstErr = err
|
|
|
|
}
|
|
|
|
|
|
|
|
errCount++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if errCount >= xl.readQuorum {
|
|
|
|
return nil, false, firstErr
|
|
|
|
} else if successCount >= xl.readQuorum {
|
|
|
|
return firstFilesInfo, firstEOF, nil
|
|
|
|
} else if emptyCount >= xl.readQuorum {
|
|
|
|
return []FileInfo{}, true, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil, false, errReadQuorum
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:32:46 -04:00
|
|
|
func listFiles(disk StorageAPI, volume, prefix, marker string, recursive bool, count int) (filesInfo []FileInfo, eof bool, err error) {
|
2016-03-28 00:52:38 -04:00
|
|
|
var fsFilesInfo []FileInfo
|
|
|
|
var markerPath = marker
|
|
|
|
if marker != "" {
|
2016-04-28 20:32:46 -04:00
|
|
|
isLeaf := isLeafDirectory(disk, volume, retainSlash(marker))
|
2016-03-28 00:52:38 -04:00
|
|
|
if isLeaf {
|
|
|
|
// For leaf for now we just point to the first block, make it
|
|
|
|
// dynamic in future based on the availability of storage disks.
|
2016-04-26 13:35:39 -04:00
|
|
|
markerPath = slashpath.Join(marker, metadataFile)
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-04-28 20:32:46 -04:00
|
|
|
// Loop and capture the proper fileInfos, requires extraction and
|
|
|
|
// separation of XL related metadata information.
|
2016-04-26 13:35:39 -04:00
|
|
|
for {
|
|
|
|
fsFilesInfo, eof, err = disk.ListFiles(volume, prefix, markerPath, recursive, count)
|
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"prefix": prefix,
|
|
|
|
"marker": markerPath,
|
|
|
|
"recursive": recursive,
|
|
|
|
"count": count,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("ListFiles failed with %s", err)
|
2016-04-26 13:35:39 -04:00
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
for _, fsFileInfo := range fsFilesInfo {
|
|
|
|
// Skip metadata files.
|
|
|
|
if strings.HasSuffix(fsFileInfo.Name, metadataFile) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var fileInfo FileInfo
|
|
|
|
var isLeaf bool
|
|
|
|
if fsFileInfo.Mode.IsDir() {
|
2016-04-28 20:32:46 -04:00
|
|
|
isLeaf = isLeafDirectory(disk, volume, fsFileInfo.Name)
|
2016-04-26 13:35:39 -04:00
|
|
|
}
|
|
|
|
if isLeaf || !fsFileInfo.Mode.IsDir() {
|
|
|
|
// Extract the parent of leaf directory or file to get the
|
|
|
|
// actual name.
|
|
|
|
path := slashpath.Dir(fsFileInfo.Name)
|
2016-04-28 20:32:46 -04:00
|
|
|
fileInfo, err = extractFileInfo(disk, volume, path)
|
2016-04-26 13:35:39 -04:00
|
|
|
if err != nil {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("extractFileInfo failed with %s", err)
|
2016-04-26 13:35:39 -04:00
|
|
|
// For a leaf directory, if err is FileNotFound then
|
|
|
|
// perhaps has a missing metadata. Ignore it and let
|
|
|
|
// healing finish its job it will become available soon.
|
|
|
|
if err == errFileNotFound {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
// For any other errors return to the caller.
|
|
|
|
return nil, true, err
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fileInfo = fsFileInfo
|
|
|
|
}
|
|
|
|
filesInfo = append(filesInfo, fileInfo)
|
|
|
|
count--
|
|
|
|
if count == 0 {
|
|
|
|
break
|
|
|
|
}
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
2016-04-28 20:32:46 -04:00
|
|
|
if len(fsFilesInfo) > 0 {
|
2016-04-26 13:35:39 -04:00
|
|
|
// markerPath for the next disk.ListFiles() iteration.
|
2016-04-28 20:32:46 -04:00
|
|
|
markerPath = fsFilesInfo[len(fsFilesInfo)-1].Name
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
2016-04-26 13:35:39 -04:00
|
|
|
if count == 0 && recursive && !strings.HasSuffix(markerPath, metadataFile) {
|
|
|
|
// If last entry is not part.json then loop once more to check if we
|
|
|
|
// have reached eof.
|
|
|
|
fsFilesInfo, eof, err = disk.ListFiles(volume, prefix, markerPath, recursive, 1)
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
2016-04-26 13:35:39 -04:00
|
|
|
"volume": volume,
|
|
|
|
"prefix": prefix,
|
|
|
|
"marker": markerPath,
|
|
|
|
"recursive": recursive,
|
|
|
|
"count": 1,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("ListFiles failed with %s", err)
|
2016-03-28 00:52:38 -04:00
|
|
|
return nil, true, err
|
|
|
|
}
|
2016-04-26 13:35:39 -04:00
|
|
|
if !eof {
|
|
|
|
// part.N and part.json are always in pairs and hence this
|
|
|
|
// entry has to be part.json. If not better to manually investigate
|
|
|
|
// and fix it.
|
|
|
|
// For the next ListFiles() call we can safely assume that the
|
|
|
|
// marker is "object/part.json"
|
|
|
|
if !strings.HasSuffix(fsFilesInfo[0].Name, metadataFile) {
|
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"prefix": prefix,
|
|
|
|
"fsFileInfo.Name": fsFilesInfo[0].Name,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("ListFiles failed with %s, expected %s to be a part.json file.", err, fsFilesInfo[0].Name)
|
2016-04-26 13:35:39 -04:00
|
|
|
return nil, true, errUnexpected
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if count == 0 || eof {
|
|
|
|
break
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
}
|
2016-04-28 04:48:57 -04:00
|
|
|
// Sort to make sure we sort entries back properly.
|
|
|
|
sort.Sort(byFileInfoName(filesInfo))
|
2016-03-28 00:52:38 -04:00
|
|
|
return filesInfo, eof, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 12:04:17 -04:00
|
|
|
// Acquire read lock.
|
|
|
|
readLock := true
|
|
|
|
xl.lockNS(volume, path, readLock)
|
2016-04-26 16:03:37 -04:00
|
|
|
_, metadata, heal, err := xl.listOnlineDisks(volume, path)
|
2016-04-22 12:04:17 -04:00
|
|
|
xl.unlockNS(volume, path, readLock)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 20:57:16 -04:00
|
|
|
}).Errorf("listOnlineDisks failed with %s", err)
|
2016-04-22 12:04:17 -04:00
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
|
|
|
|
2016-04-26 16:03:37 -04:00
|
|
|
if heal {
|
2016-04-27 18:10:19 -04: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 12:04:17 -04:00
|
|
|
}
|
|
|
|
|
2016-03-28 00:52:38 -04:00
|
|
|
// Extract metadata.
|
2016-04-22 12:04:17 -04:00
|
|
|
size, err := metadata.GetSize()
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("GetSize failed with %s", err)
|
2016-04-22 12:04:17 -04:00
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
|
|
|
modTime, err := metadata.GetModTime()
|
2016-03-28 00:52:38 -04:00
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("GetModTime failed with %s", err)
|
2016-03-28 00:52:38 -04:00
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
|
|
|
|
2016-04-22 12:04:17 -04:00
|
|
|
// Return file info.
|
|
|
|
return FileInfo{
|
|
|
|
Volume: volume,
|
|
|
|
Name: path,
|
|
|
|
Size: size,
|
|
|
|
ModTime: modTime,
|
|
|
|
Mode: os.FileMode(0644),
|
|
|
|
}, nil
|
2016-03-28 00:52:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFile - delete a file
|
|
|
|
func (xl XL) DeleteFile(volume, path string) error {
|
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
if !isValidPath(path) {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
// Loop through and delete each chunks.
|
|
|
|
for index, disk := range xl.storageDisks {
|
|
|
|
erasureFilePart := slashpath.Join(path, fmt.Sprintf("part.%d", index))
|
|
|
|
err := disk.DeleteFile(volume, erasureFilePart)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("DeleteFile failed with %s", err)
|
2016-03-28 00:52:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
metadataFilePath := slashpath.Join(path, metadataFile)
|
|
|
|
err = disk.DeleteFile(volume, metadataFilePath)
|
|
|
|
if err != nil {
|
2016-04-25 02:12:54 -04:00
|
|
|
log.WithFields(logrus.Fields{
|
|
|
|
"volume": volume,
|
|
|
|
"path": path,
|
2016-04-26 16:03:37 -04:00
|
|
|
}).Errorf("DeleteFile failed with %s", err)
|
2016-03-28 00:52:38 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|