rebase: update vendored go-ceph to v0.6

Closes: #1547
Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos
2020-10-13 17:22:31 +02:00
committed by mergify[bot]
parent 268c0f1965
commit 29c78f97c0
12 changed files with 286 additions and 40 deletions

View File

@ -7,18 +7,8 @@ package rbd
#include <stdlib.h>
#include <rbd/librbd.h>
typedef int (*diff_iterate_callback_t)(uint64_t, size_t, int, void *);
extern int diffIterateCallback(uint64_t, size_t, int, void *);
// cgo is having trouble converting the callback from the librbd header
// to a unsafe.Pointer. This shim exists solely to help it along.
static inline int wrap_rbd_diff_iterate2(
rbd_image_t image,
const char *fromsnapname,
uint64_t ofs, uint64_t len,
uint8_t include_parent, uint8_t whole_object,
uintptr_t index) {
return rbd_diff_iterate2(image, fromsnapname, ofs, len, include_parent, whole_object, diffIterateCallback, (void*)index);
}
*/
import "C"
@ -26,6 +16,7 @@ import (
"unsafe"
"github.com/ceph/go-ceph/internal/callbacks"
"github.com/ceph/go-ceph/internal/cutil"
)
var diffIterateCallbacks = callbacks.New()
@ -110,14 +101,15 @@ func (image *Image) DiffIterate(config DiffIterateConfig) error {
cbIndex := diffIterateCallbacks.Add(config)
defer diffIterateCallbacks.Remove(cbIndex)
ret := C.wrap_rbd_diff_iterate2(
ret := C.rbd_diff_iterate2(
image.image,
cSnapName,
C.uint64_t(config.Offset),
C.uint64_t(config.Length),
C.uint8_t(config.IncludeParent),
C.uint8_t(config.WholeObject),
C.uintptr_t(cbIndex))
C.diff_iterate_callback_t(C.diffIterateCallback),
cutil.VoidPtr(cbIndex))
return getError(ret)
}
@ -126,7 +118,7 @@ func (image *Image) DiffIterate(config DiffIterateConfig) error {
func diffIterateCallback(
offset C.uint64_t, length C.size_t, exists C.int, index unsafe.Pointer) C.int {
v := diffIterateCallbacks.Lookup(int(uintptr(index)))
v := diffIterateCallbacks.Lookup(uintptr(index))
config := v.(DiffIterateConfig)
return C.int(config.Callback(
uint64(offset), uint64(length), int(exists), config.Data))

View File

@ -101,3 +101,17 @@ func (image *Image) GetModifyTimestamp() (Timespec, error) {
return Timespec(ts.CStructToTimespec(ts.CTimespecPtr(&cts))), nil
}
// Sparsify makes an image sparse by deallocating runs of zeros.
// The sparseSize value will be used to find runs of zeros and must be
// a power of two no less than 4096 and no larger than the image size.
//
// Implements:
// int rbd_sparsify(rbd_image_t image, size_t sparse_size);
func (image *Image) Sparsify(sparseSize uint) error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
return getError(C.rbd_sparsify(image.image, C.size_t(sparseSize)))
}

View File

@ -7,6 +7,8 @@ import "C"
import (
"unsafe"
ts "github.com/ceph/go-ceph/internal/timespec"
)
// Snapshot represents a snapshot on a particular rbd image.
@ -160,3 +162,25 @@ func (snapshot *Snapshot) Set() error {
return getError(C.rbd_snap_set(snapshot.image.image, c_snapname))
}
// 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:
// int rbd_snap_get_timestamp(rbd_image_t image, uint64_t snap_id, struct timespec *timestamp)
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
}

View File

@ -65,6 +65,60 @@ func (image *Image) GetParentInfo(pool, name, snapname []byte) error {
return nil
}
// ImageSpec represents the image information.
type ImageSpec struct {
ImageName string
PoolName string
}
// SnapSpec represents the snapshot infomation.
type SnapSpec struct {
ID uint64
SnapName string
}
// ParentInfo represents the parent image and the parent snapshot information.
type ParentInfo struct {
Image ImageSpec
Snap SnapSpec
}
// GetParent looks for the parent of the image and returns the parent image
// information which includes the image name, the pool name and
// the snapshot information.
//
// Implements:
// int rbd_get_parent(rbd_image_t image, rbd_linked_image_spec_t *parent_image, rbd_snap_spec_t *parent_snap)
func (image *Image) GetParent() (*ParentInfo, error) {
if err := image.validate(imageIsOpen); err != nil {
return nil, err
}
parentImage := C.rbd_linked_image_spec_t{}
parentSnap := C.rbd_snap_spec_t{}
ret := C.rbd_get_parent(image.image, &parentImage, &parentSnap)
if ret != 0 {
return nil, getError(ret)
}
defer C.rbd_linked_image_spec_cleanup(&parentImage)
defer C.rbd_snap_spec_cleanup(&parentSnap)
imageSpec := ImageSpec{
ImageName: C.GoString(parentImage.image_name),
PoolName: C.GoString(parentImage.pool_name),
}
snapSpec := SnapSpec{
ID: uint64(parentSnap.id),
SnapName: C.GoString(parentSnap.name),
}
return &ParentInfo{
Image: imageSpec,
Snap: snapSpec,
}, nil
}
// ListChildren returns arrays with the pools and names of the images that are
// children of the given image. The index of the pools and images arrays can be
// used to link the two items together.
@ -104,3 +158,17 @@ func (image *Image) ListChildren() (pools []string, images []string, err error)
}
return pools, images, nil
}
// SetSnapByID updates the rbd image (not the Snapshot) such that the snapshot
// is the source of readable data.
//
// Implements:
// int rbd_snap_set_by_id(rbd_image_t image, uint64_t snap_id);
func (image *Image) SetSnapByID(snapID uint64) error {
if err := image.validate(imageIsOpen); err != nil {
return err
}
ret := C.rbd_snap_set_by_id(image.image, C.uint64_t(snapID))
return getError(ret)
}

