]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
librbd: moved new snapshot API methods to separate class
authorJason Dillaman <dillaman@redhat.com>
Thu, 11 Jan 2018 01:18:18 +0000 (20:18 -0500)
committerJason Dillaman <dillaman@redhat.com>
Thu, 11 Jan 2018 15:38:23 +0000 (10:38 -0500)
Signed-off-by: Jason Dillaman <dillaman@redhat.com>
src/cls/rbd/cls_rbd_types.cc
src/cls/rbd/cls_rbd_types.h
src/librbd/CMakeLists.txt
src/librbd/api/Group.cc
src/librbd/api/Group.h
src/librbd/api/Snapshot.cc [new file with mode: 0644]
src/librbd/api/Snapshot.h [new file with mode: 0644]
src/librbd/librbd.cc

index fcd31863565bee9049d440b85226ce82fcc5f569..cbd1aa7d16237927bee1236225402e8806688039 100644 (file)
@@ -406,9 +406,10 @@ public:
   }
 };
 
-SnapshotNamespaceType get_namespace_type(const SnapshotNamespace& snapshot_namespace) {
-  return static_cast<SnapshotNamespaceType>(boost::apply_visitor(GetTypeVisitor(),
-                                                                snapshot_namespace));
+SnapshotNamespaceType get_snap_namespace_type(
+    const SnapshotNamespace& snapshot_namespace) {
+  return static_cast<SnapshotNamespaceType>(boost::apply_visitor(
+    GetTypeVisitor(), snapshot_namespace));
 }
 
 void SnapshotNamespaceOnDisk::encode(bufferlist& bl) const {
index 5fd6e6f764039cff288d84498b72065488cb6e1e..5d224a851d96a8d9fbcd7bf3414daeaba4cd8c84 100644 (file)
@@ -336,7 +336,7 @@ struct SnapshotNamespaceOnDisk {
 };
 WRITE_CLASS_ENCODER(SnapshotNamespaceOnDisk);
 
-SnapshotNamespaceType get_namespace_type(const SnapshotNamespace& snapshot_namespace);
+SnapshotNamespaceType get_snap_namespace_type(const SnapshotNamespace& snapshot_namespace);
 
 enum GroupSnapshotState {
   GROUP_SNAPSHOT_STATE_INCOMPLETE = 0,
index 72c7f4b11148d76111cd23174478640f59f6f5da..a3e27d1cb11898a4eec42a5893d4476cf869bff8 100644 (file)
@@ -28,6 +28,7 @@ set(librbd_internal_srcs
   api/Group.cc
   api/Image.cc
   api/Mirror.cc
+  api/Snapshot.cc
   cache/ImageWriteback.cc
   cache/PassthroughImageCache.cc
   deep_copy/ImageCopyRequest.cc
index 161a9240c0b523e6622bc673a5657407a53d90db..48c31dcb71d341de9aaaf03d91c6f9defe956d3f 100644 (file)
@@ -299,54 +299,6 @@ finish:
   return ret_code;
 }
 
-class GetGroupVisitor : public boost::static_visitor<int> {
-public:
-  librados::IoCtx *image_ioctx;
-  group_snap_t group_snap;
-
-  explicit GetGroupVisitor(librados::IoCtx *_image_ioctx) : image_ioctx(_image_ioctx) {};
-
-  template <typename T>
-  inline int operator()(const T&) const {
-    // ignore other than GroupSnapshotNamespace types.
-    return -1;
-  }
-
-  inline int operator()(const cls::rbd::GroupSnapshotNamespace& snap_namespace) {
-    librados::Rados rados(*image_ioctx);
-    IoCtx group_ioctx;
-    int r = rados.ioctx_create2(snap_namespace.group_pool, group_ioctx);
-    if (r < 0) {
-      return r;
-    }
-
-    cls::rbd::GroupSnapshot group_snapshot;
-
-    std::string group_name;
-    r = cls_client::dir_get_name(&group_ioctx, RBD_GROUP_DIRECTORY,
-                                snap_namespace.group_id, &group_name);
-    if (r < 0) {
-      return r;
-    }
-
-    string group_header_oid = util::group_header_name(snap_namespace.group_id);
-
-    r = cls_client::group_snap_get_by_id(&group_ioctx,
-                                        group_header_oid,
-                                        snap_namespace.group_snapshot_id,
-                                        &group_snapshot);
-    if (r < 0) {
-      return r;
-    }
-
-    group_snap.group_pool = group_ioctx.get_id();
-    group_snap.group_name = group_name;
-    group_snap.group_snap_name = group_snapshot.name;
-
-    return 0;
-  }
-};
-
 } // anonymous namespace
 
 template <typename I>
@@ -990,43 +942,6 @@ int Group<I>::snap_list(librados::IoCtx& group_ioctx, const char *group_name,
   return 0;
 }
 
-template <typename I>
-int Group<I>::snap_get_group(I *ictx,
-                            uint64_t snap_id,
-                            group_snap_t *group_snap) {
-    const SnapInfo *snap_info;
-    {
-      RWLock::RLocker l(ictx->snap_lock);
-      snap_info = ictx->get_snap_info(snap_id);
-      if (snap_info) {
-       GetGroupVisitor ggv = GetGroupVisitor(&ictx->data_ctx);
-       int r = boost::apply_visitor(ggv, snap_info->snap_namespace);
-       if (r < 0) {
-         return r;
-       }
-       *group_snap = ggv.group_snap;
-      }
-    }
-    return 0;
-}
-
-template <typename I>
-int Group<I>::snap_get_namespace_type(I *ictx,
-                                     uint64_t snap_id,
-                                     snap_namespace_type_t *namespace_type) {
-  const SnapInfo *snap_info;
-  {
-    RWLock::RLocker l(ictx->snap_lock);
-    snap_info = ictx->get_snap_info(snap_id);
-    if (snap_info) {
-      *namespace_type = static_cast<snap_namespace_type_t>(
-                             get_namespace_type(snap_info->snap_namespace));
-    }
-  }
-  return 0;
-}
-
-
 } // namespace api
 } // namespace librbd
 
