Blame SOURCES/0142-RHBZ-1283750-kpartx-fix.patch

f20720
---
f20720
 kpartx/devmapper.c |   17 +++++++++++++--
f20720
 kpartx/devmapper.h |    2 -
f20720
 kpartx/kpartx.c    |   57 ++++++++++++++++++++++++++++++++++++++++++++++++++---
f20720
 3 files changed, 69 insertions(+), 7 deletions(-)
f20720
f20720
Index: multipath-tools-130222/kpartx/devmapper.c
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/kpartx/devmapper.c
f20720
+++ multipath-tools-130222/kpartx/devmapper.c
f20720
@@ -140,12 +140,16 @@ addout:
f20720
 }
f20720
 
f20720
 extern int
f20720
-dm_map_present (char * str)
f20720
+dm_map_present (char * str, char **uuid)
f20720
 {
f20720
 	int r = 0;
f20720
 	struct dm_task *dmt;
f20720
+	const char *uuidtmp;
f20720
 	struct dm_info info;
f20720
 
f20720
+	if (uuid)
f20720
+		*uuid = NULL;
f20720
+
f20720
 	if (!(dmt = dm_task_create(DM_DEVICE_INFO)))
f20720
 		return 0;
f20720
 
f20720
@@ -160,8 +164,15 @@ dm_map_present (char * str)
f20720
 	if (!dm_task_get_info(dmt, &info))
f20720
 		goto out;
f20720
 
f20720
-	if (info.exists)
f20720
-		r = 1;
f20720
+	if (!info.exists)
f20720
+		goto out;
f20720
+
f20720
+	r = 1;
f20720
+	if (uuid) {
f20720
+		uuidtmp = dm_task_get_uuid(dmt);
f20720
+		if (uuidtmp && strlen(uuidtmp))
f20720
+			*uuid = strdup(uuidtmp);
f20720
+	}
f20720
 out:
f20720
 	dm_task_destroy(dmt);
f20720
 	return r;
f20720
Index: multipath-tools-130222/kpartx/devmapper.h
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/kpartx/devmapper.h
f20720
+++ multipath-tools-130222/kpartx/devmapper.h
f20720
@@ -14,7 +14,7 @@ int dm_prereq (char *, int, int, int);
f20720
 int dm_simplecmd (int, const char *, int, uint32_t *, uint16_t);
f20720
 int dm_addmap (int, const char *, const char *, const char *, uint64_t,
f20720
 	       int, const char *, int, mode_t, uid_t, gid_t, uint32_t *);
f20720
-int dm_map_present (char *);
f20720
+int dm_map_present (char *, char **);
f20720
 char * dm_mapname(int major, int minor);
f20720
 dev_t dm_get_first_dep(char *devname);
f20720
 char * dm_mapuuid(int major, int minor);
f20720
Index: multipath-tools-130222/kpartx/kpartx.c
f20720
===================================================================
f20720
--- multipath-tools-130222.orig/kpartx/kpartx.c
f20720
+++ multipath-tools-130222/kpartx/kpartx.c
f20720
@@ -191,6 +191,21 @@ get_hotplug_device(void)
f20720
 	return device;
f20720
 }
f20720
 
f20720
+static int
f20720
+check_uuid(char *uuid, char *part_uuid, char **err_msg) {
f20720
+	char *map_uuid = strchr(part_uuid, '-');
f20720
+	if (!map_uuid || strncmp(part_uuid, "part", 4) != 0) {
f20720
+		*err_msg = "not a kpartx partition";
f20720
+		return -1;
f20720
+	}
f20720
+	map_uuid++;
f20720
+	if (strcmp(uuid, map_uuid) != 0) {
f20720
+		*err_msg = "a partition of a different device";
f20720
+		return -1;
f20720
+	}
f20720
+	return 0;
f20720
+}
f20720
+
f20720
 int
