LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - subsystem.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1323 1678 78.8 %
Date: 2024-07-12 22:14:58 Functions: 122 130 93.8 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 692 1084 63.8 %

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

Generated by: LCOV version 1.14