Blame SOURCES/0004-filter-mpath-get-wwids-from-sysfs-vpd_pg83.patch

d0283b
From 2966df2bcbbf553d86d0a608852dcc140df28fc0 Mon Sep 17 00:00:00 2001
d0283b
From: David Teigland <teigland@redhat.com>
d0283b
Date: Mon, 6 Jun 2022 14:04:20 -0500
d0283b
Subject: [PATCH 4/7] filter-mpath: get wwids from sysfs vpd_pg83
d0283b
d0283b
to compare with wwids in /etc/multipath/wwids when
d0283b
excluding multipath components.  The wwid printed
d0283b
from the sysfs wwid file may not be the wwid used
d0283b
in multipath wwids.  Save the wwids found for each
d0283b
device on dev->wwids to avoid repeating reading
d0283b
and parsing the sysfs files.
d0283b
d0283b
(cherry picked from commit 3b0f9cec7e999c33f17714358d2b469bda6967d2)
d0283b
---
d0283b
 lib/Makefile.in        |   1 +
d0283b
 lib/device/dev-cache.c |  18 ++++
d0283b
 lib/device/dev-cache.h |   1 +
d0283b
 lib/device/dev-mpath.c | 232 ++++++++++++++++++++++++++++++++++-------
d0283b
 lib/device/device.h    |  13 +++
d0283b
 lib/device/device_id.c |  31 +++++-
d0283b
 lib/device/device_id.h |   2 +
d0283b
 lib/device/parse_vpd.c | 199 +++++++++++++++++++++++++++++++++++
d0283b
 8 files changed, 454 insertions(+), 43 deletions(-)
d0283b
 create mode 100644 lib/device/parse_vpd.c
d0283b
d0283b
diff --git a/lib/Makefile.in b/lib/Makefile.in
d0283b
index 22b96134b..3ab5cb2f1 100644
d0283b
--- a/lib/Makefile.in
d0283b
+++ b/lib/Makefile.in
d0283b
@@ -41,6 +41,7 @@ SOURCES =\
d0283b
 	device/dev-dasd.c \
d0283b
 	device/dev-lvm1-pool.c \
d0283b
 	device/online.c \
d0283b
+	device/parse_vpd.c \
d0283b
 	display/display.c \
d0283b
 	error/errseg.c \
d0283b
 	unknown/unknown.c \
d0283b
diff --git a/lib/device/dev-cache.c b/lib/device/dev-cache.c
d0283b
index ed9c726c9..193eb7585 100644
d0283b
--- a/lib/device/dev-cache.c
d0283b
+++ b/lib/device/dev-cache.c
d0283b
@@ -80,6 +80,7 @@ static void _dev_init(struct device *dev)
d0283b
 
d0283b
 	dm_list_init(&dev->aliases);
d0283b
 	dm_list_init(&dev->ids);
d0283b
+	dm_list_init(&dev->wwids);
d0283b
 }
d0283b
 
d0283b
 void dev_destroy_file(struct device *dev)
d0283b
@@ -383,6 +384,22 @@ out:
d0283b
 	return 1;
d0283b
 }
d0283b
 
d0283b
+int get_sysfs_binary(const char *path, char *buf, size_t buf_size, int *retlen)
d0283b
+{
d0283b
+	int ret;
d0283b
+	int fd;
d0283b
+
d0283b
+	fd = open(path, O_RDONLY);
d0283b
+	if (fd < 0)
d0283b
+		return 0;
d0283b
+	ret = read(fd, buf, buf_size);
d0283b
+	close(fd);
d0283b
+	if (ret <= 0)
d0283b
+		return 0;
d0283b
+	*retlen = ret;
d0283b
+	return 1;
d0283b
+}
d0283b
+
d0283b
 int get_sysfs_value(const char *path, char *buf, size_t buf_size, int error_if_no_value)
d0283b
 {
d0283b
 	FILE *fp;
d0283b
@@ -1336,6 +1353,7 @@ int dev_cache_exit(void)
d0283b
 		dm_hash_iterate(n, _cache.names) {
d0283b
 			dev = (struct device *) dm_hash_get_data(_cache.names, n);
d0283b
 			free_dids(&dev->ids);
d0283b
+			free_wwids(&dev->wwids);
d0283b
 		}
d0283b
 	}
d0283b
 
d0283b
diff --git a/lib/device/dev-cache.h b/lib/device/dev-cache.h
d0283b
index 46b1da72c..7ffe01152 100644
d0283b
--- a/lib/device/dev-cache.h
d0283b
+++ b/lib/device/dev-cache.h
d0283b
@@ -74,6 +74,7 @@ void dev_cache_failed_path(struct device *dev, const char *path);
d0283b
 bool dev_cache_has_md_with_end_superblock(struct dev_types *dt);
