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