From e5eb925f484c872e4bf02a73b569a4b83624ef7c Mon Sep 17 00:00:00 2001 From: Ilya Dryomov Date: Wed, 17 Apr 2024 20:31:02 +0200 Subject: [PATCH] librbd: make group and group snapshot IDs more random Image IDs suffered from the same issue -- it was addressed in commit be8373688c1b ("librbd: block_name_prefix is not created randomly"). The code for generating group IDs is duplicated in api/Group.cc and got missed. Instead of cut-and-pasting the fix, just call generate_image_id() directly and rename variables for more explicitness. Before: $ rados -p rbd ls | grep rbd_group_header rbd_group_header.10256b8b4567 rbd_group_header.10216b8b4567 rbd_group_header.10236b8b4567 rbd_group_header.101f6b8b4567 After: $ rados -p rbd ls | grep rbd_group_header rbd_group_header.10255f555a5 rbd_group_header.1023f347eafb rbd_group_header.101f24c75111 rbd_group_header.1021dda4e122 Fixes: https://tracker.ceph.com/issues/65573 Signed-off-by: Ilya Dryomov (cherry picked from commit 76ed07d2d8085b733f51540154cfaf917db05e86) --- src/librbd/Utils.cc | 1 + src/librbd/api/Group.cc | 24 ++++++------------------ 2 files changed, 7 insertions(+), 18 deletions(-) diff --git a/src/librbd/Utils.cc b/src/librbd/Utils.cc index 0ea31fc1c9d43..a6acbda10aae3 100644 --- a/src/librbd/Utils.cc +++ b/src/librbd/Utils.cc @@ -59,6 +59,7 @@ librados::AioCompletion *create_rados_callback(Context *on_finish) { return create_rados_callback(on_finish); } +// also used for group and group snapshot ids std::string generate_image_id(librados::IoCtx &ioctx) { librados::Rados rados(ioctx); diff --git a/src/librbd/api/Group.cc b/src/librbd/api/Group.cc index e5f3da69cb4c0..06d38fe850073 100644 --- a/src/librbd/api/Group.cc +++ b/src/librbd/api/Group.cc @@ -53,17 +53,6 @@ snap_t get_group_snap_id(I* ictx, return CEPH_NOSNAP; } -string generate_uuid(librados::IoCtx& io_ctx) -{ - Rados rados(io_ctx); - uint64_t bid = rados.get_instance_id(); - - uint32_t extra = rand() % 0xFFFFFFFF; - std::ostringstream bid_ss; - bid_ss << std::hex << bid << std::hex << extra; - return bid_ss.str(); -} - int group_snap_list(librados::IoCtx& group_ioctx, const char *group_name, std::vector *cls_snaps) { @@ -523,21 +512,20 @@ int Group::create(librados::IoCtx& io_ctx, const char *group_name) { CephContext *cct = (CephContext *)io_ctx.cct(); - string id = generate_uuid(io_ctx); - ldout(cct, 2) << "adding group to directory..." << dendl; + std::string group_id = util::generate_image_id(io_ctx); int r = cls_client::group_dir_add(&io_ctx, RBD_GROUP_DIRECTORY, group_name, - id); + group_id); if (r < 0) { lderr(cct) << "error adding group to directory: " << cpp_strerror(r) << dendl; return r; } - string header_oid = util::group_header_name(id); - r = io_ctx.create(header_oid, true); + std::string group_header_oid = util::group_header_name(group_id); + r = io_ctx.create(group_header_oid, true); if (r < 0) { lderr(cct) << "error creating group header: " << cpp_strerror(r) << dendl; goto err_remove_from_dir; @@ -547,7 +535,7 @@ int Group::create(librados::IoCtx& io_ctx, const char *group_name) err_remove_from_dir: int remove_r = cls_client::group_dir_remove(&io_ctx, RBD_GROUP_DIRECTORY, - group_name, id); + group_name, group_id); if (remove_r < 0) { lderr(cct) << "error cleaning up group from rbd_directory " << "object after creation failed: " << cpp_strerror(remove_r) @@ -929,7 +917,7 @@ int Group::snap_create(librados::IoCtx& group_ioctx, string group_header_oid = util::group_header_name(group_id); - group_snap.id = generate_uuid(group_ioctx); + group_snap.id = util::generate_image_id(group_ioctx); group_snap.name = string(snap_name); group_snap.state = cls::rbd::GROUP_SNAPSHOT_STATE_INCOMPLETE; group_snap.snaps = image_snaps; -- 2.39.5