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

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