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-09-24 06:34:45 -04:00
|
|
|
"time"
|
2016-04-08 13:37:38 -04:00
|
|
|
|
|
|
|
router "github.com/gorilla/mux"
|
2016-08-25 20:16:34 -04:00
|
|
|
"github.com/minio/minio/pkg/disk"
|
2017-11-25 14:58:29 -05:00
|
|
|
"github.com/minio/minio/pkg/errors"
|
2016-04-08 13:37:38 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Storage server implements rpc primitives to facilitate exporting a
|
|
|
|
// disk over a network.
|
|
|
|
type storageServer struct {
|
2016-12-23 10:12:19 -05:00
|
|
|
AuthRPCServer
|
2016-09-24 06:34:45 -04:00
|
|
|
storage StorageAPI
|
|
|
|
path string
|
|
|
|
timestamp time.Time
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2016-08-25 20:16:34 -04:00
|
|
|
/// Storage operations handlers.
|
|
|
|
|
|
|
|
// DiskInfoHandler - disk info handler is rpc wrapper for DiskInfo operation.
|
2016-12-23 10:12:19 -05:00
|
|
|
func (s *storageServer) DiskInfoHandler(args *AuthRPCArgs, reply *disk.Info) error {
|
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-25 20:16:34 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-08-25 20:16:34 -04:00
|
|
|
info, err := s.storage.DiskInfo()
|
|
|
|
*reply = info
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Volume operations handlers.
|
2016-04-08 13:37:38 -04:00
|
|
|
|
|
|
|
// MakeVolHandler - make vol handler is rpc wrapper for MakeVol operation.
|
2016-12-23 10:12:19 -05:00
|
|
|
func (s *storageServer) MakeVolHandler(args *GenericVolArgs, reply *AuthRPCReply) error {
|
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-08-11 00:09:31 -04:00
|
|
|
return s.storage.MakeVol(args.Vol)
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListVolsHandler - list vols handler is rpc wrapper for ListVols operation.
|
2016-12-23 10:12:19 -05:00
|
|
|
func (s *storageServer) ListVolsHandler(args *AuthRPCArgs, reply *ListVolsReply) error {
|
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-04-08 13:37:38 -04:00
|
|
|
vols, err := s.storage.ListVols()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Vols = vols
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// StatVolHandler - stat vol handler is a rpc wrapper for StatVol operation.
|
2016-08-11 00:09:31 -04:00
|
|
|
func (s *storageServer) StatVolHandler(args *GenericVolArgs, reply *VolInfo) error {
|
2016-12-23 10:12:19 -05:00
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-08-11 00:09:31 -04:00
|
|
|
volInfo, err := s.storage.StatVol(args.Vol)
|
2016-04-08 13:37:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*reply = volInfo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVolHandler - delete vol handler is a rpc wrapper for
|
|
|
|
// DeleteVol operation.
|
2016-12-23 10:12:19 -05:00
|
|
|
func (s *storageServer) DeleteVolHandler(args *GenericVolArgs, reply *AuthRPCReply) error {
|
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-08-11 00:09:31 -04:00
|
|
|
return s.storage.DeleteVol(args.Vol)
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/// File operations
|
|
|
|
|
2016-05-05 15:51:56 -04:00
|
|
|
// StatFileHandler - stat file handler is rpc wrapper to stat file.
|
2016-08-11 00:09:31 -04:00
|
|
|
func (s *storageServer) StatFileHandler(args *StatFileArgs, reply *FileInfo) error {
|
2016-12-23 10:12:19 -05:00
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-08-11 00:09:31 -04:00
|
|
|
fileInfo, err := s.storage.StatFile(args.Vol, args.Path)
|
2016-04-08 13:37:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-05 15:51:56 -04:00
|
|
|
*reply = fileInfo
|
2016-04-08 13:37:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-05 15:51:56 -04:00
|
|
|
// ListDirHandler - list directory handler is rpc wrapper to list dir.
|
2016-08-11 00:09:31 -04:00
|
|
|
func (s *storageServer) ListDirHandler(args *ListDirArgs, reply *[]string) error {
|
2016-12-23 10:12:19 -05:00
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-08-11 00:09:31 -04:00
|
|
|
entries, err := s.storage.ListDir(args.Vol, args.Path)
|
2016-04-08 13:37:38 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-05 15:51:56 -04:00
|
|
|
*reply = entries
|
2016-04-08 13:37:38 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-25 17:51:06 -04:00
|
|
|
// ReadAllHandler - read all handler is rpc wrapper to read all storage API.
|
2016-08-11 00:09:31 -04:00
|
|
|
func (s *storageServer) ReadAllHandler(args *ReadFileArgs, reply *[]byte) error {
|
2016-12-23 10:12:19 -05:00
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-08-11 00:09:31 -04:00
|
|
|
buf, err := s.storage.ReadAll(args.Vol, args.Path)
|
2016-06-25 17:51:06 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-10 02:57:16 -04:00
|
|
|
*reply = buf
|
2016-06-25 17:51:06 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-28 18:13:15 -04:00
|
|
|
// ReadFileHandler - read file handler is rpc wrapper to read file.
|
2016-08-11 22:32:04 -04:00
|
|
|
func (s *storageServer) ReadFileHandler(args *ReadFileArgs, reply *[]byte) (err error) {
|
2016-12-23 10:12:19 -05:00
|
|
|
if err = args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
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
|
|
|
|
2017-09-25 14:32:56 -04:00
|
|
|
var n int64
|
|
|
|
n, err = s.storage.ReadFile(args.Vol, args.Path, args.Offset, args.Buffer, verifier)
|
2017-05-16 17:21:52 -04:00
|
|
|
// Sending an error over the rpc layer, would cause unmarshalling to fail. In situations
|
|
|
|
// when we have short read i.e `io.ErrUnexpectedEOF` treat it as good condition and copy
|
|
|
|
// the buffer properly.
|
|
|
|
if err == io.ErrUnexpectedEOF {
|
|
|
|
// Reset to nil as good condition.
|
|
|
|
err = nil
|
|
|
|
}
|
|
|
|
*reply = args.Buffer[0:n]
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-29 15:44:44 -04:00
|
|
|
// PrepareFileHandler - prepare file handler is rpc wrapper to prepare file.
|
2016-12-23 10:12:19 -05:00
|
|
|
func (s *storageServer) PrepareFileHandler(args *PrepareFileArgs, reply *AuthRPCReply) error {
|
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-10-29 15:44:44 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-10-29 15:44:44 -04:00
|
|
|
return s.storage.PrepareFile(args.Vol, args.Path, args.Size)
|
|
|
|
}
|
|
|
|
|
2016-05-28 18:13:15 -04:00
|
|
|
// AppendFileHandler - append file handler is rpc wrapper to append file.
|
2016-12-23 10:12:19 -05:00
|
|
|
func (s *storageServer) AppendFileHandler(args *AppendFileArgs, reply *AuthRPCReply) error {
|
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-08-11 00:09:31 -04:00
|
|
|
return s.storage.AppendFile(args.Vol, args.Path, args.Buffer)
|
2016-05-28 18:13:15 -04:00
|
|
|
}
|
|
|
|
|
2016-04-08 13:37:38 -04:00
|
|
|
// DeleteFileHandler - delete file handler is rpc wrapper to delete file.
|
2016-12-23 10:12:19 -05:00
|
|
|
func (s *storageServer) DeleteFileHandler(args *DeleteFileArgs, reply *AuthRPCReply) error {
|
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-08-11 00:09:31 -04:00
|
|
|
return s.storage.DeleteFile(args.Vol, args.Path)
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2016-05-02 06:12:18 -04:00
|
|
|
// RenameFileHandler - rename file handler is rpc wrapper to rename file.
|
2016-12-23 10:12:19 -05:00
|
|
|
func (s *storageServer) RenameFileHandler(args *RenameFileArgs, reply *AuthRPCReply) error {
|
|
|
|
if err := args.IsAuthenticated(); err != nil {
|
|
|
|
return err
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-12-23 10:12:19 -05:00
|
|
|
|
2016-08-11 00:09:31 -04:00
|
|
|
return s.storage.RenameFile(args.SrcVol, args.SrcPath, args.DstVol, args.DstPath)
|
2016-05-02 06:12:18 -04:00
|
|
|
}
|
|
|
|
|
2016-04-19 23:05:38 -04:00
|
|
|
// Initialize new storage rpc.
|
2017-07-18 12:30:46 -04:00
|
|
|
func newStorageRPCServer(endpoints EndpointList) (servers []*storageServer, err error) {
|
2017-04-11 18:44:27 -04:00
|
|
|
for _, endpoint := range endpoints {
|
|
|
|
if endpoint.IsLocal {
|
|
|
|
storage, err := newPosix(endpoint.Path)
|
2016-07-31 17:11:14 -04:00
|
|
|
if err != nil && err != errDiskNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-04-11 18:44:27 -04:00
|
|
|
|
2016-07-31 17:11:14 -04:00
|
|
|
servers = append(servers, &storageServer{
|
2016-10-11 03:50:27 -04:00
|
|
|
storage: storage,
|
2017-04-11 18:44:27 -04:00
|
|
|
path: endpoint.Path,
|
2016-07-31 17:11:14 -04:00
|
|
|
})
|
|
|
|
}
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
2017-04-11 18:44:27 -04:00
|
|
|
|
2016-10-06 05:30:54 -04:00
|
|
|
return servers, nil
|
2016-04-19 23:05:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// registerStorageRPCRouter - register storage rpc router.
|
2017-04-11 18:44:27 -04:00
|
|
|
func registerStorageRPCRouters(mux *router.Router, endpoints EndpointList) error {
|
2016-10-05 15:48:07 -04:00
|
|
|
// Initialize storage rpc servers for every disk that is hosted on this node.
|
2017-07-18 12:30:46 -04:00
|
|
|
storageRPCs, err := newStorageRPCServer(endpoints)
|
2016-10-13 02:13:24 -04:00
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return errors.Trace(err)
|
2016-10-13 02:13:24 -04:00
|
|
|
}
|
2016-10-05 15:48:07 -04:00
|
|
|
|
2016-07-31 17:11:14 -04:00
|
|
|
// Create a unique route for each disk exported from this node.
|
2016-10-05 15:48:07 -04:00
|
|
|
for _, stServer := range storageRPCs {
|
2017-07-18 12:30:46 -04:00
|
|
|
storageRPCServer := newRPCServer()
|
2016-10-13 02:13:24 -04:00
|
|
|
err = storageRPCServer.RegisterName("Storage", stServer)
|
|
|
|
if err != nil {
|
2017-11-25 14:58:29 -05:00
|
|
|
return errors.Trace(err)
|
2016-10-13 02:13:24 -04:00
|
|
|
}
|
2016-07-31 17:11:14 -04:00
|
|
|
// Add minio storage routes.
|
2017-02-16 17:52:14 -05:00
|
|
|
storageRouter := mux.PathPrefix(minioReservedBucketPath).Subrouter()
|
2017-02-18 05:52:11 -05:00
|
|
|
storageRouter.Path(path.Join(storageRPCPath, stServer.path)).Handler(storageRPCServer)
|
2016-07-31 17:11:14 -04:00
|
|
|
}
|
2016-10-13 02:13:24 -04:00
|
|
|
return nil
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|