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

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