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

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