| b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame] | 1 | From 4c6e7c0fcc6da66cf81c0714bf907762194eedf2 Mon Sep 17 00:00:00 2001 |
| 2 | From: Chris Leech <cleech@redhat.com> |
| 3 | Date: Tue, 13 Aug 2013 11:34:31 -0700 |
| 4 | Subject: [PATCH] idbm_rec_write, seperate old and new style writes |
| 5 | |
| 6 | Duplicates a small bit of code, but easier to understand and extened. |
| 7 | --- |
| 8 | usr/idbm.c | 129 +++++++++++++++++++++++++++++++++++------------------ |
| 9 | 1 file changed, 86 insertions(+), 43 deletions(-) |
| 10 | |
| 11 | --- a/usr/idbm.c |
| 12 | +++ b/usr/idbm.c |
| 13 | @@ -2130,12 +2130,7 @@ mkdir_portal: |
| 14 | return f; |
| 15 | } |
| 16 | |
| 17 | -/* |
| 18 | - * When the disable_lock param is true, the idbm_lock/idbm_unlock needs |
| 19 | - * to be holt by the caller, this will avoid overwriting each other in |
| 20 | - * case of updating(read-modify-write) the recs in parallel. |
| 21 | - */ |
| 22 | -static int idbm_rec_write(node_rec_t *rec, bool disable_lock) |
| 23 | +static int idbm_rec_write_new(node_rec_t *rec) |
| 24 | { |
| 25 | struct stat statb; |
| 26 | FILE *f; |
| 27 | @@ -2148,39 +2143,8 @@ static int idbm_rec_write(node_rec_t *re |
| 28 | return ISCSI_ERR_NOMEM; |
| 29 | } |
| 30 | |
| 31 | - snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR); |
| 32 | - if (access(portal, F_OK) != 0) { |
| 33 | - if (mkdir(portal, 0660) != 0) { |
| 34 | - log_error("Could not make %s: %s", portal, |
| 35 | - strerror(errno)); |
| 36 | - rc = ISCSI_ERR_IDBM; |
| 37 | - goto free_portal; |
| 38 | - } |
| 39 | - } |
| 40 | - |
| 41 | - snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name); |
| 42 | - if (access(portal, F_OK) != 0) { |
| 43 | - if (mkdir(portal, 0660) != 0) { |
| 44 | - log_error("Could not make %s: %s", portal, |
| 45 | - strerror(errno)); |
| 46 | - rc = ISCSI_ERR_IDBM; |
| 47 | - goto free_portal; |
| 48 | - } |
| 49 | - } |
| 50 | - |
| 51 | snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR, |
| 52 | rec->name, rec->conn[0].address, rec->conn[0].port); |
| 53 | - log_debug(5, "Looking for config file %s", portal); |
| 54 | - |
| 55 | - if (!disable_lock) { |
| 56 | - rc = idbm_lock(); |
| 57 | - if (rc) |
| 58 | - goto free_portal; |
| 59 | - } |
| 60 | - |
| 61 | - if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN) |
| 62 | - /* drop down to old style portal as config */ |
| 63 | - goto open_conf; |
| 64 | |
| 65 | rc = stat(portal, &statb); |
| 66 | if (rc) { |
| 67 | @@ -2201,11 +2165,11 @@ static int idbm_rec_write(node_rec_t *re |
| 68 | log_error("Could not convert %s: %s", portal, |
| 69 | strerror(errno)); |
| 70 | rc = ISCSI_ERR_IDBM; |
| 71 | - goto unlock; |
| 72 | + goto free_portal; |
| 73 | } |
| 74 | } else { |
| 75 | rc = ISCSI_ERR_INVAL; |
| 76 | - goto unlock; |
| 77 | + goto free_portal; |
| 78 | } |
| 79 | |
| 80 | mkdir_portal: |
| 81 | @@ -2216,24 +2180,103 @@ mkdir_portal: |
| 82 | log_error("Could not make dir %s: %s", |
| 83 | portal, strerror(errno)); |
| 84 | rc = ISCSI_ERR_IDBM; |
| 85 | - goto unlock; |
| 86 | + goto free_portal; |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | snprintf(portal, PATH_MAX, "%s/%s/%s,%d,%d/%s", NODE_CONFIG_DIR, |
| 91 | rec->name, rec->conn[0].address, rec->conn[0].port, rec->tpgt, |
| 92 | rec->iface.name); |
| 93 | -open_conf: |
| 94 | + |
| 95 | f = fopen(portal, "w"); |
| 96 | if (!f) { |
| 97 | log_error("Could not open %s: %s", portal, strerror(errno)); |
| 98 | rc = ISCSI_ERR_IDBM; |
| 99 | - goto unlock; |
| 100 | + goto free_portal; |
| 101 | + } |
| 102 | + |
| 103 | + idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f); |
| 104 | + fclose(f); |
| 105 | +free_portal: |
| 106 | + free(portal); |
| 107 | + return rc; |
| 108 | +} |
| 109 | + |
| 110 | +static int idbm_rec_write_old(node_rec_t *rec) |
| 111 | +{ |
| 112 | + FILE *f; |
| 113 | + char *portal; |
| 114 | + int rc = 0; |
| 115 | + |
| 116 | + portal = malloc(PATH_MAX); |
| 117 | + if (!portal) { |
| 118 | + log_error("Could not alloc portal"); |
| 119 | + return ISCSI_ERR_NOMEM; |
| 120 | } |
| 121 | + snprintf(portal, PATH_MAX, "%s/%s/%s,%d", NODE_CONFIG_DIR, |
| 122 | + rec->name, rec->conn[0].address, rec->conn[0].port); |
| 123 | |
| 124 | + f = fopen(portal, "w"); |
| 125 | + if (!f) { |
| 126 | + log_error("Could not open %s: %sd", portal, strerror(errno)); |
| 127 | + rc = ISCSI_ERR_IDBM; |
| 128 | + goto free_portal; |
| 129 | + } |
| 130 | idbm_print(IDBM_PRINT_TYPE_NODE, rec, 1, f); |
| 131 | fclose(f); |
| 132 | -unlock: |
| 133 | +free_portal: |
| 134 | + free(portal); |
| 135 | + return rc; |
| 136 | +} |
| 137 | + |
| 138 | +/* |
| 139 | + * When the disable_lock param is true, the idbm_lock/idbm_unlock needs |
| 140 | + * to be holt by the caller, this will avoid overwriting each other in |
| 141 | + * case of updating(read-modify-write) the recs in parallel. |
| 142 | + */ |
| 143 | +static int idbm_rec_write(node_rec_t *rec, bool disable_lock) |
| 144 | +{ |
| 145 | + char *portal; |
| 146 | + int rc = 0; |
| 147 | + |
| 148 | + portal = malloc(PATH_MAX); |
| 149 | + if (!portal) { |
| 150 | + log_error("Could not alloc portal"); |
| 151 | + return ISCSI_ERR_NOMEM; |
| 152 | + } |
| 153 | + |
| 154 | + snprintf(portal, PATH_MAX, "%s", NODE_CONFIG_DIR); |
| 155 | + if (access(portal, F_OK) != 0) { |
| 156 | + if (mkdir(portal, 0660) != 0) { |
| 157 | + log_error("Could not make %s: %s", portal, |
| 158 | + strerror(errno)); |
| 159 | + rc = ISCSI_ERR_IDBM; |
| 160 | + goto free_portal; |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + snprintf(portal, PATH_MAX, "%s/%s", NODE_CONFIG_DIR, rec->name); |
| 165 | + if (access(portal, F_OK) != 0) { |
| 166 | + if (mkdir(portal, 0660) != 0) { |
| 167 | + log_error("Could not make %s: %s", portal, |
| 168 | + strerror(errno)); |
| 169 | + rc = ISCSI_ERR_IDBM; |
| 170 | + goto free_portal; |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + if (!disable_lock) { |
| 175 | + rc = idbm_lock(); |
| 176 | + if (rc) |
| 177 | + goto free_portal; |
| 178 | + } |
| 179 | + |
| 180 | + if (rec->tpgt == PORTAL_GROUP_TAG_UNKNOWN) |
| 181 | + /* old style portal as config */ |
| 182 | + rc = idbm_rec_write_old(rec); |
| 183 | + else |
| 184 | + rc = idbm_rec_write_new(rec); |
| 185 | + |
| 186 | if (!disable_lock) |
| 187 | idbm_unlock(); |
| 188 | free_portal: |