Blame 0145-idbm_rec_write-seperate-old-and-new-style-writes.patch

c2d5d2
From 954a9492b5ed1de5907ad2a7d7cc0ae6215d8fac Mon Sep 17 00:00:00 2001
c2d5d2
From: Chris Leech <cleech@redhat.com>
c2d5d2
Date: Tue, 13 Aug 2013 11:34:31 -0700
c2d5d2
Subject: idbm_rec_write, seperate old and new style writes
c2d5d2
c2d5d2
Duplicates a small bit of code, but easier to understand and extened.
c2d5d2
---
c2d5d2
 usr/idbm.c | 116 +++++++++++++++++++++++++++++++++++++++++--------------------
c2d5d2
 1 file changed, 79 insertions(+), 37 deletions(-)
c2d5d2
c2d5d2
diff --git a/usr/idbm.c b/usr/idbm.c
c2d5d2
index 0a88699..cb6ffd1 100644
c2d5d2
--- a/usr/idbm.c
c2d5d2
+++ b/usr/idbm.c
c2d5d2
@@ -1808,7 +1808,7 @@ mkdir_portal:
c2d5d2
 	return f;
c2d5d2
 }
c2d5d2
 
c2d5d2
-static int idbm_rec_write(node_rec_t *rec)
c2d5d2
+static int idbm_rec_write_new(node_rec_t *rec)
c2d5d2
 {
c2d5d2
 	struct stat statb;
c2d5d2
 	FILE *f;
c2d5d2
@@ -1820,38 +1820,8 @@ static int idbm_rec_write(node_rec_t *rec)
c2d5d2
 		log_error("Could not alloc portal\n");
c2d5d2
 		return ISCSI_ERR_NOMEM;
c2d5d2
 	}
c2d5d2
-
c2d5d2
-	snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
c2d5d2
-	if (access(portal, F_OK) != 0) {
c2d5d2
-		if (mkdir(portal, 0660) != 0) {
c2d5d2
-			log_error("Could not make %s: %s\n", portal,
c2d5d2
-				  strerror(errno));
c2d5d2
-			rc = ISCSI_ERR_IDBM;
c2d5d2
-			goto free_portal;
c2d5d2
-		}
c2d5d2
-	}
c2d5d2
-
c2d5d2
-	snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
c2d5d2
-	if (access(portal, F_OK) != 0) {
c2d5d2
-		if (mkdir(portal, 0660) != 0) {
c2d5d2
-			log_error("Could not make %s: %s\n", portal,
c2d5d2
-				  strerror(errno));
c2d5d2
-			rc = ISCSI_ERR_IDBM;
c2d5d2
-			goto free_portal;
c2d5d2
-		}
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
-	log_debug(5, "Looking for config file %s", portal);
c2d5d2
-
c2d5d2
-	rc = idbm_lock();
c2d5d2
-	if (rc)
c2d5d2
-		goto free_portal;
c2d5d2
-
c2d5d2
-	if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
c2d5d2
-		/* drop down to old style portal as config */
c2d5d2
-		goto open_conf;
c2d5d2
 
c2d5d2
 	rc = stat(portal, &statb);
c2d5d2
 	if (rc) {
c2d5d2
@@ -1872,11 +1842,11 @@ static int idbm_rec_write(node_rec_t *rec)
c2d5d2
 			log_error("Could not convert %s: %s\n", portal,
c2d5d2
 				  strerror(errno));
c2d5d2
 			rc = ISCSI_ERR_IDBM;
c2d5d2
-			goto unlock;
c2d5d2
+			goto free_portal;
c2d5d2
 		}
c2d5d2
 	} else {
c2d5d2
 		rc = ISCSI_ERR_INVAL;
c2d5d2
-		goto unlock;
c2d5d2
+		goto free_portal;
c2d5d2
 	}	
c2d5d2
 
c2d5d2
 mkdir_portal:
c2d5d2
@@ -1887,24 +1857,96 @@ mkdir_portal:
c2d5d2
 			log_error("Could not make dir %s: %s\n",
c2d5d2
 				  portal, strerror(errno));
