LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - subsystem.c (source / functions) Hit Total Coverage
Test: Combined Lines: 1551 2160 71.8 %
Date: 2024-12-15 16:34:19 Functions: 134 143 93.7 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1781 6023 29.6 %

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

Generated by: LCOV version 1.15