| 1 |
Description: Add a reload target to istgt |
| 2 |
Currently istgt does not support on-the-fly reloading of its configurations, |
| 3 |
which means the administrator has to restart the daemon to make new settings in |
| 4 |
effect. |
| 5 |
. |
| 6 |
This is quite inconvenient when istgt is used in conjection with some "sensitive" |
| 7 |
initiators, for instance, Xen. |
| 8 |
. |
| 9 |
This patch which implements the underlying functionality was written by Kun Huang, |
| 10 |
sponsored by Applied Operations, LLC. |
| 11 |
. |
| 12 |
It was taken from the FreeBSD patch tracking sustem. |
| 13 |
Author: Andrew Ruthven <[email protected]> |
| 14 |
Origin: other, http://www.freebsd.org/cgi/query-pr.cgi?pr=165844 |
| 15 |
Forwarded: no |
| 16 |
Last-Update: 2012-06-26 |
| 17 |
|
| 18 |
--- a/src/istgt_lu.h |
| 19 |
+++ b/src/istgt_lu.h |
| 20 |
@@ -236,6 +236,9 @@ |
| 21 |
ISTGT_LU_TSIH tsih[MAX_LU_TSIH]; |
| 22 |
int maxmap; |
| 23 |
ISTGT_LU_MAP map[MAX_LU_MAP]; |
| 24 |
+ |
| 25 |
+ int to_add; |
| 26 |
+ int to_remove; |
| 27 |
} ISTGT_LU; |
| 28 |
typedef ISTGT_LU *ISTGT_LU_Ptr; |
| 29 |
|
| 30 |
--- a/src/istgt_proto.h |
| 31 |
+++ b/src/istgt_proto.h |
| 32 |
@@ -85,10 +85,10 @@ |
| 33 |
uint64_t istgt_lu_parse_media_size(const char *file, const char *size, int *flags); |
| 34 |
PORTAL *istgt_lu_find_portalgroup(ISTGT_Ptr istgt, int tag); |
| 35 |
INITIATOR_GROUP *istgt_lu_find_initiatorgroup(ISTGT_Ptr istgt, int tag); |
| 36 |
-int istgt_lu_init(ISTGT_Ptr istgt); |
| 37 |
+int istgt_lu_init(ISTGT_Ptr istgt, int ignore_dup); |
| 38 |
int istgt_lu_set_all_state(ISTGT_Ptr istgt, ISTGT_STATE state); |
| 39 |
-int istgt_lu_create_threads(ISTGT_Ptr istgt); |
| 40 |
-int istgt_lu_shutdown(ISTGT_Ptr istgt); |
| 41 |
+int istgt_lu_create_threads(ISTGT_Ptr istgt, int new_only); |
| 42 |
+int istgt_lu_shutdown(ISTGT_Ptr istgt, int removed_only); |
| 43 |
int istgt_lu_islun2lun(uint64_t islun); |
| 44 |
uint64_t istgt_lu_lun2islun(int lun, int maxlun); |
| 45 |
int istgt_lu_reset(ISTGT_LU_Ptr lu, uint64_t lun); |
| 46 |
--- a/src/istgt_lu_disk.c |
| 47 |
+++ b/src/istgt_lu_disk.c |
| 48 |
@@ -794,6 +794,7 @@ |
| 49 |
xfree(spec); |
| 50 |
lu->lun[i].spec = NULL; |
| 51 |
} |
| 52 |
+ lu->maxlun = 0; |
| 53 |
|
| 54 |
return 0; |
| 55 |
} |
| 56 |
--- a/src/istgt_lu_ctl.c |
| 57 |
+++ b/src/istgt_lu_ctl.c |
| 58 |
@@ -965,6 +965,113 @@ |
| 59 |
return UCTL_CMD_OK; |
| 60 |
} |
| 61 |
|
| 62 |
+extern const char *g_config_file; |
| 63 |
+extern ISTGT g_istgt; |
| 64 |
+ |
| 65 |
+static int |
| 66 |
+istgt_uctl_cmd_refresh(UCTL_Ptr uctl) |
| 67 |
+{ |
| 68 |
+ char *arg; |
| 69 |
+ int rc, i, j; |
| 70 |
+ CONFIG *config; |
| 71 |
+ ISTGT_Ptr istgt; |
| 72 |
+ |
| 73 |
+ arg = uctl->arg; |
| 74 |
+ istgt = &g_istgt; |
| 75 |
+ |
| 76 |
+ /* reload config, might add/remove LUNs */ |
| 77 |
+ /* step 1: re-read config files */ |
| 78 |
+ config = istgt_allocate_config(); |
| 79 |
+ rc = istgt_read_config(config, g_config_file); |
| 80 |
+ if (rc < 0) { |
| 81 |
+ fprintf(stderr, "refresh config error\n"); |
| 82 |
+ return UCTL_CMD_ERR; |
| 83 |
+ } |
| 84 |
+ if (config->section == NULL) { |
| 85 |
+ fprintf(stderr, "empty config\n"); |
| 86 |
+ istgt_free_config(config); |
| 87 |
+ return UCTL_CMD_ERR; |
| 88 |
+ } |
| 89 |
+ istgt_free_config(istgt->config); |
| 90 |
+ istgt->config = config; |
| 91 |
+ |
| 92 |
+ /* step 2: add new units, init new LUNs, tag to-add/remove LUNs */ |
| 93 |
+ rc = istgt_lu_init(istgt, 1); |
| 94 |
+ if (rc < 0) { |
| 95 |
+ ISTGT_ERRLOG("istgt_lu_init() failed\n"); |
| 96 |
+ istgt_free_config(config); |
| 97 |
+ return UCTL_CMD_ERR; |
| 98 |
+ } |
| 99 |
+ |
| 100 |
+ for (i = 0; i < MAX_LOGICAL_UNIT; i++) { |
| 101 |
+ ISTGT_LU_Ptr lu = istgt->logical_unit[i]; |
| 102 |
+ if (lu == NULL) |
| 103 |
+ continue; |
| 104 |
+ if (lu->to_add) |
| 105 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "newly added LUN %s\n", lu->name); |
| 106 |
+ if (lu->to_remove) |
| 107 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "newly removed LUN %s\n", lu->name); |
| 108 |
+ } |
| 109 |
+ |
| 110 |
+ /* creat new threads for new LUNs, do work in luworker() */ |
| 111 |
+ /* create LUN threads for command queuing */ |
| 112 |
+ rc = istgt_lu_create_threads(istgt, 1); |
| 113 |
+ if (rc < 0) { |
| 114 |
+ ISTGT_ERRLOG("lu_create_threads() failed\n"); |
| 115 |
+ istgt_free_config(config); |
| 116 |
+ return UCTL_CMD_ERR; |
| 117 |
+ } |
| 118 |
+ /* start taking IOs */ |
| 119 |
+ for (i = 0; i < MAX_LOGICAL_UNIT; i++) { |
| 120 |
+ ISTGT_LU_Ptr lu = istgt->logical_unit[i]; |
| 121 |
+ if (lu == NULL) |
| 122 |
+ continue; |
| 123 |
+ if (lu->to_add) { |
| 124 |
+ istgt_lu_set_state(lu, ISTGT_STATE_RUNNING); |
| 125 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "set newly added LUN as running %s\n", lu->name); |
| 126 |
+ } |
| 127 |
+ } |
| 128 |
+ |
| 129 |
+ /* step 3: shutdown removed LUNs */ |
| 130 |
+ /* clear outstanding IOs */ |
| 131 |
+ for (i = 0; i < MAX_LOGICAL_UNIT; i++) { |
| 132 |
+ ISTGT_LU_Ptr lu = istgt->logical_unit[i]; |
| 133 |
+ if (lu == NULL) |
| 134 |
+ continue; |
| 135 |
+ if (lu->to_remove) { |
| 136 |
+ for (j = 0; j<lu->maxlun; j++) |
| 137 |
+ istgt_lu_clear_all_task(lu, j); |
| 138 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "clear newly removed LUN queue %s\n", lu |
| 139 |
+->name); |
| 140 |
+ } |
| 141 |
+ } |
| 142 |
+ |
| 143 |
+ /* sync and close LUNs */ |
| 144 |
+ istgt_lu_shutdown(istgt, 1); |
| 145 |
+ |
| 146 |
+ /* clear tag of to_add/to_remove, get ready for next refresh */ |
| 147 |
+ for (i = 0; i < MAX_LOGICAL_UNIT; i++) { |
| 148 |
+ ISTGT_LU_Ptr lu = istgt->logical_unit[i]; |
| 149 |
+ if (lu == NULL) |
| 150 |
+ continue; |
| 151 |
+ if (lu->to_add) |
| 152 |
+ lu->to_add = 0; |
| 153 |
+ if (lu->to_remove) |
| 154 |
+ lu->to_remove = 0; |
| 155 |
+ } |
| 156 |
+ |
| 157 |
+ /* logging event */ |
| 158 |
+ ISTGT_NOTICELOG("Configuration refresh requested from %s\n", |
| 159 |
+ uctl->caddr); |
| 160 |
+ |
| 161 |
+ /* refresh succeeded */ |
| 162 |
+ istgt_uctl_snprintf(uctl, "OK %s\n", uctl->cmd); |
| 163 |
+ rc = istgt_uctl_writeline(uctl); |
| 164 |
+ if (rc != UCTL_CMD_OK) |
| 165 |
+ return rc; |
| 166 |
+ return UCTL_CMD_OK; |
| 167 |
+} |
| 168 |
+ |
| 169 |
static int |
| 170 |
istgt_uctl_cmd_reset(UCTL_Ptr uctl) |
| 171 |
{ |
| 172 |
@@ -1183,6 +1290,7 @@ |
| 173 |
{ "UNLOAD", istgt_uctl_cmd_unload }, |
| 174 |
{ "LOAD", istgt_uctl_cmd_load }, |
| 175 |
{ "CHANGE", istgt_uctl_cmd_change }, |
| 176 |
+ { "REFRESH", istgt_uctl_cmd_refresh }, |
| 177 |
{ "RESET", istgt_uctl_cmd_reset }, |
| 178 |
{ "INFO", istgt_uctl_cmd_info }, |
| 179 |
{ NULL, NULL }, |
| 180 |
--- a/src/istgtcontrol.c |
| 181 |
+++ b/src/istgtcontrol.c |
| 182 |
@@ -494,6 +494,36 @@ |
| 183 |
} |
| 184 |
|
| 185 |
static int |
| 186 |
+exec_refresh(UCTL_Ptr uctl) |
| 187 |
+{ |
| 188 |
+ const char *delim = ARGS_DELIM; |
| 189 |
+ char *arg; |
| 190 |
+ char *result; |
| 191 |
+ int rc; |
| 192 |
+ |
| 193 |
+ /* send command */ |
| 194 |
+ uctl_snprintf(uctl, "REFRESH \n"); |
| 195 |
+ rc = uctl_writeline(uctl); |
| 196 |
+ if (rc != UCTL_CMD_OK) |
| 197 |
+ return rc; |
| 198 |
+ |
| 199 |
+ /* receive result */ |
| 200 |
+ rc = uctl_readline(uctl); |
| 201 |
+ if (rc != UCTL_CMD_OK) |
| 202 |
+ return rc; |
| 203 |
+ arg = trim_string(uctl->recvbuf); |
| 204 |
+ result = strsepq(&arg, delim); |
| 205 |
+ strupr(result); |
| 206 |
+ if (strcmp(result, "OK") != 0) { |
| 207 |
+ if (is_err_req_auth(uctl, arg)) |
| 208 |
+ return UCTL_CMD_REQAUTH; |
| 209 |
+ fprintf(stderr, "ERROR %s\n", arg); |
| 210 |
+ return UCTL_CMD_ERR; |
| 211 |
+ } |
| 212 |
+ return UCTL_CMD_OK; |
| 213 |
+} |
| 214 |
+ |
| 215 |
+static int |
| 216 |
exec_reset(UCTL_Ptr uctl) |
| 217 |
{ |
| 218 |
const char *delim = ARGS_DELIM; |
| 219 |
@@ -591,6 +621,7 @@ |
| 220 |
{ "UNLOAD", exec_unload, 0, 1 }, |
| 221 |
{ "LOAD", exec_load, 0, 1 }, |
| 222 |
{ "CHANGE", exec_change, 1, 1 }, |
| 223 |
+ { "REFRESH", exec_refresh, 0, 0 }, |
| 224 |
{ "RESET", exec_reset, 0, 1 }, |
| 225 |
{ "INFO", exec_info, 0, 0 }, |
| 226 |
{ NULL, NULL, 0, 0 }, |
| 227 |
@@ -1054,6 +1085,7 @@ |
| 228 |
printf(" load load media to specified unit\n"); |
| 229 |
printf(" unload unload media from specified unit\n"); |
| 230 |
printf(" change change media with <file> at specified unit\n"); |
| 231 |
+ printf(" refresh refresh to reload target and lun configuration\n"); |
| 232 |
printf(" reset reset specified lun of target\n"); |
| 233 |
printf(" info show connections of target\n"); |
| 234 |
} |
| 235 |
--- a/src/istgt_lu.c |
| 236 |
+++ b/src/istgt_lu.c |
| 237 |
@@ -1200,7 +1200,7 @@ |
| 238 |
} |
| 239 |
|
| 240 |
static int |
| 241 |
-istgt_lu_add_unit(ISTGT_Ptr istgt, CF_SECTION *sp) |
| 242 |
+istgt_lu_add_unit(ISTGT_Ptr istgt, CF_SECTION *sp, int ignore_dup) |
| 243 |
{ |
| 244 |
char buf[MAX_TMPBUF], buf2[MAX_TMPBUF]; |
| 245 |
ISTGT_LU_Ptr lu; |
| 246 |
@@ -1220,21 +1220,40 @@ |
| 247 |
int i, j, k; |
| 248 |
int rc; |
| 249 |
|
| 250 |
- ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "add unit %d\n", sp->num); |
| 251 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "add unit %d (ignore dup = %d)\n", sp->num, ignore_dup); |
| 252 |
|
| 253 |
if (sp->num >= MAX_LOGICAL_UNIT) { |
| 254 |
ISTGT_ERRLOG("LU%d: over maximum unit number\n", sp->num); |
| 255 |
return -1; |
| 256 |
} |
| 257 |
- if (istgt->logical_unit[sp->num] != NULL) { |
| 258 |
+ if (istgt->logical_unit[sp->num] != NULL && !ignore_dup) { |
| 259 |
ISTGT_ERRLOG("LU%d: duplicate unit\n", sp->num); |
| 260 |
return -1; |
| 261 |
} |
| 262 |
+ /* existing LUNs */ |
| 263 |
+ if (istgt->logical_unit[sp->num] != NULL && ignore_dup) { |
| 264 |
+ istgt->logical_unit[sp->num]->to_add = 0; |
| 265 |
+ istgt->logical_unit[sp->num]->to_remove = 0; |
| 266 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "skip existing unit %d\n", sp->num); |
| 267 |
+ return 0; |
| 268 |
+ } |
| 269 |
+ |
| 270 |
+ /* new LUNs */ |
| 271 |
+ if (istgt->logical_unit[sp->num] == NULL && ignore_dup) { |
| 272 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "add NEW unit %d\n", sp->num); |
| 273 |
+ } |
| 274 |
|
| 275 |
lu = xmalloc(sizeof *lu); |
| 276 |
memset(lu, 0, sizeof *lu); |
| 277 |
lu->num = sp->num; |
| 278 |
lu->istgt = istgt; |
| 279 |
+ |
| 280 |
+ /* tag new LUNs */ |
| 281 |
+ if (ignore_dup) { |
| 282 |
+ lu->to_add = 1; |
| 283 |
+ lu->to_remove = 0; |
| 284 |
+ } |
| 285 |
+ |
| 286 |
istgt_lu_set_state(lu, ISTGT_STATE_INVALID); |
| 287 |
nbs64 = istgt_lu_get_nbserial(istgt->nodebase); |
| 288 |
#if 0 |
| 289 |
@@ -1942,12 +1961,15 @@ |
| 290 |
static void *luworker(void *arg); |
| 291 |
|
| 292 |
int |
| 293 |
-istgt_lu_init(ISTGT_Ptr istgt) |
| 294 |
+istgt_lu_init(ISTGT_Ptr istgt, int ignore_dup) |
| 295 |
{ |
| 296 |
ISTGT_LU_Ptr lu; |
| 297 |
CF_SECTION *sp; |
| 298 |
int rc; |
| 299 |
int i; |
| 300 |
+ int lun_map[MAX_LOGICAL_UNIT]; |
| 301 |
+ |
| 302 |
+ memset(lun_map, 0, sizeof(int) * MAX_LOGICAL_UNIT); |
| 303 |
|
| 304 |
ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_init\n"); |
| 305 |
sp = istgt_find_cf_section(istgt->config, "Global"); |
| 306 |
@@ -1967,11 +1989,12 @@ |
| 307 |
ISTGT_ERRLOG("tag %d is invalid\n", sp->num); |
| 308 |
return -1; |
| 309 |
} |
| 310 |
- rc = istgt_lu_add_unit(istgt, sp); |
| 311 |
+ rc = istgt_lu_add_unit(istgt, sp, ignore_dup); |
| 312 |
if (rc < 0) { |
| 313 |
ISTGT_ERRLOG("lu_add_unit() failed\n"); |
| 314 |
return -1; |
| 315 |
} |
| 316 |
+ lun_map[sp->num] = 1; /* in new LUN list */ |
| 317 |
} |
| 318 |
sp = sp->next; |
| 319 |
} |
| 320 |
@@ -1980,6 +2003,22 @@ |
| 321 |
lu = istgt->logical_unit[i]; |
| 322 |
if (lu == NULL) |
| 323 |
continue; |
| 324 |
+ /* tag to be removed LUNs */ |
| 325 |
+ if (lun_map[i] == 0) { |
| 326 |
+ istgt->logical_unit[i]->to_add = 0; |
| 327 |
+ istgt->logical_unit[i]->to_remove = 1; |
| 328 |
+ continue; |
| 329 |
+ } |
| 330 |
+ /* only process newly added LUNs here beyond in loop */ |
| 331 |
+ if (ignore_dup && |
| 332 |
+ (istgt->logical_unit[i]->to_add == 1 && |
| 333 |
+ istgt->logical_unit[i]->to_remove == 0)) { |
| 334 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "adding unit %d\n", i); |
| 335 |
+ } else if (ignore_dup && |
| 336 |
+ !(istgt->logical_unit[i]->to_add == 1 && |
| 337 |
+ istgt->logical_unit[i]->to_remove == 0)) { |
| 338 |
+ continue; |
| 339 |
+ } |
| 340 |
|
| 341 |
rc = pthread_mutex_init(&lu->mutex, NULL); |
| 342 |
if (rc != 0) { |
| 343 |
@@ -2067,7 +2106,7 @@ |
| 344 |
} |
| 345 |
|
| 346 |
int |
| 347 |
-istgt_lu_create_threads(ISTGT_Ptr istgt) |
| 348 |
+istgt_lu_create_threads(ISTGT_Ptr istgt, int new_only) |
| 349 |
{ |
| 350 |
#ifdef HAVE_PTHREAD_SET_NAME_NP |
| 351 |
char buf[MAX_TMPBUF]; |
| 352 |
@@ -2082,6 +2121,10 @@ |
| 353 |
lu = istgt->logical_unit[i]; |
| 354 |
if (lu == NULL) |
| 355 |
continue; |
| 356 |
+ if (new_only && !lu->to_add) { |
| 357 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "skip existing LUN %d\n", i); |
| 358 |
+ continue; |
| 359 |
+ } |
| 360 |
|
| 361 |
if (lu->queue_depth != 0) { |
| 362 |
/* create LU thread */ |
| 363 |
@@ -2112,18 +2155,22 @@ |
| 364 |
} |
| 365 |
|
| 366 |
int |
| 367 |
-istgt_lu_shutdown(ISTGT_Ptr istgt) |
| 368 |
+istgt_lu_shutdown(ISTGT_Ptr istgt, int removed_only) |
| 369 |
{ |
| 370 |
ISTGT_LU_Ptr lu; |
| 371 |
int rc; |
| 372 |
int i; |
| 373 |
|
| 374 |
- ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_shutdown\n"); |
| 375 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "istgt_lu_shutdown (removed_only = %d\n", removed_only); |
| 376 |
|
| 377 |
for (i = 0; i < MAX_LOGICAL_UNIT; i++) { |
| 378 |
lu = istgt->logical_unit[i]; |
| 379 |
if (lu == NULL) |
| 380 |
continue; |
| 381 |
+ if (removed_only && !lu->to_remove) { |
| 382 |
+ ISTGT_TRACELOG(ISTGT_TRACE_DEBUG, "skip existing LUNs %d\n", i); |
| 383 |
+ continue; |
| 384 |
+ } |
| 385 |
istgt_lu_set_state(lu, ISTGT_STATE_SHUTDOWN); |
| 386 |
|
| 387 |
switch (lu->type) { |
| 388 |
--- a/src/istgt.c |
| 389 |
+++ b/src/istgt.c |
| 390 |
@@ -71,6 +71,7 @@ |
| 391 |
#define PORTNUMLEN 32 |
| 392 |
|
| 393 |
ISTGT g_istgt; |
| 394 |
+const char *g_config_file; |
| 395 |
|
| 396 |
#if 0 |
| 397 |
void |
| 398 |
@@ -1598,6 +1599,7 @@ |
| 399 |
|
| 400 |
/* read config files */ |
| 401 |
config = istgt_allocate_config(); |
| 402 |
+ g_config_file = config_file; |
| 403 |
rc = istgt_read_config(config, config_file); |
| 404 |
if (rc < 0) { |
| 405 |
fprintf(stderr, "config error\n"); |
| 406 |
@@ -1662,7 +1664,7 @@ |
| 407 |
istgt_free_config(config); |
| 408 |
exit(EXIT_FAILURE); |
| 409 |
} |
| 410 |
- rc = istgt_lu_init(istgt); |
| 411 |
+ rc = istgt_lu_init(istgt, 0); |
| 412 |
if (rc < 0) { |
| 413 |
ISTGT_ERRLOG("istgt_lu_init() failed\n"); |
| 414 |
goto initialize_error; |
| 415 |
@@ -1772,7 +1774,7 @@ |
| 416 |
#endif |
| 417 |
|
| 418 |
/* create LUN threads for command queuing */ |
| 419 |
- rc = istgt_lu_create_threads(istgt); |
| 420 |
+ rc = istgt_lu_create_threads(istgt, 0); |
| 421 |
if (rc < 0) { |
| 422 |
ISTGT_ERRLOG("lu_create_threads() failed\n"); |
| 423 |
goto initialize_error; |
| 424 |
@@ -1809,7 +1811,7 @@ |
| 425 |
istgt_close_portal(istgt); |
| 426 |
istgt_close_uctl_portal(istgt); |
| 427 |
istgt_iscsi_shutdown(istgt); |
| 428 |
- istgt_lu_shutdown(istgt); |
| 429 |
+ istgt_lu_shutdown(istgt, 0); |
| 430 |
istgt_destory_initiator_group_array(istgt); |
| 431 |
istgt_destroy_portal_array(istgt); |
| 432 |
istgt_destroy_uctl_portal(istgt); |
| 433 |
@@ -1837,7 +1839,7 @@ |
| 434 |
istgt_close_portal(istgt); |
| 435 |
istgt_close_uctl_portal(istgt); |
| 436 |
istgt_iscsi_shutdown(istgt); |
| 437 |
- istgt_lu_shutdown(istgt); |
| 438 |
+ istgt_lu_shutdown(istgt, 0); |
| 439 |
istgt_destory_initiator_group_array(istgt); |
| 440 |
istgt_destroy_portal_array(istgt); |
| 441 |
istgt_destroy_uctl_portal(istgt); |