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

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