rebase: use go-ceph v0.3.0

v0.3.0 adds support for rbd.FeatureSet that can be used to parse the
features of an RBD image. This will be used in the followup commit that
adds rbdVolume.getImageInfo().

Signed-off-by: Niels de Vos <ndevos@redhat.com>
This commit is contained in:
Niels de Vos
2020-05-08 16:07:31 +02:00
committed by mergify[bot]
parent 323cc0e3bb
commit 772d1dfa77
23 changed files with 990 additions and 375 deletions

View File

@ -7,15 +7,9 @@ import "C"
import (
"bytes"
"errors"
"unsafe"
)
var (
// ErrNotConnected is returned when functions are called without a RADOS connection
ErrNotConnected = errors.New("RADOS not connected")
)
// ClusterStat represents Ceph cluster statistics.
type ClusterStat struct {
Kb uint64
@ -53,7 +47,7 @@ func (c *Conn) PingMonitor(id string) (string, error) {
reply := C.GoStringN(strout, (C.int)(strlen))
return reply, nil
}
return "", RadosError(int(ret))
return "", getError(ret)
}
// Connect establishes a connection to a RADOS cluster. It returns an error,
@ -61,7 +55,7 @@ func (c *Conn) PingMonitor(id string) (string, error) {
func (c *Conn) Connect() error {
ret := C.rados_connect(c.cluster)
if ret != 0 {
return RadosError(int(ret))
return getError(ret)
}
c.connected = true
return nil
@ -80,14 +74,14 @@ func (c *Conn) ReadConfigFile(path string) error {
c_path := C.CString(path)
defer C.free(unsafe.Pointer(c_path))
ret := C.rados_conf_read_file(c.cluster, c_path)
return getRadosError(int(ret))
return getError(ret)
}
// ReadDefaultConfigFile configures the connection using a Ceph configuration
// file located at default locations.
func (c *Conn) ReadDefaultConfigFile() error {
ret := C.rados_conf_read_file(c.cluster, nil)
return getRadosError(int(ret))
return getError(ret)
}
// OpenIOContext creates and returns a new IOContext for the given pool.
@ -103,20 +97,20 @@ func (c *Conn) OpenIOContext(pool string) (*IOContext, error) {
if ret == 0 {
return ioctx, nil
}
return nil, RadosError(int(ret))
return nil, getError(ret)
}
// ListPools returns the names of all existing pools.
func (c *Conn) ListPools() (names []string, err error) {
buf := make([]byte, 4096)
for {
ret := int(C.rados_pool_list(c.cluster,
(*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))))
ret := C.rados_pool_list(c.cluster,
(*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
if ret < 0 {
return nil, RadosError(int(ret))
return nil, getError(ret)
}
if ret > len(buf) {
if int(ret) > len(buf) {
buf = make([]byte, ret)
continue
}
@ -140,7 +134,7 @@ func (c *Conn) SetConfigOption(option, value string) error {
defer C.free(unsafe.Pointer(c_opt))
defer C.free(unsafe.Pointer(c_val))
ret := C.rados_conf_set(c.cluster, c_opt, c_val)
return getRadosError(int(ret))
return getError(ret)
}
// GetConfigOption returns the value of the Ceph configuration option
@ -165,7 +159,7 @@ func (c *Conn) GetConfigOption(name string) (value string, err error) {
// retrieved.
func (c *Conn) WaitForLatestOSDMap() error {
ret := C.rados_wait_for_latest_osdmap(c.cluster)
return getRadosError(int(ret))
return getError(ret)
}
func (c *Conn) ensure_connected() error {
@ -184,7 +178,7 @@ func (c *Conn) GetClusterStats() (stat ClusterStat, err error) {
c_stat := C.struct_rados_cluster_stat_t{}
ret := C.rados_cluster_stat(c.cluster, &c_stat)
if ret < 0 {
return ClusterStat{}, RadosError(int(ret))
return ClusterStat{}, getError(ret)
}
return ClusterStat{
Kb: uint64(c_stat.kb),
@ -211,28 +205,28 @@ func (c *Conn) ParseCmdLineArgs(args []string) error {
}
ret := C.rados_conf_parse_argv(c.cluster, argc, &argv[0])
return getRadosError(int(ret))
return getError(ret)
}
// ParseDefaultConfigEnv configures the connection from the default Ceph
// environment variable(s).
func (c *Conn) ParseDefaultConfigEnv() error {
ret := C.rados_conf_parse_env(c.cluster, nil)
return getRadosError(int(ret))
return getError(ret)
}
// GetFSID returns the fsid of the cluster as a hexadecimal string. The fsid
// is a unique identifier of an entire Ceph cluster.
func (c *Conn) GetFSID() (fsid string, err error) {
buf := make([]byte, 37)
ret := int(C.rados_cluster_fsid(c.cluster,
(*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf))))
ret := C.rados_cluster_fsid(c.cluster,
(*C.char)(unsafe.Pointer(&buf[0])), C.size_t(len(buf)))
// FIXME: the success case isn't documented correctly in librados.h
if ret == 36 {
fsid = C.GoString((*C.char)(unsafe.Pointer(&buf[0])))
return fsid, nil
}
return "", RadosError(int(ret))
return "", getError(ret)
}
// GetInstanceID returns a globally unique identifier for the cluster
@ -246,8 +240,8 @@ func (c *Conn) GetInstanceID() uint64 {
func (c *Conn) MakePool(name string) error {
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
ret := int(C.rados_pool_create(c.cluster, c_name))
return getRadosError(int(ret))
ret := C.rados_pool_create(c.cluster, c_name)
return getError(ret)
}
// DeletePool deletes a pool and all the data inside the pool.
@ -257,8 +251,8 @@ func (c *Conn) DeletePool(name string) error {
}
c_name := C.CString(name)
defer C.free(unsafe.Pointer(c_name))
ret := int(C.rados_pool_delete(c.cluster, c_name))
return getRadosError(int(ret))
ret := C.rados_pool_delete(c.cluster, c_name)
return getError(ret)
}
// GetPoolByName returns the ID of the pool with a given name.
@ -329,7 +323,7 @@ func (c *Conn) monCommand(args, inputBuffer []byte) (buffer []byte, info string,
C.free(unsafe.Pointer(outbuf))
}
if ret != 0 {
err = RadosError(int(ret))
err = getError(ret)
return nil, info, err
}
@ -400,7 +394,7 @@ func (c *Conn) pgCommand(pgid []byte, args [][]byte, inputBuffer []byte) (buffer
C.free(unsafe.Pointer(outbuf))
}
if ret != 0 {
err = RadosError(int(ret))
err = getError(ret)
return nil, info, err
}

63
vendor/github.com/ceph/go-ceph/rados/errors.go generated vendored Normal file
View File

@ -0,0 +1,63 @@
package rados
/*
#include <errno.h>
*/
import "C"
import (
"errors"
"fmt"
"github.com/ceph/go-ceph/internal/errutil"
)
// revive:disable:exported Temporarily live with stuttering
// RadosError represents an error condition returned from the Ceph RADOS APIs.
type RadosError int
// revive:enable:exported
// Error returns the error string for the RadosError type.
func (e RadosError) Error() string {
errno, s := errutil.FormatErrno(int(e))
if s == "" {
return fmt.Sprintf("rados: ret=%d", errno)
}
return fmt.Sprintf("rados: ret=%d, %s", errno, s)
}
func getError(e C.int) error {
if e == 0 {
return nil
}
return RadosError(e)
}
// Public go errors:
var (
// ErrNotConnected is returned when functions are called without a RADOS connection
ErrNotConnected = errors.New("RADOS not connected")
)
// Public RadosErrors:
const (
// ErrNotFound indicates a missing resource.
ErrNotFound = RadosError(-C.ENOENT)
// ErrPermissionDenied indicates a permissions issue.
ErrPermissionDenied = RadosError(-C.EPERM)
// ErrObjectExists indicates that an exclusive object creation failed.
ErrObjectExists = RadosError(-C.EEXIST)
// RadosErrorNotFound indicates a missing resource.
//
// Deprecated: use ErrNotFound instead
RadosErrorNotFound = ErrNotFound
// RadosErrorPermissionDenied indicates a permissions issue.
//
// Deprecated: use ErrPermissionDenied instead
RadosErrorPermissionDenied = ErrPermissionDenied
)

View File

@ -90,9 +90,10 @@ type IOContext struct {
ioctx C.rados_ioctx_t
}
// Pointer returns a uintptr representation of the IOContext.
func (ioctx *IOContext) Pointer() uintptr {
return uintptr(ioctx.ioctx)
// Pointer returns a pointer reference to an internal structure.
// This function should NOT be used outside of go-ceph itself.
func (ioctx *IOContext) Pointer() unsafe.Pointer {
return unsafe.Pointer(ioctx.ioctx)
}
// SetNamespace sets the namespace for objects within this IO context (pool).
@ -120,7 +121,7 @@ func (ioctx *IOContext) Create(oid string, exclusive CreateOption) error {
ret := C.rados_write_op_operate(op, ioctx.ioctx, c_oid, nil, 0)
C.rados_release_write_op(op)
return getRadosError(int(ret))
return getError(ret)
}
// Write writes len(data) bytes to the object with key oid starting at byte
@ -139,7 +140,7 @@ func (ioctx *IOContext) Write(oid string, data []byte, offset uint64) error {
(C.size_t)(len(data)),
(C.uint64_t)(offset))
return getRadosError(int(ret))
return getError(ret)
}
// WriteFull writes len(data) bytes to the object with key oid.
@ -152,7 +153,7 @@ func (ioctx *IOContext) WriteFull(oid string, data []byte) error {
ret := C.rados_write_full(ioctx.ioctx, c_oid,
(*C.char)(unsafe.Pointer(&data[0])),
(C.size_t)(len(data)))
return getRadosError(int(ret))
return getError(ret)
}
// Append appends len(data) bytes to the object with key oid.
@ -165,7 +166,7 @@ func (ioctx *IOContext) Append(oid string, data []byte) error {
ret := C.rados_append(ioctx.ioctx, c_oid,
(*C.char)(unsafe.Pointer(&data[0])),
(C.size_t)(len(data)))
return getRadosError(int(ret))
return getError(ret)
}
// Read reads up to len(data) bytes from the object with key oid starting at byte
@ -189,7 +190,7 @@ func (ioctx *IOContext) Read(oid string, data []byte, offset uint64) (int, error
if ret >= 0 {
return int(ret), nil
}
return 0, getRadosError(int(ret))
return 0, getError(ret)
}
// Delete deletes the object with key oid. It returns an error, if any.
@ -197,7 +198,7 @@ func (ioctx *IOContext) Delete(oid string) error {
c_oid := C.CString(oid)
defer C.free(unsafe.Pointer(c_oid))
return getRadosError(int(C.rados_remove(ioctx.ioctx, c_oid)))
return getError(C.rados_remove(ioctx.ioctx, c_oid))
}
// Truncate resizes the object with key oid to size size. If the operation
@ -208,7 +209,7 @@ func (ioctx *IOContext) Truncate(oid string, size uint64) error {
c_oid := C.CString(oid)
defer C.free(unsafe.Pointer(c_oid))
return getRadosError(int(C.rados_trunc(ioctx.ioctx, c_oid, (C.uint64_t)(size))))
return getError(C.rados_trunc(ioctx.ioctx, c_oid, (C.uint64_t)(size)))
}
// Destroy informs librados that the I/O context is no longer in use.
@ -228,7 +229,7 @@ func (ioctx *IOContext) GetPoolStats() (stat PoolStat, err error) {
c_stat := C.struct_rados_pool_stat_t{}
ret := C.rados_ioctx_pool_stat(ioctx.ioctx, &c_stat)
if ret < 0 {
return PoolStat{}, getRadosError(int(ret))
return PoolStat{}, getError(ret)
}
return PoolStat{
Num_bytes: uint64(c_stat.num_bytes),
@ -256,7 +257,7 @@ func (ioctx *IOContext) GetPoolName() (name string, err error) {
buf = make([]byte, len(buf)*2)
continue
} else if ret < 0 {
return "", getRadosError(int(ret))
return "", getError(ret)
}
name = C.GoStringN((*C.char)(unsafe.Pointer(&buf[0])), ret)
return name, nil
@ -276,7 +277,7 @@ func (ioctx *IOContext) ListObjects(listFn ObjectListFunc) error {
var ctx C.rados_list_ctx_t
ret := C.rados_nobjects_list_open(ioctx.ioctx, &ctx)
if ret < 0 {
return getRadosError(int(ret))
return getError(ret)
}
defer func() { C.rados_nobjects_list_close(ctx) }()
@ -286,7 +287,7 @@ func (ioctx *IOContext) ListObjects(listFn ObjectListFunc) error {
if ret == -C.ENOENT {
return nil
} else if ret < 0 {
return getRadosError(int(ret))
return getError(ret)
}
listFn(C.GoString(c_entry))
}
@ -306,7 +307,7 @@ func (ioctx *IOContext) Stat(object string) (stat ObjectStat, err error) {
&c_pmtime)
if ret < 0 {
return ObjectStat{}, getRadosError(int(ret))
return ObjectStat{}, getError(ret)
}
return ObjectStat{
Size: uint64(c_psize),
@ -332,7 +333,7 @@ func (ioctx *IOContext) GetXattr(object string, name string, data []byte) (int,
if ret >= 0 {
return int(ret), nil
}
return 0, getRadosError(int(ret))
return 0, getError(ret)
}
// SetXattr sets an xattr for an object with key `name` with value as `data`
@ -349,7 +350,7 @@ func (ioctx *IOContext) SetXattr(object string, name string, data []byte) error
(*C.char)(unsafe.Pointer(&data[0])),
(C.size_t)(len(data)))
return getRadosError(int(ret))
return getError(ret)
}
// ListXattrs lists all the xattrs for an object. The xattrs are returned as a
@ -362,7 +363,7 @@ func (ioctx *IOContext) ListXattrs(oid string) (map[string][]byte, error) {
ret := C.rados_getxattrs(ioctx.ioctx, c_oid, &it)
if ret < 0 {
return nil, getRadosError(int(ret))
return nil, getError(ret)
}
defer func() { C.rados_getxattrs_end(it) }()
m := make(map[string][]byte)
@ -374,7 +375,7 @@ func (ioctx *IOContext) ListXattrs(oid string) (map[string][]byte, error) {
ret := C.rados_getxattrs_next(it, &c_name, &c_val, &c_len)
if ret < 0 {
return nil, getRadosError(int(ret))
return nil, getError(ret)
}
// rados api returns a null name,val & 0-length upon
// end of iteration
@ -397,7 +398,7 @@ func (ioctx *IOContext) RmXattr(oid string, name string) error {
c_oid,
c_name)
return getRadosError(int(ret))
return getError(ret)
}
// LockExclusive takes an exclusive lock on an object.
@ -444,7 +445,7 @@ func (ioctx *IOContext) LockExclusive(oid, name, cookie, desc string, duration t
case -C.EEXIST:
return int(ret), nil
default:
return int(ret), RadosError(int(ret))
return int(ret), getError(ret)
}
}
@ -495,7 +496,7 @@ func (ioctx *IOContext) LockShared(oid, name, cookie, tag, desc string, duration
case -C.EEXIST:
return int(ret), nil
default:
return int(ret), RadosError(int(ret))
return int(ret), getError(ret)
}
}
@ -524,7 +525,7 @@ func (ioctx *IOContext) Unlock(oid, name, cookie string) (int, error) {
case -C.ENOENT:
return int(ret), nil
default:
return int(ret), RadosError(int(ret))
return int(ret), getError(ret)
}
}
@ -581,7 +582,7 @@ func (ioctx *IOContext) ListLockers(oid, name string) (*LockInfo, error) {
}
if ret < 0 {
return nil, RadosError(int(ret))
return nil, RadosError(ret)
}
return &LockInfo{int(ret), c_exclusive == 1, C.GoString(c_tag), splitCString(c_clients, c_clients_len), splitCString(c_cookies, c_cookies_len), splitCString(c_addrs, c_addrs_len)}, nil
}
@ -617,6 +618,6 @@ func (ioctx *IOContext) BreakLock(oid, name, client, cookie string) (int, error)
case -C.EINVAL: // -EINVAL
return int(ret), nil
default:
return int(ret), RadosError(int(ret))
return int(ret), getError(ret)
}
}

View File

@ -20,7 +20,7 @@ type IterToken uint32
func (ioctx *IOContext) Iter() (*Iter, error) {
iter := Iter{}
if cerr := C.rados_nobjects_list_open(ioctx.ioctx, &iter.ctx); cerr < 0 {
return nil, getRadosError(int(cerr))
return nil, getError(cerr)
}
return &iter, nil
}
@ -53,7 +53,7 @@ func (iter *Iter) Next() bool {
var c_entry *C.char
var c_namespace *C.char
if cerr := C.rados_nobjects_list_next(iter.ctx, &c_entry, nil, &c_namespace); cerr < 0 {
iter.err = getRadosError(int(cerr))
iter.err = getError(cerr)
return false
}
iter.entry = C.GoString(c_entry)

View File

@ -63,7 +63,7 @@ func (ioctx *IOContext) SetOmap(oid string, pairs map[string][]byte) error {
ret := C.rados_write_op_operate(op, ioctx.ioctx, c_oid, nil, 0)
C.rados_release_write_op(op)
return getRadosError(int(ret))
return getError(ret)
}
// OmapListFunc is the type of the function called for each omap key
@ -104,9 +104,9 @@ func (ioctx *IOContext) ListOmapValues(oid string, startAfter string, filterPref
ret := C.rados_read_op_operate(op, ioctx.ioctx, c_oid, 0)
if int(ret) != 0 {
return getRadosError(int(ret))
return getError(ret)
} else if int(c_prval) != 0 {
return RadosError(int(c_prval))
return getError(c_prval)
}
for {
@ -117,7 +117,7 @@ func (ioctx *IOContext) ListOmapValues(oid string, startAfter string, filterPref
ret = C.rados_omap_get_next(c_iter, &c_key, &c_val, &c_len)
if int(ret) != 0 {
return getRadosError(int(ret))
return getError(ret)
}
if c_key == nil {
@ -210,7 +210,7 @@ func (ioctx *IOContext) RmOmapKeys(oid string, keys []string) error {
ret := C.rados_write_op_operate(op, ioctx.ioctx, c_oid, nil, 0)
C.rados_release_write_op(op)
return getRadosError(int(ret))
return getError(ret)
}
// CleanOmap clears the omap `oid`
@ -224,5 +224,5 @@ func (ioctx *IOContext) CleanOmap(oid string) error {
ret := C.rados_write_op_operate(op, ioctx.ioctx, c_oid, nil, 0)
C.rados_release_write_op(op)
return getRadosError(int(ret))
return getError(ret)
}

View File

@ -7,37 +7,15 @@ package rados
import "C"
import (
"fmt"
"runtime"
"unsafe"
"github.com/ceph/go-ceph/errutil"
)
// RadosError represents an error condition returned from the Ceph RADOS APIs.
type RadosError int
// Error returns the error string for the RadosError type.
func (e RadosError) Error() string {
errno, s := errutil.FormatErrno(int(e))
if s == "" {
return fmt.Sprintf("rados: ret=%d", errno)
}
return fmt.Sprintf("rados: ret=%d, %s", errno, s)
}
const (
// AllNamespaces is used to reset a selected namespace to all
// namespaces. See the IOContext SetNamespace function.
AllNamespaces = C.LIBRADOS_ALL_NSPACES
// ErrNotFound indicates a missing resource.
ErrNotFound = RadosError(-C.ENOENT)
// ErrPermissionDenied indicates a permissions issue.
ErrPermissionDenied = RadosError(-C.EPERM)
// ErrObjectExists indicates that an exclusive object creation failed.
ErrObjectExists = RadosError(-C.EEXIST)
// FIXME: for backwards compatibility
// RadosAllNamespaces is used to reset a selected namespace to all
@ -45,23 +23,8 @@ const (
//
// Deprecated: use AllNamespaces instead
RadosAllNamespaces = AllNamespaces
// RadosErrorNotFound indicates a missing resource.
//
// Deprecated: use ErrNotFound instead
RadosErrorNotFound = ErrNotFound
// RadosErrorPermissionDenied indicates a permissions issue.
//
// Deprecated: use ErrPermissionDenied instead
RadosErrorPermissionDenied = ErrPermissionDenied
)
func getRadosError(err int) error {
if err == 0 {
return nil
}
return RadosError(err)
}
// Version returns the major, minor, and patch components of the version of
// the RADOS library linked against.
func Version() (int, int, int) {
@ -79,7 +42,7 @@ func newConn(user *C.char) (*Conn, error) {
ret := C.rados_create(&conn.cluster, user)
if ret != 0 {
return nil, RadosError(int(ret))
return nil, getError(ret)
}
runtime.SetFinalizer(conn, freeConn)
@ -112,7 +75,7 @@ func NewConnWithClusterAndUser(clusterName string, userName string) (*Conn, erro
conn := makeConn()
ret := C.rados_create2(&conn.cluster, c_cluster_name, c_name, 0)
if ret != 0 {
return nil, RadosError(int(ret))
return nil, getError(ret)
}
runtime.SetFinalizer(conn, freeConn)