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

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