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

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