2016-07-31 23:11:14 +02: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 23:50:50 +02:00
|
|
|
package cmd
|
2016-04-08 19:37:38 +02:00
|
|
|
|
|
|
|
import (
|
2016-08-12 04:32:04 +02:00
|
|
|
"bytes"
|
|
|
|
"io"
|
2016-04-08 19:37:38 +02:00
|
|
|
"net/rpc"
|
2016-07-31 23:11:14 +02:00
|
|
|
"path"
|
|
|
|
"strings"
|
2016-09-24 12:34:45 +02:00
|
|
|
"time"
|
2016-04-08 19:37:38 +02:00
|
|
|
|
|
|
|
router "github.com/gorilla/mux"
|
2016-10-06 11:30:54 +02:00
|
|
|
"github.com/minio/minio-go/pkg/set"
|
2016-08-26 02:16:34 +02:00
|
|
|
"github.com/minio/minio/pkg/disk"
|
2016-04-08 19:37:38 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
// Storage server implements rpc primitives to facilitate exporting a
|
|
|
|
// disk over a network.
|
|
|
|
type storageServer struct {
|
2016-09-24 12:34:45 +02:00
|
|
|
storage StorageAPI
|
|
|
|
path string
|
|
|
|
timestamp time.Time
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
2016-08-11 06:09:31 +02:00
|
|
|
/// Auth operations
|
|
|
|
|
|
|
|
// Login - login handler.
|
|
|
|
func (s *storageServer) LoginHandler(args *RPCLoginArgs, reply *RPCLoginReply) error {
|
2016-10-05 19:18:55 +02:00
|
|
|
jwt, err := newJWT(defaultInterNodeJWTExpiry)
|
2016-08-11 06:09:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err = jwt.Authenticate(args.Username, args.Password); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
token, err := jwt.GenerateToken(args.Username)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
reply.Token = token
|
2016-09-24 12:34:45 +02:00
|
|
|
reply.Timestamp = s.timestamp
|
2016-08-18 23:50:50 +02:00
|
|
|
reply.ServerVersion = Version
|
2016-08-11 06:09:31 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-26 02:16:34 +02:00
|
|
|
/// Storage operations handlers.
|
|
|
|
|
|
|
|
// DiskInfoHandler - disk info handler is rpc wrapper for DiskInfo operation.
|
|
|
|
func (s *storageServer) DiskInfoHandler(args *GenericArgs, reply *disk.Info) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
|
|
|
return errInvalidToken
|
|
|
|
}
|
|
|
|
info, err := s.storage.DiskInfo()
|
|
|
|
*reply = info
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Volume operations handlers.
|
2016-04-08 19:37:38 +02:00
|
|
|
|
|
|
|
// MakeVolHandler - make vol handler is rpc wrapper for MakeVol operation.
|
2016-08-11 06:09:31 +02:00
|
|
|
func (s *storageServer) MakeVolHandler(args *GenericVolArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
|
|
|
return s.storage.MakeVol(args.Vol)
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// ListVolsHandler - list vols handler is rpc wrapper for ListVols operation.
|
2016-08-22 20:01:21 +02:00
|
|
|
func (s *storageServer) ListVolsHandler(args *GenericArgs, reply *ListVolsReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
2016-04-08 19:37:38 +02: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 06:09:31 +02:00
|
|
|
func (s *storageServer) StatVolHandler(args *GenericVolArgs, reply *VolInfo) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
|
|
|
volInfo, err := s.storage.StatVol(args.Vol)
|
2016-04-08 19:37:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*reply = volInfo
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteVolHandler - delete vol handler is a rpc wrapper for
|
|
|
|
// DeleteVol operation.
|
2016-08-11 06:09:31 +02:00
|
|
|
func (s *storageServer) DeleteVolHandler(args *GenericVolArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
|
|
|
return s.storage.DeleteVol(args.Vol)
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// File operations
|
|
|
|
|
2016-05-05 21:51:56 +02:00
|
|
|
// StatFileHandler - stat file handler is rpc wrapper to stat file.
|
2016-08-11 06:09:31 +02:00
|
|
|
func (s *storageServer) StatFileHandler(args *StatFileArgs, reply *FileInfo) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
|
|
|
fileInfo, err := s.storage.StatFile(args.Vol, args.Path)
|
2016-04-08 19:37:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-05 21:51:56 +02:00
|
|
|
*reply = fileInfo
|
2016-04-08 19:37:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-05 21:51:56 +02:00
|
|
|
// ListDirHandler - list directory handler is rpc wrapper to list dir.
|
2016-08-11 06:09:31 +02:00
|
|
|
func (s *storageServer) ListDirHandler(args *ListDirArgs, reply *[]string) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
|
|
|
entries, err := s.storage.ListDir(args.Vol, args.Path)
|
2016-04-08 19:37:38 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-05-05 21:51:56 +02:00
|
|
|
*reply = entries
|
2016-04-08 19:37:38 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-06-25 23:51:06 +02:00
|
|
|
// ReadAllHandler - read all handler is rpc wrapper to read all storage API.
|
2016-08-11 06:09:31 +02:00
|
|
|
func (s *storageServer) ReadAllHandler(args *ReadFileArgs, reply *[]byte) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
|
|
|
buf, err := s.storage.ReadAll(args.Vol, args.Path)
|
2016-06-25 23:51:06 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-08-10 08:57:16 +02:00
|
|
|
*reply = buf
|
2016-06-25 23:51:06 +02:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-05-29 00:13:15 +02:00
|
|
|
// ReadFileHandler - read file handler is rpc wrapper to read file.
|
2016-08-12 04:32:04 +02:00
|
|
|
func (s *storageServer) ReadFileHandler(args *ReadFileArgs, reply *[]byte) (err error) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
// Recover any panic and return ErrCacheFull.
|
|
|
|
err = bytes.ErrTooLarge
|
|
|
|
}
|
|
|
|
}() // Do not crash the server.
|
2016-08-11 06:09:31 +02:00
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
2016-08-12 04:32:04 +02:00
|
|
|
// Allocate the requested buffer from the client.
|
|
|
|
*reply = make([]byte, args.Size)
|
|
|
|
var n int64
|
|
|
|
n, err = s.storage.ReadFile(args.Vol, args.Path, args.Offset, *reply)
|
|
|
|
// 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 = (*reply)[0:n]
|
|
|
|
return err
|
2016-05-29 00:13:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendFileHandler - append file handler is rpc wrapper to append file.
|
2016-08-11 06:09:31 +02:00
|
|
|
func (s *storageServer) AppendFileHandler(args *AppendFileArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
|
|
|
return s.storage.AppendFile(args.Vol, args.Path, args.Buffer)
|
2016-05-29 00:13:15 +02:00
|
|
|
}
|
|
|
|
|
2016-04-08 19:37:38 +02:00
|
|
|
// DeleteFileHandler - delete file handler is rpc wrapper to delete file.
|
2016-08-11 06:09:31 +02:00
|
|
|
func (s *storageServer) DeleteFileHandler(args *DeleteFileArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
|
|
|
return s.storage.DeleteFile(args.Vol, args.Path)
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
|
|
|
|
2016-05-02 12:12:18 +02:00
|
|
|
// RenameFileHandler - rename file handler is rpc wrapper to rename file.
|
2016-08-11 06:09:31 +02:00
|
|
|
func (s *storageServer) RenameFileHandler(args *RenameFileArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 19:14:14 +02:00
|
|
|
return errInvalidToken
|
2016-08-11 06:09:31 +02:00
|
|
|
}
|
|
|
|
return s.storage.RenameFile(args.SrcVol, args.SrcPath, args.DstVol, args.DstPath)
|
2016-05-02 12:12:18 +02:00
|
|
|
}
|
|
|
|
|
2016-10-05 21:48:07 +02:00
|
|
|
// TryInitHandler - wake up storage server.
|
|
|
|
func (s *storageServer) TryInitHandler(args *GenericArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
|
|
|
return errInvalidToken
|
|
|
|
}
|
|
|
|
go func() {
|
|
|
|
globalWakeupCh <- struct{}{}
|
|
|
|
}()
|
|
|
|
*reply = GenericReply{}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-04-20 05:05:38 +02:00
|
|
|
// Initialize new storage rpc.
|
2016-07-31 23:11:14 +02:00
|
|
|
func newRPCServer(serverConfig serverCmdConfig) (servers []*storageServer, err error) {
|
2016-04-29 23:24:10 +02:00
|
|
|
// Initialize posix storage API.
|
2016-07-31 23:11:14 +02:00
|
|
|
exports := serverConfig.disks
|
|
|
|
ignoredExports := serverConfig.ignoredDisks
|
|
|
|
|
2016-10-06 11:30:54 +02:00
|
|
|
// Initialize ignored disks in a new set.
|
|
|
|
ignoredSet := set.NewStringSet()
|
|
|
|
if len(ignoredExports) > 0 {
|
|
|
|
ignoredSet = set.CreateStringSet(ignoredExports...)
|
2016-07-31 23:11:14 +02:00
|
|
|
}
|
2016-10-07 20:15:55 +02:00
|
|
|
tstamp := time.Now().UTC()
|
2016-07-31 23:11:14 +02:00
|
|
|
for _, export := range exports {
|
2016-10-06 11:30:54 +02:00
|
|
|
if ignoredSet.Contains(export) {
|
|
|
|
// Ignore initializing ignored export.
|
2016-07-31 23:11:14 +02:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
// e.g server:/mnt/disk1
|
|
|
|
if isLocalStorage(export) {
|
|
|
|
if idx := strings.LastIndex(export, ":"); idx != -1 {
|
|
|
|
export = export[idx+1:]
|
|
|
|
}
|
|
|
|
var storage StorageAPI
|
|
|
|
storage, err = newPosix(export)
|
|
|
|
if err != nil && err != errDiskNotFound {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if idx := strings.LastIndex(export, ":"); idx != -1 {
|
|
|
|
export = export[idx+1:]
|
|
|
|
}
|
|
|
|
servers = append(servers, &storageServer{
|
2016-09-24 12:34:45 +02:00
|
|
|
storage: storage,
|
|
|
|
path: export,
|
2016-10-07 20:15:55 +02:00
|
|
|
timestamp: tstamp,
|
2016-07-31 23:11:14 +02:00
|
|
|
})
|
|
|
|
}
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|
2016-10-06 11:30:54 +02:00
|
|
|
return servers, nil
|
2016-04-20 05:05:38 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// registerStorageRPCRouter - register storage rpc router.
|
2016-10-05 21:48:07 +02:00
|
|
|
func registerStorageRPCRouters(mux *router.Router, srvCmdConfig serverCmdConfig) {
|
|
|
|
// Initialize storage rpc servers for every disk that is hosted on this node.
|
|
|
|
storageRPCs, err := newRPCServer(srvCmdConfig)
|
|
|
|
fatalIf(err, "Unable to initialize storage RPC server.")
|
|
|
|
|
2016-07-31 23:11:14 +02:00
|
|
|
// Create a unique route for each disk exported from this node.
|
2016-10-05 21:48:07 +02:00
|
|
|
for _, stServer := range storageRPCs {
|
2016-08-10 08:57:16 +02:00
|
|
|
storageRPCServer := rpc.NewServer()
|
2016-07-31 23:11:14 +02:00
|
|
|
storageRPCServer.RegisterName("Storage", stServer)
|
|
|
|
// Add minio storage routes.
|
|
|
|
storageRouter := mux.PathPrefix(reservedBucket).Subrouter()
|
|
|
|
storageRouter.Path(path.Join("/storage", stServer.path)).Handler(storageRPCServer)
|
|
|
|
}
|
2016-04-08 19:37:38 +02:00
|
|
|
}
|