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

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