2020-08-07 07:06:45 +00:00
|
|
|
/*
|
|
|
|
Copyright 2020 The Ceph-CSI Authors.
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
2021-09-16 13:47:57 +00:00
|
|
|
package core
|
2020-08-07 07:06:45 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2022-06-21 11:49:13 +00:00
|
|
|
"fmt"
|
2020-08-07 07:06:45 +00:00
|
|
|
|
2021-08-25 06:46:03 +00:00
|
|
|
cerrors "github.com/ceph/ceph-csi/internal/cephfs/errors"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2022-06-21 11:49:13 +00:00
|
|
|
|
|
|
|
"github.com/ceph/go-ceph/cephfs/admin"
|
2020-08-07 07:06:45 +00:00
|
|
|
)
|
|
|
|
|
2020-10-28 14:20:41 +00:00
|
|
|
// cephFSCloneState describes the status of the clone.
|
2022-06-21 11:49:13 +00:00
|
|
|
type cephFSCloneState struct {
|
|
|
|
state admin.CloneState
|
|
|
|
errno string
|
|
|
|
errorMsg string
|
|
|
|
}
|
2020-10-28 14:20:41 +00:00
|
|
|
|
2022-06-21 11:49:13 +00:00
|
|
|
// CephFSCloneError indicates that fetching the clone state returned an error.
|
|
|
|
var CephFSCloneError = cephFSCloneState{}
|
|
|
|
|
|
|
|
// ToError checks the state of the clone if it's not cephFSCloneComplete.
|
|
|
|
func (cs cephFSCloneState) ToError() error {
|
|
|
|
switch cs.state {
|
|
|
|
case admin.CloneComplete:
|
2021-07-05 13:10:53 +00:00
|
|
|
return nil
|
2022-06-21 11:49:13 +00:00
|
|
|
case CephFSCloneError.state:
|
|
|
|
return fmt.Errorf("%w: %s (%s)", cerrors.ErrInvalidClone, cs.errorMsg, cs.errno)
|
|
|
|
case admin.CloneInProgress:
|
2021-08-25 06:46:03 +00:00
|
|
|
return cerrors.ErrCloneInProgress
|
2022-06-21 11:49:13 +00:00
|
|
|
case admin.ClonePending:
|
2021-08-25 06:46:03 +00:00
|
|
|
return cerrors.ErrClonePending
|
2022-06-21 11:49:13 +00:00
|
|
|
case admin.CloneFailed:
|
|
|
|
return fmt.Errorf("%w: %s (%s)", cerrors.ErrCloneFailed, cs.errorMsg, cs.errno)
|
2021-07-05 13:10:53 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
// CreateCloneFromSubvolume creates a clone from a subvolume.
|
|
|
|
func (s *subVolumeClient) CreateCloneFromSubvolume(
|
2021-09-16 13:47:57 +00:00
|
|
|
ctx context.Context,
|
2022-06-01 10:17:19 +00:00
|
|
|
parentvolOpt *SubVolume,
|
|
|
|
) error {
|
2022-02-15 12:11:09 +00:00
|
|
|
snapshotID := s.VolID
|
2022-07-28 12:26:55 +00:00
|
|
|
snapClient := NewSnapshot(s.conn, snapshotID, s.clusterID, s.clusterName, s.enableMetadata, parentvolOpt)
|
2022-02-15 12:11:09 +00:00
|
|
|
err := snapClient.CreateSnapshot(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to create snapshot %s %v", snapshotID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-10-18 08:15:53 +00:00
|
|
|
// if cloneErr is not nil we will delete the snapshot
|
|
|
|
var cloneErr error
|
|
|
|
|
|
|
|
defer func() {
|
2020-08-07 07:06:45 +00:00
|
|
|
if cloneErr != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
if err = s.PurgeVolume(ctx, true); err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to delete volume %s: %v", s.VolID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
2022-02-15 12:11:09 +00:00
|
|
|
if err = snapClient.DeleteSnapshot(ctx); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2022-02-15 12:11:09 +00:00
|
|
|
cloneErr = snapClient.CloneSnapshot(ctx, s.SubVolume)
|
2020-08-07 07:06:45 +00:00
|
|
|
if cloneErr != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
log.ErrorLog(ctx, "failed to clone snapshot %s %s to %s %v", parentvolOpt.VolID, snapshotID, s.VolID, cloneErr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return cloneErr
|
|
|
|
}
|
2020-10-28 14:20:41 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
cloneState, cloneErr := s.GetCloneState(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if cloneErr != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to get clone state: %v", cloneErr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return cloneErr
|
|
|
|
}
|
|
|
|
|
2022-06-21 11:49:13 +00:00
|
|
|
err = cloneState.ToError()
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "clone %s did not complete: %v", s.VolID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-06-21 11:49:13 +00:00
|
|
|
return err
|
2021-07-05 13:10:53 +00:00
|
|
|
}
|
2022-01-10 04:56:28 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
err = s.ExpandVolume(ctx, s.Size)
|
2021-07-05 13:10:53 +00:00
|
|
|
if err != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
log.ErrorLog(ctx, "failed to expand volume %s: %v", s.VolID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-01-10 04:56:28 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
// As we completed clone, remove the intermediate snap
|
2022-02-15 12:11:09 +00:00
|
|
|
if err = snapClient.DeleteSnapshot(ctx); err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapshotID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
// CleanupSnapshotFromSubvolume removes the snapshot from the subvolume.
|
|
|
|
func (s *subVolumeClient) CleanupSnapshotFromSubvolume(
|
2022-06-01 10:17:19 +00:00
|
|
|
ctx context.Context, parentVol *SubVolume,
|
|
|
|
) error {
|
2020-08-07 07:06:45 +00:00
|
|
|
// snapshot name is same as clone name as we need a name which can be
|
|
|
|
// identified during PVC-PVC cloning.
|
2022-02-15 12:11:09 +00:00
|
|
|
snapShotID := s.VolID
|
2022-07-28 12:26:55 +00:00
|
|
|
snapClient := NewSnapshot(s.conn, snapShotID, s.clusterID, s.clusterName, s.enableMetadata, parentVol)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2023-10-18 08:15:53 +00:00
|
|
|
err := snapClient.DeleteSnapshot(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot %s %v", snapShotID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
// CreateSnapshotFromSubvolume creates a clone from subvolume snapshot.
|
|
|
|
func (s *subVolumeClient) CreateCloneFromSnapshot(
|
2022-06-01 10:17:19 +00:00
|
|
|
ctx context.Context, snap Snapshot,
|
|
|
|
) error {
|
2022-02-15 12:11:09 +00:00
|
|
|
snapID := snap.SnapshotID
|
2022-07-28 12:26:55 +00:00
|
|
|
snapClient := NewSnapshot(s.conn, snapID, s.clusterID, s.clusterName, s.enableMetadata, snap.SubVolume)
|
2022-02-15 12:11:09 +00:00
|
|
|
err := snapClient.CloneSnapshot(ctx, s.SubVolume)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
2021-09-16 13:47:57 +00:00
|
|
|
if !cerrors.IsCloneRetryError(err) {
|
2022-02-15 12:11:09 +00:00
|
|
|
if dErr := s.PurgeVolume(ctx, true); dErr != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to delete volume %s: %v", s.VolID, dErr)
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
2020-10-28 14:20:41 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
cloneState, err := s.GetCloneState(ctx)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to get clone state: %v", err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-10-28 14:20:41 +00:00
|
|
|
|
2022-06-21 11:49:13 +00:00
|
|
|
err = cloneState.ToError()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-07-05 13:10:53 +00:00
|
|
|
}
|
2022-01-10 04:56:28 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
err = s.ExpandVolume(ctx, s.Size)
|
2021-07-05 13:10:53 +00:00
|
|
|
if err != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
log.ErrorLog(ctx, "failed to expand volume %s with error: %v", s.VolID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-07-05 13:10:53 +00:00
|
|
|
return err
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-08-07 07:06:45 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
// GetCloneState returns the clone state of the subvolume.
|
|
|
|
func (s *subVolumeClient) GetCloneState(ctx context.Context) (cephFSCloneState, error) {
|
|
|
|
fsa, err := s.conn.GetFSAdmin()
|
2020-11-04 10:26:49 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(
|
2021-06-25 10:18:59 +00:00
|
|
|
ctx,
|
|
|
|
"could not get FSAdmin, can get clone status for volume %s with ID %s: %v",
|
2022-02-15 12:11:09 +00:00
|
|
|
s.FsName,
|
|
|
|
s.VolID,
|
2021-06-25 10:18:59 +00:00
|
|
|
err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
return CephFSCloneError, err
|
2020-10-28 14:20:41 +00:00
|
|
|
}
|
2020-08-07 07:06:45 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
cs, err := fsa.CloneStatus(s.FsName, s.SubvolumeGroup, s.VolID)
|
2020-08-07 07:06:45 +00:00
|
|
|
if err != nil {
|
2022-02-15 12:11:09 +00:00
|
|
|
log.ErrorLog(ctx, "could not get clone state for volume %s with ID %s: %v", s.FsName, s.VolID, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2022-02-15 12:11:09 +00:00
|
|
|
return CephFSCloneError, err
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|
2020-11-04 10:26:49 +00:00
|
|
|
|
2022-06-21 11:49:13 +00:00
|
|
|
errno := ""
|
|
|
|
errStr := ""
|
|
|
|
if failure := cs.GetFailure(); failure != nil {
|
|
|
|
errno = failure.Errno
|
|
|
|
errStr = failure.ErrStr
|
|
|
|
}
|
|
|
|
|
|
|
|
state := cephFSCloneState{
|
|
|
|
state: cs.State,
|
|
|
|
errno: errno,
|
|
|
|
errorMsg: errStr,
|
|
|
|
}
|
|
|
|
|
|
|
|
return state, nil
|
2020-08-07 07:06:45 +00:00
|
|
|
}
|