]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
tools/objectstore: check for wrong coll open_collection
authorPere Diaz Bou <pere-altea@hotmail.com>
Fri, 28 Jun 2024 18:13:15 +0000 (20:13 +0200)
committerPere Diaz Bou <pere-altea@hotmail.com>
Tue, 16 Jul 2024 10:56:49 +0000 (12:56 +0200)
Fixes: https://tracker.ceph.com/issues/66802
Signed-off-by: Pere Diaz Bou <pere-altea@hotmail.com>
src/tools/ceph_objectstore_tool.cc

index 996004f774bce3cf8e5d5663bab13e805001b4fe..b414f19a1b5c554e6f6acba6ae75a7c636de0b3d 100644 (file)
@@ -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;
@@ -976,6 +989,10 @@ int ObjectStoreTool::export_files(ObjectStore *store, coll_t coll, bool force)
 {
   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<ghobject_t> objects;
     int r = store->collection_list(ch, next, ghobject_t::get_max(), 300,
@@ -1013,6 +1030,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;
@@ -1033,6 +1053,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) {
@@ -1056,6 +1080,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;
@@ -1114,6 +1142,7 @@ int expand_log(
     pg_missing_t missing;
     auto ch = fs->open_collection(coll_t(pgid));
     if (!ch) {
+      cerr << "pgid " << pgid << " does not exist" << std::endl;
       return -ENOENT;
     }
     ostringstream oss;
@@ -1358,6 +1387,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);
@@ -1552,6 +1585,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);
@@ -1560,7 +1597,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) {
@@ -2285,6 +2323,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(
@@ -2358,6 +2400,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<string,bufferptr,less<>> aset;
   int r = store->getattrs(ch, ghobj, aset);
   if (r < 0) {
@@ -2377,6 +2423,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;
@@ -2400,6 +2450,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;
 
@@ -2480,6 +2534,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;
@@ -2488,6 +2546,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);
@@ -2528,6 +2590,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;
 }
@@ -2547,6 +2613,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;
 }
@@ -2554,6 +2624,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<string> keys;
   map<string, bufferlist> out;
 
@@ -2608,6 +2682,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;
 }
@@ -2627,6 +2705,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;
 }
@@ -2634,6 +2716,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);
@@ -2674,6 +2760,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;
 }
@@ -2703,6 +2793,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) {
@@ -2726,6 +2820,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");
@@ -2805,6 +2903,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) {
@@ -2829,6 +2931,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) << ", "
@@ -2844,6 +2950,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;
@@ -2932,6 +3042,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) << ", "
@@ -2944,6 +3058,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) {
@@ -2968,6 +3086,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) << ", "
@@ -3015,6 +3137,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) << ", "
@@ -3114,6 +3240,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) << ", "
@@ -3175,7 +3305,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);
@@ -3206,7 +3343,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) {
@@ -4694,7 +4831,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;