aboutsummaryrefslogtreecommitdiff
path: root/net/open-iscsi/patches/0003-idbm_rec_write-seperate-old-and-new-style-writes.patch
blob: 7a540c1486fa166f32ce7c80e198b82dfa85bf21 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
From 0bb22b50dbaa7ac44e8eb244a73a66efbd98632c Mon Sep 17 00:00:00 2001
From: Chris Leech <cleech@redhat.com>
Date: Tue, 13 Aug 2013 11:34:31 -0700
Subject: [PATCH 1/1] idbm_rec_write, seperate old and new style writes

Duplicates a small bit of code, but easier to understand and extened.
---
 usr/idbm.c | 129 +++++++++++++++++++++++++++++++++++------------------
 1 file changed, 86 insertions(+), 43 deletions(-)

--- a/usr/idbm.c
+++ b/usr/idbm.c
@@ -2152,12 +2152,7 @@ mkdir_portal:
 	return f;
 }
 
-/*
- * When the disable_lock param is true, the idbm_lock/idbm_unlock needs
- * to be holt by the caller, this will avoid overwriting each other in
- * case of updating(read-modify-write) the recs in parallel.
- */
-static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
+static int idbm_rec_write_new(node_rec_t *rec)
 {
 	struct stat statb;
 	FILE *f;
@@ -2170,39 +2165,8 @@ static int idbm_rec_write(node_rec_t *re
 		return ISCSI_ERR_NOMEM;
 	}
 
-	snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
-	if (access(portal, F_OK) != 0) {
-		if (mkdir(portal, 0770) != 0) {
-			log_error("Could not make %s: %s", portal,
-				  strerror(errno));
-			rc = ISCSI_ERR_IDBM;
-			goto free_portal;
-		}
-	}
-
-	snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
-	if (access(portal, F_OK) != 0) {
-		if (mkdir(portal, 0770) != 0) {
-			log_error("Could not make %s: %s", portal,
-				  strerror(errno));
-			rc = ISCSI_ERR_IDBM;
-			goto free_portal;
-		}
-	}
-
 	snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR,
 		 rec->name, rec->conn[0].address, rec->conn[0].port);
-	log_debug(5, "Looking for config file %s", portal);
-
-	if (!disable_lock) {
-		rc = idbm_lock();
-		if (rc)
-			goto free_portal;
-	}
-
-	if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
-		/* drop down to old style portal as config */
-		goto open_conf;
 
 	rc = stat(portal, &statb);
 	if (rc) {
@@ -2223,11 +2187,11 @@ static int idbm_rec_write(node_rec_t *re
 			log_error("Could not convert %s: %s", portal,
 				  strerror(errno));
 			rc = ISCSI_ERR_IDBM;
-			goto unlock;
+			goto free_portal;
 		}
 	} else {
 		rc = ISCSI_ERR_INVAL;
-		goto unlock;
+		goto free_portal;
 	}
 
 mkdir_portal:
@@ -2238,24 +2202,103 @@ mkdir_portal:
 			log_error("Could not make dir %s: %s",
 				  portal, strerror(errno));
 			rc = ISCSI_ERR_IDBM;
-			goto unlock;
+			goto free_portal;
 		}
 	}
 
 	snprintf(portal, PATH_MAX, "%s/%s/%s,%d,%d/%s", NODE_CONFIG_DIR,
 		 rec->name, rec->conn[0].address, rec->conn[0].port, rec->tpgt,
 		 rec->iface.name);
-open_conf:
+
 	f = fopen(portal, "w");
 	if (!f) {
 		log_error("Could not open %s: %s", portal, strerror(errno));
 		rc = ISCSI_ERR_IDBM;
-		goto unlock;
+		goto free_portal;
+	}
+
+	idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
+	fclose(f);
+free_portal:
+	free(portal);
+	return rc;
+}
+
+static int idbm_rec_write_old(node_rec_t *rec)
+{
+	FILE *f;
+	char *portal;
+	int rc = 0;
+
+	portal = malloc(PATH_MAX);
+	if (!portal) {
+		log_error("Could not alloc portal");
+		return ISCSI_ERR_NOMEM;
 	}
+	snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR,
+		 rec->name, rec->conn[0].address, rec->conn[0].port);
 
+	f = fopen(portal, "w");
+	if (!f) {
+		log_error("Could not open %s: %sd", portal, strerror(errno));
+		rc = ISCSI_ERR_IDBM;
+		goto free_portal;
+	}
 	idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f);
 	fclose(f);
-unlock:
+free_portal:
+	free(portal);
+	return rc;
+}
+
+/*
+ * When the disable_lock param is true, the idbm_lock/idbm_unlock needs
+ * to be holt by the caller, this will avoid overwriting each other in
+ * case of updating(read-modify-write) the recs in parallel.
+ */
+static int idbm_rec_write(node_rec_t *rec, bool disable_lock)
+{
+	char *portal;
+	int rc = 0;
+
+	portal = malloc(PATH_MAX);
+	if (!portal) {
+		log_error("Could not alloc portal");
+		return ISCSI_ERR_NOMEM;
+	}
+
+	snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR);
+	if (access(portal, F_OK) != 0) {
+		if (mkdir(portal, 0660) != 0) {
+			log_error("Could not make %s: %s", portal,
+				  strerror(errno));
+			rc = ISCSI_ERR_IDBM;
+			goto free_portal;
+		}
+	}
+
+	snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name);
+	if (access(portal, F_OK) != 0) {
+		if (mkdir(portal, 0660) != 0) {
+			log_error("Could not make %s: %s", portal,
+				  strerror(errno));
+			rc = ISCSI_ERR_IDBM;
+			goto free_portal;
+		}
+	}
+
+	if (!disable_lock) {
+		rc = idbm_lock();
+		if (rc)
+			goto free_portal;
+	}
+
+	if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN)
+		/* old style portal as config */
+		rc = idbm_rec_write_old(rec);
+	else
+		rc = idbm_rec_write_new(rec);
+
 	if (!disable_lock)
 		idbm_unlock();
 free_portal: