Blame SOURCES/0003-idbm_rec_write-seperate-old-and-new-style-writes.patch

c37842
From 9823d8ead9e2d91765e4af39d906e25acd4e2a04 Mon Sep 17 00:00:00 2001
46c2f0
From: Chris Leech <cleech@redhat.com>
46c2f0
Date: Tue, 13 Aug 2013 11:34:31 -0700
c37842
Subject: [PATCH 3/3] idbm_rec_write, seperate old and new style writes
46c2f0
46c2f0
Duplicates a small bit of code, but easier to understand and extened.
46c2f0
---
46c2f0
 usr/idbm.c | 129 +++++++++++++++++++++++++++++++++++------------------
46c2f0
 1 file changed, 86 insertions(+), 43 deletions(-)
46c2f0
46c2f0
diff --git a/usr/idbm.c b/usr/idbm.c
c37842
index efdda75..307a01a 100644
46c2f0
--- a/usr/idbm.c
46c2f0
+++ b/usr/idbm.c
c37842
@@ -2152,12 +2152,7 @@ mkdir_portal:
46c2f0
 	return f;
46c2f0
 }
46c2f0
 
46c2f0
-/*
46c2f0
- * When the disable_lock param is true, the idbm_lock/idbm_unlock needs
46c2f0
- * to be holt by the caller, this will avoid overwriting each other in
46c2f0
- * case of updating(read-modify-write) the recs in parallel.
46c2f0
- */
46c2f0
-static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
46c2f0
+static int idbm_rec_write_new(node_rec_t *rec)
46c2f0
 {
46c2f0
 	struct stat statb;
46c2f0
 	FILE *f;
c37842
@@ -2170,39 +2165,8 @@ static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
46c2f0
 		return ISCSI_ERR_NOMEM;
46c2f0
 	}
46c2f0
 
46c2f0
-	snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
46c2f0
-	if (access(portal, F_OK) != 0) {
c37842
-		if (mkdir(portal, 0770) != 0) {
46c2f0
-			log_error("Could not make %s: %s", portal,
46c2f0
-				  strerror(errno));
46c2f0
-			rc = ISCSI_ERR_IDBM;
46c2f0
-			goto free_portal;
46c2f0
-		}
46c2f0
-	}
46c2f0
-
46c2f0
-	snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
46c2f0
-	if (access(portal, F_OK) != 0) {
c37842
-		if (mkdir(portal, 0770) != 0) {
46c2f0
-			log_error("Could not make %s: %s", portal,
46c2f0
-				  strerror(errno));
46c2f0
-			rc = ISCSI_ERR_IDBM;
46c2f0
-			goto free_portal;
46c2f0
-		}
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
-	log_debug(5, "Looking for config file %s", portal);
46c2f0
-
46c2f0
-	if (!disable_lock) {
46c2f0
-		rc = idbm_lock();
46c2f0
-		if (rc)
46c2f0
-			goto free_portal;
46c2f0
-	}
46c2f0
-
46c2f0
-	if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
46c2f0
-		/* drop down to old style portal as config */
46c2f0
-		goto open_conf;
46c2f0
 
46c2f0
 	rc = stat(portal, &statb);
46c2f0
 	if (rc) {
c37842
@@ -2223,11 +2187,11 @@ static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
46c2f0
 			log_error("Could not convert %s: %s", portal,
46c2f0
 				  strerror(errno));
46c2f0
 			rc = ISCSI_ERR_IDBM;
46c2f0
-			goto unlock;
46c2f0
+			goto free_portal;
46c2f0
 		}
46c2f0
 	} else {
46c2f0
 		rc = ISCSI_ERR_INVAL;
46c2f0
-		goto unlock;
46c2f0
+		goto free_portal;
46c2f0
 	}
46c2f0
 
46c2f0
 mkdir_portal:
c37842
@@ -2238,24 +2202,103 @@ mkdir_portal:
46c2f0
 			log_error("Could not make dir %s: %s",
46c2f0
 				  portal, strerror(errno));
46c2f0
 			rc = ISCSI_ERR_IDBM;
46c2f0
-			goto unlock;
46c2f0
+			goto free_portal;
46c2f0
 		}
46c2f0
 	}
46c2f0
 
46c2f0
 	snprintf(portal, PATH_MAX, "%s/%s/%s,%d,%d/%s", NODE_CONFIG_DIR,
46c2f0
 		 rec->name, rec->conn[0].address, rec->conn[0].port, rec->tpgt,
46c2f0
 		 rec->iface.name);
46c2f0
-open_conf:
46c2f0
+
46c2f0
 	f = fopen(portal, "w");
46c2f0
 	if (!f) {
46c2f0
 		log_error("Could not open %s: %s", portal, strerror(errno));
46c2f0
 		rc = ISCSI_ERR_IDBM;
46c2f0
-		goto unlock;
46c2f0
+		goto free_portal;
46c2f0
 	}
46c2f0
 
46c2f0
 	idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
46c2f0
 	fclose(f);
46c2f0
-unlock:
46c2f0
+free_portal:
46c2f0
+	free(portal);
46c2f0
+	return rc;
46c2f0
+}
46c2f0
+
46c2f0
+static int idbm_rec_write_old(node_rec_t *rec)
46c2f0
+{
46c2f0
+	FILE *f;
46c2f0
+	char *portal;
46c2f0
+	int rc = 0;
46c2f0
+
46c2f0
+	portal = malloc(PATH_MAX);
46c2f0
+	if (!portal) {
46c2f0
+		log_error("Could not alloc portal");
46c2f0
+		return ISCSI_ERR_NOMEM;
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
+	f = fopen(portal, "w");
46c2f0
+	if (!f) {
46c2f0
+		log_error("Could not open %s: %sd", portal, strerror(errno));
46c2f0
+		rc = ISCSI_ERR_IDBM;
46c2f0
+		goto free_portal;
46c2f0
+	}
46c2f0
+	idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
46c2f0
+	fclose(f);
46c2f0
+free_portal:
46c2f0
+	free(portal);
46c2f0
+	return rc;
46c2f0
+}
46c2f0
+
46c2f0
+/*
46c2f0
+ * When the disable_lock param is true, the idbm_lock/idbm_unlock needs
46c2f0
+ * to be holt by the caller, this will avoid overwriting each other in
46c2f0
+ * case of updating(read-modify-write) the recs in parallel.
46c2f0
+ */
46c2f0
+static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
46c2f0
+{
46c2f0
+	char *portal;
46c2f0
+	int rc = 0;
46c2f0
+
46c2f0
+	portal = malloc(PATH_MAX);
46c2f0
+	if (!portal) {
46c2f0
+		log_error("Could not alloc portal");
46c2f0
+		return ISCSI_ERR_NOMEM;
46c2f0
+	}
46c2f0
+
46c2f0
+	snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
46c2f0
+	if (access(portal, F_OK) != 0) {
46c2f0
+		if (mkdir(portal, 0660) != 0) {
46c2f0
+			log_error("Could not make %s: %s", portal,
46c2f0
+				  strerror(errno));
46c2f0
+			rc = ISCSI_ERR_IDBM;
46c2f0
+			goto free_portal;
46c2f0
+		}
46c2f0
+	}
46c2f0
+
46c2f0
+	snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
46c2f0
+	if (access(portal, F_OK) != 0) {
46c2f0
+		if (mkdir(portal, 0660) != 0) {
46c2f0
+			log_error("Could not make %s: %s", portal,
46c2f0
+				  strerror(errno));
46c2f0
+			rc = ISCSI_ERR_IDBM;
46c2f0
+			goto free_portal;
46c2f0
+		}
46c2f0
+	}
46c2f0
+
46c2f0
+	if (!disable_lock) {
46c2f0
+		rc = idbm_lock();
46c2f0
+		if (rc)
46c2f0
+			goto free_portal;
46c2f0
+	}
46c2f0
+
46c2f0
+	if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
46c2f0
+		/* old style portal as config */
46c2f0
+		rc = idbm_rec_write_old(rec);
46c2f0
+	else
46c2f0
+		rc = idbm_rec_write_new(rec);
46c2f0
+
46c2f0
 	if (!disable_lock)
46c2f0
 		idbm_unlock();
46c2f0
 free_portal:
46c2f0
-- 
c37842
2.31.1
46c2f0