Blame SOURCES/0024-libmultipath-make-dm_get_map-status-return-codes-sym.patch

b7337d
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
b7337d
From: Benjamin Marzinski <bmarzins@redhat.com>
b7337d
Date: Tue, 23 Jun 2020 22:17:31 -0500
b7337d
Subject: [PATCH] libmultipath: make dm_get_map/status return codes symbolic
b7337d
b7337d
dm_get_map() and dm_get_status() now use symbolic return codes. They
b7337d
also differentiate between failing to get information from device-mapper
b7337d
and not finding the requested device. These symboilc return codes are
b7337d
also used by update_multipath_* functions.
b7337d
b7337d
Signed-off-by: Benjamin Marzinski <bmarzins@redhat.com>
b7337d
---
b7337d
 libmultipath/devmapper.c   | 51 +++++++++++++++++++++++++-------------
b7337d
 libmultipath/devmapper.h   |  6 +++++
b7337d
 libmultipath/structs_vec.c | 45 +++++++++++++++++++--------------
b7337d
 multipathd/main.c          | 12 ++++-----
b7337d
 4 files changed, 72 insertions(+), 42 deletions(-)
b7337d
b7337d
diff --git a/libmultipath/devmapper.c b/libmultipath/devmapper.c
b7337d
index 13a1cf53..f6204e5f 100644
b7337d
--- a/libmultipath/devmapper.c
b7337d
+++ b/libmultipath/devmapper.c
b7337d
@@ -525,36 +525,43 @@ int dm_map_present(const char * str)
b7337d
 
b7337d
 int dm_get_map(const char *name, unsigned long long *size, char *outparams)
