LCOV - code coverage report
Current view: top level - spdk/lib/nvmf - nvmf.c (source / functions) Hit Total Coverage
Test: Combined Lines: 731 969 75.4 %
Date: 2024-07-14 16:30:58 Functions: 56 67 83.6 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 337 562 60.0 %

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

Generated by: LCOV version 1.14