2018-03-05 11:59:47 +00:00
|
|
|
/*
|
2019-04-03 08:46:15 +00:00
|
|
|
Copyright 2018 The Ceph-CSI Authors.
|
2018-03-05 11:59:47 +00:00
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cephfs
|
|
|
|
|
|
|
|
import (
|
2019-08-22 17:19:06 +00:00
|
|
|
"context"
|
2020-11-30 10:05:39 +00:00
|
|
|
"errors"
|
2020-10-10 16:17:08 +00:00
|
|
|
"fmt"
|
2019-07-10 10:50:04 +00:00
|
|
|
"path"
|
2019-06-08 05:06:03 +00:00
|
|
|
"strings"
|
2019-01-28 13:59:16 +00:00
|
|
|
|
2020-04-17 09:23:49 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2020-08-05 14:31:22 +00:00
|
|
|
|
|
|
|
fsAdmin "github.com/ceph/go-ceph/cephfs/admin"
|
2020-11-30 10:05:39 +00:00
|
|
|
"github.com/ceph/go-ceph/rados"
|
2018-03-09 16:05:19 +00:00
|
|
|
)
|
|
|
|
|
2021-07-13 12:21:05 +00:00
|
|
|
// clusterAdditionalInfo contains information regarding if resize is
|
|
|
|
// supported in the particular cluster and subvolumegroup is
|
|
|
|
// created or not.
|
|
|
|
// Subvolumegroup creation and volume resize decisions are
|
|
|
|
// taken through this additional cluster information.
|
|
|
|
var clusterAdditionalInfo = make(map[string]*localClusterState)
|
2018-03-22 13:01:10 +00:00
|
|
|
|
2020-07-10 10:44:59 +00:00
|
|
|
const (
|
|
|
|
cephEntityClientPrefix = "client."
|
2020-10-01 07:18:58 +00:00
|
|
|
|
|
|
|
// modeAllRWX can be used for setting permissions to Read-Write-eXecute
|
|
|
|
// for User, Group and Other.
|
2021-07-13 12:21:05 +00:00
|
|
|
modeAllRWX = 0o777
|
2020-07-10 10:44:59 +00:00
|
|
|
)
|
2019-07-10 10:50:04 +00:00
|
|
|
|
2020-10-10 16:17:08 +00:00
|
|
|
// Subvolume holds subvolume information. This includes only the needed members
|
|
|
|
// from fsAdmin.SubVolumeInfo.
|
2020-08-04 04:21:31 +00:00
|
|
|
type Subvolume struct {
|
2020-10-10 16:17:08 +00:00
|
|
|
BytesQuota int64
|
|
|
|
Path string
|
|
|
|
Features []string
|
2020-08-04 04:21:31 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 10:50:04 +00:00
|
|
|
func getVolumeRootPathCephDeprecated(volID volumeID) string {
|
|
|
|
return path.Join("/", "csi-volumes", string(volID))
|
|
|
|
}
|
|
|
|
|
2020-11-30 09:30:01 +00:00
|
|
|
func (vo *volumeOptions) getVolumeRootPathCeph(ctx context.Context, volID volumeID) (string, error) {
|
|
|
|
fsa, err := vo.conn.GetFSAdmin()
|
2019-06-08 05:06:03 +00:00
|
|
|
if err != nil {
|
2020-11-26 08:25:48 +00:00
|
|
|
util.ErrorLog(ctx, "could not get FSAdmin err %s", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-11-26 08:25:48 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2020-11-30 09:30:01 +00:00
|
|
|
svPath, err := fsa.SubVolumePath(vo.FsName, vo.SubvolumeGroup, string(volID))
|
2020-11-26 08:25:48 +00:00
|
|
|
if err != nil {
|
|
|
|
util.ErrorLog(ctx, "failed to get the rootpath for the vol %s: %s", string(volID), err)
|
2020-11-30 10:05:39 +00:00
|
|
|
if errors.Is(err, rados.ErrNotFound) {
|
2020-07-10 00:14:39 +00:00
|
|
|
return "", util.JoinErrors(ErrVolumeNotFound, err)
|
2020-01-23 02:48:46 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-06-08 05:06:03 +00:00
|
|
|
return "", err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-11-26 08:25:48 +00:00
|
|
|
return svPath, nil
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 16:17:08 +00:00
|
|
|
func (vo *volumeOptions) getSubVolumeInfo(ctx context.Context, volID volumeID) (*Subvolume, error) {
|
|
|
|
fsa, err := vo.conn.GetFSAdmin()
|
|
|
|
if err != nil {
|
|
|
|
util.ErrorLog(ctx, "could not get FSAdmin, can not fetch metadata pool for %s:", vo.FsName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-10-10 16:17:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := fsa.SubVolumeInfo(vo.FsName, vo.SubvolumeGroup, string(volID))
|
2020-08-04 04:21:31 +00:00
|
|
|
if err != nil {
|
2020-10-01 11:38:07 +00:00
|
|
|
util.ErrorLog(ctx, "failed to get subvolume info for the vol %s: %s", string(volID), err)
|
2020-11-30 10:10:50 +00:00
|
|
|
if errors.Is(err, rados.ErrNotFound) {
|
2020-10-10 16:17:08 +00:00
|
|
|
return nil, ErrVolumeNotFound
|
2020-08-04 04:21:31 +00:00
|
|
|
}
|
2020-11-30 10:20:07 +00:00
|
|
|
// In case the error is invalid command return error to the caller.
|
|
|
|
var invalid fsAdmin.NotImplementedError
|
|
|
|
if errors.As(err, &invalid) {
|
2020-10-10 16:17:08 +00:00
|
|
|
return nil, ErrInvalidCommand
|
2020-08-04 04:21:31 +00:00
|
|
|
}
|
|
|
|
|
2020-10-10 16:17:08 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-05 14:29:30 +00:00
|
|
|
subvol := Subvolume{
|
|
|
|
// only set BytesQuota when it is of type ByteCount
|
|
|
|
Path: info.Path,
|
|
|
|
Features: make([]string, len(info.Features)),
|
|
|
|
}
|
2020-10-10 16:17:08 +00:00
|
|
|
bc, ok := info.BytesQuota.(fsAdmin.ByteCount)
|
|
|
|
if !ok {
|
2021-02-01 07:44:56 +00:00
|
|
|
// If info.BytesQuota == Infinite (in case it is not set)
|
|
|
|
// or nil (in case the subvolume is in snapshot-retained state),
|
|
|
|
// just continue without returning quota information.
|
|
|
|
// TODO: make use of subvolume "state" attribute once
|
|
|
|
// https://github.com/ceph/go-ceph/issues/453 is fixed.
|
|
|
|
if !(info.BytesQuota == fsAdmin.Infinite || info.BytesQuota == nil) {
|
2020-11-05 14:29:30 +00:00
|
|
|
return nil, fmt.Errorf("subvolume %s has unsupported quota: %v", string(volID), info.BytesQuota)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
subvol.BytesQuota = int64(bc)
|
2020-10-10 16:17:08 +00:00
|
|
|
}
|
|
|
|
for i, feature := range info.Features {
|
|
|
|
subvol.Features[i] = string(feature)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &subvol, nil
|
2020-08-04 04:21:31 +00:00
|
|
|
}
|
|
|
|
|
2020-12-22 07:40:06 +00:00
|
|
|
type operationState int64
|
|
|
|
|
|
|
|
const (
|
|
|
|
unknown operationState = iota
|
|
|
|
supported
|
|
|
|
unsupported
|
|
|
|
)
|
|
|
|
|
2020-06-25 13:48:48 +00:00
|
|
|
type localClusterState struct {
|
2020-12-22 07:40:06 +00:00
|
|
|
// set the enum value i.e., unknown, supported,
|
|
|
|
// unsupported as per the state of the cluster.
|
|
|
|
resizeState operationState
|
2020-06-25 13:48:48 +00:00
|
|
|
// set true once a subvolumegroup is created
|
|
|
|
// for corresponding cluster.
|
|
|
|
subVolumeGroupCreated bool
|
|
|
|
}
|
|
|
|
|
2020-10-01 07:08:09 +00:00
|
|
|
func createVolume(ctx context.Context, volOptions *volumeOptions, volID volumeID, bytesQuota int64) error {
|
2020-06-25 13:48:48 +00:00
|
|
|
// verify if corresponding ClusterID key is present in the map,
|
|
|
|
// and if not, initialize with default values(false).
|
|
|
|
if _, keyPresent := clusterAdditionalInfo[volOptions.ClusterID]; !keyPresent {
|
|
|
|
clusterAdditionalInfo[volOptions.ClusterID] = &localClusterState{}
|
|
|
|
}
|
|
|
|
|
2020-08-05 14:31:22 +00:00
|
|
|
ca, err := volOptions.conn.GetFSAdmin()
|
|
|
|
if err != nil {
|
|
|
|
util.ErrorLog(ctx, "could not get FSAdmin, can not create subvolume %s: %s", string(volID), err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-05 14:31:22 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-25 13:48:48 +00:00
|
|
|
// create subvolumegroup if not already created for the cluster.
|
|
|
|
if !clusterAdditionalInfo[volOptions.ClusterID].subVolumeGroupCreated {
|
2020-08-05 14:31:22 +00:00
|
|
|
opts := fsAdmin.SubVolumeGroupOptions{}
|
|
|
|
err = ca.CreateSubVolumeGroup(volOptions.FsName, volOptions.SubvolumeGroup, &opts)
|
2019-06-08 05:06:03 +00:00
|
|
|
if err != nil {
|
2021-06-25 10:18:59 +00:00
|
|
|
util.ErrorLog(
|
|
|
|
ctx,
|
|
|
|
"failed to create subvolume group %s, for the vol %s: %s",
|
|
|
|
volOptions.SubvolumeGroup,
|
|
|
|
string(volID),
|
|
|
|
err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2018-12-01 09:39:09 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-07-09 14:48:24 +00:00
|
|
|
util.DebugLog(ctx, "cephfs: created subvolume group %s", volOptions.SubvolumeGroup)
|
2020-06-25 13:48:48 +00:00
|
|
|
clusterAdditionalInfo[volOptions.ClusterID].subVolumeGroupCreated = true
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
2019-08-05 15:40:48 +00:00
|
|
|
|
2020-08-05 14:31:22 +00:00
|
|
|
opts := fsAdmin.SubVolumeOptions{
|
|
|
|
Size: fsAdmin.ByteCount(bytesQuota),
|
2020-10-01 07:18:58 +00:00
|
|
|
Mode: modeAllRWX,
|
2019-08-05 15:40:48 +00:00
|
|
|
}
|
|
|
|
if volOptions.Pool != "" {
|
2020-08-05 14:31:22 +00:00
|
|
|
opts.PoolLayout = volOptions.Pool
|
2019-08-05 15:40:48 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 14:31:22 +00:00
|
|
|
// FIXME: check if the right credentials are used ("-n", cephEntityClientPrefix + cr.ID)
|
|
|
|
err = ca.CreateSubVolume(volOptions.FsName, volOptions.SubvolumeGroup, string(volID), &opts)
|
2019-06-08 05:06:03 +00:00
|
|
|
if err != nil {
|
2020-10-01 11:38:07 +00:00
|
|
|
util.ErrorLog(ctx, "failed to create subvolume %s in fs %s: %s", string(volID), volOptions.FsName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
return err
|
|
|
|
}
|
2018-04-13 12:49:49 +00:00
|
|
|
|
2019-02-26 10:06:16 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-05-06 13:47:46 +00:00
|
|
|
// resizeVolume will try to use ceph fs subvolume resize command to resize the
|
|
|
|
// subvolume. If the command is not available as a fallback it will use
|
|
|
|
// CreateVolume to resize the subvolume.
|
2020-10-21 11:32:46 +00:00
|
|
|
func (vo *volumeOptions) resizeVolume(ctx context.Context, volID volumeID, bytesQuota int64) error {
|
2020-06-25 13:48:48 +00:00
|
|
|
// keyPresent checks whether corresponding clusterID key is present in clusterAdditionalInfo
|
|
|
|
var keyPresent bool
|
|
|
|
// verify if corresponding ClusterID key is present in the map,
|
|
|
|
// and if not, initialize with default values(false).
|
2020-10-21 09:07:44 +00:00
|
|
|
if _, keyPresent = clusterAdditionalInfo[vo.ClusterID]; !keyPresent {
|
|
|
|
clusterAdditionalInfo[vo.ClusterID] = &localClusterState{}
|
2020-06-25 13:48:48 +00:00
|
|
|
}
|
|
|
|
// resize subvolume when either it's supported, or when corresponding
|
|
|
|
// clusterID key was not present.
|
2020-12-22 07:40:06 +00:00
|
|
|
if clusterAdditionalInfo[vo.ClusterID].resizeState == unknown ||
|
|
|
|
clusterAdditionalInfo[vo.ClusterID].resizeState == supported {
|
2020-10-21 11:20:52 +00:00
|
|
|
fsa, err := vo.conn.GetFSAdmin()
|
|
|
|
if err != nil {
|
|
|
|
util.ErrorLog(ctx, "could not get FSAdmin, can not resize volume %s:", vo.FsName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-10-21 11:20:52 +00:00
|
|
|
return err
|
2020-05-06 13:47:46 +00:00
|
|
|
}
|
2020-10-21 11:20:52 +00:00
|
|
|
_, err = fsa.ResizeSubVolume(vo.FsName, vo.SubvolumeGroup, string(volID), fsAdmin.ByteCount(bytesQuota), true)
|
2020-05-06 13:47:46 +00:00
|
|
|
if err == nil {
|
2020-12-22 07:40:06 +00:00
|
|
|
clusterAdditionalInfo[vo.ClusterID].resizeState = supported
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-05-06 13:47:46 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-30 10:17:48 +00:00
|
|
|
var invalid fsAdmin.NotImplementedError
|
2020-11-24 11:54:29 +00:00
|
|
|
// In case the error is other than invalid command return error to the caller.
|
2020-11-30 10:17:48 +00:00
|
|
|
if !errors.As(err, &invalid) {
|
2020-10-21 09:07:44 +00:00
|
|
|
util.ErrorLog(ctx, "failed to resize subvolume %s in fs %s: %s", string(volID), vo.FsName, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-05-06 13:47:46 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2020-12-22 07:40:06 +00:00
|
|
|
clusterAdditionalInfo[vo.ClusterID].resizeState = unsupported
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-10-21 09:07:44 +00:00
|
|
|
return createVolume(ctx, vo, volID, bytesQuota)
|
2020-05-06 13:47:46 +00:00
|
|
|
}
|
|
|
|
|
2020-12-09 06:55:37 +00:00
|
|
|
func (vo *volumeOptions) purgeVolume(ctx context.Context, volID volumeID, force bool) error {
|
2020-12-09 06:29:10 +00:00
|
|
|
fsa, err := vo.conn.GetFSAdmin()
|
|
|
|
if err != nil {
|
|
|
|
util.ErrorLog(ctx, "could not get FSAdmin %s:", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-12-09 06:29:10 +00:00
|
|
|
return err
|
2020-08-04 04:23:26 +00:00
|
|
|
}
|
2020-12-09 06:29:10 +00:00
|
|
|
|
|
|
|
opt := fsAdmin.SubVolRmFlags{}
|
|
|
|
opt.Force = force
|
|
|
|
|
2020-12-09 06:01:55 +00:00
|
|
|
if checkSubvolumeHasFeature("snapshot-retention", vo.Features) {
|
2020-12-09 06:29:10 +00:00
|
|
|
opt.RetainSnapshots = true
|
2020-09-10 12:04:51 +00:00
|
|
|
}
|
2020-08-04 04:23:26 +00:00
|
|
|
|
2020-12-09 06:29:10 +00:00
|
|
|
err = fsa.RemoveSubVolumeWithFlags(vo.FsName, vo.SubvolumeGroup, string(volID), opt)
|
2018-08-14 09:19:41 +00:00
|
|
|
if err != nil {
|
2020-12-09 06:01:55 +00:00
|
|
|
util.ErrorLog(ctx, "failed to purge subvolume %s in fs %s: %s", string(volID), vo.FsName, err)
|
2020-09-21 05:38:14 +00:00
|
|
|
if strings.Contains(err.Error(), volumeNotEmpty) {
|
|
|
|
return util.JoinErrors(ErrVolumeHasSnapshots, err)
|
|
|
|
}
|
2020-12-09 06:29:10 +00:00
|
|
|
if errors.Is(err, rados.ErrNotFound) {
|
2020-07-10 00:14:39 +00:00
|
|
|
return util.JoinErrors(ErrVolumeNotFound, err)
|
2020-01-23 02:48:46 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2019-06-08 05:06:03 +00:00
|
|
|
return err
|
2018-04-13 12:49:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2018-03-09 16:05:19 +00:00
|
|
|
}
|
2020-09-10 12:05:53 +00:00
|
|
|
|
|
|
|
// checkSubvolumeHasFeature verifies if the referred subvolume has
|
|
|
|
// the required feature.
|
|
|
|
func checkSubvolumeHasFeature(feature string, subVolFeatures []string) bool {
|
|
|
|
// The subvolume "features" are based on the internal version of the subvolume.
|
|
|
|
// Verify if subvolume supports the required feature.
|
|
|
|
for _, subvolFeature := range subVolFeatures {
|
|
|
|
if subvolFeature == feature {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-09-10 12:05:53 +00:00
|
|
|
return false
|
|
|
|
}
|