LCOV - code coverage report
Current view: top level - lib/nvmf - subsystem.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 1104 1973 56.0 %
Date: 2024-11-05 10:06:02 Functions: 89 153 58.2 %

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

Generated by: LCOV version 1.15