|
| 1 | +//go:build ceph_preview |
| 2 | + |
| 3 | +package rbd |
| 4 | + |
| 5 | +/* |
| 6 | +#cgo LDFLAGS: -lrbd |
| 7 | +#undef _GNU_SOURCE |
| 8 | +#include <errno.h> |
| 9 | +#include <stdlib.h> |
| 10 | +#include <rbd/librbd.h> |
| 11 | +
|
| 12 | +// inline wrapper to cast uintptr_t to void* |
| 13 | +static inline int wrap_rbd_diff_iterate3(rbd_image_t image, |
| 14 | + uint64_t from_snap_id, uint64_t ofs, uint64_t len, uint8_t include_parent, |
| 15 | + uint8_t whole_object, uintptr_t arg) { |
| 16 | + return rbd_diff_iterate3(image, from_snap_id, ofs, len, include_parent, |
| 17 | + whole_object, (void*)diffIterateCallback, (void*)arg); |
| 18 | +}; |
| 19 | +*/ |
| 20 | +import "C" |
| 21 | + |
| 22 | +import ( |
| 23 | + "fmt" |
| 24 | + "sync" |
| 25 | + |
| 26 | + "github.com/ceph/go-ceph/internal/dlsym" |
| 27 | +) |
| 28 | + |
| 29 | +var ( |
| 30 | + diffIterateByIDOnce sync.Once |
| 31 | + diffIterateByIdErr error |
| 32 | +) |
| 33 | + |
| 34 | +// DiffIterateByIDConfig is used to define the parameters of a DiffIterateByID call. |
| 35 | +// Callback, Offset, and Length should always be specified when passed to |
| 36 | +// DiffIterateByID. The other values are optional. |
| 37 | +type DiffIterateByIDConfig struct { |
| 38 | + FromSnapID uint64 |
| 39 | + Offset uint64 |
| 40 | + Length uint64 |
| 41 | + IncludeParent DiffIncludeParent |
| 42 | + WholeObject DiffWholeObject |
| 43 | + Callback DiffIterateCallback |
| 44 | + Data interface{} |
| 45 | +} |
| 46 | + |
| 47 | +// DiffIterateByID calls a callback on changed extents of an image. |
| 48 | +// |
| 49 | +// Calling DiffIterateByID will cause the callback specified in the |
| 50 | +// DiffIterateConfig to be called as many times as there are changed |
| 51 | +// regions in the image (controlled by the parameters as passed to librbd). |
| 52 | +// |
| 53 | +// See the documentation of DiffIterateCallback for a description of the |
| 54 | +// arguments to the callback and the return behavior. |
| 55 | +// |
| 56 | +// Implements: |
| 57 | +// |
| 58 | +// int rbd_diff_iterate3(rbd_image_t image, |
| 59 | +// uint64_t from_snap_id, |
| 60 | +// uint64_t ofs, uint64_t len, |
| 61 | +// uint8_t include_parent, uint8_t whole_object, |
| 62 | +// int (*cb)(uint64_t, size_t, int, void *), |
| 63 | +// void *arg); |
| 64 | +func (image *Image) DiffIterateByID(config DiffIterateByIDConfig) error { |
| 65 | + if err := image.validate(imageIsOpen); err != nil { |
| 66 | + return err |
| 67 | + } |
| 68 | + if config.Callback == nil { |
| 69 | + return getError(C.EINVAL) |
| 70 | + } |
| 71 | + |
| 72 | + diffIterateByIDOnce.Do(func() { |
| 73 | + _, diffIterateByIdErr = dlsym.LookupSymbol("rbd_diff_iterate3") |
| 74 | + }) |
| 75 | + |
| 76 | + if diffIterateByIdErr != nil { |
| 77 | + return fmt.Errorf("%w: %w", ErrNotImplemented, diffIterateByIdErr) |
| 78 | + } |
| 79 | + |
| 80 | + cbIndex := diffIterateCallbacks.Add(config) |
| 81 | + defer diffIterateCallbacks.Remove(cbIndex) |
| 82 | + |
| 83 | + ret := C.wrap_rbd_diff_iterate3( |
| 84 | + image.image, |
| 85 | + C.uint64_t(config.FromSnapID), |
| 86 | + C.uint64_t(config.Offset), |
| 87 | + C.uint64_t(config.Length), |
| 88 | + C.uint8_t(config.IncludeParent), |
| 89 | + C.uint8_t(config.WholeObject), |
| 90 | + C.uintptr_t(cbIndex)) |
| 91 | + |
| 92 | + return getError(ret) |
| 93 | +} |
0 commit comments