From a2ec69db363a62d6eb30a1037c29c53d71447882 Mon Sep 17 00:00:00 2001 From: Niels de Vos Date: Fri, 12 Jul 2024 18:49:19 +0200 Subject: [PATCH] rbd: let CloneImageByID check for rbd_clone4 at runtime Some versions of librbd provide the rbd_clone4 function, and others do not. Squid will have the function backported in the 1st update, the initial release of Squid does not have it. This makes checking for the function based on the named Ceph version impractical. With the new dlsym.LookupSymbol() function, it is now possible to check the availability of rbd_clone4 during runtime. If the symbol is not found ErrNotImplemented is returned, which can be used to detect the unavailability of the function. Signed-off-by: Niels de Vos --- rbd/clone_image_by_id.go | 53 +++++++++++++++++++++++++++++------ rbd/clone_image_by_id_test.go | 9 +++++- rbd/errors.go | 2 ++ 3 files changed, 55 insertions(+), 9 deletions(-) diff --git a/rbd/clone_image_by_id.go b/rbd/clone_image_by_id.go index a73c99b184..63cf552d9c 100644 --- a/rbd/clone_image_by_id.go +++ b/rbd/clone_image_by_id.go @@ -1,20 +1,46 @@ -//go:build !(nautilus || octopus || pacific || quincy || reef) && ceph_preview +//go:build ceph_preview package rbd -// #cgo LDFLAGS: -lrbd -// #include -// #include -// #include -// #include +/* +#cgo LDFLAGS: -lrbd +#include +#include +#include +#include + +// rbd_clone4_fn matches the rbd_clone4 function signature. +typedef int(*rbd_clone4_fn)(rados_ioctx_t p_ioctx, const char *p_name, + uint64_t p_snap_id, rados_ioctx_t c_ioctx, + const char *c_name, rbd_image_options_t c_opts); + +// rbd_clone4_dlsym take *fn as rbd_clone4_fn and calls the dynamically loaded +// rbd_clone4 function passed as 1st argument. +static inline int rbd_clone4_dlsym(void *fn, rados_ioctx_t p_ioctx, + const char *p_name, uint64_t p_snap_id, + rados_ioctx_t c_ioctx, const char *c_name, + rbd_image_options_t c_opts) { + // cast function pointer fn to rbd_clone4 and call the function + return ((rbd_clone4_fn) fn)(p_ioctx, p_name, p_snap_id, c_ioctx, c_name, c_opts); +} +*/ import "C" import ( + "fmt" + "sync" "unsafe" + "github.com/ceph/go-ceph/internal/dlsym" "github.com/ceph/go-ceph/rados" ) +var ( + rbdClone4Once sync.Once + rbdClone4 unsafe.Pointer + rbdClone4Err error +) + // CloneImageByID creates a clone of the image from a snapshot with the given // ID in the provided io-context with the given name and image options. // @@ -25,22 +51,33 @@ import ( // const char *c_name, rbd_image_options_t c_opts); func CloneImageByID(ioctx *rados.IOContext, parentName string, snapID uint64, destctx *rados.IOContext, name string, rio *ImageOptions) error { - if rio == nil { return rbdError(C.EINVAL) } + rbdClone4Once.Do(func() { + rbdClone4, rbdClone4Err = dlsym.LookupSymbol("rbd_clone4") + }) + + if rbdClone4Err != nil { + return fmt.Errorf("%w: %w", ErrNotImplemented, rbdClone4Err) + } + cParentName := C.CString(parentName) defer C.free(unsafe.Pointer(cParentName)) cCloneName := C.CString(name) defer C.free(unsafe.Pointer(cCloneName)) - ret := C.rbd_clone4( + // call rbd_clone4_dlsym with the function pointer to rbd_clone4 as 1st + // argument + ret := C.rbd_clone4_dlsym( + rbdClone4, cephIoctx(ioctx), cParentName, C.uint64_t(snapID), cephIoctx(destctx), cCloneName, C.rbd_image_options_t(rio.options)) + return getError(ret) } diff --git a/rbd/clone_image_by_id_test.go b/rbd/clone_image_by_id_test.go index 96c130d120..9a50b71c7e 100644 --- a/rbd/clone_image_by_id_test.go +++ b/rbd/clone_image_by_id_test.go @@ -1,8 +1,9 @@ -//go:build !(nautilus || octopus || pacific || quincy || reef) && ceph_preview +//go:build ceph_preview package rbd import ( + "errors" "testing" "github.com/stretchr/testify/assert" @@ -91,6 +92,9 @@ func TestCloneImageByID(t *testing.T) { // Create a clone of the image using the snapshot. err = CloneImageByID(ioctx, name1, snapID, ioctx, cloneName, optionsClone) + if errors.Is(err, ErrNotImplemented) { + t.Skipf("CloneImageByID is not supported: %v", err) + } assert.NoError(t, err) defer func() { assert.NoError(t, RemoveImage(ioctx, cloneName)) }() @@ -135,6 +139,9 @@ func TestCloneImageByID(t *testing.T) { // Create a clone of the image using the snapshot. err = CloneImageByID(ioctx, name1, snapID, ioctx, cloneName, optionsClone) + if errors.Is(err, ErrNotImplemented) { + t.Skipf("CloneImageByID is not supported: %v", err) + } assert.NoError(t, err) defer func() { assert.NoError(t, RemoveImage(ioctx, cloneName)) }() diff --git a/rbd/errors.go b/rbd/errors.go index 34693333f5..4d98cce7c4 100644 --- a/rbd/errors.go +++ b/rbd/errors.go @@ -75,6 +75,8 @@ var ( const ( // ErrNotExist indicates a non-specific missing resource. ErrNotExist = rbdError(-C.ENOENT) + // ErrNotImplemented indicates a function is not implemented in by librbd. + ErrNotImplemented = rbdError(-C.ENOSYS) ) // Private errors: