]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
add snapshot rename CLI
authorxinxin shu <xinxin.shu@intel.com>
Fri, 21 Aug 2015 07:11:50 +0000 (15:11 +0800)
committerxinxin shu <xinxin.shu@intel.com>
Wed, 2 Sep 2015 23:52:28 +0000 (07:52 +0800)
Signed-off-by: xinxin shu <xinxin.shu@intel.com>
src/include/rbd/librbd.h
src/include/rbd/librbd.hpp
src/librbd/ImageCtx.cc
src/librbd/ImageWatcher.cc
src/librbd/ImageWatcher.h
src/librbd/internal.cc
src/librbd/internal.h
src/librbd/librbd.cc
src/rbd.cc
src/tracing/librbd.tp

index 690dbbd507442c229487604c2e4d4d58531e6737..2eb3aa5e9542bfcbc68d09a19d5695ef82b9869b 100644 (file)
@@ -189,6 +189,8 @@ CEPH_RBD_API int rbd_snap_rollback_with_progress(rbd_image_t image,
                                                  const char *snapname,
                                                 librbd_progress_fn_t cb,
                                                  void *cbdata);
+CEPH_RBD_API int rbd_snap_rename(rbd_image_t image, const char *snapname,
+                                const char* dstsnapsname);
 /**
  * Prevent a snapshot from being deleted until it is unprotected.
  *
index e5879164f28c1eee45e702a0a82818f90f625837..0c41b8e184a61175ae7855ba7c184cd050d7b696 100644 (file)
@@ -165,6 +165,7 @@ public:
   int snap_unprotect(const char *snap_name);
   int snap_is_protected(const char *snap_name, bool *is_protected);
   int snap_set(const char *snap_name);
+  int snap_rename(const char *srcname, const char *dstname);
 
   /* I/O */
   ssize_t read(uint64_t ofs, size_t len, ceph::bufferlist& bl);
index 3e860322c991aca8ac3052173442425844e58cfc..5682e7ec7172815f001e85ee4aa98e608ee48bf9 100644 (file)
@@ -288,6 +288,7 @@ public:
     plb.add_u64_counter(l_librbd_snap_create, "snap_create", "Snap creations");
     plb.add_u64_counter(l_librbd_snap_remove, "snap_remove", "Snap removals");
     plb.add_u64_counter(l_librbd_snap_rollback, "snap_rollback", "Snap rollbacks");
+    plb.add_u64_counter(l_librbd_snap_rename, "snap_rename", "Snap rename");
     plb.add_u64_counter(l_librbd_notify, "notify", "Updated header notifications");
     plb.add_u64_counter(l_librbd_resize, "resize", "Resizes");
     plb.add_u64_counter(l_librbd_readahead, "readahead", "Read ahead");
index 49d6a34f90e7e09810fad0ed00772d0e63677b25..5ac79f2b230fc262f98864570a54785580d7a93d 100644 (file)
@@ -496,6 +496,16 @@ int ImageWatcher::notify_snap_create(const std::string &snap_name) {
   return notify_lock_owner(bl);
 }
 
