Blame SOURCES/0093-UPBZ-1086825-user-friendly-name-remap.patch

1eb31d
---
1eb31d
 libmultipath/alias.c       |   64 ++++++++++++++++++++++++++++++++++++++++++---
1eb31d
 libmultipath/alias.h       |    2 +
1eb31d
 libmultipath/propsel.c     |   32 +++++++++++++++-------
1eb31d
 libmultipath/structs_vec.c |   15 ++++++++++
1eb31d
 4 files changed, 100 insertions(+), 13 deletions(-)
1eb31d
1eb31d
Index: multipath-tools-130222/libmultipath/alias.c
1eb31d
===================================================================
1eb31d
--- multipath-tools-130222.orig/libmultipath/alias.c
1eb31d
+++ multipath-tools-130222/libmultipath/alias.c
1eb31d
@@ -145,7 +145,7 @@ lookup_binding(FILE *f, char *map_wwid,
1eb31d
 }
1eb31d
 
1eb31d
 static int
1eb31d
-rlookup_binding(FILE *f, char *buff, char *map_alias)
1eb31d
+rlookup_binding(FILE *f, char *buff, char *map_alias, char *prefix)
1eb31d
 {
1eb31d
 	char line[LINE_MAX];
1eb31d
 	unsigned int line_nr = 0;
1eb31d
@@ -164,7 +164,7 @@ rlookup_binding(FILE *f, char *buff, cha
1eb31d
 		alias = strtok(line, " \t");
1eb31d
 		if (!alias) /* blank line */
1eb31d
 			continue;
1eb31d
-		curr_id = scan_devname(alias, NULL); /* TBD: Why this call? */
1eb31d
+		curr_id = scan_devname(alias, prefix);
1eb31d
 		if (curr_id >= id)
1eb31d
 			id = curr_id + 1;
1eb31d
 		wwid = strtok(NULL, " \t");
1eb31d
@@ -188,6 +188,11 @@ rlookup_binding(FILE *f, char *buff, cha
1eb31d
 		}
1eb31d
 	}
1eb31d
 	condlog(3, "No matching alias [%s] in bindings file.", map_alias);
1eb31d
+
1eb31d
+	/* Get the theoretical id for this map alias.
1eb31d
+	 * Used by use_existing_alias
1eb31d
+	 */
1eb31d
+	id = scan_devname(map_alias, prefix);
1eb31d
 	return id;
1eb31d
 }
1eb31d
 
1eb31d
@@ -237,6 +242,59 @@ allocate_binding(int fd, char *wwid, int
1eb31d
 }
1eb31d
 
1eb31d
 char *
1eb31d
+use_existing_alias (char *wwid, char *file, char *alias_old,
1eb31d
+		char *prefix, int bindings_read_only)
1eb31d
+{
1eb31d
+	char *alias = NULL;
1eb31d
+	int id = 0;
1eb31d
+	int fd, can_write;
1eb31d
+	char buff[WWID_SIZE];
1eb31d
+	FILE *f;
1eb31d
+
1eb31d
+	fd = open_file(file, &can_write, BINDINGS_FILE_HEADER);
1eb31d
+	if (fd < 0)
1eb31d
+		return NULL;
1eb31d
+
1eb31d
+	f = fdopen(fd, "r");
1eb31d
+	if (!f) {
1eb31d
+		condlog(0, "cannot fdopen on bindings file descriptor");
1eb31d
+		close(fd);
1eb31d
+		return NULL;
1eb31d
+	}
1eb31d
+	/* lookup the binding. if it exsists, the wwid will be in buff
1eb31d
+	 * either way, id contains the id for the alias
1eb31d
+	 */
1eb31d
+	id = rlookup_binding(f , buff,  alias_old, prefix);
1eb31d
+	if (id < 0)
1eb31d
+		goto out;
1eb31d
+
1eb31d
+	if (strlen(buff) > 0) {
1eb31d
+		/* if buff is our wwid, it's already
1eb31d
+		 * allocated correctly
1eb31d
+		 */
1eb31d
+		if (strcmp(buff, wwid) == 0)
1eb31d
+			alias = STRDUP(alias_old);
1eb31d
+		else {
1eb31d
+			alias = NULL;
1eb31d
+			condlog(0, "alias %s already bound to wwid %s, cannot reuse",
1eb31d
+				alias_old, buff);
1eb31d
+		}
1eb31d
+		goto out;	
1eb31d
+	}
1eb31d
+
1eb31d
+	/* allocate the existing alias in the bindings file */
1eb31d
+	if (can_write && id && !bindings_read_only) {
1eb31d
+		alias = allocate_binding(fd, wwid, id, prefix);
1eb31d
+		condlog(0, "Allocated existing binding [%s] for WWID [%s]",
1eb31d
+			alias, wwid);
1eb31d
+	}
1eb31d
+
1eb31d
+out:
1eb31d
+	fclose(f);
1eb31d
+	return alias;
1eb31d
+}
1eb31d
+
1eb31d
+char *
1eb31d
 get_user_friendly_alias(char *wwid, char *file, char *prefix,
1eb31d
 			int bindings_read_only)