b7337d
 {
b7337d
-	int r = 1;
b7337d
+	int r = DMP_ERR;
b7337d
 	struct dm_task *dmt;
b7337d
 	uint64_t start, length;
b7337d
 	char *target_type = NULL;
b7337d
 	char *params = NULL;
b7337d
 
b7337d
 	if (!(dmt = libmp_dm_task_create(DM_DEVICE_TABLE)))
b7337d
-		return 1;
b7337d
+		return r;
b7337d
 
b7337d
 	if (!dm_task_set_name(dmt, name))
b7337d
 		goto out;
b7337d
 
b7337d
 	dm_task_no_open_count(dmt);
b7337d
 
b7337d
-	if (!dm_task_run(dmt))
b7337d
+	errno = 0;
b7337d
+	if (!dm_task_run(dmt)) {
b7337d
+		if (dm_task_get_errno(dmt) == ENXIO)
b7337d
+			r = DMP_NOT_FOUND;
b7337d
 		goto out;
b7337d
+	}
b7337d
 
b7337d
+	r = DMP_NOT_FOUND;
b7337d
 	/* Fetch 1st target */
b7337d
-	dm_get_next_target(dmt, NULL, &start, &length,
b7337d
-			   &target_type, &params);
b7337d
+	if (dm_get_next_target(dmt, NULL, &start, &length,
b7337d
+			       &target_type, &params) != NULL)
b7337d
+		/* more than one target */
b7337d
+		goto out;
b7337d
 
b7337d
 	if (size)
b7337d
 		*size = length;
b7337d
 
b7337d
 	if (!outparams) {
b7337d
-		r = 0;
b7337d
+		r = DMP_OK;
b7337d
 		goto out;
b7337d
 	}
b7337d
 	if (snprintf(outparams, PARAMS_SIZE, "%s", params) <= PARAMS_SIZE)
b7337d
-		r = 0;
b7337d
+		r = DMP_OK;
b7337d
 out:
b7337d
 	dm_task_destroy(dmt);
b7337d
 	return r;
b7337d
@@ -628,35 +635,45 @@ is_mpath_part(const char *part_name, const char *map_name)
b7337d
 
b7337d
 int dm_get_status(const char *name, char *outstatus)
b7337d
 {
b7337d
-	int r = 1;
b7337d
+	int r = DMP_ERR;
b7337d
 	struct dm_task *dmt;
b7337d
 	uint64_t start, length;
b7337d
 	char *target_type = NULL;
b7337d
 	char *status = NULL;
b7337d
 
b7337d
 	if (!(dmt = libmp_dm_task_create(DM_DEVICE_STATUS)))
b7337d
-		return 1;
b7337d
+		return r;
b7337d
 
b7337d
 	if (!dm_task_set_name(dmt, name))
b7337d
 		goto out;
b7337d
 
b7337d
 	dm_task_no_open_count(dmt);
b7337d
 
b7337d
-	if (!dm_task_run(dmt))
b7337d
+	errno = 0;
b7337d
+	if (!dm_task_run(dmt)) {
b7337d
+		if (dm_task_get_errno(dmt) == ENXIO)
b7337d
+			r = DMP_NOT_FOUND;
b7337d
 		goto out;
b7337d
+	}
b7337d
 
b7337d
+	r = DMP_NOT_FOUND;
b7337d
 	/* Fetch 1st target */
b7337d
-	dm_get_next_target(dmt, NULL, &start, &length,
b7337d
-			   &target_type, &status);
b7337d
+	if (dm_get_next_target(dmt, NULL, &start, &length,
b7337d
+			       &target_type, &status) != NULL)
b7337d
+		goto out;
b7337d
+
b7337d
+	if (!target_type || strcmp(target_type, TGT_MPATH) != 0)
b7337d
+		goto out;
b7337d
+
b7337d
 	if (!status) {
b7337d
 		condlog(2, "get null status.");
b7337d
 		goto out;
b7337d
 	}
b7337d
 
b7337d
 	if (snprintf(outstatus, PARAMS_SIZE, "%s", status) <= PARAMS_SIZE)
b7337d
-		r = 0;
b7337d
+		r = DMP_OK;
b7337d
 out:
b7337d
-	if (r)
b7337d
+	if (r != DMP_OK)
b7337d
 		condlog(0, "%s: error getting map status string", name);
b7337d
 
b7337d
 	dm_task_destroy(dmt);
b7337d
@@ -866,7 +883,7 @@ int _dm_flush_map (const char * mapname, int need_sync, int deferred_remove,
b7337d
 			return 1;
b7337d
 
b7337d
 	if (need_suspend &&
b7337d
-	    !dm_get_map(mapname, &mapsize, params) &&
b7337d
+	    dm_get_map(mapname, &mapsize, params) == DMP_OK &&
b7337d
 	    strstr(params, "queue_if_no_path")) {
b7337d
 		if (!dm_queue_if_no_path(mapname, 0))
b7337d
 			queue_if_no_path = 1;
b7337d
@@ -1075,7 +1092,7 @@ struct multipath *dm_get_multipath(const char *name)
b7337d
 	if (!mpp->alias)
b7337d
 		goto out;
b7337d
 
b7337d
-	if (dm_get_map(name, &mpp->size, NULL))
b7337d
+	if (dm_get_map(name, &mpp->size, NULL) != DMP_OK)
b7337d
 		goto out;
b7337d
 
b7337d
 	dm_get_uuid(name, mpp->wwid, WWID_SIZE);
b7337d
@@ -1259,7 +1276,7 @@ do_foreach_partmaps (const char * mapname,
b7337d
 		    /*
b7337d
 		     * and we can fetch the map table from the kernel
b7337d
 		     */
b7337d
-		    !dm_get_map(names->name, &size, &params[0]) &&
b7337d
+		    dm_get_map(names->name, &size, &params[0]) == DMP_OK &&
b7337d
 
b7337d
 		    /*
b7337d
 		     * and the table maps over the multipath map
b7337d
diff --git a/libmultipath/devmapper.h b/libmultipath/devmapper.h
b7337d
index 7557a86b..adb55000 100644
b7337d
--- a/libmultipath/devmapper.h
b7337d
+++ b/libmultipath/devmapper.h
b7337d
@@ -27,6 +27,12 @@
b7337d
 #define UUID_PREFIX "mpath-"
b7337d
 #define UUID_PREFIX_LEN (sizeof(UUID_PREFIX) - 1)
b7337d
 
b7337d
+enum {
b7337d
+	DMP_ERR,
b7337d
+	DMP_OK,
b7337d
+	DMP_NOT_FOUND,
b7337d
+};
b7337d
+
b7337d
 void dm_init(int verbosity);
b7337d
 void libmp_dm_init(void);
b7337d
 void libmp_udev_set_sync_support(int on);
b7337d
diff --git a/libmultipath/structs_vec.c b/libmultipath/structs_vec.c
b7337d
index 077f2e42..8137ea21 100644
b7337d
--- a/libmultipath/structs_vec.c
b7337d
+++ b/libmultipath/structs_vec.c
b7337d
@@ -196,43 +196,47 @@ extract_hwe_from_path(struct multipath * mpp)
b7337d
 int
b7337d
 update_multipath_table (struct multipath *mpp, vector pathvec, int is_daemon)
b7337d
 {
b7337d
+	int r = DMP_ERR;
b7337d
 	char params[PARAMS_SIZE] = {0};
b7337d
 
b7337d
 	if (!mpp)
b7337d
-		return 1;
b7337d
+		return r;
b7337d
 
b7337d
-	if (dm_get_map(mpp->alias, &mpp->size, params)) {
b7337d
-		condlog(3, "%s: cannot get map", mpp->alias);
b7337d
-		return 1;
b7337d
+	r = dm_get_map(mpp->alias, &mpp->size, params);
b7337d
+	if (r != DMP_OK) {
b7337d
+		condlog(3, "%s: %s", mpp->alias, (r == DMP_ERR)? "error getting table" : "map not present");
b7337d
+		return r;
b7337d
 	}
b7337d
 
b7337d
 	if (disassemble_map(pathvec, params, mpp, is_daemon)) {
b7337d
 		condlog(3, "%s: cannot disassemble map", mpp->alias);
b7337d
-		return 1;
b7337d
+		return DMP_ERR;
b7337d
 	}
b7337d
 
b7337d
-	return 0;
b7337d
+	return DMP_OK;
b7337d
 }
b7337d
 
b7337d
 int
b7337d
 update_multipath_status (struct multipath *mpp)
b7337d
 {
b7337d
+	int r = DMP_ERR;
b7337d
 	char status[PARAMS_SIZE] = {0};
b7337d
 
b7337d
 	if (!mpp)
b7337d
-		return 1;
b7337d
+		return r;
b7337d
 
b7337d
-	if (dm_get_status(mpp->alias, status)) {
b7337d
-		condlog(3, "%s: cannot get status", mpp->alias);
b7337d
-		return 1;
b7337d
+	r = dm_get_status(mpp->alias, status);
b7337d
+	if (r != DMP_OK) {
b7337d
+		condlog(3, "%s: %s", mpp->alias, (r == DMP_ERR)? "error getting status" : "map not present");
b7337d
+		return r;
b7337d
 	}
b7337d
 
b7337d
 	if (disassemble_status(status, mpp)) {
b7337d
 		condlog(3, "%s: cannot disassemble status", mpp->alias);
b7337d
-		return 1;
b7337d
+		return DMP_ERR;
b7337d
 	}
b7337d
 
b7337d
-	return 0;
b7337d
+	return DMP_OK;
b7337d
 }
b7337d
 
b7337d
 void sync_paths(struct multipath *mpp, vector pathvec)
b7337d
@@ -264,10 +268,10 @@ int
b7337d
 update_multipath_strings(struct multipath *mpp, vector pathvec, int is_daemon)
b7337d
 {
b7337d
 	struct pathgroup *pgp;
b7337d
-	int i;
b7337d
+	int i, r = DMP_ERR;
b7337d
 
b7337d
 	if (!mpp)
b7337d
-		return 1;
b7337d
+		return r;
b7337d
 
b7337d
 	update_mpp_paths(mpp, pathvec);
b7337d
 	condlog(4, "%s: %s", mpp->alias, __FUNCTION__);
b7337d
@@ -276,18 +280,21 @@ update_multipath_strings(struct multipath *mpp, vector pathvec, int is_daemon)
b7337d
 	free_pgvec(mpp->pg, KEEP_PATHS);
b7337d
 	mpp->pg = NULL;
b7337d
 
b7337d
-	if (update_multipath_table(mpp, pathvec, is_daemon))
b7337d
-		return 1;
b7337d
+	r = update_multipath_table(mpp, pathvec, is_daemon);
b7337d
+	if (r != DMP_OK)
b7337d
+		return r;
b7337d
+
b7337d
 	sync_paths(mpp, pathvec);
b7337d
 
b7337d
-	if (update_multipath_status(mpp))
b7337d
-		return 1;
b7337d
+	r = update_multipath_status(mpp);
b7337d
+	if (r != DMP_OK)
b7337d
+		return r;
b7337d
 
b7337d
 	vector_foreach_slot(mpp->pg, pgp, i)
b7337d
 		if (pgp->paths)
b7337d
 			path_group_prio_update(pgp);
b7337d
 
b7337d
-	return 0;
b7337d
+	return DMP_OK;
b7337d
 }
b7337d
 
b7337d
 static void enter_recovery_mode(struct multipath *mpp)
b7337d
diff --git a/multipathd/main.c b/multipathd/main.c
b7337d
index 6b7db2c0..e3427d3d 100644
b7337d
--- a/multipathd/main.c
b7337d
+++ b/multipathd/main.c
b7337d
@@ -414,7 +414,7 @@ int __setup_multipath(struct vectors *vecs, struct multipath *mpp,
b7337d
 		goto out;
b7337d
 	}
b7337d
 
b7337d
-	if (update_multipath_strings(mpp, vecs->pathvec, 1)) {
b7337d
+	if (update_multipath_strings(mpp, vecs->pathvec, 1) != DMP_OK) {
b7337d
 		condlog(0, "%s: failed to setup multipath", mpp->alias);
b7337d
 		goto out;
b7337d
 	}
b7337d
@@ -553,9 +553,9 @@ add_map_without_path (struct vectors *vecs, const char *alias)
b7337d
 	mpp->mpe = find_mpe(conf->mptable, mpp->wwid);
b7337d
 	put_multipath_config(conf);
b7337d
 
b7337d
-	if (update_multipath_table(mpp, vecs->pathvec, 1))
b7337d
+	if (update_multipath_table(mpp, vecs->pathvec, 1) != DMP_OK)
b7337d
 		goto out;
b7337d
-	if (update_multipath_status(mpp))
b7337d
+	if (update_multipath_status(mpp) != DMP_OK)
b7337d
 		goto out;
b7337d
 
b7337d
 	if (!vector_alloc_slot(vecs->mpvec))
b7337d
@@ -1346,8 +1346,8 @@ map_discovery (struct vectors * vecs)
b7337d
 		return 1;
b7337d
 
b7337d
 	vector_foreach_slot (vecs->mpvec, mpp, i)
b7337d
-		if (update_multipath_table(mpp, vecs->pathvec, 1) ||
b7337d
-		    update_multipath_status(mpp)) {
b7337d
+		if (update_multipath_table(mpp, vecs->pathvec, 1) != DMP_OK ||
b7337d
+		    update_multipath_status(mpp) != DMP_OK) {
b7337d
 			remove_map(mpp, vecs, 1);
b7337d
 			i--;
b7337d
 		}
b7337d
@@ -2087,7 +2087,7 @@ check_path (struct vectors * vecs, struct path * pp, unsigned int ticks)
b7337d
 	/*
b7337d
 	 * Synchronize with kernel state
b7337d
 	 */
b7337d
-	if (update_multipath_strings(pp->mpp, vecs->pathvec, 1)) {
b7337d
+	if (update_multipath_strings(pp->mpp, vecs->pathvec, 1) != DMP_OK) {
b7337d
 		condlog(1, "%s: Could not synchronize with kernel state",
b7337d
 			pp->dev);
b7337d
 		pp->dmstate = PSTATE_UNDEF;
b7337d
-- 
b7337d
2.17.2
b7337d