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
|
|
|
"bytes"
|
|
|
|
"io"
|
2016-04-08 13:37:38 -04:00
|
|
|
"net/rpc"
|
2016-07-31 17:11:14 -04:00
|
|
|
"path"
|
|
|
|
"strings"
|
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"
|
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-09-24 06:34:45 -04:00
|
|
|
storage StorageAPI
|
|
|
|
path string
|
|
|
|
timestamp time.Time
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
|
|
|
|
2016-08-11 00:09:31 -04:00
|
|
|
/// Auth operations
|
|
|
|
|
|
|
|
// Login - login handler.
|
|
|
|
func (s *storageServer) LoginHandler(args *RPCLoginArgs, reply *RPCLoginReply) error {
|
|
|
|
jwt, err := newJWT(defaultTokenExpiry)
|
|
|
|
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 06:34:45 -04:00
|
|
|
reply.Timestamp = s.timestamp
|
2016-08-18 17:50:50 -04:00
|
|
|
reply.ServerVersion = Version
|
2016-08-11 00:09:31 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-08-25 20:16:34 -04: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 13:37:38 -04:00
|
|
|
|
|
|
|
// MakeVolHandler - make vol handler is rpc wrapper for MakeVol operation.
|
2016-08-11 00:09:31 -04:00
|
|
|
func (s *storageServer) MakeVolHandler(args *GenericVolArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
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-08-22 14:01:21 -04:00
|
|
|
func (s *storageServer) ListVolsHandler(args *GenericArgs, reply *ListVolsReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
2016-08-11 00:09:31 -04: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 {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
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-08-11 00:09:31 -04:00
|
|
|
func (s *storageServer) DeleteVolHandler(args *GenericVolArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
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 {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
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 {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
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 {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
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) {
|
|
|
|
defer func() {
|
|
|
|
if r := recover(); r != nil {
|
|
|
|
// Recover any panic and return ErrCacheFull.
|
|
|
|
err = bytes.ErrTooLarge
|
|
|
|
}
|
|
|
|
}() // Do not crash the server.
|
2016-08-11 00:09:31 -04:00
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
2016-08-11 00:09:31 -04:00
|
|
|
}
|
2016-08-11 22:32:04 -04: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-28 18:13:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// AppendFileHandler - append file handler is rpc wrapper to append file.
|
2016-08-11 00:09:31 -04:00
|
|
|
func (s *storageServer) AppendFileHandler(args *AppendFileArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
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-08-11 00:09:31 -04:00
|
|
|
func (s *storageServer) DeleteFileHandler(args *DeleteFileArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
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-08-11 00:09:31 -04:00
|
|
|
func (s *storageServer) RenameFileHandler(args *RenameFileArgs, reply *GenericReply) error {
|
|
|
|
if !isRPCTokenValid(args.Token) {
|
2016-08-24 13:14:14 -04:00
|
|
|
return errInvalidToken
|
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.
|
2016-07-31 17:11:14 -04:00
|
|
|
func newRPCServer(serverConfig serverCmdConfig) (servers []*storageServer, err error) {
|
2016-04-29 17:24:10 -04:00
|
|
|
// Initialize posix storage API.
|
2016-07-31 17:11:14 -04:00
|
|
|
exports := serverConfig.disks
|
|
|
|
ignoredExports := serverConfig.ignoredDisks
|
|
|
|
|
|
|
|
// Save ignored disks in a map
|
|
|
|
skipDisks := make(map[string]bool)
|
|
|
|
for _, ignoredExport := range ignoredExports {
|
|
|
|
skipDisks[ignoredExport] = true
|
|
|
|
}
|
2016-09-24 06:34:45 -04:00
|
|
|
t := time.Now().UTC()
|
2016-07-31 17:11:14 -04:00
|
|
|
for _, export := range exports {
|
|
|
|
if skipDisks[export] {
|
|
|
|
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 06:34:45 -04:00
|
|
|
storage: storage,
|
|
|
|
path: export,
|
|
|
|
timestamp: t,
|
2016-07-31 17:11:14 -04:00
|
|
|
})
|
|
|
|
}
|
2016-04-08 13:37:38 -04:00
|
|
|
}
|
2016-07-31 17:11:14 -04:00
|
|
|
return servers, err
|
2016-04-19 23:05:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// registerStorageRPCRouter - register storage rpc router.
|
2016-07-31 17:11:14 -04:00
|
|
|
func registerStorageRPCRouters(mux *router.Router, stServers []*storageServer) {
|
|
|
|
// Create a unique route for each disk exported from this node.
|
|
|
|
for _, stServer := range stServers {
|
2016-08-10 02:57:16 -04:00
|
|
|
storageRPCServer := rpc.NewServer()
|
2016-07-31 17:11:14 -04: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 13:37:38 -04:00
|
|
|
}
|