2020-06-24 07:43:24 +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
|
|
|
|
|
2023-06-02 09:49:22 +00:00
|
|
|
http://www.apache.org/licenses/LICENSE-2.0
|
2020-06-24 07:43:24 +00:00
|
|
|
|
|
|
|
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 rbd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2020-07-06 17:51:04 +00:00
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-06-24 07:43:24 +00:00
|
|
|
|
2024-09-12 13:02:55 +00:00
|
|
|
librbd "github.com/ceph/go-ceph/rbd"
|
2024-08-05 16:27:15 +00:00
|
|
|
"github.com/container-storage-interface/spec/lib/go/csi"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
|
2024-09-12 13:02:55 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/rbd/types"
|
2020-06-24 07:43:24 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util"
|
2021-08-24 15:03:25 +00:00
|
|
|
"github.com/ceph/ceph-csi/internal/util/log"
|
2020-06-24 07:43:24 +00:00
|
|
|
)
|
|
|
|
|
2021-06-25 11:52:34 +00:00
|
|
|
func createRBDClone(
|
|
|
|
ctx context.Context,
|
|
|
|
parentVol, cloneRbdVol *rbdVolume,
|
2022-06-01 10:17:19 +00:00
|
|
|
snap *rbdSnapshot,
|
|
|
|
) error {
|
2020-06-24 07:43:24 +00:00
|
|
|
// create snapshot
|
|
|
|
err := parentVol.createSnapshot(ctx, snap)
|
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to create snapshot %s: %v", snap, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-07-07 12:14:19 +00:00
|
|
|
snap.RbdImageName = parentVol.RbdImageName
|
2020-06-24 07:43:24 +00:00
|
|
|
// create clone image and delete snapshot
|
2021-03-18 11:23:08 +00:00
|
|
|
err = cloneRbdVol.cloneRbdImageFromSnapshot(ctx, snap, parentVol)
|
2020-06-24 07:43:24 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(
|
2021-06-25 11:52:34 +00:00
|
|
|
ctx,
|
|
|
|
"failed to clone rbd image %s from snapshot %s: %v",
|
|
|
|
cloneRbdVol.RbdImageName,
|
|
|
|
snap.RbdSnapName,
|
|
|
|
err)
|
|
|
|
err = fmt.Errorf(
|
|
|
|
"failed to clone rbd image %s from snapshot %s: %w",
|
|
|
|
cloneRbdVol.RbdImageName,
|
|
|
|
snap.RbdSnapName,
|
|
|
|
err)
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
|
|
|
errSnap := parentVol.deleteSnapshot(ctx, snap)
|
|
|
|
if errSnap != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot: %v", errSnap)
|
2024-07-05 06:46:09 +00:00
|
|
|
delErr := cloneRbdVol.Delete(ctx)
|
2020-06-24 07:43:24 +00:00
|
|
|
if delErr != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete rbd image: %s with error: %v", cloneRbdVol, delErr)
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-21 13:56:48 +00:00
|
|
|
// cleanUpSnapshot removes the RBD-snapshot (rbdSnap) from the RBD-image
|
|
|
|
// (parentVol) and deletes the RBD-image rbdVol.
|
2021-06-25 11:52:34 +00:00
|
|
|
func cleanUpSnapshot(
|
|
|
|
ctx context.Context,
|
|
|
|
parentVol *rbdVolume,
|
|
|
|
rbdSnap *rbdSnapshot,
|
2022-06-01 10:17:19 +00:00
|
|
|
rbdVol *rbdVolume,
|
|
|
|
) error {
|
2021-06-25 11:52:34 +00:00
|
|
|
err := parentVol.deleteSnapshot(ctx, rbdSnap)
|
|
|
|
if err != nil {
|
2024-12-16 10:33:46 +00:00
|
|
|
if !errors.Is(err, ErrImageNotFound) && !errors.Is(err, ErrSnapNotFound) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete snapshot %q: %v", rbdSnap, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-25 11:52:34 +00:00
|
|
|
return err
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-21 13:56:48 +00:00
|
|
|
|
|
|
|
if rbdVol != nil {
|
2024-07-05 06:46:09 +00:00
|
|
|
err := rbdVol.Delete(ctx)
|
2021-06-21 13:56:48 +00:00
|
|
|
if err != nil {
|
|
|
|
if !errors.Is(err, ErrImageNotFound) {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to delete rbd image %q with error: %v", rbdVol, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2021-06-21 13:56:48 +00:00
|
|
|
return err
|
|
|
|
}
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
|
|
|
}
|
2021-06-21 13:56:48 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-08-08 09:03:29 +00:00
|
|
|
func (rv *rbdVolume) toSnapshot() *rbdSnapshot {
|
|
|
|
return &rbdSnapshot{
|
|
|
|
rbdImage: rbdImage{
|
|
|
|
ClusterID: rv.ClusterID,
|
|
|
|
VolID: rv.VolID,
|
2024-08-21 07:50:09 +00:00
|
|
|
VolSize: rv.VolSize,
|
2024-08-08 09:03:29 +00:00
|
|
|
Monitors: rv.Monitors,
|
|
|
|
Pool: rv.Pool,
|
|
|
|
JournalPool: rv.JournalPool,
|
|
|
|
RadosNamespace: rv.RadosNamespace,
|
|
|
|
RbdImageName: rv.RbdImageName,
|
|
|
|
ImageID: rv.ImageID,
|
|
|
|
CreatedAt: rv.CreatedAt,
|
|
|
|
// copyEncryptionConfig cannot be used here because the volume and the
|
|
|
|
// snapshot will have the same volumeID which cases the panic in
|
|
|
|
// copyEncryptionConfig function.
|
|
|
|
blockEncryption: rv.blockEncryption,
|
|
|
|
fileEncryption: rv.fileEncryption,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-26 08:51:19 +00:00
|
|
|
func (rbdSnap *rbdSnapshot) toVolume() *rbdVolume {
|
|
|
|
return &rbdVolume{
|
|
|
|
rbdImage: rbdImage{
|
|
|
|
ClusterID: rbdSnap.ClusterID,
|
|
|
|
VolID: rbdSnap.VolID,
|
|
|
|
Monitors: rbdSnap.Monitors,
|
|
|
|
Pool: rbdSnap.Pool,
|
|
|
|
JournalPool: rbdSnap.JournalPool,
|
|
|
|
RadosNamespace: rbdSnap.RadosNamespace,
|
|
|
|
RbdImageName: rbdSnap.RbdSnapName,
|
|
|
|
ImageID: rbdSnap.ImageID,
|
2024-08-08 09:03:29 +00:00
|
|
|
CreatedAt: rbdSnap.CreatedAt,
|
2024-03-26 08:51:19 +00:00
|
|
|
// copyEncryptionConfig cannot be used here because the volume and the
|
|
|
|
// snapshot will have the same volumeID which cases the panic in
|
|
|
|
// copyEncryptionConfig function.
|
|
|
|
blockEncryption: rbdSnap.blockEncryption,
|
|
|
|
fileEncryption: rbdSnap.fileEncryption,
|
|
|
|
},
|
|
|
|
}
|
2020-06-24 07:43:24 +00:00
|
|
|
}
|
|
|
|
|
2024-08-05 16:27:15 +00:00
|
|
|
func (rbdSnap *rbdSnapshot) ToCSI(ctx context.Context) (*csi.Snapshot, error) {
|
2024-10-01 08:27:03 +00:00
|
|
|
created, err := rbdSnap.GetCreationTime(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-08-05 16:27:15 +00:00
|
|
|
return &csi.Snapshot{
|
2024-10-25 15:24:50 +00:00
|
|
|
SizeBytes: rbdSnap.VolSize,
|
|
|
|
SnapshotId: rbdSnap.VolID,
|
|
|
|
SourceVolumeId: rbdSnap.SourceVolumeID,
|
|
|
|
CreationTime: timestamppb.New(*created),
|
|
|
|
ReadyToUse: true,
|
|
|
|
GroupSnapshotId: rbdSnap.groupID,
|
2024-08-05 16:27:15 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2024-12-16 10:33:46 +00:00
|
|
|
// Delete removes the snapshot from the RBD image and then
|
|
|
|
// the RBD image itself. If the backing RBD snapshot and image is removed
|
|
|
|
// successfully, the reservation for the snapshot is removed from the journal.
|
|
|
|
//
|
|
|
|
// NOTE: As the function manipulates omaps, it should be called with a lock against the request name
|
|
|
|
// held, to prevent parallel operations from modifying the state of the omaps for this request name.
|
|
|
|
func (rbdSnap *rbdSnapshot) Delete(ctx context.Context) error {
|
|
|
|
rbdVol := rbdSnap.toVolume()
|
|
|
|
|
|
|
|
err := rbdVol.Connect(rbdSnap.conn.Creds)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer rbdVol.Destroy(ctx)
|
|
|
|
|
|
|
|
rbdVol.ImageID = rbdSnap.ImageID
|
|
|
|
// update parent name to delete the snapshot
|
|
|
|
rbdSnap.RbdImageName = rbdVol.RbdImageName
|
|
|
|
err = cleanUpSnapshot(ctx, rbdVol, rbdSnap, rbdVol)
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to cleanup image %s and snapshot %s: %v", rbdVol, rbdSnap, err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = undoSnapReservation(ctx, rbdSnap, rbdSnap.conn.Creds)
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to remove reservation for snapname (%s) with backing snap (%s) on image (%s) (%s)",
|
|
|
|
rbdSnap.RequestName, rbdSnap.RbdSnapName, rbdSnap.RbdImageName, err)
|
|
|
|
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-25 11:52:34 +00:00
|
|
|
func undoSnapshotCloning(
|
|
|
|
ctx context.Context,
|
|
|
|
parentVol *rbdVolume,
|
|
|
|
rbdSnap *rbdSnapshot,
|
|
|
|
cloneVol *rbdVolume,
|
2022-06-01 10:17:19 +00:00
|
|
|
cr *util.Credentials,
|
|
|
|
) error {
|
2021-11-10 09:38:32 +00:00
|
|
|
err := cleanUpSnapshot(ctx, parentVol, rbdSnap, cloneVol)
|
2020-06-24 07:43:24 +00:00
|
|
|
if err != nil {
|
2021-08-24 15:03:25 +00:00
|
|
|
log.ErrorLog(ctx, "failed to clean up %s or %s: %v", cloneVol, rbdSnap, err)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
err = undoSnapReservation(ctx, rbdSnap, cr)
|
2021-07-22 05:45:17 +00:00
|
|
|
|
2020-06-24 07:43:24 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-09-12 13:02:55 +00:00
|
|
|
|
|
|
|
// NewSnapshotByID creates a new rbdSnapshot from the rbdVolume.
|
|
|
|
//
|
|
|
|
// Parameters:
|
|
|
|
// - name of the new rbd-image backing the snapshot
|
|
|
|
// - id of the rbd-snapshot to clone
|
|
|
|
//
|
|
|
|
// FIXME: When resolving the Snapshot, the RbdImageName will be set to the name
|
|
|
|
// of the parent image. This is can cause issues when not accounting for that
|
|
|
|
// and the Snapshot is deleted; instead of deleting the snapshot image, the
|
|
|
|
// parent image is removed.
|
|
|
|
//
|
|
|
|
//nolint:gocyclo,cyclop // TODO: reduce complexity.
|
|
|
|
func (rv *rbdVolume) NewSnapshotByID(
|
|
|
|
ctx context.Context,
|
|
|
|
cr *util.Credentials,
|
|
|
|
name string,
|
|
|
|
id uint64,
|
|
|
|
) (types.Snapshot, error) {
|
|
|
|
snap := rv.toSnapshot()
|
|
|
|
snap.RequestName = name
|
|
|
|
|
|
|
|
srcVolID, err := rv.GetID(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
snap.SourceVolumeID = srcVolID
|
|
|
|
|
|
|
|
// reserveSnap sets snap.{RbdSnapName,ReservedID,VolID}
|
|
|
|
err = reserveSnap(ctx, snap, rv, cr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to create a reservation in the journal for snapshot image %q: %w", snap, err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
undoErr := undoSnapReservation(ctx, snap, cr)
|
|
|
|
if undoErr != nil {
|
|
|
|
log.WarningLog(ctx, "failed undoing reservation of snapshot %q: %v", name, undoErr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2024-10-01 17:06:50 +00:00
|
|
|
// A new snapshot image will be created, and needs to have a unique
|
|
|
|
// name.
|
|
|
|
// FIXME: the journal contains rv.RbdImageName as SourceName. When
|
|
|
|
// resolving the snapshot image, snap.RbdImageName will be set to the
|
|
|
|
// original RbdImageName/SourceName (incorrect). This is fixed-up in
|
|
|
|
// rbdManager.GetSnapshotByID(), this needs to be done cleaner.
|
2024-09-12 13:02:55 +00:00
|
|
|
snap.RbdImageName = snap.RbdSnapName
|
|
|
|
|
|
|
|
err = rv.Connect(cr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = rv.openIoctx()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-12-05 14:17:55 +00:00
|
|
|
// set the features for the clone image.
|
|
|
|
f := []string{librbd.FeatureNameLayering, librbd.FeatureNameDeepFlatten}
|
|
|
|
rv.ImageFeatureSet = librbd.FeatureSetFromNames(f)
|
|
|
|
|
2024-09-12 13:02:55 +00:00
|
|
|
options, err := rv.constructImageOptions(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
defer options.Destroy()
|
|
|
|
|
|
|
|
err = options.SetUint64(librbd.ImageOptionCloneFormat, 2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// indicator to remove the snapshot after a failure
|
|
|
|
removeSnap := true
|
|
|
|
var snapImage *librbd.Snapshot
|
|
|
|
|
|
|
|
log.DebugLog(ctx, "going to clone snapshot image %q from image %q with snapshot ID %d", snap, rv, id)
|
|
|
|
|
|
|
|
err = librbd.CloneImageByID(rv.ioctx, rv.RbdImageName, id, rv.ioctx, snap.RbdImageName, options)
|
2024-12-20 04:49:28 +00:00
|
|
|
if err != nil && !errors.Is(err, librbd.ErrExist) {
|
2024-09-12 13:02:55 +00:00
|
|
|
log.ErrorLog(ctx, "failed to clone snapshot %q with id %d: %v", snap, id, err)
|
|
|
|
|
|
|
|
return nil, fmt.Errorf("failed to clone %q with snapshot id %d as new image %q: %w", rv.RbdImageName, id, snap, err)
|
|
|
|
}
|
|
|
|
defer func() {
|
|
|
|
if !removeSnap {
|
|
|
|
// success, no need to remove the snapshot image
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if snapImage != nil {
|
|
|
|
err = snapImage.Remove()
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to remove snapshot of image %q after failure: %v", snap, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = librbd.RemoveImage(rv.ioctx, snap.RbdImageName)
|
|
|
|
if err != nil {
|
|
|
|
log.ErrorLog(ctx, "failed to remove snapshot image %q after failure: %v", snap, err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// update the snapshot image in the journal, after the image info is updated
|
|
|
|
j, err := snapJournal.Connect(snap.Monitors, snap.RadosNamespace, cr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("snapshot image %q failed to connect to journal: %w", snap, err)
|
|
|
|
}
|
|
|
|
defer j.Destroy()
|
|
|
|
|
|
|
|
err = snap.Connect(cr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to connect snapshot image %q: %w", snap, err)
|
|
|
|
}
|
|
|
|
defer snap.Destroy(ctx)
|
|
|
|
|
|
|
|
image, err := snap.open()
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to open snapshot image %q: %w", snap, err)
|
|
|
|
}
|
|
|
|
defer image.Close()
|
|
|
|
|
|
|
|
snapImage, err = image.CreateSnapshot(snap.RbdSnapName)
|
2024-12-20 04:49:28 +00:00
|
|
|
if err != nil && !errors.Is(err, librbd.ErrExist) {
|
2024-09-12 13:02:55 +00:00
|
|
|
return nil, fmt.Errorf("failed to create snapshot on image %q: %w", snap, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = snap.repairImageID(ctx, j, true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to repair image id for snapshot image %q: %w", snap, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// all ok, don't remove the snapshot image in a defer statement
|
|
|
|
removeSnap = false
|
|
|
|
|
|
|
|
return snap, nil
|
|
|
|
}
|
2024-10-25 15:24:50 +00:00
|
|
|
|
|
|
|
func (rbdSnap *rbdSnapshot) SetVolumeGroup(ctx context.Context, cr *util.Credentials, groupID string) error {
|
|
|
|
vi := util.CSIIdentifier{}
|
|
|
|
err := vi.DecomposeCSIID(rbdSnap.VolID)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
j, err := snapJournal.Connect(rbdSnap.Monitors, rbdSnap.RadosNamespace, cr)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("snapshot %q failed to connect to journal: %w", rbdSnap, err)
|
|
|
|
}
|
|
|
|
defer j.Destroy()
|
|
|
|
|
|
|
|
err = j.StoreGroupID(ctx, rbdSnap.Pool, vi.ObjectUUID, groupID)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set volume group ID for snapshot %q: %w", rbdSnap, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
rbdSnap.groupID = groupID
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|