1eb31d
 {
1eb31d
@@ -305,7 +363,7 @@ get_user_friendly_wwid(char *alias, char
1eb31d
 		return -1;
1eb31d
 	}
1eb31d
 
1eb31d
-	rlookup_binding(f, buff, alias);
1eb31d
+	rlookup_binding(f, buff, alias, NULL);
1eb31d
 	if (!strlen(buff)) {
1eb31d
 		fclose(f);
1eb31d
 		return -1;
1eb31d
Index: multipath-tools-130222/libmultipath/alias.h
1eb31d
===================================================================
1eb31d
--- multipath-tools-130222.orig/libmultipath/alias.h
1eb31d
+++ multipath-tools-130222/libmultipath/alias.h
1eb31d
@@ -10,3 +10,5 @@
1eb31d
 char *get_user_friendly_alias(char *wwid, char *file, char *prefix,
1eb31d
 			      int bindings_readonly);
1eb31d
 int get_user_friendly_wwid(char *alias, char *buff, char *file);
1eb31d
+char *use_existing_alias (char *wwid, char *file, char *alias_old,
1eb31d
+		char *prefix, int bindings_read_only);
1eb31d
Index: multipath-tools-130222/libmultipath/propsel.c
1eb31d
===================================================================
1eb31d
--- multipath-tools-130222.orig/libmultipath/propsel.c
1eb31d
+++ multipath-tools-130222/libmultipath/propsel.c
1eb31d
@@ -253,19 +253,31 @@ want_user_friendly_names(struct multipat
1eb31d
 extern int
1eb31d
 select_alias (struct multipath * mp)
1eb31d
 {
1eb31d
-	if (mp->mpe && mp->mpe->alias)
1eb31d
+	if (mp->mpe && mp->mpe->alias) {
1eb31d
 		mp->alias = STRDUP(mp->mpe->alias);
1eb31d
-	else {
1eb31d
-		mp->alias = NULL;
1eb31d
-		if (want_user_friendly_names(mp)) {
1eb31d
-			select_alias_prefix(mp);
1eb31d
-			mp->alias = get_user_friendly_alias(mp->wwid,
1eb31d
-					conf->bindings_file, mp->alias_prefix, conf->bindings_read_only);
1eb31d
-		}
1eb31d
-		if (mp->alias == NULL)
1eb31d
-			mp->alias = STRDUP(mp->wwid);
1eb31d
+		goto out;
1eb31d
 	}
1eb31d
 
1eb31d
+	mp->alias = NULL;
1eb31d
+	if (!want_user_friendly_names(mp))
1eb31d
+		goto out;
1eb31d
+
1eb31d
+	select_alias_prefix(mp);
1eb31d
+	
1eb31d
+	if (strlen(mp->alias_old) > 0) {
1eb31d
+		mp->alias = use_existing_alias(mp->wwid, conf->bindings_file,
1eb31d
+				mp->alias_old, mp->alias_prefix,
1eb31d
+				conf->bindings_read_only);
1eb31d
+		memset (mp->alias_old, 0, WWID_SIZE);
1eb31d
+	} 
1eb31d
+
1eb31d
+	if (mp->alias == NULL)
1eb31d
+		mp->alias = get_user_friendly_alias(mp->wwid,
1eb31d
+				conf->bindings_file, mp->alias_prefix, conf->bindings_read_only);
1eb31d
+out:
1eb31d
+	if (mp->alias == NULL)
1eb31d
+		mp->alias = STRDUP(mp->wwid);
1eb31d
+
1eb31d
 	return mp->alias ? 0 : 1;
1eb31d
 }
1eb31d
 
1eb31d
Index: multipath-tools-130222/libmultipath/structs_vec.c
1eb31d
===================================================================
1eb31d
--- multipath-tools-130222.orig/libmultipath/structs_vec.c
1eb31d
+++ multipath-tools-130222/libmultipath/structs_vec.c
1eb31d
@@ -430,6 +430,20 @@ out:
1eb31d
 	return NULL;
1eb31d
 }
1eb31d
 
1eb31d
+static void
1eb31d
+find_existing_alias (struct multipath * mpp,
1eb31d
+		     struct vectors *vecs)
1eb31d
+{
1eb31d
+	struct multipath * mp;
1eb31d
+	int i;
1eb31d
+
1eb31d
+	vector_foreach_slot (vecs->mpvec, mp, i)
1eb31d
+		if (strcmp(mp->wwid, mpp->wwid) == 0) {
1eb31d
+			strncpy(mpp->alias_old, mp->alias, WWID_SIZE);
1eb31d
+			return;
1eb31d
+		}
1eb31d
+}
1eb31d
+
1eb31d
 extern struct multipath *
1eb31d
 add_map_with_path (struct vectors * vecs,
1eb31d
 		   struct path * pp, int add_vec)
1eb31d
@@ -443,6 +457,7 @@ add_map_with_path (struct vectors * vecs
1eb31d
 	mpp->hwe = pp->hwe;
1eb31d
 
1eb31d
 	strcpy(mpp->wwid, pp->wwid);
1eb31d
+	find_existing_alias(mpp, vecs);
1eb31d
 	if (select_alias(mpp))
1eb31d
 		goto out;
1eb31d
 	mpp->size = pp->size;