]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
rbd: add new librbd method to assert namespace exists
authorsongweibin <song.weibin@zte.com.cn>
Thu, 30 Aug 2018 10:17:02 +0000 (18:17 +0800)
committersongweibin <song.weibin@zte.com.cn>
Thu, 30 Aug 2018 10:18:14 +0000 (18:18 +0800)
Signed-off-by: songweibin <song.weibin@zte.com.cn>
src/include/rbd/librbd.h
src/include/rbd/librbd.hpp
src/librbd/api/Namespace.cc
src/librbd/api/Namespace.h
src/librbd/librbd.cc
src/test/librbd/test_librbd.cc

index 66d5444e943ac41574fd9f2bab5245b94aa5f4a7..85e9636f426564db35cac10fd3b9465c7142c12e 100644 (file)
@@ -1045,6 +1045,8 @@ CEPH_RBD_API int rbd_namespace_remove(rados_ioctx_t io,
                                       const char *namespace_name);
 CEPH_RBD_API int rbd_namespace_list(rados_ioctx_t io, char *namespace_names,
                                     size_t *size);
+CEPH_RBD_API int rbd_namespace_exists(rados_ioctx_t io, const char *namespace_name,
+                                      bool *exists);
 
 #ifdef __cplusplus
 }
index c6270d33109ad85f8376a6ea1c060d5b13e21a36..38cc707a31f031aa19cd7c2c6fd6e0be8bc6c1f4 100644 (file)
@@ -271,6 +271,7 @@ public:
   int namespace_create(IoCtx& ioctx, const char *namespace_name);
   int namespace_remove(IoCtx& ioctx, const char *namespace_name);
   int namespace_list(IoCtx& io_ctx, std::vector<std::string>* namespace_names);
+  int namespace_exists(IoCtx& io_ctx, const char *namespace_name, bool *exists);
 
 private:
   /* We don't allow assignment or copying */
index f73ae845486c2768f09d3363797eb8b385b3c15f..040adb8706197b82afdebd3e19a37a9ca91d3d7d 100644 (file)
@@ -155,6 +155,34 @@ int Namespace<I>::list(IoCtx& io_ctx, vector<string> *names)
   return 0;
 }
 
+template <typename I>
+int Namespace<I>::exists(librados::IoCtx& io_ctx, const std::string& name, bool *exists)
+{
+  CephContext *cct = (CephContext *)io_ctx.cct();
+  ldout(cct, 5) << "name=" << name << dendl;
+
+  if (name.empty()) {
+    return -EINVAL;
+  }
+
+  librados::IoCtx ns_ctx;
+  ns_ctx.dup(io_ctx);
+  ns_ctx.set_namespace(name);
+
+  int r = librbd::cls_client::dir_state_assert(&ns_ctx, RBD_DIRECTORY,
+                                               cls::rbd::DIRECTORY_STATE_READY);
+  if (r == 0) {
+    *exists = true;
+  } else if (r == -ENOENT) {
+    *exists = false;
+  } else {
+    lderr(cct) << "error asserting namespace: " << cpp_strerror(r) << dendl;
+    return r;
+  }
+
+  return 0;
+}
+
 } // namespace api
 } // namespace librbd
 
index c892a3bc11ef169cd4bf96b48ff275b0f1161c2f..e26cb687be17000af7704dabbfafb6dbe77f3706 100644 (file)
@@ -22,6 +22,7 @@ struct Namespace {
   static int create(librados::IoCtx& io_ctx, const std::string& name);
   static int remove(librados::IoCtx& io_ctx, const std::string& name);
   static int list(librados::IoCtx& io_ctx, std::vector<std::string>* names);
+  static int exists(librados::IoCtx& io_ctx, const std::string& name, bool *exists);
 
 };
 
index 9616be97f8386460bc292ed548168e1c031060b4..785259afb260ec0595482cac80faafcea241a1de 100644 (file)
@@ -630,6 +630,11 @@ namespace librbd {
     return librbd::api::Namespace<>::list(io_ctx, namespace_names);
   }
 
+  int RBD::namespace_exists(IoCtx& io_ctx, const char *namespace_name,
+                            bool *exists) {
+    return librbd::api::Namespace<>::exists(io_ctx, namespace_name, exists);
+  }
+
   int RBD::list(IoCtx& io_ctx, vector<string>& names)
   {
     TracepointProvider::initialize<tracepoint_traits>(get_cct(io_ctx));
@@ -2899,6 +2904,15 @@ extern "C" int rbd_namespace_list(rados_ioctx_t io, char *names, size_t *size) {
   return (int)expected_size;
 }
 
+extern "C" int rbd_namespace_exists(rados_ioctx_t io,
+                                    const char *namespace_name,
+                                    bool *exists) {
+  librados::IoCtx io_ctx;
+  librados::IoCtx::from_rados_ioctx_t(io, io_ctx);
+
+  return librbd::api::Namespace<>::exists(io_ctx, namespace_name, exists);
+}
+
 extern "C" int rbd_copy(rbd_image_t image, rados_ioctx_t dest_p,
                        const char *destname)
 {
index 6d33b28393bcda907eb82ff838952a5196c8b61a..c882a4f85f686c2670c55aed64ec3d8434f6f96c 100644 (file)
@@ -6752,6 +6752,11 @@ TEST_F(TestLibRBD, Namespaces) {
   ASSERT_EQ(2U, cpp_names.size());
   ASSERT_EQ("name1", cpp_names[0]);
   ASSERT_EQ("name3", cpp_names[1]);
+  bool exists;
+  ASSERT_EQ(0, rbd_namespace_exists(ioctx, "name2", &exists));
+  ASSERT_FALSE(exists);
+  ASSERT_EQ(0, rbd_namespace_exists(ioctx, "name3", &exists));
+  ASSERT_TRUE(exists);
   rados_ioctx_destroy(ioctx);
 }
 
@@ -6776,6 +6781,11 @@ TEST_F(TestLibRBD, NamespacesPP) {
   ASSERT_EQ(2U, names.size());
   ASSERT_EQ("name1", names[0]);
   ASSERT_EQ("name3", names[1]);
+  bool exists;
+  ASSERT_EQ(0, rbd.namespace_exists(ioctx, "name2", &exists));
+  ASSERT_FALSE(exists);
+  ASSERT_EQ(0, rbd.namespace_exists(ioctx, "name3", &exists));
+  ASSERT_TRUE(exists);
 
   librados::IoCtx ns_io_ctx;
   ns_io_ctx.dup(ioctx);