]> git-server-git.apps.pok.os.sepia.ceph.com Git - ceph.git/commitdiff
blkdev: add an enum type for block device properties
authorAlan Somers <asomers@gmail.com>
Wed, 27 Sep 2017 16:37:02 +0000 (10:37 -0600)
committerKefu Chai <kchai@redhat.com>
Fri, 19 Oct 2018 11:35:23 +0000 (19:35 +0800)
Signed-off-by: Alan Somers <asomers@gmail.com>
src/common/blkdev.cc
src/common/blkdev.h
src/os/bluestore/KernelDevice.cc
src/os/bluestore/PMEMDevice.cc
src/test/common/test_blkdev.cc

index 5e103e15b6030d7ebd6440771bc4c5b3473aafd3..197cf895120db1b0fd23c81fdce7e863d10cc9e8 100644 (file)
@@ -36,6 +36,8 @@ int get_device_by_path(const char *path, char* partition, char* device,
 }
 
 
+#include "common/blkdev.h"
+
 #ifdef __linux__
 #include <libudev.h>
 #include <linux/fs.h>
@@ -51,7 +53,17 @@ int get_device_by_path(const char *path, char* partition, char* device,
 
 static const char *sandbox_dir = "";
 
-static std::string get_block_device_string_property_wrap(const std::string &devname, const std::string &property); 
+static std::string get_block_device_string_property_wrap(const std::string &devname,
+                                                        blkdev_prop_t property); 
+
+static const char *blkdev_props2strings[] = {
+  [BLKDEV_PROP_DEV]                 = "dev",
+  [BLKDEV_PROP_DISCARD_GRANULARITY] = "queue/discard_granularity",
+  [BLKDEV_PROP_MODEL]               = "device/model",
+  [BLKDEV_PROP_ROTATIONAL]          = "queue/rotational",
+  [BLKDEV_PROP_SERIAL]              = "device/serial",
+  [BLKDEV_PROP_VENDOR]              = "device/vendor",
+};
 
 void set_block_device_sandbox_dir(const char *dir)
 {
@@ -163,12 +175,16 @@ int get_block_device_base(const char *dev, char *out, size_t out_len)
  * return negative error on error
  */
 int64_t get_block_device_string_property(const char *devname,
-                                        const char *property,
+                                        blkdev_prop_t prop,
                                         char *val, size_t maxlen)
 {
   char filename[PATH_MAX];
+  const char *propstr;
+
+  assert(prop < BLKDEV_PROP_NUMPROPS);
+  propstr = blkdev_props2strings[prop];
   snprintf(filename, sizeof(filename),
-          "%s/sys/block/%s/%s", sandbox_dir, devname, property);
+          "%s/sys/block/%s/%s", sandbox_dir, devname, propstr);
 
   FILE *fp = fopen(filename, "r");
   if (fp == NULL) {
@@ -195,10 +211,10 @@ int64_t get_block_device_string_property(const char *devname,
  * return the value (we assume it is positive)
  * return negative error on error
  */
-int64_t get_block_device_int_property(const char *devname, const char *property)
+int64_t get_block_device_int_property(const char *devname, blkdev_prop_t prop)
 {
   char buff[256] = {0};
-  int r = get_block_device_string_property(devname, property, buff, sizeof(buff));
+  int r = get_block_device_string_property(devname, prop, buff, sizeof(buff));
   if (r < 0)
     return r;
   // take only digits
@@ -217,7 +233,7 @@ int64_t get_block_device_int_property(const char *devname, const char *property)
 
 bool block_device_support_discard(const char *devname)
 {
-  return get_block_device_int_property(devname, "queue/discard_granularity") > 0;
+  return get_block_device_int_property(devname, BLKDEV_PROP_DISCARD_GRANULARITY) > 0;
 }
 
 int block_device_discard(int fd, int64_t offset, int64_t len)
@@ -228,22 +244,23 @@ int block_device_discard(int fd, int64_t offset, int64_t len)
 
 bool block_device_is_rotational(const char *devname)
 {
-  return get_block_device_int_property(devname, "queue/rotational") > 0;
+  return get_block_device_int_property(devname, BLKDEV_PROP_ROTATIONAL) > 0;
 }
 
 int block_device_vendor(const char *devname, char *vendor, size_t max)
 {
-  return get_block_device_string_property(devname, "device/vendor", vendor, max);
+  return get_block_device_string_property(devname, BLKDEV_PROP_VENDOR, vendor, max);
 }
 
 int block_device_model(const char *devname, char *model, size_t max)
 {
-  return get_block_device_string_property(devname, "device/model", model, max);
+  return get_block_device_string_property(devname, BLKDEV_PROP_MODEL, model,
+                                          max);
 }
 
 int block_device_serial(const char *devname, char *serial, size_t max)
 {
-  return get_block_device_string_property(devname, "device/serial", serial, max);
+  return get_block_device_string_property(devname, BLKDEV_PROP_SERIAL, serial, max);
 }
 
 int get_device_by_fd(int fd, char *partition, char *device, size_t max)
@@ -427,8 +444,8 @@ std::string get_device_id(const std::string& devname)
   // returned nothing; trying to read from files.  note that the 'vendor'
   // file rarely contains the actual vendor; it's usually 'ATA'.
   std::string model, serial;
-  model = get_block_device_string_property_wrap(devname, "device/model");
-  serial = get_block_device_string_property_wrap(devname, "device/serial");
+  model = get_block_device_string_property_wrap(devname, BLKDEV_PROP_MODEL);
+  serial = get_block_device_string_property_wrap(devname, BLKDEV_PROP_MODEL);
 
   if (!model.size() || serial.size()) {
     return {};
@@ -440,11 +457,11 @@ std::string get_device_id(const std::string& devname)
 }
 
 std::string get_block_device_string_property_wrap(const std::string &devname,
-                                                 const std::string &property)
+                                                 blkdev_prop_t prop)
 {
   char buff[1024] = {0};
   std::string prop_val;
-  int ret = get_block_device_string_property(devname.c_str(), property.c_str(), buff, sizeof(buff));
+  int ret = get_block_device_string_property(devname.c_str(), prop, buff, sizeof(buff));
   if (ret < 0) {
     return {};
   }
index 6cbe16b6f88d812e57e64232c54c3274a90b08f3..a2569c23fc9101def1597bd6d9cd406eeddda67b 100644 (file)
@@ -4,6 +4,16 @@
 #include <set>
 #include <string>
 
+enum blkdev_prop_t {
+  BLKDEV_PROP_DEV,
+  BLKDEV_PROP_DISCARD_GRANULARITY,
+  BLKDEV_PROP_MODEL,
+  BLKDEV_PROP_ROTATIONAL,
+  BLKDEV_PROP_SERIAL,
+  BLKDEV_PROP_VENDOR,
+  BLKDEV_PROP_NUMPROPS
+};
+
 /* for testing purposes */
 extern void set_block_device_sandbox_dir(const char *dir);
 
@@ -18,9 +28,9 @@ extern int get_device_by_fd(int fd, char* partition, char* device, size_t max);
 
 // from a device (e.g., "sdb")
 extern int64_t get_block_device_int_property(
-       const char *devname, const char *property);
+       const char *devname, blkdev_prop_t prop);
 extern int64_t get_block_device_string_property(
-       const char *devname, const char *property,
+       const char *devname, blkdev_prop_t prop,
        char *val, size_t maxlen);
 extern bool block_device_support_discard(const char *devname);
 extern bool block_device_is_rotational(const char *devname);
index 999394e7eb39981f8fa157bc5c7971961fd8fa89..0bf470c537fdaecbeb4f94cfa3d355717e9599df 100644 (file)
@@ -209,7 +209,7 @@ void KernelDevice::close()
   path.clear();
 }
 
-static string get_dev_property(const char *dev, const char *property)
+static string get_dev_property(const char *dev, blkdev_prop_t property)
 {
   char val[1024] = {0};
   get_block_device_string_property(dev, property, val, sizeof(val));
@@ -257,18 +257,18 @@ int KernelDevice::collect_metadata(const string& prefix, map<string,string> *pm)
       {
        (*pm)[prefix + "partition_path"] = string(partition_path);
        (*pm)[prefix + "dev_node"] = string(dev_node);
-       (*pm)[prefix + "model"] = get_dev_property(dev_node, "device/model");
-       (*pm)[prefix + "dev"] = get_dev_property(dev_node, "dev");
+       (*pm)[prefix + "model"] = get_dev_property(dev_node, BLKDEV_PROP_MODEL);
+       (*pm)[prefix + "dev"] = get_dev_property(dev_node, BLKDEV_PROP_DEV);
 
        // nvme exposes a serial number
-       string serial = get_dev_property(dev_node, "device/serial");
+       string serial = get_dev_property(dev_node, BLKDEV_PROP_SERIAL);
        if (serial.length()) {
          (*pm)[prefix + "serial"] = serial;
        }
 
        // nvme has a device/device/* structure; infer from that.  there
        // is probably a better way?
-       string nvme_vendor = get_dev_property(dev_node, "device/device/vendor");
+       string nvme_vendor = get_dev_property(dev_node, BLKDEV_PROP_VENDOR);
        if (nvme_vendor.length()) {
          (*pm)[prefix + "type"] = "nvme";
        }
index 5bfa44a9d987f52924c6ef1030832c1ba63dd65b..a711c84a7eb61a1088f9239942bd4edb27287303 100644 (file)
@@ -128,7 +128,7 @@ void PMEMDevice::close()
   path.clear();
 }
 
-static string get_dev_property(const char *dev, const char *property)
+static string get_dev_property(const char *dev, blkdev_prop_t property)
 {
   char val[1024] = {0};
   get_block_device_string_property(dev, property, val, sizeof(val));
@@ -166,18 +166,18 @@ int PMEMDevice::collect_metadata(const string& prefix, map<string,string> *pm) c
       {
        (*pm)[prefix + "partition_path"] = string(partition_path);
        (*pm)[prefix + "dev_node"] = string(dev_node);
-       (*pm)[prefix + "model"] = get_dev_property(dev_node, "device/model");
-       (*pm)[prefix + "dev"] = get_dev_property(dev_node, "dev");
+       (*pm)[prefix + "model"] = get_dev_property(dev_node, BLKDEV_PROP_MODEL);
+       (*pm)[prefix + "dev"] = get_dev_property(dev_node, BLKDEV_PROP_DEV);
 
        // nvme exposes a serial number
-       string serial = get_dev_property(dev_node, "device/serial");
+       string serial = get_dev_property(dev_node, BLKDEV_PROP_SERIAL);
        if (serial.length()) {
          (*pm)[prefix + "serial"] = serial;
        }
 
        // nvme has a device/device/* structure; infer from that.  there
        // is probably a better way?
-       string nvme_vendor = get_dev_property(dev_node, "device/device/vendor");
+       string nvme_vendor = get_dev_property(dev_node, BLKDEV_PROP_VENDOR);
        if (nvme_vendor.length()) {
          (*pm)[prefix + "type"] = "nvme";
        }
index 1dcfd915194fc9ef1881d9e83603f6869d391da8..fbca85c37c05c3e2c5c0a6447fd1b77286317de7 100644 (file)
@@ -49,7 +49,7 @@ TEST(blkdev, get_block_device_base) {
       printf("  got '%s' expected '%s'\n", buf3, de->d_name);
       ASSERT_EQ(0, strcmp(de->d_name, buf3));
       printf("  discard granularity = %lld .. supported = %d\n",
-            (long long)get_block_device_int_property(base, "queue/discard_granularity"),
+            (long long)get_block_device_int_property(base, BLKDEV_PROP_DISCARD_GRANULARITY),
             (int)block_device_support_discard(base));
 
       char subdirfn[PATH_MAX];
@@ -76,7 +76,7 @@ TEST(blkdev, get_block_device_base) {
        printf("  got '%s' expected '%s'\n", buf3, de->d_name);
        ASSERT_EQ(0, strcmp(buf3, de->d_name));
        printf("  discard granularity = %lld .. supported = %d\n",
-              (long long)get_block_device_int_property(part, "queue/discard_granularity"),
+              (long long)get_block_device_int_property(part, BLKDEV_PROP_DISCARD_GRANULARITY),
               (int)block_device_support_discard(part));
       }
 
@@ -109,7 +109,7 @@ TEST(blkdev, get_block_device_string_property)
   set_block_device_sandbox_dir(root.c_str());
 
   char val[1000] = {0};
-  int rc = get_block_device_string_property("sda", "device/model",
+  int rc = get_block_device_string_property("sda", BLKDEV_PROP_MODEL,
                                            val, sizeof(val));
   ASSERT_EQ(0, rc);
   printf("val '%s'\n", val);