1 /*- 2 * BSD LICENSE 3 * 4 * Copyright (c) Intel Corporation. All rights reserved. 5 * Copyright (c) 2019 Mellanox Technologies LTD. All rights reserved. 6 * 7 * Redistribution and use in source and binary forms, with or without 8 * modification, are permitted provided that the following conditions 9 * are met: 10 * 11 * * Redistributions of source code must retain the above copyright 12 * notice, this list of conditions and the following disclaimer. 13 * * Redistributions in binary form must reproduce the above copyright 14 * notice, this list of conditions and the following disclaimer in 15 * the documentation and/or other materials provided with the 16 * distribution. 17 * * Neither the name of Intel Corporation nor the names of its 18 * contributors may be used to endorse or promote products derived 19 * from this software without specific prior written permission. 20 * 21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 24 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 25 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 27 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 28 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 29 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 30 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 31 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 32 */ 33 34 #include "spdk/stdinc.h" 35 36 #include "nvmf_internal.h" 37 #include "transport.h" 38 39 #include "spdk/likely.h" 40 #include "spdk/string.h" 41 #include "spdk/trace.h" 42 #include "spdk/nvmf_spec.h" 43 #include "spdk/uuid.h" 44 #include "spdk/json.h" 45 #include "spdk/file.h" 46 47 #include "spdk/bdev_module.h" 48 #include "spdk/log.h" 49 #include "spdk_internal/utf.h" 50 51 #define MODEL_NUMBER_DEFAULT "SPDK bdev Controller" 52 53 /* 54 * States for parsing valid domains in NQNs according to RFC 1034 55 */ 56 enum spdk_nvmf_nqn_domain_states { 57 /* First character of a domain must be a letter */ 58 SPDK_NVMF_DOMAIN_ACCEPT_LETTER = 0, 59 60 /* Subsequent characters can be any of letter, digit, or hyphen */ 61 SPDK_NVMF_DOMAIN_ACCEPT_LDH = 1, 62 63 /* A domain label must end with either a letter or digit */ 64 SPDK_NVMF_DOMAIN_ACCEPT_ANY = 2 65 }; 66 67 /* Returns true if is a valid ASCII string as defined by the NVMe spec */ 68 static bool 69 nvmf_valid_ascii_string(const void *buf, size_t size) 70 { 71 const uint8_t *str = buf; 72 size_t i; 73 74 for (i = 0; i < size; i++) { 75 if (str[i] < 0x20 || str[i] > 0x7E) { 76 return false; 77 } 78 } 79 80 return true; 81 } 82 83 static bool 84 nvmf_valid_nqn(const char *nqn) 85 { 86 size_t len; 87 struct spdk_uuid uuid_value; 88 uint32_t i; 89 int bytes_consumed; 90 uint32_t domain_label_length; 91 char *reverse_domain_end; 92 uint32_t reverse_domain_end_index; 93 enum spdk_nvmf_nqn_domain_states domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER; 94 95 /* Check for length requirements */ 96 len = strlen(nqn); 97 if (len > SPDK_NVMF_NQN_MAX_LEN) { 98 SPDK_ERRLOG("Invalid NQN \"%s\": length %zu > max %d\n", nqn, len, SPDK_NVMF_NQN_MAX_LEN); 99 return false; 100 } 101 102 /* The nqn must be at least as long as SPDK_NVMF_NQN_MIN_LEN to contain the necessary prefix. */ 103 if (len < SPDK_NVMF_NQN_MIN_LEN) { 104 SPDK_ERRLOG("Invalid NQN \"%s\": length %zu < min %d\n", nqn, len, SPDK_NVMF_NQN_MIN_LEN); 105 return false; 106 } 107 108 /* Check for discovery controller nqn */ 109 if (!strcmp(nqn, SPDK_NVMF_DISCOVERY_NQN)) { 110 return true; 111 } 112 113 /* Check for equality with the generic nqn structure of the form "nqn.2014-08.org.nvmexpress:uuid:11111111-2222-3333-4444-555555555555" */ 114 if (!strncmp(nqn, SPDK_NVMF_NQN_UUID_PRE, SPDK_NVMF_NQN_UUID_PRE_LEN)) { 115 if (len != SPDK_NVMF_NQN_UUID_PRE_LEN + SPDK_NVMF_UUID_STRING_LEN) { 116 SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not the correct length\n", nqn); 117 return false; 118 } 119 120 if (spdk_uuid_parse(&uuid_value, &nqn[SPDK_NVMF_NQN_UUID_PRE_LEN])) { 121 SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not formatted correctly\n", nqn); 122 return false; 123 } 124 return true; 125 } 126 127 /* If the nqn does not match the uuid structure, the next several checks validate the form "nqn.yyyy-mm.reverse.domain:user-string" */ 128 129 if (strncmp(nqn, "nqn.", 4) != 0) { 130 SPDK_ERRLOG("Invalid NQN \"%s\": NQN must begin with \"nqn.\".\n", nqn); 131 return false; 132 } 133 134 /* Check for yyyy-mm. */ 135 if (!(isdigit(nqn[4]) && isdigit(nqn[5]) && isdigit(nqn[6]) && isdigit(nqn[7]) && 136 nqn[8] == '-' && isdigit(nqn[9]) && isdigit(nqn[10]) && nqn[11] == '.')) { 137 SPDK_ERRLOG("Invalid date code in NQN \"%s\"\n", nqn); 138 return false; 139 } 140 141 reverse_domain_end = strchr(nqn, ':'); 142 if (reverse_domain_end != NULL && (reverse_domain_end_index = reverse_domain_end - nqn) < len - 1) { 143 } else { 144 SPDK_ERRLOG("Invalid NQN \"%s\". NQN must contain user specified name with a ':' as a prefix.\n", 145 nqn); 146 return false; 147 } 148 149 /* Check for valid reverse domain */ 150 domain_label_length = 0; 151 for (i = 12; i < reverse_domain_end_index; i++) { 152 if (domain_label_length > SPDK_DOMAIN_LABEL_MAX_LEN) { 153 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". At least one Label is too long.\n", nqn); 154 return false; 155 } 156 157 switch (domain_state) { 158 159 case SPDK_NVMF_DOMAIN_ACCEPT_LETTER: { 160 if (isalpha(nqn[i])) { 161 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY; 162 domain_label_length++; 163 break; 164 } else { 165 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must start with a letter.\n", nqn); 166 return false; 167 } 168 } 169 170 case SPDK_NVMF_DOMAIN_ACCEPT_LDH: { 171 if (isalpha(nqn[i]) || isdigit(nqn[i])) { 172 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY; 173 domain_label_length++; 174 break; 175 } else if (nqn[i] == '-') { 176 if (i == reverse_domain_end_index - 1) { 177 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n", 178 nqn); 179 return false; 180 } 181 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH; 182 domain_label_length++; 183 break; 184 } else if (nqn[i] == '.') { 185 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n", 186 nqn); 187 return false; 188 } else { 189 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n", 190 nqn); 191 return false; 192 } 193 } 194 195 case SPDK_NVMF_DOMAIN_ACCEPT_ANY: { 196 if (isalpha(nqn[i]) || isdigit(nqn[i])) { 197 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY; 198 domain_label_length++; 199 break; 200 } else if (nqn[i] == '-') { 201 if (i == reverse_domain_end_index - 1) { 202 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n", 203 nqn); 204 return false; 205 } 206 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH; 207 domain_label_length++; 208 break; 209 } else if (nqn[i] == '.') { 210 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER; 211 domain_label_length = 0; 212 break; 213 } else { 214 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n", 215 nqn); 216 return false; 217 } 218 } 219 } 220 } 221 222 i = reverse_domain_end_index + 1; 223 while (i < len) { 224 bytes_consumed = utf8_valid(&nqn[i], &nqn[len]); 225 if (bytes_consumed <= 0) { 226 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only valid utf-8.\n", nqn); 227 return false; 228 } 229 230 i += bytes_consumed; 231 } 232 return true; 233 } 234 235 static void subsystem_state_change_on_pg(struct spdk_io_channel_iter *i); 236 237 struct spdk_nvmf_subsystem * 238 spdk_nvmf_subsystem_create(struct spdk_nvmf_tgt *tgt, 239 const char *nqn, 240 enum spdk_nvmf_subtype type, 241 uint32_t num_ns) 242 { 243 struct spdk_nvmf_subsystem *subsystem; 244 uint32_t sid; 245 246 if (spdk_nvmf_tgt_find_subsystem(tgt, nqn)) { 247 SPDK_ERRLOG("Subsystem NQN '%s' already exists\n", nqn); 248 return NULL; 249 } 250 251 if (!nvmf_valid_nqn(nqn)) { 252 return NULL; 253 } 254 255 if (type == SPDK_NVMF_SUBTYPE_DISCOVERY && num_ns != 0) { 256 SPDK_ERRLOG("Discovery subsystem cannot have namespaces.\n"); 257 return NULL; 258 } 259 260 /* Find a free subsystem id (sid) */ 261 for (sid = 0; sid < tgt->max_subsystems; sid++) { 262 if (tgt->subsystems[sid] == NULL) { 263 break; 264 } 265 } 266 if (sid >= tgt->max_subsystems) { 267 return NULL; 268 } 269 270 subsystem = calloc(1, sizeof(struct spdk_nvmf_subsystem)); 271 if (subsystem == NULL) { 272 return NULL; 273 } 274 275 subsystem->thread = spdk_get_thread(); 276 subsystem->state = SPDK_NVMF_SUBSYSTEM_INACTIVE; 277 subsystem->tgt = tgt; 278 subsystem->id = sid; 279 subsystem->subtype = type; 280 subsystem->max_nsid = num_ns; 281 subsystem->max_allowed_nsid = num_ns; 282 subsystem->next_cntlid = 0; 283 snprintf(subsystem->subnqn, sizeof(subsystem->subnqn), "%s", nqn); 284 pthread_mutex_init(&subsystem->mutex, NULL); 285 TAILQ_INIT(&subsystem->listeners); 286 TAILQ_INIT(&subsystem->hosts); 287 TAILQ_INIT(&subsystem->ctrlrs); 288 289 if (num_ns != 0) { 290 subsystem->ns = calloc(num_ns, sizeof(struct spdk_nvmf_ns *)); 291 if (subsystem->ns == NULL) { 292 SPDK_ERRLOG("Namespace memory allocation failed\n"); 293 pthread_mutex_destroy(&subsystem->mutex); 294 free(subsystem); 295 return NULL; 296 } 297 } 298 299 memset(subsystem->sn, '0', sizeof(subsystem->sn) - 1); 300 subsystem->sn[sizeof(subsystem->sn) - 1] = '\0'; 301 302 snprintf(subsystem->mn, sizeof(subsystem->mn), "%s", 303 MODEL_NUMBER_DEFAULT); 304 305 tgt->subsystems[sid] = subsystem; 306 tgt->discovery_genctr++; 307 308 return subsystem; 309 } 310 311 /* Must hold subsystem->mutex while calling this function */ 312 static void 313 nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_host *host) 314 { 315 TAILQ_REMOVE(&subsystem->hosts, host, link); 316 free(host); 317 } 318 319 static void 320 _nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem, 321 struct spdk_nvmf_subsystem_listener *listener, 322 bool stop) 323 { 324 struct spdk_nvmf_transport *transport; 325 326 if (stop) { 327 transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, listener->trid->trstring); 328 if (transport != NULL) { 329 spdk_nvmf_transport_stop_listen(transport, listener->trid); 330 } 331 } 332 333 TAILQ_REMOVE(&subsystem->listeners, listener, link); 334 free(listener); 335 } 336 337 void 338 spdk_nvmf_subsystem_destroy(struct spdk_nvmf_subsystem *subsystem) 339 { 340 struct spdk_nvmf_host *host, *host_tmp; 341 struct spdk_nvmf_ctrlr *ctrlr, *ctrlr_tmp; 342 struct spdk_nvmf_ns *ns; 343 344 if (!subsystem) { 345 return; 346 } 347 348 assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE); 349 350 SPDK_DEBUGLOG(nvmf, "subsystem is %p\n", subsystem); 351 352 nvmf_subsystem_remove_all_listeners(subsystem, false); 353 354 pthread_mutex_lock(&subsystem->mutex); 355 356 TAILQ_FOREACH_SAFE(host, &subsystem->hosts, link, host_tmp) { 357 nvmf_subsystem_remove_host(subsystem, host); 358 } 359 360 pthread_mutex_unlock(&subsystem->mutex); 361 362 TAILQ_FOREACH_SAFE(ctrlr, &subsystem->ctrlrs, link, ctrlr_tmp) { 363 nvmf_ctrlr_destruct(ctrlr); 364 } 365 366 ns = spdk_nvmf_subsystem_get_first_ns(subsystem); 367 while (ns != NULL) { 368 struct spdk_nvmf_ns *next_ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns); 369 370 spdk_nvmf_subsystem_remove_ns(subsystem, ns->opts.nsid); 371 ns = next_ns; 372 } 373 374 free(subsystem->ns); 375 376 subsystem->tgt->subsystems[subsystem->id] = NULL; 377 subsystem->tgt->discovery_genctr++; 378 379 pthread_mutex_destroy(&subsystem->mutex); 380 381 free(subsystem); 382 } 383 384 385 /* we have to use the typedef in the function declaration to appease astyle. */ 386 typedef enum spdk_nvmf_subsystem_state spdk_nvmf_subsystem_state_t; 387 388 static spdk_nvmf_subsystem_state_t 389 nvmf_subsystem_get_intermediate_state(enum spdk_nvmf_subsystem_state current_state, 390 enum spdk_nvmf_subsystem_state requested_state) 391 { 392 switch (requested_state) { 393 case SPDK_NVMF_SUBSYSTEM_INACTIVE: 394 return SPDK_NVMF_SUBSYSTEM_DEACTIVATING; 395 case SPDK_NVMF_SUBSYSTEM_ACTIVE: 396 if (current_state == SPDK_NVMF_SUBSYSTEM_PAUSED) { 397 return SPDK_NVMF_SUBSYSTEM_RESUMING; 398 } else { 399 return SPDK_NVMF_SUBSYSTEM_ACTIVATING; 400 } 401 case SPDK_NVMF_SUBSYSTEM_PAUSED: 402 return SPDK_NVMF_SUBSYSTEM_PAUSING; 403 default: 404 assert(false); 405 return SPDK_NVMF_SUBSYSTEM_NUM_STATES; 406 } 407 } 408 409 static int 410 nvmf_subsystem_set_state(struct spdk_nvmf_subsystem *subsystem, 411 enum spdk_nvmf_subsystem_state state) 412 { 413 enum spdk_nvmf_subsystem_state actual_old_state, expected_old_state; 414 bool exchanged; 415 416 switch (state) { 417 case SPDK_NVMF_SUBSYSTEM_INACTIVE: 418 expected_old_state = SPDK_NVMF_SUBSYSTEM_DEACTIVATING; 419 break; 420 case SPDK_NVMF_SUBSYSTEM_ACTIVATING: 421 expected_old_state = SPDK_NVMF_SUBSYSTEM_INACTIVE; 422 break; 423 case SPDK_NVMF_SUBSYSTEM_ACTIVE: 424 expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING; 425 break; 426 case SPDK_NVMF_SUBSYSTEM_PAUSING: 427 expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 428 break; 429 case SPDK_NVMF_SUBSYSTEM_PAUSED: 430 expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSING; 431 break; 432 case SPDK_NVMF_SUBSYSTEM_RESUMING: 433 expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSED; 434 break; 435 case SPDK_NVMF_SUBSYSTEM_DEACTIVATING: 436 expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 437 break; 438 default: 439 assert(false); 440 return -1; 441 } 442 443 actual_old_state = expected_old_state; 444 exchanged = __atomic_compare_exchange_n(&subsystem->state, &actual_old_state, state, false, 445 __ATOMIC_RELAXED, __ATOMIC_RELAXED); 446 if (spdk_unlikely(exchanged == false)) { 447 if (actual_old_state == SPDK_NVMF_SUBSYSTEM_RESUMING && 448 state == SPDK_NVMF_SUBSYSTEM_ACTIVE) { 449 expected_old_state = SPDK_NVMF_SUBSYSTEM_RESUMING; 450 } 451 /* This is for the case when activating the subsystem fails. */ 452 if (actual_old_state == SPDK_NVMF_SUBSYSTEM_ACTIVATING && 453 state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING) { 454 expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING; 455 } 456 /* This is for the case when resuming the subsystem fails. */ 457 if (actual_old_state == SPDK_NVMF_SUBSYSTEM_RESUMING && 458 state == SPDK_NVMF_SUBSYSTEM_PAUSING) { 459 expected_old_state = SPDK_NVMF_SUBSYSTEM_RESUMING; 460 } 461 actual_old_state = expected_old_state; 462 __atomic_compare_exchange_n(&subsystem->state, &actual_old_state, state, false, 463 __ATOMIC_RELAXED, __ATOMIC_RELAXED); 464 } 465 assert(actual_old_state == expected_old_state); 466 return actual_old_state - expected_old_state; 467 } 468 469 struct subsystem_state_change_ctx { 470 struct spdk_nvmf_subsystem *subsystem; 471 472 enum spdk_nvmf_subsystem_state original_state; 473 474 enum spdk_nvmf_subsystem_state requested_state; 475 476 spdk_nvmf_subsystem_state_change_done cb_fn; 477 void *cb_arg; 478 }; 479 480 static void 481 subsystem_state_change_revert_done(struct spdk_io_channel_iter *i, int status) 482 { 483 struct subsystem_state_change_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 484 485 /* Nothing to be done here if the state setting fails, we are just screwed. */ 486 if (nvmf_subsystem_set_state(ctx->subsystem, ctx->requested_state)) { 487 SPDK_ERRLOG("Unable to revert the subsystem state after operation failure.\n"); 488 } 489 490 ctx->subsystem->changing_state = false; 491 if (ctx->cb_fn) { 492 /* return a failure here. This function only exists in an error path. */ 493 ctx->cb_fn(ctx->subsystem, ctx->cb_arg, -1); 494 } 495 free(ctx); 496 } 497 498 static void 499 subsystem_state_change_done(struct spdk_io_channel_iter *i, int status) 500 { 501 struct subsystem_state_change_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 502 enum spdk_nvmf_subsystem_state intermediate_state; 503 504 if (status == 0) { 505 status = nvmf_subsystem_set_state(ctx->subsystem, ctx->requested_state); 506 if (status) { 507 status = -1; 508 } 509 } 510 511 if (status) { 512 intermediate_state = nvmf_subsystem_get_intermediate_state(ctx->requested_state, 513 ctx->original_state); 514 assert(intermediate_state != SPDK_NVMF_SUBSYSTEM_NUM_STATES); 515 516 if (nvmf_subsystem_set_state(ctx->subsystem, intermediate_state)) { 517 goto out; 518 } 519 ctx->requested_state = ctx->original_state; 520 spdk_for_each_channel(ctx->subsystem->tgt, 521 subsystem_state_change_on_pg, 522 ctx, 523 subsystem_state_change_revert_done); 524 return; 525 } 526 527 out: 528 ctx->subsystem->changing_state = false; 529 if (ctx->cb_fn) { 530 ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status); 531 } 532 free(ctx); 533 } 534 535 static void 536 subsystem_state_change_continue(void *ctx, int status) 537 { 538 struct spdk_io_channel_iter *i = ctx; 539 spdk_for_each_channel_continue(i, status); 540 } 541 542 static void 543 subsystem_state_change_on_pg(struct spdk_io_channel_iter *i) 544 { 545 struct subsystem_state_change_ctx *ctx; 546 struct spdk_io_channel *ch; 547 struct spdk_nvmf_poll_group *group; 548 549 ctx = spdk_io_channel_iter_get_ctx(i); 550 ch = spdk_io_channel_iter_get_channel(i); 551 group = spdk_io_channel_get_ctx(ch); 552 553 switch (ctx->requested_state) { 554 case SPDK_NVMF_SUBSYSTEM_INACTIVE: 555 nvmf_poll_group_remove_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i); 556 break; 557 case SPDK_NVMF_SUBSYSTEM_ACTIVE: 558 if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_ACTIVATING) { 559 nvmf_poll_group_add_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i); 560 } else if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_RESUMING) { 561 nvmf_poll_group_resume_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i); 562 } 563 break; 564 case SPDK_NVMF_SUBSYSTEM_PAUSED: 565 nvmf_poll_group_pause_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i); 566 break; 567 default: 568 assert(false); 569 break; 570 } 571 } 572 573 static int 574 nvmf_subsystem_state_change(struct spdk_nvmf_subsystem *subsystem, 575 enum spdk_nvmf_subsystem_state requested_state, 576 spdk_nvmf_subsystem_state_change_done cb_fn, 577 void *cb_arg) 578 { 579 struct subsystem_state_change_ctx *ctx; 580 enum spdk_nvmf_subsystem_state intermediate_state; 581 int rc; 582 583 if (__sync_val_compare_and_swap(&subsystem->changing_state, false, true)) { 584 return -EBUSY; 585 } 586 587 /* If we are already in the requested state, just call the callback immediately. */ 588 if (subsystem->state == requested_state) { 589 subsystem->changing_state = false; 590 if (cb_fn) { 591 cb_fn(subsystem, cb_arg, 0); 592 } 593 return 0; 594 } 595 596 intermediate_state = nvmf_subsystem_get_intermediate_state(subsystem->state, requested_state); 597 assert(intermediate_state != SPDK_NVMF_SUBSYSTEM_NUM_STATES); 598 599 ctx = calloc(1, sizeof(*ctx)); 600 if (!ctx) { 601 subsystem->changing_state = false; 602 return -ENOMEM; 603 } 604 605 ctx->original_state = subsystem->state; 606 rc = nvmf_subsystem_set_state(subsystem, intermediate_state); 607 if (rc) { 608 free(ctx); 609 subsystem->changing_state = false; 610 return rc; 611 } 612 613 ctx->subsystem = subsystem; 614 ctx->requested_state = requested_state; 615 ctx->cb_fn = cb_fn; 616 ctx->cb_arg = cb_arg; 617 618 spdk_for_each_channel(subsystem->tgt, 619 subsystem_state_change_on_pg, 620 ctx, 621 subsystem_state_change_done); 622 623 return 0; 624 } 625 626 int 627 spdk_nvmf_subsystem_start(struct spdk_nvmf_subsystem *subsystem, 628 spdk_nvmf_subsystem_state_change_done cb_fn, 629 void *cb_arg) 630 { 631 return nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg); 632 } 633 634 int 635 spdk_nvmf_subsystem_stop(struct spdk_nvmf_subsystem *subsystem, 636 spdk_nvmf_subsystem_state_change_done cb_fn, 637 void *cb_arg) 638 { 639 return nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_INACTIVE, cb_fn, cb_arg); 640 } 641 642 int 643 spdk_nvmf_subsystem_pause(struct spdk_nvmf_subsystem *subsystem, 644 spdk_nvmf_subsystem_state_change_done cb_fn, 645 void *cb_arg) 646 { 647 return nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_PAUSED, cb_fn, cb_arg); 648 } 649 650 int 651 spdk_nvmf_subsystem_resume(struct spdk_nvmf_subsystem *subsystem, 652 spdk_nvmf_subsystem_state_change_done cb_fn, 653 void *cb_arg) 654 { 655 return nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg); 656 } 657 658 struct spdk_nvmf_subsystem * 659 spdk_nvmf_subsystem_get_first(struct spdk_nvmf_tgt *tgt) 660 { 661 struct spdk_nvmf_subsystem *subsystem; 662 uint32_t sid; 663 664 for (sid = 0; sid < tgt->max_subsystems; sid++) { 665 subsystem = tgt->subsystems[sid]; 666 if (subsystem) { 667 return subsystem; 668 } 669 } 670 671 return NULL; 672 } 673 674 struct spdk_nvmf_subsystem * 675 spdk_nvmf_subsystem_get_next(struct spdk_nvmf_subsystem *subsystem) 676 { 677 uint32_t sid; 678 struct spdk_nvmf_tgt *tgt; 679 680 if (!subsystem) { 681 return NULL; 682 } 683 684 tgt = subsystem->tgt; 685 686 for (sid = subsystem->id + 1; sid < tgt->max_subsystems; sid++) { 687 subsystem = tgt->subsystems[sid]; 688 if (subsystem) { 689 return subsystem; 690 } 691 } 692 693 return NULL; 694 } 695 696 /* Must hold subsystem->mutex while calling this function */ 697 static struct spdk_nvmf_host * 698 nvmf_subsystem_find_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn) 699 { 700 struct spdk_nvmf_host *host = NULL; 701 702 TAILQ_FOREACH(host, &subsystem->hosts, link) { 703 if (strcmp(hostnqn, host->nqn) == 0) { 704 return host; 705 } 706 } 707 708 return NULL; 709 } 710 711 int 712 spdk_nvmf_subsystem_add_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn) 713 { 714 struct spdk_nvmf_host *host; 715 716 if (!nvmf_valid_nqn(hostnqn)) { 717 return -EINVAL; 718 } 719 720 pthread_mutex_lock(&subsystem->mutex); 721 722 if (nvmf_subsystem_find_host(subsystem, hostnqn)) { 723 /* This subsystem already allows the specified host. */ 724 pthread_mutex_unlock(&subsystem->mutex); 725 return 0; 726 } 727 728 host = calloc(1, sizeof(*host)); 729 if (!host) { 730 pthread_mutex_unlock(&subsystem->mutex); 731 return -ENOMEM; 732 } 733 734 snprintf(host->nqn, sizeof(host->nqn), "%s", hostnqn); 735 736 TAILQ_INSERT_HEAD(&subsystem->hosts, host, link); 737 738 subsystem->tgt->discovery_genctr++; 739 740 pthread_mutex_unlock(&subsystem->mutex); 741 742 return 0; 743 } 744 745 int 746 spdk_nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn) 747 { 748 struct spdk_nvmf_host *host; 749 750 pthread_mutex_lock(&subsystem->mutex); 751 752 host = nvmf_subsystem_find_host(subsystem, hostnqn); 753 if (host == NULL) { 754 pthread_mutex_unlock(&subsystem->mutex); 755 return -ENOENT; 756 } 757 758 nvmf_subsystem_remove_host(subsystem, host); 759 pthread_mutex_unlock(&subsystem->mutex); 760 761 return 0; 762 } 763 764 struct nvmf_subsystem_disconnect_host_ctx { 765 struct spdk_nvmf_subsystem *subsystem; 766 char *hostnqn; 767 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn; 768 void *cb_arg; 769 }; 770 771 static void 772 nvmf_subsystem_disconnect_host_fini(struct spdk_io_channel_iter *i, int status) 773 { 774 struct nvmf_subsystem_disconnect_host_ctx *ctx; 775 776 ctx = spdk_io_channel_iter_get_ctx(i); 777 778 if (ctx->cb_fn) { 779 ctx->cb_fn(ctx->cb_arg, status); 780 } 781 free(ctx->hostnqn); 782 free(ctx); 783 } 784 785 static void 786 nvmf_subsystem_disconnect_qpairs_by_host(struct spdk_io_channel_iter *i) 787 { 788 struct nvmf_subsystem_disconnect_host_ctx *ctx; 789 struct spdk_nvmf_poll_group *group; 790 struct spdk_io_channel *ch; 791 struct spdk_nvmf_qpair *qpair, *tmp_qpair; 792 struct spdk_nvmf_ctrlr *ctrlr; 793 794 ctx = spdk_io_channel_iter_get_ctx(i); 795 ch = spdk_io_channel_iter_get_channel(i); 796 group = spdk_io_channel_get_ctx(ch); 797 798 TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, tmp_qpair) { 799 ctrlr = qpair->ctrlr; 800 801 if (ctrlr == NULL || ctrlr->subsys != ctx->subsystem) { 802 continue; 803 } 804 805 if (strncmp(ctrlr->hostnqn, ctx->hostnqn, sizeof(ctrlr->hostnqn)) == 0) { 806 /* Right now this does not wait for the queue pairs to actually disconnect. */ 807 spdk_nvmf_qpair_disconnect(qpair, NULL, NULL); 808 } 809 } 810 spdk_for_each_channel_continue(i, 0); 811 } 812 813 int 814 spdk_nvmf_subsystem_disconnect_host(struct spdk_nvmf_subsystem *subsystem, 815 const char *hostnqn, 816 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, 817 void *cb_arg) 818 { 819 struct nvmf_subsystem_disconnect_host_ctx *ctx; 820 821 ctx = calloc(1, sizeof(struct nvmf_subsystem_disconnect_host_ctx)); 822 if (ctx == NULL) { 823 return -ENOMEM; 824 } 825 826 ctx->subsystem = subsystem; 827 ctx->hostnqn = strdup(hostnqn); 828 ctx->cb_fn = cb_fn; 829 ctx->cb_arg = cb_arg; 830 831 spdk_for_each_channel(subsystem->tgt, nvmf_subsystem_disconnect_qpairs_by_host, ctx, 832 nvmf_subsystem_disconnect_host_fini); 833 834 return 0; 835 } 836 837 int 838 spdk_nvmf_subsystem_set_allow_any_host(struct spdk_nvmf_subsystem *subsystem, bool allow_any_host) 839 { 840 pthread_mutex_lock(&subsystem->mutex); 841 subsystem->flags.allow_any_host = allow_any_host; 842 pthread_mutex_unlock(&subsystem->mutex); 843 844 return 0; 845 } 846 847 bool 848 spdk_nvmf_subsystem_get_allow_any_host(const struct spdk_nvmf_subsystem *subsystem) 849 { 850 bool allow_any_host; 851 struct spdk_nvmf_subsystem *sub; 852 853 /* Technically, taking the mutex modifies data in the subsystem. But the const 854 * is still important to convey that this doesn't mutate any other data. Cast 855 * it away to work around this. */ 856 sub = (struct spdk_nvmf_subsystem *)subsystem; 857 858 pthread_mutex_lock(&sub->mutex); 859 allow_any_host = sub->flags.allow_any_host; 860 pthread_mutex_unlock(&sub->mutex); 861 862 return allow_any_host; 863 } 864 865 bool 866 spdk_nvmf_subsystem_host_allowed(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn) 867 { 868 bool allowed; 869 870 if (!hostnqn) { 871 return false; 872 } 873 874 pthread_mutex_lock(&subsystem->mutex); 875 876 if (subsystem->flags.allow_any_host) { 877 pthread_mutex_unlock(&subsystem->mutex); 878 return true; 879 } 880 881 allowed = nvmf_subsystem_find_host(subsystem, hostnqn) != NULL; 882 pthread_mutex_unlock(&subsystem->mutex); 883 884 return allowed; 885 } 886 887 struct spdk_nvmf_host * 888 spdk_nvmf_subsystem_get_first_host(struct spdk_nvmf_subsystem *subsystem) 889 { 890 return TAILQ_FIRST(&subsystem->hosts); 891 } 892 893 894 struct spdk_nvmf_host * 895 spdk_nvmf_subsystem_get_next_host(struct spdk_nvmf_subsystem *subsystem, 896 struct spdk_nvmf_host *prev_host) 897 { 898 return TAILQ_NEXT(prev_host, link); 899 } 900 901 const char * 902 spdk_nvmf_host_get_nqn(const struct spdk_nvmf_host *host) 903 { 904 return host->nqn; 905 } 906 907 struct spdk_nvmf_subsystem_listener * 908 nvmf_subsystem_find_listener(struct spdk_nvmf_subsystem *subsystem, 909 const struct spdk_nvme_transport_id *trid) 910 { 911 struct spdk_nvmf_subsystem_listener *listener; 912 913 TAILQ_FOREACH(listener, &subsystem->listeners, link) { 914 if (spdk_nvme_transport_id_compare(listener->trid, trid) == 0) { 915 return listener; 916 } 917 } 918 919 return NULL; 920 } 921 922 /** 923 * Function to be called once the target is listening. 924 * 925 * \param ctx Context argument passed to this function. 926 * \param status 0 if it completed successfully, or negative errno if it failed. 927 */ 928 static void 929 _nvmf_subsystem_add_listener_done(void *ctx, int status) 930 { 931 struct spdk_nvmf_subsystem_listener *listener = ctx; 932 933 if (status) { 934 listener->cb_fn(listener->cb_arg, status); 935 free(listener); 936 return; 937 } 938 939 TAILQ_INSERT_HEAD(&listener->subsystem->listeners, listener, link); 940 listener->subsystem->tgt->discovery_genctr++; 941 listener->cb_fn(listener->cb_arg, status); 942 } 943 944 void 945 spdk_nvmf_subsystem_add_listener(struct spdk_nvmf_subsystem *subsystem, 946 struct spdk_nvme_transport_id *trid, 947 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, 948 void *cb_arg) 949 { 950 struct spdk_nvmf_transport *transport; 951 struct spdk_nvmf_subsystem_listener *listener; 952 struct spdk_nvmf_listener *tr_listener; 953 int rc = 0; 954 955 assert(cb_fn != NULL); 956 957 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 958 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 959 cb_fn(cb_arg, -EAGAIN); 960 return; 961 } 962 963 if (nvmf_subsystem_find_listener(subsystem, trid)) { 964 /* Listener already exists in this subsystem */ 965 cb_fn(cb_arg, 0); 966 return; 967 } 968 969 transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, trid->trstring); 970 if (transport == NULL) { 971 SPDK_ERRLOG("Unknown transport type %d\n", trid->trtype); 972 cb_fn(cb_arg, -EINVAL); 973 return; 974 } 975 976 tr_listener = nvmf_transport_find_listener(transport, trid); 977 if (!tr_listener) { 978 SPDK_ERRLOG("Cannot find transport listener for %s\n", trid->traddr); 979 cb_fn(cb_arg, -EINVAL); 980 return; 981 } 982 983 listener = calloc(1, sizeof(*listener)); 984 if (!listener) { 985 cb_fn(cb_arg, -ENOMEM); 986 return; 987 } 988 989 listener->trid = &tr_listener->trid; 990 listener->transport = transport; 991 listener->cb_fn = cb_fn; 992 listener->cb_arg = cb_arg; 993 listener->subsystem = subsystem; 994 listener->ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE; 995 996 if (transport->ops->listen_associate != NULL) { 997 rc = transport->ops->listen_associate(transport, subsystem, trid); 998 } 999 1000 _nvmf_subsystem_add_listener_done(listener, rc); 1001 } 1002 1003 int 1004 spdk_nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem, 1005 const struct spdk_nvme_transport_id *trid) 1006 { 1007 struct spdk_nvmf_subsystem_listener *listener; 1008 1009 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 1010 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 1011 return -EAGAIN; 1012 } 1013 1014 listener = nvmf_subsystem_find_listener(subsystem, trid); 1015 if (listener == NULL) { 1016 return -ENOENT; 1017 } 1018 1019 _nvmf_subsystem_remove_listener(subsystem, listener, false); 1020 1021 return 0; 1022 } 1023 1024 void 1025 nvmf_subsystem_remove_all_listeners(struct spdk_nvmf_subsystem *subsystem, 1026 bool stop) 1027 { 1028 struct spdk_nvmf_subsystem_listener *listener, *listener_tmp; 1029 1030 TAILQ_FOREACH_SAFE(listener, &subsystem->listeners, link, listener_tmp) { 1031 _nvmf_subsystem_remove_listener(subsystem, listener, stop); 1032 } 1033 } 1034 1035 bool 1036 spdk_nvmf_subsystem_listener_allowed(struct spdk_nvmf_subsystem *subsystem, 1037 const struct spdk_nvme_transport_id *trid) 1038 { 1039 struct spdk_nvmf_subsystem_listener *listener; 1040 1041 if (!strcmp(subsystem->subnqn, SPDK_NVMF_DISCOVERY_NQN)) { 1042 return true; 1043 } 1044 1045 TAILQ_FOREACH(listener, &subsystem->listeners, link) { 1046 if (spdk_nvme_transport_id_compare(listener->trid, trid) == 0) { 1047 return true; 1048 } 1049 } 1050 1051 return false; 1052 } 1053 1054 struct spdk_nvmf_subsystem_listener * 1055 spdk_nvmf_subsystem_get_first_listener(struct spdk_nvmf_subsystem *subsystem) 1056 { 1057 return TAILQ_FIRST(&subsystem->listeners); 1058 } 1059 1060 struct spdk_nvmf_subsystem_listener * 1061 spdk_nvmf_subsystem_get_next_listener(struct spdk_nvmf_subsystem *subsystem, 1062 struct spdk_nvmf_subsystem_listener *prev_listener) 1063 { 1064 return TAILQ_NEXT(prev_listener, link); 1065 } 1066 1067 const struct spdk_nvme_transport_id * 1068 spdk_nvmf_subsystem_listener_get_trid(struct spdk_nvmf_subsystem_listener *listener) 1069 { 1070 return listener->trid; 1071 } 1072 1073 void 1074 spdk_nvmf_subsystem_allow_any_listener(struct spdk_nvmf_subsystem *subsystem, 1075 bool allow_any_listener) 1076 { 1077 subsystem->flags.allow_any_listener = allow_any_listener; 1078 } 1079 1080 bool 1081 spdk_nvmf_subsytem_any_listener_allowed(struct spdk_nvmf_subsystem *subsystem) 1082 { 1083 return subsystem->flags.allow_any_listener; 1084 } 1085 1086 1087 struct subsystem_update_ns_ctx { 1088 struct spdk_nvmf_subsystem *subsystem; 1089 1090 spdk_nvmf_subsystem_state_change_done cb_fn; 1091 void *cb_arg; 1092 }; 1093 1094 static void 1095 subsystem_update_ns_done(struct spdk_io_channel_iter *i, int status) 1096 { 1097 struct subsystem_update_ns_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 1098 1099 if (ctx->cb_fn) { 1100 ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status); 1101 } 1102 free(ctx); 1103 } 1104 1105 static void 1106 subsystem_update_ns_on_pg(struct spdk_io_channel_iter *i) 1107 { 1108 int rc; 1109 struct subsystem_update_ns_ctx *ctx; 1110 struct spdk_nvmf_poll_group *group; 1111 struct spdk_nvmf_subsystem *subsystem; 1112 1113 ctx = spdk_io_channel_iter_get_ctx(i); 1114 group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i)); 1115 subsystem = ctx->subsystem; 1116 1117 rc = nvmf_poll_group_update_subsystem(group, subsystem); 1118 spdk_for_each_channel_continue(i, rc); 1119 } 1120 1121 static int 1122 nvmf_subsystem_update_ns(struct spdk_nvmf_subsystem *subsystem, spdk_channel_for_each_cpl cpl, 1123 void *ctx) 1124 { 1125 spdk_for_each_channel(subsystem->tgt, 1126 subsystem_update_ns_on_pg, 1127 ctx, 1128 cpl); 1129 1130 return 0; 1131 } 1132 1133 static void 1134 nvmf_subsystem_ns_changed(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 1135 { 1136 struct spdk_nvmf_ctrlr *ctrlr; 1137 1138 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 1139 nvmf_ctrlr_ns_changed(ctrlr, nsid); 1140 } 1141 } 1142 1143 int 1144 spdk_nvmf_subsystem_remove_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 1145 { 1146 struct spdk_nvmf_ns *ns; 1147 struct spdk_nvmf_registrant *reg, *reg_tmp; 1148 1149 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 1150 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 1151 assert(false); 1152 return -1; 1153 } 1154 1155 if (nsid == 0 || nsid > subsystem->max_nsid) { 1156 return -1; 1157 } 1158 1159 ns = subsystem->ns[nsid - 1]; 1160 if (!ns) { 1161 return -1; 1162 } 1163 1164 subsystem->ns[nsid - 1] = NULL; 1165 1166 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) { 1167 TAILQ_REMOVE(&ns->registrants, reg, link); 1168 free(reg); 1169 } 1170 spdk_bdev_module_release_bdev(ns->bdev); 1171 spdk_bdev_close(ns->desc); 1172 if (ns->ptpl_file) { 1173 free(ns->ptpl_file); 1174 } 1175 free(ns); 1176 1177 nvmf_subsystem_ns_changed(subsystem, nsid); 1178 1179 return 0; 1180 } 1181 1182 struct subsystem_ns_change_ctx { 1183 struct spdk_nvmf_subsystem *subsystem; 1184 spdk_nvmf_subsystem_state_change_done cb_fn; 1185 uint32_t nsid; 1186 }; 1187 1188 static void 1189 _nvmf_ns_hot_remove(struct spdk_nvmf_subsystem *subsystem, 1190 void *cb_arg, int status) 1191 { 1192 struct subsystem_ns_change_ctx *ctx = cb_arg; 1193 int rc; 1194 1195 rc = spdk_nvmf_subsystem_remove_ns(subsystem, ctx->nsid); 1196 if (rc != 0) { 1197 SPDK_ERRLOG("Failed to make changes to NVME-oF subsystem with id: %u\n", subsystem->id); 1198 } 1199 1200 spdk_nvmf_subsystem_resume(subsystem, NULL, NULL); 1201 1202 free(ctx); 1203 } 1204 1205 static void 1206 nvmf_ns_change_msg(void *ns_ctx) 1207 { 1208 struct subsystem_ns_change_ctx *ctx = ns_ctx; 1209 int rc; 1210 1211 rc = spdk_nvmf_subsystem_pause(ctx->subsystem, ctx->cb_fn, ctx); 1212 if (rc) { 1213 if (rc == -EBUSY) { 1214 /* Try again, this is not a permanent situation. */ 1215 spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ctx); 1216 } else { 1217 free(ctx); 1218 SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n"); 1219 } 1220 } 1221 } 1222 1223 static void 1224 nvmf_ns_hot_remove(void *remove_ctx) 1225 { 1226 struct spdk_nvmf_ns *ns = remove_ctx; 1227 struct subsystem_ns_change_ctx *ns_ctx; 1228 int rc; 1229 1230 /* We have to allocate a new context because this op 1231 * is asynchronous and we could lose the ns in the middle. 1232 */ 1233 ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx)); 1234 if (!ns_ctx) { 1235 SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n"); 1236 return; 1237 } 1238 1239 ns_ctx->subsystem = ns->subsystem; 1240 ns_ctx->nsid = ns->opts.nsid; 1241 ns_ctx->cb_fn = _nvmf_ns_hot_remove; 1242 1243 rc = spdk_nvmf_subsystem_pause(ns->subsystem, _nvmf_ns_hot_remove, ns_ctx); 1244 if (rc) { 1245 if (rc == -EBUSY) { 1246 /* Try again, this is not a permanent situation. */ 1247 spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx); 1248 } else { 1249 SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n"); 1250 free(ns_ctx); 1251 } 1252 } 1253 } 1254 1255 static void 1256 _nvmf_ns_resize(struct spdk_nvmf_subsystem *subsystem, void *cb_arg, int status) 1257 { 1258 struct subsystem_ns_change_ctx *ctx = cb_arg; 1259 1260 nvmf_subsystem_ns_changed(subsystem, ctx->nsid); 1261 spdk_nvmf_subsystem_resume(subsystem, NULL, NULL); 1262 1263 free(ctx); 1264 } 1265 1266 static void 1267 nvmf_ns_resize(void *event_ctx) 1268 { 1269 struct spdk_nvmf_ns *ns = event_ctx; 1270 struct subsystem_ns_change_ctx *ns_ctx; 1271 int rc; 1272 1273 /* We have to allocate a new context because this op 1274 * is asynchronous and we could lose the ns in the middle. 1275 */ 1276 ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx)); 1277 if (!ns_ctx) { 1278 SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n"); 1279 return; 1280 } 1281 1282 ns_ctx->subsystem = ns->subsystem; 1283 ns_ctx->nsid = ns->opts.nsid; 1284 ns_ctx->cb_fn = _nvmf_ns_resize; 1285 1286 rc = spdk_nvmf_subsystem_pause(ns->subsystem, _nvmf_ns_resize, ns_ctx); 1287 if (rc) { 1288 if (rc == -EBUSY) { 1289 /* Try again, this is not a permanent situation. */ 1290 spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx); 1291 } 1292 SPDK_ERRLOG("Unable to pause subsystem to process namespace resize!\n"); 1293 free(ns_ctx); 1294 } 1295 } 1296 1297 static void 1298 nvmf_ns_event(enum spdk_bdev_event_type type, 1299 struct spdk_bdev *bdev, 1300 void *event_ctx) 1301 { 1302 SPDK_DEBUGLOG(nvmf, "Bdev event: type %d, name %s, subsystem_id %d, ns_id %d\n", 1303 type, 1304 bdev->name, 1305 ((struct spdk_nvmf_ns *)event_ctx)->subsystem->id, 1306 ((struct spdk_nvmf_ns *)event_ctx)->nsid); 1307 1308 switch (type) { 1309 case SPDK_BDEV_EVENT_REMOVE: 1310 nvmf_ns_hot_remove(event_ctx); 1311 break; 1312 case SPDK_BDEV_EVENT_RESIZE: 1313 nvmf_ns_resize(event_ctx); 1314 break; 1315 default: 1316 SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); 1317 break; 1318 } 1319 } 1320 1321 void 1322 spdk_nvmf_ns_opts_get_defaults(struct spdk_nvmf_ns_opts *opts, size_t opts_size) 1323 { 1324 /* All current fields are set to 0 by default. */ 1325 memset(opts, 0, opts_size); 1326 } 1327 1328 /* Dummy bdev module used to to claim bdevs. */ 1329 static struct spdk_bdev_module ns_bdev_module = { 1330 .name = "NVMe-oF Target", 1331 }; 1332 1333 static int 1334 nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info); 1335 static int 1336 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info); 1337 1338 uint32_t 1339 spdk_nvmf_subsystem_add_ns_ext(struct spdk_nvmf_subsystem *subsystem, const char *bdev_name, 1340 const struct spdk_nvmf_ns_opts *user_opts, size_t opts_size, 1341 const char *ptpl_file) 1342 { 1343 struct spdk_nvmf_ns_opts opts; 1344 struct spdk_nvmf_ns *ns; 1345 struct spdk_nvmf_reservation_info info = {0}; 1346 int rc; 1347 1348 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 1349 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 1350 return 0; 1351 } 1352 1353 spdk_nvmf_ns_opts_get_defaults(&opts, sizeof(opts)); 1354 if (user_opts) { 1355 memcpy(&opts, user_opts, spdk_min(sizeof(opts), opts_size)); 1356 } 1357 1358 if (opts.nsid == SPDK_NVME_GLOBAL_NS_TAG) { 1359 SPDK_ERRLOG("Invalid NSID %" PRIu32 "\n", opts.nsid); 1360 return 0; 1361 } 1362 1363 if (opts.nsid == 0) { 1364 /* 1365 * NSID not specified - find a free index. 1366 * 1367 * If no free slots are found, opts.nsid will be subsystem->max_nsid + 1, which will 1368 * expand max_nsid if possible. 1369 */ 1370 for (opts.nsid = 1; opts.nsid <= subsystem->max_nsid; opts.nsid++) { 1371 if (_nvmf_subsystem_get_ns(subsystem, opts.nsid) == NULL) { 1372 break; 1373 } 1374 } 1375 } 1376 1377 if (_nvmf_subsystem_get_ns(subsystem, opts.nsid)) { 1378 SPDK_ERRLOG("Requested NSID %" PRIu32 " already in use\n", opts.nsid); 1379 return 0; 1380 } 1381 1382 if (opts.nsid > subsystem->max_nsid) { 1383 struct spdk_nvmf_ns **new_ns_array; 1384 1385 /* If MaxNamespaces was specified, we can't extend max_nsid beyond it. */ 1386 if (subsystem->max_allowed_nsid > 0 && opts.nsid > subsystem->max_allowed_nsid) { 1387 SPDK_ERRLOG("Can't extend NSID range above MaxNamespaces\n"); 1388 return 0; 1389 } 1390 1391 /* If a controller is connected, we can't change NN. */ 1392 if (!TAILQ_EMPTY(&subsystem->ctrlrs)) { 1393 SPDK_ERRLOG("Can't extend NSID range while controllers are connected\n"); 1394 return 0; 1395 } 1396 1397 new_ns_array = realloc(subsystem->ns, sizeof(struct spdk_nvmf_ns *) * opts.nsid); 1398 if (new_ns_array == NULL) { 1399 SPDK_ERRLOG("Memory allocation error while resizing namespace array.\n"); 1400 return 0; 1401 } 1402 1403 memset(new_ns_array + subsystem->max_nsid, 0, 1404 sizeof(struct spdk_nvmf_ns *) * (opts.nsid - subsystem->max_nsid)); 1405 subsystem->ns = new_ns_array; 1406 subsystem->max_nsid = opts.nsid; 1407 } 1408 1409 ns = calloc(1, sizeof(*ns)); 1410 if (ns == NULL) { 1411 SPDK_ERRLOG("Namespace allocation failed\n"); 1412 return 0; 1413 } 1414 1415 rc = spdk_bdev_open_ext(bdev_name, true, nvmf_ns_event, ns, &ns->desc); 1416 if (rc != 0) { 1417 SPDK_ERRLOG("Subsystem %s: bdev %s cannot be opened, error=%d\n", 1418 subsystem->subnqn, bdev_name, rc); 1419 free(ns); 1420 return 0; 1421 } 1422 1423 ns->bdev = spdk_bdev_desc_get_bdev(ns->desc); 1424 1425 if (spdk_bdev_get_md_size(ns->bdev) != 0 && !spdk_bdev_is_md_interleaved(ns->bdev)) { 1426 SPDK_ERRLOG("Can't attach bdev with separate metadata.\n"); 1427 spdk_bdev_close(ns->desc); 1428 free(ns); 1429 return 0; 1430 } 1431 1432 rc = spdk_bdev_module_claim_bdev(ns->bdev, ns->desc, &ns_bdev_module); 1433 if (rc != 0) { 1434 spdk_bdev_close(ns->desc); 1435 free(ns); 1436 return 0; 1437 } 1438 1439 if (spdk_mem_all_zero(&opts.uuid, sizeof(opts.uuid))) { 1440 opts.uuid = *spdk_bdev_get_uuid(ns->bdev); 1441 } 1442 1443 ns->opts = opts; 1444 ns->subsystem = subsystem; 1445 subsystem->ns[opts.nsid - 1] = ns; 1446 ns->nsid = opts.nsid; 1447 TAILQ_INIT(&ns->registrants); 1448 1449 if (ptpl_file) { 1450 rc = nvmf_ns_load_reservation(ptpl_file, &info); 1451 if (!rc) { 1452 rc = nvmf_ns_reservation_restore(ns, &info); 1453 if (rc) { 1454 SPDK_ERRLOG("Subsystem restore reservation failed\n"); 1455 subsystem->ns[opts.nsid - 1] = NULL; 1456 spdk_bdev_module_release_bdev(ns->bdev); 1457 spdk_bdev_close(ns->desc); 1458 free(ns); 1459 return 0; 1460 } 1461 } 1462 ns->ptpl_file = strdup(ptpl_file); 1463 } 1464 1465 SPDK_DEBUGLOG(nvmf, "Subsystem %s: bdev %s assigned nsid %" PRIu32 "\n", 1466 spdk_nvmf_subsystem_get_nqn(subsystem), 1467 bdev_name, 1468 opts.nsid); 1469 1470 nvmf_subsystem_ns_changed(subsystem, opts.nsid); 1471 1472 return opts.nsid; 1473 } 1474 1475 uint32_t 1476 spdk_nvmf_subsystem_add_ns(struct spdk_nvmf_subsystem *subsystem, struct spdk_bdev *bdev, 1477 const struct spdk_nvmf_ns_opts *user_opts, size_t opts_size, 1478 const char *ptpl_file) 1479 { 1480 return spdk_nvmf_subsystem_add_ns_ext(subsystem, spdk_bdev_get_name(bdev), 1481 user_opts, opts_size, ptpl_file); 1482 } 1483 1484 static uint32_t 1485 nvmf_subsystem_get_next_allocated_nsid(struct spdk_nvmf_subsystem *subsystem, 1486 uint32_t prev_nsid) 1487 { 1488 uint32_t nsid; 1489 1490 if (prev_nsid >= subsystem->max_nsid) { 1491 return 0; 1492 } 1493 1494 for (nsid = prev_nsid + 1; nsid <= subsystem->max_nsid; nsid++) { 1495 if (subsystem->ns[nsid - 1]) { 1496 return nsid; 1497 } 1498 } 1499 1500 return 0; 1501 } 1502 1503 struct spdk_nvmf_ns * 1504 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem) 1505 { 1506 uint32_t first_nsid; 1507 1508 first_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, 0); 1509 return _nvmf_subsystem_get_ns(subsystem, first_nsid); 1510 } 1511 1512 struct spdk_nvmf_ns * 1513 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem, 1514 struct spdk_nvmf_ns *prev_ns) 1515 { 1516 uint32_t next_nsid; 1517 1518 next_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, prev_ns->opts.nsid); 1519 return _nvmf_subsystem_get_ns(subsystem, next_nsid); 1520 } 1521 1522 struct spdk_nvmf_ns * 1523 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 1524 { 1525 return _nvmf_subsystem_get_ns(subsystem, nsid); 1526 } 1527 1528 uint32_t 1529 spdk_nvmf_ns_get_id(const struct spdk_nvmf_ns *ns) 1530 { 1531 return ns->opts.nsid; 1532 } 1533 1534 struct spdk_bdev * 1535 spdk_nvmf_ns_get_bdev(struct spdk_nvmf_ns *ns) 1536 { 1537 return ns->bdev; 1538 } 1539 1540 void 1541 spdk_nvmf_ns_get_opts(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_ns_opts *opts, 1542 size_t opts_size) 1543 { 1544 memset(opts, 0, opts_size); 1545 memcpy(opts, &ns->opts, spdk_min(sizeof(ns->opts), opts_size)); 1546 } 1547 1548 const char * 1549 spdk_nvmf_subsystem_get_sn(const struct spdk_nvmf_subsystem *subsystem) 1550 { 1551 return subsystem->sn; 1552 } 1553 1554 int 1555 spdk_nvmf_subsystem_set_sn(struct spdk_nvmf_subsystem *subsystem, const char *sn) 1556 { 1557 size_t len, max_len; 1558 1559 max_len = sizeof(subsystem->sn) - 1; 1560 len = strlen(sn); 1561 if (len > max_len) { 1562 SPDK_DEBUGLOG(nvmf, "Invalid sn \"%s\": length %zu > max %zu\n", 1563 sn, len, max_len); 1564 return -1; 1565 } 1566 1567 if (!nvmf_valid_ascii_string(sn, len)) { 1568 SPDK_DEBUGLOG(nvmf, "Non-ASCII sn\n"); 1569 SPDK_LOGDUMP(nvmf, "sn", sn, len); 1570 return -1; 1571 } 1572 1573 snprintf(subsystem->sn, sizeof(subsystem->sn), "%s", sn); 1574 1575 return 0; 1576 } 1577 1578 const char * 1579 spdk_nvmf_subsystem_get_mn(const struct spdk_nvmf_subsystem *subsystem) 1580 { 1581 return subsystem->mn; 1582 } 1583 1584 int 1585 spdk_nvmf_subsystem_set_mn(struct spdk_nvmf_subsystem *subsystem, const char *mn) 1586 { 1587 size_t len, max_len; 1588 1589 if (mn == NULL) { 1590 mn = MODEL_NUMBER_DEFAULT; 1591 } 1592 max_len = sizeof(subsystem->mn) - 1; 1593 len = strlen(mn); 1594 if (len > max_len) { 1595 SPDK_DEBUGLOG(nvmf, "Invalid mn \"%s\": length %zu > max %zu\n", 1596 mn, len, max_len); 1597 return -1; 1598 } 1599 1600 if (!nvmf_valid_ascii_string(mn, len)) { 1601 SPDK_DEBUGLOG(nvmf, "Non-ASCII mn\n"); 1602 SPDK_LOGDUMP(nvmf, "mn", mn, len); 1603 return -1; 1604 } 1605 1606 snprintf(subsystem->mn, sizeof(subsystem->mn), "%s", mn); 1607 1608 return 0; 1609 } 1610 1611 const char * 1612 spdk_nvmf_subsystem_get_nqn(const struct spdk_nvmf_subsystem *subsystem) 1613 { 1614 return subsystem->subnqn; 1615 } 1616 1617 enum spdk_nvmf_subtype spdk_nvmf_subsystem_get_type(struct spdk_nvmf_subsystem *subsystem) 1618 { 1619 return subsystem->subtype; 1620 } 1621 1622 uint32_t 1623 spdk_nvmf_subsystem_get_max_nsid(struct spdk_nvmf_subsystem *subsystem) 1624 { 1625 return subsystem->max_nsid; 1626 } 1627 1628 static uint16_t 1629 nvmf_subsystem_gen_cntlid(struct spdk_nvmf_subsystem *subsystem) 1630 { 1631 int count; 1632 1633 /* 1634 * In the worst case, we might have to try all CNTLID values between 1 and 0xFFF0 - 1 1635 * before we find one that is unused (or find that all values are in use). 1636 */ 1637 for (count = 0; count < 0xFFF0 - 1; count++) { 1638 subsystem->next_cntlid++; 1639 if (subsystem->next_cntlid >= 0xFFF0) { 1640 /* The spec reserves cntlid values in the range FFF0h to FFFFh. */ 1641 subsystem->next_cntlid = 1; 1642 } 1643 1644 /* Check if a controller with this cntlid currently exists. */ 1645 if (nvmf_subsystem_get_ctrlr(subsystem, subsystem->next_cntlid) == NULL) { 1646 /* Found unused cntlid */ 1647 return subsystem->next_cntlid; 1648 } 1649 } 1650 1651 /* All valid cntlid values are in use. */ 1652 return 0xFFFF; 1653 } 1654 1655 int 1656 nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ctrlr *ctrlr) 1657 { 1658 ctrlr->cntlid = nvmf_subsystem_gen_cntlid(subsystem); 1659 if (ctrlr->cntlid == 0xFFFF) { 1660 /* Unable to get a cntlid */ 1661 SPDK_ERRLOG("Reached max simultaneous ctrlrs\n"); 1662 return -EBUSY; 1663 } 1664 1665 TAILQ_INSERT_TAIL(&subsystem->ctrlrs, ctrlr, link); 1666 1667 return 0; 1668 } 1669 1670 void 1671 nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem, 1672 struct spdk_nvmf_ctrlr *ctrlr) 1673 { 1674 assert(subsystem == ctrlr->subsys); 1675 TAILQ_REMOVE(&subsystem->ctrlrs, ctrlr, link); 1676 } 1677 1678 struct spdk_nvmf_ctrlr * 1679 nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, uint16_t cntlid) 1680 { 1681 struct spdk_nvmf_ctrlr *ctrlr; 1682 1683 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 1684 if (ctrlr->cntlid == cntlid) { 1685 return ctrlr; 1686 } 1687 } 1688 1689 return NULL; 1690 } 1691 1692 uint32_t 1693 spdk_nvmf_subsystem_get_max_namespaces(const struct spdk_nvmf_subsystem *subsystem) 1694 { 1695 return subsystem->max_allowed_nsid; 1696 } 1697 1698 struct _nvmf_ns_registrant { 1699 uint64_t rkey; 1700 char *host_uuid; 1701 }; 1702 1703 struct _nvmf_ns_registrants { 1704 size_t num_regs; 1705 struct _nvmf_ns_registrant reg[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 1706 }; 1707 1708 struct _nvmf_ns_reservation { 1709 bool ptpl_activated; 1710 enum spdk_nvme_reservation_type rtype; 1711 uint64_t crkey; 1712 char *bdev_uuid; 1713 char *holder_uuid; 1714 struct _nvmf_ns_registrants regs; 1715 }; 1716 1717 static const struct spdk_json_object_decoder nvmf_ns_pr_reg_decoders[] = { 1718 {"rkey", offsetof(struct _nvmf_ns_registrant, rkey), spdk_json_decode_uint64}, 1719 {"host_uuid", offsetof(struct _nvmf_ns_registrant, host_uuid), spdk_json_decode_string}, 1720 }; 1721 1722 static int 1723 nvmf_decode_ns_pr_reg(const struct spdk_json_val *val, void *out) 1724 { 1725 struct _nvmf_ns_registrant *reg = out; 1726 1727 return spdk_json_decode_object(val, nvmf_ns_pr_reg_decoders, 1728 SPDK_COUNTOF(nvmf_ns_pr_reg_decoders), reg); 1729 } 1730 1731 static int 1732 nvmf_decode_ns_pr_regs(const struct spdk_json_val *val, void *out) 1733 { 1734 struct _nvmf_ns_registrants *regs = out; 1735 1736 return spdk_json_decode_array(val, nvmf_decode_ns_pr_reg, regs->reg, 1737 SPDK_NVMF_MAX_NUM_REGISTRANTS, ®s->num_regs, 1738 sizeof(struct _nvmf_ns_registrant)); 1739 } 1740 1741 static const struct spdk_json_object_decoder nvmf_ns_pr_decoders[] = { 1742 {"ptpl", offsetof(struct _nvmf_ns_reservation, ptpl_activated), spdk_json_decode_bool, true}, 1743 {"rtype", offsetof(struct _nvmf_ns_reservation, rtype), spdk_json_decode_uint32, true}, 1744 {"crkey", offsetof(struct _nvmf_ns_reservation, crkey), spdk_json_decode_uint64, true}, 1745 {"bdev_uuid", offsetof(struct _nvmf_ns_reservation, bdev_uuid), spdk_json_decode_string}, 1746 {"holder_uuid", offsetof(struct _nvmf_ns_reservation, holder_uuid), spdk_json_decode_string, true}, 1747 {"registrants", offsetof(struct _nvmf_ns_reservation, regs), nvmf_decode_ns_pr_regs}, 1748 }; 1749 1750 static int 1751 nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info) 1752 { 1753 FILE *fd; 1754 size_t json_size; 1755 ssize_t values_cnt, rc; 1756 void *json = NULL, *end; 1757 struct spdk_json_val *values = NULL; 1758 struct _nvmf_ns_reservation res = {}; 1759 uint32_t i; 1760 1761 fd = fopen(file, "r"); 1762 /* It's not an error if the file does not exist */ 1763 if (!fd) { 1764 SPDK_NOTICELOG("File %s does not exist\n", file); 1765 return -ENOENT; 1766 } 1767 1768 /* Load all persist file contents into a local buffer */ 1769 json = spdk_posix_file_load(fd, &json_size); 1770 fclose(fd); 1771 if (!json) { 1772 SPDK_ERRLOG("Load persit file %s failed\n", file); 1773 return -ENOMEM; 1774 } 1775 1776 rc = spdk_json_parse(json, json_size, NULL, 0, &end, 0); 1777 if (rc < 0) { 1778 SPDK_NOTICELOG("Parsing JSON configuration failed (%zd)\n", rc); 1779 goto exit; 1780 } 1781 1782 values_cnt = rc; 1783 values = calloc(values_cnt, sizeof(struct spdk_json_val)); 1784 if (values == NULL) { 1785 goto exit; 1786 } 1787 1788 rc = spdk_json_parse(json, json_size, values, values_cnt, &end, 0); 1789 if (rc != values_cnt) { 1790 SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc); 1791 goto exit; 1792 } 1793 1794 /* Decode json */ 1795 if (spdk_json_decode_object(values, nvmf_ns_pr_decoders, 1796 SPDK_COUNTOF(nvmf_ns_pr_decoders), 1797 &res)) { 1798 SPDK_ERRLOG("Invalid objects in the persist file %s\n", file); 1799 rc = -EINVAL; 1800 goto exit; 1801 } 1802 1803 if (res.regs.num_regs > SPDK_NVMF_MAX_NUM_REGISTRANTS) { 1804 SPDK_ERRLOG("Can only support up to %u registrants\n", SPDK_NVMF_MAX_NUM_REGISTRANTS); 1805 rc = -ERANGE; 1806 goto exit; 1807 } 1808 1809 rc = 0; 1810 info->ptpl_activated = res.ptpl_activated; 1811 info->rtype = res.rtype; 1812 info->crkey = res.crkey; 1813 snprintf(info->bdev_uuid, sizeof(info->bdev_uuid), "%s", res.bdev_uuid); 1814 snprintf(info->holder_uuid, sizeof(info->holder_uuid), "%s", res.holder_uuid); 1815 info->num_regs = res.regs.num_regs; 1816 for (i = 0; i < res.regs.num_regs; i++) { 1817 info->registrants[i].rkey = res.regs.reg[i].rkey; 1818 snprintf(info->registrants[i].host_uuid, sizeof(info->registrants[i].host_uuid), "%s", 1819 res.regs.reg[i].host_uuid); 1820 } 1821 1822 exit: 1823 free(json); 1824 free(values); 1825 free(res.bdev_uuid); 1826 free(res.holder_uuid); 1827 for (i = 0; i < res.regs.num_regs; i++) { 1828 free(res.regs.reg[i].host_uuid); 1829 } 1830 1831 return rc; 1832 } 1833 1834 static bool 1835 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns); 1836 1837 static int 1838 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info) 1839 { 1840 uint32_t i; 1841 struct spdk_nvmf_registrant *reg, *holder = NULL; 1842 struct spdk_uuid bdev_uuid, holder_uuid; 1843 1844 SPDK_DEBUGLOG(nvmf, "NSID %u, PTPL %u, Number of registrants %u\n", 1845 ns->nsid, info->ptpl_activated, info->num_regs); 1846 1847 /* it's not an error */ 1848 if (!info->ptpl_activated || !info->num_regs) { 1849 return 0; 1850 } 1851 1852 spdk_uuid_parse(&bdev_uuid, info->bdev_uuid); 1853 if (spdk_uuid_compare(&bdev_uuid, spdk_bdev_get_uuid(ns->bdev))) { 1854 SPDK_ERRLOG("Existing bdev UUID is not same with configuration file\n"); 1855 return -EINVAL; 1856 } 1857 1858 ns->crkey = info->crkey; 1859 ns->rtype = info->rtype; 1860 ns->ptpl_activated = info->ptpl_activated; 1861 spdk_uuid_parse(&holder_uuid, info->holder_uuid); 1862 1863 SPDK_DEBUGLOG(nvmf, "Bdev UUID %s\n", info->bdev_uuid); 1864 if (info->rtype) { 1865 SPDK_DEBUGLOG(nvmf, "Holder UUID %s, RTYPE %u, RKEY 0x%"PRIx64"\n", 1866 info->holder_uuid, info->rtype, info->crkey); 1867 } 1868 1869 for (i = 0; i < info->num_regs; i++) { 1870 reg = calloc(1, sizeof(*reg)); 1871 if (!reg) { 1872 return -ENOMEM; 1873 } 1874 spdk_uuid_parse(®->hostid, info->registrants[i].host_uuid); 1875 reg->rkey = info->registrants[i].rkey; 1876 TAILQ_INSERT_TAIL(&ns->registrants, reg, link); 1877 if (!spdk_uuid_compare(&holder_uuid, ®->hostid)) { 1878 holder = reg; 1879 } 1880 SPDK_DEBUGLOG(nvmf, "Registrant RKEY 0x%"PRIx64", Host UUID %s\n", 1881 info->registrants[i].rkey, info->registrants[i].host_uuid); 1882 } 1883 1884 if (nvmf_ns_reservation_all_registrants_type(ns)) { 1885 ns->holder = TAILQ_FIRST(&ns->registrants); 1886 } else { 1887 ns->holder = holder; 1888 } 1889 1890 return 0; 1891 } 1892 1893 static int 1894 nvmf_ns_json_write_cb(void *cb_ctx, const void *data, size_t size) 1895 { 1896 char *file = cb_ctx; 1897 size_t rc; 1898 FILE *fd; 1899 1900 fd = fopen(file, "w"); 1901 if (!fd) { 1902 SPDK_ERRLOG("Can't open file %s for write\n", file); 1903 return -ENOENT; 1904 } 1905 rc = fwrite(data, 1, size, fd); 1906 fclose(fd); 1907 1908 return rc == size ? 0 : -1; 1909 } 1910 1911 static int 1912 nvmf_ns_reservation_update(const char *file, struct spdk_nvmf_reservation_info *info) 1913 { 1914 struct spdk_json_write_ctx *w; 1915 uint32_t i; 1916 int rc = 0; 1917 1918 w = spdk_json_write_begin(nvmf_ns_json_write_cb, (void *)file, 0); 1919 if (w == NULL) { 1920 return -ENOMEM; 1921 } 1922 /* clear the configuration file */ 1923 if (!info->ptpl_activated) { 1924 goto exit; 1925 } 1926 1927 spdk_json_write_object_begin(w); 1928 spdk_json_write_named_bool(w, "ptpl", info->ptpl_activated); 1929 spdk_json_write_named_uint32(w, "rtype", info->rtype); 1930 spdk_json_write_named_uint64(w, "crkey", info->crkey); 1931 spdk_json_write_named_string(w, "bdev_uuid", info->bdev_uuid); 1932 spdk_json_write_named_string(w, "holder_uuid", info->holder_uuid); 1933 1934 spdk_json_write_named_array_begin(w, "registrants"); 1935 for (i = 0; i < info->num_regs; i++) { 1936 spdk_json_write_object_begin(w); 1937 spdk_json_write_named_uint64(w, "rkey", info->registrants[i].rkey); 1938 spdk_json_write_named_string(w, "host_uuid", info->registrants[i].host_uuid); 1939 spdk_json_write_object_end(w); 1940 } 1941 spdk_json_write_array_end(w); 1942 spdk_json_write_object_end(w); 1943 1944 exit: 1945 rc = spdk_json_write_end(w); 1946 return rc; 1947 } 1948 1949 static int 1950 nvmf_ns_update_reservation_info(struct spdk_nvmf_ns *ns) 1951 { 1952 struct spdk_nvmf_reservation_info info; 1953 struct spdk_nvmf_registrant *reg, *tmp; 1954 uint32_t i = 0; 1955 1956 assert(ns != NULL); 1957 1958 if (!ns->bdev || !ns->ptpl_file) { 1959 return 0; 1960 } 1961 1962 memset(&info, 0, sizeof(info)); 1963 spdk_uuid_fmt_lower(info.bdev_uuid, sizeof(info.bdev_uuid), spdk_bdev_get_uuid(ns->bdev)); 1964 1965 if (ns->rtype) { 1966 info.rtype = ns->rtype; 1967 info.crkey = ns->crkey; 1968 if (!nvmf_ns_reservation_all_registrants_type(ns)) { 1969 assert(ns->holder != NULL); 1970 spdk_uuid_fmt_lower(info.holder_uuid, sizeof(info.holder_uuid), &ns->holder->hostid); 1971 } 1972 } 1973 1974 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 1975 spdk_uuid_fmt_lower(info.registrants[i].host_uuid, sizeof(info.registrants[i].host_uuid), 1976 ®->hostid); 1977 info.registrants[i++].rkey = reg->rkey; 1978 } 1979 1980 info.num_regs = i; 1981 info.ptpl_activated = ns->ptpl_activated; 1982 1983 return nvmf_ns_reservation_update(ns->ptpl_file, &info); 1984 } 1985 1986 static struct spdk_nvmf_registrant * 1987 nvmf_ns_reservation_get_registrant(struct spdk_nvmf_ns *ns, 1988 struct spdk_uuid *uuid) 1989 { 1990 struct spdk_nvmf_registrant *reg, *tmp; 1991 1992 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 1993 if (!spdk_uuid_compare(®->hostid, uuid)) { 1994 return reg; 1995 } 1996 } 1997 1998 return NULL; 1999 } 2000 2001 /* Generate reservation notice log to registered HostID controllers */ 2002 static void 2003 nvmf_subsystem_gen_ctrlr_notification(struct spdk_nvmf_subsystem *subsystem, 2004 struct spdk_nvmf_ns *ns, 2005 struct spdk_uuid *hostid_list, 2006 uint32_t num_hostid, 2007 enum spdk_nvme_reservation_notification_log_page_type type) 2008 { 2009 struct spdk_nvmf_ctrlr *ctrlr; 2010 uint32_t i; 2011 2012 for (i = 0; i < num_hostid; i++) { 2013 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 2014 if (!spdk_uuid_compare(&ctrlr->hostid, &hostid_list[i])) { 2015 nvmf_ctrlr_reservation_notice_log(ctrlr, ns, type); 2016 } 2017 } 2018 } 2019 } 2020 2021 /* Get all registrants' hostid other than the controller who issued the command */ 2022 static uint32_t 2023 nvmf_ns_reservation_get_all_other_hostid(struct spdk_nvmf_ns *ns, 2024 struct spdk_uuid *hostid_list, 2025 uint32_t max_num_hostid, 2026 struct spdk_uuid *current_hostid) 2027 { 2028 struct spdk_nvmf_registrant *reg, *tmp; 2029 uint32_t num_hostid = 0; 2030 2031 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 2032 if (spdk_uuid_compare(®->hostid, current_hostid)) { 2033 if (num_hostid == max_num_hostid) { 2034 assert(false); 2035 return max_num_hostid; 2036 } 2037 hostid_list[num_hostid++] = reg->hostid; 2038 } 2039 } 2040 2041 return num_hostid; 2042 } 2043 2044 /* Calculate the unregistered HostID list according to list 2045 * prior to execute preempt command and list after executing 2046 * preempt command. 2047 */ 2048 static uint32_t 2049 nvmf_ns_reservation_get_unregistered_hostid(struct spdk_uuid *old_hostid_list, 2050 uint32_t old_num_hostid, 2051 struct spdk_uuid *remaining_hostid_list, 2052 uint32_t remaining_num_hostid) 2053 { 2054 struct spdk_uuid temp_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2055 uint32_t i, j, num_hostid = 0; 2056 bool found; 2057 2058 if (!remaining_num_hostid) { 2059 return old_num_hostid; 2060 } 2061 2062 for (i = 0; i < old_num_hostid; i++) { 2063 found = false; 2064 for (j = 0; j < remaining_num_hostid; j++) { 2065 if (!spdk_uuid_compare(&old_hostid_list[i], &remaining_hostid_list[j])) { 2066 found = true; 2067 break; 2068 } 2069 } 2070 if (!found) { 2071 spdk_uuid_copy(&temp_hostid_list[num_hostid++], &old_hostid_list[i]); 2072 } 2073 } 2074 2075 if (num_hostid) { 2076 memcpy(old_hostid_list, temp_hostid_list, sizeof(struct spdk_uuid) * num_hostid); 2077 } 2078 2079 return num_hostid; 2080 } 2081 2082 /* current reservation type is all registrants or not */ 2083 static bool 2084 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns) 2085 { 2086 return (ns->rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS || 2087 ns->rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS); 2088 } 2089 2090 /* current registrant is reservation holder or not */ 2091 static bool 2092 nvmf_ns_reservation_registrant_is_holder(struct spdk_nvmf_ns *ns, 2093 struct spdk_nvmf_registrant *reg) 2094 { 2095 if (!reg) { 2096 return false; 2097 } 2098 2099 if (nvmf_ns_reservation_all_registrants_type(ns)) { 2100 return true; 2101 } 2102 2103 return (ns->holder == reg); 2104 } 2105 2106 static int 2107 nvmf_ns_reservation_add_registrant(struct spdk_nvmf_ns *ns, 2108 struct spdk_nvmf_ctrlr *ctrlr, 2109 uint64_t nrkey) 2110 { 2111 struct spdk_nvmf_registrant *reg; 2112 2113 reg = calloc(1, sizeof(*reg)); 2114 if (!reg) { 2115 return -ENOMEM; 2116 } 2117 2118 reg->rkey = nrkey; 2119 /* set hostid for the registrant */ 2120 spdk_uuid_copy(®->hostid, &ctrlr->hostid); 2121 TAILQ_INSERT_TAIL(&ns->registrants, reg, link); 2122 ns->gen++; 2123 2124 return 0; 2125 } 2126 2127 static void 2128 nvmf_ns_reservation_release_reservation(struct spdk_nvmf_ns *ns) 2129 { 2130 ns->rtype = 0; 2131 ns->crkey = 0; 2132 ns->holder = NULL; 2133 } 2134 2135 /* release the reservation if the last registrant was removed */ 2136 static void 2137 nvmf_ns_reservation_check_release_on_remove_registrant(struct spdk_nvmf_ns *ns, 2138 struct spdk_nvmf_registrant *reg) 2139 { 2140 struct spdk_nvmf_registrant *next_reg; 2141 2142 /* no reservation holder */ 2143 if (!ns->holder) { 2144 assert(ns->rtype == 0); 2145 return; 2146 } 2147 2148 next_reg = TAILQ_FIRST(&ns->registrants); 2149 if (next_reg && nvmf_ns_reservation_all_registrants_type(ns)) { 2150 /* the next valid registrant is the new holder now */ 2151 ns->holder = next_reg; 2152 } else if (nvmf_ns_reservation_registrant_is_holder(ns, reg)) { 2153 /* release the reservation */ 2154 nvmf_ns_reservation_release_reservation(ns); 2155 } 2156 } 2157 2158 static void 2159 nvmf_ns_reservation_remove_registrant(struct spdk_nvmf_ns *ns, 2160 struct spdk_nvmf_registrant *reg) 2161 { 2162 TAILQ_REMOVE(&ns->registrants, reg, link); 2163 nvmf_ns_reservation_check_release_on_remove_registrant(ns, reg); 2164 free(reg); 2165 ns->gen++; 2166 return; 2167 } 2168 2169 static uint32_t 2170 nvmf_ns_reservation_remove_registrants_by_key(struct spdk_nvmf_ns *ns, 2171 uint64_t rkey) 2172 { 2173 struct spdk_nvmf_registrant *reg, *tmp; 2174 uint32_t count = 0; 2175 2176 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 2177 if (reg->rkey == rkey) { 2178 nvmf_ns_reservation_remove_registrant(ns, reg); 2179 count++; 2180 } 2181 } 2182 return count; 2183 } 2184 2185 static uint32_t 2186 nvmf_ns_reservation_remove_all_other_registrants(struct spdk_nvmf_ns *ns, 2187 struct spdk_nvmf_registrant *reg) 2188 { 2189 struct spdk_nvmf_registrant *reg_tmp, *reg_tmp2; 2190 uint32_t count = 0; 2191 2192 TAILQ_FOREACH_SAFE(reg_tmp, &ns->registrants, link, reg_tmp2) { 2193 if (reg_tmp != reg) { 2194 nvmf_ns_reservation_remove_registrant(ns, reg_tmp); 2195 count++; 2196 } 2197 } 2198 return count; 2199 } 2200 2201 static uint32_t 2202 nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns) 2203 { 2204 struct spdk_nvmf_registrant *reg, *reg_tmp; 2205 uint32_t count = 0; 2206 2207 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) { 2208 nvmf_ns_reservation_remove_registrant(ns, reg); 2209 count++; 2210 } 2211 return count; 2212 } 2213 2214 static void 2215 nvmf_ns_reservation_acquire_reservation(struct spdk_nvmf_ns *ns, uint64_t rkey, 2216 enum spdk_nvme_reservation_type rtype, 2217 struct spdk_nvmf_registrant *holder) 2218 { 2219 ns->rtype = rtype; 2220 ns->crkey = rkey; 2221 assert(ns->holder == NULL); 2222 ns->holder = holder; 2223 } 2224 2225 static bool 2226 nvmf_ns_reservation_register(struct spdk_nvmf_ns *ns, 2227 struct spdk_nvmf_ctrlr *ctrlr, 2228 struct spdk_nvmf_request *req) 2229 { 2230 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2231 uint8_t rrega, iekey, cptpl, rtype; 2232 struct spdk_nvme_reservation_register_data key; 2233 struct spdk_nvmf_registrant *reg; 2234 uint8_t status = SPDK_NVME_SC_SUCCESS; 2235 bool update_sgroup = false; 2236 struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2237 uint32_t num_hostid = 0; 2238 int rc; 2239 2240 rrega = cmd->cdw10_bits.resv_register.rrega; 2241 iekey = cmd->cdw10_bits.resv_register.iekey; 2242 cptpl = cmd->cdw10_bits.resv_register.cptpl; 2243 2244 if (req->data && req->length >= sizeof(key)) { 2245 memcpy(&key, req->data, sizeof(key)); 2246 } else { 2247 SPDK_ERRLOG("No key provided. Failing request.\n"); 2248 status = SPDK_NVME_SC_INVALID_FIELD; 2249 goto exit; 2250 } 2251 2252 SPDK_DEBUGLOG(nvmf, "REGISTER: RREGA %u, IEKEY %u, CPTPL %u, " 2253 "NRKEY 0x%"PRIx64", NRKEY 0x%"PRIx64"\n", 2254 rrega, iekey, cptpl, key.crkey, key.nrkey); 2255 2256 if (cptpl == SPDK_NVME_RESERVE_PTPL_CLEAR_POWER_ON) { 2257 /* Ture to OFF state, and need to be updated in the configuration file */ 2258 if (ns->ptpl_activated) { 2259 ns->ptpl_activated = 0; 2260 update_sgroup = true; 2261 } 2262 } else if (cptpl == SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS) { 2263 if (ns->ptpl_file == NULL) { 2264 status = SPDK_NVME_SC_INVALID_FIELD; 2265 goto exit; 2266 } else if (ns->ptpl_activated == 0) { 2267 ns->ptpl_activated = 1; 2268 update_sgroup = true; 2269 } 2270 } 2271 2272 /* current Host Identifier has registrant or not */ 2273 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid); 2274 2275 switch (rrega) { 2276 case SPDK_NVME_RESERVE_REGISTER_KEY: 2277 if (!reg) { 2278 /* register new controller */ 2279 if (key.nrkey == 0) { 2280 SPDK_ERRLOG("Can't register zeroed new key\n"); 2281 status = SPDK_NVME_SC_INVALID_FIELD; 2282 goto exit; 2283 } 2284 rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey); 2285 if (rc < 0) { 2286 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2287 goto exit; 2288 } 2289 update_sgroup = true; 2290 } else { 2291 /* register with same key is not an error */ 2292 if (reg->rkey != key.nrkey) { 2293 SPDK_ERRLOG("The same host already register a " 2294 "key with 0x%"PRIx64"\n", 2295 reg->rkey); 2296 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2297 goto exit; 2298 } 2299 } 2300 break; 2301 case SPDK_NVME_RESERVE_UNREGISTER_KEY: 2302 if (!reg || (!iekey && reg->rkey != key.crkey)) { 2303 SPDK_ERRLOG("No registrant or current key doesn't match " 2304 "with existing registrant key\n"); 2305 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2306 goto exit; 2307 } 2308 2309 rtype = ns->rtype; 2310 num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list, 2311 SPDK_NVMF_MAX_NUM_REGISTRANTS, 2312 &ctrlr->hostid); 2313 2314 nvmf_ns_reservation_remove_registrant(ns, reg); 2315 2316 if (!ns->rtype && num_hostid && (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY || 2317 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY)) { 2318 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2319 hostid_list, 2320 num_hostid, 2321 SPDK_NVME_RESERVATION_RELEASED); 2322 } 2323 update_sgroup = true; 2324 break; 2325 case SPDK_NVME_RESERVE_REPLACE_KEY: 2326 if (!reg || (!iekey && reg->rkey != key.crkey)) { 2327 SPDK_ERRLOG("No registrant or current key doesn't match " 2328 "with existing registrant key\n"); 2329 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2330 goto exit; 2331 } 2332 if (key.nrkey == 0) { 2333 SPDK_ERRLOG("Can't register zeroed new key\n"); 2334 status = SPDK_NVME_SC_INVALID_FIELD; 2335 goto exit; 2336 } 2337 reg->rkey = key.nrkey; 2338 update_sgroup = true; 2339 break; 2340 default: 2341 status = SPDK_NVME_SC_INVALID_FIELD; 2342 goto exit; 2343 } 2344 2345 exit: 2346 if (update_sgroup) { 2347 rc = nvmf_ns_update_reservation_info(ns); 2348 if (rc != 0) { 2349 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2350 } 2351 } 2352 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2353 req->rsp->nvme_cpl.status.sc = status; 2354 return update_sgroup; 2355 } 2356 2357 static bool 2358 nvmf_ns_reservation_acquire(struct spdk_nvmf_ns *ns, 2359 struct spdk_nvmf_ctrlr *ctrlr, 2360 struct spdk_nvmf_request *req) 2361 { 2362 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2363 uint8_t racqa, iekey, rtype; 2364 struct spdk_nvme_reservation_acquire_data key; 2365 struct spdk_nvmf_registrant *reg; 2366 bool all_regs = false; 2367 uint32_t count = 0; 2368 bool update_sgroup = true; 2369 struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2370 uint32_t num_hostid = 0; 2371 struct spdk_uuid new_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2372 uint32_t new_num_hostid = 0; 2373 bool reservation_released = false; 2374 uint8_t status = SPDK_NVME_SC_SUCCESS; 2375 2376 racqa = cmd->cdw10_bits.resv_acquire.racqa; 2377 iekey = cmd->cdw10_bits.resv_acquire.iekey; 2378 rtype = cmd->cdw10_bits.resv_acquire.rtype; 2379 2380 if (req->data && req->length >= sizeof(key)) { 2381 memcpy(&key, req->data, sizeof(key)); 2382 } else { 2383 SPDK_ERRLOG("No key provided. Failing request.\n"); 2384 status = SPDK_NVME_SC_INVALID_FIELD; 2385 goto exit; 2386 } 2387 2388 SPDK_DEBUGLOG(nvmf, "ACQUIRE: RACQA %u, IEKEY %u, RTYPE %u, " 2389 "NRKEY 0x%"PRIx64", PRKEY 0x%"PRIx64"\n", 2390 racqa, iekey, rtype, key.crkey, key.prkey); 2391 2392 if (iekey || rtype > SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) { 2393 SPDK_ERRLOG("Ignore existing key field set to 1\n"); 2394 status = SPDK_NVME_SC_INVALID_FIELD; 2395 update_sgroup = false; 2396 goto exit; 2397 } 2398 2399 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid); 2400 /* must be registrant and CRKEY must match */ 2401 if (!reg || reg->rkey != key.crkey) { 2402 SPDK_ERRLOG("No registrant or current key doesn't match " 2403 "with existing registrant key\n"); 2404 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2405 update_sgroup = false; 2406 goto exit; 2407 } 2408 2409 all_regs = nvmf_ns_reservation_all_registrants_type(ns); 2410 2411 switch (racqa) { 2412 case SPDK_NVME_RESERVE_ACQUIRE: 2413 /* it's not an error for the holder to acquire same reservation type again */ 2414 if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && ns->rtype == rtype) { 2415 /* do nothing */ 2416 update_sgroup = false; 2417 } else if (ns->holder == NULL) { 2418 /* fisrt time to acquire the reservation */ 2419 nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg); 2420 } else { 2421 SPDK_ERRLOG("Invalid rtype or current registrant is not holder\n"); 2422 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2423 update_sgroup = false; 2424 goto exit; 2425 } 2426 break; 2427 case SPDK_NVME_RESERVE_PREEMPT: 2428 /* no reservation holder */ 2429 if (!ns->holder) { 2430 /* unregister with PRKEY */ 2431 nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey); 2432 break; 2433 } 2434 num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list, 2435 SPDK_NVMF_MAX_NUM_REGISTRANTS, 2436 &ctrlr->hostid); 2437 2438 /* only 1 reservation holder and reservation key is valid */ 2439 if (!all_regs) { 2440 /* preempt itself */ 2441 if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && 2442 ns->crkey == key.prkey) { 2443 ns->rtype = rtype; 2444 reservation_released = true; 2445 break; 2446 } 2447 2448 if (ns->crkey == key.prkey) { 2449 nvmf_ns_reservation_remove_registrant(ns, ns->holder); 2450 nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg); 2451 reservation_released = true; 2452 } else if (key.prkey != 0) { 2453 nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey); 2454 } else { 2455 /* PRKEY is zero */ 2456 SPDK_ERRLOG("Current PRKEY is zero\n"); 2457 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2458 update_sgroup = false; 2459 goto exit; 2460 } 2461 } else { 2462 /* release all other registrants except for the current one */ 2463 if (key.prkey == 0) { 2464 nvmf_ns_reservation_remove_all_other_registrants(ns, reg); 2465 assert(ns->holder == reg); 2466 } else { 2467 count = nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey); 2468 if (count == 0) { 2469 SPDK_ERRLOG("PRKEY doesn't match any registrant\n"); 2470 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2471 update_sgroup = false; 2472 goto exit; 2473 } 2474 } 2475 } 2476 break; 2477 default: 2478 status = SPDK_NVME_SC_INVALID_FIELD; 2479 update_sgroup = false; 2480 break; 2481 } 2482 2483 exit: 2484 if (update_sgroup && racqa == SPDK_NVME_RESERVE_PREEMPT) { 2485 new_num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, new_hostid_list, 2486 SPDK_NVMF_MAX_NUM_REGISTRANTS, 2487 &ctrlr->hostid); 2488 /* Preempt notification occurs on the unregistered controllers 2489 * other than the controller who issued the command. 2490 */ 2491 num_hostid = nvmf_ns_reservation_get_unregistered_hostid(hostid_list, 2492 num_hostid, 2493 new_hostid_list, 2494 new_num_hostid); 2495 if (num_hostid) { 2496 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2497 hostid_list, 2498 num_hostid, 2499 SPDK_NVME_REGISTRATION_PREEMPTED); 2500 2501 } 2502 /* Reservation released notification occurs on the 2503 * controllers which are the remaining registrants other than 2504 * the controller who issued the command. 2505 */ 2506 if (reservation_released && new_num_hostid) { 2507 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2508 new_hostid_list, 2509 new_num_hostid, 2510 SPDK_NVME_RESERVATION_RELEASED); 2511 2512 } 2513 } 2514 if (update_sgroup && ns->ptpl_activated) { 2515 if (nvmf_ns_update_reservation_info(ns)) { 2516 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2517 } 2518 } 2519 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2520 req->rsp->nvme_cpl.status.sc = status; 2521 return update_sgroup; 2522 } 2523 2524 static bool 2525 nvmf_ns_reservation_release(struct spdk_nvmf_ns *ns, 2526 struct spdk_nvmf_ctrlr *ctrlr, 2527 struct spdk_nvmf_request *req) 2528 { 2529 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2530 uint8_t rrela, iekey, rtype; 2531 struct spdk_nvmf_registrant *reg; 2532 uint64_t crkey; 2533 uint8_t status = SPDK_NVME_SC_SUCCESS; 2534 bool update_sgroup = true; 2535 struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2536 uint32_t num_hostid = 0; 2537 2538 rrela = cmd->cdw10_bits.resv_release.rrela; 2539 iekey = cmd->cdw10_bits.resv_release.iekey; 2540 rtype = cmd->cdw10_bits.resv_release.rtype; 2541 2542 if (req->data && req->length >= sizeof(crkey)) { 2543 memcpy(&crkey, req->data, sizeof(crkey)); 2544 } else { 2545 SPDK_ERRLOG("No key provided. Failing request.\n"); 2546 status = SPDK_NVME_SC_INVALID_FIELD; 2547 goto exit; 2548 } 2549 2550 SPDK_DEBUGLOG(nvmf, "RELEASE: RRELA %u, IEKEY %u, RTYPE %u, " 2551 "CRKEY 0x%"PRIx64"\n", rrela, iekey, rtype, crkey); 2552 2553 if (iekey) { 2554 SPDK_ERRLOG("Ignore existing key field set to 1\n"); 2555 status = SPDK_NVME_SC_INVALID_FIELD; 2556 update_sgroup = false; 2557 goto exit; 2558 } 2559 2560 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid); 2561 if (!reg || reg->rkey != crkey) { 2562 SPDK_ERRLOG("No registrant or current key doesn't match " 2563 "with existing registrant key\n"); 2564 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2565 update_sgroup = false; 2566 goto exit; 2567 } 2568 2569 num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list, 2570 SPDK_NVMF_MAX_NUM_REGISTRANTS, 2571 &ctrlr->hostid); 2572 2573 switch (rrela) { 2574 case SPDK_NVME_RESERVE_RELEASE: 2575 if (!ns->holder) { 2576 SPDK_DEBUGLOG(nvmf, "RELEASE: no holder\n"); 2577 update_sgroup = false; 2578 goto exit; 2579 } 2580 if (ns->rtype != rtype) { 2581 SPDK_ERRLOG("Type doesn't match\n"); 2582 status = SPDK_NVME_SC_INVALID_FIELD; 2583 update_sgroup = false; 2584 goto exit; 2585 } 2586 if (!nvmf_ns_reservation_registrant_is_holder(ns, reg)) { 2587 /* not the reservation holder, this isn't an error */ 2588 update_sgroup = false; 2589 goto exit; 2590 } 2591 2592 rtype = ns->rtype; 2593 nvmf_ns_reservation_release_reservation(ns); 2594 2595 if (num_hostid && rtype != SPDK_NVME_RESERVE_WRITE_EXCLUSIVE && 2596 rtype != SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 2597 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2598 hostid_list, 2599 num_hostid, 2600 SPDK_NVME_RESERVATION_RELEASED); 2601 } 2602 break; 2603 case SPDK_NVME_RESERVE_CLEAR: 2604 nvmf_ns_reservation_clear_all_registrants(ns); 2605 if (num_hostid) { 2606 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2607 hostid_list, 2608 num_hostid, 2609 SPDK_NVME_RESERVATION_PREEMPTED); 2610 } 2611 break; 2612 default: 2613 status = SPDK_NVME_SC_INVALID_FIELD; 2614 update_sgroup = false; 2615 goto exit; 2616 } 2617 2618 exit: 2619 if (update_sgroup && ns->ptpl_activated) { 2620 if (nvmf_ns_update_reservation_info(ns)) { 2621 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2622 } 2623 } 2624 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2625 req->rsp->nvme_cpl.status.sc = status; 2626 return update_sgroup; 2627 } 2628 2629 static void 2630 nvmf_ns_reservation_report(struct spdk_nvmf_ns *ns, 2631 struct spdk_nvmf_ctrlr *ctrlr, 2632 struct spdk_nvmf_request *req) 2633 { 2634 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2635 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2636 struct spdk_nvmf_ctrlr *ctrlr_tmp; 2637 struct spdk_nvmf_registrant *reg, *tmp; 2638 struct spdk_nvme_reservation_status_extended_data *status_data; 2639 struct spdk_nvme_registered_ctrlr_extended_data *ctrlr_data; 2640 uint8_t *payload; 2641 uint32_t len, count = 0; 2642 uint32_t regctl = 0; 2643 uint8_t status = SPDK_NVME_SC_SUCCESS; 2644 2645 if (req->data == NULL) { 2646 SPDK_ERRLOG("No data transfer specified for request. " 2647 " Unable to transfer back response.\n"); 2648 status = SPDK_NVME_SC_INVALID_FIELD; 2649 goto exit; 2650 } 2651 2652 if (!cmd->cdw11_bits.resv_report.eds) { 2653 SPDK_ERRLOG("NVMeoF uses extended controller data structure, " 2654 "please set EDS bit in cdw11 and try again\n"); 2655 status = SPDK_NVME_SC_HOSTID_INCONSISTENT_FORMAT; 2656 goto exit; 2657 } 2658 2659 /* Get number of registerd controllers, one Host may have more than 2660 * one controller based on different ports. 2661 */ 2662 TAILQ_FOREACH(ctrlr_tmp, &subsystem->ctrlrs, link) { 2663 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr_tmp->hostid); 2664 if (reg) { 2665 regctl++; 2666 } 2667 } 2668 2669 len = sizeof(*status_data) + sizeof(*ctrlr_data) * regctl; 2670 payload = calloc(1, len); 2671 if (!payload) { 2672 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2673 goto exit; 2674 } 2675 2676 status_data = (struct spdk_nvme_reservation_status_extended_data *)payload; 2677 status_data->data.gen = ns->gen; 2678 status_data->data.rtype = ns->rtype; 2679 status_data->data.regctl = regctl; 2680 status_data->data.ptpls = ns->ptpl_activated; 2681 2682 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 2683 assert(count <= regctl); 2684 ctrlr_data = (struct spdk_nvme_registered_ctrlr_extended_data *) 2685 (payload + sizeof(*status_data) + sizeof(*ctrlr_data) * count); 2686 /* Set to 0xffffh for dynamic controller */ 2687 ctrlr_data->cntlid = 0xffff; 2688 ctrlr_data->rcsts.status = (ns->holder == reg) ? true : false; 2689 ctrlr_data->rkey = reg->rkey; 2690 spdk_uuid_copy((struct spdk_uuid *)ctrlr_data->hostid, ®->hostid); 2691 count++; 2692 } 2693 2694 memcpy(req->data, payload, spdk_min(len, (cmd->cdw10 + 1) * sizeof(uint32_t))); 2695 free(payload); 2696 2697 exit: 2698 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2699 req->rsp->nvme_cpl.status.sc = status; 2700 return; 2701 } 2702 2703 static void 2704 nvmf_ns_reservation_complete(void *ctx) 2705 { 2706 struct spdk_nvmf_request *req = ctx; 2707 2708 spdk_nvmf_request_complete(req); 2709 } 2710 2711 static void 2712 _nvmf_ns_reservation_update_done(struct spdk_nvmf_subsystem *subsystem, 2713 void *cb_arg, int status) 2714 { 2715 struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)cb_arg; 2716 struct spdk_nvmf_poll_group *group = req->qpair->group; 2717 2718 spdk_thread_send_msg(group->thread, nvmf_ns_reservation_complete, req); 2719 } 2720 2721 void 2722 nvmf_ns_reservation_request(void *ctx) 2723 { 2724 struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)ctx; 2725 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2726 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2727 struct subsystem_update_ns_ctx *update_ctx; 2728 uint32_t nsid; 2729 struct spdk_nvmf_ns *ns; 2730 bool update_sgroup = false; 2731 2732 nsid = cmd->nsid; 2733 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 2734 assert(ns != NULL); 2735 2736 switch (cmd->opc) { 2737 case SPDK_NVME_OPC_RESERVATION_REGISTER: 2738 update_sgroup = nvmf_ns_reservation_register(ns, ctrlr, req); 2739 break; 2740 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 2741 update_sgroup = nvmf_ns_reservation_acquire(ns, ctrlr, req); 2742 break; 2743 case SPDK_NVME_OPC_RESERVATION_RELEASE: 2744 update_sgroup = nvmf_ns_reservation_release(ns, ctrlr, req); 2745 break; 2746 case SPDK_NVME_OPC_RESERVATION_REPORT: 2747 nvmf_ns_reservation_report(ns, ctrlr, req); 2748 break; 2749 default: 2750 break; 2751 } 2752 2753 /* update reservation information to subsystem's poll group */ 2754 if (update_sgroup) { 2755 update_ctx = calloc(1, sizeof(*update_ctx)); 2756 if (update_ctx == NULL) { 2757 SPDK_ERRLOG("Can't alloc subsystem poll group update context\n"); 2758 goto update_done; 2759 } 2760 update_ctx->subsystem = ctrlr->subsys; 2761 update_ctx->cb_fn = _nvmf_ns_reservation_update_done; 2762 update_ctx->cb_arg = req; 2763 2764 nvmf_subsystem_update_ns(ctrlr->subsys, subsystem_update_ns_done, update_ctx); 2765 return; 2766 } 2767 2768 update_done: 2769 _nvmf_ns_reservation_update_done(ctrlr->subsys, (void *)req, 0); 2770 } 2771 2772 int 2773 spdk_nvmf_subsystem_set_ana_reporting(struct spdk_nvmf_subsystem *subsystem, 2774 bool ana_reporting) 2775 { 2776 if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) { 2777 return -EAGAIN; 2778 } 2779 2780 subsystem->flags.ana_reporting = ana_reporting; 2781 2782 return 0; 2783 } 2784 2785 struct subsystem_listener_update_ctx { 2786 struct spdk_nvmf_subsystem_listener *listener; 2787 2788 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn; 2789 void *cb_arg; 2790 }; 2791 2792 static void 2793 subsystem_listener_update_done(struct spdk_io_channel_iter *i, int status) 2794 { 2795 struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 2796 2797 if (ctx->cb_fn) { 2798 ctx->cb_fn(ctx->cb_arg, status); 2799 } 2800 free(ctx); 2801 } 2802 2803 static void 2804 subsystem_listener_update_on_pg(struct spdk_io_channel_iter *i) 2805 { 2806 struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 2807 struct spdk_nvmf_subsystem_listener *listener; 2808 struct spdk_nvmf_poll_group *group; 2809 struct spdk_nvmf_ctrlr *ctrlr; 2810 2811 listener = ctx->listener; 2812 group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i)); 2813 2814 TAILQ_FOREACH(ctrlr, &listener->subsystem->ctrlrs, link) { 2815 if (ctrlr->admin_qpair->group == group && ctrlr->listener == listener) { 2816 nvmf_ctrlr_async_event_ana_change_notice(ctrlr); 2817 } 2818 } 2819 2820 spdk_for_each_channel_continue(i, 0); 2821 } 2822 2823 void 2824 nvmf_subsystem_set_ana_state(struct spdk_nvmf_subsystem *subsystem, 2825 const struct spdk_nvme_transport_id *trid, 2826 enum spdk_nvme_ana_state ana_state, 2827 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, void *cb_arg) 2828 { 2829 struct spdk_nvmf_subsystem_listener *listener; 2830 struct subsystem_listener_update_ctx *ctx; 2831 2832 assert(cb_fn != NULL); 2833 assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 2834 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED); 2835 2836 if (!subsystem->flags.ana_reporting) { 2837 SPDK_ERRLOG("ANA reporting is disabled\n"); 2838 cb_fn(cb_arg, -EINVAL); 2839 return; 2840 } 2841 2842 /* ANA Change state is not used, ANA Persistent Loss state 2843 * is not supported yet. 2844 */ 2845 if (!(ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE || 2846 ana_state == SPDK_NVME_ANA_NON_OPTIMIZED_STATE || 2847 ana_state == SPDK_NVME_ANA_INACCESSIBLE_STATE)) { 2848 SPDK_ERRLOG("ANA state %d is not supported\n", ana_state); 2849 cb_fn(cb_arg, -ENOTSUP); 2850 return; 2851 } 2852 2853 listener = nvmf_subsystem_find_listener(subsystem, trid); 2854 if (!listener) { 2855 SPDK_ERRLOG("Unable to find listener.\n"); 2856 cb_fn(cb_arg, -EINVAL); 2857 return; 2858 } 2859 2860 if (listener->ana_state == ana_state) { 2861 cb_fn(cb_arg, 0); 2862 return; 2863 } 2864 2865 ctx = calloc(1, sizeof(*ctx)); 2866 if (!ctx) { 2867 SPDK_ERRLOG("Unable to allocate context\n"); 2868 cb_fn(cb_arg, -ENOMEM); 2869 return; 2870 } 2871 2872 listener->ana_state = ana_state; 2873 listener->ana_state_change_count++; 2874 2875 ctx->listener = listener; 2876 ctx->cb_fn = cb_fn; 2877 ctx->cb_arg = cb_arg; 2878 2879 spdk_for_each_channel(subsystem->tgt, 2880 subsystem_listener_update_on_pg, 2881 ctx, 2882 subsystem_listener_update_done); 2883 } 2884