librbd: allow to open snapshot by id

Signed-off-by: Mykola Golub <mgolub@suse.com>
This commit is contained in:
Mykola Golub 2018-05-25 16:32:13 +03:00 committed by Jason Dillaman
parent 4c8731fe3c
commit 5065dd8fa0
8 changed files with 26 additions and 9 deletions

View File

@ -150,6 +150,12 @@ public:
journal_policy = new journal::StandardPolicy<ImageCtx>(this);
}
ImageCtx::ImageCtx(const string &image_name, const string &image_id,
uint64_t snap_id, IoCtx& p, bool ro)
: ImageCtx(image_name, image_id, "", p, ro) {
open_snap_id = snap_id;
}
ImageCtx::~ImageCtx() {
assert(image_watcher == NULL);
assert(exclusive_lock == NULL);

View File

@ -69,6 +69,7 @@ namespace librbd {
// a format librados can understand
std::map<librados::snap_t, SnapInfo> snap_info;
std::map<std::pair<cls::rbd::SnapshotNamespace, std::string>, librados::snap_t> snap_ids;
uint64_t open_snap_id = CEPH_NOSNAP;
uint64_t snap_id;
bool snap_exists; // false if our snap_id was deleted
// whether the image was opened read-only. cannot be changed after opening
@ -223,6 +224,12 @@ namespace librbd {
const char *snap, IoCtx& p, bool read_only) {
return new ImageCtx(image_name, image_id, snap, p, read_only);
}
static ImageCtx* create(const std::string &image_name,
const std::string &image_id,
librados::snap_t snap_id, IoCtx& p,
bool read_only) {
return new ImageCtx(image_name, image_id, snap_id, p, read_only);
}
void destroy() {
delete this;
}
@ -234,6 +241,8 @@ namespace librbd {
*/
ImageCtx(const std::string &image_name, const std::string &image_id,
const char *snap, IoCtx& p, bool read_only);
ImageCtx(const std::string &image_name, const std::string &image_id,
librados::snap_t snap_id, IoCtx& p, bool read_only);
~ImageCtx();
void init();
void shutdown();

View File

@ -262,7 +262,7 @@ int Image<I>::deep_copy(I *src, librados::IoCtx& dest_md_ctx,
}
opts.set(RBD_IMAGE_OPTION_ORDER, static_cast<uint64_t>(order));
ImageCtx *dest = new librbd::ImageCtx(destname, "", NULL,
ImageCtx *dest = new librbd::ImageCtx(destname, "", nullptr,
dest_md_ctx, false);
r = dest->state->open(0);
if (r < 0) {

View File

@ -325,7 +325,7 @@ template <typename I>
void CloneRequest<I>::open_child() {
ldout(m_cct, 20) << dendl;
m_imctx = I::create(m_name, "", NULL, m_ioctx, false);
m_imctx = I::create(m_name, "", nullptr, m_ioctx, false);
using klass = CloneRequest<I>;
Context *ctx = create_context_callback<

View File

@ -516,7 +516,8 @@ Context *OpenRequest<I>::handle_register_watch(int *result) {
template <typename I>
Context *OpenRequest<I>::send_set_snap(int *result) {
if (m_image_ctx->snap_name.empty()) {
if (m_image_ctx->snap_name.empty() &&
m_image_ctx->open_snap_id == CEPH_NOSNAP) {
*result = 0;
return m_on_finish;
}
@ -525,7 +526,8 @@ Context *OpenRequest<I>::send_set_snap(int *result) {
ldout(cct, 10) << this << " " << __func__ << dendl;
uint64_t snap_id = CEPH_NOSNAP;
{
std::swap(m_image_ctx->open_snap_id, snap_id);
if (snap_id == CEPH_NOSNAP) {
RWLock::RLocker snap_locker(m_image_ctx->snap_lock);
snap_id = m_image_ctx->get_snap_id(m_image_ctx->snap_namespace,
m_image_ctx->snap_name);

View File

@ -602,7 +602,7 @@ bool compare_by_name(const child_info_t& c1, const child_info_t& c2)
ioctx.set_namespace(ictx->md_ctx.get_namespace());
for (auto &id_it : info.second) {
ImageCtx *imctx = new ImageCtx("", id_it, NULL, ioctx, false);
ImageCtx *imctx = new ImageCtx("", id_it, nullptr, ioctx, false);
int r = imctx->state->open(0);
if (r < 0) {
lderr(cct) << "error opening image: "
@ -1813,8 +1813,8 @@ bool compare_by_name(const child_info_t& c1, const child_info_t& c2)
}
opts.set(RBD_IMAGE_OPTION_ORDER, static_cast<uint64_t>(order));
ImageCtx *dest = new librbd::ImageCtx(destname, "", NULL,
dest_md_ctx, false);
ImageCtx *dest = new librbd::ImageCtx(destname, "", nullptr, dest_md_ctx,
false);
r = dest->state->open(0);
if (r < 0) {
lderr(cct) << "failed to read newly created header" << dendl;

View File

@ -75,7 +75,7 @@ void TestFixture::TearDown() {
int TestFixture::open_image(const std::string &image_name,
librbd::ImageCtx **ictx) {
*ictx = new librbd::ImageCtx(image_name.c_str(), "", NULL, m_ioctx, false);
*ictx = new librbd::ImageCtx(image_name.c_str(), "", nullptr, m_ioctx, false);
m_ictxs.insert(*ictx);
return (*ictx)->state->open(false);

View File

@ -97,7 +97,7 @@ int TestFixture::create_image(librbd::RBD &rbd, librados::IoCtx &ioctx,
int TestFixture::open_image(librados::IoCtx &io_ctx,
const std::string &image_name,
librbd::ImageCtx **image_ctx) {
*image_ctx = new librbd::ImageCtx(image_name.c_str(), "", NULL, io_ctx,
*image_ctx = new librbd::ImageCtx(image_name.c_str(), "", nullptr, io_ctx,
false);
m_image_ctxs.insert(*image_ctx);
return (*image_ctx)->state->open(false);