From 0a49f7443ff078652e0d7c1160ce57fcd3e3d45a 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 Backport of: https://tracker.ceph.com/issues/66802 Signed-off-by: Pere Diaz Bou (cherry picked from commit eb538ebd5955940ef2d7752e4e710d76c4ed4f5d) (cherry picked from commit fb998d38407d96ea877655bbb77b10d73acf6c63) --- 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 27ac1cff929..6e201f56a8b 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; @@ -1232,6 +1260,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); @@ -1423,6 +1455,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); @@ -1431,7 +1467,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) { @@ -2155,6 +2192,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( @@ -2228,6 +2269,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) { @@ -2247,6 +2292,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; @@ -2270,6 +2319,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; @@ -2350,6 +2403,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; @@ -2358,6 +2415,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); @@ -2398,6 +2459,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; } @@ -2417,6 +2482,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; } @@ -2424,6 +2493,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; @@ -2478,6 +2551,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; } @@ -2497,6 +2574,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; } @@ -2504,6 +2585,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); @@ -2544,6 +2629,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; } @@ -2573,6 +2662,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) { @@ -2596,6 +2689,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"); @@ -2675,6 +2772,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) { @@ -2699,6 +2800,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) << ", " @@ -2714,6 +2819,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; @@ -2802,6 +2911,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) << ", " @@ -2814,6 +2927,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) { @@ -2838,6 +2955,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) << ", " @@ -2885,6 +3006,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) << ", " @@ -2984,6 +3109,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) << ", " @@ -3045,7 +3174,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); @@ -3076,7 +3212,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) { @@ -4482,7 +4618,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.39.5