+int ImageWatcher::notify_snap_rename(const snapid_t &src_snap_id,
+                                    const std::string &dst_snap_name) {
+  assert(m_image_ctx.owner_lock.is_locked());
+  assert(!is_lock_owner());
+
+  bufferlist bl;
+  ::encode(NotifyMessage(SnapRenamePayload(src_snap_id, dst_snap_name)), bl);
+
+  return notify_lock_owner(bl);
+}
 int ImageWatcher::notify_snap_remove(const std::string &snap_name) {
   assert(m_image_ctx.owner_lock.is_locked());
   assert(!is_lock_owner());
index 29645b48778f82573064dd1944568d3e393a7505..9099e1685d028939298af5f1a820cd7a951b3cfc 100644 (file)
@@ -50,6 +50,7 @@ namespace librbd {
     int notify_resize(uint64_t request_id, uint64_t size,
                      ProgressContext &prog_ctx);
     int notify_snap_create(const std::string &snap_name);
+    int notify_snap_rename(const snapid_t &src_snap_id, const std::string &dst_snap_name);
     int notify_snap_remove(const std::string &snap_name);
     int notify_rebuild_object_map(uint64_t request_id,
                                   ProgressContext &prog_ctx);
index 9f4b2ad4a818fdb6ec538e0ca5d844398777d47a..fce3d0608e52cfdb76d0fd65e4ac52358c8f24c9 100644 (file)
@@ -644,6 +644,45 @@ int invoke_async_request(ImageCtx *ictx, const std::string& request_type,
     return 0;
   }
 
+  int snap_rename(ImageCtx *ictx, const char *srcname, const char *dstname)
+  {
+    ldout(ictx->cct, 20) << "snap_rename " << ictx << " from " << srcname << " to " << dstname << dendl;
+
+    snapid_t snap_id;
+    if (ictx->read_only) {
+      return -EROFS;
+    }
+
+    int r = ictx_check(ictx);
+    if (r < 0)
+      return r;
+
+    {
+      RWLock::RLocker l(ictx->snap_lock);
+      snap_id = ictx->get_snap_id(srcname);
+      if (snap_id == CEPH_NOSNAP) {
+        return -ENOENT;
+      }
+      if (ictx->get_snap_id(dstname) != CEPH_NOSNAP) {
+        return -EEXIST;
+      }
+    }
+
+    r = invoke_async_request(ictx, "snap_rename", true,
+                             boost::bind(&snap_rename_helper, ictx, _1,
+                                         snap_id, dstname),
+                             boost::bind(&ImageWatcher::notify_snap_rename,
+                                         ictx->image_watcher, snap_id,
+                                        dstname));
+    if (r < 0 && r != -EEXIST) {
+      return r;
+    }
+
+    ictx->perfcounter->inc(l_librbd_snap_rename);
+    notify_change(ictx->md_ctx, ictx->header_oid, ictx);
+    return 0;
+  }
+
   int snap_rename_helper(ImageCtx* ictx, Context* ctx,
                          const uint64_t src_snap_id,
                          const char* dst_name) {
index 6ce385f679fff9d5a45bac956119e7cad7d2ae20..3d35ee0bc29a1b07442af46d8f8a505ccf93bac2 100644 (file)
@@ -34,6 +34,7 @@ enum {
   l_librbd_snap_create,
   l_librbd_snap_remove,
   l_librbd_snap_rollback,
+  l_librbd_snap_rename,
 
   l_librbd_notify,
   l_librbd_resize,
@@ -112,6 +113,7 @@ namespace librbd {
   int snap_remove_helper(ImageCtx *ictx, Context* ctx, const char *snap_name);
   int snap_rename_helper(ImageCtx *ictx, Context* ctx, const uint64_t src_snap_id,
                         const char *dst_name);
+  int snap_rename(ImageCtx *ictx, const char *srcname, const char *dstname);
   int snap_protect(ImageCtx *ictx, const char *snap_name);
   int snap_unprotect(ImageCtx *ictx, const char *snap_name);
   int snap_is_protected(ImageCtx *ictx, const char *snap_name,
index e802e9ce9e573ac3737a8f76359a9897f9c8a5d9..6ccabb4dcbfa607d0a512d078d88e2d43689e65e 100644 (file)
@@ -685,6 +685,15 @@ namespace librbd {
     return r;
   }
 
+  int Image::snap_rename(const char *srcname, const char *dstname)
+  {
+    ImageCtx *ictx = (ImageCtx *)ctx;
+    tracepoint(librbd, snap_rename_enter, ictx, ictx->name.c_str(), ictx->snap_name.c_str(), ictx->read_only, srcname, dstname);
+    int r = librbd::snap_rename(ictx, srcname, dstname);
+    tracepoint(librbd, snap_rename_exit, r);
+    return r;
+  }
+
   int Image::snap_rollback_with_progress(const char *snap_name,
                                         ProgressContext& prog_ctx)
   {
@@ -1446,6 +1455,15 @@ extern "C" int rbd_snap_create(rbd_image_t image, const char *snap_name)
   return r;
 }
 
+extern "C" int rbd_snap_rename(rbd_image_t image, const char *srcname, const char *dstname)
+{
+  librbd::ImageCtx *ictx = (librbd::ImageCtx *)image;
+  tracepoint(librbd, snap_rename_enter, ictx, ictx->name.c_str(), ictx->snap_name.c_str(), ictx->read_only, srcname, dstname);
+  int r = librbd::snap_rename(ictx, srcname, dstname);
+  tracepoint(librbd, snap_rename_exit, r);
+  return r;
+}
+
 extern "C" int rbd_snap_remove(rbd_image_t image, const char *snap_name)
 {
   librbd::ImageCtx *ictx = (librbd::ImageCtx *)image;
index f8330ff45f0c493de517adcc4f71f360f55a8362..33bee9e249428172ddda5428189eaf688adc417a 100755 (executable)
@@ -147,6 +147,7 @@ void usage()
 "  snap purge <image-spec>                     deletes all snapshots\n"
 "  snap protect <snap-spec>                    prevent a snapshot from being deleted\n"
 "  snap unprotect <snap-spec>                  allow a snapshot to be deleted\n"
+"  snap rename <src-snap-spec> <dst-snap-spec> rename a snapshot\n"
 "  watch <image-spec>                          watch events on image\n"
 "  status <image-spec>                         show the status of this image\n"
 "  map <image-spec> | <snap-spec>              map image to a block device\n"
@@ -768,6 +769,15 @@ static int do_rollback_snap(librbd::Image& image, const char *snapname)
   return 0;
 }
 
+static int do_rename_snap(librbd::Image& image, const char *srcname, const char *dstname)
+{
+  int r = image.snap_rename(srcname, dstname);
+  if (r < 0)
+    return r;
+
+  return 0;
+}
+
 static int do_purge_snaps(librbd::Image& image)
 {
   MyProgressContext pc("Removing all snapshots");
@@ -2884,6 +2894,7 @@ enum {
   OPT_SNAP_PURGE,
   OPT_SNAP_PROTECT,
   OPT_SNAP_UNPROTECT,
+  OPT_SNAP_RENAME,
   OPT_WATCH,
   OPT_STATUS,
   OPT_MAP,
@@ -2979,6 +2990,8 @@ static int get_cmd(const char *cmd, CommandType command_type)
       return OPT_SNAP_PROTECT;
     if (strcmp(cmd, "unprotect") == 0)
       return OPT_SNAP_UNPROTECT;
+    if (strcmp(cmd, "rename") == 0)
+      return OPT_SNAP_RENAME;
     break;
   case COMMAND_TYPE_METADATA:
     if (strcmp(cmd, "list") == 0)
@@ -3347,6 +3360,7 @@ if (!set_conf_param(v, p1, p2, p3)) { \
       case OPT_COPY:
       case OPT_RENAME:
       case OPT_CLONE:
+      case OPT_SNAP_RENAME:
        SET_CONF_PARAM(v, &imgname, &destname, NULL);
        break;
       case OPT_SHOWMAPPED:
@@ -3492,7 +3506,7 @@ if (!set_conf_param(v, p1, p2, p3)) { \
       opt_cmd != OPT_MAP && opt_cmd != OPT_UNMAP && opt_cmd != OPT_CLONE &&
       opt_cmd != OPT_SNAP_PROTECT && opt_cmd != OPT_SNAP_UNPROTECT &&
       opt_cmd != OPT_CHILDREN && opt_cmd != OPT_OBJECT_MAP_REBUILD &&
-      opt_cmd != OPT_DISK_USAGE) {
+      opt_cmd != OPT_DISK_USAGE && opt_cmd != OPT_SNAP_RENAME) {
     cerr << "rbd: snapname specified for a command that doesn't use it"
         << std::endl;
     return EXIT_FAILURE;
@@ -3500,19 +3514,23 @@ if (!set_conf_param(v, p1, p2, p3)) { \
   if ((opt_cmd == OPT_SNAP_CREATE || opt_cmd == OPT_SNAP_ROLLBACK ||
        opt_cmd == OPT_SNAP_REMOVE || opt_cmd == OPT_CLONE ||
        opt_cmd == OPT_SNAP_PROTECT || opt_cmd == OPT_SNAP_UNPROTECT ||
-       opt_cmd == OPT_CHILDREN) && !snapname) {
+       opt_cmd == OPT_CHILDREN || opt_cmd == OPT_SNAP_RENAME) && !snapname) {
     cerr << "rbd: snap name was not specified" << std::endl;
     return EXIT_FAILURE;
   }
 
   set_pool_image_name(destname, (char **)&dest_poolname,
                      (char **)&destname, (char **)&dest_snapname);
-  if (dest_snapname) {
+  if (dest_snapname && opt_cmd != OPT_SNAP_RENAME) {
     // no command uses dest_snapname
     cerr << "rbd: destination snapname specified for a command that doesn't use it"
          << std::endl;
     return EXIT_FAILURE;
   }
+  if (opt_cmd == OPT_SNAP_RENAME && !dest_snapname) {
+    cerr << "rbd: destination snap name was not specified" << std::endl;
+    return EXIT_FAILURE;
+  }
 
   if (opt_cmd == OPT_IMPORT) {
     if (poolname && dest_poolname) {
@@ -3651,7 +3669,8 @@ if (!set_conf_param(v, p1, p2, p3)) { \
        opt_cmd == OPT_METADATA_SET || opt_cmd == OPT_METADATA_LIST ||
        opt_cmd == OPT_METADATA_REMOVE || opt_cmd == OPT_METADATA_GET ||
        opt_cmd == OPT_FEATURE_DISABLE || opt_cmd == OPT_FEATURE_ENABLE ||
-       opt_cmd == OPT_OBJECT_MAP_REBUILD || opt_cmd == OPT_DISK_USAGE)) {
+       opt_cmd == OPT_OBJECT_MAP_REBUILD || opt_cmd == OPT_DISK_USAGE ||
+       opt_cmd == OPT_SNAP_RENAME)) {
 
     if (opt_cmd == OPT_INFO || opt_cmd == OPT_SNAP_LIST ||
        opt_cmd == OPT_EXPORT || opt_cmd == OPT_EXPORT || opt_cmd == OPT_COPY ||
@@ -3832,6 +3851,15 @@ if (!set_conf_param(v, p1, p2, p3)) { \
     }
     break;
 
+  case OPT_SNAP_RENAME:
+    r = do_rename_snap(image, snapname, dest_snapname);
+    if (r < 0) {
+      cerr << "rbd: failed to rename snapshot: " << cpp_strerror(-r)
+          << std::endl;
+      return -r;
+    }
+    break;
+
   case OPT_SNAP_ROLLBACK:
     r = do_rollback_snap(image, snapname);
     if (r < 0) {
index f9101bd92e3cdfcee155cda43f33c4bd6da77514..542084918e2fd03861b6ce4af62c36c93f62cff0 100644 (file)
@@ -966,6 +966,32 @@ TRACEPOINT_EVENT(librbd, snap_rollback_exit,
     )
 )
 
+TRACEPOINT_EVENT(librbd, snap_rename_enter,
+    TP_ARGS(
+        void*, imagectx,
+        const char*, name,
+        const char*, snap_name,
+        char, read_only,
+        const char*, src_snap_name,
+        const char*, dst_snap_name),
+    TP_FIELDS(
+        ctf_integer_hex(void*, imagectx, imagectx)
+        ctf_string(name, name)
+        ctf_string(snap_name, snap_name)
+        ctf_integer(char, read_only, read_only)
+        ctf_string(src_snap_name, src_snap_name)
+        ctf_string(dst_snap_name, dst_snap_name)
+    )
+)
+
+TRACEPOINT_EVENT(librbd, snap_rename_exit,
+    TP_ARGS(
+        int, retval),
+    TP_FIELDS(
+        ctf_integer(int, retval, retval)
+    )
+)
+
 TRACEPOINT_EVENT(librbd, snap_list_enter,
     TP_ARGS(
         void*, imagectx,