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

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