Skip to content

Commit

Permalink
rbd: replace rbdError by cephError
Browse files Browse the repository at this point in the history
Signed-off-by: Niels de Vos <[email protected]>
  • Loading branch information
nixpanic authored and mergify[bot] committed Oct 14, 2024
1 parent 7154dac commit 3dbd875
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 65 deletions.
2 changes: 1 addition & 1 deletion rbd/clone_image_by_id.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ var (
func CloneImageByID(ioctx *rados.IOContext, parentName string, snapID uint64,
destctx *rados.IOContext, name string, rio *ImageOptions) error {
if rio == nil {
return rbdError(C.EINVAL)
return getError(C.EINVAL)
}

rbdClone4Once.Do(func() {
Expand Down
2 changes: 1 addition & 1 deletion rbd/diff_iterate.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func (image *Image) DiffIterate(config DiffIterateConfig) error {
return err
}
if config.Callback == nil {
return rbdError(C.EINVAL)
return getError(C.EINVAL)
}

var cSnapName *C.char
Expand Down
31 changes: 8 additions & 23 deletions rbd/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,27 +7,17 @@ import "C"

import (
"errors"
"fmt"

"github.com/ceph/go-ceph/internal/errutil"
)

// rbdError represents an error condition returned from the librbd APIs.
type rbdError int

func (e rbdError) Error() string {
return errutil.FormatErrorCode("rbd", int(e))
}

func (e rbdError) ErrorCode() int {
return int(e)
}

func getError(err C.int) error {
if err != 0 {
if err == -C.ENOENT {
return ErrNotFound
}
return rbdError(err)
return errutil.GetError("rbd", int(err))
}
return nil
}
Expand Down Expand Up @@ -60,7 +50,7 @@ var (
ErrImageIsOpen = errors.New("RBD image is open")
// ErrNotFound may be returned from an api call when the requested item is
// missing.
ErrNotFound = errors.New("RBD image not found")
ErrNotFound = fmt.Errorf("RBD image not found: %w", errutil.GetError("rbd", -C.ENOENT))
// ErrNoNamespaceName maye be returned if an api call requires a namespace
// name and it is not provided.
ErrNoNamespaceName = errors.New("Namespace value is missing")
Expand All @@ -69,20 +59,15 @@ var (
RbdErrorImageNotOpen = ErrImageNotOpen
RbdErrorNotFound = ErrNotFound
// revive:enable:exported
)

// Public general error
const (
// ErrExist indicates a non-specific already existing resource.
ErrExist = rbdError(-C.EEXIST)
ErrExist = getError(-C.EEXIST)
// ErrNotExist indicates a non-specific missing resource.
ErrNotExist = rbdError(-C.ENOENT)
ErrNotExist = getError(-C.ENOENT)
// ErrNotImplemented indicates a function is not implemented in by librbd.
ErrNotImplemented = rbdError(-C.ENOSYS)
)
ErrNotImplemented = getError(-C.ENOSYS)

// Private errors:
// Private errors:

const (
errRange = rbdError(-C.ERANGE)
errRange = getError(-C.ERANGE)
)
2 changes: 1 addition & 1 deletion rbd/features.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (image *Image) GetFeatures() (features uint64, err error) {
}

if ret := C.rbd_get_features(image.image, (*C.uint64_t)(&features)); ret < 0 {
return 0, rbdError(ret)
return 0, getError(ret)
}

return features, nil
Expand Down
2 changes: 1 addition & 1 deletion rbd/group_snap.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ func GroupSnapRollbackWithProgress(
cb GroupSnapRollbackCallback, data interface{}) error {
// the provided callback must be a real function
if cb == nil {
return rbdError(C.EINVAL)
return getError(C.EINVAL)
}

cGroupName := C.CString(group)
Expand Down
4 changes: 2 additions & 2 deletions rbd/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func (image *Image) SetMetadata(key string, value string) error {

ret := C.rbd_metadata_set(image.image, cKey, cValue)
if ret < 0 {
return rbdError(ret)
return getError(ret)
}

return nil
Expand All @@ -83,7 +83,7 @@ func (image *Image) RemoveMetadata(key string) error {

ret := C.rbd_metadata_remove(image.image, cKey)
if ret < 0 {
return rbdError(ret)
return getError(ret)
}

return nil
Expand Down
50 changes: 25 additions & 25 deletions rbd/rbd.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func Create(ioctx *rados.IOContext, name string, size uint64, order int,
}

if ret < 0 {
return nil, rbdError(ret)
return nil, getError(ret)
}

return &Image{
Expand All @@ -205,7 +205,7 @@ func Create2(ioctx *rados.IOContext, name string, size uint64, features uint64,
ret = C.rbd_create2(cephIoctx(ioctx), cName,
C.uint64_t(size), C.uint64_t(features), &cOrder)
if ret < 0 {
return nil, rbdError(ret)
return nil, getError(ret)
}

return &Image{
Expand Down Expand Up @@ -235,7 +235,7 @@ func Create3(ioctx *rados.IOContext, name string, size uint64, features uint64,
C.uint64_t(size), C.uint64_t(features), &cOrder,
C.uint64_t(stripeUnit), C.uint64_t(stripeCount))
if ret < 0 {
return nil, rbdError(ret)
return nil, getError(ret)
}

return &Image{
Expand Down Expand Up @@ -274,7 +274,7 @@ func (image *Image) Clone(snapname string, cIoctx *rados.IOContext, cName string
C.uint64_t(features),
&cOrder)
if ret < 0 {
return nil, rbdError(ret)
return nil, getError(ret)
}

return &Image{
Expand Down Expand Up @@ -325,9 +325,9 @@ func (image *Image) Rename(destname string) error {
defer C.free(unsafe.Pointer(cSrcName))
defer C.free(unsafe.Pointer(cDestName))

err := rbdError(C.rbd_rename(cephIoctx(image.ioctx),
err := getError(C.rbd_rename(cephIoctx(image.ioctx),
cSrcName, cDestName))
if err == 0 {
if err == nil {
image.name = destname
return nil
}
Expand Down Expand Up @@ -385,7 +385,7 @@ func (image *Image) Close() error {
}

if ret := C.rbd_close(image.image); ret != 0 {
return rbdError(ret)
return getError(ret)
}

image.image = nil
Expand Down Expand Up @@ -418,7 +418,7 @@ func (image *Image) Stat() (info *ImageInfo, err error) {
var cStat C.rbd_image_info_t

if ret := C.rbd_stat(image.image, &cStat, C.size_t(unsafe.Sizeof(info))); ret < 0 {
return info, rbdError(ret)
return info, getError(ret)
}

return &ImageInfo{
Expand All @@ -443,7 +443,7 @@ func (image *Image) IsOldFormat() (bool, error) {
ret := C.rbd_get_old_format(image.image,
&cOldFormat)
if ret < 0 {
return false, rbdError(ret)
return false, getError(ret)
}

return cOldFormat != 0, nil
Expand All @@ -460,7 +460,7 @@ func (image *Image) GetSize() (size uint64, err error) {
}

if ret := C.rbd_get_size(image.image, (*C.uint64_t)(&size)); ret < 0 {
return 0, rbdError(ret)
return 0, getError(ret)
}

return size, nil
Expand All @@ -478,7 +478,7 @@ func (image *Image) GetStripeUnit() (uint64, error) {

var stripeUnit uint64
if ret := C.rbd_get_stripe_unit(image.image, (*C.uint64_t)(&stripeUnit)); ret < 0 {
return 0, rbdError(ret)
return 0, getError(ret)
}

return stripeUnit, nil
Expand All @@ -496,7 +496,7 @@ func (image *Image) GetStripeCount() (uint64, error) {

var stripeCount uint64
if ret := C.rbd_get_stripe_count(image.image, (*C.uint64_t)(&stripeCount)); ret < 0 {
return 0, rbdError(ret)
return 0, getError(ret)
}

return stripeCount, nil
Expand All @@ -514,7 +514,7 @@ func (image *Image) GetOverlap() (overlap uint64, err error) {
}

if ret := C.rbd_get_overlap(image.image, (*C.uint64_t)(&overlap)); ret < 0 {
return overlap, rbdError(ret)
return overlap, getError(ret)
}

return overlap, nil
Expand Down Expand Up @@ -573,7 +573,7 @@ func (image *Image) DeepCopy(ioctx *rados.IOContext, destname string, rio *Image
return ErrNoName
}
if rio == nil {
return rbdError(C.EINVAL)
return getError(C.EINVAL)
}

cDestname := C.CString(destname)
Expand Down Expand Up @@ -644,8 +644,8 @@ func (image *Image) ListLockers() (tag string, lockers []Locker, err error) {
// and *0* means no locker held on rbd image.
// but *0* is unexpected here because first rbd_list_lockers already
// dealt with no locker case
if int(cLockerCount) <= 0 {
return "", nil, rbdError(cLockerCount)
if cLockerCount <= 0 {
return "", nil, getError(C.int(cLockerCount))
}

clients := cutil.SplitSparseBuffer(clientsBuf)
Expand Down Expand Up @@ -754,7 +754,7 @@ func (image *Image) Read(data []byte) (int, error) {
(*C.char)(unsafe.Pointer(&data[0]))))

if ret < 0 {
return 0, rbdError(ret)
return 0, getError(C.int(ret))
}

image.offset += int64(ret)
Expand Down Expand Up @@ -786,7 +786,7 @@ func (image *Image) Write(data []byte) (n int, err error) {
}

if ret != len(data) {
err = rbdError(-C.EPERM)
err = getError(-C.EPERM)
}

return ret, err
Expand Down Expand Up @@ -825,7 +825,7 @@ func (image *Image) Discard(ofs uint64, length uint64) (int, error) {

ret := C.rbd_discard(image.image, C.uint64_t(ofs), C.uint64_t(length))
if ret < 0 {
return 0, rbdError(ret)
return 0, getError(ret)
}

return int(ret), nil
Expand All @@ -848,7 +848,7 @@ func (image *Image) ReadAt(data []byte, off int64) (int, error) {
(*C.char)(unsafe.Pointer(&data[0]))))

if ret < 0 {
return 0, rbdError(ret)
return 0, getError(C.int(ret))
}

if ret < len(data) {
Expand All @@ -872,7 +872,7 @@ func (image *Image) WriteAt(data []byte, off int64) (n int, err error) {
C.size_t(len(data)), (*C.char)(unsafe.Pointer(&data[0]))))

if ret != len(data) {
err = rbdError(-C.EPERM)
err = getError(-C.EPERM)
}

return ret, err
Expand Down Expand Up @@ -938,15 +938,15 @@ func (image *Image) GetSnapshotNames() (snaps []SnapInfo, err error) {
ret := C.rbd_snap_list(image.image, nil, &cMaxSnaps)
// bugfix index out of range(&cSnaps[0])
if cMaxSnaps < 1 {
return nil, rbdError(ret)
return nil, getError(ret)
}
cSnaps := make([]C.rbd_snap_info_t, cMaxSnaps)
snaps = make([]SnapInfo, cMaxSnaps)

ret = C.rbd_snap_list(image.image,
&cSnaps[0], &cMaxSnaps)
if ret < 0 {
return nil, rbdError(ret)
return nil, getError(ret)
}

for i, s := range cSnaps {
Expand Down Expand Up @@ -1253,7 +1253,7 @@ func CreateImage(ioctx *rados.IOContext, name string, size uint64, rio *ImageOpt
return ErrNoName
}
if rio == nil {
return rbdError(C.EINVAL)
return getError(C.EINVAL)
}

cName := C.CString(name)
Expand Down Expand Up @@ -1294,7 +1294,7 @@ func CloneImage(ioctx *rados.IOContext, parentName, snapName string,
destctx *rados.IOContext, name string, rio *ImageOptions) error {

if rio == nil {
return rbdError(C.EINVAL)
return getError(C.EINVAL)
}

cParentName := C.CString(parentName)
Expand Down
2 changes: 1 addition & 1 deletion rbd/resize.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ func resize2Callback(
func (image *Image) Resize2(size uint64, allowShrink bool, cb Resize2ProgressCallback, data interface{}) error {
// the provided callback must be a real function
if cb == nil {
return rbdError(C.EINVAL)
return getError(C.EINVAL)
}

if err := image.validate(imageIsOpen); err != nil {
Expand Down
4 changes: 2 additions & 2 deletions rbd/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (image *Image) CreateSnapshot(snapname string) (*Snapshot, error) {

ret := C.rbd_snap_create(image.image, cSnapName)
if ret < 0 {
return nil, rbdError(ret)
return nil, getError(ret)
}

return &Snapshot{
Expand Down Expand Up @@ -147,7 +147,7 @@ func (snapshot *Snapshot) IsProtected() (bool, error) {
ret := C.rbd_snap_is_protected(snapshot.image.image, cSnapName,
&cIsProtected)
if ret < 0 {
return false, rbdError(ret)
return false, getError(ret)
}

return cIsProtected != 0, nil
Expand Down
14 changes: 7 additions & 7 deletions rbd/snapshot_nautilus.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,34 +32,34 @@ func (image *Image) GetParentInfo(pool, name, snapname []byte) error {
parentSnap := C.rbd_snap_spec_t{}
ret := C.rbd_get_parent(image.image, &parentImage, &parentSnap)
if ret != 0 {
return rbdError(ret)
return getError(ret)
}

defer C.rbd_linked_image_spec_cleanup(&parentImage)
defer C.rbd_snap_spec_cleanup(&parentSnap)

strlen := int(C.strlen(parentImage.pool_name))
if len(pool) < strlen {
return rbdError(C.ERANGE)
return getError(C.ERANGE)
}
if copy(pool, C.GoString(parentImage.pool_name)) != strlen {
return rbdError(C.ERANGE)
return getError(C.ERANGE)
}

strlen = int(C.strlen(parentImage.image_name))
if len(name) < strlen {
return rbdError(C.ERANGE)
return getError(C.ERANGE)
}
if copy(name, C.GoString(parentImage.image_name)) != strlen {
return rbdError(C.ERANGE)
return getError(C.ERANGE)
}

strlen = int(C.strlen(parentSnap.name))
if len(snapname) < strlen {
return rbdError(C.ERANGE)
return getError(C.ERANGE)
}
if copy(snapname, C.GoString(parentSnap.name)) != strlen {
return rbdError(C.ERANGE)
return getError(C.ERANGE)
}

return nil
Expand Down
Loading

0 comments on commit 3dbd875

Please sign in to comment.