d0283b
 
d0283b
 int get_sysfs_value(const char *path, char *buf, size_t buf_size, int error_if_no_value);
d0283b
+int get_sysfs_binary(const char *path, char *buf, size_t buf_size, int *retlen);
d0283b
 int get_dm_uuid_from_sysfs(char *buf, size_t buf_size, int major, int minor);
d0283b
 
d0283b
 int setup_devices_file(struct cmd_context *cmd);
d0283b
diff --git a/lib/device/dev-mpath.c b/lib/device/dev-mpath.c
d0283b
index 846f6c8ba..27b0f41a6 100644
d0283b
--- a/lib/device/dev-mpath.c
d0283b
+++ b/lib/device/dev-mpath.c
d0283b
@@ -200,11 +200,12 @@ static void _read_wwid_exclusions(void)
d0283b
 		log_debug("multipath config ignored %d wwids", rem_count);
d0283b
 }
d0283b
 
d0283b
-static void _read_wwid_file(const char *config_wwids_file)
d0283b
+static void _read_wwid_file(const char *config_wwids_file, int *entries)
d0283b
 {
d0283b
 	FILE *fp;
d0283b
 	char line[MAX_WWID_LINE];
d0283b
 	char *wwid, *p;
d0283b
+	char typestr[2] = { 0 };
d0283b
 	int count = 0;
d0283b
 
d0283b
 	if (config_wwids_file[0] != '/') {
d0283b
@@ -226,8 +227,17 @@ static void _read_wwid_file(const char *config_wwids_file)
d0283b
 		if (line[0] == '/')
d0283b
 			wwid++;
d0283b
 
d0283b
-		/* skip the initial '3' */
d0283b
-		wwid++;
d0283b
+
d0283b
+		/*
d0283b
+		 * the initial character is the id type,
d0283b
+		 * 1 is t10, 2 is eui, 3 is naa, 8 is scsi name.
d0283b
+		 * wwids are stored in the hash table without the type charater.
d0283b
+		 * It seems that sometimes multipath does not include
d0283b
+		 * the type charater (seen with t10 scsi_debug devs).
d0283b
+		 */
d0283b
+		typestr[0] = *wwid;
d0283b
+		if (typestr[0] == '1' || typestr[0] == '2' || typestr[0] == '3')
d0283b
+			wwid++;
d0283b
 
d0283b
 		if ((p = strchr(wwid, '/')))
d0283b
 			*p = '\0';
d0283b
@@ -240,6 +250,7 @@ static void _read_wwid_file(const char *config_wwids_file)
d0283b
 		stack;
d0283b
 
d0283b
 	log_debug("multipath wwids read %d from %s", count, config_wwids_file);
d0283b
+	*entries = count;
d0283b
 }
d0283b
 
d0283b
 int dev_mpath_init(const char *config_wwids_file)
d0283b
@@ -247,6 +258,7 @@ int dev_mpath_init(const char *config_wwids_file)
d0283b
 	struct dm_pool *mem;
d0283b
 	struct dm_hash_table *minor_tab;
d0283b
 	struct dm_hash_table *wwid_tab;
d0283b
+	int entries = 0;
d0283b
 
d0283b
 	dm_list_init(&_ignored);
d0283b
 	dm_list_init(&_ignored_exceptions);
d0283b
@@ -283,10 +295,16 @@ int dev_mpath_init(const char *config_wwids_file)
d0283b
 	_wwid_hash_tab = wwid_tab;
d0283b
 
d0283b
 	if (config_wwids_file) {
d0283b
-		_read_wwid_file(config_wwids_file);
d0283b
+		_read_wwid_file(config_wwids_file, &entries);
d0283b
 		_read_wwid_exclusions();
d0283b
 	}
d0283b
 
d0283b
+	if (!entries) {
d0283b
+		/* reading dev wwids is skipped with null wwid_hash_tab */
d0283b
+		dm_hash_destroy(_wwid_hash_tab);
d0283b
+		_wwid_hash_tab = NULL;
d0283b
+	}
d0283b
+
d0283b
 	return 1;
d0283b
 }
d0283b
 
d0283b
@@ -434,10 +452,10 @@ static int _dev_is_mpath_component_udev(struct device *dev)
d0283b
 
d0283b
 /* mpath_devno is major:minor of the dm multipath device currently using the component dev. */
d0283b
 
