]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: allow to open snapshot by id
authorMykola Golub <mgolub@suse.com>
Fri, 25 May 2018 13:32:13 +0000 (16:32 +0300)
committerJason Dillaman <dillaman@redhat.com>
Tue, 14 Aug 2018 22:29:45 +0000 (18:29 -0400)
Signed-off-by: Mykola Golub <mgolub@suse.com>
src/librbd/ImageCtx.cc
src/librbd/ImageCtx.h
src/librbd/api/Image.cc
src/librbd/image/CloneRequest.cc
src/librbd/image/OpenRequest.cc
src/librbd/internal.cc
src/test/librbd/test_fixture.cc
src/test/rbd_mirror/test_fixture.cc

index 0370981819de36751adc46d8a437cf3248113361..3abb4f677eb8bca2c2b8bfd91fc68f1aec9e944f 100644 (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);
index 0881e6129d118e6f646ee05a5cea28cd1ec5d51a..b2558ea471e203c14950ae0f110e738e1f725fa3 100644 (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();
index 2759992113429283244a53ea94450b3d80d56fec..fee4cde84c29e7fc6842a3431f400d9e05cc9b69 100644 (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) {
index 18dbbcd42e46dd17fb9a5b65504688b348e4659b..7d6ef5764eaf408b535913f8f1b9f39193458ecd 100644 (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<
index 89f2a5d0d194732181cfd51a1fa68ce5b6c276d8..8f3b78f1a60ea4d438d3c75bf5d403b176f4dbb5 100644 (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);
index 97ba292907e4b473d826b488d0e6c664cb1776b0..bf19b63d2c33640e38c65df1686d96519c1d1a28 100644 (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;
index a16e9f66d12c75d2198c1a39fb74fd343fb4eb77..9eee5f2f4a29fb188bf37f6d43de9a24ea2e3a04 100644 (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);
index b2a51cae32a9cac53c582d126bc8bc86bb87bd43..1c312a9a871d0ffbe0c1d5a6f9c8d6d2b1052bfe 100644 (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);