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

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