Skip to contentSkip to conversation

main - filter-mpath: get wwids from sysfs vpd_pg83

PATCH David Teigland 1 participant 1 message Jun 8, 2022 Library code

lore
Share & tools
lore
#0 lore raw
Gitweb:        https://sourceware.org/git/?p=lvm2.git;a=commitdiff;h=3b0f9cec7e999c33f17714358d2b469bda6967d2
Commit:        3b0f9cec7e999c33f17714358d2b469bda6967d2
Parent:        2bea95764e7322f491c58fae2ec456ab7d60af95
Author:        David Teigland <[email protected]>
AuthorDate:    Mon Jun 6 14:04:20 2022 -0500
Committer:     David Teigland <[email protected]>
CommitterDate: Wed Jun 8 15:06:01 2022 -0500

filter-mpath: get wwids from sysfs vpd_pg83

to compare with wwids in /etc/multipath/wwids when excluding multipath components. The wwid printed from the sysfs wwid file may not be the wwid used in multipath wwids. Save the wwids found for each device on dev->wwids to avoid repeating reading and parsing the sysfs files.

---
 lib/Makefile.in        |   1 +
 lib/device/dev-cache.c |  18 ++++
 lib/device/dev-cache.h |   1 +
 lib/device/dev-mpath.c | 232 +++++++++++++++++++++++++++++++++++++++++--------
 lib/device/device.h    |  13 +++
 lib/device/device_id.c |  31 +++++--
 lib/device/device_id.h |   2 +
 lib/device/parse_vpd.c | 199 ++++++++++++++++++++++++++++++++++++++++++
 8 files changed, 454 insertions(+), 43 deletions(-)
lib/Makefile.in +1 -0
index 22b96134b..3ab5cb2f1 100644
@@ -41,6 +41,7 @@ SOURCES =\
 	device/dev-dasd.c \
 	device/dev-lvm1-pool.c \
 	device/online.c \
+	device/parse_vpd.c \
 	display/display.c \
 	error/errseg.c \
 	unknown/unknown.c \
lib/device/dev-cache.c +18 -0
index ed9c726c9..193eb7585 100644
@@ -80,6 +80,7 @@ static void _dev_init(struct device *dev)
 
 	dm_list_init(&dev->aliases);
 	dm_list_init(&dev->ids);
+	dm_list_init(&dev->wwids);
 }
 
 void dev_destroy_file(struct device *dev)
@@ -383,6 +384,22 @@ out:
 	return 1;
 }
 
+int get_sysfs_binary(const char *path, char *buf, size_t buf_size, int *retlen)
+{
+	int ret;
+	int fd;
+
+	fd = open(path, O_RDONLY);
+	if (fd < 0)
+		return 0;
+	ret = read(fd, buf, buf_size);
+	close(fd);
+	if (ret <= 0)
+		return 0;
+	*retlen = ret;
+	return 1;
+}
+
 int get_sysfs_value(const char *path, char *buf, size_t buf_size, int error_if_no_value)
 {
 	FILE *fp;
@@ -1336,6 +1353,7 @@ int dev_cache_exit(void)
 		dm_hash_iterate(n, _cache.names) {
 			dev = (struct device *) dm_hash_get_data(_cache.names, n);
 			free_dids(&dev->ids);
+			free_wwids(&dev->wwids);
 		}
 	}
 
lib/device/dev-cache.h +1 -0
index 46b1da72c..7ffe01152 100644
@@ -74,6 +74,7 @@ void dev_cache_failed_path(struct device *dev, const char *path);
 bool dev_cache_has_md_with_end_superblock(struct dev_types *dt);
 
 int get_sysfs_value(const char *path, char *buf, size_t buf_size, int error_if_no_value);
+int get_sysfs_binary(const char *path, char *buf, size_t buf_size, int *retlen);
 int get_dm_uuid_from_sysfs(char *buf, size_t buf_size, int major, int minor);
 
 int setup_devices_file(struct cmd_context *cmd);
lib/device/dev-mpath.c +194 -38

323 lines not on this page · raw message

lib/device/device.h +13 -0
index d0d670ec3..06440f44b 100644
@@ -59,6 +59,14 @@ struct dev_ext {
 	void *handle;
 };
 