f20720
 main(int argc, char **argv){
f20720
 	int i, j, m, n, op, off, arg, ro=0;
f20720
@@ -410,6 +425,8 @@ main(int argc, char **argv){
f20720
 
f20720
 		case DELETE:
f20720
 			for (j = MAXSLICES-1; j >= 0; j--) {
f20720
+				char *part_uuid, *reason;
f20720
+
f20720
 				if (safe_sprintf(partname, "%s%s%d",
f20720
 					     mapname, delim, j+1)) {
f20720
 					fprintf(stderr, "partname too small\n");
f20720
@@ -417,9 +434,18 @@ main(int argc, char **argv){
f20720
 				}
f20720
 				strip_slash(partname);
f20720
 
f20720
-				if (!dm_map_present(partname))
f20720
+				if (!dm_map_present(partname, &part_uuid))
f20720
 					continue;
f20720
 
f20720
+				if (part_uuid && uuid) {
f20720
+					if (check_uuid(uuid, part_uuid, &reason) != 0) {
f20720
+						fprintf(stderr, "%s is %s. Not removing\n", partname, reason);
f20720
+						free(part_uuid);
f20720
+						continue;
f20720
+					}
f20720
+					free(part_uuid);
f20720
+				}
f20720
+
f20720
 				if (!dm_simplecmd(DM_DEVICE_REMOVE, partname,
f20720
 						  0, &cookie, 0)) {
f20720
 					r++;
f20720
@@ -444,6 +470,8 @@ main(int argc, char **argv){
f20720
 		case UPDATE:
f20720
 			/* ADD and UPDATE share the same code that adds new partitions. */
f20720
 			for (j = 0; j < n; j++) {
f20720
+				char *part_uuid, *reason;
f20720
+
f20720
 				if (slices[j].size == 0)
f20720
 					continue;
f20720
 
f20720
@@ -460,9 +488,19 @@ main(int argc, char **argv){
f20720
 					exit(1);
f20720
 				}
f20720
 
f20720
-				op = (dm_map_present(partname) ?
f20720
+				op = (dm_map_present(partname, &part_uuid) ?
f20720
 					DM_DEVICE_RELOAD : DM_DEVICE_CREATE);
f20720
 
f20720
+				if (part_uuid && uuid) {
f20720
+					if (check_uuid(uuid, part_uuid, &reason) != 0) {
f20720
+						fprintf(stderr, "%s is already in use, and %s\n", partname, reason);
f20720
+						r++;
f20720
+						free(part_uuid);
f20720
+						continue;
f20720
+					}
f20720
+					free(part_uuid);
f20720
+				}
f20720
+
f20720
 				if (!dm_addmap(op, partname, DM_TARGET, params,
f20720
 					       slices[j].size, ro, uuid, j+1,
f20720
 					       buf.st_mode & 0777, buf.st_uid,
f20720
@@ -470,6 +508,7 @@ main(int argc, char **argv){
f20720
 					fprintf(stderr, "create/reload failed on %s\n",
f20720
 						partname);
f20720
 					r++;
f20720
+					continue;
f20720
 				}
f20720
 				if (op == DM_DEVICE_RELOAD &&
f20720
 				    !dm_simplecmd(DM_DEVICE_RESUME, partname,
f20720
@@ -477,6 +516,7 @@ main(int argc, char **argv){
f20720
 					fprintf(stderr, "resume failed on %s\n",
f20720
 						partname);
f20720
 					r++;
f20720
+					continue;
f20720
 				}
f20720
 				dm_devn(partname, &slices[j].major,
f20720
 					&slices[j].minor);
f20720
@@ -494,6 +534,7 @@ main(int argc, char **argv){
f20720
 			}
f20720
 
f20720
 			for (j = MAXSLICES-1; j >= 0; j--) {
f20720
+				char *part_uuid, *reason;
f20720
 				if (safe_sprintf(partname, "%s%s%d",
f20720
 					     mapname, delim, j+1)) {
f20720
 					fprintf(stderr, "partname too small\n");
f20720
@@ -501,9 +542,19 @@ main(int argc, char **argv){
f20720
 				}
f20720
 				strip_slash(partname);
f20720
 
f20720
-				if (slices[j].size || !dm_map_present(partname))
f20720
+				if (slices[j].size ||
f20720
+				    !dm_map_present(partname, &part_uuid))
f20720
 					continue;
f20720
 
f20720
+				if (part_uuid && uuid) {
f20720
+					if (check_uuid(uuid, part_uuid, &reason) != 0) {
f20720
+						fprintf(stderr, "%s is %s. Not removing\n", partname, reason);
f20720
+						free(part_uuid);
f20720
+						continue;
f20720
+					}
f20720
+					free(part_uuid);
f20720
+				}
f20720
+
f20720
 				if (!dm_simplecmd(DM_DEVICE_REMOVE,
f20720
 						  partname, 1, &cookie, 0)) {
f20720
 					r++;