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