LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - nvmf.c (source / functions) Hit Total Coverage
Test: Combined Lines: 710 963 73.7 %
Date: 2024-07-12 12:30:19 Functions: 55 66 83.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 320 558 57.3 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2016 Intel Corporation. All rights reserved.
       3                 :            :  *   Copyright (c) 2018-2019, 2021 Mellanox Technologies LTD. All rights reserved.
       4                 :            :  *   Copyright (c) 2021, 2023 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
       5                 :            :  */
       6                 :            : 
       7                 :            : #include "spdk/stdinc.h"
       8                 :            : 
       9                 :            : #include "spdk/bdev.h"
      10                 :            : #include "spdk/bit_array.h"
      11                 :            : #include "spdk/thread.h"
      12                 :            : #include "spdk/nvmf.h"
      13                 :            : #include "spdk/endian.h"
      14                 :            : #include "spdk/string.h"
      15                 :            : #include "spdk/log.h"
      16                 :            : #include "spdk_internal/usdt.h"
      17                 :            : 
      18                 :            : #include "nvmf_internal.h"
      19                 :            : #include "transport.h"
      20                 :            : 
      21                 :        827 : SPDK_LOG_REGISTER_COMPONENT(nvmf)
      22                 :            : 
      23                 :            : #define SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS 1024
      24                 :            : 
      25                 :            : static TAILQ_HEAD(, spdk_nvmf_tgt) g_nvmf_tgts = TAILQ_HEAD_INITIALIZER(g_nvmf_tgts);
      26                 :            : 
      27                 :            : typedef void (*nvmf_qpair_disconnect_cpl)(void *ctx, int status);
      28                 :            : 
      29                 :            : /* supplied to a single call to nvmf_qpair_disconnect */
      30                 :            : struct nvmf_qpair_disconnect_ctx {
      31                 :            :         struct spdk_nvmf_qpair *qpair;
      32                 :            :         struct spdk_nvmf_ctrlr *ctrlr;
      33                 :            :         uint16_t qid;
      34                 :            : };
      35                 :            : 
      36                 :            : /*
      37                 :            :  * There are several times when we need to iterate through the list of all qpairs and selectively delete them.
      38                 :            :  * In order to do this sequentially without overlap, we must provide a context to recover the next qpair from
      39                 :            :  * to enable calling nvmf_qpair_disconnect on the next desired qpair.
      40                 :            :  */
      41                 :            : struct nvmf_qpair_disconnect_many_ctx {
      42                 :            :         struct spdk_nvmf_subsystem *subsystem;
      43                 :            :         struct spdk_nvmf_poll_group *group;
      44                 :            :         spdk_nvmf_poll_group_mod_done cpl_fn;
      45                 :            :         void *cpl_ctx;
      46                 :            : };
      47                 :            : 
      48                 :            : static struct spdk_nvmf_referral *
      49                 :         39 : nvmf_tgt_find_referral(struct spdk_nvmf_tgt *tgt,
      50                 :            :                        const struct spdk_nvme_transport_id *trid)
      51                 :            : {
      52                 :            :         struct spdk_nvmf_referral *referral;
      53                 :            : 
      54         [ +  + ]:         60 :         TAILQ_FOREACH(referral, &tgt->referrals, link) {
      55         [ +  + ]:         39 :                 if (spdk_nvme_transport_id_compare(&referral->trid, trid) == 0) {
      56                 :         18 :                         return referral;
      57                 :            :                 }
      58                 :            :         }
      59                 :            : 
      60                 :         21 :         return NULL;
      61                 :            : }
      62                 :            : 
      63                 :            : int
      64                 :         21 : spdk_nvmf_tgt_add_referral(struct spdk_nvmf_tgt *tgt,
      65                 :            :                            const struct spdk_nvmf_referral_opts *uopts)
      66                 :            : {
      67                 :            :         struct spdk_nvmf_referral *referral;
      68                 :         21 :         struct spdk_nvmf_referral_opts opts = {};
      69                 :         21 :         struct spdk_nvme_transport_id *trid = &opts.trid;
      70                 :            : 
      71   [ -  +  -  + ]:         21 :         memcpy(&opts, uopts, spdk_min(uopts->size, sizeof(opts)));
      72         [ +  + ]:         21 :         if (trid->subnqn[0] == '\0') {
      73         [ -  + ]:         15 :                 snprintf(trid->subnqn, sizeof(trid->subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
      74                 :            :         }
      75                 :            : 
      76         [ -  + ]:         21 :         if (!nvmf_nqn_is_valid(trid->subnqn)) {
      77                 :          0 :                 SPDK_ERRLOG("Invalid subsystem NQN\n");
      78                 :          0 :                 return -EINVAL;
      79                 :            :         }
      80                 :            : 
      81                 :            :         /* If the entry already exists, just ignore it. */
      82         [ -  + ]:         21 :         if (nvmf_tgt_find_referral(tgt, trid)) {
      83                 :          0 :                 return 0;
      84                 :            :         }
      85                 :            : 
      86                 :         21 :         referral = calloc(1, sizeof(*referral));
      87         [ -  + ]:         21 :         if (!referral) {
      88                 :          0 :                 SPDK_ERRLOG("Failed to allocate memory for a referral\n");
      89                 :          0 :                 return -ENOMEM;
      90                 :            :         }
      91                 :            : 
      92         [ +  + ]:         21 :         referral->entry.subtype = nvmf_nqn_is_discovery(trid->subnqn) ?
      93                 :            :                                   SPDK_NVMF_SUBTYPE_DISCOVERY :
      94                 :            :                                   SPDK_NVMF_SUBTYPE_NVME;
      95         [ -  + ]:         42 :         referral->entry.treq.secure_channel = opts.secure_channel ?
      96         [ -  + ]:         21 :                                               SPDK_NVMF_TREQ_SECURE_CHANNEL_REQUIRED :
      97                 :            :                                               SPDK_NVMF_TREQ_SECURE_CHANNEL_NOT_REQUIRED;
      98                 :         21 :         referral->entry.cntlid = 0xffff;
      99                 :         21 :         referral->entry.trtype = trid->trtype;
     100                 :         21 :         referral->entry.adrfam = trid->adrfam;
     101   [ -  +  -  + ]:         21 :         memcpy(&referral->trid, trid, sizeof(struct spdk_nvme_transport_id));
     102                 :         21 :         spdk_strcpy_pad(referral->entry.subnqn, trid->subnqn, sizeof(trid->subnqn), '\0');
     103                 :         21 :         spdk_strcpy_pad(referral->entry.trsvcid, trid->trsvcid, sizeof(referral->entry.trsvcid), ' ');
     104                 :         21 :         spdk_strcpy_pad(referral->entry.traddr, trid->traddr, sizeof(referral->entry.traddr), ' ');
     105                 :            : 
     106         [ +  + ]:         21 :         TAILQ_INSERT_HEAD(&tgt->referrals, referral, link);
     107                 :         21 :         nvmf_update_discovery_log(tgt, NULL);
     108                 :            : 
     109                 :         21 :         return 0;
     110                 :            : }
     111                 :            : 
     112                 :            : int
     113                 :         18 : spdk_nvmf_tgt_remove_referral(struct spdk_nvmf_tgt *tgt,
     114                 :            :                               const struct spdk_nvmf_referral_opts *uopts)
     115                 :            : {
     116                 :            :         struct spdk_nvmf_referral *referral;
     117                 :         18 :         struct spdk_nvmf_referral_opts opts = {};
     118                 :         18 :         struct spdk_nvme_transport_id *trid = &opts.trid;
     119                 :            : 
     120   [ -  +  -  + ]:         18 :         memcpy(&opts, uopts, spdk_min(uopts->size, sizeof(opts)));
     121         [ +  + ]:         18 :         if (trid->subnqn[0] == '\0') {
     122         [ -  + ]:         12 :                 snprintf(trid->subnqn, sizeof(trid->subnqn), "%s", SPDK_NVMF_DISCOVERY_NQN);
     123                 :            :         }
     124                 :            : 
     125                 :         18 :         referral = nvmf_tgt_find_referral(tgt, &opts.trid);
     126         [ -  + ]:         18 :         if (referral == NULL) {
     127                 :          0 :                 return -ENOENT;
     128                 :            :         }
     129                 :            : 
     130         [ +  + ]:         18 :         TAILQ_REMOVE(&tgt->referrals, referral, link);
     131                 :         18 :         nvmf_update_discovery_log(tgt, NULL);
     132                 :            : 
     133                 :         18 :         free(referral);
     134                 :            : 
     135                 :         18 :         return 0;
     136                 :            : }
     137                 :            : 
     138                 :            : void
     139                 :      25928 : nvmf_qpair_set_state(struct spdk_nvmf_qpair *qpair,
     140                 :            :                      enum spdk_nvmf_qpair_state state)
     141                 :            : {
     142         [ -  + ]:      25928 :         assert(qpair != NULL);
     143         [ -  + ]:      25928 :         assert(qpair->group->thread == spdk_get_thread());
     144                 :            : 
     145                 :      25928 :         qpair->state = state;
     146                 :      25928 : }
     147                 :            : 
     148                 :            : static int
     149                 : 2266959209 : nvmf_poll_group_poll(void *ctx)
     150                 :            : {
     151                 : 2266959209 :         struct spdk_nvmf_poll_group *group = ctx;
     152                 :            :         int rc;
     153                 : 2266959209 :         int count = 0;
     154                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
     155                 :            : 
     156         [ +  + ]: 3392183657 :         TAILQ_FOREACH(tgroup, &group->tgroups, link) {
     157                 : 1125224469 :                 rc = nvmf_transport_poll_group_poll(tgroup);
     158         [ +  + ]: 1125224469 :                 if (rc < 0) {
     159                 :         31 :                         return SPDK_POLLER_BUSY;
     160                 :            :                 }
     161                 : 1125224438 :                 count += rc;
     162                 :            :         }
     163                 :            : 
     164                 : 2266959178 :         return count > 0 ? SPDK_POLLER_BUSY : SPDK_POLLER_IDLE;
     165                 :            : }
     166                 :            : 
     167                 :            : /*
     168                 :            :  * Reset and clean up the poll group (I/O channel code will actually free the
     169                 :            :  * group).
     170                 :            :  */
     171                 :            : static void
     172                 :       1272 : nvmf_tgt_cleanup_poll_group(struct spdk_nvmf_poll_group *group)
     173                 :            : {
     174                 :            :         struct spdk_nvmf_transport_poll_group *tgroup, *tmp;
     175                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
     176                 :            :         uint32_t sid, nsid;
     177                 :            : 
     178         [ +  + ]:       1860 :         TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) {
     179         [ -  + ]:        588 :                 TAILQ_REMOVE(&group->tgroups, tgroup, link);
     180                 :        588 :                 nvmf_transport_poll_group_destroy(tgroup);
     181                 :            :         }
     182                 :            : 
     183         [ +  + ]:    1287804 :         for (sid = 0; sid < group->num_sgroups; sid++) {
     184                 :    1286532 :                 sgroup = &group->sgroups[sid];
     185                 :            : 
     186         [ -  + ]:    1286532 :                 assert(sgroup != NULL);
     187                 :            : 
     188         [ +  + ]:    1286536 :                 for (nsid = 0; nsid < sgroup->num_ns; nsid++) {
     189         [ +  - ]:          4 :                         if (sgroup->ns_info[nsid].channel) {
     190                 :          4 :                                 spdk_put_io_channel(sgroup->ns_info[nsid].channel);
     191                 :          4 :                                 sgroup->ns_info[nsid].channel = NULL;
     192                 :            :                         }
     193                 :            :                 }
     194                 :            : 
     195                 :    1286532 :                 free(sgroup->ns_info);
     196                 :            :         }
     197                 :            : 
     198                 :       1272 :         free(group->sgroups);
     199                 :            : 
     200                 :       1272 :         spdk_poller_unregister(&group->poller);
     201                 :            : 
     202         [ +  + ]:       1272 :         if (group->destroy_cb_fn) {
     203                 :       1268 :                 group->destroy_cb_fn(group->destroy_cb_arg, 0);
     204                 :            :         }
     205                 :       1272 : }
     206                 :            : 
     207                 :            : /*
     208                 :            :  * Callback to unregister a poll group from the target, and clean up its state.
     209                 :            :  */
     210                 :            : static void
     211                 :       1272 : nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf)
     212                 :            : {
     213                 :       1272 :         struct spdk_nvmf_tgt *tgt = io_device;
     214                 :       1272 :         struct spdk_nvmf_poll_group *group = ctx_buf;
     215                 :            : 
     216                 :        290 :         SPDK_DTRACE_PROBE1_TICKS(nvmf_destroy_poll_group, spdk_thread_get_id(group->thread));
     217                 :            : 
     218         [ -  + ]:       1272 :         pthread_mutex_lock(&tgt->mutex);
     219         [ +  + ]:       1272 :         TAILQ_REMOVE(&tgt->poll_groups, group, link);
     220                 :       1272 :         tgt->num_poll_groups--;
     221         [ -  + ]:       1272 :         pthread_mutex_unlock(&tgt->mutex);
     222                 :            : 
     223   [ +  -  +  + ]:       1272 :         assert(!(tgt->state == NVMF_TGT_PAUSING || tgt->state == NVMF_TGT_RESUMING));
     224                 :       1272 :         nvmf_tgt_cleanup_poll_group(group);
     225                 :       1272 : }
     226                 :            : 
     227                 :            : static int
     228                 :        588 : nvmf_poll_group_add_transport(struct spdk_nvmf_poll_group *group,
     229                 :            :                               struct spdk_nvmf_transport *transport)
     230                 :            : {
     231                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
     232                 :            : 
     233         [ -  + ]:        588 :         TAILQ_FOREACH(tgroup, &group->tgroups, link) {
     234         [ #  # ]:          0 :                 if (tgroup->transport == transport) {
     235                 :            :                         /* Transport already in the poll group */
     236                 :          0 :                         return 0;
     237                 :            :                 }
     238                 :            :         }
     239                 :            : 
     240                 :        588 :         tgroup = nvmf_transport_poll_group_create(transport, group);
     241         [ -  + ]:        588 :         if (!tgroup) {
     242                 :          0 :                 SPDK_ERRLOG("Unable to create poll group for transport\n");
     243                 :          0 :                 return -1;
     244                 :            :         }
     245                 :        223 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_transport_poll_group_create, transport,
     246                 :            :                                  spdk_thread_get_id(group->thread));
     247                 :            : 
     248                 :        588 :         tgroup->group = group;
     249                 :        588 :         TAILQ_INSERT_TAIL(&group->tgroups, tgroup, link);
     250                 :            : 
     251                 :        588 :         return 0;
     252                 :            : }
     253                 :            : 
     254                 :            : static int
     255                 :       1272 : nvmf_tgt_create_poll_group(void *io_device, void *ctx_buf)
     256                 :            : {
     257                 :       1272 :         struct spdk_nvmf_tgt *tgt = io_device;
     258                 :       1272 :         struct spdk_nvmf_poll_group *group = ctx_buf;
     259                 :            :         struct spdk_nvmf_transport *transport;
     260                 :            :         struct spdk_nvmf_subsystem *subsystem;
     261                 :       1272 :         struct spdk_thread *thread = spdk_get_thread();
     262                 :            :         uint32_t i;
     263                 :            :         int rc;
     264                 :            : 
     265                 :       1272 :         group->tgt = tgt;
     266                 :       1272 :         TAILQ_INIT(&group->tgroups);
     267                 :       1272 :         TAILQ_INIT(&group->qpairs);
     268                 :       1272 :         group->thread = thread;
     269         [ -  + ]:       1272 :         pthread_mutex_init(&group->mutex, NULL);
     270                 :            : 
     271                 :       1272 :         group->poller = SPDK_POLLER_REGISTER(nvmf_poll_group_poll, group, 0);
     272                 :            : 
     273                 :        290 :         SPDK_DTRACE_PROBE1_TICKS(nvmf_create_poll_group, spdk_thread_get_id(thread));
     274                 :            : 
     275         [ +  + ]:       1276 :         TAILQ_FOREACH(transport, &tgt->transports, link) {
     276                 :          4 :                 rc = nvmf_poll_group_add_transport(group, transport);
     277         [ -  + ]:          4 :                 if (rc != 0) {
     278                 :          0 :                         nvmf_tgt_cleanup_poll_group(group);
     279                 :          0 :                         return rc;
     280                 :            :                 }
     281                 :            :         }
     282                 :            : 
     283                 :       1272 :         group->num_sgroups = tgt->max_subsystems;
     284                 :       1272 :         group->sgroups = calloc(tgt->max_subsystems, sizeof(struct spdk_nvmf_subsystem_poll_group));
     285         [ -  + ]:       1272 :         if (!group->sgroups) {
     286                 :          0 :                 nvmf_tgt_cleanup_poll_group(group);
     287                 :          0 :                 return -ENOMEM;
     288                 :            :         }
     289                 :            : 
     290         [ +  + ]:    1287804 :         for (i = 0; i < tgt->max_subsystems; i++) {
     291                 :    1286532 :                 TAILQ_INIT(&group->sgroups[i].queued);
     292                 :            :         }
     293                 :            : 
     294         [ +  + ]:       1351 :         for (subsystem = spdk_nvmf_subsystem_get_first(tgt);
     295         [ +  + ]:       2386 :              subsystem != NULL;
     296                 :       1272 :              subsystem = spdk_nvmf_subsystem_get_next(subsystem)) {
     297         [ -  + ]:       1272 :                 if (nvmf_poll_group_add_subsystem(group, subsystem, NULL, NULL) != 0) {
     298                 :          0 :                         nvmf_tgt_cleanup_poll_group(group);
     299                 :          0 :                         return -1;
     300                 :            :                 }
     301                 :            :         }
     302                 :            : 
     303         [ -  + ]:       1272 :         pthread_mutex_lock(&tgt->mutex);
     304                 :       1272 :         tgt->num_poll_groups++;
     305                 :       1272 :         TAILQ_INSERT_TAIL(&tgt->poll_groups, group, link);
     306         [ -  + ]:       1272 :         pthread_mutex_unlock(&tgt->mutex);
     307                 :            : 
     308                 :       1272 :         return 0;
     309                 :            : }
     310                 :            : 
     311                 :            : static void
     312                 :       1268 : _nvmf_tgt_disconnect_qpairs(void *ctx)
     313                 :            : {
     314                 :            :         struct spdk_nvmf_qpair *qpair, *qpair_tmp;
     315                 :       1268 :         struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
     316                 :       1268 :         struct spdk_nvmf_poll_group *group = qpair_ctx->group;
     317                 :            :         struct spdk_io_channel *ch;
     318                 :            :         int rc;
     319                 :            : 
     320         [ -  + ]:       1268 :         TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, qpair_tmp) {
     321                 :          0 :                 rc = spdk_nvmf_qpair_disconnect(qpair);
     322   [ #  #  #  # ]:          0 :                 if (rc && rc != -EINPROGRESS) {
     323                 :          0 :                         break;
     324                 :            :                 }
     325                 :            :         }
     326                 :            : 
     327         [ +  - ]:       1268 :         if (TAILQ_EMPTY(&group->qpairs)) {
     328                 :            :                 /* When the refcount from the channels reaches 0, nvmf_tgt_destroy_poll_group will be called. */
     329                 :       1268 :                 ch = spdk_io_channel_from_ctx(group);
     330                 :       1268 :                 spdk_put_io_channel(ch);
     331                 :       1268 :                 free(qpair_ctx);
     332                 :       1268 :                 return;
     333                 :            :         }
     334                 :            : 
     335                 :            :         /* Some qpairs are in process of being disconnected. Send a message and try to remove them again */
     336                 :          0 :         spdk_thread_send_msg(spdk_get_thread(), _nvmf_tgt_disconnect_qpairs, ctx);
     337                 :            : }
     338                 :            : 
     339                 :            : static void
     340                 :       1268 : nvmf_tgt_destroy_poll_group_qpairs(struct spdk_nvmf_poll_group *group)
     341                 :            : {
     342                 :            :         struct nvmf_qpair_disconnect_many_ctx *ctx;
     343                 :            : 
     344                 :        290 :         SPDK_DTRACE_PROBE1_TICKS(nvmf_destroy_poll_group_qpairs, spdk_thread_get_id(group->thread));
     345                 :            : 
     346                 :       1268 :         ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx));
     347         [ -  + ]:       1268 :         if (!ctx) {
     348                 :          0 :                 SPDK_ERRLOG("Failed to allocate memory for destroy poll group ctx\n");
     349                 :          0 :                 return;
     350                 :            :         }
     351                 :            : 
     352                 :       1268 :         ctx->group = group;
     353                 :       1268 :         _nvmf_tgt_disconnect_qpairs(ctx);
     354                 :            : }
     355                 :            : 
     356                 :            : struct spdk_nvmf_tgt *
     357                 :        755 : spdk_nvmf_tgt_create(struct spdk_nvmf_target_opts *_opts)
     358                 :            : {
     359                 :            :         struct spdk_nvmf_tgt *tgt, *tmp_tgt;
     360                 :        755 :         struct spdk_nvmf_target_opts opts = {
     361                 :            :                 .max_subsystems = SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS,
     362                 :            :                 .discovery_filter = SPDK_NVMF_TGT_DISCOVERY_MATCH_ANY,
     363                 :            :         };
     364                 :            : 
     365         [ -  + ]:        755 :         memcpy(&opts, _opts, _opts->size);
     366         [ -  + ]:        755 :         if (strnlen(opts.name, NVMF_TGT_NAME_MAX_LENGTH) == NVMF_TGT_NAME_MAX_LENGTH) {
     367                 :          0 :                 SPDK_ERRLOG("Provided target name exceeds the max length of %u.\n", NVMF_TGT_NAME_MAX_LENGTH);
     368                 :          0 :                 return NULL;
     369                 :            :         }
     370                 :            : 
     371         [ +  + ]:        764 :         TAILQ_FOREACH(tmp_tgt, &g_nvmf_tgts, link) {
     372   [ -  +  -  + ]:          9 :                 if (!strncmp(opts.name, tmp_tgt->name, NVMF_TGT_NAME_MAX_LENGTH)) {
     373                 :          0 :                         SPDK_ERRLOG("Provided target name must be unique.\n");
     374                 :          0 :                         return NULL;
     375                 :            :                 }
     376                 :            :         }
     377                 :            : 
     378                 :        755 :         tgt = calloc(1, sizeof(*tgt));
     379         [ -  + ]:        755 :         if (!tgt) {
     380                 :          0 :                 return NULL;
     381                 :            :         }
     382                 :            : 
     383                 :        755 :         snprintf(tgt->name, NVMF_TGT_NAME_MAX_LENGTH, "%s", opts.name);
     384                 :            : 
     385         [ +  + ]:        755 :         if (!opts.max_subsystems) {
     386                 :        707 :                 tgt->max_subsystems = SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS;
     387                 :            :         } else {
     388                 :         48 :                 tgt->max_subsystems = opts.max_subsystems;
     389                 :            :         }
     390                 :            : 
     391                 :        755 :         tgt->crdt[0] = opts.crdt[0];
     392                 :        755 :         tgt->crdt[1] = opts.crdt[1];
     393                 :        755 :         tgt->crdt[2] = opts.crdt[2];
     394                 :        755 :         tgt->discovery_filter = opts.discovery_filter;
     395                 :        755 :         tgt->discovery_genctr = 0;
     396                 :        755 :         tgt->dhchap_digests = opts.dhchap_digests;
     397                 :        755 :         tgt->dhchap_dhgroups = opts.dhchap_dhgroups;
     398                 :        755 :         TAILQ_INIT(&tgt->transports);
     399                 :        755 :         TAILQ_INIT(&tgt->poll_groups);
     400                 :        755 :         TAILQ_INIT(&tgt->referrals);
     401                 :        755 :         tgt->num_poll_groups = 0;
     402                 :            : 
     403                 :        755 :         tgt->subsystem_ids = spdk_bit_array_create(tgt->max_subsystems);
     404         [ -  + ]:        755 :         if (tgt->subsystem_ids == NULL) {
     405                 :          0 :                 free(tgt);
     406                 :          0 :                 return NULL;
     407                 :            :         }
     408                 :            : 
     409                 :        755 :         RB_INIT(&tgt->subsystems);
     410                 :            : 
     411         [ -  + ]:        755 :         pthread_mutex_init(&tgt->mutex, NULL);
     412                 :            : 
     413                 :        755 :         spdk_io_device_register(tgt,
     414                 :            :                                 nvmf_tgt_create_poll_group,
     415                 :            :                                 nvmf_tgt_destroy_poll_group,
     416                 :            :                                 sizeof(struct spdk_nvmf_poll_group),
     417                 :        755 :                                 tgt->name);
     418                 :            : 
     419                 :        755 :         tgt->state = NVMF_TGT_RUNNING;
     420                 :            : 
     421         [ +  + ]:        755 :         TAILQ_INSERT_HEAD(&g_nvmf_tgts, tgt, link);
     422                 :            : 
     423                 :        755 :         return tgt;
     424                 :            : }
     425                 :            : 
     426                 :            : static void
     427                 :       1011 : _nvmf_tgt_destroy_next_transport(void *ctx)
     428                 :            : {
     429                 :       1011 :         struct spdk_nvmf_tgt *tgt = ctx;
     430                 :            :         struct spdk_nvmf_transport *transport;
     431                 :            : 
     432         [ +  + ]:       1011 :         if (!TAILQ_EMPTY(&tgt->transports)) {
     433                 :        256 :                 transport = TAILQ_FIRST(&tgt->transports);
     434         [ -  + ]:        256 :                 TAILQ_REMOVE(&tgt->transports, transport, link);
     435                 :        256 :                 spdk_nvmf_transport_destroy(transport, _nvmf_tgt_destroy_next_transport, tgt);
     436                 :            :         } else {
     437                 :        755 :                 spdk_nvmf_tgt_destroy_done_fn *destroy_cb_fn = tgt->destroy_cb_fn;
     438                 :        755 :                 void *destroy_cb_arg = tgt->destroy_cb_arg;
     439                 :            : 
     440         [ -  + ]:        755 :                 pthread_mutex_destroy(&tgt->mutex);
     441                 :        755 :                 free(tgt);
     442                 :            : 
     443         [ +  - ]:        755 :                 if (destroy_cb_fn) {
     444                 :        755 :                         destroy_cb_fn(destroy_cb_arg, 0);
     445                 :            :                 }
     446                 :            :         }
     447                 :       1011 : }
     448                 :            : 
     449                 :            : static void
     450                 :        755 : nvmf_tgt_destroy_cb(void *io_device)
     451                 :            : {
     452                 :        755 :         struct spdk_nvmf_tgt *tgt = io_device;
     453                 :            :         struct spdk_nvmf_subsystem *subsystem, *subsystem_next;
     454                 :            :         int rc;
     455                 :            :         struct spdk_nvmf_referral *referral;
     456                 :            : 
     457         [ +  + ]:        758 :         while ((referral = TAILQ_FIRST(&tgt->referrals))) {
     458         [ -  + ]:          3 :                 TAILQ_REMOVE(&tgt->referrals, referral, link);
     459                 :          3 :                 free(referral);
     460                 :            :         }
     461                 :            : 
     462                 :            :         /* We will be freeing subsystems in this loop, so we always need to get the next one
     463                 :            :          * ahead of time, since we can't call get_next() on a subsystem that's been freed.
     464                 :            :          */
     465         [ -  + ]:       1510 :         for (subsystem = spdk_nvmf_subsystem_get_first(tgt),
     466                 :        755 :              subsystem_next = spdk_nvmf_subsystem_get_next(subsystem);
     467         [ +  + ]:        703 :              subsystem != NULL;
     468                 :          6 :              subsystem = subsystem_next,
     469                 :          6 :              subsystem_next = spdk_nvmf_subsystem_get_next(subsystem_next)) {
     470                 :          6 :                 nvmf_subsystem_remove_all_listeners(subsystem, true);
     471                 :            : 
     472                 :          6 :                 rc = spdk_nvmf_subsystem_destroy(subsystem, nvmf_tgt_destroy_cb, tgt);
     473         [ -  + ]:          6 :                 if (rc) {
     474         [ #  # ]:          0 :                         if (rc == -EINPROGRESS) {
     475                 :            :                                 /* If rc is -EINPROGRESS, nvmf_tgt_destroy_cb will be called again when subsystem #i
     476                 :            :                                  * is destroyed, nvmf_tgt_destroy_cb will continue to destroy other subsystems if any */
     477                 :          0 :                                 return;
     478                 :            :                         } else {
     479                 :          0 :                                 SPDK_ERRLOG("Failed to destroy subsystem %s, rc %d\n", subsystem->subnqn, rc);
     480                 :            :                         }
     481                 :            :                 }
     482                 :            :         }
     483                 :        755 :         spdk_bit_array_free(&tgt->subsystem_ids);
     484                 :        755 :         _nvmf_tgt_destroy_next_transport(tgt);
     485                 :            : }
     486                 :            : 
     487                 :            : void
     488                 :        755 : spdk_nvmf_tgt_destroy(struct spdk_nvmf_tgt *tgt,
     489                 :            :                       spdk_nvmf_tgt_destroy_done_fn cb_fn,
     490                 :            :                       void *cb_arg)
     491                 :            : {
     492   [ +  -  +  + ]:        755 :         assert(!(tgt->state == NVMF_TGT_PAUSING || tgt->state == NVMF_TGT_RESUMING));
     493                 :            : 
     494                 :        755 :         tgt->destroy_cb_fn = cb_fn;
     495                 :        755 :         tgt->destroy_cb_arg = cb_arg;
     496                 :            : 
     497         [ +  + ]:        755 :         TAILQ_REMOVE(&g_nvmf_tgts, tgt, link);
     498                 :            : 
     499                 :        755 :         spdk_io_device_unregister(tgt, nvmf_tgt_destroy_cb);
     500                 :        755 : }
     501                 :            : 
     502                 :            : const char *
     503                 :         21 : spdk_nvmf_tgt_get_name(struct spdk_nvmf_tgt *tgt)
     504                 :            : {
     505                 :         21 :         return tgt->name;
     506                 :            : }
     507                 :            : 
     508                 :            : struct spdk_nvmf_tgt *
     509                 :      10199 : spdk_nvmf_get_tgt(const char *name)
     510                 :            : {
     511                 :            :         struct spdk_nvmf_tgt *tgt;
     512                 :      10199 :         uint32_t num_targets = 0;
     513                 :            : 
     514         [ +  + ]:      20398 :         TAILQ_FOREACH(tgt, &g_nvmf_tgts, link) {
     515         [ +  + ]:      10205 :                 if (name) {
     516   [ -  +  -  +  :         24 :                         if (!strncmp(tgt->name, name, NVMF_TGT_NAME_MAX_LENGTH)) {
                   +  + ]
     517                 :          6 :                                 return tgt;
     518                 :            :                         }
     519                 :            :                 }
     520                 :      10199 :                 num_targets++;
     521                 :            :         }
     522                 :            : 
     523                 :            :         /*
     524                 :            :          * special case. If there is only one target and
     525                 :            :          * no name was specified, return the only available
     526                 :            :          * target. If there is more than one target, name must
     527                 :            :          * be specified.
     528                 :            :          */
     529   [ +  +  +  - ]:      10193 :         if (!name && num_targets == 1) {
     530                 :      10181 :                 return TAILQ_FIRST(&g_nvmf_tgts);
     531                 :            :         }
     532                 :            : 
     533                 :         12 :         return NULL;
     534                 :            : }
     535                 :            : 
     536                 :            : struct spdk_nvmf_tgt *
     537                 :          9 : spdk_nvmf_get_first_tgt(void)
     538                 :            : {
     539                 :          9 :         return TAILQ_FIRST(&g_nvmf_tgts);
     540                 :            : }
     541                 :            : 
     542                 :            : struct spdk_nvmf_tgt *
     543                 :         15 : spdk_nvmf_get_next_tgt(struct spdk_nvmf_tgt *prev)
     544                 :            : {
     545                 :         15 :         return TAILQ_NEXT(prev, link);
     546                 :            : }
     547                 :            : 
     548                 :            : static void
     549                 :         22 : nvmf_write_nvme_subsystem_config(struct spdk_json_write_ctx *w,
     550                 :            :                                  struct spdk_nvmf_subsystem *subsystem)
     551                 :            : {
     552                 :            :         struct spdk_nvmf_host *host;
     553                 :            :         struct spdk_nvmf_ns *ns;
     554                 :          0 :         struct spdk_nvmf_ns_opts ns_opts;
     555                 :            :         uint32_t max_namespaces;
     556                 :            :         struct spdk_nvmf_transport *transport;
     557                 :            : 
     558         [ -  + ]:         22 :         assert(spdk_nvmf_subsystem_get_type(subsystem) == SPDK_NVMF_SUBTYPE_NVME);
     559                 :            : 
     560                 :            :         /* { */
     561                 :         22 :         spdk_json_write_object_begin(w);
     562                 :         22 :         spdk_json_write_named_string(w, "method", "nvmf_create_subsystem");
     563                 :            : 
     564                 :            :         /*     "params" : { */
     565                 :         22 :         spdk_json_write_named_object_begin(w, "params");
     566                 :         22 :         spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     567                 :         22 :         spdk_json_write_named_bool(w, "allow_any_host", spdk_nvmf_subsystem_get_allow_any_host(subsystem));
     568                 :         22 :         spdk_json_write_named_string(w, "serial_number", spdk_nvmf_subsystem_get_sn(subsystem));
     569                 :         22 :         spdk_json_write_named_string(w, "model_number", spdk_nvmf_subsystem_get_mn(subsystem));
     570                 :            : 
     571                 :         22 :         max_namespaces = spdk_nvmf_subsystem_get_max_namespaces(subsystem);
     572         [ +  - ]:         22 :         if (max_namespaces != 0) {
     573                 :         22 :                 spdk_json_write_named_uint32(w, "max_namespaces", max_namespaces);
     574                 :            :         }
     575                 :            : 
     576                 :         22 :         spdk_json_write_named_uint32(w, "min_cntlid", spdk_nvmf_subsystem_get_min_cntlid(subsystem));
     577                 :         22 :         spdk_json_write_named_uint32(w, "max_cntlid", spdk_nvmf_subsystem_get_max_cntlid(subsystem));
     578                 :         22 :         spdk_json_write_named_bool(w, "ana_reporting", spdk_nvmf_subsystem_get_ana_reporting(subsystem));
     579                 :            : 
     580                 :            :         /*     } "params" */
     581                 :         22 :         spdk_json_write_object_end(w);
     582                 :            : 
     583                 :            :         /* } */
     584                 :         22 :         spdk_json_write_object_end(w);
     585                 :            : 
     586         [ +  + ]:         28 :         for (host = spdk_nvmf_subsystem_get_first_host(subsystem); host != NULL;
     587                 :          6 :              host = spdk_nvmf_subsystem_get_next_host(subsystem, host)) {
     588                 :            : 
     589                 :          6 :                 spdk_json_write_object_begin(w);
     590                 :          6 :                 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_host");
     591                 :            : 
     592                 :            :                 /*     "params" : { */
     593                 :          6 :                 spdk_json_write_named_object_begin(w, "params");
     594                 :            : 
     595                 :          6 :                 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     596                 :          6 :                 spdk_json_write_named_string(w, "host", spdk_nvmf_host_get_nqn(host));
     597         [ -  + ]:          6 :                 if (host->dhchap_key != NULL) {
     598                 :          0 :                         spdk_json_write_named_string(w, "dhchap_key",
     599                 :            :                                                      spdk_key_get_name(host->dhchap_key));
     600                 :            :                 }
     601         [ -  + ]:          6 :                 if (host->dhchap_ctrlr_key != NULL) {
     602                 :          0 :                         spdk_json_write_named_string(w, "dhchap_ctrlr_key",
     603                 :            :                                                      spdk_key_get_name(host->dhchap_ctrlr_key));
     604                 :            :                 }
     605         [ +  + ]:         12 :                 TAILQ_FOREACH(transport, &subsystem->tgt->transports, link) {
     606         [ +  - ]:          6 :                         if (transport->ops->subsystem_dump_host != NULL) {
     607                 :          6 :                                 transport->ops->subsystem_dump_host(transport, subsystem, host->nqn, w);
     608                 :            :                         }
     609                 :            :                 }
     610                 :            : 
     611                 :            :                 /*     } "params" */
     612                 :          6 :                 spdk_json_write_object_end(w);
     613                 :            : 
     614                 :            :                 /* } */
     615                 :          6 :                 spdk_json_write_object_end(w);
     616                 :            :         }
     617                 :            : 
     618         [ +  + ]:         60 :         for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
     619                 :         38 :              ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
     620                 :         38 :                 spdk_nvmf_ns_get_opts(ns, &ns_opts, sizeof(ns_opts));
     621                 :            : 
     622                 :         38 :                 spdk_json_write_object_begin(w);
     623                 :         38 :                 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_ns");
     624                 :            : 
     625                 :            :                 /*     "params" : { */
     626                 :         38 :                 spdk_json_write_named_object_begin(w, "params");
     627                 :            : 
     628                 :         38 :                 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     629                 :            : 
     630                 :            :                 /*     "namespace" : { */
     631                 :         38 :                 spdk_json_write_named_object_begin(w, "namespace");
     632                 :            : 
     633                 :         38 :                 spdk_json_write_named_uint32(w, "nsid", spdk_nvmf_ns_get_id(ns));
     634                 :         38 :                 spdk_json_write_named_string(w, "bdev_name", spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns)));
     635                 :            : 
     636         [ -  + ]:         38 :                 if (ns->ptpl_file != NULL) {
     637                 :          0 :                         spdk_json_write_named_string(w, "ptpl_file", ns->ptpl_file);
     638                 :            :                 }
     639                 :            : 
     640         [ +  - ]:         38 :                 if (!spdk_mem_all_zero(ns_opts.nguid, sizeof(ns_opts.nguid))) {
     641                 :            :                         SPDK_STATIC_ASSERT(sizeof(ns_opts.nguid) == sizeof(uint64_t) * 2, "size mismatch");
     642                 :         38 :                         spdk_json_write_named_string_fmt(w, "nguid", "%016"PRIX64"%016"PRIX64, from_be64(&ns_opts.nguid[0]),
     643                 :            :                                                          from_be64(&ns_opts.nguid[8]));
     644                 :            :                 }
     645                 :            : 
     646         [ -  + ]:         38 :                 if (!spdk_mem_all_zero(ns_opts.eui64, sizeof(ns_opts.eui64))) {
     647                 :            :                         SPDK_STATIC_ASSERT(sizeof(ns_opts.eui64) == sizeof(uint64_t), "size mismatch");
     648                 :          0 :                         spdk_json_write_named_string_fmt(w, "eui64", "%016"PRIX64, from_be64(&ns_opts.eui64));
     649                 :            :                 }
     650                 :            : 
     651         [ +  - ]:         38 :                 if (!spdk_uuid_is_null(&ns_opts.uuid)) {
     652                 :         38 :                         spdk_json_write_named_uuid(w, "uuid",  &ns_opts.uuid);
     653                 :            :                 }
     654                 :            : 
     655         [ -  + ]:         38 :                 if (spdk_nvmf_subsystem_get_ana_reporting(subsystem)) {
     656                 :          0 :                         spdk_json_write_named_uint32(w, "anagrpid", ns_opts.anagrpid);
     657                 :            :                 }
     658                 :            : 
     659         [ -  + ]:         38 :                 spdk_json_write_named_bool(w, "no_auto_visible", !ns->always_visible);
     660                 :            : 
     661                 :            :                 /*     "namespace" */
     662                 :         38 :                 spdk_json_write_object_end(w);
     663                 :            : 
     664                 :            :                 /*     } "params" */
     665                 :         38 :                 spdk_json_write_object_end(w);
     666                 :            : 
     667                 :            :                 /* } */
     668                 :         38 :                 spdk_json_write_object_end(w);
     669                 :            :         }
     670                 :         22 : }
     671                 :            : 
     672                 :            : static void
     673                 :        128 : nvmf_write_subsystem_config_json(struct spdk_json_write_ctx *w,
     674                 :            :                                  struct spdk_nvmf_subsystem *subsystem)
     675                 :            : {
     676                 :            :         struct spdk_nvmf_subsystem_listener *listener;
     677                 :            :         struct spdk_nvmf_transport *transport;
     678                 :            :         const struct spdk_nvme_transport_id *trid;
     679                 :            : 
     680         [ +  + ]:        128 :         if (spdk_nvmf_subsystem_get_type(subsystem) == SPDK_NVMF_SUBTYPE_NVME) {
     681                 :         22 :                 nvmf_write_nvme_subsystem_config(w, subsystem);
     682                 :            :         }
     683                 :            : 
     684         [ +  + ]:        150 :         for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL;
     685                 :         22 :              listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) {
     686                 :         22 :                 transport = listener->transport;
     687                 :         22 :                 trid = spdk_nvmf_subsystem_listener_get_trid(listener);
     688                 :            : 
     689                 :         22 :                 spdk_json_write_object_begin(w);
     690                 :         22 :                 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_listener");
     691                 :            : 
     692                 :            :                 /*     "params" : { */
     693                 :         22 :                 spdk_json_write_named_object_begin(w, "params");
     694                 :            : 
     695                 :         22 :                 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     696                 :            : 
     697                 :         22 :                 spdk_json_write_named_object_begin(w, "listen_address");
     698                 :         22 :                 nvmf_transport_listen_dump_trid(trid, w);
     699                 :         22 :                 spdk_json_write_object_end(w);
     700         [ -  + ]:         22 :                 if (transport->ops->listen_dump_opts) {
     701                 :          0 :                         transport->ops->listen_dump_opts(transport, trid, w);
     702                 :            :                 }
     703                 :            : 
     704         [ -  + ]:         22 :                 spdk_json_write_named_bool(w, "secure_channel", listener->opts.secure_channel);
     705                 :            : 
     706                 :            :                 /*     } "params" */
     707                 :         22 :                 spdk_json_write_object_end(w);
     708                 :            : 
     709                 :            :                 /* } */
     710                 :         22 :                 spdk_json_write_object_end(w);
     711                 :            :         }
     712                 :        128 : }
     713                 :            : 
     714                 :            : void
     715                 :        106 : spdk_nvmf_tgt_write_config_json(struct spdk_json_write_ctx *w, struct spdk_nvmf_tgt *tgt)
     716                 :            : {
     717                 :            :         struct spdk_nvmf_subsystem *subsystem;
     718                 :            :         struct spdk_nvmf_transport *transport;
     719                 :            :         struct spdk_nvmf_referral *referral;
     720                 :            : 
     721                 :        106 :         spdk_json_write_object_begin(w);
     722                 :        106 :         spdk_json_write_named_string(w, "method", "nvmf_set_max_subsystems");
     723                 :            : 
     724                 :        106 :         spdk_json_write_named_object_begin(w, "params");
     725                 :        106 :         spdk_json_write_named_uint32(w, "max_subsystems", tgt->max_subsystems);
     726                 :        106 :         spdk_json_write_object_end(w);
     727                 :            : 
     728                 :        106 :         spdk_json_write_object_end(w);
     729                 :            : 
     730                 :        106 :         spdk_json_write_object_begin(w);
     731                 :        106 :         spdk_json_write_named_string(w, "method", "nvmf_set_crdt");
     732                 :        106 :         spdk_json_write_named_object_begin(w, "params");
     733                 :        106 :         spdk_json_write_named_uint32(w, "crdt1", tgt->crdt[0]);
     734                 :        106 :         spdk_json_write_named_uint32(w, "crdt2", tgt->crdt[1]);
     735                 :        106 :         spdk_json_write_named_uint32(w, "crdt3", tgt->crdt[2]);
     736                 :        106 :         spdk_json_write_object_end(w);
     737                 :        106 :         spdk_json_write_object_end(w);
     738                 :            : 
     739                 :            :         /* write transports */
     740         [ +  + ]:        151 :         TAILQ_FOREACH(transport, &tgt->transports, link) {
     741                 :         45 :                 spdk_json_write_object_begin(w);
     742                 :         45 :                 spdk_json_write_named_string(w, "method", "nvmf_create_transport");
     743                 :         45 :                 nvmf_transport_dump_opts(transport, w, true);
     744                 :         45 :                 spdk_json_write_object_end(w);
     745                 :            :         }
     746                 :            : 
     747         [ -  + ]:        106 :         TAILQ_FOREACH(referral, &tgt->referrals, link) {
     748                 :          0 :                 spdk_json_write_object_begin(w);
     749                 :          0 :                 spdk_json_write_named_string(w, "method", "nvmf_discovery_add_referral");
     750                 :            : 
     751                 :          0 :                 spdk_json_write_named_object_begin(w, "params");
     752                 :          0 :                 spdk_json_write_named_object_begin(w, "address");
     753                 :          0 :                 nvmf_transport_listen_dump_trid(&referral->trid, w);
     754                 :          0 :                 spdk_json_write_object_end(w);
     755                 :          0 :                 spdk_json_write_named_bool(w, "secure_channel",
     756                 :          0 :                                            referral->entry.treq.secure_channel ==
     757                 :            :                                            SPDK_NVMF_TREQ_SECURE_CHANNEL_REQUIRED);
     758                 :          0 :                 spdk_json_write_named_string(w, "subnqn", referral->trid.subnqn);
     759                 :          0 :                 spdk_json_write_object_end(w);
     760                 :            : 
     761                 :          0 :                 spdk_json_write_object_end(w);
     762                 :            :         }
     763                 :            : 
     764                 :        106 :         subsystem = spdk_nvmf_subsystem_get_first(tgt);
     765         [ +  + ]:        234 :         while (subsystem) {
     766                 :        128 :                 nvmf_write_subsystem_config_json(w, subsystem);
     767                 :        128 :                 subsystem = spdk_nvmf_subsystem_get_next(subsystem);
     768                 :            :         }
     769                 :        106 : }
     770                 :            : 
     771                 :            : static void
     772                 :        969 : nvmf_listen_opts_copy(struct spdk_nvmf_listen_opts *opts,
     773                 :            :                       const struct spdk_nvmf_listen_opts *opts_src, size_t opts_size)
     774                 :            : {
     775         [ -  + ]:        969 :         assert(opts);
     776         [ -  + ]:        969 :         assert(opts_src);
     777                 :            : 
     778                 :        969 :         opts->opts_size = opts_size;
     779                 :            : 
     780                 :            : #define SET_FIELD(field) \
     781                 :            :     if (offsetof(struct spdk_nvmf_listen_opts, field) + sizeof(opts->field) <= opts_size) { \
     782                 :            :                  opts->field = opts_src->field; \
     783                 :            :     } \
     784                 :            : 
     785         [ +  - ]:        969 :         SET_FIELD(transport_specific);
     786   [ +  -  -  + ]:        969 :         SET_FIELD(secure_channel);
     787         [ +  - ]:        969 :         SET_FIELD(ana_state);
     788                 :            : #undef SET_FIELD
     789                 :            : 
     790                 :            :         /* Do not remove this statement, you should always update this statement when you adding a new field,
     791                 :            :          * and do not forget to add the SET_FIELD statement for your added field. */
     792                 :            :         SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_listen_opts) == 24, "Incorrect size");
     793                 :        969 : }
     794                 :            : 
     795                 :            : void
     796                 :        486 : spdk_nvmf_listen_opts_init(struct spdk_nvmf_listen_opts *opts, size_t opts_size)
     797                 :            : {
     798                 :        486 :         struct spdk_nvmf_listen_opts opts_local = {};
     799                 :            : 
     800                 :            :         /* local version of opts should have defaults set here */
     801                 :        486 :         opts_local.ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
     802                 :        486 :         nvmf_listen_opts_copy(opts, &opts_local, opts_size);
     803                 :        486 : }
     804                 :            : 
     805                 :            : int
     806                 :        483 : spdk_nvmf_tgt_listen_ext(struct spdk_nvmf_tgt *tgt, const struct spdk_nvme_transport_id *trid,
     807                 :            :                          struct spdk_nvmf_listen_opts *opts)
     808                 :            : {
     809                 :            :         struct spdk_nvmf_transport *transport;
     810                 :            :         int rc;
     811                 :        483 :         struct spdk_nvmf_listen_opts opts_local = {};
     812                 :            : 
     813         [ -  + ]:        483 :         if (!opts) {
     814                 :          0 :                 SPDK_ERRLOG("opts should not be NULL\n");
     815                 :          0 :                 return -EINVAL;
     816                 :            :         }
     817                 :            : 
     818         [ -  + ]:        483 :         if (!opts->opts_size) {
     819                 :          0 :                 SPDK_ERRLOG("The opts_size in opts structure should not be zero\n");
     820                 :          0 :                 return -EINVAL;
     821                 :            :         }
     822                 :            : 
     823                 :        483 :         transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring);
     824         [ -  + ]:        483 :         if (!transport) {
     825                 :          0 :                 SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
     826                 :            :                             trid->trstring);
     827                 :          0 :                 return -EINVAL;
     828                 :            :         }
     829                 :            : 
     830                 :        483 :         nvmf_listen_opts_copy(&opts_local, opts, opts->opts_size);
     831                 :        483 :         rc = spdk_nvmf_transport_listen(transport, trid, &opts_local);
     832         [ -  + ]:        483 :         if (rc < 0) {
     833                 :          0 :                 SPDK_ERRLOG("Unable to listen on address '%s'\n", trid->traddr);
     834                 :            :         }
     835                 :            : 
     836                 :        483 :         return rc;
     837                 :            : }
     838                 :            : 
     839                 :            : int
     840                 :          0 : spdk_nvmf_tgt_stop_listen(struct spdk_nvmf_tgt *tgt,
     841                 :            :                           struct spdk_nvme_transport_id *trid)
     842                 :            : {
     843                 :            :         struct spdk_nvmf_transport *transport;
     844                 :            :         int rc;
     845                 :            : 
     846                 :          0 :         transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring);
     847         [ #  # ]:          0 :         if (!transport) {
     848                 :          0 :                 SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
     849                 :            :                             trid->trstring);
     850                 :          0 :                 return -EINVAL;
     851                 :            :         }
     852                 :            : 
     853                 :          0 :         rc = spdk_nvmf_transport_stop_listen(transport, trid);
     854         [ #  # ]:          0 :         if (rc < 0) {
     855                 :          0 :                 SPDK_ERRLOG("Failed to stop listening on address '%s'\n", trid->traddr);
     856                 :          0 :                 return rc;
     857                 :            :         }
     858                 :          0 :         return 0;
     859                 :            : }
     860                 :            : 
     861                 :            : struct spdk_nvmf_tgt_add_transport_ctx {
     862                 :            :         struct spdk_nvmf_tgt *tgt;
     863                 :            :         struct spdk_nvmf_transport *transport;
     864                 :            :         spdk_nvmf_tgt_add_transport_done_fn cb_fn;
     865                 :            :         void *cb_arg;
     866                 :            :         int status;
     867                 :            : };
     868                 :            : 
     869                 :            : static void
     870                 :          0 : _nvmf_tgt_remove_transport_done(struct spdk_io_channel_iter *i, int status)
     871                 :            : {
     872                 :          0 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     873                 :            : 
     874                 :          0 :         ctx->cb_fn(ctx->cb_arg, ctx->status);
     875                 :          0 :         free(ctx);
     876                 :          0 : }
     877                 :            : 
     878                 :            : static void
     879                 :          0 : _nvmf_tgt_remove_transport(struct spdk_io_channel_iter *i)
     880                 :            : {
     881                 :          0 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     882                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     883                 :          0 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
     884                 :            :         struct spdk_nvmf_transport_poll_group *tgroup, *tmp;
     885                 :            : 
     886         [ #  # ]:          0 :         TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) {
     887         [ #  # ]:          0 :                 if (tgroup->transport == ctx->transport) {
     888         [ #  # ]:          0 :                         TAILQ_REMOVE(&group->tgroups, tgroup, link);
     889                 :          0 :                         nvmf_transport_poll_group_destroy(tgroup);
     890                 :            :                 }
     891                 :            :         }
     892                 :            : 
     893                 :          0 :         spdk_for_each_channel_continue(i, 0);
     894                 :          0 : }
     895                 :            : 
     896                 :            : static void
     897                 :        256 : _nvmf_tgt_add_transport_done(struct spdk_io_channel_iter *i, int status)
     898                 :            : {
     899                 :        256 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     900                 :            : 
     901         [ -  + ]:        256 :         if (status) {
     902                 :          0 :                 ctx->status = status;
     903                 :          0 :                 spdk_for_each_channel(ctx->tgt,
     904                 :            :                                       _nvmf_tgt_remove_transport,
     905                 :            :                                       ctx,
     906                 :            :                                       _nvmf_tgt_remove_transport_done);
     907                 :          0 :                 return;
     908                 :            :         }
     909                 :            : 
     910                 :        256 :         ctx->transport->tgt = ctx->tgt;
     911                 :        256 :         TAILQ_INSERT_TAIL(&ctx->tgt->transports, ctx->transport, link);
     912                 :        256 :         ctx->cb_fn(ctx->cb_arg, status);
     913                 :        256 :         free(ctx);
     914                 :            : }
     915                 :            : 
     916                 :            : static void
     917                 :        584 : _nvmf_tgt_add_transport(struct spdk_io_channel_iter *i)
     918                 :            : {
     919                 :        584 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     920                 :        584 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     921                 :        584 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
     922                 :            :         int rc;
     923                 :            : 
     924                 :        584 :         rc = nvmf_poll_group_add_transport(group, ctx->transport);
     925                 :        584 :         spdk_for_each_channel_continue(i, rc);
     926                 :        584 : }
     927                 :            : 
     928                 :            : void
     929                 :        256 : spdk_nvmf_tgt_add_transport(struct spdk_nvmf_tgt *tgt,
     930                 :            :                             struct spdk_nvmf_transport *transport,
     931                 :            :                             spdk_nvmf_tgt_add_transport_done_fn cb_fn,
     932                 :            :                             void *cb_arg)
     933                 :            : {
     934                 :            :         struct spdk_nvmf_tgt_add_transport_ctx *ctx;
     935                 :            : 
     936                 :        100 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_add_transport, transport, tgt->name);
     937                 :            : 
     938         [ -  + ]:        256 :         if (spdk_nvmf_tgt_get_transport(tgt, transport->ops->name)) {
     939                 :          0 :                 cb_fn(cb_arg, -EEXIST);
     940                 :          0 :                 return; /* transport already created */
     941                 :            :         }
     942                 :            : 
     943                 :        256 :         ctx = calloc(1, sizeof(*ctx));
     944         [ -  + ]:        256 :         if (!ctx) {
     945                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
     946                 :          0 :                 return;
     947                 :            :         }
     948                 :            : 
     949                 :        256 :         ctx->tgt = tgt;
     950                 :        256 :         ctx->transport = transport;
     951                 :        256 :         ctx->cb_fn = cb_fn;
     952                 :        256 :         ctx->cb_arg = cb_arg;
     953                 :            : 
     954                 :        256 :         spdk_for_each_channel(tgt,
     955                 :            :                               _nvmf_tgt_add_transport,
     956                 :            :                               ctx,
     957                 :            :                               _nvmf_tgt_add_transport_done);
     958                 :            : }
     959                 :            : 
     960                 :            : struct nvmf_tgt_pause_ctx {
     961                 :            :         struct spdk_nvmf_tgt *tgt;
     962                 :            :         spdk_nvmf_tgt_pause_polling_cb_fn cb_fn;
     963                 :            :         void *cb_arg;
     964                 :            : };
     965                 :            : 
     966                 :            : static void
     967                 :          0 : _nvmf_tgt_pause_polling_done(struct spdk_io_channel_iter *i, int status)
     968                 :            : {
     969                 :          0 :         struct nvmf_tgt_pause_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     970                 :            : 
     971                 :          0 :         ctx->tgt->state = NVMF_TGT_PAUSED;
     972                 :            : 
     973                 :          0 :         ctx->cb_fn(ctx->cb_arg, status);
     974                 :          0 :         free(ctx);
     975                 :          0 : }
     976                 :            : 
     977                 :            : static void
     978                 :          0 : _nvmf_tgt_pause_polling(struct spdk_io_channel_iter *i)
     979                 :            : {
     980                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     981                 :          0 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
     982                 :            : 
     983                 :          0 :         spdk_poller_unregister(&group->poller);
     984                 :            : 
     985                 :          0 :         spdk_for_each_channel_continue(i, 0);
     986                 :          0 : }
     987                 :            : 
     988                 :            : int
     989                 :          0 : spdk_nvmf_tgt_pause_polling(struct spdk_nvmf_tgt *tgt, spdk_nvmf_tgt_pause_polling_cb_fn cb_fn,
     990                 :            :                             void *cb_arg)
     991                 :            : {
     992                 :            :         struct nvmf_tgt_pause_ctx *ctx;
     993                 :            : 
     994                 :          0 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_pause_polling, tgt, tgt->name);
     995                 :            : 
     996      [ #  #  # ]:          0 :         switch (tgt->state) {
     997                 :          0 :         case NVMF_TGT_PAUSING:
     998                 :            :         case NVMF_TGT_RESUMING:
     999                 :          0 :                 return -EBUSY;
    1000                 :          0 :         case NVMF_TGT_RUNNING:
    1001                 :          0 :                 break;
    1002                 :          0 :         default:
    1003                 :          0 :                 return -EINVAL;
    1004                 :            :         }
    1005                 :            : 
    1006                 :          0 :         ctx = calloc(1, sizeof(*ctx));
    1007         [ #  # ]:          0 :         if (!ctx) {
    1008                 :          0 :                 return -ENOMEM;
    1009                 :            :         }
    1010                 :            : 
    1011                 :            : 
    1012                 :          0 :         tgt->state = NVMF_TGT_PAUSING;
    1013                 :            : 
    1014                 :          0 :         ctx->tgt = tgt;
    1015                 :          0 :         ctx->cb_fn = cb_fn;
    1016                 :          0 :         ctx->cb_arg = cb_arg;
    1017                 :            : 
    1018                 :          0 :         spdk_for_each_channel(tgt,
    1019                 :            :                               _nvmf_tgt_pause_polling,
    1020                 :            :                               ctx,
    1021                 :            :                               _nvmf_tgt_pause_polling_done);
    1022                 :          0 :         return 0;
    1023                 :            : }
    1024                 :            : 
    1025                 :            : static void
    1026                 :          0 : _nvmf_tgt_resume_polling_done(struct spdk_io_channel_iter *i, int status)
    1027                 :            : {
    1028                 :          0 :         struct nvmf_tgt_pause_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    1029                 :            : 
    1030                 :          0 :         ctx->tgt->state = NVMF_TGT_RUNNING;
    1031                 :            : 
    1032                 :          0 :         ctx->cb_fn(ctx->cb_arg, status);
    1033                 :          0 :         free(ctx);
    1034                 :          0 : }
    1035                 :            : 
    1036                 :            : static void
    1037                 :          0 : _nvmf_tgt_resume_polling(struct spdk_io_channel_iter *i)
    1038                 :            : {
    1039                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
    1040                 :          0 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
    1041                 :            : 
    1042         [ #  # ]:          0 :         assert(group->poller == NULL);
    1043                 :          0 :         group->poller = SPDK_POLLER_REGISTER(nvmf_poll_group_poll, group, 0);
    1044                 :            : 
    1045                 :          0 :         spdk_for_each_channel_continue(i, 0);
    1046                 :          0 : }
    1047                 :            : 
    1048                 :            : int
    1049                 :          0 : spdk_nvmf_tgt_resume_polling(struct spdk_nvmf_tgt *tgt, spdk_nvmf_tgt_resume_polling_cb_fn cb_fn,
    1050                 :            :                              void *cb_arg)
    1051                 :            : {
    1052                 :            :         struct nvmf_tgt_pause_ctx *ctx;
    1053                 :            : 
    1054                 :          0 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_resume_polling, tgt, tgt->name);
    1055                 :            : 
    1056      [ #  #  # ]:          0 :         switch (tgt->state) {
    1057                 :          0 :         case NVMF_TGT_PAUSING:
    1058                 :            :         case NVMF_TGT_RESUMING:
    1059                 :          0 :                 return -EBUSY;
    1060                 :          0 :         case NVMF_TGT_PAUSED:
    1061                 :          0 :                 break;
    1062                 :          0 :         default:
    1063                 :          0 :                 return -EINVAL;
    1064                 :            :         }
    1065                 :            : 
    1066                 :          0 :         ctx = calloc(1, sizeof(*ctx));
    1067         [ #  # ]:          0 :         if (!ctx) {
    1068                 :          0 :                 return -ENOMEM;
    1069                 :            :         }
    1070                 :            : 
    1071                 :          0 :         tgt->state = NVMF_TGT_RESUMING;
    1072                 :            : 
    1073                 :          0 :         ctx->tgt = tgt;
    1074                 :          0 :         ctx->cb_fn = cb_fn;
    1075                 :          0 :         ctx->cb_arg = cb_arg;
    1076                 :            : 
    1077                 :          0 :         spdk_for_each_channel(tgt,
    1078                 :            :                               _nvmf_tgt_resume_polling,
    1079                 :            :                               ctx,
    1080                 :            :                               _nvmf_tgt_resume_polling_done);
    1081                 :          0 :         return 0;
    1082                 :            : }
    1083                 :            : 
    1084                 :            : struct spdk_nvmf_subsystem *
    1085                 :      34709 : spdk_nvmf_tgt_find_subsystem(struct spdk_nvmf_tgt *tgt, const char *subnqn)
    1086                 :            : {
    1087                 :        932 :         struct spdk_nvmf_subsystem subsystem;
    1088                 :            : 
    1089         [ -  + ]:      34709 :         if (!subnqn) {
    1090                 :          0 :                 return NULL;
    1091                 :            :         }
    1092                 :            : 
    1093                 :            :         /* Ensure that subnqn is null terminated */
    1094   [ -  +  -  + ]:      34709 :         if (!memchr(subnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) {
    1095                 :          0 :                 SPDK_ERRLOG("Connect SUBNQN is not null terminated\n");
    1096                 :          0 :                 return NULL;
    1097                 :            :         }
    1098                 :            : 
    1099                 :      34709 :         snprintf(subsystem.subnqn, sizeof(subsystem.subnqn), "%s", subnqn);
    1100                 :      34709 :         return RB_FIND(subsystem_tree, &tgt->subsystems, &subsystem);
    1101                 :            : }
    1102                 :            : 
    1103                 :            : struct spdk_nvmf_transport *
    1104                 :       1985 : spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, const char *transport_name)
    1105                 :            : {
    1106                 :            :         struct spdk_nvmf_transport *transport;
    1107                 :            : 
    1108         [ +  + ]:       1985 :         TAILQ_FOREACH(transport, &tgt->transports, link) {
    1109   [ -  +  -  +  :       1454 :                 if (!strncasecmp(transport->ops->name, transport_name, SPDK_NVMF_TRSTRING_MAX_LEN)) {
                   +  - ]
    1110                 :       1454 :                         return transport;
    1111                 :            :                 }
    1112                 :            :         }
    1113                 :        531 :         return NULL;
    1114                 :            : }
    1115                 :            : 
    1116                 :            : struct nvmf_new_qpair_ctx {
    1117                 :            :         struct spdk_nvmf_qpair *qpair;
    1118                 :            :         struct spdk_nvmf_poll_group *group;
    1119                 :            : };
    1120                 :            : 
    1121                 :            : static void
    1122                 :       6266 : _nvmf_poll_group_add(void *_ctx)
    1123                 :            : {
    1124                 :       6266 :         struct nvmf_new_qpair_ctx *ctx = _ctx;
    1125                 :       6266 :         struct spdk_nvmf_qpair *qpair = ctx->qpair;
    1126                 :       6266 :         struct spdk_nvmf_poll_group *group = ctx->group;
    1127                 :            : 
    1128                 :       6266 :         free(_ctx);
    1129                 :            : 
    1130         [ -  + ]:       6266 :         if (spdk_nvmf_poll_group_add(group, qpair) != 0) {
    1131                 :          0 :                 SPDK_ERRLOG("Unable to add the qpair to a poll group.\n");
    1132                 :          0 :                 spdk_nvmf_qpair_disconnect(qpair);
    1133                 :            :         }
    1134                 :       6266 : }
    1135                 :            : 
    1136                 :            : void
    1137                 :       6266 : spdk_nvmf_tgt_new_qpair(struct spdk_nvmf_tgt *tgt, struct spdk_nvmf_qpair *qpair)
    1138                 :            : {
    1139                 :            :         struct spdk_nvmf_poll_group *group;
    1140                 :            :         struct nvmf_new_qpair_ctx *ctx;
    1141                 :            : 
    1142                 :       6266 :         group = spdk_nvmf_get_optimal_poll_group(qpair);
    1143         [ -  + ]:       6266 :         if (group == NULL) {
    1144         [ #  # ]:          0 :                 if (tgt->next_poll_group == NULL) {
    1145                 :          0 :                         tgt->next_poll_group = TAILQ_FIRST(&tgt->poll_groups);
    1146         [ #  # ]:          0 :                         if (tgt->next_poll_group == NULL) {
    1147                 :          0 :                                 SPDK_ERRLOG("No poll groups exist.\n");
    1148                 :          0 :                                 spdk_nvmf_qpair_disconnect(qpair);
    1149                 :          0 :                                 return;
    1150                 :            :                         }
    1151                 :            :                 }
    1152                 :          0 :                 group = tgt->next_poll_group;
    1153                 :          0 :                 tgt->next_poll_group = TAILQ_NEXT(group, link);
    1154                 :            :         }
    1155                 :            : 
    1156                 :       6266 :         ctx = calloc(1, sizeof(*ctx));
    1157         [ -  + ]:       6266 :         if (!ctx) {
    1158                 :          0 :                 SPDK_ERRLOG("Unable to send message to poll group.\n");
    1159                 :          0 :                 spdk_nvmf_qpair_disconnect(qpair);
    1160                 :          0 :                 return;
    1161                 :            :         }
    1162                 :            : 
    1163                 :       6266 :         ctx->qpair = qpair;
    1164                 :       6266 :         ctx->group = group;
    1165                 :            : 
    1166         [ -  + ]:       6266 :         pthread_mutex_lock(&group->mutex);
    1167                 :       6266 :         group->current_unassociated_qpairs++;
    1168         [ -  + ]:       6266 :         pthread_mutex_unlock(&group->mutex);
    1169                 :            : 
    1170                 :       6266 :         spdk_thread_send_msg(group->thread, _nvmf_poll_group_add, ctx);
    1171                 :            : }
    1172                 :            : 
    1173                 :            : struct spdk_nvmf_poll_group *
    1174                 :       1268 : spdk_nvmf_poll_group_create(struct spdk_nvmf_tgt *tgt)
    1175                 :            : {
    1176                 :            :         struct spdk_io_channel *ch;
    1177                 :            : 
    1178                 :       1268 :         ch = spdk_get_io_channel(tgt);
    1179         [ -  + ]:       1268 :         if (!ch) {
    1180                 :          0 :                 SPDK_ERRLOG("Unable to get I/O channel for target\n");
    1181                 :          0 :                 return NULL;
    1182                 :            :         }
    1183                 :            : 
    1184                 :       1268 :         return spdk_io_channel_get_ctx(ch);
    1185                 :            : }
    1186                 :            : 
    1187                 :            : void
    1188                 :       1268 : spdk_nvmf_poll_group_destroy(struct spdk_nvmf_poll_group *group,
    1189                 :            :                              spdk_nvmf_poll_group_destroy_done_fn cb_fn,
    1190                 :            :                              void *cb_arg)
    1191                 :            : {
    1192         [ -  + ]:       1268 :         assert(group->destroy_cb_fn == NULL);
    1193                 :       1268 :         group->destroy_cb_fn = cb_fn;
    1194                 :       1268 :         group->destroy_cb_arg = cb_arg;
    1195                 :            : 
    1196                 :            :         /* This function will put the io_channel associated with this poll group */
    1197                 :       1268 :         nvmf_tgt_destroy_poll_group_qpairs(group);
    1198                 :       1268 : }
    1199                 :            : 
    1200                 :            : int
    1201                 :       6266 : spdk_nvmf_poll_group_add(struct spdk_nvmf_poll_group *group,
    1202                 :            :                          struct spdk_nvmf_qpair *qpair)
    1203                 :            : {
    1204                 :       6266 :         int rc = -1;
    1205                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1206                 :            : 
    1207                 :       6266 :         TAILQ_INIT(&qpair->outstanding);
    1208                 :       6266 :         qpair->group = group;
    1209                 :       6266 :         qpair->ctrlr = NULL;
    1210                 :       6266 :         qpair->disconnect_started = false;
    1211                 :            : 
    1212         [ +  - ]:       6266 :         TAILQ_FOREACH(tgroup, &group->tgroups, link) {
    1213         [ +  - ]:       6266 :                 if (tgroup->transport == qpair->transport) {
    1214                 :       6266 :                         rc = nvmf_transport_poll_group_add(tgroup, qpair);
    1215                 :       6266 :                         break;
    1216                 :            :                 }
    1217                 :            :         }
    1218                 :            : 
    1219                 :            :         /* We add the qpair to the group only it is successfully added into the tgroup */
    1220         [ +  - ]:       6266 :         if (rc == 0) {
    1221                 :       2335 :                 SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_add_qpair, qpair, spdk_thread_get_id(group->thread));
    1222                 :       6266 :                 TAILQ_INSERT_TAIL(&group->qpairs, qpair, link);
    1223                 :       6266 :                 nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_CONNECTING);
    1224                 :            :         }
    1225                 :            : 
    1226                 :       6266 :         return rc;
    1227                 :            : }
    1228                 :            : 
    1229                 :            : static void
    1230                 :       1659 : _nvmf_ctrlr_destruct(void *ctx)
    1231                 :            : {
    1232                 :       1659 :         struct spdk_nvmf_ctrlr *ctrlr = ctx;
    1233                 :            : 
    1234                 :       1659 :         nvmf_ctrlr_destruct(ctrlr);
    1235                 :       1659 : }
    1236                 :            : 
    1237                 :            : static void
    1238                 :       6242 : _nvmf_ctrlr_free_from_qpair(void *ctx)
    1239                 :            : {
    1240                 :       6242 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
    1241                 :       6242 :         struct spdk_nvmf_ctrlr *ctrlr = qpair_ctx->ctrlr;
    1242                 :            :         uint32_t count;
    1243                 :            : 
    1244                 :       6242 :         spdk_bit_array_clear(ctrlr->qpair_mask, qpair_ctx->qid);
    1245                 :       6242 :         count = spdk_bit_array_count_set(ctrlr->qpair_mask);
    1246         [ +  + ]:       6242 :         if (count == 0) {
    1247   [ -  +  -  + ]:       1659 :                 assert(!ctrlr->in_destruct);
    1248   [ -  +  -  + ]:       1659 :                 SPDK_DEBUGLOG(nvmf, "Last qpair %u, destroy ctrlr 0x%hx\n", qpair_ctx->qid, ctrlr->cntlid);
    1249                 :       1659 :                 ctrlr->in_destruct = true;
    1250                 :       1659 :                 spdk_thread_send_msg(ctrlr->subsys->thread, _nvmf_ctrlr_destruct, ctrlr);
    1251                 :            :         }
    1252                 :       6242 :         free(qpair_ctx);
    1253                 :       6242 : }
    1254                 :            : 
    1255                 :            : static void
    1256                 :       6266 : _nvmf_transport_qpair_fini_complete(void *cb_ctx)
    1257                 :            : {
    1258                 :       6266 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = cb_ctx;
    1259                 :            :         struct spdk_nvmf_ctrlr *ctrlr;
    1260                 :            : 
    1261                 :       6266 :         ctrlr = qpair_ctx->ctrlr;
    1262   [ -  +  -  + ]:       6266 :         SPDK_DEBUGLOG(nvmf, "Finish destroying qid %u\n", qpair_ctx->qid);
    1263                 :            : 
    1264         [ +  + ]:       6266 :         if (ctrlr) {
    1265         [ +  + ]:       6242 :                 if (qpair_ctx->qid == 0) {
    1266                 :            :                         /* Admin qpair is removed, so set the pointer to NULL.
    1267                 :            :                          * This operation is safe since we are on ctrlr thread now, admin qpair's thread is the same
    1268                 :            :                          * as controller's thread */
    1269         [ -  + ]:       1659 :                         assert(ctrlr->thread == spdk_get_thread());
    1270                 :       1659 :                         ctrlr->admin_qpair = NULL;
    1271                 :            :                 }
    1272                 :            :                 /* Free qpair id from controller's bit mask and destroy the controller if it is the last qpair */
    1273         [ +  - ]:       6242 :                 if (ctrlr->thread) {
    1274                 :       6242 :                         spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_free_from_qpair, qpair_ctx);
    1275                 :            :                 } else {
    1276                 :          0 :                         _nvmf_ctrlr_free_from_qpair(qpair_ctx);
    1277                 :            :                 }
    1278                 :            :         } else {
    1279                 :         24 :                 free(qpair_ctx);
    1280                 :            :         }
    1281                 :       6266 : }
    1282                 :            : 
    1283                 :            : void
    1284                 :       6266 : spdk_nvmf_poll_group_remove(struct spdk_nvmf_qpair *qpair)
    1285                 :            : {
    1286                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1287                 :            :         int rc;
    1288                 :            : 
    1289                 :       2335 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_remove_qpair, qpair,
    1290                 :            :                                  spdk_thread_get_id(qpair->group->thread));
    1291                 :       6266 :         nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ERROR);
    1292                 :            : 
    1293                 :            :         /* Find the tgroup and remove the qpair from the tgroup */
    1294         [ +  - ]:       6266 :         TAILQ_FOREACH(tgroup, &qpair->group->tgroups, link) {
    1295         [ +  - ]:       6266 :                 if (tgroup->transport == qpair->transport) {
    1296                 :       6266 :                         rc = nvmf_transport_poll_group_remove(tgroup, qpair);
    1297   [ -  +  -  - ]:       6266 :                         if (rc && (rc != ENOTSUP)) {
    1298                 :          0 :                                 SPDK_ERRLOG("Cannot remove qpair=%p from transport group=%p\n",
    1299                 :            :                                             qpair, tgroup);
    1300                 :            :                         }
    1301                 :       6266 :                         break;
    1302                 :            :                 }
    1303                 :            :         }
    1304                 :            : 
    1305         [ +  + ]:       6266 :         TAILQ_REMOVE(&qpair->group->qpairs, qpair, link);
    1306                 :       6266 :         qpair->group = NULL;
    1307                 :       6266 : }
    1308                 :            : 
    1309                 :            : static void
    1310                 :      30818 : _nvmf_qpair_sgroup_req_clean(struct spdk_nvmf_subsystem_poll_group *sgroup,
    1311                 :            :                              const struct spdk_nvmf_qpair *qpair)
    1312                 :            : {
    1313                 :            :         struct spdk_nvmf_request *req, *tmp;
    1314         [ -  + ]:      30818 :         TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
    1315         [ #  # ]:          0 :                 if (req->qpair == qpair) {
    1316         [ #  # ]:          0 :                         TAILQ_REMOVE(&sgroup->queued, req, link);
    1317         [ #  # ]:          0 :                         if (nvmf_transport_req_free(req)) {
    1318                 :          0 :                                 SPDK_ERRLOG("Transport request free error!\n");
    1319                 :            :                         }
    1320                 :            :                 }
    1321                 :            :         }
    1322                 :      30818 : }
    1323                 :            : 
    1324                 :            : static void
    1325                 :       6266 : _nvmf_qpair_destroy(void *ctx, int status)
    1326                 :            : {
    1327                 :       6266 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
    1328                 :       6266 :         struct spdk_nvmf_qpair *qpair = qpair_ctx->qpair;
    1329                 :       6266 :         struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
    1330                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1331                 :            :         uint32_t sid;
    1332                 :            : 
    1333         [ -  + ]:       6266 :         assert(qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING);
    1334                 :       6266 :         qpair_ctx->qid = qpair->qid;
    1335                 :            : 
    1336   [ -  +  +  + ]:       6266 :         if (qpair->connect_received) {
    1337         [ +  + ]:       6242 :                 if (0 == qpair->qid) {
    1338         [ -  + ]:       1659 :                         assert(qpair->group->stat.current_admin_qpairs > 0);
    1339                 :       1659 :                         qpair->group->stat.current_admin_qpairs--;
    1340                 :            :                 } else {
    1341         [ -  + ]:       4583 :                         assert(qpair->group->stat.current_io_qpairs > 0);
    1342                 :       4583 :                         qpair->group->stat.current_io_qpairs--;
    1343                 :            :                 }
    1344                 :            :         } else {
    1345         [ -  + ]:         24 :                 pthread_mutex_lock(&qpair->group->mutex);
    1346                 :         24 :                 qpair->group->current_unassociated_qpairs--;
    1347         [ -  + ]:         24 :                 pthread_mutex_unlock(&qpair->group->mutex);
    1348                 :            :         }
    1349                 :            : 
    1350         [ +  + ]:       6266 :         if (ctrlr) {
    1351                 :       6242 :                 sgroup = &qpair->group->sgroups[ctrlr->subsys->id];
    1352                 :       6242 :                 _nvmf_qpair_sgroup_req_clean(sgroup, qpair);
    1353                 :            :         } else {
    1354         [ +  + ]:      24600 :                 for (sid = 0; sid < qpair->group->num_sgroups; sid++) {
    1355                 :      24576 :                         sgroup = &qpair->group->sgroups[sid];
    1356         [ -  + ]:      24576 :                         assert(sgroup != NULL);
    1357                 :      24576 :                         _nvmf_qpair_sgroup_req_clean(sgroup, qpair);
    1358                 :            :                 }
    1359                 :            :         }
    1360                 :            : 
    1361                 :       6266 :         nvmf_qpair_auth_destroy(qpair);
    1362                 :       6266 :         qpair_ctx->ctrlr = ctrlr;
    1363                 :       6266 :         spdk_nvmf_poll_group_remove(qpair);
    1364                 :       6266 :         nvmf_transport_qpair_fini(qpair, _nvmf_transport_qpair_fini_complete, qpair_ctx);
    1365                 :       6266 : }
    1366                 :            : 
    1367                 :            : static void
    1368                 :       1432 : _nvmf_qpair_disconnect_msg(void *ctx)
    1369                 :            : {
    1370                 :       1432 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
    1371                 :            : 
    1372                 :       1432 :         spdk_nvmf_qpair_disconnect(qpair_ctx->qpair);
    1373                 :       1432 :         free(ctx);
    1374                 :       1432 : }
    1375                 :            : 
    1376                 :            : int
    1377                 :     869923 : spdk_nvmf_qpair_disconnect(struct spdk_nvmf_qpair *qpair)
    1378                 :            : {
    1379                 :     869923 :         struct spdk_nvmf_poll_group *group = qpair->group;
    1380                 :            :         struct nvmf_qpair_disconnect_ctx *qpair_ctx;
    1381                 :            : 
    1382         [ +  + ]:     869923 :         if (__atomic_test_and_set(&qpair->disconnect_started, __ATOMIC_RELAXED)) {
    1383                 :     862225 :                 return -EINPROGRESS;
    1384                 :            :         }
    1385                 :            : 
    1386                 :            :         /* If we get a qpair in the uninitialized state, we can just destroy it immediately */
    1387         [ -  + ]:       7698 :         if (qpair->state == SPDK_NVMF_QPAIR_UNINITIALIZED) {
    1388                 :          0 :                 nvmf_transport_qpair_fini(qpair, NULL, NULL);
    1389                 :          0 :                 return 0;
    1390                 :            :         }
    1391                 :            : 
    1392         [ -  + ]:       7698 :         assert(group != NULL);
    1393         [ +  + ]:       7698 :         if (spdk_get_thread() != group->thread) {
    1394                 :            :                 /* clear the atomic so we can set it on the next call on the proper thread. */
    1395                 :       1432 :                 __atomic_clear(&qpair->disconnect_started, __ATOMIC_RELAXED);
    1396                 :       1432 :                 qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx));
    1397         [ -  + ]:       1432 :                 if (!qpair_ctx) {
    1398                 :          0 :                         SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n");
    1399                 :          0 :                         return -ENOMEM;
    1400                 :            :                 }
    1401                 :       1432 :                 qpair_ctx->qpair = qpair;
    1402                 :       1432 :                 spdk_thread_send_msg(group->thread, _nvmf_qpair_disconnect_msg, qpair_ctx);
    1403                 :       1432 :                 return 0;
    1404                 :            :         }
    1405                 :            : 
    1406                 :       2335 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_qpair_disconnect, qpair, spdk_thread_get_id(group->thread));
    1407         [ -  + ]:       6266 :         assert(spdk_nvmf_qpair_is_active(qpair));
    1408                 :       6266 :         nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_DEACTIVATING);
    1409                 :            : 
    1410                 :       6266 :         qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx));
    1411         [ -  + ]:       6266 :         if (!qpair_ctx) {
    1412                 :          0 :                 SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n");
    1413                 :          0 :                 return -ENOMEM;
    1414                 :            :         }
    1415                 :            : 
    1416                 :       6266 :         qpair_ctx->qpair = qpair;
    1417                 :            : 
    1418                 :            :         /* Check for outstanding I/O */
    1419         [ +  + ]:       6266 :         if (!TAILQ_EMPTY(&qpair->outstanding)) {
    1420                 :        747 :                 SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_drain_qpair, qpair, spdk_thread_get_id(group->thread));
    1421                 :       1643 :                 qpair->state_cb = _nvmf_qpair_destroy;
    1422                 :       1643 :                 qpair->state_cb_arg = qpair_ctx;
    1423                 :       1643 :                 nvmf_qpair_abort_pending_zcopy_reqs(qpair);
    1424                 :       1643 :                 nvmf_qpair_free_aer(qpair);
    1425                 :       1643 :                 return 0;
    1426                 :            :         }
    1427                 :            : 
    1428                 :       4623 :         _nvmf_qpair_destroy(qpair_ctx, 0);
    1429                 :            : 
    1430                 :       4623 :         return 0;
    1431                 :            : }
    1432                 :            : 
    1433                 :            : int
    1434                 :        296 : spdk_nvmf_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
    1435                 :            :                               struct spdk_nvme_transport_id *trid)
    1436                 :            : {
    1437         [ -  + ]:        296 :         memset(trid, 0, sizeof(*trid));
    1438                 :        296 :         return nvmf_transport_qpair_get_peer_trid(qpair, trid);
    1439                 :            : }
    1440                 :            : 
    1441                 :            : int
    1442                 :          0 : spdk_nvmf_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
    1443                 :            :                                struct spdk_nvme_transport_id *trid)
    1444                 :            : {
    1445         [ #  # ]:          0 :         memset(trid, 0, sizeof(*trid));
    1446                 :          0 :         return nvmf_transport_qpair_get_local_trid(qpair, trid);
    1447                 :            : }
    1448                 :            : 
    1449                 :            : int
    1450                 :       8511 : spdk_nvmf_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
    1451                 :            :                                 struct spdk_nvme_transport_id *trid)
    1452                 :            : {
    1453         [ -  + ]:       8511 :         memset(trid, 0, sizeof(*trid));
    1454                 :       8511 :         return nvmf_transport_qpair_get_listen_trid(qpair, trid);
    1455                 :            : }
    1456                 :            : 
    1457                 :            : static int
    1458                 :      23209 : poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
    1459                 :            :                             struct spdk_nvmf_subsystem *subsystem)
    1460                 :            : {
    1461                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1462                 :            :         uint32_t new_num_ns, old_num_ns;
    1463                 :            :         uint32_t i, j;
    1464                 :            :         struct spdk_nvmf_ns *ns;
    1465                 :            :         struct spdk_nvmf_registrant *reg, *tmp;
    1466                 :            :         struct spdk_io_channel *ch;
    1467                 :            :         struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
    1468                 :            :         struct spdk_nvmf_ctrlr *ctrlr;
    1469                 :            :         bool ns_changed;
    1470                 :            : 
    1471                 :            :         /* Make sure our poll group has memory for this subsystem allocated */
    1472         [ -  + ]:      23209 :         if (subsystem->id >= group->num_sgroups) {
    1473                 :          0 :                 return -ENOMEM;
    1474                 :            :         }
    1475                 :            : 
    1476                 :      23209 :         sgroup = &group->sgroups[subsystem->id];
    1477                 :            : 
    1478                 :            :         /* Make sure the array of namespace information is the correct size */
    1479                 :      23209 :         new_num_ns = subsystem->max_nsid;
    1480                 :      23209 :         old_num_ns = sgroup->num_ns;
    1481                 :            : 
    1482                 :      23209 :         ns_changed = false;
    1483                 :            : 
    1484         [ +  + ]:      23209 :         if (old_num_ns == 0) {
    1485         [ +  + ]:       3703 :                 if (new_num_ns > 0) {
    1486                 :            :                         /* First allocation */
    1487                 :       1031 :                         sgroup->ns_info = calloc(new_num_ns, sizeof(struct spdk_nvmf_subsystem_pg_ns_info));
    1488         [ -  + ]:       1031 :                         if (!sgroup->ns_info) {
    1489                 :          0 :                                 return -ENOMEM;
    1490                 :            :                         }
    1491                 :            :                 }
    1492         [ -  + ]:      19506 :         } else if (new_num_ns > old_num_ns) {
    1493                 :            :                 void *buf;
    1494                 :            : 
    1495                 :            :                 /* Make the array larger */
    1496                 :          0 :                 buf = realloc(sgroup->ns_info, new_num_ns * sizeof(struct spdk_nvmf_subsystem_pg_ns_info));
    1497         [ #  # ]:          0 :                 if (!buf) {
    1498                 :          0 :                         return -ENOMEM;
    1499                 :            :                 }
    1500                 :            : 
    1501                 :          0 :                 sgroup->ns_info = buf;
    1502                 :            : 
    1503                 :            :                 /* Null out the new namespace information slots */
    1504         [ #  # ]:          0 :                 for (i = old_num_ns; i < new_num_ns; i++) {
    1505         [ #  # ]:          0 :                         memset(&sgroup->ns_info[i], 0, sizeof(struct spdk_nvmf_subsystem_pg_ns_info));
    1506                 :            :                 }
    1507         [ -  + ]:      19506 :         } else if (new_num_ns < old_num_ns) {
    1508                 :            :                 void *buf;
    1509                 :            : 
    1510                 :            :                 /* Free the extra I/O channels */
    1511         [ #  # ]:          0 :                 for (i = new_num_ns; i < old_num_ns; i++) {
    1512                 :          0 :                         ns_info = &sgroup->ns_info[i];
    1513                 :            : 
    1514         [ #  # ]:          0 :                         if (ns_info->channel) {
    1515                 :          0 :                                 spdk_put_io_channel(ns_info->channel);
    1516                 :          0 :                                 ns_info->channel = NULL;
    1517                 :            :                         }
    1518                 :            :                 }
    1519                 :            : 
    1520                 :            :                 /* Make the array smaller */
    1521         [ #  # ]:          0 :                 if (new_num_ns > 0) {
    1522                 :          0 :                         buf = realloc(sgroup->ns_info, new_num_ns * sizeof(struct spdk_nvmf_subsystem_pg_ns_info));
    1523         [ #  # ]:          0 :                         if (!buf) {
    1524                 :          0 :                                 return -ENOMEM;
    1525                 :            :                         }
    1526                 :          0 :                         sgroup->ns_info = buf;
    1527                 :            :                 } else {
    1528                 :          0 :                         free(sgroup->ns_info);
    1529                 :          0 :                         sgroup->ns_info = NULL;
    1530                 :            :                 }
    1531                 :            :         }
    1532                 :            : 
    1533                 :      23209 :         sgroup->num_ns = new_num_ns;
    1534                 :            : 
    1535                 :            :         /* Detect bdevs that were added or removed */
    1536         [ +  + ]:     327399 :         for (i = 0; i < sgroup->num_ns; i++) {
    1537                 :     304190 :                 ns = subsystem->ns[i];
    1538                 :     304190 :                 ns_info = &sgroup->ns_info[i];
    1539                 :     304190 :                 ch = ns_info->channel;
    1540                 :            : 
    1541   [ +  +  +  + ]:     304190 :                 if (ns == NULL && ch == NULL) {
    1542                 :            :                         /* Both NULL. Leave empty */
    1543   [ +  +  +  - ]:      25282 :                 } else if (ns == NULL && ch != NULL) {
    1544                 :            :                         /* There was a channel here, but the namespace is gone. */
    1545                 :       7034 :                         ns_changed = true;
    1546                 :       7034 :                         spdk_put_io_channel(ch);
    1547                 :       7034 :                         ns_info->channel = NULL;
    1548   [ +  -  +  + ]:      18248 :                 } else if (ns != NULL && ch == NULL) {
    1549                 :            :                         /* A namespace appeared but there is no channel yet */
    1550                 :       7894 :                         ns_changed = true;
    1551                 :       7894 :                         ch = spdk_bdev_get_io_channel(ns->desc);
    1552         [ -  + ]:       7894 :                         if (ch == NULL) {
    1553                 :          0 :                                 SPDK_ERRLOG("Could not allocate I/O channel.\n");
    1554                 :          0 :                                 return -ENOMEM;
    1555                 :            :                         }
    1556                 :       7894 :                         ns_info->channel = ch;
    1557         [ -  + ]:      10354 :                 } else if (spdk_uuid_compare(&ns_info->uuid, spdk_bdev_get_uuid(ns->bdev)) != 0) {
    1558                 :            :                         /* A namespace was here before, but was replaced by a new one. */
    1559                 :          0 :                         ns_changed = true;
    1560                 :          0 :                         spdk_put_io_channel(ns_info->channel);
    1561         [ #  # ]:          0 :                         memset(ns_info, 0, sizeof(*ns_info));
    1562                 :            : 
    1563                 :          0 :                         ch = spdk_bdev_get_io_channel(ns->desc);
    1564         [ #  # ]:          0 :                         if (ch == NULL) {
    1565                 :          0 :                                 SPDK_ERRLOG("Could not allocate I/O channel.\n");
    1566                 :          0 :                                 return -ENOMEM;
    1567                 :            :                         }
    1568                 :          0 :                         ns_info->channel = ch;
    1569         [ +  + ]:      10354 :                 } else if (ns_info->num_blocks != spdk_bdev_get_num_blocks(ns->bdev)) {
    1570                 :            :                         /* Namespace is still there but size has changed */
    1571   [ -  +  -  + ]:        276 :                         SPDK_DEBUGLOG(nvmf, "Namespace resized: subsystem_id %u,"
    1572                 :            :                                       " nsid %u, pg %p, old %" PRIu64 ", new %" PRIu64 "\n",
    1573                 :            :                                       subsystem->id,
    1574                 :            :                                       ns->nsid,
    1575                 :            :                                       group,
    1576                 :            :                                       ns_info->num_blocks,
    1577                 :            :                                       spdk_bdev_get_num_blocks(ns->bdev));
    1578                 :        276 :                         ns_changed = true;
    1579                 :            :                 }
    1580                 :            : 
    1581         [ +  + ]:     304190 :                 if (ns == NULL) {
    1582         [ -  + ]:     285942 :                         memset(ns_info, 0, sizeof(*ns_info));
    1583                 :            :                 } else {
    1584                 :      18248 :                         ns_info->uuid = *spdk_bdev_get_uuid(ns->bdev);
    1585                 :      18248 :                         ns_info->num_blocks = spdk_bdev_get_num_blocks(ns->bdev);
    1586                 :      18248 :                         ns_info->crkey = ns->crkey;
    1587                 :      18248 :                         ns_info->rtype = ns->rtype;
    1588         [ -  + ]:      18248 :                         if (ns->holder) {
    1589                 :          0 :                                 ns_info->holder_id = ns->holder->hostid;
    1590                 :            :                         }
    1591                 :            : 
    1592         [ -  + ]:      18248 :                         memset(&ns_info->reg_hostid, 0, SPDK_NVMF_MAX_NUM_REGISTRANTS * sizeof(struct spdk_uuid));
    1593                 :      18248 :                         j = 0;
    1594         [ -  + ]:      18248 :                         TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
    1595         [ #  # ]:          0 :                                 if (j >= SPDK_NVMF_MAX_NUM_REGISTRANTS) {
    1596                 :          0 :                                         SPDK_ERRLOG("Maximum %u registrants can support.\n", SPDK_NVMF_MAX_NUM_REGISTRANTS);
    1597                 :          0 :                                         return -EINVAL;
    1598                 :            :                                 }
    1599                 :          0 :                                 ns_info->reg_hostid[j++] = reg->hostid;
    1600                 :            :                         }
    1601                 :            :                 }
    1602                 :            :         }
    1603                 :            : 
    1604         [ +  + ]:      23209 :         if (ns_changed) {
    1605         [ +  + ]:      26203 :                 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
    1606         [ +  + ]:      11011 :                         if (ctrlr->thread != spdk_get_thread()) {
    1607                 :       7337 :                                 continue;
    1608                 :            :                         }
    1609                 :            :                         /* It is possible that a ctrlr was added but the admin_qpair hasn't been
    1610                 :            :                          * assigned yet.
    1611                 :            :                          */
    1612         [ +  + ]:       3674 :                         if (!ctrlr->admin_qpair) {
    1613                 :         11 :                                 continue;
    1614                 :            :                         }
    1615         [ +  - ]:       3663 :                         if (ctrlr->admin_qpair->group == group) {
    1616                 :       3663 :                                 nvmf_ctrlr_async_event_ns_notice(ctrlr);
    1617                 :       3663 :                                 nvmf_ctrlr_async_event_ana_change_notice(ctrlr);
    1618                 :            :                         }
    1619                 :            :                 }
    1620                 :            :         }
    1621                 :            : 
    1622                 :      23209 :         return 0;
    1623                 :            : }
    1624                 :            : 
    1625                 :            : int
    1626                 :          0 : nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
    1627                 :            :                                  struct spdk_nvmf_subsystem *subsystem)
    1628                 :            : {
    1629                 :          0 :         return poll_group_update_subsystem(group, subsystem);
    1630                 :            : }
    1631                 :            : 
    1632                 :            : int
    1633                 :       3567 : nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group,
    1634                 :            :                               struct spdk_nvmf_subsystem *subsystem,
    1635                 :            :                               spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1636                 :            : {
    1637                 :       3567 :         int rc = 0;
    1638                 :       3567 :         struct spdk_nvmf_subsystem_poll_group *sgroup = &group->sgroups[subsystem->id];
    1639                 :            :         struct spdk_nvmf_request *req, *tmp;
    1640                 :            :         uint32_t i;
    1641                 :            : 
    1642         [ -  + ]:       3567 :         if (!TAILQ_EMPTY(&sgroup->queued)) {
    1643                 :          0 :                 SPDK_ERRLOG("sgroup->queued not empty when adding subsystem\n");
    1644         [ #  # ]:          0 :                 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
    1645         [ #  # ]:          0 :                         TAILQ_REMOVE(&sgroup->queued, req, link);
    1646         [ #  # ]:          0 :                         if (nvmf_transport_req_free(req)) {
    1647                 :          0 :                                 SPDK_ERRLOG("Transport request free error!\n");
    1648                 :            :                         }
    1649                 :            :                 }
    1650                 :          0 :                 assert(false);
    1651                 :            :         }
    1652                 :            : 
    1653                 :       3567 :         rc = poll_group_update_subsystem(group, subsystem);
    1654         [ -  + ]:       3567 :         if (rc) {
    1655                 :          0 :                 nvmf_poll_group_remove_subsystem(group, subsystem, NULL, NULL);
    1656                 :          0 :                 goto fini;
    1657                 :            :         }
    1658                 :            : 
    1659                 :       3567 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1660                 :            : 
    1661         [ +  + ]:      33685 :         for (i = 0; i < sgroup->num_ns; i++) {
    1662                 :      30118 :                 sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1663                 :            :         }
    1664                 :            : 
    1665                 :       3567 : fini:
    1666         [ +  + ]:       3567 :         if (cb_fn) {
    1667                 :       2295 :                 cb_fn(cb_arg, rc);
    1668                 :            :         }
    1669                 :            : 
    1670                 :        883 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_add_subsystem, spdk_thread_get_id(group->thread),
    1671                 :            :                                  subsystem->subnqn);
    1672                 :            : 
    1673                 :       3567 :         return rc;
    1674                 :            : }
    1675                 :            : 
    1676                 :            : static void
    1677                 :       2295 : _nvmf_poll_group_remove_subsystem_cb(void *ctx, int status)
    1678                 :            : {
    1679                 :       2295 :         struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
    1680                 :            :         struct spdk_nvmf_subsystem *subsystem;
    1681                 :            :         struct spdk_nvmf_poll_group *group;
    1682                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1683                 :       2295 :         spdk_nvmf_poll_group_mod_done cpl_fn = NULL;
    1684                 :       2295 :         void *cpl_ctx = NULL;
    1685                 :            :         uint32_t nsid;
    1686                 :            : 
    1687                 :       2295 :         group = qpair_ctx->group;
    1688                 :       2295 :         subsystem = qpair_ctx->subsystem;
    1689                 :       2295 :         cpl_fn = qpair_ctx->cpl_fn;
    1690                 :       2295 :         cpl_ctx = qpair_ctx->cpl_ctx;
    1691                 :       2295 :         sgroup = &group->sgroups[subsystem->id];
    1692                 :            : 
    1693         [ -  + ]:       2295 :         if (status) {
    1694                 :          0 :                 goto fini;
    1695                 :            :         }
    1696                 :            : 
    1697         [ +  + ]:      32409 :         for (nsid = 0; nsid < sgroup->num_ns; nsid++) {
    1698         [ +  + ]:      30114 :                 if (sgroup->ns_info[nsid].channel) {
    1699                 :        856 :                         spdk_put_io_channel(sgroup->ns_info[nsid].channel);
    1700                 :        856 :                         sgroup->ns_info[nsid].channel = NULL;
    1701                 :            :                 }
    1702                 :            :         }
    1703                 :            : 
    1704                 :       2295 :         sgroup->num_ns = 0;
    1705                 :       2295 :         free(sgroup->ns_info);
    1706                 :       2295 :         sgroup->ns_info = NULL;
    1707                 :       2295 : fini:
    1708                 :       2295 :         free(qpair_ctx);
    1709         [ +  - ]:       2295 :         if (cpl_fn) {
    1710                 :       2295 :                 cpl_fn(cpl_ctx, status);
    1711                 :            :         }
    1712                 :       2295 : }
    1713                 :            : 
    1714                 :            : static void nvmf_poll_group_remove_subsystem_msg(void *ctx);
    1715                 :            : 
    1716                 :            : static void
    1717                 :     218138 : nvmf_poll_group_remove_subsystem_msg(void *ctx)
    1718                 :            : {
    1719                 :            :         struct spdk_nvmf_qpair *qpair, *qpair_tmp;
    1720                 :            :         struct spdk_nvmf_subsystem *subsystem;
    1721                 :            :         struct spdk_nvmf_poll_group *group;
    1722                 :     218138 :         struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
    1723                 :     218138 :         bool qpairs_found = false;
    1724                 :     218138 :         int rc = 0;
    1725                 :            : 
    1726                 :     218138 :         group = qpair_ctx->group;
    1727                 :     218138 :         subsystem = qpair_ctx->subsystem;
    1728                 :            : 
    1729         [ +  + ]:    1081001 :         TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, qpair_tmp) {
    1730   [ +  -  +  + ]:     862863 :                 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) {
    1731                 :     862307 :                         qpairs_found = true;
    1732                 :     862307 :                         rc = spdk_nvmf_qpair_disconnect(qpair);
    1733   [ +  +  -  + ]:     862307 :                         if (rc && rc != -EINPROGRESS) {
    1734                 :          0 :                                 break;
    1735                 :            :                         }
    1736                 :            :                 }
    1737                 :            :         }
    1738                 :            : 
    1739         [ +  + ]:     218138 :         if (!qpairs_found) {
    1740                 :       2295 :                 _nvmf_poll_group_remove_subsystem_cb(ctx, 0);
    1741                 :       2295 :                 return;
    1742                 :            :         }
    1743                 :            : 
    1744                 :            :         /* Some qpairs are in process of being disconnected. Send a message and try to remove them again */
    1745                 :     215843 :         spdk_thread_send_msg(spdk_get_thread(), nvmf_poll_group_remove_subsystem_msg, ctx);
    1746                 :            : }
    1747                 :            : 
    1748                 :            : void
    1749                 :       2295 : nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group,
    1750                 :            :                                  struct spdk_nvmf_subsystem *subsystem,
    1751                 :            :                                  spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1752                 :            : {
    1753                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1754                 :            :         struct nvmf_qpair_disconnect_many_ctx *ctx;
    1755                 :            :         uint32_t i;
    1756                 :            : 
    1757                 :        593 :         SPDK_DTRACE_PROBE3_TICKS(nvmf_poll_group_remove_subsystem, group, spdk_thread_get_id(group->thread),
    1758                 :            :                                  subsystem->subnqn);
    1759                 :            : 
    1760                 :       2295 :         ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx));
    1761         [ -  + ]:       2295 :         if (!ctx) {
    1762                 :          0 :                 SPDK_ERRLOG("Unable to allocate memory for context to remove poll subsystem\n");
    1763         [ #  # ]:          0 :                 if (cb_fn) {
    1764                 :          0 :                         cb_fn(cb_arg, -1);
    1765                 :            :                 }
    1766                 :          0 :                 return;
    1767                 :            :         }
    1768                 :            : 
    1769                 :       2295 :         ctx->group = group;
    1770                 :       2295 :         ctx->subsystem = subsystem;
    1771                 :       2295 :         ctx->cpl_fn = cb_fn;
    1772                 :       2295 :         ctx->cpl_ctx = cb_arg;
    1773                 :            : 
    1774                 :       2295 :         sgroup = &group->sgroups[subsystem->id];
    1775                 :       2295 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
    1776                 :            : 
    1777         [ +  + ]:      32409 :         for (i = 0; i < sgroup->num_ns; i++) {
    1778                 :      30114 :                 sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
    1779                 :            :         }
    1780                 :            : 
    1781                 :       2295 :         nvmf_poll_group_remove_subsystem_msg(ctx);
    1782                 :            : }
    1783                 :            : 
    1784                 :            : void
    1785                 :      19642 : nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group,
    1786                 :            :                                 struct spdk_nvmf_subsystem *subsystem,
    1787                 :            :                                 uint32_t nsid,
    1788                 :            :                                 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1789                 :            : {
    1790                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1791                 :      19642 :         struct spdk_nvmf_subsystem_pg_ns_info *ns_info = NULL;
    1792                 :      19642 :         int rc = 0;
    1793                 :            :         uint32_t i;
    1794                 :            : 
    1795         [ -  + ]:      19642 :         if (subsystem->id >= group->num_sgroups) {
    1796                 :          0 :                 rc = -1;
    1797                 :          0 :                 goto fini;
    1798                 :            :         }
    1799                 :            : 
    1800                 :      19642 :         sgroup = &group->sgroups[subsystem->id];
    1801         [ -  + ]:      19642 :         if (sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSED) {
    1802                 :          0 :                 goto fini;
    1803                 :            :         }
    1804                 :      19642 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
    1805                 :            : 
    1806         [ -  + ]:      19642 :         if (nsid == SPDK_NVME_GLOBAL_NS_TAG) {
    1807         [ #  # ]:          0 :                 for (i = 0; i < sgroup->num_ns; i++) {
    1808                 :          0 :                         ns_info = &sgroup->ns_info[i];
    1809                 :          0 :                         ns_info->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
    1810                 :            :                 }
    1811                 :            :         } else {
    1812                 :            :                 /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */
    1813         [ +  + ]:      19642 :                 if (nsid - 1 < sgroup->num_ns) {
    1814                 :       9379 :                         ns_info  = &sgroup->ns_info[nsid - 1];
    1815                 :       9379 :                         ns_info->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
    1816                 :            :                 }
    1817                 :            :         }
    1818                 :            : 
    1819         [ +  + ]:      19642 :         if (sgroup->mgmt_io_outstanding > 0) {
    1820         [ -  + ]:          3 :                 assert(sgroup->cb_fn == NULL);
    1821                 :          3 :                 sgroup->cb_fn = cb_fn;
    1822         [ -  + ]:          3 :                 assert(sgroup->cb_arg == NULL);
    1823                 :          3 :                 sgroup->cb_arg = cb_arg;
    1824                 :          3 :                 return;
    1825                 :            :         }
    1826                 :            : 
    1827         [ -  + ]:      19639 :         if (nsid == SPDK_NVME_GLOBAL_NS_TAG) {
    1828         [ #  # ]:          0 :                 for (i = 0; i < sgroup->num_ns; i++) {
    1829                 :          0 :                         ns_info = &sgroup->ns_info[i];
    1830                 :            : 
    1831         [ #  # ]:          0 :                         if (ns_info->io_outstanding > 0) {
    1832         [ #  # ]:          0 :                                 assert(sgroup->cb_fn == NULL);
    1833                 :          0 :                                 sgroup->cb_fn = cb_fn;
    1834         [ #  # ]:          0 :                                 assert(sgroup->cb_arg == NULL);
    1835                 :          0 :                                 sgroup->cb_arg = cb_arg;
    1836                 :          0 :                                 return;
    1837                 :            :                         }
    1838                 :            :                 }
    1839                 :            :         } else {
    1840   [ +  +  +  + ]:      19639 :                 if (ns_info != NULL && ns_info->io_outstanding > 0) {
    1841         [ -  + ]:       1228 :                         assert(sgroup->cb_fn == NULL);
    1842                 :       1228 :                         sgroup->cb_fn = cb_fn;
    1843         [ -  + ]:       1228 :                         assert(sgroup->cb_arg == NULL);
    1844                 :       1228 :                         sgroup->cb_arg = cb_arg;
    1845                 :       1228 :                         return;
    1846                 :            :                 }
    1847                 :            :         }
    1848                 :            : 
    1849         [ -  + ]:      18411 :         assert(sgroup->mgmt_io_outstanding == 0);
    1850                 :      18411 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED;
    1851                 :      18411 : fini:
    1852         [ +  - ]:      18411 :         if (cb_fn) {
    1853                 :      18411 :                 cb_fn(cb_arg, rc);
    1854                 :            :         }
    1855                 :            : }
    1856                 :            : 
    1857                 :            : void
    1858                 :      19642 : nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group,
    1859                 :            :                                  struct spdk_nvmf_subsystem *subsystem,
    1860                 :            :                                  spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1861                 :            : {
    1862                 :            :         struct spdk_nvmf_request *req, *tmp;
    1863                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1864                 :      19642 :         int rc = 0;
    1865                 :            :         uint32_t i;
    1866                 :            : 
    1867         [ -  + ]:      19642 :         if (subsystem->id >= group->num_sgroups) {
    1868                 :          0 :                 rc = -1;
    1869                 :          0 :                 goto fini;
    1870                 :            :         }
    1871                 :            : 
    1872                 :      19642 :         sgroup = &group->sgroups[subsystem->id];
    1873                 :            : 
    1874         [ -  + ]:      19642 :         if (sgroup->state == SPDK_NVMF_SUBSYSTEM_ACTIVE) {
    1875                 :          0 :                 goto fini;
    1876                 :            :         }
    1877                 :            : 
    1878                 :      19642 :         rc = poll_group_update_subsystem(group, subsystem);
    1879         [ -  + ]:      19642 :         if (rc) {
    1880                 :          0 :                 goto fini;
    1881                 :            :         }
    1882                 :            : 
    1883         [ +  + ]:     293714 :         for (i = 0; i < sgroup->num_ns; i++) {
    1884                 :     274072 :                 sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1885                 :            :         }
    1886                 :            : 
    1887                 :      19642 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1888                 :            : 
    1889                 :            :         /* Release all queued requests */
    1890         [ +  + ]:     130325 :         TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
    1891         [ +  + ]:     110683 :                 TAILQ_REMOVE(&sgroup->queued, req, link);
    1892         [ +  + ]:     110683 :                 if (spdk_nvmf_request_using_zcopy(req)) {
    1893                 :     104957 :                         spdk_nvmf_request_zcopy_start(req);
    1894                 :            :                 } else {
    1895                 :       5726 :                         spdk_nvmf_request_exec(req);
    1896                 :            :                 }
    1897                 :            : 
    1898                 :            :         }
    1899                 :      19642 : fini:
    1900         [ +  - ]:      19642 :         if (cb_fn) {
    1901                 :      19642 :                 cb_fn(cb_arg, rc);
    1902                 :            :         }
    1903                 :      19642 : }
    1904                 :            : 
    1905                 :            : 
    1906                 :            : struct spdk_nvmf_poll_group *
    1907                 :       6266 : spdk_nvmf_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair)
    1908                 :            : {
    1909                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1910                 :            : 
    1911                 :       6266 :         tgroup = nvmf_transport_get_optimal_poll_group(qpair->transport, qpair);
    1912                 :            : 
    1913         [ -  + ]:       6266 :         if (tgroup == NULL) {
    1914                 :          0 :                 return NULL;
    1915                 :            :         }
    1916                 :            : 
    1917                 :       6266 :         return tgroup->group;
    1918                 :            : }
    1919                 :            : 
    1920                 :            : void
    1921                 :         76 : spdk_nvmf_poll_group_dump_stat(struct spdk_nvmf_poll_group *group, struct spdk_json_write_ctx *w)
    1922                 :            : {
    1923                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1924                 :            : 
    1925                 :         76 :         spdk_json_write_object_begin(w);
    1926                 :            : 
    1927                 :         76 :         spdk_json_write_named_string(w, "name", spdk_thread_get_name(spdk_get_thread()));
    1928                 :         76 :         spdk_json_write_named_uint32(w, "admin_qpairs", group->stat.admin_qpairs);
    1929                 :         76 :         spdk_json_write_named_uint32(w, "io_qpairs", group->stat.io_qpairs);
    1930                 :         76 :         spdk_json_write_named_uint32(w, "current_admin_qpairs", group->stat.current_admin_qpairs);
    1931                 :         76 :         spdk_json_write_named_uint32(w, "current_io_qpairs", group->stat.current_io_qpairs);
    1932                 :         76 :         spdk_json_write_named_uint64(w, "pending_bdev_io", group->stat.pending_bdev_io);
    1933                 :         76 :         spdk_json_write_named_uint64(w, "completed_nvme_io", group->stat.completed_nvme_io);
    1934                 :            : 
    1935                 :         76 :         spdk_json_write_named_array_begin(w, "transports");
    1936                 :            : 
    1937         [ +  + ]:        140 :         TAILQ_FOREACH(tgroup, &group->tgroups, link) {
    1938                 :         64 :                 spdk_json_write_object_begin(w);
    1939                 :            :                 /*
    1940                 :            :                  * The trtype field intentionally contains a transport name as this is more informative.
    1941                 :            :                  * The field has not been renamed for backward compatibility.
    1942                 :            :                  */
    1943                 :         64 :                 spdk_json_write_named_string(w, "trtype", spdk_nvmf_get_transport_name(tgroup->transport));
    1944                 :            : 
    1945         [ +  + ]:         64 :                 if (tgroup->transport->ops->poll_group_dump_stat) {
    1946                 :         40 :                         tgroup->transport->ops->poll_group_dump_stat(tgroup, w);
    1947                 :            :                 }
    1948                 :            : 
    1949                 :         64 :                 spdk_json_write_object_end(w);
    1950                 :            :         }
    1951                 :            : 
    1952                 :         76 :         spdk_json_write_array_end(w);
    1953                 :         76 :         spdk_json_write_object_end(w);
    1954                 :         76 : }

Generated by: LCOV version 1.14