From: Alan Somers Date: Wed, 27 Sep 2017 16:37:02 +0000 (-0600) Subject: blkdev: add an enum type for block device properties X-Git-Tag: v14.1.0~1135^2~4 X-Git-Url: http://git-server-git.apps.pok.os.sepia.ceph.com/?a=commitdiff_plain;h=559461b2840c185211bcfe8cf3bcb89adeb0373c;p=ceph.git blkdev: add an enum type for block device properties Signed-off-by: Alan Somers --- diff --git a/src/common/blkdev.cc b/src/common/blkdev.cc index 5e103e15b603..197cf895120d 100644 --- a/src/common/blkdev.cc +++ b/src/common/blkdev.cc @@ -36,6 +36,8 @@ int get_device_by_path(const char *path, char* partition, char* device, } +#include "common/blkdev.h" + #ifdef __linux__ #include #include @@ -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 {}; } diff --git a/src/common/blkdev.h b/src/common/blkdev.h index 6cbe16b6f88d..a2569c23fc91 100644 --- a/src/common/blkdev.h +++ b/src/common/blkdev.h @@ -4,6 +4,16 @@ #include #include +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); diff --git a/src/os/bluestore/KernelDevice.cc b/src/os/bluestore/KernelDevice.cc index 999394e7eb39..0bf470c537fd 100644 --- a/src/os/bluestore/KernelDevice.cc +++ b/src/os/bluestore/KernelDevice.cc @@ -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 *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"; } diff --git a/src/os/bluestore/PMEMDevice.cc b/src/os/bluestore/PMEMDevice.cc index 5bfa44a9d987..a711c84a7eb6 100644 --- a/src/os/bluestore/PMEMDevice.cc +++ b/src/os/bluestore/PMEMDevice.cc @@ -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 *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"; } diff --git a/src/test/common/test_blkdev.cc b/src/test/common/test_blkdev.cc index 1dcfd915194f..fbca85c37c05 100644 --- a/src/test/common/test_blkdev.cc +++ b/src/test/common/test_blkdev.cc @@ -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);