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

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