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

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