]> git.apps.os.sepia.ceph.com Git - ceph-ci.git/commitdiff
blk: split detect supported device and create driver
authorChangcheng Liu <changcheng.liu@aliyun.com>
Wed, 15 Jul 2020 09:33:40 +0000 (17:33 +0800)
committerChangcheng Liu <changcheng.liu@aliyun.com>
Tue, 21 Jul 2020 05:56:33 +0000 (13:56 +0800)
Use detect_device_type to detect the devices supported by driver.
Use create_with_type to create driver according to the device.

Suggested-by: Kefu Chai <kefu@redhat.com>
Signed-off-by: Changcheng Liu <changcheng.liu@aliyun.com>
src/blk/BlockDevice.cc
src/blk/BlockDevice.h
src/blk/pmem/PMEMDevice.cc
src/blk/pmem/PMEMDevice.h
src/blk/spdk/NVMEDevice.cc
src/blk/spdk/NVMEDevice.h
src/blk/zoned/HMSMRDevice.cc
src/blk/zoned/HMSMRDevice.h

index 029f0676de5c3004a12e0a08f4903e03805002f1..05e1dd24d892d7b68f2f54cb2d5a7ebe11d77831 100644 (file)
 
 #if defined(HAVE_BLUESTORE_PMEM)
 #include "pmem/PMEMDevice.h"
-#include "libpmem.h"
 #endif
 
 #if defined(HAVE_LIBZBC)
 #include "zoned/HMSMRDevice.h"
-extern "C" {
-#include <libzbc/zbc.h>
-}
 #endif
 
 #include "common/debug.h"
@@ -90,67 +86,62 @@ void IOContext::release_running_aios()
 #endif
 }
 
