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

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