From 844bba0bd3c05ce03987193e284f9d48f51cdc8b Mon Sep 17 00:00:00 2001 From: Pere Diaz Bou Date: Fri, 28 Jun 2024 20:13:15 +0200 Subject: [PATCH] tools/objectstore: check for wrong coll open_collection Fixes: https://tracker.ceph.com/issues/66802 Signed-off-by: Pere Diaz Bou (cherry picked from commit eb538ebd5955940ef2d7752e4e710d76c4ed4f5d) --- src/tools/ceph_objectstore_tool.cc | 142 ++++++++++++++++++++++++++++- 1 file changed, 139 insertions(+), 3 deletions(-) diff --git a/src/tools/ceph_objectstore_tool.cc b/src/tools/ceph_objectstore_tool.cc index eeb15a39248a..b1f519d3a84e 100644 --- a/src/tools/ceph_objectstore_tool.cc +++ b/src/tools/ceph_objectstore_tool.cc @@ -85,6 +85,10 @@ struct action_on_object_t { int _action_on_all_objects_in_pg(ObjectStore *store, coll_t coll, action_on_object_t &action, bool debug) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } unsigned LIST_AT_A_TIME = 100; ghobject_t next; @@ -442,6 +446,7 @@ int get_log(CephContext *cct, ObjectStore *fs, __u8 struct_ver, try { auto ch = fs->open_collection(coll_t(pgid)); if (!ch) { + cerr << "pgid " << pgid << " does not exist" << std::endl; return -ENOENT; } ostringstream oss; @@ -744,6 +749,10 @@ int do_trim_pg_log_dups(ObjectStore *store, const coll_t &coll, ghobject_t oid = pgid.make_pgmeta_oid(); struct stat st; auto ch = store->open_collection(coll); + if (!ch) { + cerr << "pgid " << pgid << " does not exist" << std::endl; + return -ENOENT; + } int r = store->stat(ch, oid, &st); ceph_assert(r == 0); ceph_assert(st.st_size == 0); @@ -828,6 +837,10 @@ int ObjectStoreTool::export_file(ObjectStore *store, coll_t cid, ghobject_t &obj footer ft; auto ch = store->open_collection(cid); + if (!ch) { + cerr << "Collection " << cid << " does not exist" << std::endl; + return -ENOENT; + } int ret = store->stat(ch, obj, &st); if (ret < 0) return ret; @@ -945,6 +958,10 @@ int ObjectStoreTool::export_files(ObjectStore *store, coll_t coll) { ghobject_t next; auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } while (!next.is_max()) { vector objects; int r = store->collection_list(ch, next, ghobject_t::get_max(), 300, @@ -982,6 +999,9 @@ int set_inc_osdmap(ObjectStore *store, epoch_t e, bufferlist& bl, bool force) { } } auto ch = store->open_collection(coll_t::meta()); + if (!ch) { + return -ENOENT; + } const ghobject_t inc_oid = OSD::get_inc_osdmap_pobject_name(e); if (!store->exists(ch, inc_oid)) { cerr << "inc-osdmap (" << inc_oid << ") does not exist." << std::endl; @@ -1002,6 +1022,10 @@ int set_inc_osdmap(ObjectStore *store, epoch_t e, bufferlist& bl, bool force) { int get_inc_osdmap(ObjectStore *store, epoch_t e, bufferlist& bl) { auto ch = store->open_collection(coll_t::meta()); + if (!ch) { + cerr << "Collection " << coll_t::meta() << " does not exist" << std::endl; + return -ENOENT; + } if (store->read(ch, OSD::get_inc_osdmap_pobject_name(e), 0, 0, bl) < 0) { @@ -1025,6 +1049,10 @@ int set_osdmap(ObjectStore *store, epoch_t e, bufferlist& bl, bool force) { } } auto ch = store->open_collection(coll_t::meta()); + if (!ch) { + cerr << "Collection " << coll_t::meta() << " does not exist" << std::endl; + return -ENOENT; + } const ghobject_t full_oid = OSD::get_osdmap_pobject_name(e); if (!store->exists(ch, full_oid)) { cerr << "osdmap (" << full_oid << ") does not exist." << std::endl; @@ -1249,6 +1277,10 @@ int get_attrs( as.decode(ebliter); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } if (debug) cerr << "\tattrs: len " << as.data.size() << std::endl; t->setattrs(coll, hoid, as.data); @@ -1443,6 +1475,10 @@ int ObjectStoreTool::get_object(ObjectStore *store, ceph_assert(g_ceph_context); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } if (ob.hoid.hobj.nspace != g_ceph_context->_conf->osd_hit_set_namespace) { object_t oid = ob.hoid.hobj.oid; object_locator_t loc(ob.hoid.hobj); @@ -1451,7 +1487,8 @@ int ObjectStoreTool::get_object(ObjectStore *store, spg_t coll_pgid; if (coll.is_pg(&coll_pgid) == false) { - cerr << "INTERNAL ERROR: Bad collection during import" << std::endl; + cerr << "INTERNAL ERROR: Bad collection during import: " + << coll_pgid << " does not exist" << std::endl; return -EFAULT; } if (coll_pgid.shard != ob.hoid.shard_id) { @@ -2176,6 +2213,10 @@ int do_remove_object(ObjectStore *store, coll_t coll, ghobject_t &ghobj, bool all, bool force, enum rmtype type) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } spg_t pg; coll.is_pg_prefix(&pg); OSDriver driver( @@ -2249,6 +2290,10 @@ int do_remove_object(ObjectStore *store, coll_t coll, int do_list_attrs(ObjectStore *store, coll_t coll, ghobject_t &ghobj) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } map> aset; int r = store->getattrs(ch, ghobj, aset); if (r < 0) { @@ -2268,6 +2313,10 @@ int do_list_attrs(ObjectStore *store, coll_t coll, ghobject_t &ghobj) int do_list_omap(ObjectStore *store, coll_t coll, ghobject_t &ghobj) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } ObjectMap::ObjectMapIterator iter = store->get_omap_iterator(ch, ghobj); if (!iter) { cerr << "omap_get_iterator: " << cpp_strerror(ENOENT) << std::endl; @@ -2291,6 +2340,10 @@ int do_list_omap(ObjectStore *store, coll_t coll, ghobject_t &ghobj) int do_get_bytes(ObjectStore *store, coll_t coll, ghobject_t &ghobj, int fd) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } struct stat st; mysize_t total; @@ -2371,6 +2424,10 @@ int do_set_bytes(ObjectStore *store, coll_t coll, } while(true); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } if (!dry_run) store->queue_transaction(ch, std::move(*t)); return 0; @@ -2379,6 +2436,10 @@ int do_set_bytes(ObjectStore *store, coll_t coll, int do_get_attr(ObjectStore *store, coll_t coll, ghobject_t &ghobj, string key) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } bufferptr bp; int r = store->getattr(ch, ghobj, key.c_str(), bp); @@ -2419,6 +2480,10 @@ int do_set_attr(ObjectStore *store, coll_t coll, t->setattr(coll, ghobj, key, bl); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } store->queue_transaction(ch, std::move(*t)); return 0; } @@ -2438,6 +2503,10 @@ int do_rm_attr(ObjectStore *store, coll_t coll, t->rmattr(coll, ghobj, key); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } store->queue_transaction(ch, std::move(*t)); return 0; } @@ -2445,6 +2514,10 @@ int do_rm_attr(ObjectStore *store, coll_t coll, int do_get_omap(ObjectStore *store, coll_t coll, ghobject_t &ghobj, string key) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } set keys; map out; @@ -2499,6 +2572,10 @@ int do_set_omap(ObjectStore *store, coll_t coll, t->omap_setkeys(coll, ghobj, attrset); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } store->queue_transaction(ch, std::move(*t)); return 0; } @@ -2518,6 +2595,10 @@ int do_rm_omap(ObjectStore *store, coll_t coll, t->omap_rmkey(coll, ghobj, key); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } store->queue_transaction(ch, std::move(*t)); return 0; } @@ -2525,6 +2606,10 @@ int do_rm_omap(ObjectStore *store, coll_t coll, int do_get_omaphdr(ObjectStore *store, coll_t coll, ghobject_t &ghobj) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } bufferlist hdrbl; int r = store->omap_get_header(ch, ghobj, &hdrbl, true); @@ -2565,6 +2650,10 @@ int do_set_omaphdr(ObjectStore *store, coll_t coll, t->omap_setheader(coll, ghobj, hdrbl); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } store->queue_transaction(ch, std::move(*t)); return 0; } @@ -2594,6 +2683,10 @@ struct do_fix_lost : public action_on_object_t { int get_snapset(ObjectStore *store, coll_t coll, ghobject_t &ghobj, SnapSet &ss, bool silent = false) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } bufferlist attr; int r = store->getattr(ch, ghobj, SS_ATTR, attr); if (r < 0) { @@ -2617,6 +2710,10 @@ int get_snapset(ObjectStore *store, coll_t coll, ghobject_t &ghobj, SnapSet &ss, int print_obj_info(ObjectStore *store, coll_t coll, ghobject_t &ghobj, Formatter* formatter) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } int r = 0; formatter->open_object_section("obj"); formatter->open_object_section("id"); @@ -2696,6 +2793,10 @@ int print_obj_info(ObjectStore *store, coll_t coll, ghobject_t &ghobj, Formatter int corrupt_info(ObjectStore *store, coll_t coll, ghobject_t &ghobj, Formatter* formatter) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } bufferlist attr; int r = store->getattr(ch, ghobj, OI_ATTR, attr); if (r < 0) { @@ -2720,6 +2821,10 @@ int corrupt_info(ObjectStore *store, coll_t coll, ghobject_t &ghobj, Formatter* encode(oi, attr, -1); /* fixme: using full features */ t.setattr(coll, ghobj, OI_ATTR, attr); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } r = store->queue_transaction(ch, std::move(t)); if (r < 0) { cerr << "Error writing object info: " << make_pair(coll, ghobj) << ", " @@ -2735,6 +2840,10 @@ int set_size( bool corrupt) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } if (ghobj.hobj.is_snapdir()) { cerr << "Can't set the size of a snapdir" << std::endl; return -EINVAL; @@ -2823,6 +2932,10 @@ int set_size( t.setattr(coll, head, SS_ATTR, snapattr); } auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } r = store->queue_transaction(ch, std::move(t)); if (r < 0) { cerr << "Error writing object info: " << make_pair(coll, ghobj) << ", " @@ -2835,6 +2948,10 @@ int set_size( int clear_data_digest(ObjectStore *store, coll_t coll, ghobject_t &ghobj) { auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } bufferlist attr; int r = store->getattr(ch, ghobj, OI_ATTR, attr); if (r < 0) { @@ -2859,6 +2976,10 @@ int clear_data_digest(ObjectStore *store, coll_t coll, ghobject_t &ghobj) { ObjectStore::Transaction t; t.setattr(coll, ghobj, OI_ATTR, attr); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } r = store->queue_transaction(ch, std::move(t)); if (r < 0) { cerr << "Error writing object info: " << make_pair(coll, ghobj) << ", " @@ -2906,6 +3027,10 @@ int clear_snapset(ObjectStore *store, coll_t coll, ghobject_t &ghobj, ObjectStore::Transaction t; t.setattr(coll, ghobj, SS_ATTR, bl); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } int r = store->queue_transaction(ch, std::move(t)); if (r < 0) { cerr << "Error setting snapset on : " << make_pair(coll, ghobj) << ", " @@ -3005,6 +3130,10 @@ int remove_clone( ObjectStore::Transaction t; t.setattr(coll, ghobj, SS_ATTR, bl); auto ch = store->open_collection(coll); + if (!ch) { + cerr << "Collection " << coll << " does not exist" << std::endl; + return -ENOENT; + } int r = store->queue_transaction(ch, std::move(t)); if (r < 0) { cerr << "Error setting snapset on : " << make_pair(coll, ghobj) << ", " @@ -3066,7 +3195,14 @@ int dup(string srcpath, ObjectStore *src, string dstpath, ObjectStore *dst) for (auto cid : collections) { cout << i++ << "/" << num << " " << cid << std::endl; auto ch = src->open_collection(cid); + if (!ch) { + cerr << "Collection " << cid << " does not exist" << std::endl; + return -ENOENT; + } auto dch = dst->create_new_collection(cid); + if (!dch) { + return -ENOENT; + } { ObjectStore::Transaction t; int bits = src->collection_bits(ch); @@ -3097,7 +3233,7 @@ int dup(string srcpath, ObjectStore *src, string dstpath, ObjectStore *dst) if (ls.empty()) { break; } - + for (auto& oid : ls) { //cout << " " << cid << " " << oid << std::endl; if (n % 100 == 0) { @@ -4543,7 +4679,7 @@ int main(int argc, char **argv) fs->queue_transaction(ch, std::move(*t)); } cout << "Reseting last_complete succeeded" << std::endl; - + } else if (op == "pg-log-inject-dups") { if (!vm.count("file") || file == "-") { cerr << "Must provide file containing JSON dups entries" << std::endl; -- 2.47.3