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

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