-BlockDevice *BlockDevice::create(CephContext* cct, const string& path,
-                                aio_callback_t cb, void *cbpriv, aio_callback_t d_cb, void *d_cbpriv)
+BlockDevice::block_device_t
+BlockDevice::detect_device_type(const std::string& path)
 {
-  string type = "kernel";
-  char buf[PATH_MAX + 1];
-  int r = ::readlink(path.c_str(), buf, sizeof(buf) - 1);
-  if (r >= 0) {
-    buf[r] = '\0';
-    char *bname = ::basename(buf);
-    if (strncmp(bname, SPDK_PREFIX, sizeof(SPDK_PREFIX)-1) == 0)
-      type = "ust-nvme";
-  }
-
-#if defined(HAVE_BLUESTORE_PMEM)
-  if (type == "kernel") {
-    int is_pmem = 0;
-    size_t map_len = 0;
-    void *addr = pmem_map_file(path.c_str(), 0, PMEM_FILE_EXCL, O_RDONLY, &map_len, &is_pmem);
-    if (addr != NULL) {
-      if (is_pmem)
-       type = "pmem";
-      else
-       dout(1) << path.c_str() << " isn't pmem file" << dendl;
-      pmem_unmap(addr, map_len);
-    } else {
-      dout(1) << "pmem_map_file:" << path.c_str() << " failed." << pmem_errormsg() << dendl;
-    }
+#if defined(HAVE_SPDK)
+  if (NVMEDevice::support(path)) {
+    return block_device_t::spdk;
   }
 #endif
-
-  dout(1) << __func__ << " path " << path << " type " << type << dendl;
-
 #if defined(HAVE_BLUESTORE_PMEM)
-  if (type == "pmem") {
-    return new PMEMDevice(cct, cb, cbpriv);
+  if (PMEMDevice::support(path)) {
+    return block_device_t::pmem;
   }
 #endif
-#if defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)
-#if defined(HAVE_LIBZBC)
-  r = zbc_device_is_zoned(path.c_str(), false, nullptr);
-  if (r == 1) {
-    return new HMSMRDevice(cct, cb, cbpriv, d_cb, d_cbpriv);
+#if (defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)) && defined(HAVE_LIZBC)
+  if (HMSMRDevice::support(path)) {
+    return block_device_t::hm_smr;
   }
-  ceph_assertf(r >= 0, "zbc_device_is_zoned(%s) failed: %d", path.c_str(), r);
 #endif
-  if (type == "kernel") {
+
+  return block_device_t::aio;
+}
+
+BlockDevice* BlockDevice::create_with_type(block_device_t device_type,
+  CephContext* cct, const std::string& path, aio_callback_t cb,
+  void *cbpriv, aio_callback_t d_cb, void *d_cbpriv)
+{
+
+  switch (device_type) {
+#if defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)
+  case block_device_t::aio:
     return new KernelDevice(cct, cb, cbpriv, d_cb, d_cbpriv);
-  }
 #endif
-#ifndef WITH_SEASTAR
 #if defined(HAVE_SPDK)
-  if (type == "ust-nvme") {
+  case block_device_t::spdk:
     return new NVMEDevice(cct, cb, cbpriv);
-  }
 #endif
+#if defined(HAVE_BLUESTORE_PMEM)
+  case block_device_t::pmem:
+    return new PMEMDevice(cct, cb, cbpriv);
+#endif
+#if (defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)) && defined(HAVE_LIZBC)
+  case block_device_t::hm_smr:
+    return new HMSMRDevice(cct, cb, cbpriv, d_cb, d_cbpriv);
 #endif
+  default:
+    ceph_abort_msg("unsupported device");
+    return nullptr;
+  }
+}
 
-  derr << __func__ << " unknown backend " << type << dendl;
-
-  ceph_abort();
-  return NULL;
+BlockDevice *BlockDevice::create(
+    CephContext* cct, const string& path, aio_callback_t cb,
+    void *cbpriv, aio_callback_t d_cb, void *d_cbpriv)
+{
+  block_device_t device_type = detect_device_type(path);
+  return create_with_type(device_type, cct, path, cb, cbpriv, d_cb, d_cbpriv);
 }
 
 void BlockDevice::queue_reap_ioc(IOContext *ioc)
index b0929bad12dc7ff7e84f8ec3549c57f7be522f07..a24411d0329586db376e936e00354363da645bc4 100644 (file)
@@ -136,6 +136,25 @@ private:
   ceph::mutex ioc_reap_lock = ceph::make_mutex("BlockDevice::ioc_reap_lock");
   std::vector<IOContext*> ioc_reap_queue;
   std::atomic_int ioc_reap_count = {0};
+  enum class block_device_t {
+    unknown,
+#if defined(HAVE_LIBAIO) || defined(HAVE_POSIXAIO)
+    aio,
+#if defined(HAVE_LIBZBC)
+    hm_smr,
+#endif
+#endif
+#if defined(HAVE_SPDK)
+    spdk,
+#endif
+#if defined(HAVE_BLUESTORE_PMEM)
+    pmem,
+#endif
+  };
+  static block_device_t detect_device_type(const std::string& path);
+  static BlockDevice *create_with_type(block_device_t device_type,
+    CephContext* cct, const std::string& path, aio_callback_t cb,
+    void *cbpriv, aio_callback_t d_cb, void *d_cbpriv);
 
 protected:
   uint64_t size = 0;
index 3cdcacc047c66218010daa5cc67101dc6214801e..247ed0692ed82ff6702464a1468f157319d0ca61 100644 (file)
@@ -164,6 +164,20 @@ int PMEMDevice::collect_metadata(const string& prefix, map<string,string> *pm) c
   return 0;
 }
 
+bool PMEMDevice::support(const std::string &path)
+{
+  int is_pmem = 0;
+  size_t map_len = 0;
+  void *addr = pmem_map_file(path.c_str(), 0, PMEM_FILE_EXCL, O_RDONLY, &map_len, &is_pmem);
+  if (addr != NULL) {
+    if (is_pmem) {
+      return true;
+    }
+    pmem_unmap(addr, map_len);
+  }
+  return false;
+}
+
 int PMEMDevice::flush()
 {
   //Because all write is persist. So no need
index 8b9bbfd25ab30b8ff601e6d0e85340907255c41a..a240d2a7b31446c3a296f04d68331c1d5285536f 100644 (file)
@@ -43,6 +43,8 @@ public:
 
   int collect_metadata(const std::string& prefix, map<std::string,std::string> *pm) const override;
 
+  static bool support(const std::string& path);
+
   int read(uint64_t off, uint64_t len, bufferlist *pbl,
           IOContext *ioc,
           bool buffered) override;
index adf9e31abe71f3d247709a704442931dffcbb215..7f3cde079945e3d13f4960f34ac02d08bf1be046 100644 (file)
@@ -698,6 +698,20 @@ NVMEDevice::NVMEDevice(CephContext* cct, aio_callback_t cb, void *cbpriv)
 {
 }
 
+bool NVMEDevice::support(const std::string& path)
+{
+  char buf[PATH_MAX + 1];
+  int r = ::readlink(path.c_str(), buf, sizeof(buf) - 1);
+  if (r >= 0) {
+    buf[r] = '\0';
+    char *bname = ::basename(buf);
+    if (strncmp(bname, SPDK_PREFIX, sizeof(SPDK_PREFIX)-1) == 0) {
+      return true;
+    }
+  }
+  return false;
+}
+
 int NVMEDevice::open(const string& p)
 {
   dout(1) << __func__ << " path " << p << dendl;
index 9ad5685490888b2e44340832c968931dd4fc14bf..352856093d802600a48440e10b03e9c36b42051e 100644 (file)
@@ -55,6 +55,8 @@ class NVMEDevice : public BlockDevice {
 
   bool supported_bdev_label() override { return false; }
 
+  static bool support(const std::string& path);
+
   void aio_submit(IOContext *ioc) override;
 
   int read(uint64_t off, uint64_t len, bufferlist *pbl,
index 3ae72c35f836b3a91d67f25691e7f4c9c1874547..a8b574f0562c234fb18f52fbdfd700beb6dda6df 100644 (file)
@@ -76,6 +76,18 @@ HMSMRDevice::HMSMRDevice(CephContext* cct, aio_callback_t cb, void *cbpriv, aio_
   }
 }
 
+bool HMSMRDevice::support(const std::string& path)
+{
+  int r = zbc_device_is_zoned(path.c_str(), false, nullptr);
+  if (r == 1) {
+    return true;
+  } else if (r < 0) {
+    derr << __func__ << " zbc_device_is_zoned(" << path << ") failed: "
+         << cpp_strerror(r) << dendl;
+  }
+  return false;
+}
+
 int HMSMRDevice::_lock()
 {
   dout(10) << __func__ << " " << fd_directs[WRITE_LIFE_NOT_SET] << dendl;
index 5ec8ee7d0ff06c6981325b51acd6191f9dcf2495..30941f2f9c6bd8b50af3ac227d4fff250c6d87c6 100644 (file)
@@ -117,6 +117,7 @@ class HMSMRDevice final : public BlockDevice {
 public:
   HMSMRDevice(CephContext* cct, aio_callback_t cb, void *cbpriv,
               aio_callback_t d_cb, void *d_cbpriv);
+  static bool support(const std::string& path);
 
   void aio_submit(IOContext *ioc) final;
   void discard_drain() final;