LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - subsystem.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1462 1849 79.1 %
Date: 2024-08-11 23:22:04 Functions: 133 141 94.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 809 1261 64.2 %

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

Generated by: LCOV version 1.14