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

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