2020-02-25 16:36:22 +00:00
|
|
|
package rbd
|
|
|
|
|
|
|
|
// #cgo LDFLAGS: -lrbd
|
|
|
|
// #include <stdlib.h>
|
|
|
|
// #include <rbd/librbd.h>
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"unsafe"
|
2020-09-02 14:02:17 +00:00
|
|
|
|
|
|
|
ts "github.com/ceph/go-ceph/internal/timespec"
|
2020-02-25 16:36:22 +00:00
|
|
|
)
|
|
|
|
|
2020-02-25 17:20:58 +00:00
|
|
|
// Snapshot represents a snapshot on a particular rbd image.
|
2020-02-25 16:36:22 +00:00
|
|
|
type Snapshot struct {
|
|
|
|
image *Image
|
|
|
|
name string
|
|
|
|
}
|
|
|
|
|
2020-02-25 17:20:58 +00:00
|
|
|
// CreateSnapshot returns a new Snapshot objects after creating
|
|
|
|
// a snapshot of the rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-02-09 17:23:11 +00:00
|
|
|
//
|
|
|
|
// int rbd_snap_create(rbd_image_t image, const char *snapname);
|
2020-02-25 16:36:22 +00:00
|
|
|
func (image *Image) CreateSnapshot(snapname string) (*Snapshot, error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
cSnapName := C.CString(snapname)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
2020-02-25 16:36:22 +00:00
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
ret := C.rbd_snap_create(image.image, cSnapName)
|
2020-02-25 16:36:22 +00:00
|
|
|
if ret < 0 {
|
2024-10-02 13:49:54 +00:00
|
|
|
return nil, getError(ret)
|
2020-02-25 16:36:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Snapshot{
|
|
|
|
image: image,
|
|
|
|
name: snapname,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// validate the attributes listed in the req bitmask, and return an error in
|
|
|
|
// case the attribute is not set
|
|
|
|
// Calls snapshot.image.validate(req) to validate the image attributes.
|
|
|
|
func (snapshot *Snapshot) validate(req uint32) error {
|
|
|
|
if hasBit(req, snapshotNeedsName) && snapshot.name == "" {
|
|
|
|
return ErrSnapshotNoName
|
|
|
|
} else if snapshot.image != nil {
|
|
|
|
return snapshot.image.validate(req)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-02-25 17:20:58 +00:00
|
|
|
// GetSnapshot constructs a snapshot object for the image given
|
|
|
|
// the snap name. It does not validate that this snapshot exists.
|
2020-02-25 16:36:22 +00:00
|
|
|
func (image *Image) GetSnapshot(snapname string) *Snapshot {
|
|
|
|
return &Snapshot{
|
|
|
|
image: image,
|
|
|
|
name: snapname,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-25 17:20:58 +00:00
|
|
|
// Remove the snapshot from the connected rbd image.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-02-09 17:23:11 +00:00
|
|
|
//
|
|
|
|
// int rbd_snap_remove(rbd_image_t image, const char *snapname);
|
2020-02-25 16:36:22 +00:00
|
|
|
func (snapshot *Snapshot) Remove() error {
|
|
|
|
if err := snapshot.validate(snapshotNeedsName | imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
cSnapName := C.CString(snapshot.name)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
2020-02-25 16:36:22 +00:00
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
return getError(C.rbd_snap_remove(snapshot.image.image, cSnapName))
|
2020-02-25 16:36:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 17:20:58 +00:00
|
|
|
// Rollback the image to the snapshot.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-02-09 17:23:11 +00:00
|
|
|
//
|
|
|
|
// int rbd_snap_rollback(rbd_image_t image, const char *snapname);
|
2020-02-25 16:36:22 +00:00
|
|
|
func (snapshot *Snapshot) Rollback() error {
|
|
|
|
if err := snapshot.validate(snapshotNeedsName | imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
cSnapName := C.CString(snapshot.name)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
2020-02-25 16:36:22 +00:00
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
return getError(C.rbd_snap_rollback(snapshot.image.image, cSnapName))
|
2020-02-25 16:36:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 17:20:58 +00:00
|
|
|
// Protect a snapshot from unwanted deletion.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-02-09 17:23:11 +00:00
|
|
|
//
|
|
|
|
// int rbd_snap_protect(rbd_image_t image, const char *snap_name);
|
2020-02-25 16:36:22 +00:00
|
|
|
func (snapshot *Snapshot) Protect() error {
|
|
|
|
if err := snapshot.validate(snapshotNeedsName | imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
cSnapName := C.CString(snapshot.name)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
2020-02-25 16:36:22 +00:00
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
return getError(C.rbd_snap_protect(snapshot.image.image, cSnapName))
|
2020-02-25 16:36:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 17:20:58 +00:00
|
|
|
// Unprotect stops protecting the snapshot.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-02-09 17:23:11 +00:00
|
|
|
//
|
|
|
|
// int rbd_snap_unprotect(rbd_image_t image, const char *snap_name);
|
2020-02-25 16:36:22 +00:00
|
|
|
func (snapshot *Snapshot) Unprotect() error {
|
|
|
|
if err := snapshot.validate(snapshotNeedsName | imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
cSnapName := C.CString(snapshot.name)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
2020-02-25 16:36:22 +00:00
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
return getError(C.rbd_snap_unprotect(snapshot.image.image, cSnapName))
|
2020-02-25 16:36:22 +00:00
|
|
|
}
|
|
|
|
|
2020-02-25 17:20:58 +00:00
|
|
|
// IsProtected returns true if the snapshot is currently protected.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-02-09 17:23:11 +00:00
|
|
|
//
|
|
|
|
// int rbd_snap_is_protected(rbd_image_t image, const char *snap_name,
|
|
|
|
// int *is_protected);
|
2020-02-25 16:36:22 +00:00
|
|
|
func (snapshot *Snapshot) IsProtected() (bool, error) {
|
|
|
|
if err := snapshot.validate(snapshotNeedsName | imageIsOpen); err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
|
2021-07-19 19:05:37 +00:00
|
|
|
var cIsProtected C.int
|
2020-02-25 16:36:22 +00:00
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
cSnapName := C.CString(snapshot.name)
|
|
|
|
defer C.free(unsafe.Pointer(cSnapName))
|
2020-02-25 16:36:22 +00:00
|
|
|
|
2021-07-19 19:03:47 +00:00
|
|
|
ret := C.rbd_snap_is_protected(snapshot.image.image, cSnapName,
|
2021-07-19 19:05:37 +00:00
|
|
|
&cIsProtected)
|
2020-02-25 16:36:22 +00:00
|
|
|
if ret < 0 {
|
2024-10-02 13:49:54 +00:00
|
|
|
return false, getError(ret)
|
2020-02-25 16:36:22 +00:00
|
|
|
}
|
|
|
|
|
2021-07-19 19:05:37 +00:00
|
|
|
return cIsProtected != 0, nil
|
2020-02-25 16:36:22 +00:00
|
|
|
}
|
|
|
|
|
2021-10-01 15:35:37 +00:00
|
|
|
// Set updates the rbd image (not the Snapshot) such that the snapshot is the
|
|
|
|
// source of readable data.
|
|
|
|
//
|
|
|
|
// Deprecated: use the SetSnapshot method of the Image type instead
|
2020-02-25 17:20:58 +00:00
|
|
|
//
|
|
|
|
// Implements:
|
2023-02-09 17:23:11 +00:00
|
|
|
//
|
|
|
|
// int rbd_snap_set(rbd_image_t image, const char *snapname);
|
2020-02-25 16:36:22 +00:00
|
|
|
func (snapshot *Snapshot) Set() error {
|
|
|
|
if err := snapshot.validate(snapshotNeedsName | imageIsOpen); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-10 05:34:52 +00:00
|
|
|
return snapshot.image.SetSnapshot(snapshot.name)
|
2020-02-25 16:36:22 +00:00
|
|
|
}
|
2020-09-02 14:02:17 +00:00
|
|
|
|
|
|
|
// GetSnapTimestamp returns the timestamp of a snapshot for an image.
|
|
|
|
// For a non-existing snap ID, GetSnapTimestamp() may trigger an assertion
|
|
|
|
// and crash in the ceph library.
|
|
|
|
// Check https://tracker.ceph.com/issues/47287 for details.
|
|
|
|
//
|
|
|
|
// Implements:
|
2023-02-09 17:23:11 +00:00
|
|
|
//
|
|
|
|
// int rbd_snap_get_timestamp(rbd_image_t image, uint64_t snap_id, struct timespec *timestamp)
|
2020-09-02 14:02:17 +00:00
|
|
|
func (image *Image) GetSnapTimestamp(snapID uint64) (Timespec, error) {
|
|
|
|
if err := image.validate(imageIsOpen); err != nil {
|
|
|
|
return Timespec{}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var cts C.struct_timespec
|
|
|
|
|
|
|
|
ret := C.rbd_snap_get_timestamp(image.image, C.uint64_t(snapID), &cts)
|
|
|
|
if ret < 0 {
|
|
|
|
return Timespec{}, getError(ret)
|
|
|
|
}
|
|
|
|
|
|
|
|
return Timespec(ts.CStructToTimespec(ts.CTimespecPtr(&cts))), nil
|
|
|
|
}
|