2016-07-31 17:11:14 -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-08 13:37:38 -04:00
|
|
|
|
|
|
|
import (
|
2016-08-11 22:32:04 -04:00
|
|
|
"io"
|
2016-07-31 17:11:14 -04:00
|
|
|
"path"
|
2016-04-08 13:37:38 -04:00
|
|
|
|
2018-04-21 22:23:54 -04:00
|
|
|
"github.com/gorilla/mux"
|
2018-04-05 18:04:40 -04:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2018-06-06 04:51:56 -04:00
|
|
|
xrpc "github.com/minio/minio/cmd/rpc"
|
2016-04-08 13:37:38 -04:00
|
|
|
)
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
const storageServiceName = "Storage"
|
|
|
|
const storageServiceSubPath = "/storage"
|
|
|
|
|
|
|
|
var storageServicePath = path.Join(minioReservedBucketPath, storageServiceSubPath)
|
|
|
|
|
|
|
|
// storageRPCReceiver - Storage RPC receiver for storage RPC server
|
|
|
|
type storageRPCReceiver struct {
|
|
|
|
local *posix
|
|
|
|
}
|
|
|
|
|
|
|
|
// VolArgs - generic volume args.
|
|
|
|
type VolArgs struct {
|
|
|
|
AuthArgs
|
|
|
|
Vol string
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2016-08-25 20:16:34 -04:00
|
|
|
/// Storage operations handlers.
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// Connect - authenticates remote connection.
|
|
|
|
func (receiver *storageRPCReceiver) Connect(args *AuthArgs, reply *VoidReply) (err error) {
|
|
|
|
return args.Authenticate()
|
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// DiskInfo - disk info handler is rpc wrapper for DiskInfo operation.
|
|
|
|
func (receiver *storageRPCReceiver) DiskInfo(args *AuthArgs, reply *DiskInfo) (err error) {
|
|
|
|
*reply, err = receiver.local.DiskInfo()
|
2016-08-25 20:16:34 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Volume operations handlers.
|
2016-04-08 13:37:38 -04:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// MakeVol - make vol handler is rpc wrapper for MakeVol operation.
|
|
|
|
func (receiver *storageRPCReceiver) MakeVol(args *VolArgs, reply *VoidReply) error {
|
|
|
|
return receiver.local.MakeVol(args.Vol)
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// ListVols - list vols handler is rpc wrapper for ListVols operation.
|
|
|
|
func (receiver *storageRPCReceiver) ListVols(args *AuthArgs, reply *[]VolInfo) (err error) {
|
|
|
|
*reply, err = receiver.local.ListVols()
|
|
|
|
return err
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// StatVol - stat vol handler is a rpc wrapper for StatVol operation.
|
|
|
|
func (receiver *storageRPCReceiver) StatVol(args *VolArgs, reply *VolInfo) (err error) {
|
|
|
|
*reply, err = receiver.local.StatVol(args.Vol)
|
|
|
|
return err
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// DeleteVol - delete vol handler is a rpc wrapper for
|
2016-04-08 13:37:38 -04:00
|
|
|
// DeleteVol operation.
|
2018-06-06 04:51:56 -04:00
|
|
|
func (receiver *storageRPCReceiver) DeleteVol(args *VolArgs, reply *VoidReply) error {
|
|
|
|
return receiver.local.DeleteVol(args.Vol)
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// File operations
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// StatFileArgs represents stat file RPC arguments.
|
|
|
|
type StatFileArgs struct {
|
|
|
|
AuthArgs
|
|
|
|
Vol string
|
|
|
|
Path string
|
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// StatFile - stat file handler is rpc wrapper to stat file.
|
|
|
|
func (receiver *storageRPCReceiver) StatFile(args *StatFileArgs, reply *FileInfo) (err error) {
|
|
|
|
*reply, err = receiver.local.StatFile(args.Vol, args.Path)
|
|
|
|
return err
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// ListDirArgs represents list contents RPC arguments.
|
|
|
|
type ListDirArgs struct {
|
|
|
|
AuthArgs
|
|
|
|
Vol string
|
|
|
|
Path string
|
|
|
|
Count int
|
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// ListDir - list directory handler is rpc wrapper to list dir.
|
|
|
|
func (receiver *storageRPCReceiver) ListDir(args *ListDirArgs, reply *[]string) (err error) {
|
|
|
|
*reply, err = receiver.local.ListDir(args.Vol, args.Path, args.Count)
|
|
|
|
return err
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// ReadAllArgs represents read all RPC arguments.
|
|
|
|
type ReadAllArgs struct {
|
|
|
|
AuthArgs
|
|
|
|
Vol string
|
|
|
|
Path string
|
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// ReadAll - read all handler is rpc wrapper to read all storage API.
|
|
|
|
func (receiver *storageRPCReceiver) ReadAll(args *ReadAllArgs, reply *[]byte) (err error) {
|
|
|
|
*reply, err = receiver.local.ReadAll(args.Vol, args.Path)
|
|
|
|
return err
|
2016-06-25 17:51:06 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// ReadFileArgs represents read file RPC arguments.
|
|
|
|
type ReadFileArgs struct {
|
|
|
|
AuthArgs
|
|
|
|
Vol string
|
|
|
|
Path string
|
|
|
|
Offset int64
|
2018-09-05 02:22:05 -04:00
|
|
|
Length int64
|
2018-06-06 04:51:56 -04:00
|
|
|
Algo BitrotAlgorithm
|
|
|
|
ExpectedHash []byte
|
|
|
|
Verified bool
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadFile - read file handler is rpc wrapper to read file.
|
|
|
|
func (receiver *storageRPCReceiver) ReadFile(args *ReadFileArgs, reply *[]byte) error {
|
2017-09-25 14:32:56 -04:00
|
|
|
var verifier *BitrotVerifier
|
|
|
|
if !args.Verified {
|
|
|
|
verifier = NewBitrotVerifier(args.Algo, args.ExpectedHash)
|
2016-08-11 22:32:04 -04:00
|
|
|
}
|
2016-05-28 18:13:15 -04:00
|
|
|
|
2018-09-05 02:22:05 -04:00
|
|
|
buf := make([]byte, args.Length)
|
|
|
|
n, err := receiver.local.ReadFile(args.Vol, args.Path, args.Offset, buf, verifier)
|
2018-06-06 04:51:56 -04:00
|
|
|
// Ignore io.ErrEnexpectedEOF for short reads i.e. less content available than requested.
|
2017-05-16 17:21:52 -04:00
|
|
|
if err == io.ErrUnexpectedEOF {
|
|
|
|
err = nil
|
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
|
2018-09-05 02:22:05 -04:00
|
|
|
*reply = buf[0:n]
|
2017-05-16 17:21:52 -04:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// PrepareFileArgs represents append file RPC arguments.
|
|
|
|
type PrepareFileArgs struct {
|
|
|
|
AuthArgs
|
|
|
|
Vol string
|
|
|
|
Path string
|
|
|
|
Size int64
|
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// PrepareFile - prepare file handler is rpc wrapper to prepare file.
|
|
|
|
func (receiver *storageRPCReceiver) PrepareFile(args *PrepareFileArgs, reply *VoidReply) error {
|
|
|
|
return receiver.local.PrepareFile(args.Vol, args.Path, args.Size)
|
2016-10-29 15:44:44 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// AppendFileArgs represents append file RPC arguments.
|
|
|
|
type AppendFileArgs struct {
|
|
|
|
AuthArgs
|
|
|
|
Vol string
|
|
|
|
Path string
|
|
|
|
Buffer []byte
|
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// AppendFile - append file handler is rpc wrapper to append file.
|
|
|
|
func (receiver *storageRPCReceiver) AppendFile(args *AppendFileArgs, reply *VoidReply) error {
|
|
|
|
return receiver.local.AppendFile(args.Vol, args.Path, args.Buffer)
|
2016-05-28 18:13:15 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// DeleteFileArgs represents delete file RPC arguments.
|
|
|
|
type DeleteFileArgs struct {
|
|
|
|
AuthArgs
|
|
|
|
Vol string
|
|
|
|
Path string
|
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// DeleteFile - delete file handler is rpc wrapper to delete file.
|
|
|
|
func (receiver *storageRPCReceiver) DeleteFile(args *DeleteFileArgs, reply *VoidReply) error {
|
|
|
|
return receiver.local.DeleteFile(args.Vol, args.Path)
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// RenameFileArgs represents rename file RPC arguments.
|
|
|
|
type RenameFileArgs struct {
|
|
|
|
AuthArgs
|
|
|
|
SrcVol string
|
|
|
|
SrcPath string
|
|
|
|
DstVol string
|
|
|
|
DstPath string
|
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// RenameFile - rename file handler is rpc wrapper to rename file.
|
|
|
|
func (receiver *storageRPCReceiver) RenameFile(args *RenameFileArgs, reply *VoidReply) error {
|
|
|
|
return receiver.local.RenameFile(args.SrcVol, args.SrcPath, args.DstVol, args.DstPath)
|
2016-05-02 06:12:18 -04:00
|
|
|
}
|
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
// NewStorageRPCServer - returns new storage RPC server.
|
|
|
|
func NewStorageRPCServer(endpointPath string) (*xrpc.Server, error) {
|
|
|
|
storage, err := newPosix(endpointPath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
2017-04-11 18:44:27 -04:00
|
|
|
|
2018-06-06 04:51:56 -04:00
|
|
|
rpcServer := xrpc.NewServer()
|
|
|
|
if err = rpcServer.RegisterName(storageServiceName, &storageRPCReceiver{storage}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return rpcServer, nil
|
2016-04-19 23:05:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// registerStorageRPCRouter - register storage rpc router.
|
2018-06-06 04:51:56 -04:00
|
|
|
func registerStorageRPCRouters(router *mux.Router, endpoints EndpointList) {
|
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
if endpoint.IsLocal {
|
|
|
|
rpcServer, err := NewStorageRPCServer(endpoint.Path)
|
2018-06-06 21:10:51 -04:00
|
|
|
if err != nil {
|
|
|
|
logger.Fatal(uiErrUnableToWriteInBackend(err), "Unable to configure one of server's RPC services")
|
|
|
|
}
|
2018-06-06 04:51:56 -04:00
|
|
|
subrouter := router.PathPrefix(minioReservedBucketPath).Subrouter()
|
2018-08-07 05:51:30 -04:00
|
|
|
subrouter.Path(path.Join(storageServiceSubPath, endpoint.Path)).HandlerFunc(httpTraceHdrs(rpcServer.ServeHTTP))
|
2016-10-13 02:13:24 -04:00
|
|
|
}
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|