2016-04-12 15:45:15 -04:00
|
|
|
/*
|
|
|
|
* Minio Cloud Storage, (C) 2016 Minio, Inc.
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
2016-08-18 17:50:50 -04:00
|
|
|
package cmd
|
2016-04-12 15:45:15 -04:00
|
|
|
|
|
|
|
import (
|
2016-12-08 23:35:07 -05:00
|
|
|
"bytes"
|
2016-08-11 18:55:36 -04:00
|
|
|
"io"
|
2016-09-01 05:49:06 -04:00
|
|
|
"net"
|
|
|
|
"net/rpc"
|
2016-10-27 06:30:52 -04:00
|
|
|
"net/url"
|
2016-07-31 17:11:14 -04:00
|
|
|
"path"
|
2016-12-29 22:42:02 -05:00
|
|
|
"sync"
|
2016-11-23 18:48:10 -05:00
|
|
|
"sync/atomic"
|
2016-12-29 22:42:02 -05:00
|
|
|
"time"
|
2016-08-25 20:16:34 -04:00
|
|
|
|
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-04-12 15:45:15 -04:00
|
|
|
)
|
|
|
|
|
2016-05-05 15:51:56 -04:00
|
|
|
type networkStorage struct {
|
2016-11-23 18:48:10 -05:00
|
|
|
networkIOErrCount int32 // ref: https://golang.org/pkg/sync/atomic/#pkg-note-BUG
|
|
|
|
netAddr string
|
|
|
|
netPath string
|
2016-12-29 22:42:02 -05:00
|
|
|
rpcClient *storageRPCClient
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2016-04-24 03:36:00 -04:00
|
|
|
storageRPCPath = reservedBucket + "/storage"
|
2016-04-12 15:45:15 -04:00
|
|
|
)
|
|
|
|
|
2016-04-16 15:48:41 -04:00
|
|
|
// Converts rpc.ServerError to underlying error. This function is
|
|
|
|
// written so that the storageAPI errors are consistent across network
|
|
|
|
// disks as well.
|
|
|
|
func toStorageErr(err error) error {
|
2016-08-11 22:32:04 -04:00
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2016-09-01 05:49:06 -04:00
|
|
|
|
|
|
|
switch err.(type) {
|
|
|
|
case *net.OpError:
|
|
|
|
return errDiskNotFound
|
|
|
|
}
|
|
|
|
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == rpc.ErrShutdown {
|
|
|
|
return errDiskNotFound
|
|
|
|
}
|
|
|
|
|
2016-04-16 15:48:41 -04:00
|
|
|
switch err.Error() {
|
2016-08-11 18:55:36 -04:00
|
|
|
case io.EOF.Error():
|
|
|
|
return io.EOF
|
|
|
|
case io.ErrUnexpectedEOF.Error():
|
|
|
|
return io.ErrUnexpectedEOF
|
|
|
|
case errUnexpected.Error():
|
|
|
|
return errUnexpected
|
2016-04-19 05:42:10 -04:00
|
|
|
case errDiskFull.Error():
|
|
|
|
return errDiskFull
|
2016-04-16 15:48:41 -04:00
|
|
|
case errVolumeNotFound.Error():
|
|
|
|
return errVolumeNotFound
|
|
|
|
case errVolumeExists.Error():
|
|
|
|
return errVolumeExists
|
|
|
|
case errFileNotFound.Error():
|
|
|
|
return errFileNotFound
|
2016-08-11 18:55:36 -04:00
|
|
|
case errFileNameTooLong.Error():
|
|
|
|
return errFileNameTooLong
|
|
|
|
case errFileAccessDenied.Error():
|
|
|
|
return errFileAccessDenied
|
2016-04-16 15:48:41 -04:00
|
|
|
case errIsNotRegular.Error():
|
|
|
|
return errIsNotRegular
|
|
|
|
case errVolumeNotEmpty.Error():
|
|
|
|
return errVolumeNotEmpty
|
|
|
|
case errVolumeAccessDenied.Error():
|
|
|
|
return errVolumeAccessDenied
|
2016-08-11 18:55:36 -04:00
|
|
|
case errCorruptedFormat.Error():
|
|
|
|
return errCorruptedFormat
|
|
|
|
case errUnformattedDisk.Error():
|
|
|
|
return errUnformattedDisk
|
2016-10-07 14:15:55 -04:00
|
|
|
case errInvalidAccessKeyID.Error():
|
|
|
|
return errInvalidAccessKeyID
|
|
|
|
case errAuthentication.Error():
|
|
|
|
return errAuthentication
|
|
|
|
case errServerVersionMismatch.Error():
|
|
|
|
return errServerVersionMismatch
|
|
|
|
case errServerTimeMismatch.Error():
|
|
|
|
return errServerTimeMismatch
|
2016-04-16 15:48:41 -04:00
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-12-29 22:42:02 -05:00
|
|
|
// storageRPCClient is a wrapper type for RPCClient which provides JWT based authentication across reconnects.
|
|
|
|
type storageRPCClient struct {
|
|
|
|
sync.Mutex
|
|
|
|
cfg storageConfig
|
|
|
|
rpc *RPCClient // reconnect'able rpc client built on top of net/rpc Client
|
|
|
|
serverToken string // Disk rpc JWT based token.
|
|
|
|
serverVersion string // Server version exchanged by the RPC.
|
|
|
|
}
|
|
|
|
|
|
|
|
// Storage config represents authentication credentials and Login
|
|
|
|
// method name to be used for fetching JWT tokens from the storage
|
|
|
|
// server.
|
|
|
|
type storageConfig struct {
|
|
|
|
addr string // Network address path of storage RPC server.
|
|
|
|
path string // Network storage path for HTTP dial.
|
|
|
|
secureConn bool // Indicates if this storage RPC is on a secured connection.
|
|
|
|
creds credential
|
|
|
|
}
|
|
|
|
|
|
|
|
// newStorageClient - returns a jwt based authenticated (go) storage rpc client.
|
|
|
|
func newStorageClient(storageCfg storageConfig) *storageRPCClient {
|
|
|
|
return &storageRPCClient{
|
|
|
|
// Save the config.
|
|
|
|
cfg: storageCfg,
|
|
|
|
rpc: newRPCClient(storageCfg.addr, storageCfg.path, storageCfg.secureConn),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close - closes underlying rpc connection.
|
|
|
|
func (storageClient *storageRPCClient) Close() error {
|
|
|
|
storageClient.Lock()
|
|
|
|
// reset token on closing a connection
|
|
|
|
storageClient.serverToken = ""
|
|
|
|
storageClient.Unlock()
|
|
|
|
return storageClient.rpc.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Login - a jwt based authentication is performed with rpc server.
|
|
|
|
func (storageClient *storageRPCClient) Login() (err error) {
|
|
|
|
storageClient.Lock()
|
|
|
|
// As soon as the function returns unlock,
|
|
|
|
defer storageClient.Unlock()
|
|
|
|
|
|
|
|
// Return if token is already set.
|
|
|
|
if storageClient.serverToken != "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
reply := RPCLoginReply{}
|
|
|
|
if err = storageClient.rpc.Call("Storage.LoginHandler", RPCLoginArgs{
|
|
|
|
Username: storageClient.cfg.creds.AccessKey,
|
|
|
|
Password: storageClient.cfg.creds.SecretKey,
|
|
|
|
}, &reply); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate if version do indeed match.
|
|
|
|
if reply.ServerVersion != Version {
|
|
|
|
return errServerVersionMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate if server timestamp is skewed.
|
|
|
|
curTime := time.Now().UTC()
|
|
|
|
if curTime.Sub(reply.Timestamp) > globalMaxSkewTime {
|
|
|
|
return errServerTimeMismatch
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set token, time stamp as received from a successful login call.
|
|
|
|
storageClient.serverToken = reply.Token
|
|
|
|
storageClient.serverVersion = reply.ServerVersion
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call - If rpc connection isn't established yet since previous disconnect,
|
|
|
|
// connection is established, a jwt authenticated login is performed and then
|
|
|
|
// the call is performed.
|
|
|
|
func (storageClient *storageRPCClient) Call(serviceMethod string, args interface {
|
|
|
|
SetToken(token string)
|
|
|
|
SetTimestamp(tstamp time.Time)
|
|
|
|
}, reply interface{}) (err error) {
|
|
|
|
// On successful login, attempt the call.
|
|
|
|
if err = storageClient.Login(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// Set token and timestamp before the rpc call.
|
|
|
|
args.SetToken(storageClient.serverToken)
|
|
|
|
args.SetTimestamp(time.Now().UTC())
|
|
|
|
|
|
|
|
// Call the underlying rpc.
|
|
|
|
err = storageClient.rpc.Call(serviceMethod, args, reply)
|
|
|
|
|
|
|
|
// Invalidate token, and mark it for re-login.
|
|
|
|
if err == rpc.ErrShutdown {
|
|
|
|
storageClient.Close()
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-27 06:30:52 -04:00
|
|
|
// Initialize new storage rpc client.
|
|
|
|
func newStorageRPC(ep *url.URL) (StorageAPI, error) {
|
|
|
|
if ep == nil {
|
2016-04-12 15:45:15 -04:00
|
|
|
return nil, errInvalidArgument
|
|
|
|
}
|
|
|
|
|
2016-07-31 17:11:14 -04:00
|
|
|
// Dial minio rpc storage http path.
|
2016-10-27 06:30:52 -04:00
|
|
|
rpcPath := path.Join(storageRPCPath, getPath(ep))
|
|
|
|
rpcAddr := ep.Host
|
2016-10-18 15:49:24 -04:00
|
|
|
|
2016-08-15 03:00:18 -04:00
|
|
|
// Initialize rpc client with network address and rpc path.
|
2016-12-29 22:42:02 -05:00
|
|
|
accessKey := serverConfig.GetCredential().AccessKey
|
|
|
|
secretKey := serverConfig.GetCredential().SecretKey
|
2016-10-27 06:30:52 -04:00
|
|
|
if ep.User != nil {
|
2016-12-29 22:42:02 -05:00
|
|
|
accessKey = ep.User.Username()
|
2016-10-27 06:30:52 -04:00
|
|
|
if key, set := ep.User.Password(); set {
|
2016-12-29 22:42:02 -05:00
|
|
|
secretKey = key
|
2016-10-27 06:30:52 -04:00
|
|
|
}
|
|
|
|
}
|
2016-10-06 05:30:54 -04:00
|
|
|
|
2016-04-12 15:45:15 -04:00
|
|
|
// Initialize network storage.
|
2016-05-05 15:51:56 -04:00
|
|
|
ndisk := &networkStorage{
|
2016-12-29 22:42:02 -05:00
|
|
|
netAddr: ep.Host,
|
|
|
|
netPath: getPath(ep),
|
|
|
|
rpcClient: newStorageClient(storageConfig{
|
|
|
|
addr: rpcAddr,
|
|
|
|
path: rpcPath,
|
|
|
|
creds: credential{
|
|
|
|
AccessKey: accessKey,
|
|
|
|
SecretKey: secretKey,
|
|
|
|
},
|
|
|
|
secureConn: isSSL(),
|
|
|
|
}),
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns successfully here.
|
|
|
|
return ndisk, nil
|
|
|
|
}
|
|
|
|
|
2016-10-05 15:48:07 -04:00
|
|
|
// Stringer interface compatible representation of network device.
|
2016-11-23 18:48:10 -05:00
|
|
|
func (n *networkStorage) String() string {
|
2016-10-05 15:48:07 -04:00
|
|
|
return n.netAddr + ":" + n.netPath
|
|
|
|
}
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// maximum allowed network IOError.
|
|
|
|
const maxAllowedNetworkIOError = 1024
|
|
|
|
|
|
|
|
// Initializes the remote RPC connection by attempting a login attempt.
|
|
|
|
func (n *networkStorage) Init() (err error) {
|
|
|
|
// Attempt a login to reconnect.
|
2016-12-29 06:13:51 -05:00
|
|
|
err = n.rpcClient.Login()
|
|
|
|
return toStorageErr(err)
|
2016-11-23 18:48:10 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// Closes the underlying RPC connection.
|
|
|
|
func (n *networkStorage) Close() (err error) {
|
|
|
|
// Close the underlying connection.
|
2016-12-29 06:13:51 -05:00
|
|
|
err = n.rpcClient.Close()
|
|
|
|
return toStorageErr(err)
|
2016-11-23 18:48:10 -05:00
|
|
|
}
|
|
|
|
|
2016-08-25 20:16:34 -04:00
|
|
|
// DiskInfo - fetch disk information for a remote disk.
|
2016-11-23 18:48:10 -05:00
|
|
|
func (n *networkStorage) DiskInfo() (info disk.Info, err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return disk.Info{}, errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:16:34 -04:00
|
|
|
args := GenericArgs{}
|
|
|
|
if err = n.rpcClient.Call("Storage.DiskInfoHandler", &args, &info); err != nil {
|
2016-11-19 20:37:57 -05:00
|
|
|
return disk.Info{}, toStorageErr(err)
|
2016-08-25 20:16:34 -04:00
|
|
|
}
|
|
|
|
return info, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MakeVol - create a volume on a remote disk.
|
2016-11-23 18:48:10 -05:00
|
|
|
func (n *networkStorage) MakeVol(volume string) (err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:45:15 -04:00
|
|
|
reply := GenericReply{}
|
2016-08-23 22:19:24 -04:00
|
|
|
args := GenericVolArgs{Vol: volume}
|
2016-08-22 14:01:21 -04:00
|
|
|
if err := n.rpcClient.Call("Storage.MakeVolHandler", &args, &reply); err != nil {
|
2016-04-16 15:48:41 -04:00
|
|
|
return toStorageErr(err)
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:16:34 -04:00
|
|
|
// ListVols - List all volumes on a remote disk.
|
2016-11-23 18:48:10 -05:00
|
|
|
func (n *networkStorage) ListVols() (vols []VolInfo, err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return nil, errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:45:15 -04:00
|
|
|
ListVols := ListVolsReply{}
|
2016-08-23 22:19:24 -04:00
|
|
|
err = n.rpcClient.Call("Storage.ListVolsHandler", &GenericArgs{}, &ListVols)
|
2016-04-12 15:45:15 -04:00
|
|
|
if err != nil {
|
2016-11-19 20:37:57 -05:00
|
|
|
return nil, toStorageErr(err)
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
return ListVols.Vols, nil
|
|
|
|
}
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// StatVol - get volume info over the network.
|
|
|
|
func (n *networkStorage) StatVol(volume string) (volInfo VolInfo, err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return VolInfo{}, errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-08-23 22:19:24 -04:00
|
|
|
args := GenericVolArgs{Vol: volume}
|
2016-08-22 14:01:21 -04:00
|
|
|
if err = n.rpcClient.Call("Storage.StatVolHandler", &args, &volInfo); err != nil {
|
2016-04-16 15:48:41 -04:00
|
|
|
return VolInfo{}, toStorageErr(err)
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
return volInfo, nil
|
|
|
|
}
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// DeleteVol - Deletes a volume over the network.
|
|
|
|
func (n *networkStorage) DeleteVol(volume string) (err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:45:15 -04:00
|
|
|
reply := GenericReply{}
|
2016-08-23 22:19:24 -04:00
|
|
|
args := GenericVolArgs{Vol: volume}
|
2016-08-22 14:01:21 -04:00
|
|
|
if err := n.rpcClient.Call("Storage.DeleteVolHandler", &args, &reply); err != nil {
|
2016-04-16 15:48:41 -04:00
|
|
|
return toStorageErr(err)
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// File operations.
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
func (n *networkStorage) PrepareFile(volume, path string, length int64) (err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-10-29 15:44:44 -04:00
|
|
|
reply := GenericReply{}
|
|
|
|
if err = n.rpcClient.Call("Storage.PrepareFileHandler", &PrepareFileArgs{
|
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
|
|
|
Size: length,
|
|
|
|
}, &reply); err != nil {
|
|
|
|
return toStorageErr(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// AppendFile - append file writes buffer to a remote network path.
|
|
|
|
func (n *networkStorage) AppendFile(volume, path string, buffer []byte) (err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-06-19 18:31:13 -04:00
|
|
|
reply := GenericReply{}
|
2016-08-22 14:01:21 -04:00
|
|
|
if err = n.rpcClient.Call("Storage.AppendFileHandler", &AppendFileArgs{
|
2016-08-23 22:19:24 -04:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
|
|
|
Buffer: buffer,
|
2016-06-19 18:31:13 -04:00
|
|
|
}, &reply); err != nil {
|
|
|
|
return toStorageErr(err)
|
2016-05-28 18:13:15 -04:00
|
|
|
}
|
2016-06-19 18:31:13 -04:00
|
|
|
return nil
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// StatFile - get latest Stat information for a file at path.
|
2016-11-23 18:48:10 -05:00
|
|
|
func (n *networkStorage) StatFile(volume, path string) (fileInfo FileInfo, err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return FileInfo{}, errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-08-22 14:01:21 -04:00
|
|
|
if err = n.rpcClient.Call("Storage.StatFileHandler", &StatFileArgs{
|
2016-08-23 22:19:24 -04:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
2016-04-12 15:45:15 -04:00
|
|
|
}, &fileInfo); err != nil {
|
2016-04-16 15:48:41 -04:00
|
|
|
return FileInfo{}, toStorageErr(err)
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
return fileInfo, nil
|
|
|
|
}
|
|
|
|
|
2016-06-27 01:05:48 -04:00
|
|
|
// ReadAll - reads entire contents of the file at path until EOF, returns the
|
2016-06-25 17:51:06 -04:00
|
|
|
// contents in a byte slice. Returns buf == nil if err != nil.
|
|
|
|
// 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.
|
2016-11-23 18:48:10 -05:00
|
|
|
func (n *networkStorage) ReadAll(volume, path string) (buf []byte, err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return nil, errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-08-22 14:01:21 -04:00
|
|
|
if err = n.rpcClient.Call("Storage.ReadAllHandler", &ReadAllArgs{
|
2016-08-23 22:19:24 -04:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
2016-06-25 17:51:06 -04:00
|
|
|
}, &buf); err != nil {
|
|
|
|
return nil, toStorageErr(err)
|
|
|
|
}
|
|
|
|
return buf, nil
|
|
|
|
}
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// ReadFile - reads a file at remote path and fills the buffer.
|
|
|
|
func (n *networkStorage) ReadFile(volume string, path string, offset int64, buffer []byte) (m int64, err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2016-12-08 23:35:07 -05:00
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
// Recover any panic from allocation, and return error.
|
|
|
|
err = bytes.ErrTooLarge
|
|
|
|
}
|
|
|
|
}() // Do not crash the server.
|
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return 0, errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-08-11 22:32:04 -04:00
|
|
|
var result []byte
|
2016-08-22 14:01:21 -04:00
|
|
|
err = n.rpcClient.Call("Storage.ReadFileHandler", &ReadFileArgs{
|
2016-08-23 22:19:24 -04:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
|
|
|
Offset: offset,
|
2016-12-08 23:35:07 -05:00
|
|
|
Buffer: buffer,
|
2016-08-11 22:32:04 -04:00
|
|
|
}, &result)
|
2016-12-08 23:35:07 -05:00
|
|
|
|
2016-08-11 22:32:04 -04:00
|
|
|
// Copy results to buffer.
|
|
|
|
copy(buffer, result)
|
2016-12-08 23:35:07 -05:00
|
|
|
|
2016-08-11 22:32:04 -04:00
|
|
|
// Return length of result, err if any.
|
|
|
|
return int64(len(result)), toStorageErr(err)
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
2016-05-05 15:51:56 -04:00
|
|
|
// ListDir - list all entries at prefix.
|
2016-11-23 18:48:10 -05:00
|
|
|
func (n *networkStorage) ListDir(volume, path string) (entries []string, err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return nil, errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-08-22 14:01:21 -04:00
|
|
|
if err = n.rpcClient.Call("Storage.ListDirHandler", &ListDirArgs{
|
2016-08-23 22:19:24 -04:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
2016-05-05 15:51:56 -04:00
|
|
|
}, &entries); err != nil {
|
|
|
|
return nil, toStorageErr(err)
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
2016-04-16 15:48:41 -04:00
|
|
|
// Return successfully unmarshalled results.
|
2016-05-05 15:51:56 -04:00
|
|
|
return entries, nil
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteFile - Delete a file at path.
|
2016-11-23 18:48:10 -05:00
|
|
|
func (n *networkStorage) DeleteFile(volume, path string) (err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-04-12 15:45:15 -04:00
|
|
|
reply := GenericReply{}
|
2016-08-22 14:01:21 -04:00
|
|
|
if err = n.rpcClient.Call("Storage.DeleteFileHandler", &DeleteFileArgs{
|
2016-08-23 22:19:24 -04:00
|
|
|
Vol: volume,
|
|
|
|
Path: path,
|
2016-04-12 15:45:15 -04:00
|
|
|
}, &reply); err != nil {
|
2016-04-16 15:48:41 -04:00
|
|
|
return toStorageErr(err)
|
2016-04-12 15:45:15 -04:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2016-04-29 15:17:48 -04:00
|
|
|
|
2016-11-23 18:48:10 -05:00
|
|
|
// RenameFile - rename a remote file from source to destination.
|
|
|
|
func (n *networkStorage) RenameFile(srcVolume, srcPath, dstVolume, dstPath string) (err error) {
|
|
|
|
defer func() {
|
2016-12-29 06:13:51 -05:00
|
|
|
if err == errDiskNotFound {
|
2016-11-23 18:48:10 -05:00
|
|
|
atomic.AddInt32(&n.networkIOErrCount, 1)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Take remote disk offline if the total network errors.
|
|
|
|
// are more than maximum allowable IO error limit.
|
|
|
|
if n.networkIOErrCount > maxAllowedNetworkIOError {
|
|
|
|
return errFaultyRemoteDisk
|
|
|
|
}
|
|
|
|
|
2016-05-02 06:12:18 -04:00
|
|
|
reply := GenericReply{}
|
2016-08-22 14:01:21 -04:00
|
|
|
if err = n.rpcClient.Call("Storage.RenameFileHandler", &RenameFileArgs{
|
2016-08-23 22:19:24 -04:00
|
|
|
SrcVol: srcVolume,
|
|
|
|
SrcPath: srcPath,
|
|
|
|
DstVol: dstVolume,
|
|
|
|
DstPath: dstPath,
|
2016-05-02 06:12:18 -04:00
|
|
|
}, &reply); err != nil {
|
|
|
|
return toStorageErr(err)
|
|
|
|
}
|
|
|
|
return nil
|
2016-04-29 15:17:48 -04:00
|
|
|
}
|