22// vim: ts=8 sw=2 smarttab
33
44#include " librbd/migration/NativeFormat.h"
5- #include " include/neorados/RADOS.hpp"
65#include " common/dout.h"
76#include " common/errno.h"
87#include " librbd/ImageCtx.h"
98#include " librbd/ImageState.h"
10- #include " librbd/Utils.h"
11- #include " librbd/asio/ContextWQ.h"
12- #include " librbd/io/ImageDispatchSpec.h"
139#include " json_spirit/json_spirit.h"
1410#include " boost/lexical_cast.hpp"
15- #include < sstream>
1611
1712#define dout_subsys ceph_subsys_rbd
1813#undef dout_prefix
@@ -65,22 +60,19 @@ int NativeFormat<I>::create_image_ctx(
6560 const json_spirit::mObject & source_spec_object,
6661 bool import_only, uint64_t src_snap_id, I** src_image_ctx) {
6762 auto cct = reinterpret_cast <CephContext*>(dst_io_ctx.cct ());
63+ std::string pool_name;
6864 int64_t pool_id = -1 ;
6965 std::string pool_namespace;
7066 std::string image_name;
7167 std::string image_id;
7268 std::string snap_name;
7369 uint64_t snap_id = CEPH_NOSNAP;
70+ int r;
7471
7572 if (auto it = source_spec_object.find (POOL_NAME_KEY);
7673 it != source_spec_object.end ()) {
7774 if (it->second .type () == json_spirit::str_type) {
78- librados::Rados rados (dst_io_ctx);
79- pool_id = rados.pool_lookup (it->second .get_str ().c_str ());
80- if (pool_id < 0 ) {
81- lderr (cct) << " failed to lookup pool" << dendl;
82- return static_cast <int >(pool_id);
83- }
75+ pool_name = it->second .get_str ();
8476 } else {
8577 lderr (cct) << " invalid pool name" << dendl;
8678 return -EINVAL;
@@ -89,7 +81,7 @@ int NativeFormat<I>::create_image_ctx(
8981
9082 if (auto it = source_spec_object.find (POOL_ID_KEY);
9183 it != source_spec_object.end ()) {
92- if (pool_id != - 1 ) {
84+ if (!pool_name. empty () ) {
9385 lderr (cct) << " cannot specify both pool name and pool id" << dendl;
9486 return -EINVAL;
9587 }
@@ -107,7 +99,7 @@ int NativeFormat<I>::create_image_ctx(
10799 }
108100 }
109101
110- if (pool_id == -1 ) {
102+ if (pool_name. empty () && pool_id == -1 ) {
111103 lderr (cct) << " missing pool name or pool id" << dendl;
112104 return -EINVAL;
113105 }
@@ -177,7 +169,7 @@ int NativeFormat<I>::create_image_ctx(
177169
178170 // snapshot is required for import to keep source read-only
179171 if (import_only && snap_name.empty () && snap_id == CEPH_NOSNAP) {
180- lderr (cct) << " snapshot required for import" << dendl;
172+ lderr (cct) << " snap name or snap id required for import" << dendl;
181173 return -EINVAL;
182174 }
183175
@@ -190,12 +182,19 @@ int NativeFormat<I>::create_image_ctx(
190182
191183 // TODO add support for external clusters
192184 librados::IoCtx src_io_ctx;
193- int r = util::create_ioctx (dst_io_ctx, " source image" , pool_id,
194- pool_namespace, &src_io_ctx);
185+ if (!pool_name.empty ()) {
186+ r = rados_ptr->ioctx_create (pool_name.c_str (), src_io_ctx);
187+ } else {
188+ r = rados_ptr->ioctx_create2 (pool_id, src_io_ctx);
189+ }
195190 if (r < 0 ) {
191+ lderr (cct) << " failed to open source image pool: " << cpp_strerror (r)
192+ << dendl;
196193 return r;
197194 }
198195
196+ src_io_ctx.set_namespace (pool_namespace);
197+
199198 if (!snap_name.empty () && snap_id == CEPH_NOSNAP) {
200199 *src_image_ctx = I::create (image_name, image_id, snap_name.c_str (),
201200 src_io_ctx, true );
0 commit comments