From bc254c851c0dea361f1b95418d33478124ea5db8 Mon Sep 17 00:00:00 2001 From: Ricardo Dias Date: Mon, 28 Mar 2016 15:09:29 +0100 Subject: [PATCH] rbd: rbd-mirroring: Enabling image mirroring depends on pool mirroring mode Signed-off-by: Ricardo Dias --- src/librbd/internal.cc | 124 ++++++++++++++++++++++++----------------- 1 file changed, 72 insertions(+), 52 deletions(-) diff --git a/src/librbd/internal.cc b/src/librbd/internal.cc index 3d2b6df7b81fa..f140a00f10895 100644 --- a/src/librbd/internal.cc +++ b/src/librbd/internal.cc @@ -200,6 +200,69 @@ int validate_mirroring_enabled(ImageCtx *ictx) { return 0; } +int mirror_image_enable_internal(ImageCtx *ictx) { + CephContext *cct = ictx->cct; + + if ((ictx->features & RBD_FEATURE_JOURNALING) == 0) { + lderr(cct) << "cannot enable mirroring: journaling is not enabled" + << dendl; + return -EINVAL; + } + + bool is_primary; + int r = Journal<>::is_tag_owner(ictx, &is_primary); + if (r < 0) { + lderr(cct) << "cannot enable mirroring: failed to check tag ownership: " + << cpp_strerror(r) << dendl; + return r; + } + + if (!is_primary) { + lderr(cct) << + "cannot enable mirroring: last journal tag not owned by local cluster" + << dendl; + return -EINVAL; + } + + cls::rbd::MirrorImage mirror_image_internal; + r = cls_client::mirror_image_get(&ictx->md_ctx, ictx->id, + &mirror_image_internal); + if (r < 0 && r != -ENOENT) { + lderr(cct) << "cannot enable mirroring: " << cpp_strerror(r) << dendl; + return r; + } + + if (mirror_image_internal.state == + cls::rbd::MirrorImageState::MIRROR_IMAGE_STATE_ENABLED) { + // mirroring is already enabled + return 0; + } + else if (r != -ENOENT) { + lderr(cct) << "cannot enable mirroring: mirroring image is in " + "disabling state" << dendl; + return -EINVAL; + } + + mirror_image_internal.state = + cls::rbd::MirrorImageState::MIRROR_IMAGE_STATE_ENABLED; + + uuid_d uuid_gen; + uuid_gen.generate_random(); + mirror_image_internal.global_image_id = uuid_gen.to_string(); + + r = cls_client::mirror_image_set(&ictx->md_ctx, ictx->id, + mirror_image_internal); + if (r < 0) { + lderr(cct) << "cannot enable mirroring: " << cpp_strerror(r) << dendl; + return r; + } + + ldout(cct, 20) << "image mirroring is enabled: global_id=" << + mirror_image_internal.global_image_id << dendl; + + return 0; +} + } // anonymous namespace int detect_format(IoCtx &io_ctx, const string &name, @@ -917,7 +980,7 @@ int validate_mirroring_enabled(ImageCtx *ictx) { delete img_ctx; goto err_remove_object_map; } - r = mirror_image_enable(img_ctx); + r = mirror_image_enable_internal(img_ctx); if (r < 0) { lderr(cct) << "error enabling mirroring: " << cpp_strerror(r) << dendl; @@ -1566,7 +1629,7 @@ int validate_mirroring_enabled(ImageCtx *ictx) { lderr(cct) << "error opening image: " << cpp_strerror(r) << dendl; delete img_ctx; } else { - r = mirror_image_enable(img_ctx); + r = mirror_image_enable_internal(img_ctx); if (r < 0) { lderr(cct) << "error enabling mirroring: " << cpp_strerror(r) << dendl; @@ -2433,64 +2496,21 @@ int validate_mirroring_enabled(ImageCtx *ictx) { CephContext *cct = ictx->cct; ldout(cct, 20) << "mirror_image_enable " << ictx << dendl; - if ((ictx->features & RBD_FEATURE_JOURNALING) == 0) { - lderr(cct) << "cannot enable mirroring: journaling is not enabled" - << dendl; - return -EINVAL; - } - - bool is_primary; - int r = Journal<>::is_tag_owner(ictx, &is_primary); + cls::rbd::MirrorMode mirror_mode; + int r = cls_client::mirror_mode_get(&ictx->md_ctx, &mirror_mode); if (r < 0) { - lderr(cct) << "cannot enable mirroring: failed to check tag ownership: " + lderr(cct) << "cannot enable mirroring: failed to retrieve mirror mode: " << cpp_strerror(r) << dendl; return r; } - if (!is_primary) { - lderr(cct) << - "cannot enable mirroring: last journal tag not owned by local cluster" - << dendl; + if (mirror_mode != cls::rbd::MIRROR_MODE_IMAGE) { + lderr(cct) << "cannot enable mirroring in the current pool mirroring " + "mode" << dendl; return -EINVAL; } - cls::rbd::MirrorImage mirror_image_internal; - r = cls_client::mirror_image_get(&ictx->md_ctx, ictx->id, - &mirror_image_internal); - if (r < 0 && r != -ENOENT) { - lderr(cct) << "cannot enable mirroring: " << cpp_strerror(r) << dendl; - return r; - } - - if (mirror_image_internal.state == - cls::rbd::MirrorImageState::MIRROR_IMAGE_STATE_ENABLED) { - // mirroring is already enabled - return 0; - } - else if (r != -ENOENT) { - lderr(cct) << "cannot enable mirroring: mirroring image is in " - "disabling state" << dendl; - return -EINVAL; - } - - mirror_image_internal.state = - cls::rbd::MirrorImageState::MIRROR_IMAGE_STATE_ENABLED; - - uuid_d uuid_gen; - uuid_gen.generate_random(); - mirror_image_internal.global_image_id = uuid_gen.to_string(); - - r = cls_client::mirror_image_set(&ictx->md_ctx, ictx->id, - mirror_image_internal); - if (r < 0) { - lderr(cct) << "cannot enable mirroring: " << cpp_strerror(r) << dendl; - return r; - } - - ldout(cct, 20) << "image mirroring is enabled: global_id=" << - mirror_image_internal.global_image_id << dendl; - - return 0; + return mirror_image_enable_internal(ictx); } int mirror_image_disable(ImageCtx *ictx, bool force) { -- 2.39.5