From 88171d09f366237aeb2d94d8662f68b106f58477 Mon Sep 17 00:00:00 2001 From: Jason Dillaman Date: Mon, 5 Feb 2018 19:04:37 -0500 Subject: [PATCH] cls/rbd: store original namespace type within trash snapshot namespace Signed-off-by: Jason Dillaman --- src/cls/rbd/cls_rbd.cc | 9 ++-- src/cls/rbd/cls_rbd_types.cc | 44 ++++++++++++++++--- src/cls/rbd/cls_rbd_types.h | 9 +++- .../test_mock_SnapshotRemoveRequest.cc | 4 +- 4 files changed, 53 insertions(+), 13 deletions(-) diff --git a/src/cls/rbd/cls_rbd.cc b/src/cls/rbd/cls_rbd.cc index 777cdbb524f01..123f68ad3dba5 100644 --- a/src/cls/rbd/cls_rbd.cc +++ b/src/cls/rbd/cls_rbd.cc @@ -2055,8 +2055,10 @@ int snapshot_trash_add(cls_method_context_t hctx, bufferlist *in, if (snap.protection_status != RBD_PROTECTION_STATUS_UNPROTECTED) { return -EBUSY; - } else if (cls::rbd::get_snap_namespace_type(snap.snapshot_namespace) == - cls::rbd::SNAPSHOT_NAMESPACE_TYPE_TRASH) { + } + + auto snap_type = cls::rbd::get_snap_namespace_type(snap.snapshot_namespace); + if (snap_type == cls::rbd::SNAPSHOT_NAMESPACE_TYPE_TRASH) { return -EEXIST; } @@ -2067,7 +2069,8 @@ int snapshot_trash_add(cls_method_context_t hctx, bufferlist *in, return r; } - snap.snapshot_namespace = cls::rbd::TrashSnapshotNamespace{snap.name}; + snap.snapshot_namespace = cls::rbd::TrashSnapshotNamespace{snap_type, + snap.name}; uuid_d uuid_gen; uuid_gen.generate_random(); snap.name = uuid_gen.to_string(); diff --git a/src/cls/rbd/cls_rbd_types.cc b/src/cls/rbd/cls_rbd_types.cc index d709ba80f4ec8..301879c3a021d 100644 --- a/src/cls/rbd/cls_rbd_types.cc +++ b/src/cls/rbd/cls_rbd_types.cc @@ -382,15 +382,22 @@ void GroupSnapshotNamespace::dump(Formatter *f) const { void TrashSnapshotNamespace::encode(bufferlist& bl) const { using ceph::encode; encode(original_name, bl); + encode(static_cast(original_snapshot_namespace_type), bl); } void TrashSnapshotNamespace::decode(bufferlist::iterator& it) { using ceph::decode; decode(original_name, it); + uint32_t snap_type; + decode(snap_type, it); + original_snapshot_namespace_type = static_cast( + snap_type); } void TrashSnapshotNamespace::dump(Formatter *f) const { f->dump_string("original_name", original_name); + f->dump_stream("original_snapshot_namespace") + << original_snapshot_namespace_type; } class EncodeSnapshotNamespaceVisitor : public boost::static_visitor { @@ -492,7 +499,9 @@ void SnapshotInfo::generate_test_instances(std::list &o) { o.push_back(new SnapshotInfo(2ULL, GroupSnapshotNamespace{567, "group1", "snap1"}, "snap1", 123, {123456, 0}, 987)); - o.push_back(new SnapshotInfo(3ULL, TrashSnapshotNamespace{"snap1"}, + o.push_back(new SnapshotInfo(3ULL, + TrashSnapshotNamespace{ + SNAPSHOT_NAMESPACE_TYPE_USER, "snap1"}, "12345", 123, {123456, 0}, 429)); } @@ -539,21 +548,42 @@ void SnapshotNamespace::generate_test_instances(std::list &o o.push_back(new SnapshotNamespace(TrashSnapshotNamespace())); } +std::ostream& operator<<(std::ostream& os, const SnapshotNamespaceType& type) { + switch (type) { + case SNAPSHOT_NAMESPACE_TYPE_USER: + os << "user"; + break; + case SNAPSHOT_NAMESPACE_TYPE_GROUP: + os << "group"; + break; + case SNAPSHOT_NAMESPACE_TYPE_TRASH: + os << "trash"; + break; + default: + os << "unknown"; + break; + } + return os; +} + std::ostream& operator<<(std::ostream& os, const UserSnapshotNamespace& ns) { - os << "[user]"; + os << "[" << SNAPSHOT_NAMESPACE_TYPE_USER << "]"; return os; } std::ostream& operator<<(std::ostream& os, const GroupSnapshotNamespace& ns) { - os << "[group" - << " group_pool=" << ns.group_pool - << " group_id=" << ns.group_id - << " group_snapshot_id=" << ns.group_snapshot_id << "]"; + os << "[" << SNAPSHOT_NAMESPACE_TYPE_GROUP << " " + << "group_pool=" << ns.group_pool << ", " + << "group_id=" << ns.group_id << ", " + << "group_snapshot_id=" << ns.group_snapshot_id << "]"; return os; } std::ostream& operator<<(std::ostream& os, const TrashSnapshotNamespace& ns) { - os << "[trash]"; + os << "[" << SNAPSHOT_NAMESPACE_TYPE_TRASH << " " + << "original_name=" << ns.original_name << ", " + << "original_snapshot_namespace=" << ns.original_snapshot_namespace_type + << "]"; return os; } diff --git a/src/cls/rbd/cls_rbd_types.h b/src/cls/rbd/cls_rbd_types.h index f34b33773c2c7..3d61a66788646 100644 --- a/src/cls/rbd/cls_rbd_types.h +++ b/src/cls/rbd/cls_rbd_types.h @@ -328,10 +328,14 @@ struct TrashSnapshotNamespace { SNAPSHOT_NAMESPACE_TYPE_TRASH; std::string original_name; + SnapshotNamespaceType original_snapshot_namespace_type = + SNAPSHOT_NAMESPACE_TYPE_USER; TrashSnapshotNamespace() {} - TrashSnapshotNamespace(const std::string& original_name) - : original_name(original_name) {} + TrashSnapshotNamespace(SnapshotNamespaceType original_snapshot_namespace_type, + const std::string& original_name) + : original_name(original_name), + original_snapshot_namespace_type(original_snapshot_namespace_type) {} void encode(bufferlist& bl) const; void decode(bufferlist::iterator& it); @@ -364,6 +368,7 @@ struct UnknownSnapshotNamespace { } }; +std::ostream& operator<<(std::ostream& os, const SnapshotNamespaceType& type); std::ostream& operator<<(std::ostream& os, const UserSnapshotNamespace& ns); std::ostream& operator<<(std::ostream& os, const GroupSnapshotNamespace& ns); std::ostream& operator<<(std::ostream& os, const TrashSnapshotNamespace& ns); diff --git a/src/test/librbd/operation/test_mock_SnapshotRemoveRequest.cc b/src/test/librbd/operation/test_mock_SnapshotRemoveRequest.cc index 4f86b93fec714..3747e20d86a91 100644 --- a/src/test/librbd/operation/test_mock_SnapshotRemoveRequest.cc +++ b/src/test/librbd/operation/test_mock_SnapshotRemoveRequest.cc @@ -291,7 +291,9 @@ TEST_F(TestMockOperationSnapshotRemoveRequest, SuccessTrash) { uint64_t snap_id = ictx->snap_info.rbegin()->first; expect_snapshot_get(mock_image_ctx, - {snap_id, {cls::rbd::TrashSnapshotNamespace{"snap1"}}, + {snap_id, + {cls::rbd::TrashSnapshotNamespace{ + cls::rbd::SNAPSHOT_NAMESPACE_TYPE_USER, "snap1"}}, "snap1", 123, {}, 0}, 0); expect_get_parent_spec(mock_image_ctx, 0); -- 2.39.5