]> git.apps.os.sepia.ceph.com Git - ceph.git/commitdiff
osd/OSD: use scope_guard to umount objecstore
authorKefu Chai <kchai@redhat.com>
Tue, 25 May 2021 07:41:26 +0000 (15:41 +0800)
committerKefu Chai <kchai@redhat.com>
Thu, 27 May 2021 15:07:10 +0000 (23:07 +0800)
RAII can simplify the clean up logic in OSD::mkfs().

and since `ch` is a smart pointer, so it is able to take care of itself,
as long as we ensure that it is destructed before objectstore.

Signed-off-by: Kefu Chai <kchai@redhat.com>
src/osd/OSD.cc

index 92df17e3c9292d9985a0638a4e973c32cf87d612..0a506032be8562b60c4368292812ffe7c5ec8998 100644 (file)
@@ -42,6 +42,7 @@
 #include "include/types.h"
 #include "include/compat.h"
 #include "include/random.h"
+#include "include/scope_guard.h"
 
 #include "OSD.h"
 #include "OSDMap.h"
@@ -2040,8 +2041,6 @@ int OSD::mkfs(CephContext *cct,
 
   OSDSuperblock sb;
   bufferlist sbbl;
-  ObjectStore::CollectionHandle ch;
-
   // if we are fed a uuid for this osd, use it.
   store->set_fsid(cct->_conf->osd_uuid);
 
@@ -2061,7 +2060,12 @@ int OSD::mkfs(CephContext *cct,
     return ret;
   }
 
-  ch = store->open_collection(coll_t::meta());
+  auto umount_store = make_scope_guard([&] {
+    store->umount();
+  });
+
+  ObjectStore::CollectionHandle ch =
+    store->open_collection(coll_t::meta());
   if (ch) {
     ret = store->read(ch, OSD_SUPERBLOCK_GOBJECT, 0, 0, sbbl);
     if (ret < 0) {
@@ -2075,14 +2079,12 @@ int OSD::mkfs(CephContext *cct,
     if (whoami != sb.whoami) {
       derr << "provided osd id " << whoami << " != superblock's " << sb.whoami
           << dendl;
-      ret = -EINVAL;
-      goto umount_store;
+      return -EINVAL;
     }
     if (fsid != sb.cluster_fsid) {
       derr << "provided cluster fsid " << fsid
           << " != superblock's " << sb.cluster_fsid << dendl;
-      ret = -EINVAL;
-      goto umount_store;
+      return -EINVAL;
     }
   } else {
     // create superblock
@@ -2103,7 +2105,7 @@ int OSD::mkfs(CephContext *cct,
     if (ret) {
       derr << "OSD::mkfs: error while writing OSD_SUPERBLOCK_GOBJECT: "
           << "queue_transaction returned " << cpp_strerror(ret) << dendl;
-      goto umount_store;
+      return ret;
     }
   }
 
@@ -2111,14 +2113,7 @@ int OSD::mkfs(CephContext *cct,
   if (ret) {
     derr << "OSD::mkfs: failed to write fsid file: error "
          << cpp_strerror(ret) << dendl;
-    goto umount_store;
-  }
-
-umount_store:
-  if (ch) {
-    ch.reset();
   }
-  store->umount();
   return ret;
 }