LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - subsystem.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1587 2161 73.4 %
Date: 2024-12-09 12:07:20 Functions: 128 143 89.5 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1857 6015 30.9 %

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

Generated by: LCOV version 1.15