Blame 0146-idbw_rec_write-pick-tpgt-from-existing-record.patch

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