66
vendor/github.com/ceph/go-ceph/rbd/snapshot_octopus.go generated vendored Normal file
View File

@ -0,0 +1,66 @@
// +build octopus
package rbd
// #cgo LDFLAGS: -lrbd
// #include <stdlib.h>
// #include <rbd/librbd.h>
import "C"
import (
"unsafe"
"github.com/ceph/go-ceph/internal/retry"
)
// GetSnapID returns the snapshot ID for the given snapshot name.
//
// Implements:
// int rbd_snap_get_id(rbd_image_t image, const char *snapname, uint64_t *snap_id)
func (image *Image) GetSnapID(snapName string) (uint64, error) {
var snapID C.uint64_t
if err := image.validate(imageIsOpen); err != nil {
return uint64(snapID), err
}
if snapName == "" {
return uint64(snapID), ErrSnapshotNoName
}
cSnapName := C.CString(snapName)
defer C.free(unsafe.Pointer(cSnapName))
ret := C.rbd_snap_get_id(image.image, cSnapName, &snapID)
return uint64(snapID), getError(ret)
}
// GetSnapByID returns the snapshot name for the given snapshot ID.
//
// Implements:
// int rbd_snap_get_name(rbd_image_t image, uint64_t snap_id, char *snapname, size_t *name_len)
func (image *Image) GetSnapByID(snapID uint64) (string, error) {
if err := image.validate(imageIsOpen); err != nil {
return "", err
}
var (
buf []byte
err error
)
// range from 1k to 64KiB
retry.WithSizes(1024, 1<<16, func(len int) retry.Hint {
cLen := C.size_t(len)
buf = make([]byte, cLen)
ret := C.rbd_snap_get_name(
image.image,
(C.uint64_t)(snapID),
(*C.char)(unsafe.Pointer(&buf[0])),
&cLen)
err = getError(ret)
return retry.Size(int(cLen)).If(err == errRange)
})
if err != nil {
return "", err
}
return C.GoString((*C.char)(unsafe.Pointer(&buf[0]))), nil
}

View File

@ -9,18 +9,6 @@ package rbd
#include <rbd/librbd.h>
extern void imageWatchCallback(void *);
// cgo has trouble converting the types of the callback and data arg defined in
// librbd header. It wants the callback function to be a byte pointer and
// the arg to be a pointer, which is pretty much the opposite of what we
// actually want. This shim exists to help coerce the auto-type-conversion
// to do the right thing for us.
static inline int wrap_rbd_update_watch(
rbd_image_t image,
uint64_t *handle,
uintptr_t index) {
return rbd_update_watch(image, handle, imageWatchCallback, (void*)index);
}
*/
import "C"
@ -28,6 +16,7 @@ import (
"unsafe"
"github.com/ceph/go-ceph/internal/callbacks"
"github.com/ceph/go-ceph/internal/cutil"
"github.com/ceph/go-ceph/internal/retry"
)
@ -95,7 +84,7 @@ type Watch struct {
image *Image
wcc watchCallbackCtx
handle C.uint64_t
cbIndex int
cbIndex uintptr
}
// UpdateWatch updates the image object to watch metadata changes to the
@ -118,10 +107,11 @@ func (image *Image) UpdateWatch(cb WatchCallback, data interface{}) (*Watch, err
cbIndex: watchCallbacks.Add(wcc),
}
ret := C.wrap_rbd_update_watch(
ret := C.rbd_update_watch(
image.image,
&w.handle,
C.uintptr_t(w.cbIndex))
C.rbd_update_callback_t(C.imageWatchCallback),
cutil.VoidPtr(w.cbIndex))
if ret != 0 {
return nil, getError(ret)
}
@ -146,7 +136,7 @@ func (w *Watch) Unwatch() error {
//export imageWatchCallback
func imageWatchCallback(index unsafe.Pointer) {
v := watchCallbacks.Lookup(int(uintptr(index)))
v := watchCallbacks.Lookup(uintptr(index))
wcc := v.(watchCallbackCtx)
wcc.callback(wcc.data)
}