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/event.h" 40 #include "spdk/likely.h" 41 #include "spdk/string.h" 42 #include "spdk/trace.h" 43 #include "spdk/nvmf_spec.h" 44 #include "spdk/uuid.h" 45 #include "spdk/json.h" 46 #include "spdk/file.h" 47 48 #include "spdk/bdev_module.h" 49 #include "spdk_internal/log.h" 50 #include "spdk_internal/utf.h" 51 52 #define MODEL_NUMBER_DEFAULT "SPDK bdev Controller" 53 54 /* 55 * States for parsing valid domains in NQNs according to RFC 1034 56 */ 57 enum spdk_nvmf_nqn_domain_states { 58 /* First character of a domain must be a letter */ 59 SPDK_NVMF_DOMAIN_ACCEPT_LETTER = 0, 60 61 /* Subsequent characters can be any of letter, digit, or hyphen */ 62 SPDK_NVMF_DOMAIN_ACCEPT_LDH = 1, 63 64 /* A domain label must end with either a letter or digit */ 65 SPDK_NVMF_DOMAIN_ACCEPT_ANY = 2 66 }; 67 68 /* Returns true if is a valid ASCII string as defined by the NVMe spec */ 69 static bool 70 spdk_nvmf_valid_ascii_string(const void *buf, size_t size) 71 { 72 const uint8_t *str = buf; 73 size_t i; 74 75 for (i = 0; i < size; i++) { 76 if (str[i] < 0x20 || str[i] > 0x7E) { 77 return false; 78 } 79 } 80 81 return true; 82 } 83 84 static bool 85 spdk_nvmf_valid_nqn(const char *nqn) 86 { 87 size_t len; 88 struct spdk_uuid uuid_value; 89 uint32_t i; 90 int bytes_consumed; 91 uint32_t domain_label_length; 92 char *reverse_domain_end; 93 uint32_t reverse_domain_end_index; 94 enum spdk_nvmf_nqn_domain_states domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER; 95 96 /* Check for length requirements */ 97 len = strlen(nqn); 98 if (len > SPDK_NVMF_NQN_MAX_LEN) { 99 SPDK_ERRLOG("Invalid NQN \"%s\": length %zu > max %d\n", nqn, len, SPDK_NVMF_NQN_MAX_LEN); 100 return false; 101 } 102 103 /* The nqn must be at least as long as SPDK_NVMF_NQN_MIN_LEN to contain the necessary prefix. */ 104 if (len < SPDK_NVMF_NQN_MIN_LEN) { 105 SPDK_ERRLOG("Invalid NQN \"%s\": length %zu < min %d\n", nqn, len, SPDK_NVMF_NQN_MIN_LEN); 106 return false; 107 } 108 109 /* Check for discovery controller nqn */ 110 if (!strcmp(nqn, SPDK_NVMF_DISCOVERY_NQN)) { 111 return true; 112 } 113 114 /* Check for equality with the generic nqn structure of the form "nqn.2014-08.org.nvmexpress:uuid:11111111-2222-3333-4444-555555555555" */ 115 if (!strncmp(nqn, SPDK_NVMF_NQN_UUID_PRE, SPDK_NVMF_NQN_UUID_PRE_LEN)) { 116 if (len != SPDK_NVMF_NQN_UUID_PRE_LEN + SPDK_NVMF_UUID_STRING_LEN) { 117 SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not the correct length\n", nqn); 118 return false; 119 } 120 121 if (spdk_uuid_parse(&uuid_value, &nqn[SPDK_NVMF_NQN_UUID_PRE_LEN])) { 122 SPDK_ERRLOG("Invalid NQN \"%s\": uuid is not formatted correctly\n", nqn); 123 return false; 124 } 125 return true; 126 } 127 128 /* If the nqn does not match the uuid structure, the next several checks validate the form "nqn.yyyy-mm.reverse.domain:user-string" */ 129 130 if (strncmp(nqn, "nqn.", 4) != 0) { 131 SPDK_ERRLOG("Invalid NQN \"%s\": NQN must begin with \"nqn.\".\n", nqn); 132 return false; 133 } 134 135 /* Check for yyyy-mm. */ 136 if (!(isdigit(nqn[4]) && isdigit(nqn[5]) && isdigit(nqn[6]) && isdigit(nqn[7]) && 137 nqn[8] == '-' && isdigit(nqn[9]) && isdigit(nqn[10]) && nqn[11] == '.')) { 138 SPDK_ERRLOG("Invalid date code in NQN \"%s\"\n", nqn); 139 return false; 140 } 141 142 reverse_domain_end = strchr(nqn, ':'); 143 if (reverse_domain_end != NULL && (reverse_domain_end_index = reverse_domain_end - nqn) < len - 1) { 144 } else { 145 SPDK_ERRLOG("Invalid NQN \"%s\". NQN must contain user specified name with a ':' as a prefix.\n", 146 nqn); 147 return false; 148 } 149 150 /* Check for valid reverse domain */ 151 domain_label_length = 0; 152 for (i = 12; i < reverse_domain_end_index; i++) { 153 if (domain_label_length > SPDK_DOMAIN_LABEL_MAX_LEN) { 154 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". At least one Label is too long.\n", nqn); 155 return false; 156 } 157 158 switch (domain_state) { 159 160 case SPDK_NVMF_DOMAIN_ACCEPT_LETTER: { 161 if (isalpha(nqn[i])) { 162 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY; 163 domain_label_length++; 164 break; 165 } else { 166 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must start with a letter.\n", nqn); 167 return false; 168 } 169 } 170 171 case SPDK_NVMF_DOMAIN_ACCEPT_LDH: { 172 if (isalpha(nqn[i]) || isdigit(nqn[i])) { 173 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY; 174 domain_label_length++; 175 break; 176 } else if (nqn[i] == '-') { 177 if (i == reverse_domain_end_index - 1) { 178 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n", 179 nqn); 180 return false; 181 } 182 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH; 183 domain_label_length++; 184 break; 185 } else if (nqn[i] == '.') { 186 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n", 187 nqn); 188 return false; 189 } else { 190 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n", 191 nqn); 192 return false; 193 } 194 } 195 196 case SPDK_NVMF_DOMAIN_ACCEPT_ANY: { 197 if (isalpha(nqn[i]) || isdigit(nqn[i])) { 198 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_ANY; 199 domain_label_length++; 200 break; 201 } else if (nqn[i] == '-') { 202 if (i == reverse_domain_end_index - 1) { 203 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must end with an alphanumeric symbol.\n", 204 nqn); 205 return false; 206 } 207 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LDH; 208 domain_label_length++; 209 break; 210 } else if (nqn[i] == '.') { 211 domain_state = SPDK_NVMF_DOMAIN_ACCEPT_LETTER; 212 domain_label_length = 0; 213 break; 214 } else { 215 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only [a-z,A-Z,0-9,'-','.'].\n", 216 nqn); 217 return false; 218 } 219 } 220 } 221 } 222 223 i = reverse_domain_end_index + 1; 224 while (i < len) { 225 bytes_consumed = utf8_valid(&nqn[i], &nqn[len]); 226 if (bytes_consumed <= 0) { 227 SPDK_ERRLOG("Invalid domain name in NQN \"%s\". Label names must contain only valid utf-8.\n", nqn); 228 return false; 229 } 230 231 i += bytes_consumed; 232 } 233 return true; 234 } 235 236 struct spdk_nvmf_subsystem * 237 spdk_nvmf_subsystem_create(struct spdk_nvmf_tgt *tgt, 238 const char *nqn, 239 enum spdk_nvmf_subtype type, 240 uint32_t num_ns) 241 { 242 struct spdk_nvmf_subsystem *subsystem; 243 uint32_t sid; 244 245 if (spdk_nvmf_tgt_find_subsystem(tgt, nqn)) { 246 SPDK_ERRLOG("Subsystem NQN '%s' already exists\n", nqn); 247 return NULL; 248 } 249 250 if (!spdk_nvmf_valid_nqn(nqn)) { 251 return NULL; 252 } 253 254 if (type == SPDK_NVMF_SUBTYPE_DISCOVERY && num_ns != 0) { 255 SPDK_ERRLOG("Discovery subsystem cannot have namespaces.\n"); 256 return NULL; 257 } 258 259 /* Find a free subsystem id (sid) */ 260 for (sid = 0; sid < tgt->max_subsystems; sid++) { 261 if (tgt->subsystems[sid] == NULL) { 262 break; 263 } 264 } 265 if (sid >= tgt->max_subsystems) { 266 return NULL; 267 } 268 269 subsystem = calloc(1, sizeof(struct spdk_nvmf_subsystem)); 270 if (subsystem == NULL) { 271 return NULL; 272 } 273 274 subsystem->thread = spdk_get_thread(); 275 subsystem->state = SPDK_NVMF_SUBSYSTEM_INACTIVE; 276 subsystem->tgt = tgt; 277 subsystem->id = sid; 278 subsystem->subtype = type; 279 subsystem->max_nsid = num_ns; 280 subsystem->max_allowed_nsid = num_ns; 281 subsystem->next_cntlid = 0; 282 snprintf(subsystem->subnqn, sizeof(subsystem->subnqn), "%s", nqn); 283 TAILQ_INIT(&subsystem->listeners); 284 TAILQ_INIT(&subsystem->hosts); 285 TAILQ_INIT(&subsystem->ctrlrs); 286 287 if (num_ns != 0) { 288 subsystem->ns = calloc(num_ns, sizeof(struct spdk_nvmf_ns *)); 289 if (subsystem->ns == NULL) { 290 SPDK_ERRLOG("Namespace memory allocation failed\n"); 291 free(subsystem); 292 return NULL; 293 } 294 } 295 296 memset(subsystem->sn, '0', sizeof(subsystem->sn) - 1); 297 subsystem->sn[sizeof(subsystem->sn) - 1] = '\0'; 298 299 snprintf(subsystem->mn, sizeof(subsystem->mn), "%s", 300 MODEL_NUMBER_DEFAULT); 301 302 tgt->subsystems[sid] = subsystem; 303 tgt->discovery_genctr++; 304 305 return subsystem; 306 } 307 308 static void 309 _spdk_nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_host *host) 310 { 311 TAILQ_REMOVE(&subsystem->hosts, host, link); 312 free(host); 313 } 314 315 static void 316 _nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem, 317 struct spdk_nvmf_listener *listener) 318 { 319 struct spdk_nvmf_transport *transport; 320 321 transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, listener->trid.trtype); 322 if (transport != NULL) { 323 spdk_nvmf_transport_stop_listen(transport, &listener->trid); 324 } 325 326 TAILQ_REMOVE(&subsystem->listeners, listener, link); 327 free(listener); 328 } 329 330 void 331 spdk_nvmf_subsystem_destroy(struct spdk_nvmf_subsystem *subsystem) 332 { 333 struct spdk_nvmf_listener *listener, *listener_tmp; 334 struct spdk_nvmf_host *host, *host_tmp; 335 struct spdk_nvmf_ctrlr *ctrlr, *ctrlr_tmp; 336 struct spdk_nvmf_ns *ns; 337 338 if (!subsystem) { 339 return; 340 } 341 342 assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE); 343 344 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "subsystem is %p\n", subsystem); 345 346 TAILQ_FOREACH_SAFE(listener, &subsystem->listeners, link, listener_tmp) { 347 _nvmf_subsystem_remove_listener(subsystem, listener); 348 } 349 350 TAILQ_FOREACH_SAFE(host, &subsystem->hosts, link, host_tmp) { 351 _spdk_nvmf_subsystem_remove_host(subsystem, host); 352 } 353 354 TAILQ_FOREACH_SAFE(ctrlr, &subsystem->ctrlrs, link, ctrlr_tmp) { 355 spdk_nvmf_ctrlr_destruct(ctrlr); 356 } 357 358 ns = spdk_nvmf_subsystem_get_first_ns(subsystem); 359 while (ns != NULL) { 360 struct spdk_nvmf_ns *next_ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns); 361 362 spdk_nvmf_subsystem_remove_ns(subsystem, ns->opts.nsid); 363 ns = next_ns; 364 } 365 366 free(subsystem->ns); 367 368 subsystem->tgt->subsystems[subsystem->id] = NULL; 369 subsystem->tgt->discovery_genctr++; 370 371 free(subsystem); 372 } 373 374 static int 375 spdk_nvmf_subsystem_set_state(struct spdk_nvmf_subsystem *subsystem, 376 enum spdk_nvmf_subsystem_state state) 377 { 378 enum spdk_nvmf_subsystem_state actual_old_state, expected_old_state; 379 380 switch (state) { 381 case SPDK_NVMF_SUBSYSTEM_INACTIVE: 382 expected_old_state = SPDK_NVMF_SUBSYSTEM_DEACTIVATING; 383 break; 384 case SPDK_NVMF_SUBSYSTEM_ACTIVATING: 385 expected_old_state = SPDK_NVMF_SUBSYSTEM_INACTIVE; 386 break; 387 case SPDK_NVMF_SUBSYSTEM_ACTIVE: 388 expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING; 389 break; 390 case SPDK_NVMF_SUBSYSTEM_PAUSING: 391 expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 392 break; 393 case SPDK_NVMF_SUBSYSTEM_PAUSED: 394 expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSING; 395 break; 396 case SPDK_NVMF_SUBSYSTEM_RESUMING: 397 expected_old_state = SPDK_NVMF_SUBSYSTEM_PAUSED; 398 break; 399 case SPDK_NVMF_SUBSYSTEM_DEACTIVATING: 400 expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVE; 401 break; 402 default: 403 assert(false); 404 return -1; 405 } 406 407 actual_old_state = __sync_val_compare_and_swap(&subsystem->state, expected_old_state, state); 408 if (actual_old_state != expected_old_state) { 409 if (actual_old_state == SPDK_NVMF_SUBSYSTEM_RESUMING && 410 state == SPDK_NVMF_SUBSYSTEM_ACTIVE) { 411 expected_old_state = SPDK_NVMF_SUBSYSTEM_RESUMING; 412 } 413 /* This is for the case when activating the subsystem fails. */ 414 if (actual_old_state == SPDK_NVMF_SUBSYSTEM_ACTIVATING && 415 state == SPDK_NVMF_SUBSYSTEM_DEACTIVATING) { 416 expected_old_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING; 417 } 418 actual_old_state = __sync_val_compare_and_swap(&subsystem->state, expected_old_state, state); 419 } 420 assert(actual_old_state == expected_old_state); 421 return actual_old_state - expected_old_state; 422 } 423 424 struct subsystem_state_change_ctx { 425 struct spdk_nvmf_subsystem *subsystem; 426 427 enum spdk_nvmf_subsystem_state requested_state; 428 429 spdk_nvmf_subsystem_state_change_done cb_fn; 430 void *cb_arg; 431 }; 432 433 static void 434 subsystem_state_change_done(struct spdk_io_channel_iter *i, int status) 435 { 436 struct subsystem_state_change_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 437 438 if (status == 0) { 439 status = spdk_nvmf_subsystem_set_state(ctx->subsystem, ctx->requested_state); 440 if (status) { 441 status = -1; 442 } 443 } 444 445 if (ctx->cb_fn) { 446 ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status); 447 } 448 free(ctx); 449 } 450 451 static void 452 subsystem_state_change_continue(void *ctx, int status) 453 { 454 struct spdk_io_channel_iter *i = ctx; 455 spdk_for_each_channel_continue(i, status); 456 } 457 458 static void 459 subsystem_state_change_on_pg(struct spdk_io_channel_iter *i) 460 { 461 struct subsystem_state_change_ctx *ctx; 462 struct spdk_io_channel *ch; 463 struct spdk_nvmf_poll_group *group; 464 465 ctx = spdk_io_channel_iter_get_ctx(i); 466 ch = spdk_io_channel_iter_get_channel(i); 467 group = spdk_io_channel_get_ctx(ch); 468 469 switch (ctx->requested_state) { 470 case SPDK_NVMF_SUBSYSTEM_INACTIVE: 471 spdk_nvmf_poll_group_remove_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i); 472 break; 473 case SPDK_NVMF_SUBSYSTEM_ACTIVE: 474 if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_ACTIVATING) { 475 spdk_nvmf_poll_group_add_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i); 476 } else if (ctx->subsystem->state == SPDK_NVMF_SUBSYSTEM_RESUMING) { 477 spdk_nvmf_poll_group_resume_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i); 478 } 479 break; 480 case SPDK_NVMF_SUBSYSTEM_PAUSED: 481 spdk_nvmf_poll_group_pause_subsystem(group, ctx->subsystem, subsystem_state_change_continue, i); 482 break; 483 default: 484 assert(false); 485 break; 486 } 487 } 488 489 static int 490 spdk_nvmf_subsystem_state_change(struct spdk_nvmf_subsystem *subsystem, 491 enum spdk_nvmf_subsystem_state requested_state, 492 spdk_nvmf_subsystem_state_change_done cb_fn, 493 void *cb_arg) 494 { 495 struct subsystem_state_change_ctx *ctx; 496 enum spdk_nvmf_subsystem_state intermediate_state; 497 int rc; 498 499 switch (requested_state) { 500 case SPDK_NVMF_SUBSYSTEM_INACTIVE: 501 intermediate_state = SPDK_NVMF_SUBSYSTEM_DEACTIVATING; 502 break; 503 case SPDK_NVMF_SUBSYSTEM_ACTIVE: 504 if (subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED) { 505 intermediate_state = SPDK_NVMF_SUBSYSTEM_RESUMING; 506 } else { 507 intermediate_state = SPDK_NVMF_SUBSYSTEM_ACTIVATING; 508 } 509 break; 510 case SPDK_NVMF_SUBSYSTEM_PAUSED: 511 intermediate_state = SPDK_NVMF_SUBSYSTEM_PAUSING; 512 break; 513 default: 514 assert(false); 515 return -EINVAL; 516 } 517 518 ctx = calloc(1, sizeof(*ctx)); 519 if (!ctx) { 520 return -ENOMEM; 521 } 522 523 rc = spdk_nvmf_subsystem_set_state(subsystem, intermediate_state); 524 if (rc) { 525 free(ctx); 526 return rc; 527 } 528 529 ctx->subsystem = subsystem; 530 ctx->requested_state = requested_state; 531 ctx->cb_fn = cb_fn; 532 ctx->cb_arg = cb_arg; 533 534 spdk_for_each_channel(subsystem->tgt, 535 subsystem_state_change_on_pg, 536 ctx, 537 subsystem_state_change_done); 538 539 return 0; 540 } 541 542 int 543 spdk_nvmf_subsystem_start(struct spdk_nvmf_subsystem *subsystem, 544 spdk_nvmf_subsystem_state_change_done cb_fn, 545 void *cb_arg) 546 { 547 return spdk_nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg); 548 } 549 550 int 551 spdk_nvmf_subsystem_stop(struct spdk_nvmf_subsystem *subsystem, 552 spdk_nvmf_subsystem_state_change_done cb_fn, 553 void *cb_arg) 554 { 555 return spdk_nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_INACTIVE, cb_fn, cb_arg); 556 } 557 558 int 559 spdk_nvmf_subsystem_pause(struct spdk_nvmf_subsystem *subsystem, 560 spdk_nvmf_subsystem_state_change_done cb_fn, 561 void *cb_arg) 562 { 563 return spdk_nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_PAUSED, cb_fn, cb_arg); 564 } 565 566 int 567 spdk_nvmf_subsystem_resume(struct spdk_nvmf_subsystem *subsystem, 568 spdk_nvmf_subsystem_state_change_done cb_fn, 569 void *cb_arg) 570 { 571 return spdk_nvmf_subsystem_state_change(subsystem, SPDK_NVMF_SUBSYSTEM_ACTIVE, cb_fn, cb_arg); 572 } 573 574 struct spdk_nvmf_subsystem * 575 spdk_nvmf_subsystem_get_first(struct spdk_nvmf_tgt *tgt) 576 { 577 struct spdk_nvmf_subsystem *subsystem; 578 uint32_t sid; 579 580 for (sid = 0; sid < tgt->max_subsystems; sid++) { 581 subsystem = tgt->subsystems[sid]; 582 if (subsystem) { 583 return subsystem; 584 } 585 } 586 587 return NULL; 588 } 589 590 struct spdk_nvmf_subsystem * 591 spdk_nvmf_subsystem_get_next(struct spdk_nvmf_subsystem *subsystem) 592 { 593 uint32_t sid; 594 struct spdk_nvmf_tgt *tgt; 595 596 if (!subsystem) { 597 return NULL; 598 } 599 600 tgt = subsystem->tgt; 601 602 for (sid = subsystem->id + 1; sid < tgt->max_subsystems; sid++) { 603 subsystem = tgt->subsystems[sid]; 604 if (subsystem) { 605 return subsystem; 606 } 607 } 608 609 return NULL; 610 } 611 612 static struct spdk_nvmf_host * 613 _spdk_nvmf_subsystem_find_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn) 614 { 615 struct spdk_nvmf_host *host = NULL; 616 617 TAILQ_FOREACH(host, &subsystem->hosts, link) { 618 if (strcmp(hostnqn, host->nqn) == 0) { 619 return host; 620 } 621 } 622 623 return NULL; 624 } 625 626 int 627 spdk_nvmf_subsystem_add_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn) 628 { 629 struct spdk_nvmf_host *host; 630 631 if (!spdk_nvmf_valid_nqn(hostnqn)) { 632 return -EINVAL; 633 } 634 635 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 636 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 637 return -EAGAIN; 638 } 639 640 if (_spdk_nvmf_subsystem_find_host(subsystem, hostnqn)) { 641 /* This subsystem already allows the specified host. */ 642 return 0; 643 } 644 645 host = calloc(1, sizeof(*host)); 646 if (!host) { 647 return -ENOMEM; 648 } 649 650 snprintf(host->nqn, sizeof(host->nqn), "%s", hostnqn); 651 652 TAILQ_INSERT_HEAD(&subsystem->hosts, host, link); 653 subsystem->tgt->discovery_genctr++; 654 655 return 0; 656 } 657 658 int 659 spdk_nvmf_subsystem_remove_host(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn) 660 { 661 struct spdk_nvmf_host *host; 662 663 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 664 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 665 return -EAGAIN; 666 } 667 668 host = _spdk_nvmf_subsystem_find_host(subsystem, hostnqn); 669 if (host == NULL) { 670 return -ENOENT; 671 } 672 673 _spdk_nvmf_subsystem_remove_host(subsystem, host); 674 return 0; 675 } 676 677 int 678 spdk_nvmf_subsystem_set_allow_any_host(struct spdk_nvmf_subsystem *subsystem, bool allow_any_host) 679 { 680 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 681 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 682 return -EAGAIN; 683 } 684 685 subsystem->allow_any_host = allow_any_host; 686 687 return 0; 688 } 689 690 bool 691 spdk_nvmf_subsystem_get_allow_any_host(const struct spdk_nvmf_subsystem *subsystem) 692 { 693 return subsystem->allow_any_host; 694 } 695 696 bool 697 spdk_nvmf_subsystem_host_allowed(struct spdk_nvmf_subsystem *subsystem, const char *hostnqn) 698 { 699 if (!hostnqn) { 700 return false; 701 } 702 703 if (subsystem->allow_any_host) { 704 return true; 705 } 706 707 return _spdk_nvmf_subsystem_find_host(subsystem, hostnqn) != NULL; 708 } 709 710 struct spdk_nvmf_host * 711 spdk_nvmf_subsystem_get_first_host(struct spdk_nvmf_subsystem *subsystem) 712 { 713 return TAILQ_FIRST(&subsystem->hosts); 714 } 715 716 717 struct spdk_nvmf_host * 718 spdk_nvmf_subsystem_get_next_host(struct spdk_nvmf_subsystem *subsystem, 719 struct spdk_nvmf_host *prev_host) 720 { 721 return TAILQ_NEXT(prev_host, link); 722 } 723 724 const char * 725 spdk_nvmf_host_get_nqn(struct spdk_nvmf_host *host) 726 { 727 return host->nqn; 728 } 729 730 static struct spdk_nvmf_listener * 731 _spdk_nvmf_subsystem_find_listener(struct spdk_nvmf_subsystem *subsystem, 732 const struct spdk_nvme_transport_id *trid) 733 { 734 struct spdk_nvmf_listener *listener; 735 736 TAILQ_FOREACH(listener, &subsystem->listeners, link) { 737 if (spdk_nvme_transport_id_compare(&listener->trid, trid) == 0) { 738 return listener; 739 } 740 } 741 742 return NULL; 743 } 744 745 int 746 spdk_nvmf_subsystem_add_listener(struct spdk_nvmf_subsystem *subsystem, 747 struct spdk_nvme_transport_id *trid) 748 { 749 struct spdk_nvmf_transport *transport; 750 struct spdk_nvmf_listener *listener; 751 752 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 753 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 754 return -EAGAIN; 755 } 756 757 if (_spdk_nvmf_subsystem_find_listener(subsystem, trid)) { 758 /* Listener already exists in this subsystem */ 759 return 0; 760 } 761 762 transport = spdk_nvmf_tgt_get_transport(subsystem->tgt, trid->trtype); 763 if (transport == NULL) { 764 SPDK_ERRLOG("Unknown transport type %d\n", trid->trtype); 765 return -EINVAL; 766 } 767 768 listener = calloc(1, sizeof(*listener)); 769 if (!listener) { 770 return -ENOMEM; 771 } 772 773 listener->trid = *trid; 774 listener->transport = transport; 775 776 TAILQ_INSERT_HEAD(&subsystem->listeners, listener, link); 777 778 return 0; 779 } 780 781 int 782 spdk_nvmf_subsystem_remove_listener(struct spdk_nvmf_subsystem *subsystem, 783 const struct spdk_nvme_transport_id *trid) 784 { 785 struct spdk_nvmf_listener *listener; 786 787 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 788 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 789 return -EAGAIN; 790 } 791 792 listener = _spdk_nvmf_subsystem_find_listener(subsystem, trid); 793 if (listener == NULL) { 794 return -ENOENT; 795 } 796 797 _nvmf_subsystem_remove_listener(subsystem, listener); 798 799 return 0; 800 } 801 802 bool 803 spdk_nvmf_subsystem_listener_allowed(struct spdk_nvmf_subsystem *subsystem, 804 struct spdk_nvme_transport_id *trid) 805 { 806 struct spdk_nvmf_listener *listener; 807 808 if (!strcmp(subsystem->subnqn, SPDK_NVMF_DISCOVERY_NQN)) { 809 return true; 810 } 811 812 TAILQ_FOREACH(listener, &subsystem->listeners, link) { 813 if (spdk_nvme_transport_id_compare(&listener->trid, trid) == 0) { 814 return true; 815 } 816 } 817 818 return false; 819 } 820 821 struct spdk_nvmf_listener * 822 spdk_nvmf_subsystem_get_first_listener(struct spdk_nvmf_subsystem *subsystem) 823 { 824 return TAILQ_FIRST(&subsystem->listeners); 825 } 826 827 struct spdk_nvmf_listener * 828 spdk_nvmf_subsystem_get_next_listener(struct spdk_nvmf_subsystem *subsystem, 829 struct spdk_nvmf_listener *prev_listener) 830 { 831 return TAILQ_NEXT(prev_listener, link); 832 } 833 834 const struct spdk_nvme_transport_id * 835 spdk_nvmf_listener_get_trid(struct spdk_nvmf_listener *listener) 836 { 837 return &listener->trid; 838 } 839 840 struct subsystem_update_ns_ctx { 841 struct spdk_nvmf_subsystem *subsystem; 842 843 spdk_nvmf_subsystem_state_change_done cb_fn; 844 void *cb_arg; 845 }; 846 847 static void 848 subsystem_update_ns_done(struct spdk_io_channel_iter *i, int status) 849 { 850 struct subsystem_update_ns_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 851 852 if (ctx->cb_fn) { 853 ctx->cb_fn(ctx->subsystem, ctx->cb_arg, status); 854 } 855 free(ctx); 856 } 857 858 static void 859 subsystem_update_ns_on_pg(struct spdk_io_channel_iter *i) 860 { 861 int rc; 862 struct subsystem_update_ns_ctx *ctx; 863 struct spdk_nvmf_poll_group *group; 864 struct spdk_nvmf_subsystem *subsystem; 865 866 ctx = spdk_io_channel_iter_get_ctx(i); 867 group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i)); 868 subsystem = ctx->subsystem; 869 870 rc = spdk_nvmf_poll_group_update_subsystem(group, subsystem); 871 spdk_for_each_channel_continue(i, rc); 872 } 873 874 static int 875 spdk_nvmf_subsystem_update_ns(struct spdk_nvmf_subsystem *subsystem, spdk_channel_for_each_cpl cpl, 876 void *ctx) 877 { 878 spdk_for_each_channel(subsystem->tgt, 879 subsystem_update_ns_on_pg, 880 ctx, 881 cpl); 882 883 return 0; 884 } 885 886 static void 887 spdk_nvmf_subsystem_ns_changed(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 888 { 889 struct spdk_nvmf_ctrlr *ctrlr; 890 891 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 892 spdk_nvmf_ctrlr_ns_changed(ctrlr, nsid); 893 } 894 } 895 896 int 897 spdk_nvmf_subsystem_remove_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 898 { 899 struct spdk_nvmf_ns *ns; 900 struct spdk_nvmf_registrant *reg, *reg_tmp; 901 902 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 903 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 904 assert(false); 905 return -1; 906 } 907 908 if (nsid == 0 || nsid > subsystem->max_nsid) { 909 return -1; 910 } 911 912 ns = subsystem->ns[nsid - 1]; 913 if (!ns) { 914 return -1; 915 } 916 917 subsystem->ns[nsid - 1] = NULL; 918 919 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) { 920 TAILQ_REMOVE(&ns->registrants, reg, link); 921 free(reg); 922 } 923 spdk_bdev_module_release_bdev(ns->bdev); 924 spdk_bdev_close(ns->desc); 925 if (ns->ptpl_file) { 926 free(ns->ptpl_file); 927 } 928 free(ns); 929 930 spdk_nvmf_subsystem_ns_changed(subsystem, nsid); 931 932 return 0; 933 } 934 935 static void 936 _spdk_nvmf_ns_hot_remove(struct spdk_nvmf_subsystem *subsystem, 937 void *cb_arg, int status) 938 { 939 struct spdk_nvmf_ns *ns = cb_arg; 940 int rc; 941 942 rc = spdk_nvmf_subsystem_remove_ns(subsystem, ns->opts.nsid); 943 if (rc != 0) { 944 SPDK_ERRLOG("Failed to make changes to NVME-oF subsystem with id: %u\n", subsystem->id); 945 } 946 947 spdk_nvmf_subsystem_resume(subsystem, NULL, NULL); 948 } 949 950 static void 951 spdk_nvmf_ns_hot_remove(void *remove_ctx) 952 { 953 struct spdk_nvmf_ns *ns = remove_ctx; 954 int rc; 955 956 rc = spdk_nvmf_subsystem_pause(ns->subsystem, _spdk_nvmf_ns_hot_remove, ns); 957 if (rc) { 958 SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n"); 959 } 960 } 961 962 static void 963 spdk_nvmf_ns_event(enum spdk_bdev_event_type type, 964 struct spdk_bdev *bdev, 965 void *event_ctx) 966 { 967 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Bdev event: type %d, name %s, subsystem_id %d, ns_id %d\n", 968 type, 969 bdev->name, 970 ((struct spdk_nvmf_ns *)event_ctx)->subsystem->id, 971 ((struct spdk_nvmf_ns *)event_ctx)->nsid); 972 973 switch (type) { 974 case SPDK_BDEV_EVENT_REMOVE: 975 spdk_nvmf_ns_hot_remove(event_ctx); 976 break; 977 default: 978 SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); 979 break; 980 } 981 } 982 983 void 984 spdk_nvmf_ns_opts_get_defaults(struct spdk_nvmf_ns_opts *opts, size_t opts_size) 985 { 986 /* All current fields are set to 0 by default. */ 987 memset(opts, 0, opts_size); 988 } 989 990 /* Dummy bdev module used to to claim bdevs. */ 991 static struct spdk_bdev_module ns_bdev_module = { 992 .name = "NVMe-oF Target", 993 }; 994 995 static int 996 spdk_nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info); 997 static int 998 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info); 999 1000 uint32_t 1001 spdk_nvmf_subsystem_add_ns(struct spdk_nvmf_subsystem *subsystem, struct spdk_bdev *bdev, 1002 const struct spdk_nvmf_ns_opts *user_opts, size_t opts_size, 1003 const char *ptpl_file) 1004 { 1005 struct spdk_nvmf_ns_opts opts; 1006 struct spdk_nvmf_ns *ns; 1007 struct spdk_nvmf_reservation_info info = {0}; 1008 int rc; 1009 1010 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 1011 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 1012 return 0; 1013 } 1014 1015 if (spdk_bdev_get_md_size(bdev) != 0 && !spdk_bdev_is_md_interleaved(bdev)) { 1016 SPDK_ERRLOG("Can't attach bdev with separate metadata.\n"); 1017 return 0; 1018 } 1019 1020 spdk_nvmf_ns_opts_get_defaults(&opts, sizeof(opts)); 1021 if (user_opts) { 1022 memcpy(&opts, user_opts, spdk_min(sizeof(opts), opts_size)); 1023 } 1024 1025 if (spdk_mem_all_zero(&opts.uuid, sizeof(opts.uuid))) { 1026 opts.uuid = *spdk_bdev_get_uuid(bdev); 1027 } 1028 1029 if (opts.nsid == SPDK_NVME_GLOBAL_NS_TAG) { 1030 SPDK_ERRLOG("Invalid NSID %" PRIu32 "\n", opts.nsid); 1031 return 0; 1032 } 1033 1034 if (opts.nsid == 0) { 1035 /* 1036 * NSID not specified - find a free index. 1037 * 1038 * If no free slots are found, opts.nsid will be subsystem->max_nsid + 1, which will 1039 * expand max_nsid if possible. 1040 */ 1041 for (opts.nsid = 1; opts.nsid <= subsystem->max_nsid; opts.nsid++) { 1042 if (_spdk_nvmf_subsystem_get_ns(subsystem, opts.nsid) == NULL) { 1043 break; 1044 } 1045 } 1046 } 1047 1048 if (_spdk_nvmf_subsystem_get_ns(subsystem, opts.nsid)) { 1049 SPDK_ERRLOG("Requested NSID %" PRIu32 " already in use\n", opts.nsid); 1050 return 0; 1051 } 1052 1053 if (opts.nsid > subsystem->max_nsid) { 1054 struct spdk_nvmf_ns **new_ns_array; 1055 1056 /* If MaxNamespaces was specified, we can't extend max_nsid beyond it. */ 1057 if (subsystem->max_allowed_nsid > 0 && opts.nsid > subsystem->max_allowed_nsid) { 1058 SPDK_ERRLOG("Can't extend NSID range above MaxNamespaces\n"); 1059 return 0; 1060 } 1061 1062 /* If a controller is connected, we can't change NN. */ 1063 if (!TAILQ_EMPTY(&subsystem->ctrlrs)) { 1064 SPDK_ERRLOG("Can't extend NSID range while controllers are connected\n"); 1065 return 0; 1066 } 1067 1068 new_ns_array = realloc(subsystem->ns, sizeof(struct spdk_nvmf_ns *) * opts.nsid); 1069 if (new_ns_array == NULL) { 1070 SPDK_ERRLOG("Memory allocation error while resizing namespace array.\n"); 1071 return 0; 1072 } 1073 1074 memset(new_ns_array + subsystem->max_nsid, 0, 1075 sizeof(struct spdk_nvmf_ns *) * (opts.nsid - subsystem->max_nsid)); 1076 subsystem->ns = new_ns_array; 1077 subsystem->max_nsid = opts.nsid; 1078 } 1079 1080 ns = calloc(1, sizeof(*ns)); 1081 if (ns == NULL) { 1082 SPDK_ERRLOG("Namespace allocation failed\n"); 1083 return 0; 1084 } 1085 1086 ns->bdev = bdev; 1087 ns->opts = opts; 1088 ns->subsystem = subsystem; 1089 rc = spdk_bdev_open_ext(bdev->name, true, spdk_nvmf_ns_event, ns, &ns->desc); 1090 if (rc != 0) { 1091 SPDK_ERRLOG("Subsystem %s: bdev %s cannot be opened, error=%d\n", 1092 subsystem->subnqn, spdk_bdev_get_name(bdev), rc); 1093 free(ns); 1094 return 0; 1095 } 1096 rc = spdk_bdev_module_claim_bdev(bdev, ns->desc, &ns_bdev_module); 1097 if (rc != 0) { 1098 spdk_bdev_close(ns->desc); 1099 free(ns); 1100 return 0; 1101 } 1102 subsystem->ns[opts.nsid - 1] = ns; 1103 ns->nsid = opts.nsid; 1104 TAILQ_INIT(&ns->registrants); 1105 1106 if (ptpl_file) { 1107 rc = spdk_nvmf_ns_load_reservation(ptpl_file, &info); 1108 if (!rc) { 1109 rc = nvmf_ns_reservation_restore(ns, &info); 1110 if (rc) { 1111 SPDK_ERRLOG("Subsystem restore reservation failed\n"); 1112 subsystem->ns[opts.nsid - 1] = NULL; 1113 spdk_bdev_close(ns->desc); 1114 free(ns); 1115 return 0; 1116 } 1117 } 1118 ns->ptpl_file = strdup(ptpl_file); 1119 } 1120 1121 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Subsystem %s: bdev %s assigned nsid %" PRIu32 "\n", 1122 spdk_nvmf_subsystem_get_nqn(subsystem), 1123 spdk_bdev_get_name(bdev), 1124 opts.nsid); 1125 1126 spdk_nvmf_subsystem_ns_changed(subsystem, opts.nsid); 1127 1128 return opts.nsid; 1129 } 1130 1131 static uint32_t 1132 spdk_nvmf_subsystem_get_next_allocated_nsid(struct spdk_nvmf_subsystem *subsystem, 1133 uint32_t prev_nsid) 1134 { 1135 uint32_t nsid; 1136 1137 if (prev_nsid >= subsystem->max_nsid) { 1138 return 0; 1139 } 1140 1141 for (nsid = prev_nsid + 1; nsid <= subsystem->max_nsid; nsid++) { 1142 if (subsystem->ns[nsid - 1]) { 1143 return nsid; 1144 } 1145 } 1146 1147 return 0; 1148 } 1149 1150 struct spdk_nvmf_ns * 1151 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem) 1152 { 1153 uint32_t first_nsid; 1154 1155 first_nsid = spdk_nvmf_subsystem_get_next_allocated_nsid(subsystem, 0); 1156 return _spdk_nvmf_subsystem_get_ns(subsystem, first_nsid); 1157 } 1158 1159 struct spdk_nvmf_ns * 1160 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem, 1161 struct spdk_nvmf_ns *prev_ns) 1162 { 1163 uint32_t next_nsid; 1164 1165 next_nsid = spdk_nvmf_subsystem_get_next_allocated_nsid(subsystem, prev_ns->opts.nsid); 1166 return _spdk_nvmf_subsystem_get_ns(subsystem, next_nsid); 1167 } 1168 1169 struct spdk_nvmf_ns * 1170 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 1171 { 1172 return _spdk_nvmf_subsystem_get_ns(subsystem, nsid); 1173 } 1174 1175 uint32_t 1176 spdk_nvmf_ns_get_id(const struct spdk_nvmf_ns *ns) 1177 { 1178 return ns->opts.nsid; 1179 } 1180 1181 struct spdk_bdev * 1182 spdk_nvmf_ns_get_bdev(struct spdk_nvmf_ns *ns) 1183 { 1184 return ns->bdev; 1185 } 1186 1187 void 1188 spdk_nvmf_ns_get_opts(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_ns_opts *opts, 1189 size_t opts_size) 1190 { 1191 memset(opts, 0, opts_size); 1192 memcpy(opts, &ns->opts, spdk_min(sizeof(ns->opts), opts_size)); 1193 } 1194 1195 const char * 1196 spdk_nvmf_subsystem_get_sn(const struct spdk_nvmf_subsystem *subsystem) 1197 { 1198 return subsystem->sn; 1199 } 1200 1201 int 1202 spdk_nvmf_subsystem_set_sn(struct spdk_nvmf_subsystem *subsystem, const char *sn) 1203 { 1204 size_t len, max_len; 1205 1206 max_len = sizeof(subsystem->sn) - 1; 1207 len = strlen(sn); 1208 if (len > max_len) { 1209 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Invalid sn \"%s\": length %zu > max %zu\n", 1210 sn, len, max_len); 1211 return -1; 1212 } 1213 1214 if (!spdk_nvmf_valid_ascii_string(sn, len)) { 1215 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Non-ASCII sn\n"); 1216 SPDK_LOGDUMP(SPDK_LOG_NVMF, "sn", sn, len); 1217 return -1; 1218 } 1219 1220 snprintf(subsystem->sn, sizeof(subsystem->sn), "%s", sn); 1221 1222 return 0; 1223 } 1224 1225 const char * 1226 spdk_nvmf_subsystem_get_mn(const struct spdk_nvmf_subsystem *subsystem) 1227 { 1228 return subsystem->mn; 1229 } 1230 1231 int 1232 spdk_nvmf_subsystem_set_mn(struct spdk_nvmf_subsystem *subsystem, const char *mn) 1233 { 1234 size_t len, max_len; 1235 1236 if (mn == NULL) { 1237 mn = MODEL_NUMBER_DEFAULT; 1238 } 1239 max_len = sizeof(subsystem->mn) - 1; 1240 len = strlen(mn); 1241 if (len > max_len) { 1242 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Invalid mn \"%s\": length %zu > max %zu\n", 1243 mn, len, max_len); 1244 return -1; 1245 } 1246 1247 if (!spdk_nvmf_valid_ascii_string(mn, len)) { 1248 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Non-ASCII mn\n"); 1249 SPDK_LOGDUMP(SPDK_LOG_NVMF, "mn", mn, len); 1250 return -1; 1251 } 1252 1253 snprintf(subsystem->mn, sizeof(subsystem->mn), "%s", mn); 1254 1255 return 0; 1256 } 1257 1258 const char * 1259 spdk_nvmf_subsystem_get_nqn(struct spdk_nvmf_subsystem *subsystem) 1260 { 1261 return subsystem->subnqn; 1262 } 1263 1264 /* Workaround for astyle formatting bug */ 1265 typedef enum spdk_nvmf_subtype nvmf_subtype_t; 1266 1267 nvmf_subtype_t 1268 spdk_nvmf_subsystem_get_type(struct spdk_nvmf_subsystem *subsystem) 1269 { 1270 return subsystem->subtype; 1271 } 1272 1273 static uint16_t 1274 spdk_nvmf_subsystem_gen_cntlid(struct spdk_nvmf_subsystem *subsystem) 1275 { 1276 int count; 1277 1278 /* 1279 * In the worst case, we might have to try all CNTLID values between 1 and 0xFFF0 - 1 1280 * before we find one that is unused (or find that all values are in use). 1281 */ 1282 for (count = 0; count < 0xFFF0 - 1; count++) { 1283 subsystem->next_cntlid++; 1284 if (subsystem->next_cntlid >= 0xFFF0) { 1285 /* The spec reserves cntlid values in the range FFF0h to FFFFh. */ 1286 subsystem->next_cntlid = 1; 1287 } 1288 1289 /* Check if a controller with this cntlid currently exists. */ 1290 if (spdk_nvmf_subsystem_get_ctrlr(subsystem, subsystem->next_cntlid) == NULL) { 1291 /* Found unused cntlid */ 1292 return subsystem->next_cntlid; 1293 } 1294 } 1295 1296 /* All valid cntlid values are in use. */ 1297 return 0xFFFF; 1298 } 1299 1300 int 1301 spdk_nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ctrlr *ctrlr) 1302 { 1303 ctrlr->cntlid = spdk_nvmf_subsystem_gen_cntlid(subsystem); 1304 if (ctrlr->cntlid == 0xFFFF) { 1305 /* Unable to get a cntlid */ 1306 SPDK_ERRLOG("Reached max simultaneous ctrlrs\n"); 1307 return -EBUSY; 1308 } 1309 1310 TAILQ_INSERT_TAIL(&subsystem->ctrlrs, ctrlr, link); 1311 1312 return 0; 1313 } 1314 1315 void 1316 spdk_nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem, 1317 struct spdk_nvmf_ctrlr *ctrlr) 1318 { 1319 assert(subsystem == ctrlr->subsys); 1320 TAILQ_REMOVE(&subsystem->ctrlrs, ctrlr, link); 1321 } 1322 1323 struct spdk_nvmf_ctrlr * 1324 spdk_nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, uint16_t cntlid) 1325 { 1326 struct spdk_nvmf_ctrlr *ctrlr; 1327 1328 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 1329 if (ctrlr->cntlid == cntlid) { 1330 return ctrlr; 1331 } 1332 } 1333 1334 return NULL; 1335 } 1336 1337 uint32_t 1338 spdk_nvmf_subsystem_get_max_namespaces(const struct spdk_nvmf_subsystem *subsystem) 1339 { 1340 return subsystem->max_allowed_nsid; 1341 } 1342 1343 struct _nvmf_ns_registrant { 1344 uint64_t rkey; 1345 char *host_uuid; 1346 }; 1347 1348 struct _nvmf_ns_registrants { 1349 size_t num_regs; 1350 struct _nvmf_ns_registrant reg[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 1351 }; 1352 1353 struct _nvmf_ns_reservation { 1354 bool ptpl_activated; 1355 enum spdk_nvme_reservation_type rtype; 1356 uint64_t crkey; 1357 char *bdev_uuid; 1358 char *holder_uuid; 1359 struct _nvmf_ns_registrants regs; 1360 }; 1361 1362 static const struct spdk_json_object_decoder nvmf_ns_pr_reg_decoders[] = { 1363 {"rkey", offsetof(struct _nvmf_ns_registrant, rkey), spdk_json_decode_uint64}, 1364 {"host_uuid", offsetof(struct _nvmf_ns_registrant, host_uuid), spdk_json_decode_string}, 1365 }; 1366 1367 static int 1368 nvmf_decode_ns_pr_reg(const struct spdk_json_val *val, void *out) 1369 { 1370 struct _nvmf_ns_registrant *reg = out; 1371 1372 return spdk_json_decode_object(val, nvmf_ns_pr_reg_decoders, 1373 SPDK_COUNTOF(nvmf_ns_pr_reg_decoders), reg); 1374 } 1375 1376 static int 1377 nvmf_decode_ns_pr_regs(const struct spdk_json_val *val, void *out) 1378 { 1379 struct _nvmf_ns_registrants *regs = out; 1380 1381 return spdk_json_decode_array(val, nvmf_decode_ns_pr_reg, regs->reg, 1382 SPDK_NVMF_MAX_NUM_REGISTRANTS, ®s->num_regs, 1383 sizeof(struct _nvmf_ns_registrant)); 1384 } 1385 1386 static const struct spdk_json_object_decoder nvmf_ns_pr_decoders[] = { 1387 {"ptpl", offsetof(struct _nvmf_ns_reservation, ptpl_activated), spdk_json_decode_bool, true}, 1388 {"rtype", offsetof(struct _nvmf_ns_reservation, rtype), spdk_json_decode_uint32, true}, 1389 {"crkey", offsetof(struct _nvmf_ns_reservation, crkey), spdk_json_decode_uint64, true}, 1390 {"bdev_uuid", offsetof(struct _nvmf_ns_reservation, bdev_uuid), spdk_json_decode_string}, 1391 {"holder_uuid", offsetof(struct _nvmf_ns_reservation, holder_uuid), spdk_json_decode_string, true}, 1392 {"registrants", offsetof(struct _nvmf_ns_reservation, regs), nvmf_decode_ns_pr_regs}, 1393 }; 1394 1395 static int 1396 spdk_nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info) 1397 { 1398 FILE *fd; 1399 size_t json_size; 1400 ssize_t values_cnt, rc; 1401 void *json = NULL, *end; 1402 struct spdk_json_val *values = NULL; 1403 struct _nvmf_ns_reservation res = {}; 1404 uint32_t i; 1405 1406 fd = fopen(file, "r"); 1407 /* It's not an error if the file does not exist */ 1408 if (!fd) { 1409 SPDK_NOTICELOG("File %s does not exist\n", file); 1410 return -ENOENT; 1411 } 1412 1413 /* Load all persist file contents into a local buffer */ 1414 json = spdk_posix_file_load(fd, &json_size); 1415 fclose(fd); 1416 if (!json) { 1417 SPDK_ERRLOG("Load persit file %s failed\n", file); 1418 return -ENOMEM; 1419 } 1420 1421 rc = spdk_json_parse(json, json_size, NULL, 0, &end, 0); 1422 if (rc < 0) { 1423 SPDK_NOTICELOG("Parsing JSON configuration failed (%zd)\n", rc); 1424 goto exit; 1425 } 1426 1427 values_cnt = rc; 1428 values = calloc(values_cnt, sizeof(struct spdk_json_val)); 1429 if (values == NULL) { 1430 goto exit; 1431 } 1432 1433 rc = spdk_json_parse(json, json_size, values, values_cnt, &end, 0); 1434 if (rc != values_cnt) { 1435 SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc); 1436 goto exit; 1437 } 1438 1439 /* Decode json */ 1440 if (spdk_json_decode_object(values, nvmf_ns_pr_decoders, 1441 SPDK_COUNTOF(nvmf_ns_pr_decoders), 1442 &res)) { 1443 SPDK_ERRLOG("Invalid objects in the persist file %s\n", file); 1444 rc = -EINVAL; 1445 goto exit; 1446 } 1447 1448 if (res.regs.num_regs > SPDK_NVMF_MAX_NUM_REGISTRANTS) { 1449 SPDK_ERRLOG("Can only support up to %u registrants\n", SPDK_NVMF_MAX_NUM_REGISTRANTS); 1450 rc = -ERANGE; 1451 goto exit; 1452 } 1453 1454 rc = 0; 1455 info->ptpl_activated = res.ptpl_activated; 1456 info->rtype = res.rtype; 1457 info->crkey = res.crkey; 1458 snprintf(info->bdev_uuid, sizeof(info->bdev_uuid), "%s", res.bdev_uuid); 1459 snprintf(info->holder_uuid, sizeof(info->holder_uuid), "%s", res.holder_uuid); 1460 info->num_regs = res.regs.num_regs; 1461 for (i = 0; i < res.regs.num_regs; i++) { 1462 info->registrants[i].rkey = res.regs.reg[i].rkey; 1463 snprintf(info->registrants[i].host_uuid, sizeof(info->registrants[i].host_uuid), "%s", 1464 res.regs.reg[i].host_uuid); 1465 } 1466 1467 exit: 1468 free(json); 1469 free(values); 1470 free(res.bdev_uuid); 1471 free(res.holder_uuid); 1472 for (i = 0; i < res.regs.num_regs; i++) { 1473 free(res.regs.reg[i].host_uuid); 1474 } 1475 1476 return rc; 1477 } 1478 1479 static bool 1480 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns); 1481 1482 static int 1483 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info) 1484 { 1485 uint32_t i; 1486 struct spdk_nvmf_registrant *reg, *holder = NULL; 1487 struct spdk_uuid bdev_uuid, holder_uuid; 1488 1489 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "NSID %u, PTPL %u, Number of registrants %u\n", 1490 ns->nsid, info->ptpl_activated, info->num_regs); 1491 1492 /* it's not an error */ 1493 if (!info->ptpl_activated || !info->num_regs) { 1494 return 0; 1495 } 1496 1497 spdk_uuid_parse(&bdev_uuid, info->bdev_uuid); 1498 if (spdk_uuid_compare(&bdev_uuid, spdk_bdev_get_uuid(ns->bdev))) { 1499 SPDK_ERRLOG("Existing bdev UUID is not same with configuration file\n"); 1500 return -EINVAL; 1501 } 1502 1503 ns->crkey = info->crkey; 1504 ns->rtype = info->rtype; 1505 ns->ptpl_activated = info->ptpl_activated; 1506 spdk_uuid_parse(&holder_uuid, info->holder_uuid); 1507 1508 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Bdev UUID %s\n", info->bdev_uuid); 1509 if (info->rtype) { 1510 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Holder UUID %s, RTYPE %u, RKEY 0x%"PRIx64"\n", 1511 info->holder_uuid, info->rtype, info->crkey); 1512 } 1513 1514 for (i = 0; i < info->num_regs; i++) { 1515 reg = calloc(1, sizeof(*reg)); 1516 if (!reg) { 1517 return -ENOMEM; 1518 } 1519 spdk_uuid_parse(®->hostid, info->registrants[i].host_uuid); 1520 reg->rkey = info->registrants[i].rkey; 1521 TAILQ_INSERT_TAIL(&ns->registrants, reg, link); 1522 if (!spdk_uuid_compare(&holder_uuid, ®->hostid)) { 1523 holder = reg; 1524 } 1525 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "Registrant RKEY 0x%"PRIx64", Host UUID %s\n", 1526 info->registrants[i].rkey, info->registrants[i].host_uuid); 1527 } 1528 1529 if (nvmf_ns_reservation_all_registrants_type(ns)) { 1530 ns->holder = TAILQ_FIRST(&ns->registrants); 1531 } else { 1532 ns->holder = holder; 1533 } 1534 1535 return 0; 1536 } 1537 1538 static int 1539 spdk_nvmf_ns_json_write_cb(void *cb_ctx, const void *data, size_t size) 1540 { 1541 char *file = cb_ctx; 1542 size_t rc; 1543 FILE *fd; 1544 1545 fd = fopen(file, "w"); 1546 if (!fd) { 1547 SPDK_ERRLOG("Can't open file %s for write\n", file); 1548 return -ENOENT; 1549 } 1550 rc = fwrite(data, 1, size, fd); 1551 fclose(fd); 1552 1553 return rc == size ? 0 : -1; 1554 } 1555 1556 static int 1557 spdk_nvmf_ns_reservation_update(const char *file, struct spdk_nvmf_reservation_info *info) 1558 { 1559 struct spdk_json_write_ctx *w; 1560 uint32_t i; 1561 int rc = 0; 1562 1563 w = spdk_json_write_begin(spdk_nvmf_ns_json_write_cb, (void *)file, 0); 1564 if (w == NULL) { 1565 return -ENOMEM; 1566 } 1567 /* clear the configuration file */ 1568 if (!info->ptpl_activated) { 1569 goto exit; 1570 } 1571 1572 spdk_json_write_object_begin(w); 1573 spdk_json_write_named_bool(w, "ptpl", info->ptpl_activated); 1574 spdk_json_write_named_uint32(w, "rtype", info->rtype); 1575 spdk_json_write_named_uint64(w, "crkey", info->crkey); 1576 spdk_json_write_named_string(w, "bdev_uuid", info->bdev_uuid); 1577 spdk_json_write_named_string(w, "holder_uuid", info->holder_uuid); 1578 1579 spdk_json_write_named_array_begin(w, "registrants"); 1580 for (i = 0; i < info->num_regs; i++) { 1581 spdk_json_write_object_begin(w); 1582 spdk_json_write_named_uint64(w, "rkey", info->registrants[i].rkey); 1583 spdk_json_write_named_string(w, "host_uuid", info->registrants[i].host_uuid); 1584 spdk_json_write_object_end(w); 1585 } 1586 spdk_json_write_array_end(w); 1587 spdk_json_write_object_end(w); 1588 1589 exit: 1590 rc = spdk_json_write_end(w); 1591 return rc; 1592 } 1593 1594 static int 1595 nvmf_ns_update_reservation_info(struct spdk_nvmf_ns *ns) 1596 { 1597 struct spdk_nvmf_reservation_info info; 1598 struct spdk_nvmf_registrant *reg, *tmp; 1599 uint32_t i = 0; 1600 1601 assert(ns != NULL); 1602 1603 if (!ns->bdev || !ns->ptpl_file) { 1604 return 0; 1605 } 1606 1607 memset(&info, 0, sizeof(info)); 1608 spdk_uuid_fmt_lower(info.bdev_uuid, sizeof(info.bdev_uuid), spdk_bdev_get_uuid(ns->bdev)); 1609 1610 if (ns->rtype) { 1611 info.rtype = ns->rtype; 1612 info.crkey = ns->crkey; 1613 if (!nvmf_ns_reservation_all_registrants_type(ns)) { 1614 assert(ns->holder != NULL); 1615 spdk_uuid_fmt_lower(info.holder_uuid, sizeof(info.holder_uuid), &ns->holder->hostid); 1616 } 1617 } 1618 1619 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 1620 spdk_uuid_fmt_lower(info.registrants[i].host_uuid, sizeof(info.registrants[i].host_uuid), 1621 ®->hostid); 1622 info.registrants[i++].rkey = reg->rkey; 1623 } 1624 1625 info.num_regs = i; 1626 info.ptpl_activated = ns->ptpl_activated; 1627 1628 return spdk_nvmf_ns_reservation_update(ns->ptpl_file, &info); 1629 } 1630 1631 static struct spdk_nvmf_registrant * 1632 nvmf_ns_reservation_get_registrant(struct spdk_nvmf_ns *ns, 1633 struct spdk_uuid *uuid) 1634 { 1635 struct spdk_nvmf_registrant *reg, *tmp; 1636 1637 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 1638 if (!spdk_uuid_compare(®->hostid, uuid)) { 1639 return reg; 1640 } 1641 } 1642 1643 return NULL; 1644 } 1645 1646 /* Generate reservation notice log to registered HostID controllers */ 1647 static void 1648 nvmf_subsystem_gen_ctrlr_notification(struct spdk_nvmf_subsystem *subsystem, 1649 struct spdk_nvmf_ns *ns, 1650 struct spdk_uuid *hostid_list, 1651 uint32_t num_hostid, 1652 enum spdk_nvme_reservation_notification_log_page_type type) 1653 { 1654 struct spdk_nvmf_ctrlr *ctrlr; 1655 uint32_t i; 1656 1657 for (i = 0; i < num_hostid; i++) { 1658 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 1659 if (!spdk_uuid_compare(&ctrlr->hostid, &hostid_list[i])) { 1660 spdk_nvmf_ctrlr_reservation_notice_log(ctrlr, ns, type); 1661 } 1662 } 1663 } 1664 } 1665 1666 /* Get all registrants' hostid other than the controller who issued the command */ 1667 static uint32_t 1668 nvmf_ns_reservation_get_all_other_hostid(struct spdk_nvmf_ns *ns, 1669 struct spdk_uuid *hostid_list, 1670 uint32_t max_num_hostid, 1671 struct spdk_uuid *current_hostid) 1672 { 1673 struct spdk_nvmf_registrant *reg, *tmp; 1674 uint32_t num_hostid = 0; 1675 1676 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 1677 if (spdk_uuid_compare(®->hostid, current_hostid)) { 1678 if (num_hostid == max_num_hostid) { 1679 assert(false); 1680 return max_num_hostid; 1681 } 1682 hostid_list[num_hostid++] = reg->hostid; 1683 } 1684 } 1685 1686 return num_hostid; 1687 } 1688 1689 /* Calculate the unregistered HostID list according to list 1690 * prior to execute preempt command and list after executing 1691 * preempt command. 1692 */ 1693 static uint32_t 1694 nvmf_ns_reservation_get_unregistered_hostid(struct spdk_uuid *old_hostid_list, 1695 uint32_t old_num_hostid, 1696 struct spdk_uuid *remaining_hostid_list, 1697 uint32_t remaining_num_hostid) 1698 { 1699 struct spdk_uuid temp_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 1700 uint32_t i, j, num_hostid = 0; 1701 bool found; 1702 1703 if (!remaining_num_hostid) { 1704 return old_num_hostid; 1705 } 1706 1707 for (i = 0; i < old_num_hostid; i++) { 1708 found = false; 1709 for (j = 0; j < remaining_num_hostid; j++) { 1710 if (!spdk_uuid_compare(&old_hostid_list[i], &remaining_hostid_list[j])) { 1711 found = true; 1712 break; 1713 } 1714 } 1715 if (!found) { 1716 spdk_uuid_copy(&temp_hostid_list[num_hostid++], &old_hostid_list[i]); 1717 } 1718 } 1719 1720 if (num_hostid) { 1721 memcpy(old_hostid_list, temp_hostid_list, sizeof(struct spdk_uuid) * num_hostid); 1722 } 1723 1724 return num_hostid; 1725 } 1726 1727 /* current reservation type is all registrants or not */ 1728 static bool 1729 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns) 1730 { 1731 return (ns->rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS || 1732 ns->rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS); 1733 } 1734 1735 /* current registrant is reservation holder or not */ 1736 static bool 1737 nvmf_ns_reservation_registrant_is_holder(struct spdk_nvmf_ns *ns, 1738 struct spdk_nvmf_registrant *reg) 1739 { 1740 if (!reg) { 1741 return false; 1742 } 1743 1744 if (nvmf_ns_reservation_all_registrants_type(ns)) { 1745 return true; 1746 } 1747 1748 return (ns->holder == reg); 1749 } 1750 1751 static int 1752 nvmf_ns_reservation_add_registrant(struct spdk_nvmf_ns *ns, 1753 struct spdk_nvmf_ctrlr *ctrlr, 1754 uint64_t nrkey) 1755 { 1756 struct spdk_nvmf_registrant *reg; 1757 1758 reg = calloc(1, sizeof(*reg)); 1759 if (!reg) { 1760 return -ENOMEM; 1761 } 1762 1763 reg->rkey = nrkey; 1764 /* set hostid for the registrant */ 1765 spdk_uuid_copy(®->hostid, &ctrlr->hostid); 1766 TAILQ_INSERT_TAIL(&ns->registrants, reg, link); 1767 ns->gen++; 1768 1769 return 0; 1770 } 1771 1772 static void 1773 nvmf_ns_reservation_release_reservation(struct spdk_nvmf_ns *ns) 1774 { 1775 ns->rtype = 0; 1776 ns->crkey = 0; 1777 ns->holder = NULL; 1778 } 1779 1780 /* release the reservation if the last registrant was removed */ 1781 static void 1782 nvmf_ns_reservation_check_release_on_remove_registrant(struct spdk_nvmf_ns *ns, 1783 struct spdk_nvmf_registrant *reg) 1784 { 1785 struct spdk_nvmf_registrant *next_reg; 1786 1787 /* no reservation holder */ 1788 if (!ns->holder) { 1789 assert(ns->rtype == 0); 1790 return; 1791 } 1792 1793 next_reg = TAILQ_FIRST(&ns->registrants); 1794 if (next_reg && nvmf_ns_reservation_all_registrants_type(ns)) { 1795 /* the next valid registrant is the new holder now */ 1796 ns->holder = next_reg; 1797 } else if (nvmf_ns_reservation_registrant_is_holder(ns, reg)) { 1798 /* release the reservation */ 1799 nvmf_ns_reservation_release_reservation(ns); 1800 } 1801 } 1802 1803 static void 1804 nvmf_ns_reservation_remove_registrant(struct spdk_nvmf_ns *ns, 1805 struct spdk_nvmf_registrant *reg) 1806 { 1807 TAILQ_REMOVE(&ns->registrants, reg, link); 1808 nvmf_ns_reservation_check_release_on_remove_registrant(ns, reg); 1809 free(reg); 1810 ns->gen++; 1811 return; 1812 } 1813 1814 static uint32_t 1815 nvmf_ns_reservation_remove_registrants_by_key(struct spdk_nvmf_ns *ns, 1816 uint64_t rkey) 1817 { 1818 struct spdk_nvmf_registrant *reg, *tmp; 1819 uint32_t count = 0; 1820 1821 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 1822 if (reg->rkey == rkey) { 1823 nvmf_ns_reservation_remove_registrant(ns, reg); 1824 count++; 1825 } 1826 } 1827 return count; 1828 } 1829 1830 static uint32_t 1831 nvmf_ns_reservation_remove_all_other_registrants(struct spdk_nvmf_ns *ns, 1832 struct spdk_nvmf_registrant *reg) 1833 { 1834 struct spdk_nvmf_registrant *reg_tmp, *reg_tmp2; 1835 uint32_t count = 0; 1836 1837 TAILQ_FOREACH_SAFE(reg_tmp, &ns->registrants, link, reg_tmp2) { 1838 if (reg_tmp != reg) { 1839 nvmf_ns_reservation_remove_registrant(ns, reg_tmp); 1840 count++; 1841 } 1842 } 1843 return count; 1844 } 1845 1846 static uint32_t 1847 nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns) 1848 { 1849 struct spdk_nvmf_registrant *reg, *reg_tmp; 1850 uint32_t count = 0; 1851 1852 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) { 1853 nvmf_ns_reservation_remove_registrant(ns, reg); 1854 count++; 1855 } 1856 return count; 1857 } 1858 1859 static void 1860 nvmf_ns_reservation_acquire_reservation(struct spdk_nvmf_ns *ns, uint64_t rkey, 1861 enum spdk_nvme_reservation_type rtype, 1862 struct spdk_nvmf_registrant *holder) 1863 { 1864 ns->rtype = rtype; 1865 ns->crkey = rkey; 1866 assert(ns->holder == NULL); 1867 ns->holder = holder; 1868 } 1869 1870 static bool 1871 nvmf_ns_reservation_register(struct spdk_nvmf_ns *ns, 1872 struct spdk_nvmf_ctrlr *ctrlr, 1873 struct spdk_nvmf_request *req) 1874 { 1875 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 1876 uint8_t rrega, iekey, cptpl, rtype; 1877 struct spdk_nvme_reservation_register_data key; 1878 struct spdk_nvmf_registrant *reg; 1879 uint8_t status = SPDK_NVME_SC_SUCCESS; 1880 bool update_sgroup = false; 1881 struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 1882 uint32_t num_hostid = 0; 1883 int rc; 1884 1885 rrega = cmd->cdw10 & 0x7u; 1886 iekey = (cmd->cdw10 >> 3) & 0x1u; 1887 cptpl = (cmd->cdw10 >> 30) & 0x3u; 1888 1889 if (req->data && req->length >= sizeof(key)) { 1890 memcpy(&key, req->data, sizeof(key)); 1891 } else { 1892 SPDK_ERRLOG("No key provided. Failing request.\n"); 1893 status = SPDK_NVME_SC_INVALID_FIELD; 1894 goto exit; 1895 } 1896 1897 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "REGISTER: RREGA %u, IEKEY %u, CPTPL %u, " 1898 "NRKEY 0x%"PRIx64", NRKEY 0x%"PRIx64"\n", 1899 rrega, iekey, cptpl, key.crkey, key.nrkey); 1900 1901 if (cptpl == SPDK_NVME_RESERVE_PTPL_CLEAR_POWER_ON) { 1902 /* Ture to OFF state, and need to be updated in the configuration file */ 1903 if (ns->ptpl_activated) { 1904 ns->ptpl_activated = 0; 1905 update_sgroup = true; 1906 } 1907 } else if (cptpl == SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS) { 1908 if (ns->ptpl_file == NULL) { 1909 status = SPDK_NVME_SC_INVALID_FIELD; 1910 goto exit; 1911 } else if (ns->ptpl_activated == 0) { 1912 ns->ptpl_activated = 1; 1913 update_sgroup = true; 1914 } 1915 } 1916 1917 /* current Host Identifier has registrant or not */ 1918 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid); 1919 1920 switch (rrega) { 1921 case SPDK_NVME_RESERVE_REGISTER_KEY: 1922 if (!reg) { 1923 /* register new controller */ 1924 if (key.nrkey == 0) { 1925 SPDK_ERRLOG("Can't register zeroed new key\n"); 1926 status = SPDK_NVME_SC_INVALID_FIELD; 1927 goto exit; 1928 } 1929 rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey); 1930 if (rc < 0) { 1931 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 1932 goto exit; 1933 } 1934 update_sgroup = true; 1935 } else { 1936 /* register with same key is not an error */ 1937 if (reg->rkey != key.nrkey) { 1938 SPDK_ERRLOG("The same host already register a " 1939 "key with 0x%"PRIx64"\n", 1940 reg->rkey); 1941 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 1942 goto exit; 1943 } 1944 } 1945 break; 1946 case SPDK_NVME_RESERVE_UNREGISTER_KEY: 1947 if (!reg || (!iekey && reg->rkey != key.crkey)) { 1948 SPDK_ERRLOG("No registrant or current key doesn't match " 1949 "with existing registrant key\n"); 1950 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 1951 goto exit; 1952 } 1953 1954 rtype = ns->rtype; 1955 num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list, 1956 SPDK_NVMF_MAX_NUM_REGISTRANTS, 1957 &ctrlr->hostid); 1958 1959 nvmf_ns_reservation_remove_registrant(ns, reg); 1960 1961 if (!ns->rtype && num_hostid && (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY || 1962 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY)) { 1963 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 1964 hostid_list, 1965 num_hostid, 1966 SPDK_NVME_RESERVATION_RELEASED); 1967 } 1968 update_sgroup = true; 1969 break; 1970 case SPDK_NVME_RESERVE_REPLACE_KEY: 1971 if (!reg || (!iekey && reg->rkey != key.crkey)) { 1972 SPDK_ERRLOG("No registrant or current key doesn't match " 1973 "with existing registrant key\n"); 1974 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 1975 goto exit; 1976 } 1977 if (key.nrkey == 0) { 1978 SPDK_ERRLOG("Can't register zeroed new key\n"); 1979 status = SPDK_NVME_SC_INVALID_FIELD; 1980 goto exit; 1981 } 1982 reg->rkey = key.nrkey; 1983 update_sgroup = true; 1984 break; 1985 default: 1986 status = SPDK_NVME_SC_INVALID_FIELD; 1987 goto exit; 1988 } 1989 1990 exit: 1991 if (update_sgroup) { 1992 rc = nvmf_ns_update_reservation_info(ns); 1993 if (rc != 0) { 1994 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 1995 } 1996 } 1997 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 1998 req->rsp->nvme_cpl.status.sc = status; 1999 return update_sgroup; 2000 } 2001 2002 static bool 2003 nvmf_ns_reservation_acquire(struct spdk_nvmf_ns *ns, 2004 struct spdk_nvmf_ctrlr *ctrlr, 2005 struct spdk_nvmf_request *req) 2006 { 2007 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2008 uint8_t racqa, iekey, rtype; 2009 struct spdk_nvme_reservation_acquire_data key; 2010 struct spdk_nvmf_registrant *reg; 2011 bool all_regs = false; 2012 uint32_t count = 0; 2013 bool update_sgroup = true; 2014 struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2015 uint32_t num_hostid = 0; 2016 struct spdk_uuid new_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2017 uint32_t new_num_hostid = 0; 2018 bool reservation_released = false; 2019 uint8_t status = SPDK_NVME_SC_SUCCESS; 2020 2021 racqa = cmd->cdw10 & 0x7u; 2022 iekey = (cmd->cdw10 >> 3) & 0x1u; 2023 rtype = (cmd->cdw10 >> 8) & 0xffu; 2024 2025 if (req->data && req->length >= sizeof(key)) { 2026 memcpy(&key, req->data, sizeof(key)); 2027 } else { 2028 SPDK_ERRLOG("No key provided. Failing request.\n"); 2029 status = SPDK_NVME_SC_INVALID_FIELD; 2030 goto exit; 2031 } 2032 2033 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "ACQUIRE: RACQA %u, IEKEY %u, RTYPE %u, " 2034 "NRKEY 0x%"PRIx64", PRKEY 0x%"PRIx64"\n", 2035 racqa, iekey, rtype, key.crkey, key.prkey); 2036 2037 if (iekey || rtype > SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) { 2038 SPDK_ERRLOG("Ignore existing key field set to 1\n"); 2039 status = SPDK_NVME_SC_INVALID_FIELD; 2040 update_sgroup = false; 2041 goto exit; 2042 } 2043 2044 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid); 2045 /* must be registrant and CRKEY must match */ 2046 if (!reg || reg->rkey != key.crkey) { 2047 SPDK_ERRLOG("No registrant or current key doesn't match " 2048 "with existing registrant key\n"); 2049 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2050 update_sgroup = false; 2051 goto exit; 2052 } 2053 2054 all_regs = nvmf_ns_reservation_all_registrants_type(ns); 2055 2056 switch (racqa) { 2057 case SPDK_NVME_RESERVE_ACQUIRE: 2058 /* it's not an error for the holder to acquire same reservation type again */ 2059 if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && ns->rtype == rtype) { 2060 /* do nothing */ 2061 update_sgroup = false; 2062 } else if (ns->holder == NULL) { 2063 /* fisrt time to acquire the reservation */ 2064 nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg); 2065 } else { 2066 SPDK_ERRLOG("Invalid rtype or current registrant is not holder\n"); 2067 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2068 update_sgroup = false; 2069 goto exit; 2070 } 2071 break; 2072 case SPDK_NVME_RESERVE_PREEMPT: 2073 /* no reservation holder */ 2074 if (!ns->holder) { 2075 /* unregister with PRKEY */ 2076 nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey); 2077 break; 2078 } 2079 num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list, 2080 SPDK_NVMF_MAX_NUM_REGISTRANTS, 2081 &ctrlr->hostid); 2082 2083 /* only 1 reservation holder and reservation key is valid */ 2084 if (!all_regs) { 2085 /* preempt itself */ 2086 if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && 2087 ns->crkey == key.prkey) { 2088 ns->rtype = rtype; 2089 reservation_released = true; 2090 break; 2091 } 2092 2093 if (ns->crkey == key.prkey) { 2094 nvmf_ns_reservation_remove_registrant(ns, ns->holder); 2095 nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg); 2096 reservation_released = true; 2097 } else if (key.prkey != 0) { 2098 nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey); 2099 } else { 2100 /* PRKEY is zero */ 2101 SPDK_ERRLOG("Current PRKEY is zero\n"); 2102 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2103 update_sgroup = false; 2104 goto exit; 2105 } 2106 } else { 2107 /* release all other registrants except for the current one */ 2108 if (key.prkey == 0) { 2109 nvmf_ns_reservation_remove_all_other_registrants(ns, reg); 2110 assert(ns->holder == reg); 2111 } else { 2112 count = nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey); 2113 if (count == 0) { 2114 SPDK_ERRLOG("PRKEY doesn't match any registrant\n"); 2115 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2116 update_sgroup = false; 2117 goto exit; 2118 } 2119 } 2120 } 2121 break; 2122 default: 2123 status = SPDK_NVME_SC_INVALID_FIELD; 2124 update_sgroup = false; 2125 break; 2126 } 2127 2128 exit: 2129 if (update_sgroup && racqa == SPDK_NVME_RESERVE_PREEMPT) { 2130 new_num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, new_hostid_list, 2131 SPDK_NVMF_MAX_NUM_REGISTRANTS, 2132 &ctrlr->hostid); 2133 /* Preempt notification occurs on the unregistered controllers 2134 * other than the controller who issued the command. 2135 */ 2136 num_hostid = nvmf_ns_reservation_get_unregistered_hostid(hostid_list, 2137 num_hostid, 2138 new_hostid_list, 2139 new_num_hostid); 2140 if (num_hostid) { 2141 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2142 hostid_list, 2143 num_hostid, 2144 SPDK_NVME_REGISTRATION_PREEMPTED); 2145 2146 } 2147 /* Reservation released notification occurs on the 2148 * controllers which are the remaining registrants other than 2149 * the controller who issued the command. 2150 */ 2151 if (reservation_released && new_num_hostid) { 2152 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2153 new_hostid_list, 2154 new_num_hostid, 2155 SPDK_NVME_RESERVATION_RELEASED); 2156 2157 } 2158 } 2159 if (update_sgroup && ns->ptpl_activated) { 2160 if (nvmf_ns_update_reservation_info(ns)) { 2161 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2162 } 2163 } 2164 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2165 req->rsp->nvme_cpl.status.sc = status; 2166 return update_sgroup; 2167 } 2168 2169 static bool 2170 nvmf_ns_reservation_release(struct spdk_nvmf_ns *ns, 2171 struct spdk_nvmf_ctrlr *ctrlr, 2172 struct spdk_nvmf_request *req) 2173 { 2174 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2175 uint8_t rrela, iekey, rtype; 2176 struct spdk_nvmf_registrant *reg; 2177 uint64_t crkey; 2178 uint8_t status = SPDK_NVME_SC_SUCCESS; 2179 bool update_sgroup = true; 2180 struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2181 uint32_t num_hostid = 0; 2182 2183 rrela = cmd->cdw10 & 0x7u; 2184 iekey = (cmd->cdw10 >> 3) & 0x1u; 2185 rtype = (cmd->cdw10 >> 8) & 0xffu; 2186 2187 if (req->data && req->length >= sizeof(crkey)) { 2188 memcpy(&crkey, req->data, sizeof(crkey)); 2189 } else { 2190 SPDK_ERRLOG("No key provided. Failing request.\n"); 2191 status = SPDK_NVME_SC_INVALID_FIELD; 2192 goto exit; 2193 } 2194 2195 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "RELEASE: RRELA %u, IEKEY %u, RTYPE %u, " 2196 "CRKEY 0x%"PRIx64"\n", rrela, iekey, rtype, crkey); 2197 2198 if (iekey) { 2199 SPDK_ERRLOG("Ignore existing key field set to 1\n"); 2200 status = SPDK_NVME_SC_INVALID_FIELD; 2201 update_sgroup = false; 2202 goto exit; 2203 } 2204 2205 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid); 2206 if (!reg || reg->rkey != crkey) { 2207 SPDK_ERRLOG("No registrant or current key doesn't match " 2208 "with existing registrant key\n"); 2209 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2210 update_sgroup = false; 2211 goto exit; 2212 } 2213 2214 num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list, 2215 SPDK_NVMF_MAX_NUM_REGISTRANTS, 2216 &ctrlr->hostid); 2217 2218 switch (rrela) { 2219 case SPDK_NVME_RESERVE_RELEASE: 2220 if (!ns->holder) { 2221 SPDK_DEBUGLOG(SPDK_LOG_NVMF, "RELEASE: no holder\n"); 2222 update_sgroup = false; 2223 goto exit; 2224 } 2225 if (ns->rtype != rtype) { 2226 SPDK_ERRLOG("Type doesn't match\n"); 2227 status = SPDK_NVME_SC_INVALID_FIELD; 2228 update_sgroup = false; 2229 goto exit; 2230 } 2231 if (!nvmf_ns_reservation_registrant_is_holder(ns, reg)) { 2232 /* not the reservation holder, this isn't an error */ 2233 update_sgroup = false; 2234 goto exit; 2235 } 2236 2237 rtype = ns->rtype; 2238 nvmf_ns_reservation_release_reservation(ns); 2239 2240 if (num_hostid && rtype != SPDK_NVME_RESERVE_WRITE_EXCLUSIVE && 2241 rtype != SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 2242 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2243 hostid_list, 2244 num_hostid, 2245 SPDK_NVME_RESERVATION_RELEASED); 2246 } 2247 break; 2248 case SPDK_NVME_RESERVE_CLEAR: 2249 nvmf_ns_reservation_clear_all_registrants(ns); 2250 if (num_hostid) { 2251 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2252 hostid_list, 2253 num_hostid, 2254 SPDK_NVME_RESERVATION_PREEMPTED); 2255 } 2256 break; 2257 default: 2258 status = SPDK_NVME_SC_INVALID_FIELD; 2259 update_sgroup = false; 2260 goto exit; 2261 } 2262 2263 exit: 2264 if (update_sgroup && ns->ptpl_activated) { 2265 if (nvmf_ns_update_reservation_info(ns)) { 2266 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2267 } 2268 } 2269 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2270 req->rsp->nvme_cpl.status.sc = status; 2271 return update_sgroup; 2272 } 2273 2274 static void 2275 nvmf_ns_reservation_report(struct spdk_nvmf_ns *ns, 2276 struct spdk_nvmf_ctrlr *ctrlr, 2277 struct spdk_nvmf_request *req) 2278 { 2279 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2280 struct spdk_nvmf_subsystem *subsystem = ctrlr->subsys; 2281 struct spdk_nvmf_ctrlr *ctrlr_tmp; 2282 struct spdk_nvmf_registrant *reg, *tmp; 2283 struct spdk_nvme_reservation_status_extended_data *status_data; 2284 struct spdk_nvme_registered_ctrlr_extended_data *ctrlr_data; 2285 uint8_t *payload; 2286 uint32_t len, count = 0; 2287 uint32_t regctl = 0; 2288 uint8_t status = SPDK_NVME_SC_SUCCESS; 2289 2290 if (req->data == NULL) { 2291 SPDK_ERRLOG("No data transfer specified for request. " 2292 " Unable to transfer back response.\n"); 2293 status = SPDK_NVME_SC_INVALID_FIELD; 2294 goto exit; 2295 } 2296 2297 /* NVMeoF uses Extended Data Structure */ 2298 if ((cmd->cdw11 & 0x00000001u) == 0) { 2299 SPDK_ERRLOG("NVMeoF uses extended controller data structure, " 2300 "please set EDS bit in cdw11 and try again\n"); 2301 status = SPDK_NVME_SC_INVALID_FIELD; 2302 goto exit; 2303 } 2304 2305 /* Get number of registerd controllers, one Host may have more than 2306 * one controller based on different ports. 2307 */ 2308 TAILQ_FOREACH(ctrlr_tmp, &subsystem->ctrlrs, link) { 2309 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr_tmp->hostid); 2310 if (reg) { 2311 regctl++; 2312 } 2313 } 2314 2315 len = sizeof(*status_data) + sizeof(*ctrlr_data) * regctl; 2316 payload = calloc(1, len); 2317 if (!payload) { 2318 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2319 goto exit; 2320 } 2321 2322 status_data = (struct spdk_nvme_reservation_status_extended_data *)payload; 2323 status_data->data.gen = ns->gen; 2324 status_data->data.rtype = ns->rtype; 2325 status_data->data.regctl = regctl; 2326 status_data->data.ptpls = ns->ptpl_activated; 2327 2328 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 2329 assert(count <= regctl); 2330 ctrlr_data = (struct spdk_nvme_registered_ctrlr_extended_data *) 2331 (payload + sizeof(*status_data) + sizeof(*ctrlr_data) * count); 2332 /* Set to 0xffffh for dynamic controller */ 2333 ctrlr_data->cntlid = 0xffff; 2334 ctrlr_data->rcsts.status = (ns->holder == reg) ? true : false; 2335 ctrlr_data->rkey = reg->rkey; 2336 spdk_uuid_copy((struct spdk_uuid *)ctrlr_data->hostid, ®->hostid); 2337 count++; 2338 } 2339 2340 memcpy(req->data, payload, spdk_min(len, (cmd->cdw10 + 1) * sizeof(uint32_t))); 2341 free(payload); 2342 2343 exit: 2344 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2345 req->rsp->nvme_cpl.status.sc = status; 2346 return; 2347 } 2348 2349 static void 2350 spdk_nvmf_ns_reservation_complete(void *ctx) 2351 { 2352 struct spdk_nvmf_request *req = ctx; 2353 2354 spdk_nvmf_request_complete(req); 2355 } 2356 2357 static void 2358 _nvmf_ns_reservation_update_done(struct spdk_nvmf_subsystem *subsystem, 2359 void *cb_arg, int status) 2360 { 2361 struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)cb_arg; 2362 struct spdk_nvmf_poll_group *group = req->qpair->group; 2363 2364 spdk_thread_send_msg(group->thread, spdk_nvmf_ns_reservation_complete, req); 2365 } 2366 2367 void 2368 spdk_nvmf_ns_reservation_request(void *ctx) 2369 { 2370 struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)ctx; 2371 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2372 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 2373 struct subsystem_update_ns_ctx *update_ctx; 2374 uint32_t nsid; 2375 struct spdk_nvmf_ns *ns; 2376 bool update_sgroup = false; 2377 2378 nsid = cmd->nsid; 2379 ns = _spdk_nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 2380 assert(ns != NULL); 2381 2382 switch (cmd->opc) { 2383 case SPDK_NVME_OPC_RESERVATION_REGISTER: 2384 update_sgroup = nvmf_ns_reservation_register(ns, ctrlr, req); 2385 break; 2386 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 2387 update_sgroup = nvmf_ns_reservation_acquire(ns, ctrlr, req); 2388 break; 2389 case SPDK_NVME_OPC_RESERVATION_RELEASE: 2390 update_sgroup = nvmf_ns_reservation_release(ns, ctrlr, req); 2391 break; 2392 case SPDK_NVME_OPC_RESERVATION_REPORT: 2393 nvmf_ns_reservation_report(ns, ctrlr, req); 2394 break; 2395 default: 2396 break; 2397 } 2398 2399 /* update reservation information to subsystem's poll group */ 2400 if (update_sgroup) { 2401 update_ctx = calloc(1, sizeof(*update_ctx)); 2402 if (update_ctx == NULL) { 2403 SPDK_ERRLOG("Can't alloc subsystem poll group update context\n"); 2404 goto update_done; 2405 } 2406 update_ctx->subsystem = ctrlr->subsys; 2407 update_ctx->cb_fn = _nvmf_ns_reservation_update_done; 2408 update_ctx->cb_arg = req; 2409 2410 spdk_nvmf_subsystem_update_ns(ctrlr->subsys, subsystem_update_ns_done, update_ctx); 2411 return; 2412 } 2413 2414 update_done: 2415 _nvmf_ns_reservation_update_done(ctrlr->subsys, (void *)req, 0); 2416 } 2417