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