Blame SOURCES/0146-idbw_rec_write-pick-tpgt-from-existing-record.patch

786c6d
From ef61cd4912e90c8202598f2fa4e9f1842a5b5822 Mon Sep 17 00:00:00 2001
6c64be
From: Chris Leech <cleech@redhat.com>
6c64be
Date: Tue, 13 Aug 2013 12:39:07 -0700
6c64be
Subject: idbw_rec_write, pick tpgt from existing record
6c64be
6c64be
On a static add (-m node -o new) without a user specified tpgt, looks
6c64be
for existing new style records with tpgt before creating an old style
6c64be
record without.  If one exists, take the tpgt from it an write an
6c64be
updated new style record instead.
6c64be
---
786c6d
 usr/idbm.c | 40 ++++++++++++++++++++++++++++++++++++++++
786c6d
 1 file changed, 40 insertions(+)
6c64be
6c64be
diff --git a/usr/idbm.c b/usr/idbm.c
786c6d
index cb6ffd1..0410079 100644
6c64be
--- a/usr/idbm.c
6c64be
+++ b/usr/idbm.c
6c64be
@@ -27,6 +27,7 @@
6c64be
 #include <errno.h>
6c64be
 #include <dirent.h>
6c64be
 #include <limits.h>
6c64be
+#include <glob.h>
6c64be
 #include <sys/stat.h>
6c64be
 #include <sys/file.h>
6c64be
 
786c6d
@@ -162,6 +163,8 @@ static struct idbm *db;
786c6d
 	_n++; \
786c6d
 } while(0)
786c6d
 
786c6d
+static int idbm_remove_disc_to_node_link(node_rec_t *rec, char *portal);
786c6d
+
786c6d
 static void
786c6d
 idbm_recinfo_discovery(discovery_rec_t *r, recinfo_t *ri)
786c6d
 {
786c6d
@@ -2082,12 +2085,49 @@ static int idbm_rec_write_old(node_rec_t *rec)
6c64be
 	FILE *f;
6c64be
 	char *portal;
6c64be
 	int rc = 0;
6c64be
+	glob_t globbuf;
6c64be
+	int i;
6c64be
+	int tpgt = PORTAL_GROUP_TAG_UNKNOWN;
6c64be
 
6c64be
 	portal = malloc(PATH_MAX);
6c64be
 	if (!portal) {
6c64be
 		log_error("Could not alloc portal\n");
6c64be
 		return ISCSI_ERR_NOMEM;
6c64be
 	}
6c64be
+
6c64be
+	/* check for newer portal dir with tpgt */
6c64be
+	snprintf(portal, PATH_MAX, "%s/%s/%s,%d,*", NODE_CONFIG_DIR,
6c64be
+		 rec->name, rec->conn[0].address, rec->conn[0].port);
6c64be
+	rc = glob(portal, GLOB_ONLYDIR, NULL, &globbuf);
6c64be
+	if (!rc) {
6c64be
+		if (globbuf.gl_pathc > 1)
6c64be
+			log_warning("multiple tpg records for portal "
6c64be
+				    "%s/%s:%d found", rec->name,
6c64be
+				    rec->conn[0].address, rec->conn[0].port);
6c64be
+		/* set pattern for sscanf matching of tpgt */
6c64be
+		snprintf(portal, PATH_MAX, "%s/%s/%s,%d,%%u", NODE_CONFIG_DIR,
6c64be
+			 rec->name, rec->conn[0].address, rec->conn[0].port);
6c64be
+		for (i = 0; i < globbuf.gl_pathc; i++) {
6c64be
+			rc = sscanf(globbuf.gl_pathv[i], portal, &tpgt);
6c64be
+			if (rc == 1)
6c64be
+				break;
6c64be
+		}
6c64be
+		if (tpgt == PORTAL_GROUP_TAG_UNKNOWN)
6c64be
+			log_warning("glob match on existing records, "
6c64be
+				    "but no valid tpgt found");
6c64be
+	}
6c64be
+	globfree(&globbuf);
786c6d
+	rc = 0;
6c64be
+
6c64be
+	/* if a tpgt was selected from an old record, write entry in new format */
6c64be
+	if (tpgt != PORTAL_GROUP_TAG_UNKNOWN) {
6c64be
+		log_warning("using tpgt %u from existing record", tpgt);
6c64be
+		rec->tpgt = tpgt;
786c6d
+		rc = idbm_remove_disc_to_node_link(rec, portal);
6c64be
+		free(portal);
6c64be
+		return idbm_rec_write_new(rec);
6c64be
+	}
6c64be
+
6c64be
 	snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR,
6c64be
 		 rec->name, rec->conn[0].address, rec->conn[0].port);
6c64be
 
6c64be
-- 
786c6d
1.8.3.1
6c64be