From: Kefu Chai Date: Tue, 25 May 2021 07:41:26 +0000 (+0800) Subject: osd/OSD: use scope_guard to umount objecstore X-Git-Tag: v17.1.0~1806^2~3 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=8b2c3a211a84904dc0307eed7267f5687c13dc74;p=ceph.git osd/OSD: use scope_guard to umount objecstore 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 --- diff --git a/src/osd/OSD.cc b/src/osd/OSD.cc index 92df17e3c929..0a506032be85 100644 --- a/src/osd/OSD.cc +++ b/src/osd/OSD.cc @@ -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; }