LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - nvmf.c (source / functions) Hit Total Coverage
Test: Combined Lines: 711 953 74.6 %
Date: 2024-07-16 01:00:24 Functions: 55 66 83.3 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 322 550 58.5 %

           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                 :        845 : 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                 :      27602 : nvmf_qpair_set_state(struct spdk_nvmf_qpair *qpair,
     140                 :            :                      enum spdk_nvmf_qpair_state state)
     141                 :            : {
     142         [ -  + ]:      27602 :         assert(qpair != NULL);
     143         [ -  + ]:      27602 :         assert(qpair->group->thread == spdk_get_thread());
     144                 :            : 
     145                 :      27602 :         qpair->state = state;
     146                 :      27602 : }
     147                 :            : 
     148                 :            : static int
     149                 : 2247580054 : nvmf_poll_group_poll(void *ctx)
     150                 :            : {
     151                 : 2247580054 :         struct spdk_nvmf_poll_group *group = ctx;
     152                 :            :         int rc;
     153                 : 2247580054 :         int count = 0;
     154                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
     155                 :            : 
     156         [ +  + ]: 3326769149 :         TAILQ_FOREACH(tgroup, &group->tgroups, link) {
     157                 : 1079189173 :                 rc = nvmf_transport_poll_group_poll(tgroup);
     158         [ +  + ]: 1079189173 :                 if (rc < 0) {
     159                 :          8 :                         return SPDK_POLLER_BUSY;
     160                 :            :                 }
     161                 : 1079189165 :                 count += rc;
     162                 :            :         }
     163                 :            : 
     164                 : 2247580046 :         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                 :       1290 : 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         [ +  + ]:       1866 :         TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) {
     179         [ -  + ]:        576 :                 TAILQ_REMOVE(&group->tgroups, tgroup, link);
     180                 :        576 :                 nvmf_transport_poll_group_destroy(tgroup);
     181                 :            :         }
     182                 :            : 
     183         [ +  + ]:    1307277 :         for (sid = 0; sid < group->num_sgroups; sid++) {
     184                 :    1305987 :                 sgroup = &group->sgroups[sid];
     185                 :            : 
     186         [ -  + ]:    1305987 :                 assert(sgroup != NULL);
     187                 :            : 
     188         [ +  + ]:    1305990 :                 for (nsid = 0; nsid < sgroup->num_ns; nsid++) {
     189         [ +  - ]:          3 :                         if (sgroup->ns_info[nsid].channel) {
     190                 :          3 :                                 spdk_put_io_channel(sgroup->ns_info[nsid].channel);
     191                 :          3 :                                 sgroup->ns_info[nsid].channel = NULL;
     192                 :            :                         }
     193                 :            :                 }
     194                 :            : 
     195                 :    1305987 :                 free(sgroup->ns_info);
     196                 :            :         }
     197                 :            : 
     198                 :       1290 :         free(group->sgroups);
     199                 :            : 
     200                 :       1290 :         spdk_poller_unregister(&group->poller);
     201                 :            : 
     202         [ +  + ]:       1290 :         if (group->destroy_cb_fn) {
     203                 :       1287 :                 group->destroy_cb_fn(group->destroy_cb_arg, 0);
     204                 :            :         }
     205                 :       1290 : }
     206                 :            : 
     207                 :            : /*
     208                 :            :  * Callback to unregister a poll group from the target, and clean up its state.
     209                 :            :  */
     210                 :            : static void
     211                 :       1290 : nvmf_tgt_destroy_poll_group(void *io_device, void *ctx_buf)
     212                 :            : {
     213                 :       1290 :         struct spdk_nvmf_tgt *tgt = io_device;
     214                 :       1290 :         struct spdk_nvmf_poll_group *group = ctx_buf;
     215                 :            : 
     216                 :        288 :         SPDK_DTRACE_PROBE1_TICKS(nvmf_destroy_poll_group, spdk_thread_get_id(group->thread));
     217                 :            : 
     218         [ -  + ]:       1290 :         pthread_mutex_lock(&tgt->mutex);
     219         [ +  + ]:       1290 :         TAILQ_REMOVE(&tgt->poll_groups, group, link);
     220                 :       1290 :         tgt->num_poll_groups--;
     221         [ -  + ]:       1290 :         pthread_mutex_unlock(&tgt->mutex);
     222                 :            : 
     223   [ +  -  +  - ]:       1290 :         assert(!(tgt->state == NVMF_TGT_PAUSING || tgt->state == NVMF_TGT_RESUMING));
     224                 :       1290 :         nvmf_tgt_cleanup_poll_group(group);
     225                 :       1290 : }
     226                 :            : 
     227                 :            : static int
     228                 :        576 : 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         [ -  + ]:        576 :         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                 :        576 :         tgroup = nvmf_transport_poll_group_create(transport, group);
     241         [ -  + ]:        576 :         if (!tgroup) {
     242                 :          0 :                 SPDK_ERRLOG("Unable to create poll group for transport\n");
     243                 :          0 :                 return -1;
     244                 :            :         }
     245                 :        220 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_transport_poll_group_create, transport,
     246                 :            :                                  spdk_thread_get_id(group->thread));
     247                 :            : 
     248                 :        576 :         tgroup->group = group;
     249                 :        576 :         TAILQ_INSERT_TAIL(&group->tgroups, tgroup, link);
     250                 :            : 
     251                 :        576 :         return 0;
     252                 :            : }
     253                 :            : 
     254                 :            : static int
     255                 :       1290 : nvmf_tgt_create_poll_group(void *io_device, void *ctx_buf)
     256                 :            : {
     257                 :       1290 :         struct spdk_nvmf_tgt *tgt = io_device;
     258                 :       1290 :         struct spdk_nvmf_poll_group *group = ctx_buf;
     259                 :            :         struct spdk_nvmf_transport *transport;
     260                 :            :         struct spdk_nvmf_subsystem *subsystem;
     261                 :       1290 :         struct spdk_thread *thread = spdk_get_thread();
     262                 :            :         uint32_t i;
     263                 :            :         int rc;
     264                 :            : 
     265                 :       1290 :         group->tgt = tgt;
     266                 :       1290 :         TAILQ_INIT(&group->tgroups);
     267                 :       1290 :         TAILQ_INIT(&group->qpairs);
     268                 :       1290 :         group->thread = thread;
     269         [ -  + ]:       1290 :         pthread_mutex_init(&group->mutex, NULL);
     270                 :            : 
     271                 :       1290 :         group->poller = SPDK_POLLER_REGISTER(nvmf_poll_group_poll, group, 0);
     272                 :            : 
     273                 :        288 :         SPDK_DTRACE_PROBE1_TICKS(nvmf_create_poll_group, spdk_thread_get_id(thread));
     274                 :            : 
     275         [ +  + ]:       1293 :         TAILQ_FOREACH(transport, &tgt->transports, link) {
     276                 :          3 :                 rc = nvmf_poll_group_add_transport(group, transport);
     277         [ -  + ]:          3 :                 if (rc != 0) {
     278                 :          0 :                         nvmf_tgt_cleanup_poll_group(group);
     279                 :          0 :                         return rc;
     280                 :            :                 }
     281                 :            :         }
     282                 :            : 
     283                 :       1290 :         group->num_sgroups = tgt->max_subsystems;
     284                 :       1290 :         group->sgroups = calloc(tgt->max_subsystems, sizeof(struct spdk_nvmf_subsystem_poll_group));
     285         [ -  + ]:       1290 :         if (!group->sgroups) {
     286                 :          0 :                 nvmf_tgt_cleanup_poll_group(group);
     287                 :          0 :                 return -ENOMEM;
     288                 :            :         }
     289                 :            : 
     290         [ +  + ]:    1307277 :         for (i = 0; i < tgt->max_subsystems; i++) {
     291                 :    1305987 :                 TAILQ_INIT(&group->sgroups[i].queued);
     292                 :            :         }
     293                 :            : 
     294         [ +  + ]:       1336 :         for (subsystem = spdk_nvmf_subsystem_get_first(tgt);
     295         [ +  + ]:       2488 :              subsystem != NULL;
     296                 :       1290 :              subsystem = spdk_nvmf_subsystem_get_next(subsystem)) {
     297         [ -  + ]:       1290 :                 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         [ -  + ]:       1290 :         pthread_mutex_lock(&tgt->mutex);
     304                 :       1290 :         tgt->num_poll_groups++;
     305                 :       1290 :         TAILQ_INSERT_TAIL(&tgt->poll_groups, group, link);
     306         [ -  + ]:       1290 :         pthread_mutex_unlock(&tgt->mutex);
     307                 :            : 
     308                 :       1290 :         return 0;
     309                 :            : }
     310                 :            : 
     311                 :            : static void
     312                 :       1287 : _nvmf_tgt_disconnect_qpairs(void *ctx)
     313                 :            : {
     314                 :            :         struct spdk_nvmf_qpair *qpair, *qpair_tmp;
     315                 :       1287 :         struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
     316                 :       1287 :         struct spdk_nvmf_poll_group *group = qpair_ctx->group;
     317                 :            :         struct spdk_io_channel *ch;
     318                 :            :         int rc;
     319                 :            : 
     320         [ -  + ]:       1287 :         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         [ +  - ]:       1287 :         if (TAILQ_EMPTY(&group->qpairs)) {
     328                 :            :                 /* When the refcount from the channels reaches 0, nvmf_tgt_destroy_poll_group will be called. */
     329                 :       1287 :                 ch = spdk_io_channel_from_ctx(group);
     330                 :       1287 :                 spdk_put_io_channel(ch);
     331                 :       1287 :                 free(qpair_ctx);
     332                 :       1287 :                 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                 :       1287 : nvmf_tgt_destroy_poll_group_qpairs(struct spdk_nvmf_poll_group *group)
     341                 :            : {
     342                 :            :         struct nvmf_qpair_disconnect_many_ctx *ctx;
     343                 :            : 
     344                 :        288 :         SPDK_DTRACE_PROBE1_TICKS(nvmf_destroy_poll_group_qpairs, spdk_thread_get_id(group->thread));
     345                 :            : 
     346                 :       1287 :         ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx));
     347         [ -  + ]:       1287 :         if (!ctx) {
     348                 :          0 :                 SPDK_ERRLOG("Failed to allocate memory for destroy poll group ctx\n");
     349                 :          0 :                 return;
     350                 :            :         }
     351                 :            : 
     352                 :       1287 :         ctx->group = group;
     353                 :       1287 :         _nvmf_tgt_disconnect_qpairs(ctx);
     354                 :            : }
     355                 :            : 
     356                 :            : struct spdk_nvmf_tgt *
     357                 :        774 : spdk_nvmf_tgt_create(struct spdk_nvmf_target_opts *_opts)
     358                 :            : {
     359                 :            :         struct spdk_nvmf_tgt *tgt, *tmp_tgt;
     360                 :        774 :         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         [ -  + ]:        774 :         memcpy(&opts, _opts, _opts->size);
     366         [ -  + ]:        774 :         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         [ +  + ]:        783 :         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                 :        774 :         tgt = calloc(1, sizeof(*tgt));
     379         [ -  + ]:        774 :         if (!tgt) {
     380                 :          0 :                 return NULL;
     381                 :            :         }
     382                 :            : 
     383                 :        774 :         snprintf(tgt->name, NVMF_TGT_NAME_MAX_LENGTH, "%s", opts.name);
     384                 :            : 
     385         [ +  + ]:        774 :         if (!opts.max_subsystems) {
     386                 :        725 :                 tgt->max_subsystems = SPDK_NVMF_DEFAULT_MAX_SUBSYSTEMS;
     387                 :            :         } else {
     388                 :         49 :                 tgt->max_subsystems = opts.max_subsystems;
     389                 :            :         }
     390                 :            : 
     391                 :        774 :         tgt->crdt[0] = opts.crdt[0];
     392                 :        774 :         tgt->crdt[1] = opts.crdt[1];
     393                 :        774 :         tgt->crdt[2] = opts.crdt[2];
     394                 :        774 :         tgt->discovery_filter = opts.discovery_filter;
     395                 :        774 :         tgt->discovery_genctr = 0;
     396                 :        774 :         tgt->dhchap_digests = opts.dhchap_digests;
     397                 :        774 :         tgt->dhchap_dhgroups = opts.dhchap_dhgroups;
     398                 :        774 :         TAILQ_INIT(&tgt->transports);
     399                 :        774 :         TAILQ_INIT(&tgt->poll_groups);
     400                 :        774 :         TAILQ_INIT(&tgt->referrals);
     401                 :        774 :         tgt->num_poll_groups = 0;
     402                 :            : 
     403                 :        774 :         tgt->subsystem_ids = spdk_bit_array_create(tgt->max_subsystems);
     404         [ -  + ]:        774 :         if (tgt->subsystem_ids == NULL) {
     405                 :          0 :                 free(tgt);
     406                 :          0 :                 return NULL;
     407                 :            :         }
     408                 :            : 
     409                 :        774 :         RB_INIT(&tgt->subsystems);
     410                 :            : 
     411         [ -  + ]:        774 :         pthread_mutex_init(&tgt->mutex, NULL);
     412                 :            : 
     413                 :        774 :         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                 :        774 :                                 tgt->name);
     418                 :            : 
     419                 :        774 :         tgt->state = NVMF_TGT_RUNNING;
     420                 :            : 
     421         [ +  + ]:        774 :         TAILQ_INSERT_HEAD(&g_nvmf_tgts, tgt, link);
     422                 :            : 
     423                 :        774 :         return tgt;
     424                 :            : }
     425                 :            : 
     426                 :            : static void
     427                 :       1030 : _nvmf_tgt_destroy_next_transport(void *ctx)
     428                 :            : {
     429                 :       1030 :         struct spdk_nvmf_tgt *tgt = ctx;
     430                 :            :         struct spdk_nvmf_transport *transport;
     431                 :            : 
     432         [ +  + ]:       1030 :         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                 :        774 :                 spdk_nvmf_tgt_destroy_done_fn *destroy_cb_fn = tgt->destroy_cb_fn;
     438                 :        774 :                 void *destroy_cb_arg = tgt->destroy_cb_arg;
     439                 :            : 
     440         [ -  + ]:        774 :                 pthread_mutex_destroy(&tgt->mutex);
     441                 :        774 :                 free(tgt);
     442                 :            : 
     443         [ +  - ]:        774 :                 if (destroy_cb_fn) {
     444                 :        774 :                         destroy_cb_fn(destroy_cb_arg, 0);
     445                 :            :                 }
     446                 :            :         }
     447                 :       1030 : }
     448                 :            : 
     449                 :            : static void
     450                 :        774 : nvmf_tgt_destroy_cb(void *io_device)
     451                 :            : {
     452                 :        774 :         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         [ +  + ]:        777 :         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         [ -  + ]:       1548 :         for (subsystem = spdk_nvmf_subsystem_get_first(tgt),
     466                 :        774 :              subsystem_next = spdk_nvmf_subsystem_get_next(subsystem);
     467         [ +  + ]:        747 :              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                 :        774 :         spdk_bit_array_free(&tgt->subsystem_ids);
     484                 :        774 :         _nvmf_tgt_destroy_next_transport(tgt);
     485                 :            : }
     486                 :            : 
     487                 :            : void
     488                 :        774 : spdk_nvmf_tgt_destroy(struct spdk_nvmf_tgt *tgt,
     489                 :            :                       spdk_nvmf_tgt_destroy_done_fn cb_fn,
     490                 :            :                       void *cb_arg)
     491                 :            : {
     492   [ +  -  +  - ]:        774 :         assert(!(tgt->state == NVMF_TGT_PAUSING || tgt->state == NVMF_TGT_RESUMING));
     493                 :            : 
     494                 :        774 :         tgt->destroy_cb_fn = cb_fn;
     495                 :        774 :         tgt->destroy_cb_arg = cb_arg;
     496                 :            : 
     497         [ +  + ]:        774 :         TAILQ_REMOVE(&g_nvmf_tgts, tgt, link);
     498                 :            : 
     499                 :        774 :         spdk_io_device_unregister(tgt, nvmf_tgt_destroy_cb);
     500                 :        774 : }
     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                 :       9998 : spdk_nvmf_get_tgt(const char *name)
     510                 :            : {
     511                 :            :         struct spdk_nvmf_tgt *tgt;
     512                 :       9998 :         uint32_t num_targets = 0;
     513                 :            : 
     514         [ +  + ]:      19996 :         TAILQ_FOREACH(tgt, &g_nvmf_tgts, link) {
     515         [ +  + ]:      10004 :                 if (name) {
     516   [ -  +  -  +  :         24 :                         if (!strncmp(tgt->name, name, NVMF_TGT_NAME_MAX_LENGTH)) {
                   +  + ]
     517                 :          6 :                                 return tgt;
     518                 :            :                         }
     519                 :            :                 }
     520                 :       9998 :                 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   [ +  +  +  - ]:       9992 :         if (!name && num_targets == 1) {
     530                 :       9980 :                 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                 :         23 : 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                 :          1 :         struct spdk_nvmf_ns_opts ns_opts;
     555                 :            :         uint32_t max_namespaces;
     556                 :            :         struct spdk_nvmf_transport *transport;
     557                 :            : 
     558         [ -  + ]:         23 :         assert(spdk_nvmf_subsystem_get_type(subsystem) == SPDK_NVMF_SUBTYPE_NVME);
     559                 :            : 
     560                 :            :         /* { */
     561                 :         23 :         spdk_json_write_object_begin(w);
     562                 :         23 :         spdk_json_write_named_string(w, "method", "nvmf_create_subsystem");
     563                 :            : 
     564                 :            :         /*     "params" : { */
     565                 :         23 :         spdk_json_write_named_object_begin(w, "params");
     566                 :         23 :         spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     567                 :         23 :         spdk_json_write_named_bool(w, "allow_any_host", spdk_nvmf_subsystem_get_allow_any_host(subsystem));
     568                 :         23 :         spdk_json_write_named_string(w, "serial_number", spdk_nvmf_subsystem_get_sn(subsystem));
     569                 :         23 :         spdk_json_write_named_string(w, "model_number", spdk_nvmf_subsystem_get_mn(subsystem));
     570                 :            : 
     571                 :         23 :         max_namespaces = spdk_nvmf_subsystem_get_max_namespaces(subsystem);
     572         [ +  - ]:         23 :         if (max_namespaces != 0) {
     573                 :         23 :                 spdk_json_write_named_uint32(w, "max_namespaces", max_namespaces);
     574                 :            :         }
     575                 :            : 
     576                 :         23 :         spdk_json_write_named_uint32(w, "min_cntlid", spdk_nvmf_subsystem_get_min_cntlid(subsystem));
     577                 :         23 :         spdk_json_write_named_uint32(w, "max_cntlid", spdk_nvmf_subsystem_get_max_cntlid(subsystem));
     578                 :         23 :         spdk_json_write_named_bool(w, "ana_reporting", spdk_nvmf_subsystem_get_ana_reporting(subsystem));
     579                 :            : 
     580                 :            :         /*     } "params" */
     581                 :         23 :         spdk_json_write_object_end(w);
     582                 :            : 
     583                 :            :         /* } */
     584                 :         23 :         spdk_json_write_object_end(w);
     585                 :            : 
     586         [ +  + ]:         29 :         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         [ +  + ]:         62 :         for (ns = spdk_nvmf_subsystem_get_first_ns(subsystem); ns != NULL;
     619                 :         39 :              ns = spdk_nvmf_subsystem_get_next_ns(subsystem, ns)) {
     620                 :         39 :                 spdk_nvmf_ns_get_opts(ns, &ns_opts, sizeof(ns_opts));
     621                 :            : 
     622                 :         39 :                 spdk_json_write_object_begin(w);
     623                 :         39 :                 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_ns");
     624                 :            : 
     625                 :            :                 /*     "params" : { */
     626                 :         39 :                 spdk_json_write_named_object_begin(w, "params");
     627                 :            : 
     628                 :         39 :                 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     629                 :            : 
     630                 :            :                 /*     "namespace" : { */
     631                 :         39 :                 spdk_json_write_named_object_begin(w, "namespace");
     632                 :            : 
     633                 :         39 :                 spdk_json_write_named_uint32(w, "nsid", spdk_nvmf_ns_get_id(ns));
     634                 :         39 :                 spdk_json_write_named_string(w, "bdev_name", spdk_bdev_get_name(spdk_nvmf_ns_get_bdev(ns)));
     635                 :            : 
     636         [ -  + ]:         39 :                 if (ns->ptpl_file != NULL) {
     637                 :          0 :                         spdk_json_write_named_string(w, "ptpl_file", ns->ptpl_file);
     638                 :            :                 }
     639                 :            : 
     640         [ +  - ]:         39 :                 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                 :         39 :                         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         [ -  + ]:         39 :                 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         [ +  - ]:         39 :                 if (!spdk_uuid_is_null(&ns_opts.uuid)) {
     652                 :         39 :                         spdk_json_write_named_uuid(w, "uuid",  &ns_opts.uuid);
     653                 :            :                 }
     654                 :            : 
     655         [ -  + ]:         39 :                 if (spdk_nvmf_subsystem_get_ana_reporting(subsystem)) {
     656                 :          0 :                         spdk_json_write_named_uint32(w, "anagrpid", ns_opts.anagrpid);
     657                 :            :                 }
     658                 :            : 
     659         [ -  + ]:         39 :                 spdk_json_write_named_bool(w, "no_auto_visible", !ns->always_visible);
     660                 :            : 
     661                 :            :                 /*     "namespace" */
     662                 :         39 :                 spdk_json_write_object_end(w);
     663                 :            : 
     664                 :            :                 /*     } "params" */
     665                 :         39 :                 spdk_json_write_object_end(w);
     666                 :            : 
     667                 :            :                 /* } */
     668                 :         39 :                 spdk_json_write_object_end(w);
     669                 :            : 
     670         [ -  + ]:         39 :                 TAILQ_FOREACH(host, &ns->hosts, link) {
     671                 :          0 :                         spdk_json_write_object_begin(w);
     672                 :          0 :                         spdk_json_write_named_string(w, "method", "nvmf_ns_add_host");
     673                 :          0 :                         spdk_json_write_named_object_begin(w, "params");
     674                 :          0 :                         spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     675                 :          0 :                         spdk_json_write_named_uint32(w, "nsid", spdk_nvmf_ns_get_id(ns));
     676                 :          0 :                         spdk_json_write_named_string(w, "host", spdk_nvmf_host_get_nqn(host));
     677                 :          0 :                         spdk_json_write_object_end(w);
     678                 :          0 :                         spdk_json_write_object_end(w);
     679                 :            :                 }
     680                 :            :         }
     681                 :         23 : }
     682                 :            : 
     683                 :            : static void
     684                 :        128 : nvmf_write_subsystem_config_json(struct spdk_json_write_ctx *w,
     685                 :            :                                  struct spdk_nvmf_subsystem *subsystem)
     686                 :            : {
     687                 :            :         struct spdk_nvmf_subsystem_listener *listener;
     688                 :            :         struct spdk_nvmf_transport *transport;
     689                 :            :         const struct spdk_nvme_transport_id *trid;
     690                 :            : 
     691         [ +  + ]:        128 :         if (spdk_nvmf_subsystem_get_type(subsystem) == SPDK_NVMF_SUBTYPE_NVME) {
     692                 :         23 :                 nvmf_write_nvme_subsystem_config(w, subsystem);
     693                 :            :         }
     694                 :            : 
     695         [ +  + ]:        151 :         for (listener = spdk_nvmf_subsystem_get_first_listener(subsystem); listener != NULL;
     696                 :         23 :              listener = spdk_nvmf_subsystem_get_next_listener(subsystem, listener)) {
     697                 :         23 :                 transport = listener->transport;
     698                 :         23 :                 trid = spdk_nvmf_subsystem_listener_get_trid(listener);
     699                 :            : 
     700                 :         23 :                 spdk_json_write_object_begin(w);
     701                 :         23 :                 spdk_json_write_named_string(w, "method", "nvmf_subsystem_add_listener");
     702                 :            : 
     703                 :            :                 /*     "params" : { */
     704                 :         23 :                 spdk_json_write_named_object_begin(w, "params");
     705                 :            : 
     706                 :         23 :                 spdk_json_write_named_string(w, "nqn", spdk_nvmf_subsystem_get_nqn(subsystem));
     707                 :            : 
     708                 :         23 :                 spdk_json_write_named_object_begin(w, "listen_address");
     709                 :         23 :                 nvmf_transport_listen_dump_trid(trid, w);
     710                 :         23 :                 spdk_json_write_object_end(w);
     711         [ -  + ]:         23 :                 if (transport->ops->listen_dump_opts) {
     712                 :          0 :                         transport->ops->listen_dump_opts(transport, trid, w);
     713                 :            :                 }
     714                 :            : 
     715         [ -  + ]:         23 :                 spdk_json_write_named_bool(w, "secure_channel", listener->opts.secure_channel);
     716                 :            : 
     717         [ +  + ]:         23 :                 if (listener->opts.sock_impl) {
     718                 :          3 :                         spdk_json_write_named_string(w, "sock_impl", listener->opts.sock_impl);
     719                 :            :                 }
     720                 :            : 
     721                 :            :                 /*     } "params" */
     722                 :         23 :                 spdk_json_write_object_end(w);
     723                 :            : 
     724                 :            :                 /* } */
     725                 :         23 :                 spdk_json_write_object_end(w);
     726                 :            :         }
     727                 :        128 : }
     728                 :            : 
     729                 :            : void
     730                 :        105 : spdk_nvmf_tgt_write_config_json(struct spdk_json_write_ctx *w, struct spdk_nvmf_tgt *tgt)
     731                 :            : {
     732                 :            :         struct spdk_nvmf_subsystem *subsystem;
     733                 :            :         struct spdk_nvmf_transport *transport;
     734                 :            :         struct spdk_nvmf_referral *referral;
     735                 :            : 
     736                 :        105 :         spdk_json_write_object_begin(w);
     737                 :        105 :         spdk_json_write_named_string(w, "method", "nvmf_set_max_subsystems");
     738                 :            : 
     739                 :        105 :         spdk_json_write_named_object_begin(w, "params");
     740                 :        105 :         spdk_json_write_named_uint32(w, "max_subsystems", tgt->max_subsystems);
     741                 :        105 :         spdk_json_write_object_end(w);
     742                 :            : 
     743                 :        105 :         spdk_json_write_object_end(w);
     744                 :            : 
     745                 :        105 :         spdk_json_write_object_begin(w);
     746                 :        105 :         spdk_json_write_named_string(w, "method", "nvmf_set_crdt");
     747                 :        105 :         spdk_json_write_named_object_begin(w, "params");
     748                 :        105 :         spdk_json_write_named_uint32(w, "crdt1", tgt->crdt[0]);
     749                 :        105 :         spdk_json_write_named_uint32(w, "crdt2", tgt->crdt[1]);
     750                 :        105 :         spdk_json_write_named_uint32(w, "crdt3", tgt->crdt[2]);
     751                 :        105 :         spdk_json_write_object_end(w);
     752                 :        105 :         spdk_json_write_object_end(w);
     753                 :            : 
     754                 :            :         /* write transports */
     755         [ +  + ]:        151 :         TAILQ_FOREACH(transport, &tgt->transports, link) {
     756                 :         46 :                 spdk_json_write_object_begin(w);
     757                 :         46 :                 spdk_json_write_named_string(w, "method", "nvmf_create_transport");
     758                 :         46 :                 nvmf_transport_dump_opts(transport, w, true);
     759                 :         46 :                 spdk_json_write_object_end(w);
     760                 :            :         }
     761                 :            : 
     762         [ -  + ]:        105 :         TAILQ_FOREACH(referral, &tgt->referrals, link) {
     763                 :          0 :                 spdk_json_write_object_begin(w);
     764                 :          0 :                 spdk_json_write_named_string(w, "method", "nvmf_discovery_add_referral");
     765                 :            : 
     766                 :          0 :                 spdk_json_write_named_object_begin(w, "params");
     767                 :          0 :                 spdk_json_write_named_object_begin(w, "address");
     768                 :          0 :                 nvmf_transport_listen_dump_trid(&referral->trid, w);
     769                 :          0 :                 spdk_json_write_object_end(w);
     770                 :          0 :                 spdk_json_write_named_bool(w, "secure_channel",
     771                 :          0 :                                            referral->entry.treq.secure_channel ==
     772                 :            :                                            SPDK_NVMF_TREQ_SECURE_CHANNEL_REQUIRED);
     773                 :          0 :                 spdk_json_write_named_string(w, "subnqn", referral->trid.subnqn);
     774                 :          0 :                 spdk_json_write_object_end(w);
     775                 :            : 
     776                 :          0 :                 spdk_json_write_object_end(w);
     777                 :            :         }
     778                 :            : 
     779                 :        105 :         subsystem = spdk_nvmf_subsystem_get_first(tgt);
     780         [ +  + ]:        233 :         while (subsystem) {
     781                 :        128 :                 nvmf_write_subsystem_config_json(w, subsystem);
     782                 :        128 :                 subsystem = spdk_nvmf_subsystem_get_next(subsystem);
     783                 :            :         }
     784                 :        105 : }
     785                 :            : 
     786                 :            : static void
     787                 :        965 : nvmf_listen_opts_copy(struct spdk_nvmf_listen_opts *opts,
     788                 :            :                       const struct spdk_nvmf_listen_opts *opts_src, size_t opts_size)
     789                 :            : {
     790         [ -  + ]:        965 :         assert(opts);
     791         [ -  + ]:        965 :         assert(opts_src);
     792                 :            : 
     793                 :        965 :         opts->opts_size = opts_size;
     794                 :            : 
     795                 :            : #define SET_FIELD(field) \
     796                 :            :     if (offsetof(struct spdk_nvmf_listen_opts, field) + sizeof(opts->field) <= opts_size) { \
     797                 :            :                  opts->field = opts_src->field; \
     798                 :            :     } \
     799                 :            : 
     800         [ +  - ]:        965 :         SET_FIELD(transport_specific);
     801   [ +  -  -  + ]:        965 :         SET_FIELD(secure_channel);
     802         [ +  - ]:        965 :         SET_FIELD(ana_state);
     803         [ +  - ]:        965 :         SET_FIELD(sock_impl);
     804                 :            : #undef SET_FIELD
     805                 :            : 
     806                 :            :         /* Do not remove this statement, you should always update this statement when you adding a new field,
     807                 :            :          * and do not forget to add the SET_FIELD statement for your added field. */
     808                 :            :         SPDK_STATIC_ASSERT(sizeof(struct spdk_nvmf_listen_opts) == 32, "Incorrect size");
     809                 :        965 : }
     810                 :            : 
     811                 :            : void
     812                 :        484 : spdk_nvmf_listen_opts_init(struct spdk_nvmf_listen_opts *opts, size_t opts_size)
     813                 :            : {
     814                 :        484 :         struct spdk_nvmf_listen_opts opts_local = {};
     815                 :            : 
     816                 :            :         /* local version of opts should have defaults set here */
     817                 :        484 :         opts_local.ana_state = SPDK_NVME_ANA_OPTIMIZED_STATE;
     818                 :        484 :         nvmf_listen_opts_copy(opts, &opts_local, opts_size);
     819                 :        484 : }
     820                 :            : 
     821                 :            : int
     822                 :        481 : spdk_nvmf_tgt_listen_ext(struct spdk_nvmf_tgt *tgt, const struct spdk_nvme_transport_id *trid,
     823                 :            :                          struct spdk_nvmf_listen_opts *opts)
     824                 :            : {
     825                 :            :         struct spdk_nvmf_transport *transport;
     826                 :            :         int rc;
     827                 :        481 :         struct spdk_nvmf_listen_opts opts_local = {};
     828                 :            : 
     829         [ -  + ]:        481 :         if (!opts) {
     830                 :          0 :                 SPDK_ERRLOG("opts should not be NULL\n");
     831                 :          0 :                 return -EINVAL;
     832                 :            :         }
     833                 :            : 
     834         [ -  + ]:        481 :         if (!opts->opts_size) {
     835                 :          0 :                 SPDK_ERRLOG("The opts_size in opts structure should not be zero\n");
     836                 :          0 :                 return -EINVAL;
     837                 :            :         }
     838                 :            : 
     839                 :        481 :         transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring);
     840         [ -  + ]:        481 :         if (!transport) {
     841                 :          0 :                 SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
     842                 :            :                             trid->trstring);
     843                 :          0 :                 return -EINVAL;
     844                 :            :         }
     845                 :            : 
     846                 :        481 :         nvmf_listen_opts_copy(&opts_local, opts, opts->opts_size);
     847                 :        481 :         rc = spdk_nvmf_transport_listen(transport, trid, &opts_local);
     848         [ -  + ]:        481 :         if (rc < 0) {
     849                 :          0 :                 SPDK_ERRLOG("Unable to listen on address '%s'\n", trid->traddr);
     850                 :            :         }
     851                 :            : 
     852                 :        481 :         return rc;
     853                 :            : }
     854                 :            : 
     855                 :            : int
     856                 :          0 : spdk_nvmf_tgt_stop_listen(struct spdk_nvmf_tgt *tgt,
     857                 :            :                           struct spdk_nvme_transport_id *trid)
     858                 :            : {
     859                 :            :         struct spdk_nvmf_transport *transport;
     860                 :            :         int rc;
     861                 :            : 
     862                 :          0 :         transport = spdk_nvmf_tgt_get_transport(tgt, trid->trstring);
     863         [ #  # ]:          0 :         if (!transport) {
     864                 :          0 :                 SPDK_ERRLOG("Unable to find %s transport. The transport must be created first also make sure it is properly registered.\n",
     865                 :            :                             trid->trstring);
     866                 :          0 :                 return -EINVAL;
     867                 :            :         }
     868                 :            : 
     869                 :          0 :         rc = spdk_nvmf_transport_stop_listen(transport, trid);
     870         [ #  # ]:          0 :         if (rc < 0) {
     871                 :          0 :                 SPDK_ERRLOG("Failed to stop listening on address '%s'\n", trid->traddr);
     872                 :          0 :                 return rc;
     873                 :            :         }
     874                 :          0 :         return 0;
     875                 :            : }
     876                 :            : 
     877                 :            : struct spdk_nvmf_tgt_add_transport_ctx {
     878                 :            :         struct spdk_nvmf_tgt *tgt;
     879                 :            :         struct spdk_nvmf_transport *transport;
     880                 :            :         spdk_nvmf_tgt_add_transport_done_fn cb_fn;
     881                 :            :         void *cb_arg;
     882                 :            :         int status;
     883                 :            : };
     884                 :            : 
     885                 :            : static void
     886                 :          0 : _nvmf_tgt_remove_transport_done(struct spdk_io_channel_iter *i, int status)
     887                 :            : {
     888                 :          0 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     889                 :            : 
     890                 :          0 :         ctx->cb_fn(ctx->cb_arg, ctx->status);
     891                 :          0 :         free(ctx);
     892                 :          0 : }
     893                 :            : 
     894                 :            : static void
     895                 :          0 : _nvmf_tgt_remove_transport(struct spdk_io_channel_iter *i)
     896                 :            : {
     897                 :          0 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     898                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     899                 :          0 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
     900                 :            :         struct spdk_nvmf_transport_poll_group *tgroup, *tmp;
     901                 :            : 
     902         [ #  # ]:          0 :         TAILQ_FOREACH_SAFE(tgroup, &group->tgroups, link, tmp) {
     903         [ #  # ]:          0 :                 if (tgroup->transport == ctx->transport) {
     904         [ #  # ]:          0 :                         TAILQ_REMOVE(&group->tgroups, tgroup, link);
     905                 :          0 :                         nvmf_transport_poll_group_destroy(tgroup);
     906                 :            :                 }
     907                 :            :         }
     908                 :            : 
     909                 :          0 :         spdk_for_each_channel_continue(i, 0);
     910                 :          0 : }
     911                 :            : 
     912                 :            : static void
     913                 :        256 : _nvmf_tgt_add_transport_done(struct spdk_io_channel_iter *i, int status)
     914                 :            : {
     915                 :        256 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     916                 :            : 
     917         [ -  + ]:        256 :         if (status) {
     918                 :          0 :                 ctx->status = status;
     919                 :          0 :                 spdk_for_each_channel(ctx->tgt,
     920                 :            :                                       _nvmf_tgt_remove_transport,
     921                 :            :                                       ctx,
     922                 :            :                                       _nvmf_tgt_remove_transport_done);
     923                 :          0 :                 return;
     924                 :            :         }
     925                 :            : 
     926                 :        256 :         ctx->transport->tgt = ctx->tgt;
     927                 :        256 :         TAILQ_INSERT_TAIL(&ctx->tgt->transports, ctx->transport, link);
     928                 :        256 :         ctx->cb_fn(ctx->cb_arg, status);
     929                 :        256 :         free(ctx);
     930                 :            : }
     931                 :            : 
     932                 :            : static void
     933                 :        573 : _nvmf_tgt_add_transport(struct spdk_io_channel_iter *i)
     934                 :            : {
     935                 :        573 :         struct spdk_nvmf_tgt_add_transport_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     936                 :        573 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     937                 :        573 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
     938                 :            :         int rc;
     939                 :            : 
     940                 :        573 :         rc = nvmf_poll_group_add_transport(group, ctx->transport);
     941                 :        573 :         spdk_for_each_channel_continue(i, rc);
     942                 :        573 : }
     943                 :            : 
     944                 :            : void
     945                 :        256 : spdk_nvmf_tgt_add_transport(struct spdk_nvmf_tgt *tgt,
     946                 :            :                             struct spdk_nvmf_transport *transport,
     947                 :            :                             spdk_nvmf_tgt_add_transport_done_fn cb_fn,
     948                 :            :                             void *cb_arg)
     949                 :            : {
     950                 :            :         struct spdk_nvmf_tgt_add_transport_ctx *ctx;
     951                 :            : 
     952                 :        100 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_add_transport, transport, tgt->name);
     953                 :            : 
     954         [ -  + ]:        256 :         if (spdk_nvmf_tgt_get_transport(tgt, transport->ops->name)) {
     955                 :          0 :                 cb_fn(cb_arg, -EEXIST);
     956                 :          0 :                 return; /* transport already created */
     957                 :            :         }
     958                 :            : 
     959                 :        256 :         ctx = calloc(1, sizeof(*ctx));
     960         [ -  + ]:        256 :         if (!ctx) {
     961                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
     962                 :          0 :                 return;
     963                 :            :         }
     964                 :            : 
     965                 :        256 :         ctx->tgt = tgt;
     966                 :        256 :         ctx->transport = transport;
     967                 :        256 :         ctx->cb_fn = cb_fn;
     968                 :        256 :         ctx->cb_arg = cb_arg;
     969                 :            : 
     970                 :        256 :         spdk_for_each_channel(tgt,
     971                 :            :                               _nvmf_tgt_add_transport,
     972                 :            :                               ctx,
     973                 :            :                               _nvmf_tgt_add_transport_done);
     974                 :            : }
     975                 :            : 
     976                 :            : struct nvmf_tgt_pause_ctx {
     977                 :            :         struct spdk_nvmf_tgt *tgt;
     978                 :            :         spdk_nvmf_tgt_pause_polling_cb_fn cb_fn;
     979                 :            :         void *cb_arg;
     980                 :            : };
     981                 :            : 
     982                 :            : static void
     983                 :          0 : _nvmf_tgt_pause_polling_done(struct spdk_io_channel_iter *i, int status)
     984                 :            : {
     985                 :          0 :         struct nvmf_tgt_pause_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     986                 :            : 
     987                 :          0 :         ctx->tgt->state = NVMF_TGT_PAUSED;
     988                 :            : 
     989                 :          0 :         ctx->cb_fn(ctx->cb_arg, status);
     990                 :          0 :         free(ctx);
     991                 :          0 : }
     992                 :            : 
     993                 :            : static void
     994                 :          0 : _nvmf_tgt_pause_polling(struct spdk_io_channel_iter *i)
     995                 :            : {
     996                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
     997                 :          0 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
     998                 :            : 
     999                 :          0 :         spdk_poller_unregister(&group->poller);
    1000                 :            : 
    1001                 :          0 :         spdk_for_each_channel_continue(i, 0);
    1002                 :          0 : }
    1003                 :            : 
    1004                 :            : int
    1005                 :          0 : spdk_nvmf_tgt_pause_polling(struct spdk_nvmf_tgt *tgt, spdk_nvmf_tgt_pause_polling_cb_fn cb_fn,
    1006                 :            :                             void *cb_arg)
    1007                 :            : {
    1008                 :            :         struct nvmf_tgt_pause_ctx *ctx;
    1009                 :            : 
    1010                 :          0 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_pause_polling, tgt, tgt->name);
    1011                 :            : 
    1012      [ #  #  # ]:          0 :         switch (tgt->state) {
    1013                 :          0 :         case NVMF_TGT_PAUSING:
    1014                 :            :         case NVMF_TGT_RESUMING:
    1015                 :          0 :                 return -EBUSY;
    1016                 :          0 :         case NVMF_TGT_RUNNING:
    1017                 :          0 :                 break;
    1018                 :          0 :         default:
    1019                 :          0 :                 return -EINVAL;
    1020                 :            :         }
    1021                 :            : 
    1022                 :          0 :         ctx = calloc(1, sizeof(*ctx));
    1023         [ #  # ]:          0 :         if (!ctx) {
    1024                 :          0 :                 return -ENOMEM;
    1025                 :            :         }
    1026                 :            : 
    1027                 :            : 
    1028                 :          0 :         tgt->state = NVMF_TGT_PAUSING;
    1029                 :            : 
    1030                 :          0 :         ctx->tgt = tgt;
    1031                 :          0 :         ctx->cb_fn = cb_fn;
    1032                 :          0 :         ctx->cb_arg = cb_arg;
    1033                 :            : 
    1034                 :          0 :         spdk_for_each_channel(tgt,
    1035                 :            :                               _nvmf_tgt_pause_polling,
    1036                 :            :                               ctx,
    1037                 :            :                               _nvmf_tgt_pause_polling_done);
    1038                 :          0 :         return 0;
    1039                 :            : }
    1040                 :            : 
    1041                 :            : static void
    1042                 :          0 : _nvmf_tgt_resume_polling_done(struct spdk_io_channel_iter *i, int status)
    1043                 :            : {
    1044                 :          0 :         struct nvmf_tgt_pause_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
    1045                 :            : 
    1046                 :          0 :         ctx->tgt->state = NVMF_TGT_RUNNING;
    1047                 :            : 
    1048                 :          0 :         ctx->cb_fn(ctx->cb_arg, status);
    1049                 :          0 :         free(ctx);
    1050                 :          0 : }
    1051                 :            : 
    1052                 :            : static void
    1053                 :          0 : _nvmf_tgt_resume_polling(struct spdk_io_channel_iter *i)
    1054                 :            : {
    1055                 :          0 :         struct spdk_io_channel *ch = spdk_io_channel_iter_get_channel(i);
    1056                 :          0 :         struct spdk_nvmf_poll_group *group = spdk_io_channel_get_ctx(ch);
    1057                 :            : 
    1058         [ #  # ]:          0 :         assert(group->poller == NULL);
    1059                 :          0 :         group->poller = SPDK_POLLER_REGISTER(nvmf_poll_group_poll, group, 0);
    1060                 :            : 
    1061                 :          0 :         spdk_for_each_channel_continue(i, 0);
    1062                 :          0 : }
    1063                 :            : 
    1064                 :            : int
    1065                 :          0 : spdk_nvmf_tgt_resume_polling(struct spdk_nvmf_tgt *tgt, spdk_nvmf_tgt_resume_polling_cb_fn cb_fn,
    1066                 :            :                              void *cb_arg)
    1067                 :            : {
    1068                 :            :         struct nvmf_tgt_pause_ctx *ctx;
    1069                 :            : 
    1070                 :          0 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_tgt_resume_polling, tgt, tgt->name);
    1071                 :            : 
    1072      [ #  #  # ]:          0 :         switch (tgt->state) {
    1073                 :          0 :         case NVMF_TGT_PAUSING:
    1074                 :            :         case NVMF_TGT_RESUMING:
    1075                 :          0 :                 return -EBUSY;
    1076                 :          0 :         case NVMF_TGT_PAUSED:
    1077                 :          0 :                 break;
    1078                 :          0 :         default:
    1079                 :          0 :                 return -EINVAL;
    1080                 :            :         }
    1081                 :            : 
    1082                 :          0 :         ctx = calloc(1, sizeof(*ctx));
    1083         [ #  # ]:          0 :         if (!ctx) {
    1084                 :          0 :                 return -ENOMEM;
    1085                 :            :         }
    1086                 :            : 
    1087                 :          0 :         tgt->state = NVMF_TGT_RESUMING;
    1088                 :            : 
    1089                 :          0 :         ctx->tgt = tgt;
    1090                 :          0 :         ctx->cb_fn = cb_fn;
    1091                 :          0 :         ctx->cb_arg = cb_arg;
    1092                 :            : 
    1093                 :          0 :         spdk_for_each_channel(tgt,
    1094                 :            :                               _nvmf_tgt_resume_polling,
    1095                 :            :                               ctx,
    1096                 :            :                               _nvmf_tgt_resume_polling_done);
    1097                 :          0 :         return 0;
    1098                 :            : }
    1099                 :            : 
    1100                 :            : struct spdk_nvmf_subsystem *
    1101                 :      37213 : spdk_nvmf_tgt_find_subsystem(struct spdk_nvmf_tgt *tgt, const char *subnqn)
    1102                 :            : {
    1103                 :       1035 :         struct spdk_nvmf_subsystem subsystem;
    1104                 :            : 
    1105         [ -  + ]:      37213 :         if (!subnqn) {
    1106                 :          0 :                 return NULL;
    1107                 :            :         }
    1108                 :            : 
    1109                 :            :         /* Ensure that subnqn is null terminated */
    1110   [ -  +  -  + ]:      37213 :         if (!memchr(subnqn, '\0', SPDK_NVMF_NQN_MAX_LEN + 1)) {
    1111                 :          0 :                 SPDK_ERRLOG("Connect SUBNQN is not null terminated\n");
    1112                 :          0 :                 return NULL;
    1113                 :            :         }
    1114                 :            : 
    1115                 :      37213 :         snprintf(subsystem.subnqn, sizeof(subsystem.subnqn), "%s", subnqn);
    1116                 :      37213 :         return RB_FIND(subsystem_tree, &tgt->subsystems, &subsystem);
    1117                 :            : }
    1118                 :            : 
    1119                 :            : struct spdk_nvmf_transport *
    1120                 :       1979 : spdk_nvmf_tgt_get_transport(struct spdk_nvmf_tgt *tgt, const char *transport_name)
    1121                 :            : {
    1122                 :            :         struct spdk_nvmf_transport *transport;
    1123                 :            : 
    1124         [ +  + ]:       1979 :         TAILQ_FOREACH(transport, &tgt->transports, link) {
    1125   [ -  +  -  +  :       1448 :                 if (!strncasecmp(transport->ops->name, transport_name, SPDK_NVMF_TRSTRING_MAX_LEN)) {
                   +  - ]
    1126                 :       1448 :                         return transport;
    1127                 :            :                 }
    1128                 :            :         }
    1129                 :        531 :         return NULL;
    1130                 :            : }
    1131                 :            : 
    1132                 :            : struct nvmf_new_qpair_ctx {
    1133                 :            :         struct spdk_nvmf_qpair *qpair;
    1134                 :            :         struct spdk_nvmf_poll_group *group;
    1135                 :            : };
    1136                 :            : 
    1137                 :            : static void
    1138                 :       6812 : _nvmf_poll_group_add(void *_ctx)
    1139                 :            : {
    1140                 :       6812 :         struct nvmf_new_qpair_ctx *ctx = _ctx;
    1141                 :       6812 :         struct spdk_nvmf_qpair *qpair = ctx->qpair;
    1142                 :       6812 :         struct spdk_nvmf_poll_group *group = ctx->group;
    1143                 :            : 
    1144                 :       6812 :         free(_ctx);
    1145                 :            : 
    1146         [ -  + ]:       6812 :         if (spdk_nvmf_poll_group_add(group, qpair) != 0) {
    1147                 :          0 :                 SPDK_ERRLOG("Unable to add the qpair to a poll group.\n");
    1148                 :          0 :                 spdk_nvmf_qpair_disconnect(qpair);
    1149                 :            :         }
    1150                 :       6812 : }
    1151                 :            : 
    1152                 :            : void
    1153                 :       6812 : spdk_nvmf_tgt_new_qpair(struct spdk_nvmf_tgt *tgt, struct spdk_nvmf_qpair *qpair)
    1154                 :            : {
    1155                 :            :         struct spdk_nvmf_poll_group *group;
    1156                 :            :         struct nvmf_new_qpair_ctx *ctx;
    1157                 :            : 
    1158                 :       6812 :         group = spdk_nvmf_get_optimal_poll_group(qpair);
    1159         [ -  + ]:       6812 :         if (group == NULL) {
    1160         [ #  # ]:          0 :                 if (tgt->next_poll_group == NULL) {
    1161                 :          0 :                         tgt->next_poll_group = TAILQ_FIRST(&tgt->poll_groups);
    1162         [ #  # ]:          0 :                         if (tgt->next_poll_group == NULL) {
    1163                 :          0 :                                 SPDK_ERRLOG("No poll groups exist.\n");
    1164                 :          0 :                                 spdk_nvmf_qpair_disconnect(qpair);
    1165                 :          0 :                                 return;
    1166                 :            :                         }
    1167                 :            :                 }
    1168                 :          0 :                 group = tgt->next_poll_group;
    1169                 :          0 :                 tgt->next_poll_group = TAILQ_NEXT(group, link);
    1170                 :            :         }
    1171                 :            : 
    1172                 :       6812 :         ctx = calloc(1, sizeof(*ctx));
    1173         [ -  + ]:       6812 :         if (!ctx) {
    1174                 :          0 :                 SPDK_ERRLOG("Unable to send message to poll group.\n");
    1175                 :          0 :                 spdk_nvmf_qpair_disconnect(qpair);
    1176                 :          0 :                 return;
    1177                 :            :         }
    1178                 :            : 
    1179                 :       6812 :         ctx->qpair = qpair;
    1180                 :       6812 :         ctx->group = group;
    1181                 :            : 
    1182         [ -  + ]:       6812 :         pthread_mutex_lock(&group->mutex);
    1183                 :       6812 :         group->current_unassociated_qpairs++;
    1184         [ -  + ]:       6812 :         pthread_mutex_unlock(&group->mutex);
    1185                 :            : 
    1186                 :       6812 :         spdk_thread_send_msg(group->thread, _nvmf_poll_group_add, ctx);
    1187                 :            : }
    1188                 :            : 
    1189                 :            : struct spdk_nvmf_poll_group *
    1190                 :       1287 : spdk_nvmf_poll_group_create(struct spdk_nvmf_tgt *tgt)
    1191                 :            : {
    1192                 :            :         struct spdk_io_channel *ch;
    1193                 :            : 
    1194                 :       1287 :         ch = spdk_get_io_channel(tgt);
    1195         [ -  + ]:       1287 :         if (!ch) {
    1196                 :          0 :                 SPDK_ERRLOG("Unable to get I/O channel for target\n");
    1197                 :          0 :                 return NULL;
    1198                 :            :         }
    1199                 :            : 
    1200                 :       1287 :         return spdk_io_channel_get_ctx(ch);
    1201                 :            : }
    1202                 :            : 
    1203                 :            : void
    1204                 :       1287 : spdk_nvmf_poll_group_destroy(struct spdk_nvmf_poll_group *group,
    1205                 :            :                              spdk_nvmf_poll_group_destroy_done_fn cb_fn,
    1206                 :            :                              void *cb_arg)
    1207                 :            : {
    1208         [ -  + ]:       1287 :         assert(group->destroy_cb_fn == NULL);
    1209                 :       1287 :         group->destroy_cb_fn = cb_fn;
    1210                 :       1287 :         group->destroy_cb_arg = cb_arg;
    1211                 :            : 
    1212                 :            :         /* This function will put the io_channel associated with this poll group */
    1213                 :       1287 :         nvmf_tgt_destroy_poll_group_qpairs(group);
    1214                 :       1287 : }
    1215                 :            : 
    1216                 :            : int
    1217                 :       6812 : spdk_nvmf_poll_group_add(struct spdk_nvmf_poll_group *group,
    1218                 :            :                          struct spdk_nvmf_qpair *qpair)
    1219                 :            : {
    1220                 :       6812 :         int rc = -1;
    1221                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1222                 :            : 
    1223                 :       6812 :         TAILQ_INIT(&qpair->outstanding);
    1224                 :       6812 :         qpair->group = group;
    1225                 :       6812 :         qpair->ctrlr = NULL;
    1226                 :       6812 :         qpair->disconnect_started = false;
    1227                 :            : 
    1228         [ +  - ]:       6812 :         TAILQ_FOREACH(tgroup, &group->tgroups, link) {
    1229         [ +  - ]:       6812 :                 if (tgroup->transport == qpair->transport) {
    1230                 :       6812 :                         rc = nvmf_transport_poll_group_add(tgroup, qpair);
    1231                 :       6812 :                         break;
    1232                 :            :                 }
    1233                 :            :         }
    1234                 :            : 
    1235                 :            :         /* We add the qpair to the group only it is successfully added into the tgroup */
    1236         [ +  - ]:       6812 :         if (rc == 0) {
    1237                 :       2343 :                 SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_add_qpair, qpair, spdk_thread_get_id(group->thread));
    1238                 :       6812 :                 TAILQ_INSERT_TAIL(&group->qpairs, qpair, link);
    1239                 :       6812 :                 nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_CONNECTING);
    1240                 :            :         }
    1241                 :            : 
    1242                 :       6812 :         return rc;
    1243                 :            : }
    1244                 :            : 
    1245                 :            : static void
    1246                 :       1668 : _nvmf_ctrlr_destruct(void *ctx)
    1247                 :            : {
    1248                 :       1668 :         struct spdk_nvmf_ctrlr *ctrlr = ctx;
    1249                 :            : 
    1250                 :       1668 :         nvmf_ctrlr_destruct(ctrlr);
    1251                 :       1668 : }
    1252                 :            : 
    1253                 :            : static void
    1254                 :       6278 : _nvmf_ctrlr_free_from_qpair(void *ctx)
    1255                 :            : {
    1256                 :       6278 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
    1257                 :       6278 :         struct spdk_nvmf_ctrlr *ctrlr = qpair_ctx->ctrlr;
    1258                 :            :         uint32_t count;
    1259                 :            : 
    1260                 :       6278 :         spdk_bit_array_clear(ctrlr->qpair_mask, qpair_ctx->qid);
    1261   [ -  +  -  + ]:       6278 :         SPDK_DEBUGLOG(nvmf, "qpair_mask cleared, qid %u\n", qpair_ctx->qid);
    1262                 :       6278 :         count = spdk_bit_array_count_set(ctrlr->qpair_mask);
    1263         [ +  + ]:       6278 :         if (count == 0) {
    1264   [ -  +  -  + ]:       1668 :                 assert(!ctrlr->in_destruct);
    1265   [ -  +  -  + ]:       1668 :                 SPDK_DEBUGLOG(nvmf, "Last qpair %u, destroy ctrlr 0x%hx\n", qpair_ctx->qid, ctrlr->cntlid);
    1266                 :       1668 :                 ctrlr->in_destruct = true;
    1267                 :       1668 :                 spdk_thread_send_msg(ctrlr->subsys->thread, _nvmf_ctrlr_destruct, ctrlr);
    1268                 :            :         }
    1269                 :       6278 :         free(qpair_ctx);
    1270                 :       6278 : }
    1271                 :            : 
    1272                 :            : static void
    1273                 :       6812 : _nvmf_transport_qpair_fini_complete(void *cb_ctx)
    1274                 :            : {
    1275                 :       6812 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = cb_ctx;
    1276                 :            :         struct spdk_nvmf_ctrlr *ctrlr;
    1277                 :            : 
    1278                 :       6812 :         ctrlr = qpair_ctx->ctrlr;
    1279   [ -  +  -  + ]:       6812 :         SPDK_DEBUGLOG(nvmf, "Finish destroying qid %u\n", qpair_ctx->qid);
    1280                 :            : 
    1281         [ +  + ]:       6812 :         if (ctrlr) {
    1282         [ +  + ]:       6278 :                 if (qpair_ctx->qid == 0) {
    1283                 :            :                         /* Admin qpair is removed, so set the pointer to NULL.
    1284                 :            :                          * This operation is safe since we are on ctrlr thread now, admin qpair's thread is the same
    1285                 :            :                          * as controller's thread */
    1286         [ -  + ]:       1668 :                         assert(ctrlr->thread == spdk_get_thread());
    1287                 :       1668 :                         ctrlr->admin_qpair = NULL;
    1288                 :            :                 }
    1289                 :            :                 /* Free qpair id from controller's bit mask and destroy the controller if it is the last qpair */
    1290         [ +  - ]:       6278 :                 if (ctrlr->thread) {
    1291                 :       6278 :                         spdk_thread_send_msg(ctrlr->thread, _nvmf_ctrlr_free_from_qpair, qpair_ctx);
    1292                 :            :                 } else {
    1293                 :          0 :                         _nvmf_ctrlr_free_from_qpair(qpair_ctx);
    1294                 :            :                 }
    1295                 :            :         } else {
    1296                 :        534 :                 free(qpair_ctx);
    1297                 :            :         }
    1298                 :       6812 : }
    1299                 :            : 
    1300                 :            : void
    1301                 :       6812 : spdk_nvmf_poll_group_remove(struct spdk_nvmf_qpair *qpair)
    1302                 :            : {
    1303                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1304                 :            :         int rc;
    1305                 :            : 
    1306                 :       2343 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_remove_qpair, qpair,
    1307                 :            :                                  spdk_thread_get_id(qpair->group->thread));
    1308                 :       6812 :         nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_ERROR);
    1309                 :            : 
    1310                 :            :         /* Find the tgroup and remove the qpair from the tgroup */
    1311         [ +  - ]:       6812 :         TAILQ_FOREACH(tgroup, &qpair->group->tgroups, link) {
    1312         [ +  - ]:       6812 :                 if (tgroup->transport == qpair->transport) {
    1313                 :       6812 :                         rc = nvmf_transport_poll_group_remove(tgroup, qpair);
    1314   [ -  +  -  - ]:       6812 :                         if (rc && (rc != ENOTSUP)) {
    1315                 :          0 :                                 SPDK_ERRLOG("Cannot remove qpair=%p from transport group=%p\n",
    1316                 :            :                                             qpair, tgroup);
    1317                 :            :                         }
    1318                 :       6812 :                         break;
    1319                 :            :                 }
    1320                 :            :         }
    1321                 :            : 
    1322         [ +  + ]:       6812 :         TAILQ_REMOVE(&qpair->group->qpairs, qpair, link);
    1323                 :       6812 :         qpair->group = NULL;
    1324                 :       6812 : }
    1325                 :            : 
    1326                 :            : static void
    1327                 :     553094 : _nvmf_qpair_sgroup_req_clean(struct spdk_nvmf_subsystem_poll_group *sgroup,
    1328                 :            :                              const struct spdk_nvmf_qpair *qpair)
    1329                 :            : {
    1330                 :            :         struct spdk_nvmf_request *req, *tmp;
    1331         [ -  + ]:     553094 :         TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
    1332         [ #  # ]:          0 :                 if (req->qpair == qpair) {
    1333         [ #  # ]:          0 :                         TAILQ_REMOVE(&sgroup->queued, req, link);
    1334         [ #  # ]:          0 :                         if (nvmf_transport_req_free(req)) {
    1335                 :          0 :                                 SPDK_ERRLOG("Transport request free error!\n");
    1336                 :            :                         }
    1337                 :            :                 }
    1338                 :            :         }
    1339                 :     553094 : }
    1340                 :            : 
    1341                 :            : static void
    1342                 :       6812 : _nvmf_qpair_destroy(void *ctx, int status)
    1343                 :            : {
    1344                 :       6812 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
    1345                 :       6812 :         struct spdk_nvmf_qpair *qpair = qpair_ctx->qpair;
    1346                 :       6812 :         struct spdk_nvmf_ctrlr *ctrlr = qpair->ctrlr;
    1347                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1348                 :            :         uint32_t sid;
    1349                 :            : 
    1350         [ -  + ]:       6812 :         assert(qpair->state == SPDK_NVMF_QPAIR_DEACTIVATING);
    1351                 :       6812 :         qpair_ctx->qid = qpair->qid;
    1352                 :            : 
    1353   [ -  +  +  + ]:       6812 :         if (qpair->connect_received) {
    1354         [ +  + ]:       6788 :                 if (0 == qpair->qid) {
    1355         [ -  + ]:       1668 :                         assert(qpair->group->stat.current_admin_qpairs > 0);
    1356                 :       1668 :                         qpair->group->stat.current_admin_qpairs--;
    1357                 :            :                 } else {
    1358         [ -  + ]:       5120 :                         assert(qpair->group->stat.current_io_qpairs > 0);
    1359                 :       5120 :                         qpair->group->stat.current_io_qpairs--;
    1360                 :            :                 }
    1361                 :            :         } else {
    1362         [ -  + ]:         24 :                 pthread_mutex_lock(&qpair->group->mutex);
    1363                 :         24 :                 qpair->group->current_unassociated_qpairs--;
    1364         [ -  + ]:         24 :                 pthread_mutex_unlock(&qpair->group->mutex);
    1365                 :            :         }
    1366                 :            : 
    1367         [ +  + ]:       6812 :         if (ctrlr) {
    1368                 :       6278 :                 sgroup = &qpair->group->sgroups[ctrlr->subsys->id];
    1369                 :       6278 :                 _nvmf_qpair_sgroup_req_clean(sgroup, qpair);
    1370                 :            :         } else {
    1371         [ +  + ]:     547350 :                 for (sid = 0; sid < qpair->group->num_sgroups; sid++) {
    1372                 :     546816 :                         sgroup = &qpair->group->sgroups[sid];
    1373         [ -  + ]:     546816 :                         assert(sgroup != NULL);
    1374                 :     546816 :                         _nvmf_qpair_sgroup_req_clean(sgroup, qpair);
    1375                 :            :                 }
    1376                 :            :         }
    1377                 :            : 
    1378                 :       6812 :         nvmf_qpair_auth_destroy(qpair);
    1379                 :       6812 :         qpair_ctx->ctrlr = ctrlr;
    1380                 :       6812 :         spdk_nvmf_poll_group_remove(qpair);
    1381                 :       6812 :         nvmf_transport_qpair_fini(qpair, _nvmf_transport_qpair_fini_complete, qpair_ctx);
    1382                 :       6812 : }
    1383                 :            : 
    1384                 :            : static void
    1385                 :       1413 : _nvmf_qpair_disconnect_msg(void *ctx)
    1386                 :            : {
    1387                 :       1413 :         struct nvmf_qpair_disconnect_ctx *qpair_ctx = ctx;
    1388                 :            : 
    1389                 :       1413 :         spdk_nvmf_qpair_disconnect(qpair_ctx->qpair);
    1390                 :       1413 :         free(ctx);
    1391                 :       1413 : }
    1392                 :            : 
    1393                 :            : int
    1394                 :    1012785 : spdk_nvmf_qpair_disconnect(struct spdk_nvmf_qpair *qpair)
    1395                 :            : {
    1396                 :    1012785 :         struct spdk_nvmf_poll_group *group = qpair->group;
    1397                 :            :         struct nvmf_qpair_disconnect_ctx *qpair_ctx;
    1398                 :            : 
    1399         [ +  + ]:    1012785 :         if (__atomic_test_and_set(&qpair->disconnect_started, __ATOMIC_RELAXED)) {
    1400                 :    1004560 :                 return -EINPROGRESS;
    1401                 :            :         }
    1402                 :            : 
    1403                 :            :         /* If we get a qpair in the uninitialized state, we can just destroy it immediately */
    1404         [ -  + ]:       8225 :         if (qpair->state == SPDK_NVMF_QPAIR_UNINITIALIZED) {
    1405                 :          0 :                 nvmf_transport_qpair_fini(qpair, NULL, NULL);
    1406                 :          0 :                 return 0;
    1407                 :            :         }
    1408                 :            : 
    1409         [ -  + ]:       8225 :         assert(group != NULL);
    1410         [ +  + ]:       8225 :         if (spdk_get_thread() != group->thread) {
    1411                 :            :                 /* clear the atomic so we can set it on the next call on the proper thread. */
    1412                 :       1413 :                 __atomic_clear(&qpair->disconnect_started, __ATOMIC_RELAXED);
    1413                 :       1413 :                 qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx));
    1414         [ -  + ]:       1413 :                 if (!qpair_ctx) {
    1415                 :          0 :                         SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n");
    1416                 :          0 :                         return -ENOMEM;
    1417                 :            :                 }
    1418                 :       1413 :                 qpair_ctx->qpair = qpair;
    1419                 :       1413 :                 spdk_thread_send_msg(group->thread, _nvmf_qpair_disconnect_msg, qpair_ctx);
    1420                 :       1413 :                 return 0;
    1421                 :            :         }
    1422                 :            : 
    1423                 :       2343 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_qpair_disconnect, qpair, spdk_thread_get_id(group->thread));
    1424         [ -  + ]:       6812 :         assert(spdk_nvmf_qpair_is_active(qpair));
    1425                 :       6812 :         nvmf_qpair_set_state(qpair, SPDK_NVMF_QPAIR_DEACTIVATING);
    1426                 :            : 
    1427                 :       6812 :         qpair_ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_ctx));
    1428         [ -  + ]:       6812 :         if (!qpair_ctx) {
    1429                 :          0 :                 SPDK_ERRLOG("Unable to allocate context for nvmf_qpair_disconnect\n");
    1430                 :          0 :                 return -ENOMEM;
    1431                 :            :         }
    1432                 :            : 
    1433                 :       6812 :         qpair_ctx->qpair = qpair;
    1434                 :            : 
    1435                 :            :         /* Check for outstanding I/O */
    1436         [ +  + ]:       6812 :         if (!TAILQ_EMPTY(&qpair->outstanding)) {
    1437                 :        752 :                 SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_drain_qpair, qpair, spdk_thread_get_id(group->thread));
    1438                 :       1652 :                 qpair->state_cb = _nvmf_qpair_destroy;
    1439                 :       1652 :                 qpair->state_cb_arg = qpair_ctx;
    1440                 :       1652 :                 nvmf_qpair_abort_pending_zcopy_reqs(qpair);
    1441                 :       1652 :                 nvmf_qpair_free_aer(qpair);
    1442                 :       1652 :                 return 0;
    1443                 :            :         }
    1444                 :            : 
    1445                 :       5160 :         _nvmf_qpair_destroy(qpair_ctx, 0);
    1446                 :            : 
    1447                 :       5160 :         return 0;
    1448                 :            : }
    1449                 :            : 
    1450                 :            : int
    1451                 :        296 : spdk_nvmf_qpair_get_peer_trid(struct spdk_nvmf_qpair *qpair,
    1452                 :            :                               struct spdk_nvme_transport_id *trid)
    1453                 :            : {
    1454         [ -  + ]:        296 :         memset(trid, 0, sizeof(*trid));
    1455                 :        296 :         return nvmf_transport_qpair_get_peer_trid(qpair, trid);
    1456                 :            : }
    1457                 :            : 
    1458                 :            : int
    1459                 :          0 : spdk_nvmf_qpair_get_local_trid(struct spdk_nvmf_qpair *qpair,
    1460                 :            :                                struct spdk_nvme_transport_id *trid)
    1461                 :            : {
    1462         [ #  # ]:          0 :         memset(trid, 0, sizeof(*trid));
    1463                 :          0 :         return nvmf_transport_qpair_get_local_trid(qpair, trid);
    1464                 :            : }
    1465                 :            : 
    1466                 :            : int
    1467                 :       9066 : spdk_nvmf_qpair_get_listen_trid(struct spdk_nvmf_qpair *qpair,
    1468                 :            :                                 struct spdk_nvme_transport_id *trid)
    1469                 :            : {
    1470         [ -  + ]:       9066 :         memset(trid, 0, sizeof(*trid));
    1471                 :       9066 :         return nvmf_transport_qpair_get_listen_trid(qpair, trid);
    1472                 :            : }
    1473                 :            : 
    1474                 :            : static int
    1475                 :      22948 : poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
    1476                 :            :                             struct spdk_nvmf_subsystem *subsystem)
    1477                 :            : {
    1478                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1479                 :            :         uint32_t i, j;
    1480                 :            :         struct spdk_nvmf_ns *ns;
    1481                 :            :         struct spdk_nvmf_registrant *reg, *tmp;
    1482                 :            :         struct spdk_io_channel *ch;
    1483                 :            :         struct spdk_nvmf_subsystem_pg_ns_info *ns_info;
    1484                 :            :         struct spdk_nvmf_ctrlr *ctrlr;
    1485                 :            :         bool ns_changed;
    1486                 :            : 
    1487                 :            :         /* Make sure our poll group has memory for this subsystem allocated */
    1488         [ -  + ]:      22948 :         if (subsystem->id >= group->num_sgroups) {
    1489                 :          0 :                 return -ENOMEM;
    1490                 :            :         }
    1491                 :            : 
    1492                 :      22948 :         sgroup = &group->sgroups[subsystem->id];
    1493                 :            : 
    1494                 :            :         /* Make sure the array of namespace information is the correct size */
    1495   [ +  +  +  + ]:      22948 :         if (sgroup->num_ns == 0 && subsystem->max_nsid > 0) {
    1496                 :            :                 /* First allocation */
    1497                 :       1015 :                 sgroup->ns_info = calloc(subsystem->max_nsid, sizeof(struct spdk_nvmf_subsystem_pg_ns_info));
    1498         [ -  + ]:       1015 :                 if (!sgroup->ns_info) {
    1499                 :          0 :                         return -ENOMEM;
    1500                 :            :                 }
    1501                 :       1015 :                 sgroup->num_ns = subsystem->max_nsid;
    1502                 :            :         }
    1503                 :            : 
    1504                 :      22948 :         ns_changed = false;
    1505                 :            : 
    1506                 :            :         /* Detect bdevs that were added or removed */
    1507         [ +  + ]:     321793 :         for (i = 0; i < sgroup->num_ns; i++) {
    1508                 :     298845 :                 ns = subsystem->ns[i];
    1509                 :     298845 :                 ns_info = &sgroup->ns_info[i];
    1510                 :     298845 :                 ch = ns_info->channel;
    1511                 :            : 
    1512   [ +  +  +  + ]:     298845 :                 if (ns == NULL && ch == NULL) {
    1513                 :            :                         /* Both NULL. Leave empty */
    1514   [ +  +  +  - ]:      24913 :                 } else if (ns == NULL && ch != NULL) {
    1515                 :            :                         /* There was a channel here, but the namespace is gone. */
    1516                 :       7028 :                         ns_changed = true;
    1517                 :       7028 :                         spdk_put_io_channel(ch);
    1518                 :       7028 :                         ns_info->channel = NULL;
    1519   [ +  -  +  + ]:      17885 :                 } else if (ns != NULL && ch == NULL) {
    1520                 :            :                         /* A namespace appeared but there is no channel yet */
    1521                 :       7863 :                         ns_changed = true;
    1522                 :       7863 :                         ch = spdk_bdev_get_io_channel(ns->desc);
    1523         [ -  + ]:       7863 :                         if (ch == NULL) {
    1524                 :          0 :                                 SPDK_ERRLOG("Could not allocate I/O channel.\n");
    1525                 :          0 :                                 return -ENOMEM;
    1526                 :            :                         }
    1527                 :       7863 :                         ns_info->channel = ch;
    1528         [ -  + ]:      10022 :                 } else if (spdk_uuid_compare(&ns_info->uuid, spdk_bdev_get_uuid(ns->bdev)) != 0) {
    1529                 :            :                         /* A namespace was here before, but was replaced by a new one. */
    1530                 :          0 :                         ns_changed = true;
    1531                 :          0 :                         spdk_put_io_channel(ns_info->channel);
    1532         [ #  # ]:          0 :                         memset(ns_info, 0, sizeof(*ns_info));
    1533                 :            : 
    1534                 :          0 :                         ch = spdk_bdev_get_io_channel(ns->desc);
    1535         [ #  # ]:          0 :                         if (ch == NULL) {
    1536                 :          0 :                                 SPDK_ERRLOG("Could not allocate I/O channel.\n");
    1537                 :          0 :                                 return -ENOMEM;
    1538                 :            :                         }
    1539                 :          0 :                         ns_info->channel = ch;
    1540         [ +  + ]:      10022 :                 } else if (ns_info->num_blocks != spdk_bdev_get_num_blocks(ns->bdev)) {
    1541                 :            :                         /* Namespace is still there but size has changed */
    1542   [ -  +  -  + ]:        273 :                         SPDK_DEBUGLOG(nvmf, "Namespace resized: subsystem_id %u,"
    1543                 :            :                                       " nsid %u, pg %p, old %" PRIu64 ", new %" PRIu64 "\n",
    1544                 :            :                                       subsystem->id,
    1545                 :            :                                       ns->nsid,
    1546                 :            :                                       group,
    1547                 :            :                                       ns_info->num_blocks,
    1548                 :            :                                       spdk_bdev_get_num_blocks(ns->bdev));
    1549                 :        273 :                         ns_changed = true;
    1550                 :            :                 }
    1551                 :            : 
    1552         [ +  + ]:     298845 :                 if (ns == NULL) {
    1553         [ -  + ]:     280960 :                         memset(ns_info, 0, sizeof(*ns_info));
    1554                 :            :                 } else {
    1555                 :      17885 :                         ns_info->uuid = *spdk_bdev_get_uuid(ns->bdev);
    1556                 :      17885 :                         ns_info->num_blocks = spdk_bdev_get_num_blocks(ns->bdev);
    1557                 :      17885 :                         ns_info->crkey = ns->crkey;
    1558                 :      17885 :                         ns_info->rtype = ns->rtype;
    1559         [ -  + ]:      17885 :                         if (ns->holder) {
    1560                 :          0 :                                 ns_info->holder_id = ns->holder->hostid;
    1561                 :            :                         }
    1562                 :            : 
    1563         [ -  + ]:      17885 :                         memset(&ns_info->reg_hostid, 0, SPDK_NVMF_MAX_NUM_REGISTRANTS * sizeof(struct spdk_uuid));
    1564                 :      17885 :                         j = 0;
    1565         [ -  + ]:      17885 :                         TAILQ_FOREACH_SAFE(reg, &ns->registrants, link, tmp) {
    1566         [ #  # ]:          0 :                                 if (j >= SPDK_NVMF_MAX_NUM_REGISTRANTS) {
    1567                 :          0 :                                         SPDK_ERRLOG("Maximum %u registrants can support.\n", SPDK_NVMF_MAX_NUM_REGISTRANTS);
    1568                 :          0 :                                         return -EINVAL;
    1569                 :            :                                 }
    1570                 :          0 :                                 ns_info->reg_hostid[j++] = reg->hostid;
    1571                 :            :                         }
    1572                 :            :                 }
    1573                 :            :         }
    1574                 :            : 
    1575         [ +  + ]:      22948 :         if (ns_changed) {
    1576         [ +  + ]:      26103 :                 TAILQ_FOREACH(ctrlr, &subsystem->ctrlrs, link) {
    1577         [ +  + ]:      10945 :                         if (ctrlr->thread != spdk_get_thread()) {
    1578                 :       7287 :                                 continue;
    1579                 :            :                         }
    1580                 :            :                         /* It is possible that a ctrlr was added but the admin_qpair hasn't been
    1581                 :            :                          * assigned yet.
    1582                 :            :                          */
    1583         [ +  + ]:       3658 :                         if (!ctrlr->admin_qpair) {
    1584                 :         11 :                                 continue;
    1585                 :            :                         }
    1586         [ +  - ]:       3647 :                         if (ctrlr->admin_qpair->group == group) {
    1587                 :       3647 :                                 nvmf_ctrlr_async_event_ns_notice(ctrlr);
    1588                 :       3647 :                                 nvmf_ctrlr_async_event_ana_change_notice(ctrlr);
    1589                 :            :                         }
    1590                 :            :                 }
    1591                 :            :         }
    1592                 :            : 
    1593                 :      22948 :         return 0;
    1594                 :            : }
    1595                 :            : 
    1596                 :            : int
    1597                 :          0 : nvmf_poll_group_update_subsystem(struct spdk_nvmf_poll_group *group,
    1598                 :            :                                  struct spdk_nvmf_subsystem *subsystem)
    1599                 :            : {
    1600                 :          0 :         return poll_group_update_subsystem(group, subsystem);
    1601                 :            : }
    1602                 :            : 
    1603                 :            : int
    1604                 :       3589 : nvmf_poll_group_add_subsystem(struct spdk_nvmf_poll_group *group,
    1605                 :            :                               struct spdk_nvmf_subsystem *subsystem,
    1606                 :            :                               spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1607                 :            : {
    1608                 :       3589 :         int rc = 0;
    1609                 :       3589 :         struct spdk_nvmf_subsystem_poll_group *sgroup = &group->sgroups[subsystem->id];
    1610                 :            :         struct spdk_nvmf_request *req, *tmp;
    1611                 :            :         uint32_t i;
    1612                 :            : 
    1613         [ -  + ]:       3589 :         if (!TAILQ_EMPTY(&sgroup->queued)) {
    1614                 :          0 :                 SPDK_ERRLOG("sgroup->queued not empty when adding subsystem\n");
    1615         [ #  # ]:          0 :                 TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
    1616         [ #  # ]:          0 :                         TAILQ_REMOVE(&sgroup->queued, req, link);
    1617         [ #  # ]:          0 :                         if (nvmf_transport_req_free(req)) {
    1618                 :          0 :                                 SPDK_ERRLOG("Transport request free error!\n");
    1619                 :            :                         }
    1620                 :            :                 }
    1621                 :          0 :                 assert(false);
    1622                 :            :         }
    1623                 :            : 
    1624                 :       3589 :         rc = poll_group_update_subsystem(group, subsystem);
    1625         [ -  + ]:       3589 :         if (rc) {
    1626                 :          0 :                 nvmf_poll_group_remove_subsystem(group, subsystem, NULL, NULL);
    1627                 :          0 :                 goto fini;
    1628                 :            :         }
    1629                 :            : 
    1630                 :       3589 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1631                 :            : 
    1632         [ +  + ]:      33164 :         for (i = 0; i < sgroup->num_ns; i++) {
    1633                 :      29575 :                 sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1634                 :            :         }
    1635                 :            : 
    1636                 :       3589 : fini:
    1637         [ +  + ]:       3589 :         if (cb_fn) {
    1638                 :       2299 :                 cb_fn(cb_arg, rc);
    1639                 :            :         }
    1640                 :            : 
    1641                 :        876 :         SPDK_DTRACE_PROBE2_TICKS(nvmf_poll_group_add_subsystem, spdk_thread_get_id(group->thread),
    1642                 :            :                                  subsystem->subnqn);
    1643                 :            : 
    1644                 :       3589 :         return rc;
    1645                 :            : }
    1646                 :            : 
    1647                 :            : static void
    1648                 :       2299 : _nvmf_poll_group_remove_subsystem_cb(void *ctx, int status)
    1649                 :            : {
    1650                 :       2299 :         struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
    1651                 :            :         struct spdk_nvmf_subsystem *subsystem;
    1652                 :            :         struct spdk_nvmf_poll_group *group;
    1653                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1654                 :       2299 :         spdk_nvmf_poll_group_mod_done cpl_fn = NULL;
    1655                 :       2299 :         void *cpl_ctx = NULL;
    1656                 :            :         uint32_t nsid;
    1657                 :            : 
    1658                 :       2299 :         group = qpair_ctx->group;
    1659                 :       2299 :         subsystem = qpair_ctx->subsystem;
    1660                 :       2299 :         cpl_fn = qpair_ctx->cpl_fn;
    1661                 :       2299 :         cpl_ctx = qpair_ctx->cpl_ctx;
    1662                 :       2299 :         sgroup = &group->sgroups[subsystem->id];
    1663                 :            : 
    1664         [ -  + ]:       2299 :         if (status) {
    1665                 :          0 :                 goto fini;
    1666                 :            :         }
    1667                 :            : 
    1668         [ +  + ]:      31871 :         for (nsid = 0; nsid < sgroup->num_ns; nsid++) {
    1669         [ +  + ]:      29572 :                 if (sgroup->ns_info[nsid].channel) {
    1670                 :        832 :                         spdk_put_io_channel(sgroup->ns_info[nsid].channel);
    1671                 :        832 :                         sgroup->ns_info[nsid].channel = NULL;
    1672                 :            :                 }
    1673                 :            :         }
    1674                 :            : 
    1675                 :       2299 :         sgroup->num_ns = 0;
    1676                 :       2299 :         free(sgroup->ns_info);
    1677                 :       2299 :         sgroup->ns_info = NULL;
    1678                 :       2299 : fini:
    1679                 :       2299 :         free(qpair_ctx);
    1680         [ +  - ]:       2299 :         if (cpl_fn) {
    1681                 :       2299 :                 cpl_fn(cpl_ctx, status);
    1682                 :            :         }
    1683                 :       2299 : }
    1684                 :            : 
    1685                 :            : static void nvmf_poll_group_remove_subsystem_msg(void *ctx);
    1686                 :            : 
    1687                 :            : static void
    1688                 :     254011 : nvmf_poll_group_remove_subsystem_msg(void *ctx)
    1689                 :            : {
    1690                 :            :         struct spdk_nvmf_qpair *qpair, *qpair_tmp;
    1691                 :            :         struct spdk_nvmf_subsystem *subsystem;
    1692                 :            :         struct spdk_nvmf_poll_group *group;
    1693                 :     254011 :         struct nvmf_qpair_disconnect_many_ctx *qpair_ctx = ctx;
    1694                 :     254011 :         bool qpairs_found = false;
    1695                 :     254011 :         int rc = 0;
    1696                 :            : 
    1697                 :     254011 :         group = qpair_ctx->group;
    1698                 :     254011 :         subsystem = qpair_ctx->subsystem;
    1699                 :            : 
    1700         [ +  + ]:    1259206 :         TAILQ_FOREACH_SAFE(qpair, &group->qpairs, link, qpair_tmp) {
    1701   [ +  -  +  + ]:    1005195 :                 if ((qpair->ctrlr != NULL) && (qpair->ctrlr->subsys == subsystem)) {
    1702                 :    1004639 :                         qpairs_found = true;
    1703                 :    1004639 :                         rc = spdk_nvmf_qpair_disconnect(qpair);
    1704   [ +  +  -  + ]:    1004639 :                         if (rc && rc != -EINPROGRESS) {
    1705                 :          0 :                                 break;
    1706                 :            :                         }
    1707                 :            :                 }
    1708                 :            :         }
    1709                 :            : 
    1710         [ +  + ]:     254011 :         if (!qpairs_found) {
    1711                 :       2299 :                 _nvmf_poll_group_remove_subsystem_cb(ctx, 0);
    1712                 :       2299 :                 return;
    1713                 :            :         }
    1714                 :            : 
    1715                 :            :         /* Some qpairs are in process of being disconnected. Send a message and try to remove them again */
    1716                 :     251712 :         spdk_thread_send_msg(spdk_get_thread(), nvmf_poll_group_remove_subsystem_msg, ctx);
    1717                 :            : }
    1718                 :            : 
    1719                 :            : void
    1720                 :       2299 : nvmf_poll_group_remove_subsystem(struct spdk_nvmf_poll_group *group,
    1721                 :            :                                  struct spdk_nvmf_subsystem *subsystem,
    1722                 :            :                                  spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1723                 :            : {
    1724                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1725                 :            :         struct nvmf_qpair_disconnect_many_ctx *ctx;
    1726                 :            :         uint32_t i;
    1727                 :            : 
    1728                 :        588 :         SPDK_DTRACE_PROBE3_TICKS(nvmf_poll_group_remove_subsystem, group, spdk_thread_get_id(group->thread),
    1729                 :            :                                  subsystem->subnqn);
    1730                 :            : 
    1731                 :       2299 :         ctx = calloc(1, sizeof(struct nvmf_qpair_disconnect_many_ctx));
    1732         [ -  + ]:       2299 :         if (!ctx) {
    1733                 :          0 :                 SPDK_ERRLOG("Unable to allocate memory for context to remove poll subsystem\n");
    1734         [ #  # ]:          0 :                 if (cb_fn) {
    1735                 :          0 :                         cb_fn(cb_arg, -1);
    1736                 :            :                 }
    1737                 :          0 :                 return;
    1738                 :            :         }
    1739                 :            : 
    1740                 :       2299 :         ctx->group = group;
    1741                 :       2299 :         ctx->subsystem = subsystem;
    1742                 :       2299 :         ctx->cpl_fn = cb_fn;
    1743                 :       2299 :         ctx->cpl_ctx = cb_arg;
    1744                 :            : 
    1745                 :       2299 :         sgroup = &group->sgroups[subsystem->id];
    1746                 :       2299 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
    1747                 :            : 
    1748         [ +  + ]:      31871 :         for (i = 0; i < sgroup->num_ns; i++) {
    1749                 :      29572 :                 sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_INACTIVE;
    1750                 :            :         }
    1751                 :            : 
    1752                 :       2299 :         nvmf_poll_group_remove_subsystem_msg(ctx);
    1753                 :            : }
    1754                 :            : 
    1755                 :            : void
    1756                 :      19359 : nvmf_poll_group_pause_subsystem(struct spdk_nvmf_poll_group *group,
    1757                 :            :                                 struct spdk_nvmf_subsystem *subsystem,
    1758                 :            :                                 uint32_t nsid,
    1759                 :            :                                 spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1760                 :            : {
    1761                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1762                 :      19359 :         struct spdk_nvmf_subsystem_pg_ns_info *ns_info = NULL;
    1763                 :      19359 :         int rc = 0;
    1764                 :            :         uint32_t i;
    1765                 :            : 
    1766         [ -  + ]:      19359 :         if (subsystem->id >= group->num_sgroups) {
    1767                 :          0 :                 rc = -1;
    1768                 :          0 :                 goto fini;
    1769                 :            :         }
    1770                 :            : 
    1771                 :      19359 :         sgroup = &group->sgroups[subsystem->id];
    1772         [ -  + ]:      19359 :         if (sgroup->state == SPDK_NVMF_SUBSYSTEM_PAUSED) {
    1773                 :          0 :                 goto fini;
    1774                 :            :         }
    1775                 :      19359 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
    1776                 :            : 
    1777         [ -  + ]:      19359 :         if (nsid == SPDK_NVME_GLOBAL_NS_TAG) {
    1778         [ #  # ]:          0 :                 for (i = 0; i < sgroup->num_ns; i++) {
    1779                 :          0 :                         ns_info = &sgroup->ns_info[i];
    1780                 :          0 :                         ns_info->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
    1781                 :            :                 }
    1782                 :            :         } else {
    1783                 :            :                 /* NOTE: This implicitly also checks for 0, since 0 - 1 wraps around to UINT32_MAX. */
    1784         [ +  + ]:      19359 :                 if (nsid - 1 < sgroup->num_ns) {
    1785                 :       9124 :                         ns_info  = &sgroup->ns_info[nsid - 1];
    1786                 :       9124 :                         ns_info->state = SPDK_NVMF_SUBSYSTEM_PAUSING;
    1787                 :            :                 }
    1788                 :            :         }
    1789                 :            : 
    1790         [ +  + ]:      19359 :         if (sgroup->mgmt_io_outstanding > 0) {
    1791         [ -  + ]:          2 :                 assert(sgroup->cb_fn == NULL);
    1792                 :          2 :                 sgroup->cb_fn = cb_fn;
    1793         [ -  + ]:          2 :                 assert(sgroup->cb_arg == NULL);
    1794                 :          2 :                 sgroup->cb_arg = cb_arg;
    1795                 :          2 :                 return;
    1796                 :            :         }
    1797                 :            : 
    1798         [ -  + ]:      19357 :         if (nsid == SPDK_NVME_GLOBAL_NS_TAG) {
    1799         [ #  # ]:          0 :                 for (i = 0; i < sgroup->num_ns; i++) {
    1800                 :          0 :                         ns_info = &sgroup->ns_info[i];
    1801                 :            : 
    1802         [ #  # ]:          0 :                         if (ns_info->io_outstanding > 0) {
    1803         [ #  # ]:          0 :                                 assert(sgroup->cb_fn == NULL);
    1804                 :          0 :                                 sgroup->cb_fn = cb_fn;
    1805         [ #  # ]:          0 :                                 assert(sgroup->cb_arg == NULL);
    1806                 :          0 :                                 sgroup->cb_arg = cb_arg;
    1807                 :          0 :                                 return;
    1808                 :            :                         }
    1809                 :            :                 }
    1810                 :            :         } else {
    1811   [ +  +  +  + ]:      19357 :                 if (ns_info != NULL && ns_info->io_outstanding > 0) {
    1812         [ -  + ]:       1060 :                         assert(sgroup->cb_fn == NULL);
    1813                 :       1060 :                         sgroup->cb_fn = cb_fn;
    1814         [ -  + ]:       1060 :                         assert(sgroup->cb_arg == NULL);
    1815                 :       1060 :                         sgroup->cb_arg = cb_arg;
    1816                 :       1060 :                         return;
    1817                 :            :                 }
    1818                 :            :         }
    1819                 :            : 
    1820         [ -  + ]:      18297 :         assert(sgroup->mgmt_io_outstanding == 0);
    1821                 :      18297 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_PAUSED;
    1822                 :      18297 : fini:
    1823         [ +  - ]:      18297 :         if (cb_fn) {
    1824                 :      18297 :                 cb_fn(cb_arg, rc);
    1825                 :            :         }
    1826                 :            : }
    1827                 :            : 
    1828                 :            : void
    1829                 :      19359 : nvmf_poll_group_resume_subsystem(struct spdk_nvmf_poll_group *group,
    1830                 :            :                                  struct spdk_nvmf_subsystem *subsystem,
    1831                 :            :                                  spdk_nvmf_poll_group_mod_done cb_fn, void *cb_arg)
    1832                 :            : {
    1833                 :            :         struct spdk_nvmf_request *req, *tmp;
    1834                 :            :         struct spdk_nvmf_subsystem_poll_group *sgroup;
    1835                 :      19359 :         int rc = 0;
    1836                 :            :         uint32_t i;
    1837                 :            : 
    1838         [ -  + ]:      19359 :         if (subsystem->id >= group->num_sgroups) {
    1839                 :          0 :                 rc = -1;
    1840                 :          0 :                 goto fini;
    1841                 :            :         }
    1842                 :            : 
    1843                 :      19359 :         sgroup = &group->sgroups[subsystem->id];
    1844                 :            : 
    1845         [ -  + ]:      19359 :         if (sgroup->state == SPDK_NVMF_SUBSYSTEM_ACTIVE) {
    1846                 :          0 :                 goto fini;
    1847                 :            :         }
    1848                 :            : 
    1849                 :      19359 :         rc = poll_group_update_subsystem(group, subsystem);
    1850         [ -  + ]:      19359 :         if (rc) {
    1851                 :          0 :                 goto fini;
    1852                 :            :         }
    1853                 :            : 
    1854         [ +  + ]:     288629 :         for (i = 0; i < sgroup->num_ns; i++) {
    1855                 :     269270 :                 sgroup->ns_info[i].state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1856                 :            :         }
    1857                 :            : 
    1858                 :      19359 :         sgroup->state = SPDK_NVMF_SUBSYSTEM_ACTIVE;
    1859                 :            : 
    1860                 :            :         /* Release all queued requests */
    1861         [ +  + ]:     111960 :         TAILQ_FOREACH_SAFE(req, &sgroup->queued, link, tmp) {
    1862         [ +  + ]:      92601 :                 TAILQ_REMOVE(&sgroup->queued, req, link);
    1863         [ +  + ]:      92601 :                 if (spdk_nvmf_request_using_zcopy(req)) {
    1864                 :      86449 :                         spdk_nvmf_request_zcopy_start(req);
    1865                 :            :                 } else {
    1866                 :       6152 :                         spdk_nvmf_request_exec(req);
    1867                 :            :                 }
    1868                 :            : 
    1869                 :            :         }
    1870                 :      19359 : fini:
    1871         [ +  - ]:      19359 :         if (cb_fn) {
    1872                 :      19359 :                 cb_fn(cb_arg, rc);
    1873                 :            :         }
    1874                 :      19359 : }
    1875                 :            : 
    1876                 :            : 
    1877                 :            : struct spdk_nvmf_poll_group *
    1878                 :       6812 : spdk_nvmf_get_optimal_poll_group(struct spdk_nvmf_qpair *qpair)
    1879                 :            : {
    1880                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1881                 :            : 
    1882                 :       6812 :         tgroup = nvmf_transport_get_optimal_poll_group(qpair->transport, qpair);
    1883                 :            : 
    1884         [ -  + ]:       6812 :         if (tgroup == NULL) {
    1885                 :          0 :                 return NULL;
    1886                 :            :         }
    1887                 :            : 
    1888                 :       6812 :         return tgroup->group;
    1889                 :            : }
    1890                 :            : 
    1891                 :            : void
    1892                 :         44 : spdk_nvmf_poll_group_dump_stat(struct spdk_nvmf_poll_group *group, struct spdk_json_write_ctx *w)
    1893                 :            : {
    1894                 :            :         struct spdk_nvmf_transport_poll_group *tgroup;
    1895                 :            : 
    1896                 :         44 :         spdk_json_write_object_begin(w);
    1897                 :            : 
    1898                 :         44 :         spdk_json_write_named_string(w, "name", spdk_thread_get_name(spdk_get_thread()));
    1899                 :         44 :         spdk_json_write_named_uint32(w, "admin_qpairs", group->stat.admin_qpairs);
    1900                 :         44 :         spdk_json_write_named_uint32(w, "io_qpairs", group->stat.io_qpairs);
    1901                 :         44 :         spdk_json_write_named_uint32(w, "current_admin_qpairs", group->stat.current_admin_qpairs);
    1902                 :         44 :         spdk_json_write_named_uint32(w, "current_io_qpairs", group->stat.current_io_qpairs);
    1903                 :         44 :         spdk_json_write_named_uint64(w, "pending_bdev_io", group->stat.pending_bdev_io);
    1904                 :         44 :         spdk_json_write_named_uint64(w, "completed_nvme_io", group->stat.completed_nvme_io);
    1905                 :            : 
    1906                 :         44 :         spdk_json_write_named_array_begin(w, "transports");
    1907                 :            : 
    1908         [ +  + ]:         76 :         TAILQ_FOREACH(tgroup, &group->tgroups, link) {
    1909                 :         32 :                 spdk_json_write_object_begin(w);
    1910                 :            :                 /*
    1911                 :            :                  * The trtype field intentionally contains a transport name as this is more informative.
    1912                 :            :                  * The field has not been renamed for backward compatibility.
    1913                 :            :                  */
    1914                 :         32 :                 spdk_json_write_named_string(w, "trtype", spdk_nvmf_get_transport_name(tgroup->transport));
    1915                 :            : 
    1916         [ +  + ]:         32 :                 if (tgroup->transport->ops->poll_group_dump_stat) {
    1917                 :          8 :                         tgroup->transport->ops->poll_group_dump_stat(tgroup, w);
    1918                 :            :                 }
    1919                 :            : 
    1920                 :         32 :                 spdk_json_write_object_end(w);
    1921                 :            :         }
    1922                 :            : 
    1923                 :         44 :         spdk_json_write_array_end(w);
    1924                 :         44 :         spdk_json_write_object_end(w);
    1925                 :         44 : }

Generated by: LCOV version 1.14