2022-06-07 07:55:30 +00:00
|
|
|
/*
|
|
|
|
Copyright 2022 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package core
|
|
|
|
|
|
|
|
import (
|
2022-07-19 10:32:58 +00:00
|
|
|
"errors"
|
2022-06-07 07:55:30 +00:00
|
|
|
"fmt"
|
2022-07-19 10:32:58 +00:00
|
|
|
|
2023-11-20 11:31:39 +00:00
|
|
|
libcephfs "github.com/ceph/go-ceph/cephfs"
|
2022-07-19 10:32:58 +00:00
|
|
|
fsAdmin "github.com/ceph/go-ceph/cephfs/admin"
|
2022-06-07 07:55:30 +00:00
|
|
|
)
|
|
|
|
|
2022-07-19 10:32:58 +00:00
|
|
|
// ErrSubVolSnapMetadataNotSupported is returned when set/get/list/remove
|
|
|
|
// subvolume snapshot metadata options are not supported.
|
|
|
|
var ErrSubVolSnapMetadataNotSupported = errors.New("subvolume snapshot metadata operations are not supported")
|
|
|
|
|
|
|
|
func (s *snapshotClient) supportsSubVolSnapMetadata() bool {
|
2022-09-07 06:16:22 +00:00
|
|
|
newLocalClusterState(s.clusterID)
|
2022-07-19 10:32:58 +00:00
|
|
|
|
|
|
|
return clusterAdditionalInfo[s.clusterID].subVolSnapshotMetadataState != unsupported
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *snapshotClient) isUnsupportedSubVolSnapMetadata(err error) bool {
|
|
|
|
var invalid fsAdmin.NotImplementedError
|
2022-10-11 11:52:26 +00:00
|
|
|
if err != nil && errors.As(err, &invalid) {
|
2022-07-19 10:32:58 +00:00
|
|
|
// In case the error is other than invalid command return error to
|
|
|
|
// the caller.
|
|
|
|
clusterAdditionalInfo[s.clusterID].subVolSnapshotMetadataState = unsupported
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
clusterAdditionalInfo[s.clusterID].subVolSnapshotMetadataState = supported
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2022-06-07 07:55:30 +00:00
|
|
|
// setSnapshotMetadata sets custom metadata on the subvolume snapshot in a
|
|
|
|
// volume as a key-value pair.
|
|
|
|
func (s *snapshotClient) setSnapshotMetadata(key, value string) error {
|
2022-07-19 10:32:58 +00:00
|
|
|
if !s.supportsSubVolSnapMetadata() {
|
|
|
|
return ErrSubVolSnapMetadataNotSupported
|
|
|
|
}
|
2022-06-07 07:55:30 +00:00
|
|
|
fsa, err := s.conn.GetFSAdmin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-19 10:32:58 +00:00
|
|
|
err = fsa.SetSnapshotMetadata(s.FsName, s.SubvolumeGroup, s.VolID, s.SnapshotID, key, value)
|
|
|
|
if !s.isUnsupportedSubVolSnapMetadata(err) {
|
|
|
|
return ErrSubVolSnapMetadataNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2022-06-07 07:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// removeSnapshotMetadata removes custom metadata set on the subvolume
|
|
|
|
// snapshot in a volume using the metadata key.
|
|
|
|
func (s *snapshotClient) removeSnapshotMetadata(key string) error {
|
2022-07-19 10:32:58 +00:00
|
|
|
if !s.supportsSubVolSnapMetadata() {
|
|
|
|
return ErrSubVolSnapMetadataNotSupported
|
|
|
|
}
|
2022-06-07 07:55:30 +00:00
|
|
|
fsa, err := s.conn.GetFSAdmin()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-07-19 10:32:58 +00:00
|
|
|
err = fsa.RemoveSnapshotMetadata(s.FsName, s.SubvolumeGroup, s.VolID, s.SnapshotID, key)
|
|
|
|
if !s.isUnsupportedSubVolSnapMetadata(err) {
|
|
|
|
return ErrSubVolSnapMetadataNotSupported
|
|
|
|
}
|
|
|
|
|
|
|
|
return err
|
2022-06-07 07:55:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetAllSnapshotMetadata set all the metadata from arg parameters on
|
|
|
|
// subvolume snapshot.
|
|
|
|
func (s *snapshotClient) SetAllSnapshotMetadata(parameters map[string]string) error {
|
2022-07-28 12:26:55 +00:00
|
|
|
if !s.enableMetadata {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 07:55:30 +00:00
|
|
|
for k, v := range parameters {
|
|
|
|
err := s.setSnapshotMetadata(k, v)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set metadata key %q, value %q on subvolume snapshot %s %s in fs %s: %w",
|
|
|
|
k, v, s.SnapshotID, s.VolID, s.FsName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 08:44:52 +00:00
|
|
|
if s.clusterName != "" {
|
|
|
|
err := s.setSnapshotMetadata(clusterNameKey, s.clusterName)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to set metadata key %q, value %q on subvolume snapshot %s %s in fs %s: %w",
|
|
|
|
clusterNameKey, s.clusterName, s.SnapshotID, s.VolID, s.FsName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-07 07:55:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// UnsetAllSnapshotMetadata unset all the metadata from arg keys on subvolume
|
|
|
|
// snapshot.
|
|
|
|
func (s *snapshotClient) UnsetAllSnapshotMetadata(keys []string) error {
|
2022-07-28 12:26:55 +00:00
|
|
|
if !s.enableMetadata {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-06-07 07:55:30 +00:00
|
|
|
for _, key := range keys {
|
|
|
|
err := s.removeSnapshotMetadata(key)
|
2023-11-20 11:31:39 +00:00
|
|
|
if err != nil && !errors.Is(err, libcephfs.ErrNotExist) {
|
2022-06-07 07:55:30 +00:00
|
|
|
return fmt.Errorf("failed to unset metadata key %q on subvolume snapshot %s %s in fs %s: %w",
|
|
|
|
key, s.SnapshotID, s.VolID, s.FsName, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-28 08:44:52 +00:00
|
|
|
err := s.removeSnapshotMetadata(clusterNameKey)
|
2023-11-20 11:31:39 +00:00
|
|
|
if err != nil && !errors.Is(err, libcephfs.ErrNotExist) {
|
2022-07-28 08:44:52 +00:00
|
|
|
return fmt.Errorf("failed to unset metadata key %q on subvolume snapshot %s %s in fs %s: %w",
|
|
|
|
clusterNameKey, s.SnapshotID, s.VolID, s.FsName, err)
|
|
|
|
}
|
|
|
|
|
2022-06-07 07:55:30 +00:00
|
|
|
return nil
|
|
|
|
}
|