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

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