LCOV - code coverage report
Current view: top level - lib/nvmf - subsystem.c (source / functions) Hit Total Coverage
Test: ut_cov_unit.info Lines: 1092 1823 59.9 %
Date: 2024-07-15 18:15:05 Functions: 87 141 61.7 %

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

Generated by: LCOV version 1.15