]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: specify image id for create and clone operations
authorJason Dillaman <dillaman@redhat.com>
Fri, 21 Apr 2017 00:38:37 +0000 (20:38 -0400)
committerJason Dillaman <dillaman@redhat.com>
Fri, 21 Apr 2017 02:38:18 +0000 (22:38 -0400)
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
src/librbd/image/CloneRequest.cc
src/librbd/image/CloneRequest.h
src/librbd/internal.cc
src/librbd/internal.h
src/librbd/librbd.cc
src/test/rbd_mirror/image_replayer/test_mock_CreateImageRequest.cc
src/tools/rbd_mirror/image_replayer/CreateImageRequest.cc

index c7d2ddca1b5d09e11c4e4175c79aace897112908..8a7ec00886981036b86bb91ce1e1ff1962feb597 100644 (file)
@@ -26,12 +26,15 @@ using util::create_context_callback;
 using util::create_async_context_callback;
 
 template <typename I>
-CloneRequest<I>::CloneRequest(I *p_imctx, IoCtx &c_ioctx, const std::string &c_name,
+CloneRequest<I>::CloneRequest(I *p_imctx, IoCtx &c_ioctx,
+                             const std::string &c_name,
+                             const std::string &c_id,
                              ImageOptions c_options,
                              const std::string &non_primary_global_image_id,
                              const std::string &primary_mirror_uuid,
                              ContextWQ *op_work_queue, Context *on_finish)
-  : m_p_imctx(p_imctx), m_ioctx(c_ioctx), m_name(c_name), m_opts(c_options),
+  : m_p_imctx(p_imctx), m_ioctx(c_ioctx), m_name(c_name), m_id(c_id),
+    m_opts(c_options),
     m_pspec(m_p_imctx->md_ctx.get_id(), m_p_imctx->id, m_p_imctx->snap_id),
     m_non_primary_global_image_id(non_primary_global_image_id),
     m_primary_mirror_uuid(primary_mirror_uuid),
@@ -199,7 +202,6 @@ void CloneRequest<I>::send_create() {
   Context *ctx = create_context_callback<klass, &klass::handle_create>(this);
 
   RWLock::RLocker snap_locker(m_p_imctx->snap_lock);
-  m_id = librbd::util::generate_image_id(m_ioctx);
   CreateRequest<I> *req = CreateRequest<I>::create(
     m_ioctx, m_name, m_id, m_size, m_opts, m_non_primary_global_image_id,
     m_primary_mirror_uuid, true, m_op_work_queue, ctx);
index d4052542306b1014080c01bea1f30828cd3231fe..e1622eb1d863497e3338d18e3892a24ae300fd7b 100644 (file)
@@ -19,11 +19,11 @@ template <typename ImageCtxT = ImageCtx>
 class CloneRequest {
 public:
   static CloneRequest *create(ImageCtxT *p_imctx, IoCtx &c_ioctx, const std::string &c_name,
-                             ImageOptions c_options,
+                             const std::string &c_id, ImageOptions c_options,
                              const std::string &non_primary_global_image_id,
                              const std::string &primary_mirror_uuid,
                              ContextWQ *op_work_queue, Context *on_finish) {
-    return new CloneRequest(p_imctx, c_ioctx, c_name, c_options,
+    return new CloneRequest(p_imctx, c_ioctx, c_name, c_id, c_options,
                              non_primary_global_image_id, primary_mirror_uuid,
                              op_work_queue, on_finish);
   }
@@ -78,6 +78,7 @@ private:
    */
 
   CloneRequest(ImageCtxT *p_imctx, IoCtx &c_ioctx, const std::string &c_name,
+                             const std::string &c_id,
                              ImageOptions c_options,
                              const std::string &non_primary_global_image_id,
                              const std::string &primary_mirror_uuid,
index d9a151d1404d76d6bd10131a1a04f5a425afa83b..7558169c9e46437057301fafcb146bbfe18da276 100644 (file)
@@ -785,7 +785,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
     int r = opts.set(RBD_IMAGE_OPTION_ORDER, order_);
     assert(r == 0);
 
-    r = create(io_ctx, imgname, size, opts, "", "", false);
+    r = create(io_ctx, imgname, "", size, opts, "", "", false);
 
     int r1 = opts.get(RBD_IMAGE_OPTION_ORDER, &order_);
     assert(r1 == 0);
@@ -817,7 +817,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
     r = opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, stripe_count);
     assert(r == 0);
 
-    r = create(io_ctx, imgname, size, opts, "", "", false);
+    r = create(io_ctx, imgname, "", size, opts, "", "", false);
 
     int r1 = opts.get(RBD_IMAGE_OPTION_ORDER, &order_);
     assert(r1 == 0);
@@ -826,30 +826,37 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
     return r;
   }
 
-  int create(IoCtx& io_ctx, const char *imgname, uint64_t size,
+  int create(IoCtx& io_ctx, const std::string &image_name,
+            const std::string &image_id, uint64_t size,
             ImageOptions& opts,
              const std::string &non_primary_global_image_id,
              const std::string &primary_mirror_uuid,
              bool skip_mirror_enable)
   {
+    std::string id(image_id);
+    if (id.empty()) {
+      id = util::generate_image_id(io_ctx);
+    }
+
     CephContext *cct = (CephContext *)io_ctx.cct();
-    ldout(cct, 10) << __func__ << " name=" << imgname << ", "
-                   << "size=" << size << ", opts=" << opts << dendl;
+    ldout(cct, 10) << __func__ << " name=" << image_name << ", "
+                  << "id= " << id << ", "
+                  << "size=" << size << ", opts=" << opts << dendl;
 
     uint64_t format;
     if (opts.get(RBD_IMAGE_OPTION_FORMAT, &format) != 0)
       format = cct->_conf->rbd_default_format;
     bool old_format = format == 1;
 
-
     // make sure it doesn't already exist, in either format
-    int r = detect_format(io_ctx, imgname, NULL, NULL);
+    int r = detect_format(io_ctx, image_name, NULL, NULL);
     if (r != -ENOENT) {
       if (r) {
-       lderr(cct) << "Could not tell if " << imgname << " already exists" << dendl;
+       lderr(cct) << "Could not tell if " << image_name << " already exists"
+                  << dendl;
        return r;
       }
-      lderr(cct) << "rbd image " << imgname << " already exists" << dendl;
+      lderr(cct) << "rbd image " << image_name << " already exists" << dendl;
       return -EEXIST;
     }
 
@@ -863,16 +870,15 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
     }
 
     if (old_format) {
-      r = create_v1(io_ctx, imgname, size, order);
+      r = create_v1(io_ctx, image_name.c_str(), size, order);
     } else {
       ThreadPool *thread_pool;
       ContextWQ *op_work_queue;
       ImageCtx::get_thread_pool_instance(cct, &thread_pool, &op_work_queue);
 
       C_SaferCond cond;
-      std::string id = util::generate_image_id(io_ctx);
       image::CreateRequest<> *req = image::CreateRequest<>::create(
-        io_ctx, imgname, id, size, opts, non_primary_global_image_id,
+        io_ctx, image_name, id, size, opts, non_primary_global_image_id,
         primary_mirror_uuid, skip_mirror_enable, op_work_queue, &cond);
       req->send();
 
@@ -925,7 +931,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
       return r;
     }
 
-    r = clone(p_imctx, c_ioctx, c_name, c_opts, "", "");
+    r = clone(p_imctx, c_ioctx, c_name, "", c_opts, "", "");
 
     int close_r = p_imctx->state->close();
     if (r == 0 && close_r < 0) {
@@ -938,12 +944,21 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
     return 0;
   }
 
-  int clone(ImageCtx *p_imctx, IoCtx& c_ioctx, const char *c_name,
-            ImageOptions& c_opts,
+  int clone(ImageCtx *p_imctx, IoCtx& c_ioctx, const std::string &c_name,
+            const std::string &c_id, ImageOptions& c_opts,
             const std::string &non_primary_global_image_id,
             const std::string &primary_mirror_uuid)
   {
+    std::string id(c_id);
+    if (id.empty()) {
+      id = util::generate_image_id(c_ioctx);
+    }
+
     CephContext *cct = (CephContext *)c_ioctx.cct();
+    ldout(cct, 10) << __func__ << " "
+                  << "c_name=" << c_name << ", "
+                  << "c_id= " << c_id << ", "
+                  << "c_opts=" << c_opts << dendl;
 
     ThreadPool *thread_pool;
     ContextWQ *op_work_queue;
@@ -951,8 +966,8 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
 
     C_SaferCond cond;
     auto *req = image::CloneRequest<>::create(
-      p_imctx, c_ioctx, std::string(c_name), c_opts, non_primary_global_image_id,
-      primary_mirror_uuid, op_work_queue, &cond);
+      p_imctx, c_ioctx, c_name, id, c_opts,
+      non_primary_global_image_id, primary_mirror_uuid, op_work_queue, &cond);
     req->send();
 
     return cond.wait();
@@ -1703,7 +1718,7 @@ int validate_pool(IoCtx &io_ctx, CephContext *cct) {
       return -ENOSYS;
     }
 
-    int r = create(dest_md_ctx, destname, src_size, opts, "", "", false);
+    int r = create(dest_md_ctx, destname, "", src_size, opts, "", "", false);
     if (r < 0) {
       lderr(cct) << "header creation failed" << dendl;
       return r;
index ebf703dc78693de10f0bac9bf7791b6ad37ecacc..59c9fad926a3a93c948ff707443ff60275ef8f1a 100644 (file)
@@ -106,8 +106,8 @@ namespace librbd {
   int create(librados::IoCtx& io_ctx, const char *imgname, uint64_t size,
             bool old_format, uint64_t features, int *order,
             uint64_t stripe_unit, uint64_t stripe_count);
-  int create(IoCtx& io_ctx, const char *imgname, uint64_t size,
-            ImageOptions& opts,
+  int create(IoCtx& io_ctx, const std::string &image_name,
+            const std::string &image_id, uint64_t size, ImageOptions& opts,
              const std::string &non_primary_global_image_id,
              const std::string &primary_mirror_uuid,
              bool skip_mirror_enable);
@@ -117,8 +117,8 @@ namespace librbd {
            uint64_t stripe_unit, int stripe_count);
   int clone(IoCtx& p_ioctx, const char *p_name, const char *p_snap_name,
            IoCtx& c_ioctx, const char *c_name, ImageOptions& c_opts);
-  int clone(ImageCtx *p_imctx, IoCtx& c_ioctx, const char *c_name,
-            ImageOptions& c_opts,
+  int clone(ImageCtx *p_imctx, IoCtx& c_ioctx, const std::string &c_name,
+            const std::string &c_id, ImageOptions& c_opts,
             const std::string &non_primary_global_image_id,
             const std::string &primary_mirror_uuid);
   int rename(librados::IoCtx& io_ctx, const char *srcname, const char *dstname);
index 516189102e661e829fc7d0aa728f3e3692928ff2..5a4bf51d5ba33b224b62e63a10b065d12a2dceb4 100644 (file)
@@ -481,7 +481,7 @@ namespace librbd {
   {
     TracepointProvider::initialize<tracepoint_traits>(get_cct(io_ctx));
     tracepoint(librbd, create4_enter, io_ctx.get_pool_name().c_str(), io_ctx.get_id(), name, size, opts.opts);
-    int r = librbd::create(io_ctx, name, size, opts, "", "", false);
+    int r = librbd::create(io_ctx, name, "", size, opts, "", "", false);
     tracepoint(librbd, create4_exit, r);
     return r;
   }
@@ -2195,7 +2195,7 @@ extern "C" int rbd_create4(rados_ioctx_t p, const char *name,
   TracepointProvider::initialize<tracepoint_traits>(get_cct(io_ctx));
   tracepoint(librbd, create4_enter, io_ctx.get_pool_name().c_str(), io_ctx.get_id(), name, size, opts);
   librbd::ImageOptions opts_(opts);
-  int r = librbd::create(io_ctx, name, size, opts_, "", "", false);
+  int r = librbd::create(io_ctx, name, "", size, opts_, "", "", false);
   tracepoint(librbd, create4_exit, r);
   return r;
 }
index 1aed64600e076cec9f4e9e80ac635a2cbaa3ca2f..1cfff1879da472f12057ac0436a603cce2998287 100644 (file)
@@ -74,7 +74,7 @@ struct CloneRequest<librbd::MockTestImageCtx> {
 
   static CloneRequest *create(librbd::MockTestImageCtx *p_imctx,
                              IoCtx &c_ioctx, const std::string &c_name,
-                             ImageOptions c_options,
+                             const std::string &c_id, ImageOptions c_options,
                              const std::string &non_primary_global_image_id,
                              const std::string &primary_mirror_uuid,
                              MockContextWQ *op_work_queue, Context *on_finish) {
index 78a51c6575c917cbba11a358b4bb93bc523d5097..2ddf80b5dcad6ba9bcad7d53b4225eb4b94ca340 100644 (file)
@@ -289,11 +289,13 @@ void CreateImageRequest<I>::clone_image() {
   opts.set(RBD_IMAGE_OPTION_STRIPE_UNIT, m_remote_image_ctx->stripe_unit);
   opts.set(RBD_IMAGE_OPTION_STRIPE_COUNT, m_remote_image_ctx->stripe_count);
 
+  std::string id = librbd::util::generate_image_id(m_local_io_ctx);
+
   using klass = CreateImageRequest<I>;
   Context *ctx = create_context_callback<klass, &klass::handle_clone_image>(this);
 
   librbd::image::CloneRequest<I> *req = librbd::image::CloneRequest<I>::create(
-    m_local_parent_image_ctx, m_local_io_ctx, m_local_image_name, opts,
+    m_local_parent_image_ctx, m_local_io_ctx, m_local_image_name, id, opts,
     m_global_image_id, m_remote_mirror_uuid,
     m_remote_image_ctx->op_work_queue, ctx);
   req->send();