d0283b
-static int _dev_is_mpath_component_sysfs(struct cmd_context *cmd, struct device *dev, dev_t *mpath_devno)
d0283b
+static int _dev_is_mpath_component_sysfs(struct cmd_context *cmd, struct device *dev,
d0283b
+					 int primary_result, dev_t primary_dev, dev_t *mpath_devno)
d0283b
 {
d0283b
 	struct dev_types *dt = cmd->dev_types;
d0283b
-	const char *part_name;
d0283b
 	const char *name;               /* e.g. "sda" for "/dev/sda" */
d0283b
 	char link_path[PATH_MAX];       /* some obscure, unpredictable sysfs path */
d0283b
 	char holders_path[PATH_MAX];    /* e.g. "/sys/block/sda/holders/" */
d0283b
@@ -451,25 +469,15 @@ static int _dev_is_mpath_component_sysfs(struct cmd_context *cmd, struct device
d0283b
 	int dm_dev_major;
d0283b
 	int dm_dev_minor;
d0283b
 	struct stat info;
d0283b
-	dev_t primary_dev;
d0283b
 	int is_mpath_component = 0;
d0283b
 
d0283b
-	/* multipathing is only known to exist for SCSI or NVME devices */
d0283b
-	if (!major_is_scsi_device(dt, dev_major) && !dev_is_nvme(dt, dev))
d0283b
-		return 0;
d0283b
-
d0283b
-	switch (dev_get_primary_dev(dt, dev, &primary_dev)) {
d0283b
+	switch (primary_result) {
d0283b
 
d0283b
 	case 2: /* The dev is partition. */
d0283b
-		part_name = dev_name(dev); /* name of original dev for log_debug msg */
d0283b
 
d0283b
 		/* gets "foo" for "/dev/foo" where "/dev/foo" comes from major:minor */
d0283b
 		if (!(name = _get_sysfs_name_by_devt(sysfs_dir, primary_dev, link_path, sizeof(link_path))))
d0283b
 			return_0;
d0283b
-
d0283b
-		log_debug_devs("%s: Device is a partition, using primary "
d0283b
-			       "device %s for mpath component detection",
d0283b
-			       part_name, name);
d0283b
 		break;
d0283b
 
d0283b
 	case 1: /* The dev is already a primary dev. Just continue with the dev. */
d0283b
@@ -593,47 +601,189 @@ static int _dev_is_mpath_component_sysfs(struct cmd_context *cmd, struct device
d0283b
 	return is_mpath_component;
d0283b
 }
d0283b
 
d0283b
-static int _dev_in_wwid_file(struct cmd_context *cmd, struct device *dev)
d0283b
+static int _read_sys_wwid(struct cmd_context *cmd, struct device *dev,
d0283b
+			  char *idbuf, int idbufsize)
d0283b
 {
d0283b
-	char sysbuf[PATH_MAX] = { 0 };
d0283b
-	char *wwid;
d0283b
-	long look;
d0283b
+	char idtmp[DEV_WWID_SIZE];
d0283b
 
d0283b
-	if (!_wwid_hash_tab)
d0283b
+	if (!read_sys_block(cmd, dev, "device/wwid", idbuf, idbufsize)) {
d0283b
+		/* the wwid file is not under device for nvme devs */
d0283b
+		if (!read_sys_block(cmd, dev, "wwid", idbuf, idbufsize))
d0283b
+			return 0;
d0283b
+	}
d0283b
+	if (!idbuf[0])
d0283b
 		return 0;
d0283b
 
d0283b
-	if (!read_sys_block(cmd, dev, "device/wwid", sysbuf, sizeof(sysbuf)))
d0283b
+	/* in t10 id, replace series of spaces with one _ like multipath */
d0283b
+	if (!strncmp(idbuf, "t10.", 4) && strchr(idbuf, ' ')) {
d0283b
+		if (idbufsize < DEV_WWID_SIZE)
d0283b
+			return 0;
d0283b
+		memcpy(idtmp, idbuf, DEV_WWID_SIZE);
d0283b
+		memset(idbuf, 0, idbufsize);
d0283b
+		format_t10_id((const unsigned char *)idtmp, DEV_WWID_SIZE, (unsigned char *)idbuf, idbufsize);
d0283b
+	}
d0283b
+	return 1;
d0283b
+}
d0283b
+
d0283b
+#define VPD_SIZE 4096
d0283b
+
d0283b
+static int _read_sys_vpd_wwids(struct cmd_context *cmd, struct device *dev,
d0283b
+			       struct dm_list *ids)
d0283b
+{
d0283b
+	unsigned char vpd_data[VPD_SIZE] = { 0 };
d0283b
+	int vpd_datalen = 0;
d0283b
+
d0283b
+	if (!read_sys_block_binary(cmd, dev, "device/vpd_pg83", (char *)vpd_data, VPD_SIZE, &vpd_datalen))
d0283b
+		return 0;
d0283b
+	if (!vpd_datalen)
d0283b
 		return 0;
d0283b
 
d0283b
-	if (!sysbuf[0])
d0283b
+	/* adds dev_wwid entry to dev->wwids for each id in vpd data */
d0283b
+	parse_vpd_ids(vpd_data, vpd_datalen, ids);
d0283b
+	return 1;
d0283b
+}
d0283b
+
d0283b
+void free_wwids(struct dm_list *ids)
d0283b
+{
d0283b
+	struct dev_wwid *dw, *safe;
d0283b
+
d0283b
+	dm_list_iterate_items_safe(dw, safe, ids) {
d0283b
+		dm_list_del(&dw->list);
d0283b
+		free(dw);
d0283b
+	}
d0283b
+}
d0283b
+
d0283b
+static int _wwid_type_num(char *id)
d0283b
+{
d0283b
+	if (!strncmp(id, "naa.", 4))
d0283b
+		return 3;
d0283b
+	else if (!strncmp(id, "eui.", 4))
d0283b
+		return 2;
d0283b
+	else if (!strncmp(id, "t10.", 4))
d0283b
+		return 1;
d0283b
+	else
d0283b
+		return -1;
d0283b
+}
d0283b
+
d0283b
+/*
d0283b
+ * TODO: if each of the different wwid types (naa/eui/t10) were
d0283b
+ * represented by different DEV_ID_TYPE_FOO values, and used
d0283b
+ * as device_id types, then we could drop struct dev_wwid and
d0283b
+ * drop dev->wwids, and just use dev->ids for each of the
d0283b
+ * different wwids found in vpd_pg83.  This would also require
d0283b
+ * the ability to handle both the original method of replacing
d0283b
+ * every space in the id string with _ and the new/multipath
d0283b
+ * format_t10_id replacing series of spaces with one _.
d0283b
+ */
d0283b
+struct dev_wwid *add_wwid(char *id, int id_type, struct dm_list *ids)
d0283b
+{
d0283b
+	struct dev_wwid *dw;
d0283b
+	int len;
d0283b
+
d0283b
+	if (!id_type) {
d0283b
+		id_type = _wwid_type_num(id);
d0283b
+		if (id_type == -1)
d0283b
+			log_debug("unknown wwid type %s", id);
d0283b
+	}
d0283b
+
d0283b
+	if (!(dw = zalloc(sizeof(struct dev_wwid))))
d0283b
+		return NULL;
d0283b
+	len = strlen(id);
d0283b
+	if (len >= DEV_WWID_SIZE)
d0283b
+		len = DEV_WWID_SIZE - 1;
d0283b
+	memcpy(dw->id, id, len);
d0283b
+	dw->type = id_type;
d0283b
+	dm_list_add(ids, &dw->list);
d0283b
+	return dw;
d0283b
+}
d0283b
+
d0283b
+/*
d0283b
+ * we save ids with format: naa.<value>, eui.<value>, t10.<value>.
d0283b
+ * multipath wwids file uses format: 3<value>, 2<value>, 1<value>.
d0283b
+ * The values are saved in wwid_hash_tab without the type prefix.
d0283b
+ */
d0283b
+
d0283b
+static int _dev_in_wwid_file(struct cmd_context *cmd, struct device *dev,
d0283b
+			     int primary_result, dev_t primary_dev)
d0283b
+{
d0283b
+	char idbuf[DEV_WWID_SIZE] = { 0 };
d0283b
+	struct dev_wwid *dw;
d0283b
+	char *wwid;
d0283b
+
d0283b
+	if (!_wwid_hash_tab)
d0283b
 		return 0;
d0283b
 
d0283b
 	/*
d0283b
-	 * sysfs prints wwid as <typestr>.<value>
d0283b
-	 * multipath wwid uses '3'<value>
d0283b
-	 * does "<typestr>." always correspond to "3"?
d0283b
+	 * Check the primary device, not the partition.
d0283b
 	 */
d0283b
-	if (!(wwid = strchr(sysbuf, '.')))
d0283b
-		return 0;
d0283b
+	if (primary_result == 2) {
d0283b
+		if (!(dev = dev_cache_get_by_devt(cmd, primary_dev))) {
d0283b
+			log_debug("dev_is_mpath_component %s no primary dev", dev_name(dev));
d0283b
+			return 0;
d0283b
+		}
d0283b
+	}
d0283b
 
d0283b
-	/* skip the type and dot, just as '3' was skipped from wwids entry */
d0283b
-	wwid++;
d0283b
-	
d0283b
-	look = (long) dm_hash_lookup_binary(_wwid_hash_tab, wwid, strlen(wwid));
d0283b
+	/*
d0283b
+	 * This function may be called multiple times for the same device, in
d0283b
+	 * particular if partitioned for each partition.
d0283b
+	 */
d0283b
+	if (!dm_list_empty(&dev->wwids))
d0283b
+		goto lookup;
d0283b
 
d0283b
-	if (look) {
d0283b
-		log_debug_devs("dev_is_mpath_component %s multipath wwid %s", dev_name(dev), wwid);
d0283b
-		return 1;
d0283b
+	/*
d0283b
+	 * Get all the ids for the device from vpd_pg83 and check if any of
d0283b
+	 * those are in /etc/multipath/wwids.  These ids should include the
d0283b
+	 * value printed from the sysfs wwid file.
d0283b
+	 */
d0283b
+	_read_sys_vpd_wwids(cmd, dev, &dev->wwids);
d0283b
+	if (!dm_list_empty(&dev->wwids))
d0283b
+		goto lookup;
d0283b
+
d0283b
+	/*
d0283b
+	 * This will read the sysfs wwid file, nvme devices in particular have
d0283b
+	 * a wwid file but not a vpd_pg83 file.
d0283b
+	 */
d0283b
+	if (_read_sys_wwid(cmd, dev, idbuf, sizeof(idbuf)))
d0283b
+		add_wwid(idbuf, 0, &dev->wwids);
d0283b
+
d0283b
+ lookup:
d0283b
+	dm_list_iterate_items(dw, &dev->wwids) {
d0283b
+		if (dw->type == 1 || dw->type == 2 || dw->type == 3)
d0283b
+			wwid = &dw->id[4];
d0283b
+		else
d0283b
+			wwid = dw->id;
d0283b
+
d0283b
+		if (dm_hash_lookup_binary(_wwid_hash_tab, wwid, strlen(wwid))) {
d0283b
+			log_debug_devs("dev_is_mpath_component %s %s in wwids file", dev_name(dev), dw->id);
d0283b
+			return 1;
d0283b
+		}
d0283b
 	}
d0283b
+
d0283b
 	return 0;
d0283b
 }
d0283b
 
d0283b
 int dev_is_mpath_component(struct cmd_context *cmd, struct device *dev, dev_t *holder_devno)
d0283b
 {
d0283b
-	if (_dev_is_mpath_component_sysfs(cmd, dev, holder_devno) == 1)
d0283b
+	struct dev_types *dt = cmd->dev_types;
d0283b
+	int primary_result;
d0283b
+	dev_t primary_dev;
d0283b
+
d0283b
+	/*
d0283b
+	 * multipath only uses SCSI or NVME devices
d0283b
+	 */
d0283b
+	if (!major_is_scsi_device(dt, MAJOR(dev->dev)) && !dev_is_nvme(dt, dev))
d0283b
+		return 0;
d0283b
+
d0283b
+	/*
d0283b
+	 * primary_result 2: dev is a partition, primary_dev is the whole device
d0283b
+	 * primary_result 1: dev is a whole device
d0283b
+	 */
d0283b
+	primary_result = dev_get_primary_dev(dt, dev, &primary_dev);
d0283b
+
d0283b
+	if (_dev_is_mpath_component_sysfs(cmd, dev, primary_result, primary_dev, holder_devno) == 1)
d0283b
 		goto found;
d0283b
 
d0283b
-	if (_dev_in_wwid_file(cmd, dev))
d0283b
+	if (_dev_in_wwid_file(cmd, dev, primary_result, primary_dev))
d0283b
 		goto found;
d0283b
 
d0283b
 	if (external_device_info_source() == DEV_EXT_UDEV) {
d0283b
@@ -641,6 +791,12 @@ int dev_is_mpath_component(struct cmd_context *cmd, struct device *dev, dev_t *h
d0283b
 			goto found;
d0283b
 	}
d0283b
 
d0283b
+	/*
d0283b
+	 * TODO: save the result of this function in dev->flags and use those
d0283b
+	 * flags on repeated calls to avoid repeating the work multiple times
d0283b
+	 * for the same device when there are partitions on the device.
d0283b
+	 */
d0283b
+
d0283b
 	return 0;
d0283b
 found:
d0283b
 	return 1;
d0283b
diff --git a/lib/device/device.h b/lib/device/device.h
d0283b
index d0d670ec3..06440f44b 100644
d0283b
--- a/lib/device/device.h
d0283b
+++ b/lib/device/device.h
d0283b
@@ -59,6 +59,14 @@ struct dev_ext {
d0283b
 	void *handle;
d0283b
 };
d0283b
 
d0283b
+#define DEV_WWID_SIZE 128
d0283b
+
d0283b
+struct dev_wwid {
d0283b
+	struct dm_list list;
d0283b
+	int type;
d0283b
+	char id[DEV_WWID_SIZE];
d0283b
+};
d0283b
+
d0283b
 #define DEV_ID_TYPE_SYS_WWID   0x0001
d0283b
 #define DEV_ID_TYPE_SYS_SERIAL 0x0002
d0283b
 #define DEV_ID_TYPE_MPATH_UUID 0x0003
d0283b
@@ -105,6 +113,7 @@ struct dev_use {
d0283b
  */
d0283b
 struct device {
d0283b
 	struct dm_list aliases;	/* struct dm_str_list */
d0283b
+	struct dm_list wwids; /* struct dev_wwid, used for multipath component detection */
d0283b
 	struct dm_list ids; /* struct dev_id, different entries for different idtypes */
d0283b
 	struct dev_id *id; /* points to the the ids entry being used for this dev */
d0283b
 	dev_t dev;
d0283b
@@ -206,5 +215,9 @@ void dev_destroy_file(struct device *dev);
d0283b
 
d0283b
 int dev_mpath_init(const char *config_wwids_file);
d0283b
 void dev_mpath_exit(void);
d0283b
+struct dev_wwid *add_wwid(char *id, int id_type, struct dm_list *ids);
d0283b
+void free_wwids(struct dm_list *ids);
d0283b
+int parse_vpd_ids(const unsigned char *vpd_data, int vpd_datalen, struct dm_list *ids);
d0283b
+int format_t10_id(const unsigned char *in, int in_bytes, unsigned char *out, int out_bytes);
d0283b
 
d0283b
 #endif
d0283b
diff --git a/lib/device/device_id.c b/lib/device/device_id.c
d0283b
index f1928347c..9dec9f884 100644
d0283b
--- a/lib/device/device_id.c
d0283b
+++ b/lib/device/device_id.c
d0283b
@@ -182,7 +182,9 @@ void free_dids(struct dm_list *ids)
d0283b
 	}
d0283b
 }
d0283b
 
d0283b
-int read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suffix, char *sysbuf, int sysbufsize)
d0283b
+static int _read_sys_block(struct cmd_context *cmd, struct device *dev,
d0283b
+			   const char *suffix, char *sysbuf, int sysbufsize,
d0283b
+			   int binary, int *retlen)
d0283b
 {
d0283b
 	char path[PATH_MAX];
d0283b
 	dev_t devt = dev->dev;
d0283b
@@ -196,11 +198,17 @@ int read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suff
d0283b
 		return 0;
d0283b
 	}
d0283b
 
d0283b
-	get_sysfs_value(path, sysbuf, sysbufsize, 0);
d0283b
+	if (binary) {
d0283b
+		ret = get_sysfs_binary(path, sysbuf, sysbufsize, retlen);
d0283b
+		if (ret && !*retlen)
d0283b
+			ret = 0;
d0283b
+	} else {
d0283b
+		ret = get_sysfs_value(path, sysbuf, sysbufsize, 0);
d0283b
+		if (ret && !sysbuf[0])
d0283b
+			ret = 0;
d0283b
+	}
d0283b
 
d0283b
-	if (sysbuf[0]) {
d0283b
-		if (prim)
d0283b
-			log_debug("Using primary device_id for partition %s.", dev_name(dev));
d0283b
+	if (ret) {
d0283b
 		sysbuf[sysbufsize - 1] = '\0';
d0283b
 		return 1;
d0283b
 	}
d0283b
@@ -220,6 +228,19 @@ int read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suff
d0283b
 	return 0;
d0283b
 }
d0283b
 
d0283b
+int read_sys_block(struct cmd_context *cmd, struct device *dev,
d0283b
+		   const char *suffix, char *sysbuf, int sysbufsize)
d0283b
+{
d0283b
+	return _read_sys_block(cmd, dev, suffix, sysbuf, sysbufsize, 0, NULL);
d0283b
+}
d0283b
+
d0283b
+int read_sys_block_binary(struct cmd_context *cmd, struct device *dev,
d0283b
+			  const char *suffix, char *sysbuf, int sysbufsize,
d0283b
+			  int *retlen)
d0283b
+{
d0283b
+	return _read_sys_block(cmd, dev, suffix, sysbuf, sysbufsize, 1, retlen);
d0283b
+}
d0283b
+
d0283b
 static int _dm_uuid_has_prefix(char *sysbuf, const char *prefix)
d0283b
 {
d0283b
 	if (!strncmp(sysbuf, prefix, strlen(prefix)))
d0283b
diff --git a/lib/device/device_id.h b/lib/device/device_id.h
d0283b
index 94773a65e..9b9c9ce03 100644
d0283b
--- a/lib/device/device_id.h
d0283b
+++ b/lib/device/device_id.h
d0283b
@@ -58,6 +58,8 @@ void devices_file_exit(struct cmd_context *cmd);
d0283b
 void unlink_searched_devnames(struct cmd_context *cmd);
d0283b
 
d0283b
 int read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suffix, char *sysbuf, int sysbufsize);
d0283b
+int read_sys_block_binary(struct cmd_context *cmd, struct device *dev,
d0283b
+			  const char *suffix, char *sysbuf, int sysbufsize, int *retlen);
d0283b
 
d0283b
 int dev_has_mpath_uuid(struct cmd_context *cmd, struct device *dev, const char **idname_out);
d0283b
 
d0283b
diff --git a/lib/device/parse_vpd.c b/lib/device/parse_vpd.c
d0283b
new file mode 100644
d0283b
index 000000000..4bafa7b9e
d0283b
--- /dev/null
d0283b
+++ b/lib/device/parse_vpd.c
d0283b
@@ -0,0 +1,199 @@
d0283b
+/*
d0283b
+ * Copyright (C) 2022 Red Hat, Inc. All rights reserved.
d0283b
+ *
d0283b
+ * This file is part of LVM2.
d0283b
+ *
d0283b
+ * This copyrighted material is made available to anyone wishing to use,
d0283b
+ * modify, copy, or redistribute it subject to the terms and conditions
d0283b
+ * of the GNU Lesser General Public License v.2.1.
d0283b
+ *
d0283b
+ * You should have received a copy of the GNU Lesser General Public License
d0283b
+ * along with this program; if not, write to the Free Software Foundation,
d0283b
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
d0283b
+ */
d0283b
+
d0283b
+#include "base/memory/zalloc.h"
d0283b
+#include "lib/misc/lib.h"
d0283b
+#include "lib/device/device.h"
d0283b
+
d0283b
+#include <stdio.h>
d0283b
+#include <unistd.h>
d0283b
+#include <stdint.h>
d0283b
+#include <stdlib.h>
d0283b
+#include <stdarg.h>
d0283b
+#include <string.h>
d0283b
+#include <inttypes.h>
d0283b
+#include <sys/types.h>
d0283b
+#include <sys/ioctl.h>
d0283b
+#include <sys/stat.h>
d0283b
+#include <fcntl.h>
d0283b
+#include <ctype.h>
d0283b
+#include <limits.h>
d0283b
+#include <dirent.h>
d0283b
+#include <errno.h>
d0283b
+#include <stdbool.h>
d0283b
+#include <assert.h>
d0283b
+
d0283b
+/*
d0283b
+ * Replace series of spaces with a single _.
d0283b
+ */
d0283b
+int format_t10_id(const unsigned char *in, int in_bytes, unsigned char *out, int out_bytes)
d0283b
+{
d0283b
+	int in_space = 0;
d0283b
+	int retlen = 0;
d0283b
+	int j = 0;
d0283b
+	int i;
d0283b
+
d0283b
+	for (i = 0; i < in_bytes; i++) {
d0283b
+		if (!in[i])
d0283b
+			break;
d0283b
+		if (j >= (out_bytes - 2))
d0283b
+			break;
d0283b
+		/* skip leading spaces */
d0283b
+		if (!retlen && (in[i] == ' '))
d0283b
+			continue;
d0283b
+		/* replace one or more spaces with _ */
d0283b
+		if (in[i] == ' ') {
d0283b
+			in_space = 1;
d0283b
+			continue;
d0283b
+		}
d0283b
+		/* spaces are finished so insert _ */
d0283b
+		if (in_space) {
d0283b
+			out[j++] = '_';
d0283b
+			in_space = 0;
d0283b
+			retlen++;
d0283b
+		}
d0283b
+		out[j++] = in[i];
d0283b
+		retlen++;
d0283b
+	}
d0283b
+	return retlen;
d0283b
+}
d0283b
+
d0283b
+static int _to_hex(const unsigned char *in, int in_bytes, unsigned char *out, int out_bytes)
d0283b
+{
d0283b
+	int off = 0;
d0283b
+	int num;
d0283b
+	int i;
d0283b
+
d0283b
+	for (i = 0; i < in_bytes; i++) {
d0283b
+		num = sprintf((char *)out + off, "%02x", in[i]);
d0283b
+		if (num < 0)
d0283b
+			break;
d0283b
+		off += num;
d0283b
+		if (off + 2 >= out_bytes)
d0283b
+			break;
d0283b
+	}
d0283b
+	return off;
d0283b
+}
d0283b
+
d0283b
+#define ID_BUFSIZE 1024
d0283b
+
d0283b
+/*
d0283b
+ * based on linux kernel function
d0283b
+ */
d0283b
+int parse_vpd_ids(const unsigned char *vpd_data, int vpd_datalen, struct dm_list *ids)
d0283b
+{
d0283b
+	char id[ID_BUFSIZE];
d0283b
+	unsigned char tmp_str[ID_BUFSIZE];
d0283b
+	const unsigned char *d, *cur_id_str;
d0283b
+	size_t id_len = ID_BUFSIZE;
d0283b
+	int id_size = -1;
d0283b
+	uint8_t cur_id_size = 0;
d0283b
+
d0283b
+	memset(id, 0, ID_BUFSIZE);
d0283b
+	for (d = vpd_data + 4;
d0283b
+	     d < vpd_data + vpd_datalen;
d0283b
+	     d += d[3] + 4) {
d0283b
+		memset(tmp_str, 0, sizeof(tmp_str));
d0283b
+
d0283b
+		switch (d[1] & 0xf) {
d0283b
+		case 0x1:
d0283b
+			/* T10 Vendor ID */
d0283b
+			cur_id_size = d[3];
d0283b
+			if (cur_id_size + 4 > id_len)
d0283b
+				cur_id_size = id_len - 4;
d0283b
+			cur_id_str = d + 4;
d0283b
+			format_t10_id(cur_id_str, cur_id_size, tmp_str, sizeof(tmp_str));
d0283b
+			id_size = snprintf(id, ID_BUFSIZE, "t10.%s", tmp_str);
d0283b
+			if (id_size < 0)
d0283b
+				break;
d0283b
+			if (id_size >= ID_BUFSIZE)
d0283b
+				id_size = ID_BUFSIZE - 1;
d0283b
+			add_wwid(id, 1, ids);
d0283b
+			break;
d0283b
+		case 0x2:
d0283b
+			/* EUI-64 */
d0283b
+			cur_id_size = d[3];
d0283b
+			cur_id_str = d + 4;
d0283b
+			switch (cur_id_size) {
d0283b
+			case 8:
d0283b
+				_to_hex(cur_id_str, 8, tmp_str, sizeof(tmp_str));
d0283b
+				id_size = snprintf(id, ID_BUFSIZE, "eui.%s", tmp_str);
d0283b
+				break;
d0283b
+			case 12:
d0283b
+				_to_hex(cur_id_str, 12, tmp_str, sizeof(tmp_str));
d0283b
+				id_size = snprintf(id, ID_BUFSIZE, "eui.%s", tmp_str);
d0283b
+				break;
d0283b
+			case 16:
d0283b
+				_to_hex(cur_id_str, 16, tmp_str, sizeof(tmp_str));
d0283b
+				id_size = snprintf(id, ID_BUFSIZE, "eui.%s", tmp_str);
d0283b
+				break;
d0283b
+			default:
d0283b
+				break;
d0283b
+			}
d0283b
+			if (id_size < 0)
d0283b
+				break;
d0283b
+			if (id_size >= ID_BUFSIZE)
d0283b
+				id_size = ID_BUFSIZE - 1;
d0283b
+			add_wwid(id, 2, ids);
d0283b
+			break;
d0283b
+		case 0x3:
d0283b
+			/* NAA */
d0283b
+			cur_id_size = d[3];
d0283b
+			cur_id_str = d + 4;
d0283b
+			switch (cur_id_size) {
d0283b
+			case 8:
d0283b
+				_to_hex(cur_id_str, 8, tmp_str, sizeof(tmp_str));
d0283b
+				id_size = snprintf(id, ID_BUFSIZE, "naa.%s", tmp_str);
d0283b
+				break;
d0283b
+			case 16:
d0283b
+				_to_hex(cur_id_str, 16, tmp_str, sizeof(tmp_str));
d0283b
+				id_size = snprintf(id, ID_BUFSIZE, "naa.%s", tmp_str);
d0283b
+				break;
d0283b
+			default:
d0283b
+				break;
d0283b
+			}
d0283b
+			if (id_size < 0)
d0283b
+				break;
d0283b
+			if (id_size >= ID_BUFSIZE)
d0283b
+				id_size = ID_BUFSIZE - 1;
d0283b
+			add_wwid(id, 3, ids);
d0283b
+			break;
d0283b
+		case 0x8:
d0283b
+			/* SCSI name string */
d0283b
+			cur_id_size = d[3];
d0283b
+			cur_id_str = d + 4;
d0283b
+			if (cur_id_size >= id_len)
d0283b
+				cur_id_size = id_len - 1;
d0283b
+			memcpy(id, cur_id_str, cur_id_size);
d0283b
+			id_size = cur_id_size;
d0283b
+
d0283b
+			/*
d0283b
+			 * Not in the kernel version, copying multipath code,
d0283b
+			 * which checks if this string begins with naa or eui
d0283b
+			 * and if so does tolower() on the chars.
d0283b
+			 */
d0283b
+			if (!strncmp(id, "naa.", 4) || !strncmp(id, "eui.", 4)) {
d0283b
+				int i;
d0283b
+				for (i = 0; i < id_size; i++)
d0283b
+					id[i] = tolower(id[i]);
d0283b
+			}
d0283b
+			add_wwid(id, 8, ids);
d0283b
+			break;
d0283b
+		default:
d0283b
+			break;
d0283b
+		}
d0283b
+	}
d0283b
+
d0283b
+	return id_size;
d0283b
+}
d0283b
-- 
d0283b
2.34.3
d0283b