+#define DEV_WWID_SIZE 128
+
+struct dev_wwid {
+	struct dm_list list;
+	int type;
+	char id[DEV_WWID_SIZE];
+};
+
 #define DEV_ID_TYPE_SYS_WWID   0x0001
 #define DEV_ID_TYPE_SYS_SERIAL 0x0002
 #define DEV_ID_TYPE_MPATH_UUID 0x0003
@@ -105,6 +113,7 @@ struct dev_use {
  */
 struct device {
 	struct dm_list aliases;	/* struct dm_str_list */
+	struct dm_list wwids; /* struct dev_wwid, used for multipath component detection */
 	struct dm_list ids; /* struct dev_id, different entries for different idtypes */
 	struct dev_id *id; /* points to the the ids entry being used for this dev */
 	dev_t dev;
@@ -206,5 +215,9 @@ void dev_destroy_file(struct device *dev);
 
 int dev_mpath_init(const char *config_wwids_file);
 void dev_mpath_exit(void);
+struct dev_wwid *add_wwid(char *id, int id_type, struct dm_list *ids);
+void free_wwids(struct dm_list *ids);
+int parse_vpd_ids(const unsigned char *vpd_data, int vpd_datalen, struct dm_list *ids);
+int format_t10_id(const unsigned char *in, int in_bytes, unsigned char *out, int out_bytes);
 
 #endif
lib/device/device_id.c +26 -5
index f1928347c..9dec9f884 100644
@@ -182,7 +182,9 @@ void free_dids(struct dm_list *ids)
 	}
 }
 
-int read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suffix, char *sysbuf, int sysbufsize)
+static int _read_sys_block(struct cmd_context *cmd, struct device *dev,
+			   const char *suffix, char *sysbuf, int sysbufsize,
+			   int binary, int *retlen)
 {
 	char path[PATH_MAX];
 	dev_t devt = dev->dev;
@@ -196,11 +198,17 @@ int read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suff
 		return 0;
 	}
 
-	get_sysfs_value(path, sysbuf, sysbufsize, 0);
+	if (binary) {
+		ret = get_sysfs_binary(path, sysbuf, sysbufsize, retlen);
+		if (ret && !*retlen)
+			ret = 0;
+	} else {
+		ret = get_sysfs_value(path, sysbuf, sysbufsize, 0);
+		if (ret && !sysbuf[0])
+			ret = 0;
+	}
 
-	if (sysbuf[0]) {
-		if (prim)
-			log_debug("Using primary device_id for partition %s.", dev_name(dev));
+	if (ret) {
 		sysbuf[sysbufsize - 1] = '\0';
 		return 1;
 	}
@@ -220,6 +228,19 @@ int read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suff
 	return 0;
 }
 
+int read_sys_block(struct cmd_context *cmd, struct device *dev,
+		   const char *suffix, char *sysbuf, int sysbufsize)
+{
+	return _read_sys_block(cmd, dev, suffix, sysbuf, sysbufsize, 0, NULL);
+}
+
+int read_sys_block_binary(struct cmd_context *cmd, struct device *dev,
+			  const char *suffix, char *sysbuf, int sysbufsize,
+			  int *retlen)
+{
+	return _read_sys_block(cmd, dev, suffix, sysbuf, sysbufsize, 1, retlen);
+}
+
 static int _dm_uuid_has_prefix(char *sysbuf, const char *prefix)
 {
 	if (!strncmp(sysbuf, prefix, strlen(prefix)))
lib/device/device_id.h +2 -0
index 94773a65e..9b9c9ce03 100644
@@ -58,6 +58,8 @@ void devices_file_exit(struct cmd_context *cmd);
 void unlink_searched_devnames(struct cmd_context *cmd);
 
 int read_sys_block(struct cmd_context *cmd, struct device *dev, const char *suffix, char *sysbuf, int sysbufsize);
+int read_sys_block_binary(struct cmd_context *cmd, struct device *dev,
+			  const char *suffix, char *sysbuf, int sysbufsize, int *retlen);
 
 int dev_has_mpath_uuid(struct cmd_context *cmd, struct device *dev, const char **idname_out);
 
lib/device/parse_vpd.c new file +199 -0

199 lines not on this page · raw message