index 6c17b29582e2f2526d8153d5e3305fb47e46f153..58f8ad556b62d4963d6a4ed93441593c8dc50db4 100644 (file)
@@ -42,13 +42,7 @@ struct Group {
                               const char *group_name, const char *snap_name);
   static int snap_list(librados::IoCtx& group_ioctx, const char *group_name,
                             std::vector<group_snap_spec_t> *snaps);
-  static int snap_get_group(ImageCtxT *ictx,
-                           uint64_t snap_id,
-                           group_snap_t *group_snap);
 
-  static int snap_get_namespace_type(ImageCtxT *ictx,
-                                    uint64_t snap_id,
-                                    snap_namespace_type_t *namespace_type);
 };
 
 } // namespace api
diff --git a/src/librbd/api/Snapshot.cc b/src/librbd/api/Snapshot.cc
new file mode 100644 (file)
index 0000000..aebb8d6
--- /dev/null
@@ -0,0 +1,113 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#include "librbd/api/Snapshot.h"
+#include "cls/rbd/cls_rbd_types.h"
+#include "common/errno.h"
+#include "librbd/ImageCtx.h"
+#include "librbd/Utils.h"
+#include <boost/variant.hpp>
+
+#define dout_subsys ceph_subsys_rbd
+#undef dout_prefix
+#define dout_prefix *_dout << "librbd::api::Snapshot: " << __func__ << ": "
+
+namespace librbd {
+namespace api {
+
+namespace {
+
+class GetGroupVisitor : public boost::static_visitor<int> {
+public:
+  CephContext* cct;
+  librados::IoCtx *image_ioctx;
+  group_snap_t group_snap;
+
+  explicit GetGroupVisitor(CephContext* cct, librados::IoCtx *_image_ioctx)
+    : cct(cct), image_ioctx(_image_ioctx) {};
+
+  template <typename T>
+  inline int operator()(const T&) const {
+    // ignore other than GroupSnapshotNamespace types.
+    return -EINVAL;
+  }
+
+  inline int operator()(
+      const cls::rbd::GroupSnapshotNamespace& snap_namespace) {
+    librados::Rados rados(*image_ioctx);
+    IoCtx group_ioctx;
+    int r = rados.ioctx_create2(snap_namespace.group_pool, group_ioctx);
+    if (r < 0) {
+      lderr(cct) << "failed to open group pool: " << cpp_strerror(r) << dendl;
+      return r;
+    }
+
+    cls::rbd::GroupSnapshot group_snapshot;
+
+    std::string group_name;
+    r = cls_client::dir_get_name(&group_ioctx, RBD_GROUP_DIRECTORY,
+                                snap_namespace.group_id, &group_name);
+    if (r < 0) {
+      lderr(cct) << "failed to retrieve group name: " << cpp_strerror(r)
+                 << dendl;
+      return r;
+    }
+
+    string group_header_oid = util::group_header_name(snap_namespace.group_id);
+    r = cls_client::group_snap_get_by_id(&group_ioctx,
+                                        group_header_oid,
+                                        snap_namespace.group_snapshot_id,
+                                        &group_snapshot);
+    if (r < 0) {
+      lderr(cct) << "failed to retrieve group snapshot: " << cpp_strerror(r)
+                 << dendl;
+      return r;
+    }
+
+    group_snap.group_pool = group_ioctx.get_id();
+    group_snap.group_name = group_name;
+    group_snap.group_snap_name = group_snapshot.name;
+    return 0;
+  }
+};
+
+} // anonymous namespace
+
+template <typename I>
+int Snapshot<I>::get_group(I *ictx, uint64_t snap_id,
+                           group_snap_t *group_snap) {
+  const SnapInfo *snap_info;
+  {
+    RWLock::RLocker snap_locker(ictx->snap_lock);
+    snap_info = ictx->get_snap_info(snap_id);
+    if (snap_info) {
+      GetGroupVisitor ggv = GetGroupVisitor(ictx->cct, &ictx->data_ctx);
+      int r = boost::apply_visitor(ggv, snap_info->snap_namespace);
+      if (r < 0) {
+       return r;
+      }
+      *group_snap = ggv.group_snap;
+    }
+  }
+  return 0;
+}
+
+template <typename I>
+int Snapshot<I>::get_namespace_type(I *ictx, uint64_t snap_id,
+                                snap_namespace_type_t *namespace_type) {
+  const SnapInfo *snap_info;
+  {
+    RWLock::RLocker l(ictx->snap_lock);
+    snap_info = ictx->get_snap_info(snap_id);
+    if (snap_info) {
+      *namespace_type = static_cast<snap_namespace_type_t>(
+        cls::rbd::get_snap_namespace_type(snap_info->snap_namespace));
+    }
+  }
+  return 0;
+}
+
+} // namespace api
+} // namespace librbd
+
+template class librbd::api::Snapshot<librbd::ImageCtx>;
diff --git a/src/librbd/api/Snapshot.h b/src/librbd/api/Snapshot.h
new file mode 100644 (file)
index 0000000..83b1b30
--- /dev/null
@@ -0,0 +1,31 @@
+// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*-
+// vim: ts=8 sw=2 smarttab
+
+#ifndef CEPH_LIBRBD_API_SNAPSHOT_H
+#define CEPH_LIBRBD_API_SNAPSHOT_H
+
+#include "include/rbd/librbd.hpp"
+
+namespace librbd {
+
+struct ImageCtx;
+
+namespace api {
+
+template <typename ImageCtxT = librbd::ImageCtx>
+struct Snapshot {
+
+  static int get_group(ImageCtxT *ictx, uint64_t snap_id,
+                      group_snap_t *group_snap);
+
+  static int get_namespace_type(ImageCtxT *ictx, uint64_t snap_id,
+                               snap_namespace_type_t *namespace_type);
+
+};
+
+} // namespace api
+} // namespace librbd
+
+extern template class librbd::api::Snapshot<librbd::ImageCtx>;
+
+#endif // CEPH_LIBRBD_API_SNAPSHOT_H
index ebdf6d62452f4eb7570984e0d4fe088df3cd88ba..0097639fd5d5053d13ce10eced4a2b7cbaa6cdb0 100644 (file)
@@ -31,6 +31,7 @@
 #include "librbd/api/Group.h"
 #include "librbd/api/Image.h"
 #include "librbd/api/Mirror.h"