c2d5d2
 			rc = ISCSI_ERR_IDBM;
c2d5d2
-			goto unlock;
c2d5d2
+			goto free_portal;
c2d5d2
 		}
c2d5d2
 	}
c2d5d2
 
c2d5d2
 	snprintf(portal, PATH_MAX, "%s/%s/%s,%d,%d/%s", NODE_CONFIG_DIR,
c2d5d2
 		 rec->name, rec->conn[0].address, rec->conn[0].port, rec->tpgt,
c2d5d2
 		 rec->iface.name);
c2d5d2
-open_conf:
c2d5d2
+/* open_conf: */
c2d5d2
 	f = fopen(portal, "w");
c2d5d2
 	if (!f) {
c2d5d2
 		log_error("Could not open %s: %sd\n", portal, strerror(errno));
c2d5d2
 		rc = ISCSI_ERR_IDBM;
c2d5d2
-		goto unlock;
c2d5d2
+		goto free_portal;
c2d5d2
 	}
c2d5d2
 
c2d5d2
 	idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
c2d5d2
 	fclose(f);
c2d5d2
-unlock:
c2d5d2
+free_portal:
c2d5d2
+	free(portal);
c2d5d2
+	return rc;
c2d5d2
+}
c2d5d2
+
c2d5d2
+static int idbm_rec_write_old(node_rec_t *rec)
c2d5d2
+{
c2d5d2
+	FILE *f;
c2d5d2
+	char *portal;
c2d5d2
+	int rc = 0;
c2d5d2
+
c2d5d2
+	portal = malloc(PATH_MAX);
c2d5d2
+	if (!portal) {
c2d5d2
+		log_error("Could not alloc portal\n");
c2d5d2
+		return ISCSI_ERR_NOMEM;
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
+	f = fopen(portal, "w");
c2d5d2
+	if (!f) {
c2d5d2
+		log_error("Could not open %s: %sd\n", portal, strerror(errno));
c2d5d2
+		rc = ISCSI_ERR_IDBM;
c2d5d2
+		goto free_portal;
c2d5d2
+	}
c2d5d2
+	idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
c2d5d2
+	fclose(f);
c2d5d2
+free_portal:
c2d5d2
+	free(portal);
c2d5d2
+	return rc;
c2d5d2
+}
c2d5d2
+
c2d5d2
+static int idbm_rec_write(node_rec_t *rec)
c2d5d2
+{
c2d5d2
+	char *portal;
c2d5d2
+	int rc = 0;
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
+	snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
c2d5d2
+	if (access(portal, F_OK) != 0) {
c2d5d2
+		if (mkdir(portal, 0660) != 0) {
c2d5d2
+			log_error("Could not make %s: %s\n", portal,
c2d5d2
+				  strerror(errno));
c2d5d2
+			rc = ISCSI_ERR_IDBM;
c2d5d2
+			goto free_portal;
c2d5d2
+		}
c2d5d2
+	}
c2d5d2
+
c2d5d2
+	snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
c2d5d2
+	if (access(portal, F_OK) != 0) {
c2d5d2
+		if (mkdir(portal, 0660) != 0) {
c2d5d2
+			log_error("Could not make %s: %s\n", portal,
c2d5d2
+				  strerror(errno));
c2d5d2
+			rc = ISCSI_ERR_IDBM;
c2d5d2
+			goto free_portal;
c2d5d2
+		}
c2d5d2
+	}
c2d5d2
+
c2d5d2
+	rc = idbm_lock();
c2d5d2
+	if (rc)
c2d5d2
+		goto free_portal;
c2d5d2
+
c2d5d2
+	if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
c2d5d2
+		/* old style portal as config */
c2d5d2
+		rc = idbm_rec_write_old(rec);
c2d5d2
+	else
c2d5d2
+		rc = idbm_rec_write_new(rec);
c2d5d2
+
c2d5d2
 	idbm_unlock();
c2d5d2
 free_portal:
c2d5d2
 	free(portal);
c2d5d2
-- 
c2d5d2
1.8.1.4
c2d5d2