Skip to contentSkip to conversation

multipath-tools: Handle tableless DM devices

PATCH New Benjamin Marzinski 1 participant 13 messages Nov 26, 2024 Device mapper

lore
Share & tools
lore

Benjamin Marzinski

author maintainer
#0 lore raw

v3 cover multipath-tools: Handle tableless DM devices

This is another stab at handling tableless multipath devices. The first two patches are reworkings of two patches from my last patchset based on feedback from Martin. The rest are new.

libmp_mapinfo now has a new return value, DMP_EMPTY, for devices that don't have any table. The creation failure path is fixed to check for this value before removing a device after a failed create, instead of just removing any existing DM device with the same name.

libmp_mapinfo now also always fills in info.name, info.uuid and info.dmi if requested, whenever a device is found, even if it returns DMP_NO_MATCH, DMP_EMPTY, or DMP_ERR. dm_find_map_by_wwid() uses this, as well as MAPINFO_MPATH_ONLY, to only return DM_OK for valid multipath devices, but to return the alias of any device that matches the uuid, even if it's not a valid multipath device.

domap() has been updated to use dm_find_map_by_wwid() to check for any DM device that matches the WWID of the device to be created. This lets it handle tableless devices that share the WWID of the device to be created but not its alias. Previously, multipath would fail attempting to create these devices. Now it correctly renames and reloads them when running multipath, starting or reloading multipathd, or running "multipathd add map".

Finally, dm_flush_map__() and do_foreach_partmaps() also handle tabless devices. So multipath -F will remove tabless multipath devices, as well as tableless partition devices on top of multipath devices (tabless or not). I cleanup up a number of minor issues while working on this.

Changes from v2 (based on suggestions from Martin Wilck):
0008: Fixed commit message
0009 & 0010: merged. All future patch number are one less then their
previous number.
0011 & 0012: Fixed commit messages

Changes from v1 (based on discussions with Martin Wilck):
0004: Dropped the flag. libmp_mapinfo now always fills in uuid, name, and
dmi if a device was found and they are non-NULL.
0005: Adapt to the changes in 0004
0007-0013: New patches to handle tabless devices in dm_flush_map__() and
do_foreach_partmaps(), as well as some minor fixes/cleanups I encountered along the way.

Benjamin Marzinski (11):
libmultipath: signal device with no table in libmp_mapinfo
libmultipath: fix removing device after failed creation
libmultipath: set uuid, name, and dmi if a device is found
libmultipath: check table type in dm_find_map_by_wwid
libmultipath: handle a create corner case for empty devices
libmultipath: handle empty maps in dm_flush_map__
libmultipath: factor out code to check if a device is a partition
libmultipath: remove recursive calls in partmap_funcs
libmultipath: assume device is in use if dm_get_opencount fails
libmultipath: accept empty partitions as valid in do_foreach_partmaps
libmultipath: reduce log level for DMP_NOT_FOUND

Martin Wilck (1):
multipath-tools tests: fix mapinfo tests

 libmultipath/configure.c   |  21 +++-
 libmultipath/devmapper.c   | 212 +++++++++++++++++++++----------------
 libmultipath/devmapper.h   |  13 ++-
 libmultipath/structs_vec.c |   5 +-
 multipathd/cli_handlers.c  |   8 +-
 tests/mapinfo.c            |  92 ++++++++++++----
 6 files changed, 235 insertions(+), 116 deletions(-)
Signature · 1 line
2.46.2
author maintainer
#1 lore raw

PATCH v3 1/12 libmultipath: signal device with no table in libmp_mapinfo

if libmp_mapinfo() is run on a device that has no active table, it will now return with a new exit code, DMP_EMPTY, to signal this. Currently all code paths treat this identically to DMP_NOT_FOUND.

---
 libmultipath/devmapper.c | 6 ++++--
 libmultipath/devmapper.h | 7 ++++++-
 2 files changed, 10 insertions(+), 3 deletions(-)
libmultipath/devmapper.c +4 -2
index 224be512..33425d02 100644
@@ -86,6 +86,7 @@ const char *dmp_errstr(int rc)
 		[DMP_OK] = "success",
 		[DMP_NOT_FOUND] = "not found",
 		[DMP_NO_MATCH] = "target type mismatch",
+		[DMP_EMPTY] = "no target",
 		[DMP_LAST__] = "**invalid**",
 	};
 	if (rc < 0 || rc > DMP_LAST__)
@@ -747,9 +748,9 @@ static int libmp_mapinfo__(int flags, mapid_t id, mapinfo_t info, const char *ma
 			condlog(lvl, "%s: map %s has multiple targets", fname__, map_id);
 			return DMP_NO_MATCH;
 		}
