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, 1393 spdk_nvmf_subsystem_state_change_done cb_fn, void *cb_arg) 1394 { 1395 struct subsystem_update_ns_ctx *ctx; 1396 1397 ctx = calloc(1, sizeof(*ctx)); 1398 if (ctx == NULL) { 1399 SPDK_ERRLOG("Can't alloc subsystem poll group update context\n"); 1400 return -ENOMEM; 1401 } 1402 ctx->subsystem = subsystem; 1403 ctx->cb_fn = cb_fn; 1404 ctx->cb_arg = cb_arg; 1405 1406 spdk_for_each_channel(subsystem->tgt, 1407 subsystem_update_ns_on_pg, 1408 ctx, 1409 subsystem_update_ns_done); 1410 return 0; 1411 } 1412 1413 static void 1414 nvmf_subsystem_ns_changed(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 1415 { 1416 struct spdk_nvmf_ctrlr *ctrlr; 1417 1418 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 1419 nvmf_ctrlr_ns_changed(ctrlr, nsid); 1420 } 1421 } 1422 1423 static uint32_t nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns); 1424 1425 int 1426 spdk_nvmf_subsystem_remove_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 1427 { 1428 struct spdk_nvmf_transport *transport; 1429 struct spdk_nvmf_ns *ns; 1430 1431 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 1432 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 1433 assert(false); 1434 return -1; 1435 } 1436 1437 if (nsid == 0 || nsid > subsystem->max_nsid) { 1438 return -1; 1439 } 1440 1441 ns = subsystem->ns[nsid - 1]; 1442 if (!ns) { 1443 return -1; 1444 } 1445 1446 subsystem->ns[nsid - 1] = NULL; 1447 1448 assert(ns->anagrpid - 1 < subsystem->max_nsid); 1449 assert(subsystem->ana_group[ns->anagrpid - 1] > 0); 1450 1451 subsystem->ana_group[ns->anagrpid - 1]--; 1452 1453 free(ns->ptpl_file); 1454 nvmf_ns_reservation_clear_all_registrants(ns); 1455 spdk_bdev_module_release_bdev(ns->bdev); 1456 spdk_bdev_close(ns->desc); 1457 free(ns); 1458 1459 for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport; 1460 transport = spdk_nvmf_transport_get_next(transport)) { 1461 if (transport->ops->subsystem_remove_ns) { 1462 transport->ops->subsystem_remove_ns(transport, subsystem, nsid); 1463 } 1464 } 1465 1466 nvmf_subsystem_ns_changed(subsystem, nsid); 1467 1468 return 0; 1469 } 1470 1471 struct subsystem_ns_change_ctx { 1472 struct spdk_nvmf_subsystem *subsystem; 1473 spdk_nvmf_subsystem_state_change_done cb_fn; 1474 uint32_t nsid; 1475 }; 1476 1477 static void 1478 _nvmf_ns_hot_remove(struct spdk_nvmf_subsystem *subsystem, 1479 void *cb_arg, int status) 1480 { 1481 struct subsystem_ns_change_ctx *ctx = cb_arg; 1482 int rc; 1483 1484 rc = spdk_nvmf_subsystem_remove_ns(subsystem, ctx->nsid); 1485 if (rc != 0) { 1486 SPDK_ERRLOG("Failed to make changes to NVME-oF subsystem with id: %u\n", subsystem->id); 1487 } 1488 1489 rc = spdk_nvmf_subsystem_resume(subsystem, NULL, NULL); 1490 if (rc != 0) { 1491 SPDK_ERRLOG("Failed to resume NVME-oF subsystem with id: %u\n", subsystem->id); 1492 } 1493 1494 free(ctx); 1495 } 1496 1497 static void 1498 nvmf_ns_change_msg(void *ns_ctx) 1499 { 1500 struct subsystem_ns_change_ctx *ctx = ns_ctx; 1501 int rc; 1502 1503 SPDK_DTRACE_PROBE2(nvmf_ns_change, ctx->nsid, ctx->subsystem->subnqn); 1504 1505 rc = spdk_nvmf_subsystem_pause(ctx->subsystem, ctx->nsid, ctx->cb_fn, ctx); 1506 if (rc) { 1507 if (rc == -EBUSY) { 1508 /* Try again, this is not a permanent situation. */ 1509 spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ctx); 1510 } else { 1511 free(ctx); 1512 SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n"); 1513 } 1514 } 1515 } 1516 1517 static void 1518 nvmf_ns_hot_remove(void *remove_ctx) 1519 { 1520 struct spdk_nvmf_ns *ns = remove_ctx; 1521 struct subsystem_ns_change_ctx *ns_ctx; 1522 int rc; 1523 1524 /* We have to allocate a new context because this op 1525 * is asynchronous and we could lose the ns in the middle. 1526 */ 1527 ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx)); 1528 if (!ns_ctx) { 1529 SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n"); 1530 return; 1531 } 1532 1533 ns_ctx->subsystem = ns->subsystem; 1534 ns_ctx->nsid = ns->opts.nsid; 1535 ns_ctx->cb_fn = _nvmf_ns_hot_remove; 1536 1537 rc = spdk_nvmf_subsystem_pause(ns->subsystem, ns_ctx->nsid, _nvmf_ns_hot_remove, ns_ctx); 1538 if (rc) { 1539 if (rc == -EBUSY) { 1540 /* Try again, this is not a permanent situation. */ 1541 spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx); 1542 } else { 1543 SPDK_ERRLOG("Unable to pause subsystem to process namespace removal!\n"); 1544 free(ns_ctx); 1545 } 1546 } 1547 } 1548 1549 static void 1550 _nvmf_ns_resize(struct spdk_nvmf_subsystem *subsystem, void *cb_arg, int status) 1551 { 1552 struct subsystem_ns_change_ctx *ctx = cb_arg; 1553 1554 nvmf_subsystem_ns_changed(subsystem, ctx->nsid); 1555 if (spdk_nvmf_subsystem_resume(subsystem, NULL, NULL) != 0) { 1556 SPDK_ERRLOG("Failed to resume NVME-oF subsystem with id: %u\n", subsystem->id); 1557 } 1558 1559 free(ctx); 1560 } 1561 1562 static void 1563 nvmf_ns_resize(void *event_ctx) 1564 { 1565 struct spdk_nvmf_ns *ns = event_ctx; 1566 struct subsystem_ns_change_ctx *ns_ctx; 1567 int rc; 1568 1569 /* We have to allocate a new context because this op 1570 * is asynchronous and we could lose the ns in the middle. 1571 */ 1572 ns_ctx = calloc(1, sizeof(struct subsystem_ns_change_ctx)); 1573 if (!ns_ctx) { 1574 SPDK_ERRLOG("Unable to allocate context to process namespace removal!\n"); 1575 return; 1576 } 1577 1578 ns_ctx->subsystem = ns->subsystem; 1579 ns_ctx->nsid = ns->opts.nsid; 1580 ns_ctx->cb_fn = _nvmf_ns_resize; 1581 1582 /* Specify 0 for the nsid here, because we do not need to pause the namespace. 1583 * Namespaces can only be resized bigger, so there is no need to quiesce I/O. 1584 */ 1585 rc = spdk_nvmf_subsystem_pause(ns->subsystem, 0, _nvmf_ns_resize, ns_ctx); 1586 if (rc) { 1587 if (rc == -EBUSY) { 1588 /* Try again, this is not a permanent situation. */ 1589 spdk_thread_send_msg(spdk_get_thread(), nvmf_ns_change_msg, ns_ctx); 1590 } else { 1591 SPDK_ERRLOG("Unable to pause subsystem to process namespace resize!\n"); 1592 free(ns_ctx); 1593 } 1594 } 1595 } 1596 1597 static void 1598 nvmf_ns_event(enum spdk_bdev_event_type type, 1599 struct spdk_bdev *bdev, 1600 void *event_ctx) 1601 { 1602 SPDK_DEBUGLOG(nvmf, "Bdev event: type %d, name %s, subsystem_id %d, ns_id %d\n", 1603 type, 1604 spdk_bdev_get_name(bdev), 1605 ((struct spdk_nvmf_ns *)event_ctx)->subsystem->id, 1606 ((struct spdk_nvmf_ns *)event_ctx)->nsid); 1607 1608 switch (type) { 1609 case SPDK_BDEV_EVENT_REMOVE: 1610 nvmf_ns_hot_remove(event_ctx); 1611 break; 1612 case SPDK_BDEV_EVENT_RESIZE: 1613 nvmf_ns_resize(event_ctx); 1614 break; 1615 default: 1616 SPDK_NOTICELOG("Unsupported bdev event: type %d\n", type); 1617 break; 1618 } 1619 } 1620 1621 void 1622 spdk_nvmf_ns_opts_get_defaults(struct spdk_nvmf_ns_opts *opts, size_t opts_size) 1623 { 1624 if (!opts) { 1625 SPDK_ERRLOG("opts should not be NULL.\n"); 1626 return; 1627 } 1628 1629 if (!opts_size) { 1630 SPDK_ERRLOG("opts_size should not be zero.\n"); 1631 return; 1632 } 1633 1634 memset(opts, 0, opts_size); 1635 opts->opts_size = opts_size; 1636 1637 #define FIELD_OK(field) \ 1638 offsetof(struct spdk_nvmf_ns_opts, field) + sizeof(opts->field) <= opts_size 1639 1640 #define SET_FIELD(field, value) \ 1641 if (FIELD_OK(field)) { \ 1642 opts->field = value; \ 1643 } \ 1644 1645 /* All current fields are set to 0 by default. */ 1646 SET_FIELD(nsid, 0); 1647 if (FIELD_OK(nguid)) { 1648 memset(opts->nguid, 0, sizeof(opts->nguid)); 1649 } 1650 if (FIELD_OK(eui64)) { 1651 memset(opts->eui64, 0, sizeof(opts->eui64)); 1652 } 1653 if (FIELD_OK(uuid)) { 1654 spdk_uuid_set_null(&opts->uuid); 1655 } 1656 SET_FIELD(anagrpid, 0); 1657 1658 #undef FIELD_OK 1659 #undef SET_FIELD 1660 } 1661 1662 static void 1663 nvmf_ns_opts_copy(struct spdk_nvmf_ns_opts *opts, 1664 const struct spdk_nvmf_ns_opts *user_opts, 1665 size_t opts_size) 1666 { 1667 #define FIELD_OK(field) \ 1668 offsetof(struct spdk_nvmf_ns_opts, field) + sizeof(opts->field) <= user_opts->opts_size 1669 1670 #define SET_FIELD(field) \ 1671 if (FIELD_OK(field)) { \ 1672 opts->field = user_opts->field; \ 1673 } \ 1674 1675 SET_FIELD(nsid); 1676 if (FIELD_OK(nguid)) { 1677 memcpy(opts->nguid, user_opts->nguid, sizeof(opts->nguid)); 1678 } 1679 if (FIELD_OK(eui64)) { 1680 memcpy(opts->eui64, user_opts->eui64, sizeof(opts->eui64)); 1681 } 1682 if (FIELD_OK(uuid)) { 1683 spdk_uuid_copy(&opts->uuid, &user_opts->uuid); 1684 } 1685 SET_FIELD(anagrpid); 1686 1687 opts->opts_size = user_opts->opts_size; 1688 1689 /* We should not remove this statement, but need to update the assert statement 1690 * if we add a new field, and also add a corresponding SET_FIELD statement. 1691 */ 1692 SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_ns_opts) == 64, "Incorrect size"); 1693 1694 #undef FIELD_OK 1695 #undef SET_FIELD 1696 } 1697 1698 /* Dummy bdev module used to to claim bdevs. */ 1699 static struct spdk_bdev_module ns_bdev_module = { 1700 .name = "NVMe-oF Target", 1701 }; 1702 1703 static int nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info); 1704 static int nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, 1705 struct spdk_nvmf_reservation_info *info); 1706 1707 uint32_t 1708 spdk_nvmf_subsystem_add_ns_ext(struct spdk_nvmf_subsystem *subsystem, const char *bdev_name, 1709 const struct spdk_nvmf_ns_opts *user_opts, size_t opts_size, 1710 const char *ptpl_file) 1711 { 1712 struct spdk_nvmf_transport *transport; 1713 struct spdk_nvmf_ns_opts opts; 1714 struct spdk_nvmf_ns *ns; 1715 struct spdk_nvmf_reservation_info info = {0}; 1716 int rc; 1717 bool zone_append_supported; 1718 uint64_t max_zone_append_size_kib; 1719 1720 if (!(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 1721 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED)) { 1722 return 0; 1723 } 1724 1725 spdk_nvmf_ns_opts_get_defaults(&opts, sizeof(opts)); 1726 if (user_opts) { 1727 nvmf_ns_opts_copy(&opts, user_opts, opts_size); 1728 } 1729 1730 if (opts.nsid == SPDK_NVME_GLOBAL_NS_TAG) { 1731 SPDK_ERRLOG("Invalid NSID %" PRIu32 "\n", opts.nsid); 1732 return 0; 1733 } 1734 1735 if (opts.nsid == 0) { 1736 /* 1737 * NSID not specified - find a free index. 1738 * 1739 * If no free slots are found, opts.nsid will be subsystem->max_nsid + 1, which will 1740 * expand max_nsid if possible. 1741 */ 1742 for (opts.nsid = 1; opts.nsid <= subsystem->max_nsid; opts.nsid++) { 1743 if (_nvmf_subsystem_get_ns(subsystem, opts.nsid) == NULL) { 1744 break; 1745 } 1746 } 1747 } 1748 1749 if (_nvmf_subsystem_get_ns(subsystem, opts.nsid)) { 1750 SPDK_ERRLOG("Requested NSID %" PRIu32 " already in use\n", opts.nsid); 1751 return 0; 1752 } 1753 1754 if (opts.nsid > subsystem->max_nsid) { 1755 SPDK_ERRLOG("NSID greater than maximum not allowed\n"); 1756 return 0; 1757 } 1758 1759 if (opts.anagrpid == 0) { 1760 opts.anagrpid = opts.nsid; 1761 } 1762 1763 if (opts.anagrpid > subsystem->max_nsid) { 1764 SPDK_ERRLOG("ANAGRPID greater than maximum NSID not allowed\n"); 1765 return 0; 1766 } 1767 1768 ns = calloc(1, sizeof(*ns)); 1769 if (ns == NULL) { 1770 SPDK_ERRLOG("Namespace allocation failed\n"); 1771 return 0; 1772 } 1773 1774 rc = spdk_bdev_open_ext(bdev_name, true, nvmf_ns_event, ns, &ns->desc); 1775 if (rc != 0) { 1776 SPDK_ERRLOG("Subsystem %s: bdev %s cannot be opened, error=%d\n", 1777 subsystem->subnqn, bdev_name, rc); 1778 free(ns); 1779 return 0; 1780 } 1781 1782 ns->bdev = spdk_bdev_desc_get_bdev(ns->desc); 1783 1784 if (spdk_bdev_get_md_size(ns->bdev) != 0) { 1785 if (!spdk_bdev_is_md_interleaved(ns->bdev)) { 1786 SPDK_ERRLOG("Can't attach bdev with separate metadata.\n"); 1787 spdk_bdev_close(ns->desc); 1788 free(ns); 1789 return 0; 1790 } 1791 1792 if (spdk_bdev_get_md_size(ns->bdev) > SPDK_BDEV_MAX_INTERLEAVED_MD_SIZE) { 1793 SPDK_ERRLOG("Maximum supported interleaved md size %u, current md size %u\n", 1794 SPDK_BDEV_MAX_INTERLEAVED_MD_SIZE, spdk_bdev_get_md_size(ns->bdev)); 1795 spdk_bdev_close(ns->desc); 1796 free(ns); 1797 return 0; 1798 } 1799 } 1800 1801 rc = spdk_bdev_module_claim_bdev(ns->bdev, ns->desc, &ns_bdev_module); 1802 if (rc != 0) { 1803 spdk_bdev_close(ns->desc); 1804 free(ns); 1805 return 0; 1806 } 1807 1808 /* Cache the zcopy capability of the bdev device */ 1809 ns->zcopy = spdk_bdev_io_type_supported(ns->bdev, SPDK_BDEV_IO_TYPE_ZCOPY); 1810 1811 if (spdk_uuid_is_null(&opts.uuid)) { 1812 opts.uuid = *spdk_bdev_get_uuid(ns->bdev); 1813 } 1814 1815 /* if nguid descriptor is supported by bdev module (nvme) then uuid = nguid */ 1816 if (spdk_mem_all_zero(opts.nguid, sizeof(opts.nguid))) { 1817 SPDK_STATIC_ASSERT(sizeof(opts.nguid) == sizeof(opts.uuid), "size mismatch"); 1818 memcpy(opts.nguid, spdk_bdev_get_uuid(ns->bdev), sizeof(opts.nguid)); 1819 } 1820 1821 if (spdk_bdev_is_zoned(ns->bdev)) { 1822 SPDK_DEBUGLOG(nvmf, "The added namespace is backed by a zoned block device.\n"); 1823 ns->csi = SPDK_NVME_CSI_ZNS; 1824 1825 zone_append_supported = spdk_bdev_io_type_supported(ns->bdev, 1826 SPDK_BDEV_IO_TYPE_ZONE_APPEND); 1827 max_zone_append_size_kib = spdk_bdev_get_max_zone_append_size( 1828 ns->bdev) * spdk_bdev_get_block_size(ns->bdev); 1829 1830 if (_nvmf_subsystem_get_first_zoned_ns(subsystem) != NULL && 1831 (subsystem->zone_append_supported != zone_append_supported || 1832 subsystem->max_zone_append_size_kib != max_zone_append_size_kib)) { 1833 SPDK_ERRLOG("Namespaces with different zone append support or different zone append size are not allowed.\n"); 1834 goto err; 1835 } 1836 1837 subsystem->zone_append_supported = zone_append_supported; 1838 subsystem->max_zone_append_size_kib = max_zone_append_size_kib; 1839 } 1840 1841 ns->opts = opts; 1842 ns->subsystem = subsystem; 1843 subsystem->ns[opts.nsid - 1] = ns; 1844 ns->nsid = opts.nsid; 1845 ns->anagrpid = opts.anagrpid; 1846 subsystem->ana_group[ns->anagrpid - 1]++; 1847 TAILQ_INIT(&ns->registrants); 1848 if (ptpl_file) { 1849 ns->ptpl_file = strdup(ptpl_file); 1850 if (!ns->ptpl_file) { 1851 SPDK_ERRLOG("Namespace ns->ptpl_file allocation failed\n"); 1852 goto err; 1853 } 1854 1855 rc = nvmf_ns_load_reservation(ptpl_file, &info); 1856 if (!rc) { 1857 rc = nvmf_ns_reservation_restore(ns, &info); 1858 if (rc) { 1859 SPDK_ERRLOG("Subsystem restore reservation failed\n"); 1860 goto err; 1861 } 1862 } 1863 } 1864 1865 for (transport = spdk_nvmf_transport_get_first(subsystem->tgt); transport; 1866 transport = spdk_nvmf_transport_get_next(transport)) { 1867 if (transport->ops->subsystem_add_ns) { 1868 rc = transport->ops->subsystem_add_ns(transport, subsystem, ns); 1869 if (rc) { 1870 SPDK_ERRLOG("Namespace attachment is not allowed by %s transport\n", transport->ops->name); 1871 nvmf_ns_reservation_clear_all_registrants(ns); 1872 goto err; 1873 } 1874 } 1875 } 1876 1877 SPDK_DEBUGLOG(nvmf, "Subsystem %s: bdev %s assigned nsid %" PRIu32 "\n", 1878 spdk_nvmf_subsystem_get_nqn(subsystem), 1879 bdev_name, 1880 opts.nsid); 1881 1882 nvmf_subsystem_ns_changed(subsystem, opts.nsid); 1883 1884 SPDK_DTRACE_PROBE2(nvmf_subsystem_add_ns, subsystem->subnqn, ns->nsid); 1885 1886 return opts.nsid; 1887 err: 1888 subsystem->ns[opts.nsid - 1] = NULL; 1889 spdk_bdev_module_release_bdev(ns->bdev); 1890 spdk_bdev_close(ns->desc); 1891 free(ns->ptpl_file); 1892 free(ns); 1893 1894 return 0; 1895 } 1896 1897 static uint32_t 1898 nvmf_subsystem_get_next_allocated_nsid(struct spdk_nvmf_subsystem *subsystem, 1899 uint32_t prev_nsid) 1900 { 1901 uint32_t nsid; 1902 1903 if (prev_nsid >= subsystem->max_nsid) { 1904 return 0; 1905 } 1906 1907 for (nsid = prev_nsid + 1; nsid <= subsystem->max_nsid; nsid++) { 1908 if (subsystem->ns[nsid - 1]) { 1909 return nsid; 1910 } 1911 } 1912 1913 return 0; 1914 } 1915 1916 struct spdk_nvmf_ns * 1917 spdk_nvmf_subsystem_get_first_ns(struct spdk_nvmf_subsystem *subsystem) 1918 { 1919 uint32_t first_nsid; 1920 1921 first_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, 0); 1922 return _nvmf_subsystem_get_ns(subsystem, first_nsid); 1923 } 1924 1925 struct spdk_nvmf_ns * 1926 spdk_nvmf_subsystem_get_next_ns(struct spdk_nvmf_subsystem *subsystem, 1927 struct spdk_nvmf_ns *prev_ns) 1928 { 1929 uint32_t next_nsid; 1930 1931 next_nsid = nvmf_subsystem_get_next_allocated_nsid(subsystem, prev_ns->opts.nsid); 1932 return _nvmf_subsystem_get_ns(subsystem, next_nsid); 1933 } 1934 1935 struct spdk_nvmf_ns * 1936 spdk_nvmf_subsystem_get_ns(struct spdk_nvmf_subsystem *subsystem, uint32_t nsid) 1937 { 1938 return _nvmf_subsystem_get_ns(subsystem, nsid); 1939 } 1940 1941 uint32_t 1942 spdk_nvmf_ns_get_id(const struct spdk_nvmf_ns *ns) 1943 { 1944 return ns->opts.nsid; 1945 } 1946 1947 struct spdk_bdev * 1948 spdk_nvmf_ns_get_bdev(struct spdk_nvmf_ns *ns) 1949 { 1950 return ns->bdev; 1951 } 1952 1953 void 1954 spdk_nvmf_ns_get_opts(const struct spdk_nvmf_ns *ns, struct spdk_nvmf_ns_opts *opts, 1955 size_t opts_size) 1956 { 1957 memset(opts, 0, opts_size); 1958 memcpy(opts, &ns->opts, spdk_min(sizeof(ns->opts), opts_size)); 1959 } 1960 1961 const char * 1962 spdk_nvmf_subsystem_get_sn(const struct spdk_nvmf_subsystem *subsystem) 1963 { 1964 return subsystem->sn; 1965 } 1966 1967 int 1968 spdk_nvmf_subsystem_set_sn(struct spdk_nvmf_subsystem *subsystem, const char *sn) 1969 { 1970 size_t len, max_len; 1971 1972 max_len = sizeof(subsystem->sn) - 1; 1973 len = strlen(sn); 1974 if (len > max_len) { 1975 SPDK_DEBUGLOG(nvmf, "Invalid sn \"%s\": length %zu > max %zu\n", 1976 sn, len, max_len); 1977 return -1; 1978 } 1979 1980 if (!nvmf_valid_ascii_string(sn, len)) { 1981 SPDK_DEBUGLOG(nvmf, "Non-ASCII sn\n"); 1982 SPDK_LOGDUMP(nvmf, "sn", sn, len); 1983 return -1; 1984 } 1985 1986 snprintf(subsystem->sn, sizeof(subsystem->sn), "%s", sn); 1987 1988 return 0; 1989 } 1990 1991 const char * 1992 spdk_nvmf_subsystem_get_mn(const struct spdk_nvmf_subsystem *subsystem) 1993 { 1994 return subsystem->mn; 1995 } 1996 1997 int 1998 spdk_nvmf_subsystem_set_mn(struct spdk_nvmf_subsystem *subsystem, const char *mn) 1999 { 2000 size_t len, max_len; 2001 2002 if (mn == NULL) { 2003 mn = MODEL_NUMBER_DEFAULT; 2004 } 2005 max_len = sizeof(subsystem->mn) - 1; 2006 len = strlen(mn); 2007 if (len > max_len) { 2008 SPDK_DEBUGLOG(nvmf, "Invalid mn \"%s\": length %zu > max %zu\n", 2009 mn, len, max_len); 2010 return -1; 2011 } 2012 2013 if (!nvmf_valid_ascii_string(mn, len)) { 2014 SPDK_DEBUGLOG(nvmf, "Non-ASCII mn\n"); 2015 SPDK_LOGDUMP(nvmf, "mn", mn, len); 2016 return -1; 2017 } 2018 2019 snprintf(subsystem->mn, sizeof(subsystem->mn), "%s", mn); 2020 2021 return 0; 2022 } 2023 2024 const char * 2025 spdk_nvmf_subsystem_get_nqn(const struct spdk_nvmf_subsystem *subsystem) 2026 { 2027 return subsystem->subnqn; 2028 } 2029 2030 /* We have to use the typedef in the function declaration to appease astyle. */ 2031 typedef enum spdk_nvmf_subtype spdk_nvmf_subtype_t; 2032 2033 spdk_nvmf_subtype_t 2034 spdk_nvmf_subsystem_get_type(struct spdk_nvmf_subsystem *subsystem) 2035 { 2036 return subsystem->subtype; 2037 } 2038 2039 uint32_t 2040 spdk_nvmf_subsystem_get_max_nsid(struct spdk_nvmf_subsystem *subsystem) 2041 { 2042 return subsystem->max_nsid; 2043 } 2044 2045 int 2046 nvmf_subsystem_set_cntlid_range(struct spdk_nvmf_subsystem *subsystem, 2047 uint16_t min_cntlid, uint16_t max_cntlid) 2048 { 2049 if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) { 2050 return -EAGAIN; 2051 } 2052 2053 if (min_cntlid > max_cntlid) { 2054 return -EINVAL; 2055 } 2056 /* The spec reserves cntlid values in the range FFF0h to FFFFh. */ 2057 if (min_cntlid < NVMF_MIN_CNTLID || min_cntlid > NVMF_MAX_CNTLID || 2058 max_cntlid < NVMF_MIN_CNTLID || max_cntlid > NVMF_MAX_CNTLID) { 2059 return -EINVAL; 2060 } 2061 subsystem->min_cntlid = min_cntlid; 2062 subsystem->max_cntlid = max_cntlid; 2063 if (subsystem->next_cntlid < min_cntlid || subsystem->next_cntlid > max_cntlid - 1) { 2064 subsystem->next_cntlid = min_cntlid - 1; 2065 } 2066 2067 return 0; 2068 } 2069 2070 static uint16_t 2071 nvmf_subsystem_gen_cntlid(struct spdk_nvmf_subsystem *subsystem) 2072 { 2073 int count; 2074 2075 /* 2076 * In the worst case, we might have to try all CNTLID values between min_cntlid and max_cntlid 2077 * before we find one that is unused (or find that all values are in use). 2078 */ 2079 for (count = 0; count < subsystem->max_cntlid - subsystem->min_cntlid + 1; count++) { 2080 subsystem->next_cntlid++; 2081 if (subsystem->next_cntlid > subsystem->max_cntlid) { 2082 subsystem->next_cntlid = subsystem->min_cntlid; 2083 } 2084 2085 /* Check if a controller with this cntlid currently exists. */ 2086 if (nvmf_subsystem_get_ctrlr(subsystem, subsystem->next_cntlid) == NULL) { 2087 /* Found unused cntlid */ 2088 return subsystem->next_cntlid; 2089 } 2090 } 2091 2092 /* All valid cntlid values are in use. */ 2093 return 0xFFFF; 2094 } 2095 2096 int 2097 nvmf_subsystem_add_ctrlr(struct spdk_nvmf_subsystem *subsystem, struct spdk_nvmf_ctrlr *ctrlr) 2098 { 2099 2100 if (ctrlr->dynamic_ctrlr) { 2101 ctrlr->cntlid = nvmf_subsystem_gen_cntlid(subsystem); 2102 if (ctrlr->cntlid == 0xFFFF) { 2103 /* Unable to get a cntlid */ 2104 SPDK_ERRLOG("Reached max simultaneous ctrlrs\n"); 2105 return -EBUSY; 2106 } 2107 } else if (nvmf_subsystem_get_ctrlr(subsystem, ctrlr->cntlid) != NULL) { 2108 SPDK_ERRLOG("Ctrlr with cntlid %u already exist\n", ctrlr->cntlid); 2109 return -EEXIST; 2110 } 2111 2112 TAILQ_INSERT_TAIL(&subsystem->ctrlrs, ctrlr, link); 2113 2114 SPDK_DTRACE_PROBE3(nvmf_subsystem_add_ctrlr, subsystem->subnqn, ctrlr, ctrlr->hostnqn); 2115 2116 return 0; 2117 } 2118 2119 void 2120 nvmf_subsystem_remove_ctrlr(struct spdk_nvmf_subsystem *subsystem, 2121 struct spdk_nvmf_ctrlr *ctrlr) 2122 { 2123 SPDK_DTRACE_PROBE3(nvmf_subsystem_remove_ctrlr, subsystem->subnqn, ctrlr, ctrlr->hostnqn); 2124 2125 assert(spdk_get_thread() == subsystem->thread); 2126 assert(subsystem == ctrlr->subsys); 2127 SPDK_DEBUGLOG(nvmf, "remove ctrlr %p id 0x%x from subsys %p %s\n", ctrlr, ctrlr->cntlid, subsystem, 2128 subsystem->subnqn); 2129 TAILQ_REMOVE(&subsystem->ctrlrs, ctrlr, link); 2130 } 2131 2132 struct spdk_nvmf_ctrlr * 2133 nvmf_subsystem_get_ctrlr(struct spdk_nvmf_subsystem *subsystem, uint16_t cntlid) 2134 { 2135 struct spdk_nvmf_ctrlr *ctrlr; 2136 2137 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 2138 if (ctrlr->cntlid == cntlid) { 2139 return ctrlr; 2140 } 2141 } 2142 2143 return NULL; 2144 } 2145 2146 uint32_t 2147 spdk_nvmf_subsystem_get_max_namespaces(const struct spdk_nvmf_subsystem *subsystem) 2148 { 2149 return subsystem->max_nsid; 2150 } 2151 2152 uint16_t 2153 spdk_nvmf_subsystem_get_min_cntlid(const struct spdk_nvmf_subsystem *subsystem) 2154 { 2155 return subsystem->min_cntlid; 2156 } 2157 2158 uint16_t 2159 spdk_nvmf_subsystem_get_max_cntlid(const struct spdk_nvmf_subsystem *subsystem) 2160 { 2161 return subsystem->max_cntlid; 2162 } 2163 2164 struct _nvmf_ns_registrant { 2165 uint64_t rkey; 2166 char *host_uuid; 2167 }; 2168 2169 struct _nvmf_ns_registrants { 2170 size_t num_regs; 2171 struct _nvmf_ns_registrant reg[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2172 }; 2173 2174 struct _nvmf_ns_reservation { 2175 bool ptpl_activated; 2176 enum spdk_nvme_reservation_type rtype; 2177 uint64_t crkey; 2178 char *bdev_uuid; 2179 char *holder_uuid; 2180 struct _nvmf_ns_registrants regs; 2181 }; 2182 2183 static const struct spdk_json_object_decoder nvmf_ns_pr_reg_decoders[] = { 2184 {"rkey", offsetof(struct _nvmf_ns_registrant, rkey), spdk_json_decode_uint64}, 2185 {"host_uuid", offsetof(struct _nvmf_ns_registrant, host_uuid), spdk_json_decode_string}, 2186 }; 2187 2188 static int 2189 nvmf_decode_ns_pr_reg(const struct spdk_json_val *val, void *out) 2190 { 2191 struct _nvmf_ns_registrant *reg = out; 2192 2193 return spdk_json_decode_object(val, nvmf_ns_pr_reg_decoders, 2194 SPDK_COUNTOF(nvmf_ns_pr_reg_decoders), reg); 2195 } 2196 2197 static int 2198 nvmf_decode_ns_pr_regs(const struct spdk_json_val *val, void *out) 2199 { 2200 struct _nvmf_ns_registrants *regs = out; 2201 2202 return spdk_json_decode_array(val, nvmf_decode_ns_pr_reg, regs->reg, 2203 SPDK_NVMF_MAX_NUM_REGISTRANTS, ®s->num_regs, 2204 sizeof(struct _nvmf_ns_registrant)); 2205 } 2206 2207 static const struct spdk_json_object_decoder nvmf_ns_pr_decoders[] = { 2208 {"ptpl", offsetof(struct _nvmf_ns_reservation, ptpl_activated), spdk_json_decode_bool, true}, 2209 {"rtype", offsetof(struct _nvmf_ns_reservation, rtype), spdk_json_decode_uint32, true}, 2210 {"crkey", offsetof(struct _nvmf_ns_reservation, crkey), spdk_json_decode_uint64, true}, 2211 {"bdev_uuid", offsetof(struct _nvmf_ns_reservation, bdev_uuid), spdk_json_decode_string}, 2212 {"holder_uuid", offsetof(struct _nvmf_ns_reservation, holder_uuid), spdk_json_decode_string, true}, 2213 {"registrants", offsetof(struct _nvmf_ns_reservation, regs), nvmf_decode_ns_pr_regs}, 2214 }; 2215 2216 static int 2217 nvmf_ns_load_reservation(const char *file, struct spdk_nvmf_reservation_info *info) 2218 { 2219 FILE *fd; 2220 size_t json_size; 2221 ssize_t values_cnt, rc; 2222 void *json = NULL, *end; 2223 struct spdk_json_val *values = NULL; 2224 struct _nvmf_ns_reservation res = {}; 2225 uint32_t i; 2226 2227 fd = fopen(file, "r"); 2228 /* It's not an error if the file does not exist */ 2229 if (!fd) { 2230 SPDK_NOTICELOG("File %s does not exist\n", file); 2231 return -ENOENT; 2232 } 2233 2234 /* Load all persist file contents into a local buffer */ 2235 json = spdk_posix_file_load(fd, &json_size); 2236 fclose(fd); 2237 if (!json) { 2238 SPDK_ERRLOG("Load persit file %s failed\n", file); 2239 return -ENOMEM; 2240 } 2241 2242 rc = spdk_json_parse(json, json_size, NULL, 0, &end, 0); 2243 if (rc < 0) { 2244 SPDK_NOTICELOG("Parsing JSON configuration failed (%zd)\n", rc); 2245 goto exit; 2246 } 2247 2248 values_cnt = rc; 2249 values = calloc(values_cnt, sizeof(struct spdk_json_val)); 2250 if (values == NULL) { 2251 goto exit; 2252 } 2253 2254 rc = spdk_json_parse(json, json_size, values, values_cnt, &end, 0); 2255 if (rc != values_cnt) { 2256 SPDK_ERRLOG("Parsing JSON configuration failed (%zd)\n", rc); 2257 goto exit; 2258 } 2259 2260 /* Decode json */ 2261 if (spdk_json_decode_object(values, nvmf_ns_pr_decoders, 2262 SPDK_COUNTOF(nvmf_ns_pr_decoders), 2263 &res)) { 2264 SPDK_ERRLOG("Invalid objects in the persist file %s\n", file); 2265 rc = -EINVAL; 2266 goto exit; 2267 } 2268 2269 if (res.regs.num_regs > SPDK_NVMF_MAX_NUM_REGISTRANTS) { 2270 SPDK_ERRLOG("Can only support up to %u registrants\n", SPDK_NVMF_MAX_NUM_REGISTRANTS); 2271 rc = -ERANGE; 2272 goto exit; 2273 } 2274 2275 rc = 0; 2276 info->ptpl_activated = res.ptpl_activated; 2277 info->rtype = res.rtype; 2278 info->crkey = res.crkey; 2279 snprintf(info->bdev_uuid, sizeof(info->bdev_uuid), "%s", res.bdev_uuid); 2280 snprintf(info->holder_uuid, sizeof(info->holder_uuid), "%s", res.holder_uuid); 2281 info->num_regs = res.regs.num_regs; 2282 for (i = 0; i < res.regs.num_regs; i++) { 2283 info->registrants[i].rkey = res.regs.reg[i].rkey; 2284 snprintf(info->registrants[i].host_uuid, sizeof(info->registrants[i].host_uuid), "%s", 2285 res.regs.reg[i].host_uuid); 2286 } 2287 2288 exit: 2289 free(json); 2290 free(values); 2291 free(res.bdev_uuid); 2292 free(res.holder_uuid); 2293 for (i = 0; i < res.regs.num_regs; i++) { 2294 free(res.regs.reg[i].host_uuid); 2295 } 2296 2297 return rc; 2298 } 2299 2300 static bool nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns); 2301 2302 static int 2303 nvmf_ns_reservation_restore(struct spdk_nvmf_ns *ns, struct spdk_nvmf_reservation_info *info) 2304 { 2305 uint32_t i; 2306 struct spdk_nvmf_registrant *reg, *holder = NULL; 2307 struct spdk_uuid bdev_uuid, holder_uuid; 2308 bool rkey_flag = false; 2309 2310 SPDK_DEBUGLOG(nvmf, "NSID %u, PTPL %u, Number of registrants %u\n", 2311 ns->nsid, info->ptpl_activated, info->num_regs); 2312 2313 /* it's not an error */ 2314 if (!info->ptpl_activated || !info->num_regs) { 2315 return 0; 2316 } 2317 2318 /* Check info->crkey exist or not in info->registrants[i].rkey */ 2319 for (i = 0; i < info->num_regs; i++) { 2320 if (info->crkey == info->registrants[i].rkey) { 2321 rkey_flag = true; 2322 } 2323 } 2324 if (!rkey_flag && info->crkey != 0) { 2325 return -EINVAL; 2326 } 2327 2328 spdk_uuid_parse(&bdev_uuid, info->bdev_uuid); 2329 if (spdk_uuid_compare(&bdev_uuid, spdk_bdev_get_uuid(ns->bdev))) { 2330 SPDK_ERRLOG("Existing bdev UUID is not same with configuration file\n"); 2331 return -EINVAL; 2332 } 2333 2334 ns->crkey = info->crkey; 2335 ns->rtype = info->rtype; 2336 ns->ptpl_activated = info->ptpl_activated; 2337 spdk_uuid_parse(&holder_uuid, info->holder_uuid); 2338 2339 SPDK_DEBUGLOG(nvmf, "Bdev UUID %s\n", info->bdev_uuid); 2340 if (info->rtype) { 2341 SPDK_DEBUGLOG(nvmf, "Holder UUID %s, RTYPE %u, RKEY 0x%"PRIx64"\n", 2342 info->holder_uuid, info->rtype, info->crkey); 2343 } 2344 2345 for (i = 0; i < info->num_regs; i++) { 2346 reg = calloc(1, sizeof(*reg)); 2347 if (!reg) { 2348 return -ENOMEM; 2349 } 2350 spdk_uuid_parse(®->hostid, info->registrants[i].host_uuid); 2351 reg->rkey = info->registrants[i].rkey; 2352 TAILQ_INSERT_TAIL(&ns->registrants, reg, link); 2353 if (info->crkey != 0 && !spdk_uuid_compare(&holder_uuid, ®->hostid)) { 2354 holder = reg; 2355 } 2356 SPDK_DEBUGLOG(nvmf, "Registrant RKEY 0x%"PRIx64", Host UUID %s\n", 2357 info->registrants[i].rkey, info->registrants[i].host_uuid); 2358 } 2359 2360 if (nvmf_ns_reservation_all_registrants_type(ns)) { 2361 ns->holder = TAILQ_FIRST(&ns->registrants); 2362 } else { 2363 ns->holder = holder; 2364 } 2365 2366 return 0; 2367 } 2368 2369 static int 2370 nvmf_ns_json_write_cb(void *cb_ctx, const void *data, size_t size) 2371 { 2372 char *file = cb_ctx; 2373 size_t rc; 2374 FILE *fd; 2375 2376 fd = fopen(file, "w"); 2377 if (!fd) { 2378 SPDK_ERRLOG("Can't open file %s for write\n", file); 2379 return -ENOENT; 2380 } 2381 rc = fwrite(data, 1, size, fd); 2382 fclose(fd); 2383 2384 return rc == size ? 0 : -1; 2385 } 2386 2387 static int 2388 nvmf_ns_reservation_update(const char *file, struct spdk_nvmf_reservation_info *info) 2389 { 2390 struct spdk_json_write_ctx *w; 2391 uint32_t i; 2392 int rc = 0; 2393 2394 w = spdk_json_write_begin(nvmf_ns_json_write_cb, (void *)file, 0); 2395 if (w == NULL) { 2396 return -ENOMEM; 2397 } 2398 /* clear the configuration file */ 2399 if (!info->ptpl_activated) { 2400 goto exit; 2401 } 2402 2403 spdk_json_write_object_begin(w); 2404 spdk_json_write_named_bool(w, "ptpl", info->ptpl_activated); 2405 spdk_json_write_named_uint32(w, "rtype", info->rtype); 2406 spdk_json_write_named_uint64(w, "crkey", info->crkey); 2407 spdk_json_write_named_string(w, "bdev_uuid", info->bdev_uuid); 2408 spdk_json_write_named_string(w, "holder_uuid", info->holder_uuid); 2409 2410 spdk_json_write_named_array_begin(w, "registrants"); 2411 for (i = 0; i < info->num_regs; i++) { 2412 spdk_json_write_object_begin(w); 2413 spdk_json_write_named_uint64(w, "rkey", info->registrants[i].rkey); 2414 spdk_json_write_named_string(w, "host_uuid", info->registrants[i].host_uuid); 2415 spdk_json_write_object_end(w); 2416 } 2417 spdk_json_write_array_end(w); 2418 spdk_json_write_object_end(w); 2419 2420 exit: 2421 rc = spdk_json_write_end(w); 2422 return rc; 2423 } 2424 2425 static int 2426 nvmf_ns_update_reservation_info(struct spdk_nvmf_ns *ns) 2427 { 2428 struct spdk_nvmf_reservation_info info; 2429 struct spdk_nvmf_registrant *reg, *tmp; 2430 uint32_t i = 0; 2431 2432 assert(ns != NULL); 2433 2434 if (!ns->bdev || !nvmf_ns_is_ptpl_capable(ns)) { 2435 return 0; 2436 } 2437 2438 memset(&info, 0, sizeof(info)); 2439 spdk_uuid_fmt_lower(info.bdev_uuid, sizeof(info.bdev_uuid), spdk_bdev_get_uuid(ns->bdev)); 2440 2441 if (ns->rtype) { 2442 info.rtype = ns->rtype; 2443 info.crkey = ns->crkey; 2444 if (!nvmf_ns_reservation_all_registrants_type(ns)) { 2445 assert(ns->holder != NULL); 2446 spdk_uuid_fmt_lower(info.holder_uuid, sizeof(info.holder_uuid), &ns->holder->hostid); 2447 } 2448 } 2449 2450 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 2451 spdk_uuid_fmt_lower(info.registrants[i].host_uuid, sizeof(info.registrants[i].host_uuid), 2452 ®->hostid); 2453 info.registrants[i++].rkey = reg->rkey; 2454 } 2455 2456 info.num_regs = i; 2457 info.ptpl_activated = ns->ptpl_activated; 2458 2459 return nvmf_ns_reservation_update(ns->ptpl_file, &info); 2460 } 2461 2462 static struct spdk_nvmf_registrant * 2463 nvmf_ns_reservation_get_registrant(struct spdk_nvmf_ns *ns, 2464 struct spdk_uuid *uuid) 2465 { 2466 struct spdk_nvmf_registrant *reg, *tmp; 2467 2468 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 2469 if (!spdk_uuid_compare(®->hostid, uuid)) { 2470 return reg; 2471 } 2472 } 2473 2474 return NULL; 2475 } 2476 2477 /* Generate reservation notice log to registered HostID controllers */ 2478 static void 2479 nvmf_subsystem_gen_ctrlr_notification(struct spdk_nvmf_subsystem *subsystem, 2480 struct spdk_nvmf_ns *ns, 2481 struct spdk_uuid *hostid_list, 2482 uint32_t num_hostid, 2483 enum spdk_nvme_reservation_notification_log_page_type type) 2484 { 2485 struct spdk_nvmf_ctrlr *ctrlr; 2486 uint32_t i; 2487 2488 for (i = 0; i < num_hostid; i++) { 2489 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) { 2490 if (!spdk_uuid_compare(&ctrlr->hostid, &hostid_list[i])) { 2491 nvmf_ctrlr_reservation_notice_log(ctrlr, ns, type); 2492 } 2493 } 2494 } 2495 } 2496 2497 /* Get all registrants' hostid other than the controller who issued the command */ 2498 static uint32_t 2499 nvmf_ns_reservation_get_all_other_hostid(struct spdk_nvmf_ns *ns, 2500 struct spdk_uuid *hostid_list, 2501 uint32_t max_num_hostid, 2502 struct spdk_uuid *current_hostid) 2503 { 2504 struct spdk_nvmf_registrant *reg, *tmp; 2505 uint32_t num_hostid = 0; 2506 2507 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 2508 if (spdk_uuid_compare(®->hostid, current_hostid)) { 2509 if (num_hostid == max_num_hostid) { 2510 assert(false); 2511 return max_num_hostid; 2512 } 2513 hostid_list[num_hostid++] = reg->hostid; 2514 } 2515 } 2516 2517 return num_hostid; 2518 } 2519 2520 /* Calculate the unregistered HostID list according to list 2521 * prior to execute preempt command and list after executing 2522 * preempt command. 2523 */ 2524 static uint32_t 2525 nvmf_ns_reservation_get_unregistered_hostid(struct spdk_uuid *old_hostid_list, 2526 uint32_t old_num_hostid, 2527 struct spdk_uuid *remaining_hostid_list, 2528 uint32_t remaining_num_hostid) 2529 { 2530 struct spdk_uuid temp_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2531 uint32_t i, j, num_hostid = 0; 2532 bool found; 2533 2534 if (!remaining_num_hostid) { 2535 return old_num_hostid; 2536 } 2537 2538 for (i = 0; i < old_num_hostid; i++) { 2539 found = false; 2540 for (j = 0; j < remaining_num_hostid; j++) { 2541 if (!spdk_uuid_compare(&old_hostid_list[i], &remaining_hostid_list[j])) { 2542 found = true; 2543 break; 2544 } 2545 } 2546 if (!found) { 2547 spdk_uuid_copy(&temp_hostid_list[num_hostid++], &old_hostid_list[i]); 2548 } 2549 } 2550 2551 if (num_hostid) { 2552 memcpy(old_hostid_list, temp_hostid_list, sizeof(struct spdk_uuid) * num_hostid); 2553 } 2554 2555 return num_hostid; 2556 } 2557 2558 /* current reservation type is all registrants or not */ 2559 static bool 2560 nvmf_ns_reservation_all_registrants_type(struct spdk_nvmf_ns *ns) 2561 { 2562 return (ns->rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_ALL_REGS || 2563 ns->rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS); 2564 } 2565 2566 /* current registrant is reservation holder or not */ 2567 static bool 2568 nvmf_ns_reservation_registrant_is_holder(struct spdk_nvmf_ns *ns, 2569 struct spdk_nvmf_registrant *reg) 2570 { 2571 if (!reg) { 2572 return false; 2573 } 2574 2575 if (nvmf_ns_reservation_all_registrants_type(ns)) { 2576 return true; 2577 } 2578 2579 return (ns->holder == reg); 2580 } 2581 2582 static int 2583 nvmf_ns_reservation_add_registrant(struct spdk_nvmf_ns *ns, 2584 struct spdk_nvmf_ctrlr *ctrlr, 2585 uint64_t nrkey) 2586 { 2587 struct spdk_nvmf_registrant *reg; 2588 2589 reg = calloc(1, sizeof(*reg)); 2590 if (!reg) { 2591 return -ENOMEM; 2592 } 2593 2594 reg->rkey = nrkey; 2595 /* set hostid for the registrant */ 2596 spdk_uuid_copy(®->hostid, &ctrlr->hostid); 2597 TAILQ_INSERT_TAIL(&ns->registrants, reg, link); 2598 ns->gen++; 2599 2600 return 0; 2601 } 2602 2603 static void 2604 nvmf_ns_reservation_release_reservation(struct spdk_nvmf_ns *ns) 2605 { 2606 ns->rtype = 0; 2607 ns->crkey = 0; 2608 ns->holder = NULL; 2609 } 2610 2611 /* release the reservation if the last registrant was removed */ 2612 static void 2613 nvmf_ns_reservation_check_release_on_remove_registrant(struct spdk_nvmf_ns *ns, 2614 struct spdk_nvmf_registrant *reg) 2615 { 2616 struct spdk_nvmf_registrant *next_reg; 2617 2618 /* no reservation holder */ 2619 if (!ns->holder) { 2620 assert(ns->rtype == 0); 2621 return; 2622 } 2623 2624 next_reg = TAILQ_FIRST(&ns->registrants); 2625 if (next_reg && nvmf_ns_reservation_all_registrants_type(ns)) { 2626 /* the next valid registrant is the new holder now */ 2627 ns->holder = next_reg; 2628 } else if (nvmf_ns_reservation_registrant_is_holder(ns, reg)) { 2629 /* release the reservation */ 2630 nvmf_ns_reservation_release_reservation(ns); 2631 } 2632 } 2633 2634 static void 2635 nvmf_ns_reservation_remove_registrant(struct spdk_nvmf_ns *ns, 2636 struct spdk_nvmf_registrant *reg) 2637 { 2638 TAILQ_REMOVE(&ns->registrants, reg, link); 2639 nvmf_ns_reservation_check_release_on_remove_registrant(ns, reg); 2640 free(reg); 2641 ns->gen++; 2642 return; 2643 } 2644 2645 static uint32_t 2646 nvmf_ns_reservation_remove_registrants_by_key(struct spdk_nvmf_ns *ns, 2647 uint64_t rkey) 2648 { 2649 struct spdk_nvmf_registrant *reg, *tmp; 2650 uint32_t count = 0; 2651 2652 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 2653 if (reg->rkey == rkey) { 2654 nvmf_ns_reservation_remove_registrant(ns, reg); 2655 count++; 2656 } 2657 } 2658 return count; 2659 } 2660 2661 static uint32_t 2662 nvmf_ns_reservation_remove_all_other_registrants(struct spdk_nvmf_ns *ns, 2663 struct spdk_nvmf_registrant *reg) 2664 { 2665 struct spdk_nvmf_registrant *reg_tmp, *reg_tmp2; 2666 uint32_t count = 0; 2667 2668 TAILQ_FOREACH_SAFE(reg_tmp, &ns->registrants, link, reg_tmp2) { 2669 if (reg_tmp != reg) { 2670 nvmf_ns_reservation_remove_registrant(ns, reg_tmp); 2671 count++; 2672 } 2673 } 2674 return count; 2675 } 2676 2677 static uint32_t 2678 nvmf_ns_reservation_clear_all_registrants(struct spdk_nvmf_ns *ns) 2679 { 2680 struct spdk_nvmf_registrant *reg, *reg_tmp; 2681 uint32_t count = 0; 2682 2683 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, reg_tmp) { 2684 nvmf_ns_reservation_remove_registrant(ns, reg); 2685 count++; 2686 } 2687 return count; 2688 } 2689 2690 static void 2691 nvmf_ns_reservation_acquire_reservation(struct spdk_nvmf_ns *ns, uint64_t rkey, 2692 enum spdk_nvme_reservation_type rtype, 2693 struct spdk_nvmf_registrant *holder) 2694 { 2695 ns->rtype = rtype; 2696 ns->crkey = rkey; 2697 assert(ns->holder == NULL); 2698 ns->holder = holder; 2699 } 2700 2701 static bool 2702 nvmf_ns_reservation_register(struct spdk_nvmf_ns *ns, 2703 struct spdk_nvmf_ctrlr *ctrlr, 2704 struct spdk_nvmf_request *req) 2705 { 2706 struct spdk_nvme_reservation_register_data key = { 0 }; 2707 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2708 uint8_t rrega, iekey, cptpl, rtype; 2709 struct spdk_nvmf_registrant *reg; 2710 uint8_t status = SPDK_NVME_SC_SUCCESS; 2711 bool update_sgroup = false; 2712 struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2713 uint32_t num_hostid = 0; 2714 int rc; 2715 2716 rrega = cmd->cdw10_bits.resv_register.rrega; 2717 iekey = cmd->cdw10_bits.resv_register.iekey; 2718 cptpl = cmd->cdw10_bits.resv_register.cptpl; 2719 2720 if (req->iovcnt > 0 && req->length >= sizeof(key)) { 2721 struct spdk_iov_xfer ix; 2722 spdk_iov_xfer_init(&ix, req->iov, req->iovcnt); 2723 spdk_iov_xfer_to_buf(&ix, &key, sizeof(key)); 2724 } else { 2725 SPDK_ERRLOG("No key provided. Failing request.\n"); 2726 status = SPDK_NVME_SC_INVALID_FIELD; 2727 goto exit; 2728 } 2729 2730 SPDK_DEBUGLOG(nvmf, "REGISTER: RREGA %u, IEKEY %u, CPTPL %u, " 2731 "NRKEY 0x%"PRIx64", NRKEY 0x%"PRIx64"\n", 2732 rrega, iekey, cptpl, key.crkey, key.nrkey); 2733 2734 if (cptpl == SPDK_NVME_RESERVE_PTPL_CLEAR_POWER_ON) { 2735 /* Ture to OFF state, and need to be updated in the configuration file */ 2736 if (ns->ptpl_activated) { 2737 ns->ptpl_activated = 0; 2738 update_sgroup = true; 2739 } 2740 } else if (cptpl == SPDK_NVME_RESERVE_PTPL_PERSIST_POWER_LOSS) { 2741 if (!nvmf_ns_is_ptpl_capable(ns)) { 2742 status = SPDK_NVME_SC_INVALID_FIELD; 2743 goto exit; 2744 } else if (ns->ptpl_activated == 0) { 2745 ns->ptpl_activated = 1; 2746 update_sgroup = true; 2747 } 2748 } 2749 2750 /* current Host Identifier has registrant or not */ 2751 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid); 2752 2753 switch (rrega) { 2754 case SPDK_NVME_RESERVE_REGISTER_KEY: 2755 if (!reg) { 2756 /* register new controller */ 2757 if (key.nrkey == 0) { 2758 SPDK_ERRLOG("Can't register zeroed new key\n"); 2759 status = SPDK_NVME_SC_INVALID_FIELD; 2760 goto exit; 2761 } 2762 rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey); 2763 if (rc < 0) { 2764 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2765 goto exit; 2766 } 2767 update_sgroup = true; 2768 } else { 2769 /* register with same key is not an error */ 2770 if (reg->rkey != key.nrkey) { 2771 SPDK_ERRLOG("The same host already register a " 2772 "key with 0x%"PRIx64"\n", 2773 reg->rkey); 2774 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2775 goto exit; 2776 } 2777 } 2778 break; 2779 case SPDK_NVME_RESERVE_UNREGISTER_KEY: 2780 if (!reg || (!iekey && reg->rkey != key.crkey)) { 2781 SPDK_ERRLOG("No registrant or current key doesn't match " 2782 "with existing registrant key\n"); 2783 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2784 goto exit; 2785 } 2786 2787 rtype = ns->rtype; 2788 num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list, 2789 SPDK_NVMF_MAX_NUM_REGISTRANTS, 2790 &ctrlr->hostid); 2791 2792 nvmf_ns_reservation_remove_registrant(ns, reg); 2793 2794 if (!ns->rtype && num_hostid && (rtype == SPDK_NVME_RESERVE_WRITE_EXCLUSIVE_REG_ONLY || 2795 rtype == SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_REG_ONLY)) { 2796 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2797 hostid_list, 2798 num_hostid, 2799 SPDK_NVME_RESERVATION_RELEASED); 2800 } 2801 update_sgroup = true; 2802 break; 2803 case SPDK_NVME_RESERVE_REPLACE_KEY: 2804 if (key.nrkey == 0) { 2805 SPDK_ERRLOG("Can't register zeroed new key\n"); 2806 status = SPDK_NVME_SC_INVALID_FIELD; 2807 goto exit; 2808 } 2809 /* Registrant exists */ 2810 if (reg) { 2811 if (!iekey && reg->rkey != key.crkey) { 2812 SPDK_ERRLOG("Current key doesn't match " 2813 "existing registrant key\n"); 2814 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2815 goto exit; 2816 } 2817 if (reg->rkey == key.nrkey) { 2818 goto exit; 2819 } 2820 reg->rkey = key.nrkey; 2821 } else if (iekey) { /* No registrant but IEKEY is set */ 2822 /* new registrant */ 2823 rc = nvmf_ns_reservation_add_registrant(ns, ctrlr, key.nrkey); 2824 if (rc < 0) { 2825 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 2826 goto exit; 2827 } 2828 } else { /* No registrant */ 2829 SPDK_ERRLOG("No registrant\n"); 2830 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2831 goto exit; 2832 2833 } 2834 update_sgroup = true; 2835 break; 2836 default: 2837 status = SPDK_NVME_SC_INVALID_FIELD; 2838 goto exit; 2839 } 2840 2841 exit: 2842 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 2843 req->rsp->nvme_cpl.status.sc = status; 2844 return update_sgroup; 2845 } 2846 2847 static bool 2848 nvmf_ns_reservation_acquire(struct spdk_nvmf_ns *ns, 2849 struct spdk_nvmf_ctrlr *ctrlr, 2850 struct spdk_nvmf_request *req) 2851 { 2852 struct spdk_nvme_reservation_acquire_data key = { 0 }; 2853 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 2854 uint8_t racqa, iekey, rtype; 2855 struct spdk_nvmf_registrant *reg; 2856 bool all_regs = false; 2857 uint32_t count = 0; 2858 bool update_sgroup = true; 2859 struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2860 uint32_t num_hostid = 0; 2861 struct spdk_uuid new_hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 2862 uint32_t new_num_hostid = 0; 2863 bool reservation_released = false; 2864 uint8_t status = SPDK_NVME_SC_SUCCESS; 2865 2866 racqa = cmd->cdw10_bits.resv_acquire.racqa; 2867 iekey = cmd->cdw10_bits.resv_acquire.iekey; 2868 rtype = cmd->cdw10_bits.resv_acquire.rtype; 2869 2870 if (req->iovcnt > 0 && req->length >= sizeof(key)) { 2871 struct spdk_iov_xfer ix; 2872 spdk_iov_xfer_init(&ix, req->iov, req->iovcnt); 2873 spdk_iov_xfer_to_buf(&ix, &key, sizeof(key)); 2874 } else { 2875 SPDK_ERRLOG("No key provided. Failing request.\n"); 2876 status = SPDK_NVME_SC_INVALID_FIELD; 2877 goto exit; 2878 } 2879 2880 SPDK_DEBUGLOG(nvmf, "ACQUIRE: RACQA %u, IEKEY %u, RTYPE %u, " 2881 "NRKEY 0x%"PRIx64", PRKEY 0x%"PRIx64"\n", 2882 racqa, iekey, rtype, key.crkey, key.prkey); 2883 2884 if (iekey || rtype > SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS_ALL_REGS) { 2885 SPDK_ERRLOG("Ignore existing key field set to 1\n"); 2886 status = SPDK_NVME_SC_INVALID_FIELD; 2887 update_sgroup = false; 2888 goto exit; 2889 } 2890 2891 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid); 2892 /* must be registrant and CRKEY must match */ 2893 if (!reg || reg->rkey != key.crkey) { 2894 SPDK_ERRLOG("No registrant or current key doesn't match " 2895 "with existing registrant key\n"); 2896 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2897 update_sgroup = false; 2898 goto exit; 2899 } 2900 2901 all_regs = nvmf_ns_reservation_all_registrants_type(ns); 2902 2903 switch (racqa) { 2904 case SPDK_NVME_RESERVE_ACQUIRE: 2905 /* it's not an error for the holder to acquire same reservation type again */ 2906 if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && ns->rtype == rtype) { 2907 /* do nothing */ 2908 update_sgroup = false; 2909 } else if (ns->holder == NULL) { 2910 /* first time to acquire the reservation */ 2911 nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg); 2912 } else { 2913 SPDK_ERRLOG("Invalid rtype or current registrant is not holder\n"); 2914 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2915 update_sgroup = false; 2916 goto exit; 2917 } 2918 break; 2919 case SPDK_NVME_RESERVE_PREEMPT: 2920 /* no reservation holder */ 2921 if (!ns->holder) { 2922 /* unregister with PRKEY */ 2923 nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey); 2924 break; 2925 } 2926 num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list, 2927 SPDK_NVMF_MAX_NUM_REGISTRANTS, 2928 &ctrlr->hostid); 2929 2930 /* only 1 reservation holder and reservation key is valid */ 2931 if (!all_regs) { 2932 /* preempt itself */ 2933 if (nvmf_ns_reservation_registrant_is_holder(ns, reg) && 2934 ns->crkey == key.prkey) { 2935 ns->rtype = rtype; 2936 reservation_released = true; 2937 break; 2938 } 2939 2940 if (ns->crkey == key.prkey) { 2941 nvmf_ns_reservation_remove_registrant(ns, ns->holder); 2942 nvmf_ns_reservation_acquire_reservation(ns, key.crkey, rtype, reg); 2943 reservation_released = true; 2944 } else if (key.prkey != 0) { 2945 nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey); 2946 } else { 2947 /* PRKEY is zero */ 2948 SPDK_ERRLOG("Current PRKEY is zero\n"); 2949 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2950 update_sgroup = false; 2951 goto exit; 2952 } 2953 } else { 2954 /* release all other registrants except for the current one */ 2955 if (key.prkey == 0) { 2956 nvmf_ns_reservation_remove_all_other_registrants(ns, reg); 2957 assert(ns->holder == reg); 2958 } else { 2959 count = nvmf_ns_reservation_remove_registrants_by_key(ns, key.prkey); 2960 if (count == 0) { 2961 SPDK_ERRLOG("PRKEY doesn't match any registrant\n"); 2962 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 2963 update_sgroup = false; 2964 goto exit; 2965 } 2966 } 2967 } 2968 break; 2969 default: 2970 status = SPDK_NVME_SC_INVALID_FIELD; 2971 update_sgroup = false; 2972 break; 2973 } 2974 2975 exit: 2976 if (update_sgroup && racqa == SPDK_NVME_RESERVE_PREEMPT) { 2977 new_num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, new_hostid_list, 2978 SPDK_NVMF_MAX_NUM_REGISTRANTS, 2979 &ctrlr->hostid); 2980 /* Preempt notification occurs on the unregistered controllers 2981 * other than the controller who issued the command. 2982 */ 2983 num_hostid = nvmf_ns_reservation_get_unregistered_hostid(hostid_list, 2984 num_hostid, 2985 new_hostid_list, 2986 new_num_hostid); 2987 if (num_hostid) { 2988 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 2989 hostid_list, 2990 num_hostid, 2991 SPDK_NVME_REGISTRATION_PREEMPTED); 2992 2993 } 2994 /* Reservation released notification occurs on the 2995 * controllers which are the remaining registrants other than 2996 * the controller who issued the command. 2997 */ 2998 if (reservation_released && new_num_hostid) { 2999 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 3000 new_hostid_list, 3001 new_num_hostid, 3002 SPDK_NVME_RESERVATION_RELEASED); 3003 3004 } 3005 } 3006 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3007 req->rsp->nvme_cpl.status.sc = status; 3008 return update_sgroup; 3009 } 3010 3011 static bool 3012 nvmf_ns_reservation_release(struct spdk_nvmf_ns *ns, 3013 struct spdk_nvmf_ctrlr *ctrlr, 3014 struct spdk_nvmf_request *req) 3015 { 3016 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 3017 uint8_t rrela, iekey, rtype; 3018 struct spdk_nvmf_registrant *reg; 3019 uint64_t crkey = 0; 3020 uint8_t status = SPDK_NVME_SC_SUCCESS; 3021 bool update_sgroup = true; 3022 struct spdk_uuid hostid_list[SPDK_NVMF_MAX_NUM_REGISTRANTS]; 3023 uint32_t num_hostid = 0; 3024 3025 rrela = cmd->cdw10_bits.resv_release.rrela; 3026 iekey = cmd->cdw10_bits.resv_release.iekey; 3027 rtype = cmd->cdw10_bits.resv_release.rtype; 3028 3029 if (req->iovcnt > 0 && req->length >= sizeof(crkey)) { 3030 struct spdk_iov_xfer ix; 3031 spdk_iov_xfer_init(&ix, req->iov, req->iovcnt); 3032 spdk_iov_xfer_to_buf(&ix, &crkey, sizeof(crkey)); 3033 } else { 3034 SPDK_ERRLOG("No key provided. Failing request.\n"); 3035 status = SPDK_NVME_SC_INVALID_FIELD; 3036 goto exit; 3037 } 3038 3039 SPDK_DEBUGLOG(nvmf, "RELEASE: RRELA %u, IEKEY %u, RTYPE %u, " 3040 "CRKEY 0x%"PRIx64"\n", rrela, iekey, rtype, crkey); 3041 3042 if (iekey) { 3043 SPDK_ERRLOG("Ignore existing key field set to 1\n"); 3044 status = SPDK_NVME_SC_INVALID_FIELD; 3045 update_sgroup = false; 3046 goto exit; 3047 } 3048 3049 reg = nvmf_ns_reservation_get_registrant(ns, &ctrlr->hostid); 3050 if (!reg || reg->rkey != crkey) { 3051 SPDK_ERRLOG("No registrant or current key doesn't match " 3052 "with existing registrant key\n"); 3053 status = SPDK_NVME_SC_RESERVATION_CONFLICT; 3054 update_sgroup = false; 3055 goto exit; 3056 } 3057 3058 num_hostid = nvmf_ns_reservation_get_all_other_hostid(ns, hostid_list, 3059 SPDK_NVMF_MAX_NUM_REGISTRANTS, 3060 &ctrlr->hostid); 3061 3062 switch (rrela) { 3063 case SPDK_NVME_RESERVE_RELEASE: 3064 if (!ns->holder) { 3065 SPDK_DEBUGLOG(nvmf, "RELEASE: no holder\n"); 3066 update_sgroup = false; 3067 goto exit; 3068 } 3069 if (ns->rtype != rtype) { 3070 SPDK_ERRLOG("Type doesn't match\n"); 3071 status = SPDK_NVME_SC_INVALID_FIELD; 3072 update_sgroup = false; 3073 goto exit; 3074 } 3075 if (!nvmf_ns_reservation_registrant_is_holder(ns, reg)) { 3076 /* not the reservation holder, this isn't an error */ 3077 update_sgroup = false; 3078 goto exit; 3079 } 3080 3081 rtype = ns->rtype; 3082 nvmf_ns_reservation_release_reservation(ns); 3083 3084 if (num_hostid && rtype != SPDK_NVME_RESERVE_WRITE_EXCLUSIVE && 3085 rtype != SPDK_NVME_RESERVE_EXCLUSIVE_ACCESS) { 3086 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 3087 hostid_list, 3088 num_hostid, 3089 SPDK_NVME_RESERVATION_RELEASED); 3090 } 3091 break; 3092 case SPDK_NVME_RESERVE_CLEAR: 3093 nvmf_ns_reservation_clear_all_registrants(ns); 3094 if (num_hostid) { 3095 nvmf_subsystem_gen_ctrlr_notification(ns->subsystem, ns, 3096 hostid_list, 3097 num_hostid, 3098 SPDK_NVME_RESERVATION_PREEMPTED); 3099 } 3100 break; 3101 default: 3102 status = SPDK_NVME_SC_INVALID_FIELD; 3103 update_sgroup = false; 3104 goto exit; 3105 } 3106 3107 exit: 3108 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3109 req->rsp->nvme_cpl.status.sc = status; 3110 return update_sgroup; 3111 } 3112 3113 static void 3114 nvmf_ns_reservation_report(struct spdk_nvmf_ns *ns, 3115 struct spdk_nvmf_ctrlr *ctrlr, 3116 struct spdk_nvmf_request *req) 3117 { 3118 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 3119 struct spdk_nvmf_registrant *reg, *tmp; 3120 struct spdk_nvme_reservation_status_extended_data status_data = { 0 }; 3121 struct spdk_iov_xfer ix; 3122 uint32_t transfer_len; 3123 uint32_t regctl = 0; 3124 uint8_t status = SPDK_NVME_SC_SUCCESS; 3125 3126 if (req->iovcnt == 0) { 3127 SPDK_ERRLOG("No data transfer specified for request. " 3128 " Unable to transfer back response.\n"); 3129 status = SPDK_NVME_SC_INVALID_FIELD; 3130 goto exit; 3131 } 3132 3133 if (!cmd->cdw11_bits.resv_report.eds) { 3134 SPDK_ERRLOG("NVMeoF uses extended controller data structure, " 3135 "please set EDS bit in cdw11 and try again\n"); 3136 status = SPDK_NVME_SC_HOSTID_INCONSISTENT_FORMAT; 3137 goto exit; 3138 } 3139 3140 /* Number of Dwords of the Reservation Status data structure to transfer */ 3141 transfer_len = (cmd->cdw10 + 1) * sizeof(uint32_t); 3142 3143 if (transfer_len < sizeof(struct spdk_nvme_reservation_status_extended_data)) { 3144 status = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 3145 goto exit; 3146 } 3147 3148 spdk_iov_xfer_init(&ix, req->iov, req->iovcnt); 3149 3150 status_data.data.gen = ns->gen; 3151 status_data.data.rtype = ns->rtype; 3152 status_data.data.ptpls = ns->ptpl_activated; 3153 3154 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 3155 regctl++; 3156 } 3157 3158 /* 3159 * We report the number of registrants as per the spec here, even if 3160 * the iov isn't big enough to contain them all. In that case, the 3161 * spdk_iov_xfer_from_buf() won't actually copy any of the remaining 3162 * data; as it keeps track of the iov cursor itself, it's simplest to 3163 * just walk the entire list anyway. 3164 */ 3165 status_data.data.regctl = regctl; 3166 3167 spdk_iov_xfer_from_buf(&ix, &status_data, sizeof(status_data)); 3168 3169 TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) { 3170 struct spdk_nvme_registered_ctrlr_extended_data ctrlr_data = { 0 }; 3171 3172 /* Set to 0xffffh for dynamic controller */ 3173 ctrlr_data.cntlid = 0xffff; 3174 ctrlr_data.rcsts.status = (ns->holder == reg) ? true : false; 3175 ctrlr_data.rkey = reg->rkey; 3176 spdk_uuid_copy((struct spdk_uuid *)ctrlr_data.hostid, ®->hostid); 3177 3178 spdk_iov_xfer_from_buf(&ix, &ctrlr_data, sizeof(ctrlr_data)); 3179 } 3180 3181 exit: 3182 req->rsp->nvme_cpl.status.sct = SPDK_NVME_SCT_GENERIC; 3183 req->rsp->nvme_cpl.status.sc = status; 3184 return; 3185 } 3186 3187 static void 3188 nvmf_ns_reservation_complete(void *ctx) 3189 { 3190 struct spdk_nvmf_request *req = ctx; 3191 3192 spdk_nvmf_request_complete(req); 3193 } 3194 3195 static void 3196 _nvmf_ns_reservation_update_done(struct spdk_nvmf_subsystem *subsystem, 3197 void *cb_arg, int status) 3198 { 3199 struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)cb_arg; 3200 struct spdk_nvmf_poll_group *group = req->qpair->group; 3201 3202 spdk_thread_send_msg(group->thread, nvmf_ns_reservation_complete, req); 3203 } 3204 3205 void 3206 nvmf_ns_reservation_request(void *ctx) 3207 { 3208 struct spdk_nvmf_request *req = (struct spdk_nvmf_request *)ctx; 3209 struct spdk_nvme_cmd *cmd = &req->cmd->nvme_cmd; 3210 struct spdk_nvmf_ctrlr *ctrlr = req->qpair->ctrlr; 3211 uint32_t nsid; 3212 struct spdk_nvmf_ns *ns; 3213 bool update_sgroup = false; 3214 int status = 0; 3215 3216 nsid = cmd->nsid; 3217 ns = _nvmf_subsystem_get_ns(ctrlr->subsys, nsid); 3218 assert(ns != NULL); 3219 3220 switch (cmd->opc) { 3221 case SPDK_NVME_OPC_RESERVATION_REGISTER: 3222 update_sgroup = nvmf_ns_reservation_register(ns, ctrlr, req); 3223 break; 3224 case SPDK_NVME_OPC_RESERVATION_ACQUIRE: 3225 update_sgroup = nvmf_ns_reservation_acquire(ns, ctrlr, req); 3226 break; 3227 case SPDK_NVME_OPC_RESERVATION_RELEASE: 3228 update_sgroup = nvmf_ns_reservation_release(ns, ctrlr, req); 3229 break; 3230 case SPDK_NVME_OPC_RESERVATION_REPORT: 3231 nvmf_ns_reservation_report(ns, ctrlr, req); 3232 break; 3233 default: 3234 break; 3235 } 3236 3237 /* update reservation information to subsystem's poll group */ 3238 if (update_sgroup) { 3239 if (ns->ptpl_activated || cmd->opc == SPDK_NVME_OPC_RESERVATION_REGISTER) { 3240 if (nvmf_ns_update_reservation_info(ns) != 0) { 3241 req->rsp->nvme_cpl.status.sc = SPDK_NVME_SC_INTERNAL_DEVICE_ERROR; 3242 } 3243 } 3244 status = nvmf_subsystem_update_ns(ctrlr->subsys, _nvmf_ns_reservation_update_done, req); 3245 if (status == 0) { 3246 return; 3247 } 3248 } 3249 3250 _nvmf_ns_reservation_update_done(ctrlr->subsys, req, status); 3251 } 3252 3253 int 3254 spdk_nvmf_subsystem_set_ana_reporting(struct spdk_nvmf_subsystem *subsystem, 3255 bool ana_reporting) 3256 { 3257 if (subsystem->state != SPDK_NVMF_SUBSYSTEM_INACTIVE) { 3258 return -EAGAIN; 3259 } 3260 3261 subsystem->flags.ana_reporting = ana_reporting; 3262 3263 return 0; 3264 } 3265 3266 bool 3267 spdk_nvmf_subsystem_get_ana_reporting(struct spdk_nvmf_subsystem *subsystem) 3268 { 3269 return subsystem->flags.ana_reporting; 3270 } 3271 3272 struct subsystem_listener_update_ctx { 3273 struct spdk_nvmf_subsystem_listener *listener; 3274 3275 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn; 3276 void *cb_arg; 3277 }; 3278 3279 static void 3280 subsystem_listener_update_done(struct spdk_io_channel_iter *i, int status) 3281 { 3282 struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 3283 3284 if (ctx->cb_fn) { 3285 ctx->cb_fn(ctx->cb_arg, status); 3286 } 3287 free(ctx); 3288 } 3289 3290 static void 3291 subsystem_listener_update_on_pg(struct spdk_io_channel_iter *i) 3292 { 3293 struct subsystem_listener_update_ctx *ctx = spdk_io_channel_iter_get_ctx(i); 3294 struct spdk_nvmf_subsystem_listener *listener; 3295 struct spdk_nvmf_poll_group *group; 3296 struct spdk_nvmf_ctrlr *ctrlr; 3297 3298 listener = ctx->listener; 3299 group = spdk_io_channel_get_ctx(spdk_io_channel_iter_get_channel(i)); 3300 3301 TAILQ_FOREACH(ctrlr, &listener->subsystem->ctrlrs, link) { 3302 if (ctrlr->thread != spdk_get_thread()) { 3303 continue; 3304 } 3305 3306 if (ctrlr->admin_qpair && ctrlr->admin_qpair->group == group && ctrlr->listener == listener) { 3307 nvmf_ctrlr_async_event_ana_change_notice(ctrlr); 3308 } 3309 } 3310 3311 spdk_for_each_channel_continue(i, 0); 3312 } 3313 3314 void 3315 spdk_nvmf_subsystem_set_ana_state(struct spdk_nvmf_subsystem *subsystem, 3316 const struct spdk_nvme_transport_id *trid, 3317 enum spdk_nvme_ana_state ana_state, uint32_t anagrpid, 3318 spdk_nvmf_tgt_subsystem_listen_done_fn cb_fn, void *cb_arg) 3319 { 3320 struct spdk_nvmf_subsystem_listener *listener; 3321 struct subsystem_listener_update_ctx *ctx; 3322 uint32_t i; 3323 3324 assert(cb_fn != NULL); 3325 assert(subsystem->state == SPDK_NVMF_SUBSYSTEM_INACTIVE || 3326 subsystem->state == SPDK_NVMF_SUBSYSTEM_PAUSED); 3327 3328 if (!subsystem->flags.ana_reporting) { 3329 SPDK_ERRLOG("ANA reporting is disabled\n"); 3330 cb_fn(cb_arg, -EINVAL); 3331 return; 3332 } 3333 3334 /* ANA Change state is not used, ANA Persistent Loss state 3335 * is not supported yet. 3336 */ 3337 if (!(ana_state == SPDK_NVME_ANA_OPTIMIZED_STATE || 3338 ana_state == SPDK_NVME_ANA_NON_OPTIMIZED_STATE || 3339 ana_state == SPDK_NVME_ANA_INACCESSIBLE_STATE)) { 3340 SPDK_ERRLOG("ANA state %d is not supported\n", ana_state); 3341 cb_fn(cb_arg, -ENOTSUP); 3342 return; 3343 } 3344 3345 if (anagrpid > subsystem->max_nsid) { 3346 SPDK_ERRLOG("ANA group ID %" PRIu32 " is more than maximum\n", anagrpid); 3347 cb_fn(cb_arg, -EINVAL); 3348 return; 3349 } 3350 3351 listener = nvmf_subsystem_find_listener(subsystem, trid); 3352 if (!listener) { 3353 SPDK_ERRLOG("Unable to find listener.\n"); 3354 cb_fn(cb_arg, -EINVAL); 3355 return; 3356 } 3357 3358 if (anagrpid != 0 && listener->ana_state[anagrpid - 1] == ana_state) { 3359 cb_fn(cb_arg, 0); 3360 return; 3361 } 3362 3363 ctx = calloc(1, sizeof(*ctx)); 3364 if (!ctx) { 3365 SPDK_ERRLOG("Unable to allocate context\n"); 3366 cb_fn(cb_arg, -ENOMEM); 3367 return; 3368 } 3369 3370 for (i = 1; i <= subsystem->max_nsid; i++) { 3371 if (anagrpid == 0 || i == anagrpid) { 3372 listener->ana_state[i - 1] = ana_state; 3373 } 3374 } 3375 listener->ana_state_change_count++; 3376 3377 ctx->listener = listener; 3378 ctx->cb_fn = cb_fn; 3379 ctx->cb_arg = cb_arg; 3380 3381 spdk_for_each_channel(subsystem->tgt, 3382 subsystem_listener_update_on_pg, 3383 ctx, 3384 subsystem_listener_update_done); 3385 } 3386 3387 bool 3388 spdk_nvmf_subsystem_is_discovery(struct spdk_nvmf_subsystem *subsystem) 3389 { 3390 return subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY_CURRENT || 3391 subsystem->subtype == SPDK_NVMF_SUBTYPE_DISCOVERY; 3392 } 3393 3394 bool 3395 nvmf_nqn_is_discovery(const char *nqn) 3396 { 3397 return strcmp(nqn, SPDK_NVMF_DISCOVERY_NQN) == 0; 3398 } 3399