2015-10-16 20:26:01 +02:00
|
|
|
/*
|
2017-01-18 21:24:34 +01:00
|
|
|
* Minio Cloud Storage, (C) 2016, 2017 Minio, Inc.
|
2015-10-16 20:26:01 +02: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-19 01:23:42 +02:00
|
|
|
package cmd
|
2015-10-16 20:26:01 +02:00
|
|
|
|
|
|
|
import (
|
2017-05-16 23:21:52 +02:00
|
|
|
"encoding/hex"
|
2016-04-08 19:37:38 +02:00
|
|
|
"io"
|
2016-06-25 23:51:06 +02:00
|
|
|
"io/ioutil"
|
2016-04-09 02:13:16 +02:00
|
|
|
"os"
|
2016-05-09 09:46:54 +02:00
|
|
|
slashpath "path"
|
2016-05-29 00:13:15 +02:00
|
|
|
"path/filepath"
|
2016-06-20 15:18:47 +02:00
|
|
|
"runtime"
|
2016-10-27 02:14:05 +02:00
|
|
|
"sync"
|
2016-06-21 01:57:14 +02:00
|
|
|
"sync/atomic"
|
2016-04-08 19:37:38 +02:00
|
|
|
"syscall"
|
2015-10-16 20:26:01 +02:00
|
|
|
|
2016-11-23 03:18:22 +01:00
|
|
|
humanize "github.com/dustin/go-humanize"
|
2016-04-09 02:13:16 +02:00
|
|
|
"github.com/minio/minio/pkg/disk"
|
2015-10-16 20:26:01 +02:00
|
|
|
)
|
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
const (
|
2017-08-04 05:07:22 +02:00
|
|
|
diskMinFreeSpace = 1 * humanize.GiByte // Min 1GiB free space.
|
|
|
|
diskMinTotalSpace = diskMinFreeSpace // Min 1GiB total space.
|
|
|
|
maxAllowedIOError = 5
|
2016-04-08 19:37:38 +02:00
|
|
|
)
|
|
|
|
|
2016-05-29 00:13:15 +02:00
|
|
|
// posix - implements StorageAPI interface.
|
|
|
|
type posix struct {
|
2017-03-07 21:25:40 +01:00
|
|
|
ioErrCount int32 // ref: https://golang.org/pkg/sync/atomic/#pkg-note-BUG
|
|
|
|
diskPath string
|
|
|
|
pool sync.Pool
|
2016-03-28 18:52:09 +02:00
|
|
|
}
|
|
|
|
|
2016-05-11 21:55:02 +02:00
|
|
|
// checkPathLength - returns error if given path name length more than 255
|
|
|
|
func checkPathLength(pathName string) error {
|
2016-07-03 20:17:08 +02:00
|
|
|
// Apple OS X path length is limited to 1016
|
|
|
|
if runtime.GOOS == "darwin" && len(pathName) > 1016 {
|
|
|
|
return errFileNameTooLong
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
// Convert any '\' to '/'.
|
|
|
|
pathName = filepath.ToSlash(pathName)
|
|
|
|
|
2016-06-13 11:53:09 +02:00
|
|
|
// Check each path segment length is > 255
|
2016-05-11 21:55:02 +02:00
|
|
|
for len(pathName) > 0 && pathName != "." && pathName != "/" {
|
|
|
|
dir, file := slashpath.Dir(pathName), slashpath.Base(pathName)
|
|
|
|
|
|
|
|
if len(file) > 255 {
|
|
|
|
return errFileNameTooLong
|
|
|
|
}
|
|
|
|
|
|
|
|
pathName = dir
|
2016-06-13 11:53:09 +02:00
|
|
|
} // Success.
|
2016-05-11 21:55:02 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
// isDirEmpty - returns whether given directory is empty or not.
|
2016-05-08 10:58:05 +02:00
|
|
|
func isDirEmpty(dirname string) bool {
|
2017-08-13 04:25:43 +02:00
|
|
|
f, err := os.Open((dirname))
|
2016-05-08 10:58:05 +02:00
|
|
|
if err != nil {
|
2017-08-04 19:43:51 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
errorIf(err, "Unable to access directory")
|
|
|
|
}
|
|
|
|
|
2016-05-08 10:58:05 +02:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
// List one entry.
|
|
|
|
_, err = f.Readdirnames(1)
|
2016-10-20 18:26:18 +02:00
|
|
|
if err != io.EOF {
|
2017-08-04 19:43:51 +02:00
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
errorIf(err, "Unable to list directory")
|
|
|
|
}
|
|
|
|
|
2016-05-08 10:58:05 +02:00
|
|
|
return false
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-10-20 18:26:18 +02:00
|
|
|
// Returns true if we have reached EOF, directory is indeed empty.
|
|
|
|
return true
|
2016-02-18 09:38:58 +01:00
|
|
|
}
|
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
// Initialize a new storage disk.
|
2016-10-27 12:30:52 +02:00
|
|
|
func newPosix(path string) (StorageAPI, error) {
|
|
|
|
if path == "" {
|
2016-04-08 19:37:38 +02:00
|
|
|
return nil, errInvalidArgument
|
|
|
|
}
|
2016-06-13 11:53:09 +02:00
|
|
|
// Disallow relative paths, figure out absolute paths.
|
2016-10-27 12:30:52 +02:00
|
|
|
diskPath, err := filepath.Abs(path)
|
2016-06-13 11:53:09 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-08-16 00:10:50 +02:00
|
|
|
|
2017-07-11 03:14:48 +02:00
|
|
|
st := &posix{
|
2017-03-07 21:25:40 +01:00
|
|
|
diskPath: diskPath,
|
2016-10-27 02:14:05 +02:00
|
|
|
// 1MiB buffer pool for posix internal operations.
|
|
|
|
pool: sync.Pool{
|
|
|
|
New: func() interface{} {
|
|
|
|
b := make([]byte, readSizeV1)
|
|
|
|
return &b
|
|
|
|
},
|
|
|
|
},
|
2016-05-11 21:55:02 +02:00
|
|
|
}
|
2017-08-13 04:25:43 +02:00
|
|
|
fi, err := osStat((diskPath))
|
2016-10-31 17:34:44 +01:00
|
|
|
if err == nil {
|
|
|
|
if !fi.IsDir() {
|
|
|
|
return nil, syscall.ENOTDIR
|
2016-05-12 03:58:32 +02:00
|
|
|
}
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-10-31 17:34:44 +01:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
// Disk not found create it.
|
2017-08-13 04:25:43 +02:00
|
|
|
err = os.MkdirAll(diskPath, 0777)
|
2016-10-31 17:34:44 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2017-07-11 03:14:48 +02:00
|
|
|
|
2017-08-13 04:25:43 +02:00
|
|
|
di, err := getDiskInfo((diskPath))
|
2017-07-11 03:14:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if disk has minimum required total space.
|
|
|
|
if err = checkDiskMinTotal(di); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Success.
|
|
|
|
return st, nil
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-01-25 08:03:38 +01:00
|
|
|
|
2016-06-29 20:25:35 +02:00
|
|
|
// getDiskInfo returns given disk information.
|
|
|
|
func getDiskInfo(diskPath string) (di disk.Info, err error) {
|
|
|
|
if err = checkPathLength(diskPath); err == nil {
|
|
|
|
di, err = disk.GetInfo(diskPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
err = errDiskNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return di, err
|
|
|
|
}
|
|
|
|
|
2017-02-09 07:27:35 +01:00
|
|
|
// List of operating systems where we ignore disk space
|
|
|
|
// verification.
|
|
|
|
var ignoreDiskFreeOS = []string{
|
|
|
|
globalWindowsOSName,
|
|
|
|
globalNetBSDOSName,
|
|
|
|
globalSolarisOSName,
|
|
|
|
}
|
|
|
|
|
2017-07-11 03:14:48 +02:00
|
|
|
// check if disk total has minimum required size.
|
|
|
|
func checkDiskMinTotal(di disk.Info) (err error) {
|
|
|
|
// Remove 5% from total space for cumulative disk space
|
|
|
|
// used for journalling, inodes etc.
|
|
|
|
totalDiskSpace := float64(di.Total) * 0.95
|
|
|
|
if int64(totalDiskSpace) <= diskMinTotalSpace {
|
|
|
|
return errDiskFull
|
2016-10-31 17:34:44 +01:00
|
|
|
}
|
2017-07-11 03:14:48 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// check if disk free has minimum required size.
|
|
|
|
func checkDiskMinFree(di disk.Info) error {
|
2016-09-27 21:46:38 +02:00
|
|
|
// Remove 5% from free space for cumulative disk space used for journalling, inodes etc.
|
|
|
|
availableDiskSpace := float64(di.Free) * 0.95
|
2017-07-11 03:14:48 +02:00
|
|
|
if int64(availableDiskSpace) <= diskMinFreeSpace {
|
2016-04-19 11:42:10 +02:00
|
|
|
return errDiskFull
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-03-28 18:52:09 +02:00
|
|
|
|
2017-07-11 03:14:48 +02:00
|
|
|
// Success.
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// checkDiskFree verifies if disk path has sufficient minimum free disk space and files.
|
|
|
|
func checkDiskFree(diskPath string, neededSpace int64) (err error) {
|
|
|
|
// We don't validate disk space or inode utilization on windows.
|
|
|
|
// Each windows call to 'GetVolumeInformationW' takes around
|
|
|
|
// 3-5seconds. And StatDISK is not supported by Go for solaris
|
|
|
|
// and netbsd.
|
|
|
|
if contains(ignoreDiskFreeOS, runtime.GOOS) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var di disk.Info
|
2017-08-13 04:25:43 +02:00
|
|
|
di, err = getDiskInfo((diskPath))
|
2017-07-11 03:14:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkDiskMinFree(di); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-07 21:25:40 +01:00
|
|
|
// Check if we have enough space to store data
|
2017-07-11 03:14:48 +02:00
|
|
|
if neededSpace > int64(float64(di.Free)*0.95) {
|
2017-03-07 21:25:40 +01:00
|
|
|
return errDiskFull
|
|
|
|
}
|
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-05 21:48:07 +02:00
|
|
|
// Implements stringer compatible interface.
|
|
|
|
func (s *posix) String() string {
|
2016-10-27 12:30:52 +02:00
|
|
|
return s.diskPath
|
2016-10-05 21:48:07 +02:00
|
|
|
}
|
|
|
|
|
2016-11-24 00:48:10 +01:00
|
|
|
// Init - this is a dummy call.
|
|
|
|
func (s *posix) Init() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close - this is a dummy call.
|
|
|
|
func (s *posix) Close() error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-26 02:16:34 +02:00
|
|
|
// DiskInfo provides current information about disk space usage,
|
|
|
|
// total free inodes and underlying filesystem.
|
|
|
|
func (s *posix) DiskInfo() (info disk.Info, err error) {
|
2017-08-13 04:25:43 +02:00
|
|
|
return getDiskInfo((s.diskPath))
|
2016-08-26 02:16:34 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 06:22:27 +02:00
|
|
|
// getVolDir - will convert incoming volume names to
|
2016-04-13 20:32:47 +02:00
|
|
|
// corresponding valid volume names on the backend in a platform
|
|
|
|
// compatible way for all operating systems. If volume is not found
|
|
|
|
// an error is generated.
|
2016-06-21 01:57:14 +02:00
|
|
|
func (s *posix) getVolDir(volume string) (string, error) {
|
2016-04-13 20:32:47 +02:00
|
|
|
if !isValidVolname(volume) {
|
|
|
|
return "", errInvalidArgument
|
|
|
|
}
|
2016-05-05 10:39:26 +02:00
|
|
|
volumeDir := pathJoin(s.diskPath, volume)
|
2016-05-18 06:22:27 +02:00
|
|
|
return volumeDir, nil
|
2016-04-13 20:32:47 +02:00
|
|
|
}
|
2016-04-08 19:37:38 +02:00
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
// checkDiskFound - validates if disk is available,
|
|
|
|
// returns errDiskNotFound if not found.
|
|
|
|
func (s *posix) checkDiskFound() (err error) {
|
2017-08-13 04:25:43 +02:00
|
|
|
_, err = osStat((s.diskPath))
|
2016-10-31 17:34:44 +01:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return errDiskNotFound
|
|
|
|
} else if isSysErrTooLong(err) {
|
|
|
|
return errFileNameTooLong
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-13 20:32:47 +02:00
|
|
|
// Make a volume entry.
|
2016-06-21 01:57:14 +02:00
|
|
|
func (s *posix) MakeVol(volume string) (err error) {
|
2016-06-09 07:02:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
2016-06-21 01:57:14 +02:00
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-06-09 07:02:10 +02:00
|
|
|
return errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
2016-04-24 09:36:00 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-18 06:22:27 +02:00
|
|
|
volumeDir, err := s.getVolDir(volume)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-06-24 05:19:27 +02:00
|
|
|
// Make a volume entry, with mode 0777 mkdir honors system umask.
|
2017-08-13 04:25:43 +02:00
|
|
|
err = os.Mkdir((volumeDir), 0777)
|
2016-07-02 10:59:28 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsExist(err) {
|
|
|
|
return errVolumeExists
|
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
return errDiskAccessDenied
|
|
|
|
}
|
|
|
|
return err
|
2016-04-13 20:32:47 +02:00
|
|
|
}
|
2016-05-18 06:22:27 +02:00
|
|
|
// Success
|
|
|
|
return nil
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListVols - list volumes.
|
2016-06-21 01:57:14 +02:00
|
|
|
func (s *posix) ListVols() (volsInfo []VolInfo, err error) {
|
2016-06-09 07:02:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
2016-06-21 01:57:14 +02:00
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-06-09 07:02:10 +02:00
|
|
|
return nil, errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-08-13 04:25:43 +02:00
|
|
|
volsInfo, err = listVols((s.diskPath))
|
2016-04-08 19:37:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2016-04-17 21:00:23 +02:00
|
|
|
for i, vol := range volsInfo {
|
2016-04-08 19:37:38 +02:00
|
|
|
volInfo := VolInfo{
|
2016-05-20 03:52:55 +02:00
|
|
|
Name: vol.Name,
|
2016-04-16 21:48:41 +02:00
|
|
|
Created: vol.Created,
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-04-17 21:00:23 +02:00
|
|
|
volsInfo[i] = volInfo
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
return volsInfo, nil
|
2015-10-16 20:26:01 +02:00
|
|
|
}
|
config/main: Re-write config files - add to new config v3
- New config format.
```
{
"version": "3",
"address": ":9000",
"backend": {
"type": "fs",
"disk": "/path"
},
"credential": {
"accessKey": "WLGDGYAQYIGI833EV05A",
"secretKey": "BYvgJM101sHngl2uzjXS/OBF/aMxAN06JrJ3qJlF"
},
"region": "us-east-1",
"logger": {
"file": {
"enable": false,
"fileName": "",
"level": "error"
},
"syslog": {
"enable": false,
"address": "",
"level": "debug"
},
"console": {
"enable": true,
"level": "fatal"
}
}
}
```
New command lines in lieu of supporting XL.
Minio initialize filesystem backend.
~~~
$ minio init fs <path>
~~~
Minio initialize XL backend.
~~~
$ minio init xl <url1>...<url16>
~~~
For 'fs' backend it starts the server.
~~~
$ minio server
~~~
For 'xl' backend it waits for servers to join.
~~~
$ minio server
... [PROGRESS BAR] of servers connecting
~~~
Now on other servers execute 'join' and they connect.
~~~
....
minio join <url1> -- from <url2> && minio server
minio join <url1> -- from <url3> && minio server
...
...
minio join <url1> -- from <url16> && minio server
~~~
2016-02-13 00:27:10 +01:00
|
|
|
|
2016-08-12 04:57:14 +02:00
|
|
|
// List all the volumes from diskPath.
|
|
|
|
func listVols(dirPath string) ([]VolInfo, error) {
|
|
|
|
if err := checkPathLength(dirPath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
entries, err := readDir(dirPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errDiskNotFound
|
|
|
|
}
|
|
|
|
var volsInfo []VolInfo
|
|
|
|
for _, entry := range entries {
|
2017-02-16 23:52:14 +01:00
|
|
|
if !hasSuffix(entry, slashSeparator) || !isValidVolname(slashpath.Clean(entry)) {
|
2016-08-12 04:57:14 +02:00
|
|
|
// Skip if entry is neither a directory not a valid volume name.
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
var fi os.FileInfo
|
2017-08-13 04:25:43 +02:00
|
|
|
fi, err = osStat((pathJoin(dirPath, entry)))
|
2016-08-12 04:57:14 +02:00
|
|
|
if err != nil {
|
|
|
|
// If the file does not exist, skip the entry.
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
volsInfo = append(volsInfo, VolInfo{
|
|
|
|
Name: fi.Name(),
|
2017-05-02 11:35:27 +02:00
|
|
|
// As osStat() doesn't carry other than ModTime(), use
|
2016-08-12 04:57:14 +02:00
|
|
|
// ModTime() as CreatedTime.
|
|
|
|
Created: fi.ModTime(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return volsInfo, nil
|
|
|
|
}
|
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
// StatVol - get volume info.
|
2016-06-21 01:57:14 +02:00
|
|
|
func (s *posix) StatVol(volume string) (volInfo VolInfo, err error) {
|
2016-06-09 07:02:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
2016-06-21 01:57:14 +02:00
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-06-09 07:02:10 +02:00
|
|
|
return VolInfo{}, errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
2016-06-03 01:34:15 +02:00
|
|
|
return VolInfo{}, err
|
|
|
|
}
|
|
|
|
|
2016-04-13 20:32:47 +02:00
|
|
|
// Verify if volume is valid and it exists.
|
2016-05-18 06:22:27 +02:00
|
|
|
volumeDir, err := s.getVolDir(volume)
|
2016-04-13 20:32:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return VolInfo{}, err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
// Stat a volume entry.
|
|
|
|
var st os.FileInfo
|
2017-08-13 04:25:43 +02:00
|
|
|
st, err = osStat((volumeDir))
|
2016-04-08 19:37:38 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return VolInfo{}, errVolumeNotFound
|
|
|
|
}
|
|
|
|
return VolInfo{}, err
|
2016-04-09 02:13:16 +02:00
|
|
|
}
|
2017-05-02 11:35:27 +02:00
|
|
|
// As osStat() doesn't carry other than ModTime(), use ModTime()
|
2016-04-16 21:48:41 +02:00
|
|
|
// as CreatedTime.
|
2016-04-13 20:32:47 +02:00
|
|
|
createdTime := st.ModTime()
|
2016-04-08 19:37:38 +02:00
|
|
|
return VolInfo{
|
2016-04-13 20:32:47 +02:00
|
|
|
Name: volume,
|
|
|
|
Created: createdTime,
|
2016-04-08 19:37:38 +02:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVol - delete a volume.
|
2016-06-21 01:57:14 +02:00
|
|
|
func (s *posix) DeleteVol(volume string) (err error) {
|
2016-06-09 07:02:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
2016-06-21 01:57:14 +02:00
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-06-09 07:02:10 +02:00
|
|
|
return errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
2016-06-03 07:49:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-04-13 20:32:47 +02:00
|
|
|
// Verify if volume is valid and it exists.
|
2016-05-18 06:22:27 +02:00
|
|
|
volumeDir, err := s.getVolDir(volume)
|
2016-04-13 20:32:47 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2017-08-13 04:25:43 +02:00
|
|
|
err = os.Remove((volumeDir))
|
2016-04-16 21:48:41 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return errVolumeNotFound
|
2016-08-31 23:43:20 +02:00
|
|
|
} else if isSysErrNotEmpty(err) {
|
2016-04-16 21:48:41 +02:00
|
|
|
return errVolumeNotEmpty
|
|
|
|
}
|
|
|
|
return err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-03-28 06:52:38 +02:00
|
|
|
return nil
|
2016-04-08 19:37: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 "/".
|
2016-06-21 01:57:14 +02:00
|
|
|
func (s *posix) ListDir(volume, dirPath string) (entries []string, err error) {
|
2016-06-09 07:02:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
2016-06-21 01:57:14 +02:00
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-06-09 07:02:10 +02:00
|
|
|
return nil, errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
2016-06-03 07:49:27 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-04-13 20:32:47 +02:00
|
|
|
// Verify if volume is valid and it exists.
|
2016-05-18 06:22:27 +02:00
|
|
|
volumeDir, err := s.getVolDir(volume)
|
2016-04-13 20:32:47 +02:00
|
|
|
if err != nil {
|
2016-05-05 21:51:56 +02:00
|
|
|
return nil, err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-05-05 21:51:56 +02:00
|
|
|
// Stat a volume entry.
|
2017-08-13 04:25:43 +02:00
|
|
|
_, err = osStat((volumeDir))
|
2016-05-05 21:51:56 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, errVolumeNotFound
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-05-05 21:51:56 +02:00
|
|
|
return nil, err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-05-05 21:51:56 +02:00
|
|
|
return readDir(pathJoin(volumeDir, dirPath))
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
2016-06-25 23:51:06 +02:00
|
|
|
// ReadAll reads from r until an error or EOF and returns the data it read.
|
|
|
|
// A successful call returns err == nil, not err == EOF. Because ReadAll is
|
|
|
|
// defined to read from src until EOF, it does not treat an EOF from Read
|
|
|
|
// as an error to be reported.
|
|
|
|
// This API is meant to be used on files which have small memory footprint, do
|
|
|
|
// not use this on large files as it would cause server to crash.
|
|
|
|
func (s *posix) ReadAll(volume, path string) (buf []byte, err error) {
|
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-06-25 23:51:06 +02:00
|
|
|
return nil, errFaultyDisk
|
|
|
|
}
|
2016-06-29 20:25:35 +02:00
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
2016-06-25 23:51:06 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
volumeDir, err := s.getVolDir(volume)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
// Stat a volume entry.
|
2017-08-13 04:25:43 +02:00
|
|
|
_, err = osStat((volumeDir))
|
2016-06-25 23:51:06 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, errVolumeNotFound
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate file path length, before reading.
|
|
|
|
filePath := pathJoin(volumeDir, path)
|
2017-08-13 04:25:43 +02:00
|
|
|
if err = checkPathLength((filePath)); err != nil {
|
2016-06-25 23:51:06 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file for reading.
|
2017-08-13 04:25:43 +02:00
|
|
|
buf, err = ioutil.ReadFile((filePath))
|
2016-06-25 23:51:06 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return nil, errFileNotFound
|
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
return nil, errFileAccessDenied
|
|
|
|
} else if pathErr, ok := err.(*os.PathError); ok {
|
2016-07-11 09:15:37 +02:00
|
|
|
switch pathErr.Err {
|
|
|
|
case syscall.ENOTDIR, syscall.EISDIR:
|
2016-06-25 23:51:06 +02:00
|
|
|
return nil, errFileNotFound
|
2016-07-11 09:15:37 +02:00
|
|
|
default:
|
2016-08-31 23:43:20 +02:00
|
|
|
if isSysErrHandleInvalid(pathErr.Err) {
|
2016-07-11 09:15:37 +02:00
|
|
|
// This case is special and needs to be handled for windows.
|
|
|
|
return nil, errFileNotFound
|
|
|
|
}
|
2016-06-25 23:51:06 +02:00
|
|
|
}
|
|
|
|
return nil, pathErr
|
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2016-05-29 00:13:15 +02:00
|
|
|
// ReadFile reads exactly len(buf) bytes into buf. It returns the
|
|
|
|
// number of bytes copied. The error is EOF only if no bytes were
|
|
|
|
// read. On return, n == len(buf) if and only if err == nil. n == 0
|
2016-11-21 08:42:53 +01:00
|
|
|
// for io.EOF.
|
2017-05-16 23:21:52 +02:00
|
|
|
//
|
2016-11-21 08:42:53 +01:00
|
|
|
// If an EOF happens after reading some but not all the bytes,
|
2017-09-25 20:32:56 +02:00
|
|
|
// ReadFile returns ErrUnexpectedEOF.
|
|
|
|
//
|
|
|
|
// If the BitrotVerifier is not nil or not verified ReadFile
|
|
|
|
// tries to verify whether the disk has bitrot.
|
2017-05-16 23:21:52 +02:00
|
|
|
//
|
|
|
|
// Additionally ReadFile also starts reading from an offset. ReadFile
|
|
|
|
// semantics are same as io.ReadFull.
|
2017-09-25 20:32:56 +02:00
|
|
|
func (s *posix) ReadFile(volume, path string, offset int64, buffer []byte, verifier *BitrotVerifier) (n int64, err error) {
|
2016-06-09 07:02:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
2016-06-21 01:57:14 +02:00
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-06-09 07:02:10 +02:00
|
|
|
return 0, errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
2016-06-03 07:49:27 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
|
2016-05-18 06:22:27 +02:00
|
|
|
volumeDir, err := s.getVolDir(volume)
|
|
|
|
if err != nil {
|
2016-05-29 00:13:15 +02:00
|
|
|
return 0, err
|
2016-05-18 06:22:27 +02:00
|
|
|
}
|
|
|
|
// Stat a volume entry.
|
2017-08-13 04:25:43 +02:00
|
|
|
_, err = osStat((volumeDir))
|
2016-04-13 20:32:47 +02:00
|
|
|
if err != nil {
|
2016-05-18 06:22:27 +02:00
|
|
|
if os.IsNotExist(err) {
|
2016-05-29 00:13:15 +02:00
|
|
|
return 0, errVolumeNotFound
|
2016-05-18 06:22:27 +02:00
|
|
|
}
|
2016-05-29 00:13:15 +02:00
|
|
|
return 0, err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-04-13 20:32:47 +02:00
|
|
|
|
2016-06-25 23:51:06 +02:00
|
|
|
// Validate effective path length before reading.
|
2016-05-05 10:39:26 +02:00
|
|
|
filePath := pathJoin(volumeDir, path)
|
2017-08-13 04:25:43 +02:00
|
|
|
if err = checkPathLength((filePath)); err != nil {
|
2016-05-29 00:13:15 +02:00
|
|
|
return 0, err
|
2016-05-11 21:55:02 +02:00
|
|
|
}
|
2016-06-25 23:51:06 +02:00
|
|
|
|
|
|
|
// Open the file for reading.
|
2017-08-13 04:25:43 +02:00
|
|
|
file, err := os.Open((filePath))
|
2016-04-08 19:37:38 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2016-05-29 00:13:15 +02:00
|
|
|
return 0, errFileNotFound
|
2016-04-13 20:32:47 +02:00
|
|
|
} else if os.IsPermission(err) {
|
2016-05-29 00:13:15 +02:00
|
|
|
return 0, errFileAccessDenied
|
2016-08-31 23:43:20 +02:00
|
|
|
} else if isSysErrNotDir(err) {
|
2016-07-29 06:57:11 +02:00
|
|
|
return 0, errFileAccessDenied
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-05-29 00:13:15 +02:00
|
|
|
return 0, err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-07-28 04:22:32 +02:00
|
|
|
|
|
|
|
// Close the file descriptor.
|
|
|
|
defer file.Close()
|
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
st, err := file.Stat()
|
|
|
|
if err != nil {
|
2016-05-29 00:13:15 +02:00
|
|
|
return 0, err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-07-29 06:57:11 +02:00
|
|
|
|
2017-05-16 23:21:52 +02:00
|
|
|
// Verify it is a regular file, otherwise subsequent Seek is
|
|
|
|
// undefined.
|
2016-04-08 19:37:38 +02:00
|
|
|
if !st.Mode().IsRegular() {
|
2016-07-29 06:57:11 +02:00
|
|
|
return 0, errIsNotRegular
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-07-29 06:57:11 +02:00
|
|
|
|
2017-09-25 20:32:56 +02:00
|
|
|
if verifier != nil && !verifier.IsVerified() {
|
2017-08-15 03:08:42 +02:00
|
|
|
bufp := s.pool.Get().(*[]byte)
|
|
|
|
defer s.pool.Put(bufp)
|
2017-05-16 23:21:52 +02:00
|
|
|
|
2017-08-15 03:08:42 +02:00
|
|
|
if offset != 0 {
|
|
|
|
if _, err = io.CopyBuffer(verifier, io.LimitReader(file, offset), *bufp); err != nil {
|
2017-05-16 23:21:52 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
}
|
2017-08-15 03:08:42 +02:00
|
|
|
if _, err = file.Read(buffer); err != nil {
|
2017-05-16 23:21:52 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-08-15 03:08:42 +02:00
|
|
|
if _, err = verifier.Write(buffer); err != nil {
|
2017-05-16 23:21:52 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-08-15 03:08:42 +02:00
|
|
|
if _, err = io.CopyBuffer(verifier, file, *bufp); err != nil {
|
2017-05-16 23:21:52 +02:00
|
|
|
return 0, err
|
|
|
|
}
|
2017-08-15 03:08:42 +02:00
|
|
|
if !verifier.Verify() {
|
|
|
|
return 0, hashMismatchError{hex.EncodeToString(verifier.sum), hex.EncodeToString(verifier.Sum(nil))}
|
2017-05-16 23:21:52 +02:00
|
|
|
}
|
2017-08-15 03:08:42 +02:00
|
|
|
return int64(len(buffer)), err
|
2017-05-16 23:21:52 +02:00
|
|
|
}
|
2016-05-29 00:13:15 +02:00
|
|
|
|
2017-08-15 03:08:42 +02:00
|
|
|
m, err := file.ReadAt(buffer, offset)
|
|
|
|
if m > 0 && m < len(buffer) {
|
|
|
|
err = io.ErrUnexpectedEOF
|
|
|
|
}
|
2016-05-29 00:13:15 +02:00
|
|
|
return int64(m), err
|
2016-04-09 02:13:16 +02:00
|
|
|
}
|
|
|
|
|
2016-10-29 21:44:44 +02:00
|
|
|
func (s *posix) createFile(volume, path string) (f *os.File, err error) {
|
2016-06-09 07:02:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
2016-06-21 01:57:14 +02:00
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-10-29 21:44:44 +02:00
|
|
|
return nil, errFaultyDisk
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
2016-10-29 21:44:44 +02:00
|
|
|
return nil, err
|
2016-06-03 07:49:27 +02:00
|
|
|
}
|
|
|
|
|
2016-05-18 06:22:27 +02:00
|
|
|
volumeDir, err := s.getVolDir(volume)
|
2016-04-13 20:32:47 +02:00
|
|
|
if err != nil {
|
2016-10-29 21:44:44 +02:00
|
|
|
return nil, err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-05-18 06:22:27 +02:00
|
|
|
// Stat a volume entry.
|
2017-08-13 04:25:43 +02:00
|
|
|
_, err = osStat((volumeDir))
|
2016-05-18 06:22:27 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
2016-10-29 21:44:44 +02:00
|
|
|
return nil, errVolumeNotFound
|
2016-05-18 06:22:27 +02:00
|
|
|
}
|
2016-10-29 21:44:44 +02:00
|
|
|
return nil, err
|
2016-05-18 06:22:27 +02:00
|
|
|
}
|
2016-10-29 21:44:44 +02:00
|
|
|
|
2016-05-05 10:39:26 +02:00
|
|
|
filePath := pathJoin(volumeDir, path)
|
2017-08-13 04:25:43 +02:00
|
|
|
if err = checkPathLength((filePath)); err != nil {
|
2016-10-29 21:44:44 +02:00
|
|
|
return nil, err
|
2016-05-11 21:55:02 +02:00
|
|
|
}
|
2016-10-29 21:44:44 +02:00
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
// Verify if the file already exists and is not of regular type.
|
2016-05-04 21:18:40 +02:00
|
|
|
var st os.FileInfo
|
2017-08-13 04:25:43 +02:00
|
|
|
if st, err = osStat((filePath)); err == nil {
|
2016-07-29 06:57:11 +02:00
|
|
|
if !st.Mode().IsRegular() {
|
2016-10-29 21:44:44 +02:00
|
|
|
return nil, errIsNotRegular
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-10-29 21:44:44 +02:00
|
|
|
} else {
|
|
|
|
// Create top level directories if they don't exist.
|
|
|
|
// with mode 0777 mkdir honors system umask.
|
2017-08-13 04:25:43 +02:00
|
|
|
if err = os.MkdirAll(slashpath.Dir(filePath), 0777); err != nil {
|
2016-10-29 21:44:44 +02:00
|
|
|
// File path cannot be verified since one of the parents is a file.
|
|
|
|
if isSysErrNotDir(err) {
|
|
|
|
return nil, errFileAccessDenied
|
|
|
|
} else if isSysErrPathNotFound(err) {
|
|
|
|
// Add specific case for windows.
|
|
|
|
return nil, errFileAccessDenied
|
|
|
|
}
|
|
|
|
return nil, err
|
2016-07-03 20:17:08 +02:00
|
|
|
}
|
2016-05-29 00:13:15 +02:00
|
|
|
}
|
2016-06-24 05:19:27 +02:00
|
|
|
|
2017-08-13 04:25:43 +02:00
|
|
|
w, err := os.OpenFile((filePath), os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0666)
|
2016-05-04 21:18:40 +02:00
|
|
|
if err != nil {
|
|
|
|
// File path cannot be verified since one of the parents is a file.
|
2016-08-31 23:43:20 +02:00
|
|
|
if isSysErrNotDir(err) {
|
2016-10-29 21:44:44 +02:00
|
|
|
return nil, errFileAccessDenied
|
2016-05-04 21:18:40 +02:00
|
|
|
}
|
2016-10-29 21:44:44 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return w, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareFile - run prior actions before creating a new file for optimization purposes
|
2016-11-10 16:44:41 +01:00
|
|
|
// Currently we use fallocate when available to avoid disk fragmentation as much as possible
|
2016-10-29 21:44:44 +02:00
|
|
|
func (s *posix) PrepareFile(volume, path string, fileSize int64) (err error) {
|
|
|
|
// It doesn't make sense to create a negative-sized file
|
|
|
|
if fileSize <= 0 {
|
|
|
|
return errInvalidArgument
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-10-29 21:44:44 +02:00
|
|
|
return errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
// Validate if disk is indeed free.
|
2017-03-07 21:25:40 +01:00
|
|
|
if err = checkDiskFree(s.diskPath, fileSize); err != nil {
|
2016-10-31 17:34:44 +01:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-29 21:44:44 +02:00
|
|
|
// Create file if not found
|
|
|
|
w, err := s.createFile(volume, path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close upon return.
|
|
|
|
defer w.Close()
|
|
|
|
|
|
|
|
// Allocate needed disk space to append data
|
|
|
|
e := Fallocate(int(w.Fd()), 0, fileSize)
|
|
|
|
|
|
|
|
// Ignore errors when Fallocate is not supported in the current system
|
|
|
|
if e != nil && !isSysErrNoSys(e) && !isSysErrOpNotSupported(e) {
|
|
|
|
switch {
|
|
|
|
case isSysErrNoSpace(e):
|
|
|
|
err = errDiskFull
|
|
|
|
case isSysErrIO(e):
|
|
|
|
err = e
|
|
|
|
default:
|
|
|
|
// For errors: EBADF, EINTR, EINVAL, ENODEV, EPERM, ESPIPE and ETXTBSY
|
|
|
|
// Appending was failed anyway, returns unexpected error
|
|
|
|
err = errUnexpected
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AppendFile - append a byte array at path, if file doesn't exist at
|
|
|
|
// path this call explicitly creates it.
|
|
|
|
func (s *posix) AppendFile(volume, path string, buf []byte) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-10-29 21:44:44 +02:00
|
|
|
return errFaultyDisk
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create file if not found
|
|
|
|
w, err := s.createFile(volume, path)
|
|
|
|
if err != nil {
|
2016-06-20 00:31:13 +02:00
|
|
|
return err
|
2016-05-04 21:18:40 +02:00
|
|
|
}
|
2017-08-15 03:08:42 +02:00
|
|
|
_, err = w.Write(buf)
|
|
|
|
w.Close()
|
2016-06-20 00:31:13 +02:00
|
|
|
return err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// StatFile - get file info.
|
2016-06-21 01:57:14 +02:00
|
|
|
func (s *posix) StatFile(volume, path string) (file FileInfo, err error) {
|
2016-06-09 07:02:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
2016-06-21 01:57:14 +02:00
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-06-09 07:02:10 +02:00
|
|
|
return FileInfo{}, errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
2016-06-03 07:49:27 +02:00
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
|
|
|
|
2016-05-18 06:22:27 +02:00
|
|
|
volumeDir, err := s.getVolDir(volume)
|
|
|
|
if err != nil {
|
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
|
|
|
// Stat a volume entry.
|
2017-08-13 04:25:43 +02:00
|
|
|
_, err = osStat((volumeDir))
|
2016-04-13 20:32:47 +02:00
|
|
|
if err != nil {
|
2016-05-18 06:22:27 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return FileInfo{}, errVolumeNotFound
|
|
|
|
}
|
2016-04-13 20:32:47 +02:00
|
|
|
return FileInfo{}, err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-09 09:46:54 +02:00
|
|
|
filePath := slashpath.Join(volumeDir, path)
|
2017-08-13 04:25:43 +02:00
|
|
|
if err = checkPathLength((filePath)); err != nil {
|
2016-05-11 21:55:02 +02:00
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
2017-08-13 04:25:43 +02:00
|
|
|
st, err := osStat((filePath))
|
2016-04-08 19:37:38 +02:00
|
|
|
if err != nil {
|
2016-04-13 20:32:47 +02:00
|
|
|
// File is really not found.
|
2016-04-08 19:37:38 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return FileInfo{}, errFileNotFound
|
|
|
|
}
|
2016-04-24 09:36:00 +02:00
|
|
|
|
2016-04-13 20:32:47 +02:00
|
|
|
// File path cannot be verified since one of the parents is a file.
|
2016-08-31 23:43:20 +02:00
|
|
|
if isSysErrNotDir(err) {
|
2016-04-25 19:39:28 +02:00
|
|
|
return FileInfo{}, errFileNotFound
|
2016-04-12 21:45:15 +02:00
|
|
|
}
|
2016-04-24 09:36:00 +02:00
|
|
|
|
2016-04-13 20:32:47 +02:00
|
|
|
// Return all errors here.
|
2016-04-08 19:37:38 +02:00
|
|
|
return FileInfo{}, err
|
|
|
|
}
|
2016-04-13 20:32:47 +02:00
|
|
|
// If its a directory its not a regular file.
|
2016-04-08 19:37:38 +02:00
|
|
|
if st.Mode().IsDir() {
|
2016-04-25 19:39:28 +02:00
|
|
|
return FileInfo{}, errFileNotFound
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-04-24 09:36:00 +02:00
|
|
|
return FileInfo{
|
2016-04-08 19:37:38 +02:00
|
|
|
Volume: volume,
|
|
|
|
Name: path,
|
|
|
|
ModTime: st.ModTime(),
|
|
|
|
Size: st.Size(),
|
|
|
|
Mode: st.Mode(),
|
2016-04-24 09:36:00 +02:00
|
|
|
}, nil
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
2017-08-04 05:04:28 +02:00
|
|
|
// deleteFile deletes a file path if its empty. If it's successfully deleted,
|
|
|
|
// it will recursively move up the tree, deleting empty parent directories
|
|
|
|
// until it finds one with files in it. Returns nil for a non-empty directory.
|
2016-04-13 20:32:47 +02:00
|
|
|
func deleteFile(basePath, deletePath string) error {
|
2016-04-08 19:37:38 +02:00
|
|
|
if basePath == deletePath {
|
|
|
|
return nil
|
|
|
|
}
|
2017-08-04 05:04:28 +02:00
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
// Attempt to remove path.
|
2017-08-13 04:25:43 +02:00
|
|
|
if err := os.Remove((deletePath)); err != nil {
|
2017-08-04 05:04:28 +02:00
|
|
|
// Ignore errors if the directory is not empty. The server relies on
|
|
|
|
// this functionality, and sometimes uses recursion that should not
|
|
|
|
// error on parent directories.
|
|
|
|
if isSysErrNotEmpty(err) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-18 01:38:46 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return errFileNotFound
|
|
|
|
} else if os.IsPermission(err) {
|
|
|
|
return errFileAccessDenied
|
|
|
|
}
|
2016-04-13 20:32:47 +02:00
|
|
|
return err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2017-08-04 05:04:28 +02:00
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
// Recursively go down the next path and delete again.
|
posix: do not upstream errors in deleteFile (#4771)
This commit changes posix's deleteFile() to not upstream errors from
removing parent directories. This fixes a race condition.
The race condition occurs when multiple deleteFile()s are called on the
same parent directory, but different child files. Because deleteFile()
recursively removes parent directories if they are empty, but
deleteFile() errors if the selected deletePath does not exist, there was
an opportunity for a race condition. The two processes would remove the
child directories successfully, then depend on the parent directory
still existing. In some cases this is an invalid assumption, because
other processes can remove the parent directory beforehand. This commit
changes deleteFile() to not upstream an error if one occurs, because the
only required error should be from the immediate deletePath, not from a
parent path.
In the specific bug report, multiple CompleteMultipartUpload requests
would launch multiple deleteFile() requests. Because they chain up on
parent directories, ultimately at the end, there would be multiple
remove files for the ultimate parent directory,
.minio.sys/multipart/{bucket}. Because only one will succeed and one
will fail, an error would be upstreamed saying that the file does not
exist, and the CompleteMultipartUpload code interpreted this as
NoSuchKey, or that the object/part id doesn't exist. This was faulty
behavior and is now fixed.
The added test fails before this change and passes after this change.
Fixes: https://github.com/minio/minio/issues/4727
2017-08-05 01:51:20 +02:00
|
|
|
// Errors for parent directories shouldn't trickle down.
|
|
|
|
deleteFile(basePath, slashpath.Dir(deletePath))
|
|
|
|
|
|
|
|
return nil
|
2016-04-09 02:13:16 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
// DeleteFile - delete a file at path.
|
2016-06-21 01:57:14 +02:00
|
|
|
func (s *posix) DeleteFile(volume, path string) (err error) {
|
2016-06-09 07:02:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
2016-06-21 01:57:14 +02:00
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-06-09 07:02:10 +02:00
|
|
|
return errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
2016-06-03 07:49:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-18 06:22:27 +02:00
|
|
|
volumeDir, err := s.getVolDir(volume)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Stat a volume entry.
|
2017-08-13 04:25:43 +02:00
|
|
|
_, err = osStat((volumeDir))
|
2016-04-13 20:32:47 +02:00
|
|
|
if err != nil {
|
2016-05-18 06:22:27 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return errVolumeNotFound
|
|
|
|
}
|
2016-04-13 20:32:47 +02:00
|
|
|
return err
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Following code is needed so that we retain "/" suffix if any in
|
2016-04-13 20:32:47 +02:00
|
|
|
// path argument.
|
2016-05-05 10:39:26 +02:00
|
|
|
filePath := pathJoin(volumeDir, path)
|
2017-08-13 04:25:43 +02:00
|
|
|
if err = checkPathLength((filePath)); err != nil {
|
2016-05-11 21:55:02 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-04-08 19:37:38 +02:00
|
|
|
|
|
|
|
// Delete file and delete parent directory as well if its empty.
|
2016-04-13 20:32:47 +02:00
|
|
|
return deleteFile(volumeDir, filePath)
|
config/main: Re-write config files - add to new config v3
- New config format.
```
{
"version": "3",
"address": ":9000",
"backend": {
"type": "fs",
"disk": "/path"
},
"credential": {
"accessKey": "WLGDGYAQYIGI833EV05A",
"secretKey": "BYvgJM101sHngl2uzjXS/OBF/aMxAN06JrJ3qJlF"
},
"region": "us-east-1",
"logger": {
"file": {
"enable": false,
"fileName": "",
"level": "error"
},
"syslog": {
"enable": false,
"address": "",
"level": "debug"
},
"console": {
"enable": true,
"level": "fatal"
}
}
}
```
New command lines in lieu of supporting XL.
Minio initialize filesystem backend.
~~~
$ minio init fs <path>
~~~
Minio initialize XL backend.
~~~
$ minio init xl <url1>...<url16>
~~~
For 'fs' backend it starts the server.
~~~
$ minio server
~~~
For 'xl' backend it waits for servers to join.
~~~
$ minio server
... [PROGRESS BAR] of servers connecting
~~~
Now on other servers execute 'join' and they connect.
~~~
....
minio join <url1> -- from <url2> && minio server
minio join <url1> -- from <url3> && minio server
...
...
minio join <url1> -- from <url16> && minio server
~~~
2016-02-13 00:27:10 +01:00
|
|
|
}
|
2016-04-29 21:17:48 +02:00
|
|
|
|
2016-05-29 00:13:15 +02:00
|
|
|
// RenameFile - rename source path to destination path atomically.
|
2016-06-21 01:57:14 +02:00
|
|
|
func (s *posix) RenameFile(srcVolume, srcPath, dstVolume, dstPath string) (err error) {
|
2016-06-09 07:02:10 +02:00
|
|
|
defer func() {
|
|
|
|
if err == syscall.EIO {
|
2016-06-21 01:57:14 +02:00
|
|
|
atomic.AddInt32(&s.ioErrCount, 1)
|
2016-06-09 07:02:10 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-05-31 18:22:53 +02:00
|
|
|
if atomic.LoadInt32(&s.ioErrCount) > maxAllowedIOError {
|
2016-06-09 07:02:10 +02:00
|
|
|
return errFaultyDisk
|
|
|
|
}
|
|
|
|
|
2016-10-31 17:34:44 +01:00
|
|
|
if err = s.checkDiskFound(); err != nil {
|
2016-06-03 07:49:27 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-05-18 06:22:27 +02:00
|
|
|
srcVolumeDir, err := s.getVolDir(srcVolume)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
dstVolumeDir, err := s.getVolDir(dstVolume)
|
2016-04-29 21:17:48 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-18 06:22:27 +02:00
|
|
|
// Stat a volume entry.
|
2017-08-13 04:25:43 +02:00
|
|
|
_, err = osStat((srcVolumeDir))
|
2016-04-29 21:17:48 +02:00
|
|
|
if err != nil {
|
2016-05-18 06:22:27 +02:00
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return errVolumeNotFound
|
|
|
|
}
|
2016-04-29 21:17:48 +02:00
|
|
|
return err
|
|
|
|
}
|
2017-08-13 04:25:43 +02:00
|
|
|
_, err = osStat((dstVolumeDir))
|
2016-05-18 06:22:27 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return errVolumeNotFound
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-16 23:52:14 +01:00
|
|
|
srcIsDir := hasSuffix(srcPath, slashSeparator)
|
|
|
|
dstIsDir := hasSuffix(dstPath, slashSeparator)
|
2016-05-16 23:31:28 +02:00
|
|
|
// Either src and dst have to be directories or files, else return error.
|
2016-05-13 20:52:36 +02:00
|
|
|
if !(srcIsDir && dstIsDir || !srcIsDir && !dstIsDir) {
|
|
|
|
return errFileAccessDenied
|
|
|
|
}
|
2016-06-17 20:57:51 +02:00
|
|
|
srcFilePath := slashpath.Join(srcVolumeDir, srcPath)
|
2017-08-13 04:25:43 +02:00
|
|
|
if err = checkPathLength((srcFilePath)); err != nil {
|
2016-06-17 20:57:51 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
dstFilePath := slashpath.Join(dstVolumeDir, dstPath)
|
2017-08-13 04:25:43 +02:00
|
|
|
if err = checkPathLength((dstFilePath)); err != nil {
|
2016-06-17 20:57:51 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-05-13 20:52:36 +02:00
|
|
|
if srcIsDir {
|
|
|
|
// If source is a directory we expect the destination to be non-existent always.
|
2017-08-13 04:25:43 +02:00
|
|
|
_, err = osStat((dstFilePath))
|
2016-05-13 20:52:36 +02:00
|
|
|
if err == nil {
|
|
|
|
return errFileAccessDenied
|
|
|
|
}
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Destination does not exist, hence proceed with the rename.
|
|
|
|
}
|
2016-06-24 05:19:27 +02:00
|
|
|
// Creates all the parent directories, with mode 0777 mkdir honors system umask.
|
2017-08-13 04:25:43 +02:00
|
|
|
if err = os.MkdirAll(slashpath.Dir(dstFilePath), 0777); err != nil {
|
2016-05-04 21:18:40 +02:00
|
|
|
// File path cannot be verified since one of the parents is a file.
|
2016-08-31 23:43:20 +02:00
|
|
|
if isSysErrNotDir(err) {
|
2016-05-04 21:18:40 +02:00
|
|
|
return errFileAccessDenied
|
2016-08-31 23:43:20 +02:00
|
|
|
} else if isSysErrPathNotFound(err) {
|
2016-06-20 15:18:47 +02:00
|
|
|
// This is a special case should be handled only for
|
|
|
|
// windows, because windows API does not return "not a
|
|
|
|
// directory" error message. Handle this specifically here.
|
|
|
|
return errFileAccessDenied
|
2016-05-04 21:18:40 +02:00
|
|
|
}
|
2016-04-29 21:17:48 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-06-24 05:19:27 +02:00
|
|
|
// Finally attempt a rename.
|
2017-08-13 04:25:43 +02:00
|
|
|
err = os.Rename((srcFilePath), (dstFilePath))
|
2016-05-04 01:10:24 +02:00
|
|
|
if err != nil {
|
|
|
|
if os.IsNotExist(err) {
|
|
|
|
return errFileNotFound
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
2016-11-22 01:34:57 +01:00
|
|
|
|
|
|
|
// Remove parent dir of the source file if empty
|
2016-12-01 05:56:16 +01:00
|
|
|
if parentDir := slashpath.Dir(srcFilePath); isDirEmpty(parentDir) {
|
2016-11-22 01:34:57 +01:00
|
|
|
deleteFile(srcVolumeDir, parentDir)
|
|
|
|
}
|
|
|
|
|
2016-05-04 01:10:24 +02:00
|
|
|
return nil
|
2016-04-29 21:17:48 +02:00
|
|
|
}
|