-		if (!params) {
+		if (!params || !target_type) {
 			condlog(lvl, "%s: map %s has no targets", fname__, map_id);
-			return DMP_NOT_FOUND;
+			return DMP_EMPTY;
 		}
 		if (flags & MAPINFO_TGT_TYPE__) {
 			const char *tgt_type = flags & MAPINFO_MPATH_ONLY ? TGT_MPATH : TGT_PART;
@@ -873,6 +874,7 @@ int dm_is_mpath(const char *name)
 		return DM_IS_MPATH_YES;
 	case DMP_NOT_FOUND:
 	case DMP_NO_MATCH:
+	case DMP_EMPTY:
 		return DM_IS_MPATH_NO;
 	case DMP_ERR:
 	default:
libmultipath/devmapper.h +6 -1
index 6b3bbad9..23926e3f 100644
@@ -35,6 +35,7 @@ enum {
 	DMP_OK,
 	DMP_NOT_FOUND,
 	DMP_NO_MATCH,
+	DMP_EMPTY,
 	DMP_LAST__,
 };
 
@@ -105,7 +106,11 @@ typedef struct libmp_map_info {
  * @returns:
  *     DMP_OK if successful.
  *     DMP_NOT_FOUND if the map wasn't found, or has no or multiple targets.
- *     DMP_NO_MATCH if the map didn't match @tgt_type (see above).
+ *     DMP_NO_MATCH if the map didn't match @tgt_type (see above) or didn't
+ *                  have a multipath uuid prefix.
+ *     DMP_EMPTY if the map has no table. Note. The check for matching uuid
+ *               prefix will happen first, but the check for matching
+ *               tgt_type will happen afterwards.
  *     DMP_ERR if some other error occurred.
  *
  * This function obtains the requested information for the device-mapper map
Signature · 1 line
2.46.2
author maintainer
#2 lore raw

PATCH v3 2/12 multipath-tools tests: fix mapinfo tests

In reply to Benjamin Marzinski

From: Martin Wilck <[email protected]>

Add tests for the check for empty table introduced in ("libmultipath:
signal device with no table in libmp_mapinfo").

---
 tests/mapinfo.c | 49 +++++++++++++++++++++++++++++++++++++++++++------
 1 file changed, 43 insertions(+), 6 deletions(-)
tests/mapinfo.c +43 -6
index 4362cdb0..ee487989 100644
@@ -47,11 +47,13 @@ static const struct dm_info __attribute__((unused)) MPATH_DMI_02 = {
 	.exists = 1,
 	.live_table = 0,
 	.open_count = 1,
-	.target_count = 1,
+	.target_count = 0,
 	.major = 254,
 	.minor = 123,
 };
 
+#define TGT_ANY "any"
+
 static const char MPATH_NAME_01[] = "mpathx";
 static const char MPATH_UUID_01[] = "mpath-3600a098038302d414b2b4d4453474f62";
 static const char MPATH_TARGET_01[] =
@@ -858,7 +860,7 @@ static void test_mapinfo_good_size(void **state)
 	mock_mapinfo_name_1(DM_DEVICE_STATUS, 1, "foo", 1, 1, 0);
 	WRAP_DM_TASK_GET_INFO(1);
 	WRAP_DM_TASK_GET_INFO(&MPATH_DMI_01);
-	mock_dm_get_next_target(12345, NULL, MPATH_TARGET_01, NULL);
+	mock_dm_get_next_target(12345, TGT_ANY, MPATH_TARGET_01, NULL);
 	rc = libmp_mapinfo(DM_MAP_BY_NAME,
 			   (mapid_t) { .str = "foo", },
 			   (mapinfo_t) { .size = &size });
@@ -889,13 +891,13 @@ static void test_mapinfo_bad_next_target_02(void **state)
 
 	mock_mapinfo_name_1(DM_DEVICE_STATUS, 1, "foo", 1, 1, 0);
 	WRAP_DM_TASK_GET_INFO(1);
-	WRAP_DM_TASK_GET_INFO(&MPATH_DMI_01);
+	WRAP_DM_TASK_GET_INFO(&MPATH_DMI_02);
 	/* no targets */
-	mock_dm_get_next_target(12345, NULL, NULL, NULL);
+	mock_dm_get_next_target(0, NULL, NULL, NULL);
 	rc = libmp_mapinfo(DM_MAP_BY_NAME,
 			   (mapid_t) { .str = "foo", },
 			   (mapinfo_t) { .size = &size });
-	assert_int_equal(rc, DMP_NOT_FOUND);
+	assert_int_equal(rc, DMP_EMPTY);
 }
 
 /* libmp_mapinfo needs to do a DM_DEVICE_STATUS ioctl */
@@ -1049,6 +1051,39 @@ static void test_mapinfo_good_target_type_04(void **state)
 	assert_true(!strcmp(uuid, MPATH_UUID_01));
 }
 
+static void test_mapinfo_no_table_01(void **state)
+{
+	int rc;
+	struct dm_info dmi = { .suspended = 0 };
+
+	mock_mapinfo_name_1(DM_DEVICE_STATUS, 1, "foo", 1, 1, 0);
+	WRAP_DM_TASK_GET_INFO(1);
+	/* DMI with no live table, MAPINFO_CHECK_UUID not set */
+	WRAP_DM_TASK_GET_INFO(&MPATH_DMI_02);
+	mock_dm_get_next_target(0, NULL, NULL, NULL);
+	rc = libmp_mapinfo(DM_MAP_BY_NAME | MAPINFO_MPATH_ONLY,
+			   (mapid_t) { .str = "foo", },
+			   (mapinfo_t) { .dmi = &dmi });
+	assert_int_equal(rc, DMP_EMPTY);
+}
+
+static void test_mapinfo_no_table_02(void **state)
+{
+	int rc;
+	struct dm_info dmi = { .suspended = 0 };
+
+	mock_mapinfo_name_1(DM_DEVICE_STATUS, 1, "foo", 1, 1, 0);
+	WRAP_DM_TASK_GET_INFO(1);
+	/* DMI with no live table, MAPINFO_CHECK_UUID set */
+	WRAP_DM_TASK_GET_INFO(&MPATH_DMI_02);
+	mock_dm_get_next_target(0, NULL, NULL, NULL);
+	will_return(__wrap_dm_task_get_uuid, MPATH_UUID_01);
+	rc = libmp_mapinfo(DM_MAP_BY_NAME | MAPINFO_CHECK_UUID | MAPINFO_MPATH_ONLY,
+			   (mapid_t) { .str = "foo", },
+			   (mapinfo_t) { .dmi = &dmi });
+	assert_int_equal(rc, DMP_EMPTY);
+}
+
 static void test_mapinfo_good_status_01(void **state)
 {
 	int rc;
@@ -1142,7 +1177,7 @@ static void test_mapinfo_bad_task_run_11(void **state)
 	mock_mapinfo_name_1(DM_DEVICE_TABLE, 1, "foo", 1, 1, 0);
 	WRAP_DM_TASK_GET_INFO(1);
 	WRAP_DM_TASK_GET_INFO(&MPATH_DMI_01);
-	mock_dm_get_next_target(12345, NULL, MPATH_TARGET_01, NULL);
+	mock_dm_get_next_target(12345, TGT_ANY, MPATH_TARGET_01, NULL);
 	will_return(__wrap_strdup, 1);
 	/* error in 2nd dm_task_run */
 	mock_mapinfo_name_1(DM_DEVICE_STATUS, 1, "foo", 1, 0, EINVAL);
@@ -1384,6 +1419,8 @@ static int test_mapinfo(void)
 		cmocka_unit_test(test_mapinfo_good_target_type_02),
 		cmocka_unit_test(test_mapinfo_good_target_type_03),
 		cmocka_unit_test(test_mapinfo_good_target_type_04),
+		cmocka_unit_test(test_mapinfo_no_table_01),
+		cmocka_unit_test(test_mapinfo_no_table_02),
 		cmocka_unit_test(test_mapinfo_good_status_01),
 		cmocka_unit_test(test_mapinfo_bad_get_name_01),
 		cmocka_unit_test(test_mapinfo_bad_get_uuid_01),
Signature · 1 line
2.46.2
author maintainer
#3 lore raw

PATCH v3 3/12 libmultipath: fix removing device after failed creation

In reply to Benjamin Marzinski

dm_flush_nap_nosync() only removes a device if it has a multipath table. On failed removes, there is no table, so this function does nothing. Instead, if libmp_mapinfo() returns DMP_EMPTY, then there is an empty map, and it is removed using dm_device_remove().

Also, since there are no longer any callers of dm_flush_map_nosync(), remove it.

---
 libmultipath/devmapper.c | 7 +++++--
 libmultipath/devmapper.h | 1 -
 2 files changed, 5 insertions(+), 3 deletions(-)
libmultipath/devmapper.c +5 -2
index 33425d02..42176667 100644
@@ -557,9 +557,12 @@ int dm_addmap_create (struct multipath *mpp, char * params)
 		 * Failing the second part leaves an empty map. Clean it up.
 		 */
 		err = errno;
-		if (dm_map_present(mpp->alias)) {
+		if (libmp_mapinfo(DM_MAP_BY_NAME | MAPINFO_MPATH_ONLY |
+				  MAPINFO_CHECK_UUID,
+				(mapid_t) { .str = mpp->alias },
+				(mapinfo_t) { .uuid = NULL }) == DMP_EMPTY) {
 			condlog(3, "%s: failed to load map (a path might be in use)", mpp->alias);
-			dm_flush_map_nosync(mpp->alias);
+			dm_device_remove(mpp->alias, 0);
 		}
 		if (err != EROFS) {
 			condlog(3, "%s: failed to load map, error %d",
libmultipath/devmapper.h +0 -1
index 23926e3f..0bd2b907 100644
@@ -174,7 +174,6 @@ enum {
 
 int dm_flush_map__ (const char *mapname, int flags, int retries);
 #define dm_flush_map(mapname) dm_flush_map__(mapname, DMFL_NEED_SYNC, 0)
-#define dm_flush_map_nosync(mapname) dm_flush_map__(mapname, DMFL_NONE, 0)
 #define dm_suspend_and_flush_map(mapname, retries) \
 	dm_flush_map__(mapname, DMFL_NEED_SYNC|DMFL_SUSPEND, retries)
 int dm_flush_map_nopaths(const char * mapname, int deferred_remove);
Signature · 1 line
2.46.2
author maintainer
#4 lore raw

PATCH v3 4/12 libmultipath: set uuid, name, and dmi if a device is found

In reply to Benjamin Marzinski

Instead of only setting the uuid, name, and dmi on DMP_OK, return them whenever we find a device, even if that device doesn't match. To make the code simpler, we set these values before checks that could possibly return DMP_ERR.

The only caller of libmp_mapinfo() that could update a multipath value incorrectly because of this is update_multipath_table(). Fix that.

---
 libmultipath/devmapper.c   | 47 +++++++++++++++++++-------------------
 libmultipath/devmapper.h   |  5 +++-
 libmultipath/structs_vec.c |  5 +++-
 tests/mapinfo.c            | 45 +++++++++++++++++++++++++-----------
 4 files changed, 63 insertions(+), 39 deletions(-)
libmultipath/devmapper.c +23 -24
index 42176667..38c49bd5 100644
@@ -736,6 +736,29 @@ static int libmp_mapinfo__(int flags, mapid_t id, mapinfo_t info, const char *ma
 		&& !(uuid = dm_task_get_uuid(dmt))))
 		return DMP_ERR;
 
+	if (info.name) {
+		strlcpy(info.name, name, WWID_SIZE);
+		condlog(4, "%s: %s: name: \"%s\"", fname__, map_id, info.name);
+	}
+	if (info.uuid) {
+		strlcpy(info.uuid, uuid, DM_UUID_LEN);
+		condlog(4, "%s: %s: uuid: \"%s\"", fname__, map_id, info.uuid);
+	}
+
+	if (info.dmi) {
+		memcpy(info.dmi, &dmi, sizeof(*info.dmi));
+		condlog(4, "%s: %s %d:%d, %d targets, %s table, %s, %s, %d opened, %u events",
+			fname__, map_id,
+			info.dmi->major, info.dmi->minor,
+			info.dmi->target_count,
+			info.dmi->live_table ? "live" :
+				info.dmi->inactive_table ? "inactive" : "no",
+			info.dmi->suspended ? "suspended" : "active",
+			info.dmi->read_only ? "ro" : "rw",
+			info.dmi->open_count,
+			info.dmi->event_nr);
+	}
+
 	if (flags & MAPINFO_CHECK_UUID &&
 	    ((flags & MAPINFO_PART_ONLY && !is_mpath_part_uuid(uuid, NULL)) ||
 	     (!(flags & MAPINFO_PART_ONLY) && !is_mpath_uuid(uuid)))) {
@@ -768,40 +791,16 @@ static int libmp_mapinfo__(int flags, mapid_t id, mapinfo_t info, const char *ma
 
 	/*
 	 * Check possible error conditions.
-	 * If error is returned, don't touch any output parameters.
 	 */
 	if ((info.status && !(tmp_status = strdup(params)))
 	    || (info.target && !tmp_target && !(tmp_target = strdup(params))))
 		return DMP_ERR;
 
-	if (info.name) {
-		strlcpy(info.name, name, WWID_SIZE);
-		condlog(4, "%s: %s: name: \"%s\"", fname__, map_id, info.name);
-	}
-	if (info.uuid) {
-		strlcpy(info.uuid, uuid, DM_UUID_LEN);
-		condlog(4, "%s: %s: uuid: \"%s\"", fname__, map_id, info.uuid);
-	}
-
 	if (info.size) {
 		*info.size = length;
 		condlog(4, "%s: %s: size: %lld", fname__, map_id, *info.size);
 	}
 
-	if (info.dmi) {
-		memcpy(info.dmi, &dmi, sizeof(*info.dmi));
-		condlog(4, "%s: %s %d:%d, %d targets, %s table, %s, %s, %d opened, %u events",
-			fname__, map_id,
-			info.dmi->major, info.dmi->minor,
-			info.dmi->target_count,
-			info.dmi->live_table ? "live" :
-				info.dmi->inactive_table ? "inactive" : "no",
-			info.dmi->suspended ? "suspended" : "active",
-			info.dmi->read_only ? "ro" : "rw",
-			info.dmi->open_count,
-			info.dmi->event_nr);
-	}
-
 	if (info.target) {
 		*info.target = steal_ptr(tmp_target);
 		if (!tgt_set)
libmultipath/devmapper.h +4 -1
index 0bd2b907..4aeefad1 100644
@@ -115,7 +115,10 @@ typedef struct libmp_map_info {
  *
  * This function obtains the requested information for the device-mapper map
  * identified by the input parameters.
- * Output parameters are only filled in if the return value is DMP_OK.
+ * If non-NULL, the name, uuid, and dmi output paramters may be filled in for
+ * any return value besides DMP_NOT_FOUND and will always be filled in for
+ * return values other than DMP_NOT_FOUND and DMP_ERR.
+ * The other parameters are only filled in if the return value is DMP_OK.
  * For target / status / size information, the  map's table should contain
  * only one target (usually multipath or linear).
  */
libmultipath/structs_vec.c +4 -1
index 5df495b3..d22056ca 100644
@@ -504,6 +504,8 @@ update_multipath_table (struct multipath *mpp, vector pathvec, int flags)
 	int r = DMP_ERR;
 	char __attribute__((cleanup(cleanup_charp))) *params = NULL;
 	char __attribute__((cleanup(cleanup_charp))) *status = NULL;
+	/* only set the actual mpp->dmi if libmp_mapinfo returns DMP_OK */
+	struct dm_info dmi;
 	unsigned long long size;
 	struct config *conf;
 
@@ -522,7 +524,7 @@ update_multipath_table (struct multipath *mpp, vector pathvec, int flags)
 				  .target = &params,
 				  .status = &status,
 				  .size = &mpp->size,
-				  .dmi = &mpp->dmi,
+				  .dmi = &dmi,
 			  });
 
 	if (r != DMP_OK) {
@@ -531,6 +533,7 @@ update_multipath_table (struct multipath *mpp, vector pathvec, int flags)
 	} else if (size != mpp->size)
 		condlog(0, "%s: size changed from %llu to %llu", mpp->alias, size, mpp->size);
 
+	mpp->dmi = dmi;
 	return update_multipath_table__(mpp, pathvec, flags, params, status);
 }
 
tests/mapinfo.c +32 -13

114 lines not on this page · raw message

Signature · 1 line
2.46.2
author maintainer
#5 lore raw

PATCH v3 5/12 libmultipath: check table type in dm_find_map_by_wwid

In reply to Benjamin Marzinski

This function is only supposed to work on multipath devices (it adds the multipath uuid prefix to the passed in wwid). Check this, and adapt cli_add_map() to handle the corner case where there is a non-multipath device with a multipath uuid.

---
 libmultipath/devmapper.c  | 2 +-
 multipathd/cli_handlers.c | 8 +++++++-
 2 files changed, 8 insertions(+), 2 deletions(-)
libmultipath/devmapper.c +1 -1
index 38c49bd5..e5f49a5b 100644
@@ -892,7 +892,7 @@ int dm_find_map_by_wwid(const char *wwid, char *name, struct dm_info *dmi)
 	if (safe_sprintf(tmp, UUID_PREFIX "%s", wwid))
 		return DMP_ERR;
 
-	return libmp_mapinfo(DM_MAP_BY_UUID,
+	return libmp_mapinfo(DM_MAP_BY_UUID | MAPINFO_MPATH_ONLY,
 			     (mapid_t) { .str = tmp },
 			     (mapinfo_t) { .name = name, .dmi = dmi });
 }
multipathd/cli_handlers.c +7 -1
index 184c3f91..ec330d81 100644
@@ -725,7 +725,13 @@ cli_add_map (void * v, struct strbuf *reply, void * data)
 		condlog(2, "%s: unknown map.", param);
 		return -ENODEV;
 	}
-	if (dm_find_map_by_wwid(refwwid, alias, &dmi) != DMP_OK) {
+	rc = dm_find_map_by_wwid(refwwid, alias, &dmi);
+	if (rc == DMP_NO_MATCH) {
+		condlog(2, "%s: wwid %s already in use by non-multipath device %s",
+			param, refwwid, alias);
+		return 1;
+	}
+	if (rc != DMP_OK) {
 		condlog(3, "%s: map not present. creating", param);
 		if (coalesce_paths(vecs, NULL, refwwid, FORCE_RELOAD_NONE,
 				   CMD_NONE) != CP_OK) {
Signature · 1 line
2.46.2
author maintainer
#6 lore raw

PATCH v3 6/12 libmultipath: handle a create corner case for empty devices

In reply to Benjamin Marzinski

Multipath already notices if there is any empty device with the same alias and wwid as a device it is trying to create, and switches to a reload. However, if the empty device has a different alias but the same wwid, instead of reloading and renaming the device like it would for a device with a table. multipath will attempt to create a new device and fail.

Fix this by checking for these devices, and doing a reload and rename.

---
 libmultipath/configure.c | 21 ++++++++++++++++++++-
 1 file changed, 20 insertions(+), 1 deletion(-)
libmultipath/configure.c +20 -1
index a7257981..d0e9c958 100644
@@ -835,7 +835,6 @@ int domap(struct multipath *mpp, char *params, int is_daemon)
 {
 	int r = DOMAP_FAIL;
 	struct config *conf;
-	char wwid[WWID_SIZE];
 
 	/*
 	 * last chance to quit before touching the devmaps
@@ -846,6 +845,7 @@ int domap(struct multipath *mpp, char *params, int is_daemon)
 	}
 
 	if (mpp->action == ACT_CREATE) {
+		char wwid[WWID_SIZE];
 		int rc = dm_get_wwid(mpp->alias, wwid, sizeof(wwid));
 
 		if (rc == DMP_OK && !strncmp(mpp->wwid, wwid, sizeof(wwid))) {
@@ -863,7 +863,26 @@ int domap(struct multipath *mpp, char *params, int is_daemon)
 			mpp->action = ACT_REJECT;
 		}
 	}
+	if (mpp->action == ACT_CREATE) {
+		char alias[WWID_SIZE];
+		int rc = dm_find_map_by_wwid(mpp->wwid, alias, NULL);
 
+		if (rc == DMP_NO_MATCH) {
+			condlog(1, "%s: wwid \"%s\" already in use by non-multipath map \"%s\"",
+				mpp->alias, mpp->wwid, alias);
+			mpp->action = ACT_REJECT;
+		} else if (rc == DMP_OK || rc == DMP_EMPTY) {
+			/*
+			 * we already handled the case were rc == DMO_OK and
+			 * the alias == mpp->alias above. So the alias must be
+			 * different here.
+			 */
+			condlog(3, "%s: map already present with a different name \"%s\". reloading",
+				mpp->alias, alias);
+			strlcpy(mpp->alias_old, alias, WWID_SIZE);
+			mpp->action = ACT_RELOAD_RENAME;
+		}
+	}
 	if (mpp->action == ACT_RENAME || mpp->action == ACT_SWITCHPG_RENAME ||
 	    mpp->action == ACT_RELOAD_RENAME ||
 	    mpp->action == ACT_RESIZE_RENAME) {
Signature · 1 line
2.46.2
author maintainer
#7 lore raw

PATCH v3 7/12 libmultipath: handle empty maps in dm_flush_map__

In reply to Benjamin Marzinski

Make dm_flush_map__() remove empty devices, as long as they have valid multipath uuids.

---
 libmultipath/devmapper.c | 9 +++++++--
 1 file changed, 7 insertions(+), 2 deletions(-)
libmultipath/devmapper.c +7 -2
index e5f49a5b..b0bd0d4f 100644
@@ -982,11 +982,16 @@ int dm_flush_map__ (const char *mapname, int flags, int retries)
 	int udev_flags = 0;
 	char *params __attribute__((cleanup(cleanup_charp))) = NULL;
 
-	if (libmp_mapinfo(DM_MAP_BY_NAME | MAPINFO_MPATH_ONLY | MAPINFO_CHECK_UUID,
+	r = libmp_mapinfo(DM_MAP_BY_NAME | MAPINFO_MPATH_ONLY | MAPINFO_CHECK_UUID,
 			  (mapid_t) { .str = mapname },
-			  (mapinfo_t) { .target = &params }) != DMP_OK)
+			  (mapinfo_t) { .target = &params });
+	if (r != DMP_OK && r != DMP_EMPTY)
 		return DM_FLUSH_OK; /* nothing to do */
 
+	/* device mapper will not let you resume an empty device */
+	if (r == DMP_EMPTY)
+		flags &= ~DMFL_SUSPEND;
+
 	/* if the device currently has no partitions, do not
 	   run kpartx on it if you fail to delete it */
 	if (do_foreach_partmaps(mapname, has_partmap, NULL) == 0)
Signature · 1 line
2.46.2
author maintainer
#8 lore raw

PATCH v3 8/12 libmultipath: factor out code to check if a device is a partition

In reply to Benjamin Marzinski

Move the code to check if a device is a valid partition of a multipath device out into its own function. A future patch will make this code more complicated. No functional changes.

---
 libmultipath/devmapper.c | 57 ++++++++++++++++++++--------------------
 1 file changed, 29 insertions(+), 28 deletions(-)
libmultipath/devmapper.c +29 -28
index b0bd0d4f..0df9ee40 100644
@@ -1318,17 +1318,43 @@ char *dm_mapname(int major, int minor)
 	return strdup(name);
 }
 
+static bool
+is_valid_partmap(const char *name, const char *map_dev_t,
+		 const char *map_uuid) {
+	int r;
+	char __attribute__((cleanup(cleanup_charp))) *params = NULL;
+	char *p;
+	char part_uuid[DM_UUID_LEN];
+
+	r = libmp_mapinfo(DM_MAP_BY_NAME | MAPINFO_PART_ONLY | MAPINFO_CHECK_UUID,
+			  (mapid_t) { .str = name },
+			  (mapinfo_t) { .uuid = part_uuid, .target = &params});
+
+	/* There must be a single linear target */
+	if (r != DMP_OK)
+		return false;
+
+	/*
+	 * and the uuid of the target must be a partition of the uuid of the
+	 * multipath device
+	 */
+	if (!is_mpath_part_uuid(part_uuid, map_uuid))
+		return false;
+
+	/* and the table must map over the multipath map */
+	return ((p = strstr(params, map_dev_t)) &&
+		!isdigit(*(p + strlen(map_dev_t))));
+}
+
 static int
 do_foreach_partmaps (const char *mapname,
 		     int (*partmap_func)(const char *, void *),
 		     void *data)
 {
 	struct dm_task __attribute__((cleanup(cleanup_dm_task))) *dmt = NULL;
-	char __attribute__((cleanup(cleanup_charp))) *params = NULL;
 	struct dm_names *names;
 	unsigned next = 0;
 	char dev_t[BLK_DEV_SIZE];
-	char *p;
 	char map_uuid[DM_UUID_LEN];
 	struct dm_info info;
 
@@ -1354,35 +1380,10 @@ do_foreach_partmaps (const char *mapname,
 		return 0;
 
 	do {
-		char part_uuid[DM_UUID_LEN];
-
-		if (
-		    /*
-		     * if there is only a single "linear" target
-		     */
-		    libmp_mapinfo(DM_MAP_BY_NAME | MAPINFO_PART_ONLY | MAPINFO_CHECK_UUID,
-				  (mapid_t) { .str = names->name },
-				  (mapinfo_t) {
-					  .uuid = part_uuid,
-					  .target = &params,
-				  }) == DMP_OK &&
-		    /*
-		     * and the uuid of the target is a partition of the
-		     * uuid of the multipath device
-		     */
-		    is_mpath_part_uuid(part_uuid, map_uuid) &&
-
-		    /*
-		     * and the table maps over the multipath map
-		     */
-		    (p = strstr(params, dev_t)) &&
-		    !isdigit(*(p + strlen(dev_t))) &&
-
+		if (is_valid_partmap(names->name, dev_t, map_uuid) &&
 		    (partmap_func(names->name, data) != 0))
 			return 1;
 
-		free(params);
-		params = NULL;
 		next = names->next;
 		names = (void*) names + next;
 	} while (next);
Signature · 1 line
2.46.2
author maintainer
#9 lore raw

PATCH v3 9/12 libmultipath: remove recursive calls in partmap_funcs

In reply to Benjamin Marzinski

cancel_remove_partmap() and rename_partmap() both could end up calling do_foreach_partmaps() with the partition instead of the original multipath device. This won't ever do anything, since do_foreach_partmaps() checks that it's called with a multipath device.

---
 libmultipath/devmapper.c | 59 +++++++++++++++++++++-------------------
 1 file changed, 31 insertions(+), 28 deletions(-)
libmultipath/devmapper.c +31 -28
index 0df9ee40..751b45d8 100644
@@ -1421,8 +1421,6 @@ dm_remove_partmaps (const char * mapname, int flags)
 static int
 cancel_remove_partmap (const char *name, void *unused __attribute__((unused)))
 {
-	if (dm_get_opencount(name))
-		dm_cancel_remove_partmaps(name);
 	if (dm_message(name, "@cancel_deferred_remove") != 0)
 		condlog(0, "%s: can't cancel deferred remove: %s", name,
 			strerror(errno));
@@ -1481,6 +1479,34 @@ struct rename_data {
 	char *delim;
 };
 
+static int
+dm_rename__(const char *old, char *new, int skip_kpartx)
+{
+	int r = 0;
+	struct dm_task __attribute__((cleanup(cleanup_dm_task))) *dmt = NULL;
+	uint32_t cookie = 0;
+	uint16_t udev_flags = DM_UDEV_DISABLE_LIBRARY_FALLBACK | ((skip_kpartx == SKIP_KPARTX_ON)? MPATH_UDEV_NO_KPARTX_FLAG : 0);
+
+	if (!(dmt = libmp_dm_task_create(DM_DEVICE_RENAME)))
+		return r;
+
+	if (!dm_task_set_name(dmt, old))
+		return r;
+
+	if (!dm_task_set_newname(dmt, new))
+		return r;
+
+	if (!dm_task_set_cookie(dmt, &cookie, udev_flags))
+		return r;
+
+	r = libmp_dm_task_run(dmt);
+	if (!r)
+		dm_log_error(2, DM_DEVICE_RENAME, dmt);
+
+	libmp_udev_wait(cookie);
+	return r;
+}
+
 static int
 rename_partmap (const char *name, void *data)
 {
@@ -1492,7 +1518,7 @@ rename_partmap (const char *name, void *data)
 		return 0;
 	for (offset = strlen(rd->old); name[offset] && !(isdigit(name[offset])); offset++); /* do nothing */
 	if (asprintf(&buff, "%s%s%s", rd->new, rd->delim, name + offset) >= 0) {
-		dm_rename(name, buff, rd->delim, SKIP_KPARTX_OFF);
+		dm_rename__(name, buff, SKIP_KPARTX_OFF);
 		free(buff);
 		condlog(4, "partition map %s renamed", name);
 	} else
@@ -1522,32 +1548,9 @@ dm_rename_partmaps (const char * old, char * new, char *delim)
 int
 dm_rename (const char * old, char * new, char *delim, int skip_kpartx)
 {
-	int r = 0;
-	struct dm_task __attribute__((cleanup(cleanup_dm_task))) *dmt = NULL;
-	uint32_t cookie = 0;
-	uint16_t udev_flags = DM_UDEV_DISABLE_LIBRARY_FALLBACK | ((skip_kpartx == SKIP_KPARTX_ON)? MPATH_UDEV_NO_KPARTX_FLAG : 0);
-
 	if (dm_rename_partmaps(old, new, delim))
-		return r;
-
-	if (!(dmt = libmp_dm_task_create(DM_DEVICE_RENAME)))
-		return r;
-
-	if (!dm_task_set_name(dmt, old))
-		return r;
-
-	if (!dm_task_set_newname(dmt, new))
-		return r;
-
-	if (!dm_task_set_cookie(dmt, &cookie, udev_flags))
-		return r;
-
-	r = libmp_dm_task_run(dmt);
-	if (!r)
-		dm_log_error(2, DM_DEVICE_RENAME, dmt);
-
-	libmp_udev_wait(cookie);
-	return r;
+		return 0;
+	return dm_rename__(old, new, skip_kpartx);
 }
 
 void dm_reassign_deps(char *table, const char *dep, const char *newdep)
Signature · 1 line
2.46.2
author maintainer
#10 lore raw

PATCH v3 10/12 libmultipath: assume device is in use if dm_get_opencount fails

In reply to Benjamin Marzinski

If dm_get_opencount() fails, open_count will be negative, which means mpath_in_use() will never fail. However, dm_flush_map__() will eventually fail. There are multiple callers of dm_get_opencount() that run when trying to remove a device. All the others treat a failed call as if the device was in use, including one that will later be called directly on this device. So save ourselves the work, and exit early

---
 libmultipath/devmapper.c | 5 +++++
 1 file changed, 5 insertions(+)
libmultipath/devmapper.c +5 -0
index 751b45d8..3667c51b 100644
@@ -961,6 +961,11 @@ int mpath_in_use(const char *name)
 {
 	int open_count = dm_get_opencount(name);
 
+	if (open_count < 0) {
+		condlog(0, "%s: %s: failed to get open count, assuming in use",
+			__func__, name);
+		return 1;
+	}
 	if (open_count) {
 		int part_count = 0;
 
Signature · 1 line
2.46.2
author maintainer
#11 lore raw

PATCH v3 11/12 libmultipath: accept empty partitions as valid in do_foreach_partmaps

In reply to Benjamin Marzinski

If the partition is empty, is_valid_partmap() only checks that the uuid matches the multipath device, since there is no table to check for mapping over the multipath device.

The only partmap_func that needs changes to handle empty devices is count_partitions(), which should only count a partition if it has a table (since otherwise it will not have the multipath device open).

---
 libmultipath/devmapper.c | 24 +++++++++++++++++-------
 1 file changed, 17 insertions(+), 7 deletions(-)
libmultipath/devmapper.c +17 -7
index 3667c51b..c96e4b22 100644
@@ -944,16 +944,23 @@ has_partmap(const char *name __attribute__((unused)),
  * This will be called from mpath_in_use, for each partition.
  * If the partition itself in use, returns 1 immediately, causing
  * do_foreach_partmaps() to stop iterating and return 1.
- * Otherwise, increases the partition count.
+ * Otherwise, increases the partition count if the partition has a
+ * table (live or inactive). Devices with no table don't count
+ * towards the multipath device open count.
  */
 static int count_partitions(const char *name, void *data)
 {
+	struct dm_info info;
 	int *ret_count = (int *)data;
-	int open_count = dm_get_opencount(name);
 
-	if (open_count)
+	if (dm_get_info(name, &info) != DMP_OK)
+		return 1;
+
+	if (info.open_count)
 		return 1;
-	(*ret_count)++;
+
+	if (info.live_table || info.inactive_table)
+		(*ret_count)++;
 	return 0;
 }
 
@@ -1335,8 +1342,8 @@ is_valid_partmap(const char *name, const char *map_dev_t,
 			  (mapid_t) { .str = name },
 			  (mapinfo_t) { .uuid = part_uuid, .target = &params});
 
-	/* There must be a single linear target */
-	if (r != DMP_OK)
+	/* There must be a single linear target or an empty map. */
+	if (r != DMP_OK && r != DMP_EMPTY)
 		return false;
 
 	/*
@@ -1346,7 +1353,10 @@ is_valid_partmap(const char *name, const char *map_dev_t,
 	if (!is_mpath_part_uuid(part_uuid, map_uuid))
 		return false;
 
-	/* and the table must map over the multipath map */
+	if (r == DMP_EMPTY)
+		return true;
+
+	/* and if the table isn't empty it must map over the multipath map */
 	return ((p = strstr(params, map_dev_t)) &&
 		!isdigit(*(p + strlen(map_dev_t))));
 }
Signature · 1 line
2.46.2
author maintainer
#12 lore raw

PATCH v3 12/12 libmultipath: reduce log level for DMP_NOT_FOUND

In reply to Benjamin Marzinski

multipath makes many calls just to check if a device exists. Don't warn on each of these.

---
 libmultipath/devmapper.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libmultipath/devmapper.c +1 -1
index c96e4b22..d4dd9540 100644
@@ -727,7 +727,7 @@ static int libmp_mapinfo__(int flags, mapid_t id, mapinfo_t info, const char *ma
 		condlog(2, "%s: dm_task_get_info() failed for %s ", fname__, map_id);
 		return DMP_ERR;
 	} else if(!dmi.exists) {
-		condlog(2, "%s: map %s doesn't exist", fname__, map_id);
+		condlog(3, "%s: map %s doesn't exist", fname__, map_id);
 		return DMP_NOT_FOUND;
 	}
 
Signature · 1 line
2.46.2