+#include "librbd/api/Snapshot.h"
 #include "librbd/io/AioCompletion.h"
 #include "librbd/io/ImageRequestWQ.h"
 #include "librbd/io/ReadResult.h"
@@ -1564,7 +1565,7 @@ namespace librbd {
                                     snap_namespace_type_t *namespace_type) {
     ImageCtx *ictx = (ImageCtx *)ctx;
     tracepoint(librbd, snap_get_namespace_type_enter, ictx, ictx->name.c_str());
-    int r = librbd::api::Group<>::snap_get_namespace_type(ictx, snap_id, namespace_type);
+    int r = librbd::api::Snapshot<>::get_namespace_type(ictx, snap_id, namespace_type);
     tracepoint(librbd, snap_get_namespace_type_exit, r);
     return r;
   }
@@ -1573,7 +1574,7 @@ namespace librbd {
                            group_snap_t *group_snap) {
     ImageCtx *ictx = (ImageCtx *)ctx;
     tracepoint(librbd, snap_get_group_enter, ictx, ictx->name.c_str());
-    int r = librbd::api::Group<>::snap_get_group(ictx, snap_id, group_snap);
+    int r = librbd::api::Snapshot<>::get_group(ictx, snap_id, group_snap);
     tracepoint(librbd, snap_get_group_exit, r);
     return r;
   }
@@ -4652,10 +4653,12 @@ extern "C" int rbd_snap_get_namespace_type(rbd_image_t image,
                                           rbd_snap_namespace_type_t *namespace_type) {
   librbd::ImageCtx *ictx = (librbd::ImageCtx *)image;
   tracepoint(librbd, snap_get_namespace_type_enter, ictx, ictx->name.c_str());
-  int r = librbd::api::Group<>::snap_get_namespace_type(ictx, snap_id, namespace_type);
+  int r = librbd::api::Snapshot<>::get_namespace_type(ictx, snap_id,
+                                                      namespace_type);
   tracepoint(librbd, snap_get_namespace_type_exit, r);
   return r;
 }
+
 extern "C" int rbd_watchers_list(rbd_image_t image,
                                 rbd_image_watcher_t *watchers,
                                 size_t *max_watchers) {