LCOV - code coverage report
Current view: top level - spdk/lib/blob - blobstore.c (source / functions) Hit Total Coverage
Test: Combined Lines: 4137 5110 81.0 %
Date: 2024-07-14 14:30:59 Functions: 337 358 94.1 %
Legend: Lines: hit not hit | Branches: + taken - not taken # not executed Branches: 1941 3136 61.9 %

           Branch data     Line data    Source code
       1                 :            : /*   SPDX-License-Identifier: BSD-3-Clause
       2                 :            :  *   Copyright (C) 2017 Intel Corporation.
       3                 :            :  *   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/blob.h"
      10                 :            : #include "spdk/crc32.h"
      11                 :            : #include "spdk/env.h"
      12                 :            : #include "spdk/queue.h"
      13                 :            : #include "spdk/thread.h"
      14                 :            : #include "spdk/bit_array.h"
      15                 :            : #include "spdk/bit_pool.h"
      16                 :            : #include "spdk/likely.h"
      17                 :            : #include "spdk/util.h"
      18                 :            : #include "spdk/string.h"
      19                 :            : 
      20                 :            : #include "spdk_internal/assert.h"
      21                 :            : #include "spdk/log.h"
      22                 :            : 
      23                 :            : #include "blobstore.h"
      24                 :            : 
      25                 :            : #define BLOB_CRC32C_INITIAL    0xffffffffUL
      26                 :            : 
      27                 :            : static int bs_register_md_thread(struct spdk_blob_store *bs);
      28                 :            : static int bs_unregister_md_thread(struct spdk_blob_store *bs);
      29                 :            : static void blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno);
      30                 :            : static void blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
      31                 :            :                 uint64_t cluster, uint32_t extent, struct spdk_blob_md_page *page,
      32                 :            :                 spdk_blob_op_complete cb_fn, void *cb_arg);
      33                 :            : static void blob_free_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
      34                 :            :                 uint32_t extent_page, struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg);
      35                 :            : 
      36                 :            : static int blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
      37                 :            :                           uint16_t value_len, bool internal);
      38                 :            : static int blob_get_xattr_value(struct spdk_blob *blob, const char *name,
      39                 :            :                                 const void **value, size_t *value_len, bool internal);
      40                 :            : static int blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal);
      41                 :            : 
      42                 :            : static void blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num,
      43                 :            :                                    struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg);
      44                 :            : static void blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg);
      45                 :            : 
      46                 :            : static void bs_shallow_copy_cluster_find_next(void *cb_arg);
      47                 :            : 
      48                 :            : /*
      49                 :            :  * External snapshots require a channel per thread per esnap bdev.  The tree
      50                 :            :  * is populated lazily as blob IOs are handled by the back_bs_dev. When this
      51                 :            :  * channel is destroyed, all the channels in the tree are destroyed.
      52                 :            :  */
      53                 :            : 
      54                 :            : struct blob_esnap_channel {
      55                 :            :         RB_ENTRY(blob_esnap_channel)    node;
      56                 :            :         spdk_blob_id                    blob_id;
      57                 :            :         struct spdk_io_channel          *channel;
      58                 :            : };
      59                 :            : 
      60                 :            : static int blob_esnap_channel_compare(struct blob_esnap_channel *c1, struct blob_esnap_channel *c2);
      61                 :            : static void blob_esnap_destroy_bs_dev_channels(struct spdk_blob *blob, bool abort_io,
      62                 :            :                 spdk_blob_op_with_handle_complete cb_fn, void *cb_arg);
      63                 :            : static void blob_esnap_destroy_bs_channel(struct spdk_bs_channel *ch);
      64                 :            : static void blob_set_back_bs_dev_frozen(void *_ctx, int bserrno);
      65   [ +  +  +  +  :      83279 : RB_GENERATE_STATIC(blob_esnap_channel_tree, blob_esnap_channel, node, blob_esnap_channel_compare)
          +  +  +  -  +  
          +  +  -  -  +  
          -  +  -  +  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
          -  -  -  +  #  
          #  #  #  #  #  
          #  #  #  #  #  
                #  #  # ]
      66                 :            : 
      67                 :            : static inline bool
      68                 :   22267396 : blob_is_esnap_clone(const struct spdk_blob *blob)
      69                 :            : {
      70         [ -  + ]:   22267396 :         assert(blob != NULL);
      71                 :   22267396 :         return !!(blob->invalid_flags & SPDK_BLOB_EXTERNAL_SNAPSHOT);
      72                 :            : }
      73                 :            : 
      74                 :            : static int
      75                 :     123365 : blob_id_cmp(struct spdk_blob *blob1, struct spdk_blob *blob2)
      76                 :            : {
      77   [ +  -  +  + ]:     123365 :         assert(blob1 != NULL && blob2 != NULL);
      78         [ +  + ]:     123365 :         return (blob1->id < blob2->id ? -1 : blob1->id > blob2->id);
      79                 :            : }
      80                 :            : 
      81   [ +  +  +  +  :     322496 : RB_GENERATE_STATIC(spdk_blob_tree, spdk_blob, link, blob_id_cmp);
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          +  +  +  +  +  
          -  +  +  +  +  
          +  +  +  +  +  
                +  +  + ]
      82                 :            : 
      83                 :            : static void
      84                 :    3582391 : blob_verify_md_op(struct spdk_blob *blob)
      85                 :            : {
      86         [ -  + ]:    3582391 :         assert(blob != NULL);
      87         [ -  + ]:    3582391 :         assert(spdk_get_thread() == blob->bs->md_thread);
      88         [ -  + ]:    3582391 :         assert(blob->state != SPDK_BLOB_STATE_LOADING);
      89                 :    3582391 : }
      90                 :            : 
      91                 :            : static struct spdk_blob_list *
      92                 :      29420 : bs_get_snapshot_entry(struct spdk_blob_store *bs, spdk_blob_id blobid)
      93                 :            : {
      94                 :      29420 :         struct spdk_blob_list *snapshot_entry = NULL;
      95                 :            : 
      96         [ +  + ]:      35667 :         TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) {
      97         [ +  + ]:      11212 :                 if (snapshot_entry->id == blobid) {
      98                 :       4965 :                         break;
      99                 :            :                 }
     100                 :            :         }
     101                 :            : 
     102                 :      29420 :         return snapshot_entry;
     103                 :            : }
     104                 :            : 
     105                 :            : static void
     106                 :      29591 : bs_claim_md_page(struct spdk_blob_store *bs, uint32_t page)
     107                 :            : {
     108         [ -  + ]:      29591 :         assert(spdk_spin_held(&bs->used_lock));
     109         [ -  + ]:      29591 :         assert(page < spdk_bit_array_capacity(bs->used_md_pages));
     110         [ -  + ]:      29591 :         assert(spdk_bit_array_get(bs->used_md_pages, page) == false);
     111                 :            : 
     112                 :      29591 :         spdk_bit_array_set(bs->used_md_pages, page);
     113                 :      29591 : }
     114                 :            : 
     115                 :            : static void
     116                 :      21947 : bs_release_md_page(struct spdk_blob_store *bs, uint32_t page)
     117                 :            : {
     118         [ -  + ]:      21947 :         assert(spdk_spin_held(&bs->used_lock));
     119         [ -  + ]:      21947 :         assert(page < spdk_bit_array_capacity(bs->used_md_pages));
     120         [ -  + ]:      21947 :         assert(spdk_bit_array_get(bs->used_md_pages, page) == true);
     121                 :            : 
     122                 :      21947 :         spdk_bit_array_clear(bs->used_md_pages, page);
     123                 :      21947 : }
     124                 :            : 
     125                 :            : static uint32_t
     126                 :    2501346 : bs_claim_cluster(struct spdk_blob_store *bs)
     127                 :            : {
     128                 :            :         uint32_t cluster_num;
     129                 :            : 
     130         [ -  + ]:    2501346 :         assert(spdk_spin_held(&bs->used_lock));
     131                 :            : 
     132                 :    2501346 :         cluster_num = spdk_bit_pool_allocate_bit(bs->used_clusters);
     133         [ +  + ]:    2501346 :         if (cluster_num == UINT32_MAX) {
     134                 :          1 :                 return UINT32_MAX;
     135                 :            :         }
     136                 :            : 
     137   [ -  +  -  + ]:    2501345 :         SPDK_DEBUGLOG(blob, "Claiming cluster %u\n", cluster_num);
     138                 :    2501345 :         bs->num_free_clusters--;
     139                 :            : 
     140                 :    2501345 :         return cluster_num;
     141                 :            : }
     142                 :            : 
     143                 :            : static void
     144                 :    2337289 : bs_release_cluster(struct spdk_blob_store *bs, uint32_t cluster_num)
     145                 :            : {
     146         [ -  + ]:    2337289 :         assert(spdk_spin_held(&bs->used_lock));
     147         [ -  + ]:    2337289 :         assert(cluster_num < spdk_bit_pool_capacity(bs->used_clusters));
     148         [ -  + ]:    2337289 :         assert(spdk_bit_pool_is_allocated(bs->used_clusters, cluster_num) == true);
     149         [ -  + ]:    2337289 :         assert(bs->num_free_clusters < bs->total_clusters);
     150                 :            : 
     151   [ -  +  -  + ]:    2337289 :         SPDK_DEBUGLOG(blob, "Releasing cluster %u\n", cluster_num);
     152                 :            : 
     153                 :    2337289 :         spdk_bit_pool_free_bit(bs->used_clusters, cluster_num);
     154                 :    2337289 :         bs->num_free_clusters++;
     155                 :    2337289 : }
     156                 :            : 
     157                 :            : static int
     158                 :    2501345 : blob_insert_cluster(struct spdk_blob *blob, uint32_t cluster_num, uint64_t cluster)
     159                 :            : {
     160                 :    2501345 :         uint64_t *cluster_lba = &blob->active.clusters[cluster_num];
     161                 :            : 
     162                 :    2501345 :         blob_verify_md_op(blob);
     163                 :            : 
     164         [ +  + ]:    2501345 :         if (*cluster_lba != 0) {
     165                 :         37 :                 return -EEXIST;
     166                 :            :         }
     167                 :            : 
     168                 :    2501308 :         *cluster_lba = bs_cluster_to_lba(blob->bs, cluster);
     169                 :    2501308 :         blob->active.num_allocated_clusters++;
     170                 :            : 
     171                 :    2501308 :         return 0;
     172                 :            : }
     173                 :            : 
     174                 :            : static int
     175                 :    2501346 : bs_allocate_cluster(struct spdk_blob *blob, uint32_t cluster_num,
     176                 :            :                     uint64_t *cluster, uint32_t *lowest_free_md_page, bool update_map)
     177                 :            : {
     178                 :    2501346 :         uint32_t *extent_page = 0;
     179                 :            : 
     180         [ -  + ]:    2501346 :         assert(spdk_spin_held(&blob->bs->used_lock));
     181                 :            : 
     182                 :    2501346 :         *cluster = bs_claim_cluster(blob->bs);
     183         [ +  + ]:    2501346 :         if (*cluster == UINT32_MAX) {
     184                 :            :                 /* No more free clusters. Cannot satisfy the request */
     185                 :          1 :                 return -ENOSPC;
     186                 :            :         }
     187                 :            : 
     188   [ +  +  +  + ]:    2501345 :         if (blob->use_extent_table) {
     189                 :    2477033 :                 extent_page = bs_cluster_to_extent_page(blob, cluster_num);
     190         [ +  + ]:    2477033 :                 if (*extent_page == 0) {
     191                 :            :                         /* Extent page shall never occupy md_page so start the search from 1 */
     192         [ +  + ]:      12558 :                         if (*lowest_free_md_page == 0) {
     193                 :       8408 :                                 *lowest_free_md_page = 1;
     194                 :            :                         }
     195                 :            :                         /* No extent_page is allocated for the cluster */
     196                 :      12558 :                         *lowest_free_md_page = spdk_bit_array_find_first_clear(blob->bs->used_md_pages,
     197                 :            :                                                *lowest_free_md_page);
     198         [ -  + ]:      12558 :                         if (*lowest_free_md_page == UINT32_MAX) {
     199                 :            :                                 /* No more free md pages. Cannot satisfy the request */
     200                 :          0 :                                 bs_release_cluster(blob->bs, *cluster);
     201                 :          0 :                                 return -ENOSPC;
     202                 :            :                         }
     203                 :      12558 :                         bs_claim_md_page(blob->bs, *lowest_free_md_page);
     204                 :            :                 }
     205                 :            :         }
     206                 :            : 
     207   [ -  +  -  + ]:    2501345 :         SPDK_DEBUGLOG(blob, "Claiming cluster %" PRIu64 " for blob 0x%" PRIx64 "\n", *cluster,
     208                 :            :                       blob->id);
     209                 :            : 
     210         [ +  + ]:    2501345 :         if (update_map) {
     211                 :    2461574 :                 blob_insert_cluster(blob, cluster_num, *cluster);
     212   [ +  +  +  +  :    2461574 :                 if (blob->use_extent_table && *extent_page == 0) {
                   +  + ]
     213                 :      11088 :                         *extent_page = *lowest_free_md_page;
     214                 :            :                 }
     215                 :            :         }
     216                 :            : 
     217                 :    2501345 :         return 0;
     218                 :            : }
     219                 :            : 
     220                 :            : static void
     221                 :      40725 : blob_xattrs_init(struct spdk_blob_xattr_opts *xattrs)
     222                 :            : {
     223                 :      40725 :         xattrs->count = 0;
     224                 :      40725 :         xattrs->names = NULL;
     225                 :      40725 :         xattrs->ctx = NULL;
     226                 :      40725 :         xattrs->get_value = NULL;
     227                 :      40725 : }
     228                 :            : 
     229                 :            : void
     230                 :      25999 : spdk_blob_opts_init(struct spdk_blob_opts *opts, size_t opts_size)
     231                 :            : {
     232         [ -  + ]:      25999 :         if (!opts) {
     233                 :          0 :                 SPDK_ERRLOG("opts should not be NULL\n");
     234                 :          0 :                 return;
     235                 :            :         }
     236                 :            : 
     237         [ -  + ]:      25999 :         if (!opts_size) {
     238                 :          0 :                 SPDK_ERRLOG("opts_size should not be zero value\n");
     239                 :          0 :                 return;
     240                 :            :         }
     241                 :            : 
     242         [ -  + ]:      25999 :         memset(opts, 0, opts_size);
     243                 :      25999 :         opts->opts_size = opts_size;
     244                 :            : 
     245                 :            : #define FIELD_OK(field) \
     246                 :            :         offsetof(struct spdk_blob_opts, field) + sizeof(opts->field) <= opts_size
     247                 :            : 
     248                 :            : #define SET_FIELD(field, value) \
     249                 :            :         if (FIELD_OK(field)) { \
     250                 :            :                 opts->field = value; \
     251                 :            :         } \
     252                 :            : 
     253         [ +  - ]:      25999 :         SET_FIELD(num_clusters, 0);
     254         [ +  - ]:      25999 :         SET_FIELD(thin_provision, false);
     255         [ +  - ]:      25999 :         SET_FIELD(clear_method, BLOB_CLEAR_WITH_DEFAULT);
     256                 :            : 
     257         [ +  - ]:      25999 :         if (FIELD_OK(xattrs)) {
     258                 :      25999 :                 blob_xattrs_init(&opts->xattrs);
     259                 :            :         }
     260                 :            : 
     261         [ +  - ]:      25999 :         SET_FIELD(use_extent_table, true);
     262                 :            : 
     263                 :            : #undef FIELD_OK
     264                 :            : #undef SET_FIELD
     265                 :            : }
     266                 :            : 
     267                 :            : void
     268                 :      42711 : spdk_blob_open_opts_init(struct spdk_blob_open_opts *opts, size_t opts_size)
     269                 :            : {
     270         [ -  + ]:      42711 :         if (!opts) {
     271                 :          0 :                 SPDK_ERRLOG("opts should not be NULL\n");
     272                 :          0 :                 return;
     273                 :            :         }
     274                 :            : 
     275         [ -  + ]:      42711 :         if (!opts_size) {
     276                 :          0 :                 SPDK_ERRLOG("opts_size should not be zero value\n");
     277                 :          0 :                 return;
     278                 :            :         }
     279                 :            : 
     280         [ -  + ]:      42711 :         memset(opts, 0, opts_size);
     281                 :      42711 :         opts->opts_size = opts_size;
     282                 :            : 
     283                 :            : #define FIELD_OK(field) \
     284                 :            :         offsetof(struct spdk_blob_open_opts, field) + sizeof(opts->field) <= opts_size
     285                 :            : 
     286                 :            : #define SET_FIELD(field, value) \
     287                 :            :         if (FIELD_OK(field)) { \
     288                 :            :                 opts->field = value; \
     289                 :            :         } \
     290                 :            : 
     291         [ +  - ]:      42711 :         SET_FIELD(clear_method, BLOB_CLEAR_WITH_DEFAULT);
     292                 :            : 
     293                 :            : #undef FIELD_OK
     294                 :            : #undef SET_FILED
     295                 :            : }
     296                 :            : 
     297                 :            : static struct spdk_blob *
     298                 :      56785 : blob_alloc(struct spdk_blob_store *bs, spdk_blob_id id)
     299                 :            : {
     300                 :            :         struct spdk_blob *blob;
     301                 :            : 
     302                 :      56785 :         blob = calloc(1, sizeof(*blob));
     303         [ -  + ]:      56785 :         if (!blob) {
     304                 :          0 :                 return NULL;
     305                 :            :         }
     306                 :            : 
     307                 :      56785 :         blob->id = id;
     308                 :      56785 :         blob->bs = bs;
     309                 :            : 
     310                 :      56785 :         blob->parent_id = SPDK_BLOBID_INVALID;
     311                 :            : 
     312                 :      56785 :         blob->state = SPDK_BLOB_STATE_DIRTY;
     313                 :      56785 :         blob->extent_rle_found = false;
     314                 :      56785 :         blob->extent_table_found = false;
     315                 :      56785 :         blob->active.num_pages = 1;
     316                 :      56785 :         blob->active.pages = calloc(1, sizeof(*blob->active.pages));
     317         [ -  + ]:      56785 :         if (!blob->active.pages) {
     318                 :          0 :                 free(blob);
     319                 :          0 :                 return NULL;
     320                 :            :         }
     321                 :            : 
     322                 :      56785 :         blob->active.pages[0] = bs_blobid_to_page(id);
     323                 :            : 
     324                 :      56785 :         TAILQ_INIT(&blob->xattrs);
     325                 :      56785 :         TAILQ_INIT(&blob->xattrs_internal);
     326                 :      56785 :         TAILQ_INIT(&blob->pending_persists);
     327                 :      56785 :         TAILQ_INIT(&blob->persists_to_complete);
     328                 :            : 
     329                 :      56785 :         return blob;
     330                 :            : }
     331                 :            : 
     332                 :            : static void
     333                 :     113570 : xattrs_free(struct spdk_xattr_tailq *xattrs)
     334                 :            : {
     335                 :            :         struct spdk_xattr       *xattr, *xattr_tmp;
     336                 :            : 
     337         [ +  + ]:     167978 :         TAILQ_FOREACH_SAFE(xattr, xattrs, link, xattr_tmp) {
     338         [ +  + ]:      54408 :                 TAILQ_REMOVE(xattrs, xattr, link);
     339                 :      54408 :                 free(xattr->name);
     340                 :      54408 :                 free(xattr->value);
     341                 :      54408 :                 free(xattr);
     342                 :            :         }
     343                 :     113570 : }
     344                 :            : 
     345                 :            : static void
     346                 :      56785 : blob_free(struct spdk_blob *blob)
     347                 :            : {
     348         [ -  + ]:      56785 :         assert(blob != NULL);
     349         [ -  + ]:      56785 :         assert(TAILQ_EMPTY(&blob->pending_persists));
     350         [ -  + ]:      56785 :         assert(TAILQ_EMPTY(&blob->persists_to_complete));
     351                 :            : 
     352                 :      56785 :         free(blob->active.extent_pages);
     353                 :      56785 :         free(blob->clean.extent_pages);
     354                 :      56785 :         free(blob->active.clusters);
     355                 :      56785 :         free(blob->clean.clusters);
     356                 :      56785 :         free(blob->active.pages);
     357                 :      56785 :         free(blob->clean.pages);
     358                 :            : 
     359                 :      56785 :         xattrs_free(&blob->xattrs);
     360                 :      56785 :         xattrs_free(&blob->xattrs_internal);
     361                 :            : 
     362         [ +  + ]:      56785 :         if (blob->back_bs_dev) {
     363                 :       6894 :                 blob->back_bs_dev->destroy(blob->back_bs_dev);
     364                 :            :         }
     365                 :            : 
     366                 :      56785 :         free(blob);
     367                 :      56785 : }
     368                 :            : 
     369                 :            : static void
     370                 :       1972 : blob_back_bs_destroy_esnap_done(void *ctx, struct spdk_blob *blob, int bserrno)
     371                 :            : {
     372                 :       1972 :         struct spdk_bs_dev      *bs_dev = ctx;
     373                 :            : 
     374         [ -  + ]:       1972 :         if (bserrno != 0) {
     375                 :            :                 /*
     376                 :            :                  * This is probably due to a memory allocation failure when creating the
     377                 :            :                  * blob_esnap_destroy_ctx before iterating threads.
     378                 :            :                  */
     379                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": Unable to destroy bs dev channels: error %d\n",
     380                 :            :                             blob->id, bserrno);
     381                 :          0 :                 assert(false);
     382                 :            :         }
     383                 :            : 
     384         [ -  + ]:       1972 :         if (bs_dev == NULL) {
     385                 :            :                 /*
     386                 :            :                  * This check exists to make scanbuild happy.
     387                 :            :                  *
     388                 :            :                  * blob->back_bs_dev for an esnap is NULL during the first iteration of blobs while
     389                 :            :                  * the blobstore is being loaded. It could also be NULL if there was an error
     390                 :            :                  * opening the esnap device. In each of these cases, no channels could have been
     391                 :            :                  * created because back_bs_dev->create_channel() would have led to a NULL pointer
     392                 :            :                  * deref.
     393                 :            :                  */
     394                 :          0 :                 assert(false);
     395                 :            :                 return;
     396                 :            :         }
     397                 :            : 
     398   [ -  +  -  + ]:       1972 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": calling destroy on back_bs_dev\n", blob->id);
     399                 :       1972 :         bs_dev->destroy(bs_dev);
     400                 :            : }
     401                 :            : 
     402                 :            : static void
     403                 :       1972 : blob_back_bs_destroy(struct spdk_blob *blob)
     404                 :            : {
     405   [ -  +  -  + ]:       1972 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": preparing to destroy back_bs_dev\n",
     406                 :            :                       blob->id);
     407                 :            : 
     408                 :       1972 :         blob_esnap_destroy_bs_dev_channels(blob, false, blob_back_bs_destroy_esnap_done,
     409                 :       1972 :                                            blob->back_bs_dev);
     410                 :       1972 :         blob->back_bs_dev = NULL;
     411                 :       1972 : }
     412                 :            : 
     413                 :            : struct blob_parent {
     414                 :            :         union {
     415                 :            :                 struct {
     416                 :            :                         spdk_blob_id id;
     417                 :            :                         struct spdk_blob *blob;
     418                 :            :                 } snapshot;
     419                 :            : 
     420                 :            :                 struct {
     421                 :            :                         void *id;
     422                 :            :                         uint32_t id_len;
     423                 :            :                         struct spdk_bs_dev *back_bs_dev;
     424                 :            :                 } esnap;
     425                 :            :         } u;
     426                 :            : };
     427                 :            : 
     428                 :            : typedef int (*set_parent_refs_cb)(struct spdk_blob *blob, struct blob_parent *parent);
     429                 :            : 
     430                 :            : struct set_bs_dev_ctx {
     431                 :            :         struct spdk_blob        *blob;
     432                 :            :         struct spdk_bs_dev      *back_bs_dev;
     433                 :            : 
     434                 :            :         /*
     435                 :            :          * This callback is used during a set parent operation to change the references
     436                 :            :          * to the parent of the blob.
     437                 :            :          */
     438                 :            :         set_parent_refs_cb      parent_refs_cb_fn;
     439                 :            :         struct blob_parent      *parent_refs_cb_arg;
     440                 :            : 
     441                 :            :         spdk_blob_op_complete   cb_fn;
     442                 :            :         void                    *cb_arg;
     443                 :            :         int                     bserrno;
     444                 :            : };
     445                 :            : 
     446                 :            : static void
     447                 :        177 : blob_set_back_bs_dev(struct spdk_blob *blob, struct spdk_bs_dev *back_bs_dev,
     448                 :            :                      set_parent_refs_cb parent_refs_cb_fn, struct blob_parent *parent_refs_cb_arg,
     449                 :            :                      spdk_blob_op_complete cb_fn, void *cb_arg)
     450                 :            : {
     451                 :            :         struct set_bs_dev_ctx   *ctx;
     452                 :            : 
     453                 :        177 :         ctx = calloc(1, sizeof(*ctx));
     454         [ -  + ]:        177 :         if (ctx == NULL) {
     455                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": out of memory while setting back_bs_dev\n",
     456                 :            :                             blob->id);
     457                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
     458                 :          0 :                 return;
     459                 :            :         }
     460                 :            : 
     461                 :        177 :         ctx->parent_refs_cb_fn = parent_refs_cb_fn;
     462                 :        177 :         ctx->parent_refs_cb_arg = parent_refs_cb_arg;
     463                 :        177 :         ctx->cb_fn = cb_fn;
     464                 :        177 :         ctx->cb_arg = cb_arg;
     465                 :        177 :         ctx->back_bs_dev = back_bs_dev;
     466                 :        177 :         ctx->blob = blob;
     467                 :            : 
     468                 :        177 :         blob_freeze_io(blob, blob_set_back_bs_dev_frozen, ctx);
     469                 :            : }
     470                 :            : 
     471                 :            : struct freeze_io_ctx {
     472                 :            :         struct spdk_bs_cpl cpl;
     473                 :            :         struct spdk_blob *blob;
     474                 :            : };
     475                 :            : 
     476                 :            : static void
     477                 :     118418 : blob_io_sync(struct spdk_io_channel_iter *i)
     478                 :            : {
     479                 :     118418 :         spdk_for_each_channel_continue(i, 0);
     480                 :     118418 : }
     481                 :            : 
     482                 :            : static void
     483                 :     118346 : blob_execute_queued_io(struct spdk_io_channel_iter *i)
     484                 :            : {
     485                 :     118346 :         struct spdk_io_channel *_ch = spdk_io_channel_iter_get_channel(i);
     486                 :     118346 :         struct spdk_bs_channel *ch = spdk_io_channel_get_ctx(_ch);
     487                 :     118346 :         struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     488                 :            :         struct spdk_bs_request_set      *set;
     489                 :            :         struct spdk_bs_user_op_args     *args;
     490                 :            :         spdk_bs_user_op_t *op, *tmp;
     491                 :            : 
     492         [ +  + ]:     120070 :         TAILQ_FOREACH_SAFE(op, &ch->queued_io, link, tmp) {
     493                 :       1724 :                 set = (struct spdk_bs_request_set *)op;
     494                 :       1724 :                 args = &set->u.user_op;
     495                 :            : 
     496         [ +  + ]:       1724 :                 if (args->blob == ctx->blob) {
     497         [ +  + ]:       1717 :                         TAILQ_REMOVE(&ch->queued_io, op, link);
     498                 :       1717 :                         bs_user_op_execute(op);
     499                 :            :                 }
     500                 :            :         }
     501                 :            : 
     502                 :     118346 :         spdk_for_each_channel_continue(i, 0);
     503                 :     118346 : }
     504                 :            : 
     505                 :            : static void
     506                 :     236524 : blob_io_cpl(struct spdk_io_channel_iter *i, int status)
     507                 :            : {
     508                 :     236524 :         struct freeze_io_ctx *ctx = spdk_io_channel_iter_get_ctx(i);
     509                 :            : 
     510                 :     236524 :         ctx->cpl.u.blob_basic.cb_fn(ctx->cpl.u.blob_basic.cb_arg, 0);
     511                 :            : 
     512                 :     236524 :         free(ctx);
     513                 :     236524 : }
     514                 :            : 
     515                 :            : static void
     516                 :     118298 : blob_freeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
     517                 :            : {
     518                 :            :         struct freeze_io_ctx *ctx;
     519                 :            : 
     520                 :     118298 :         blob_verify_md_op(blob);
     521                 :            : 
     522                 :     118298 :         ctx = calloc(1, sizeof(*ctx));
     523         [ -  + ]:     118298 :         if (!ctx) {
     524                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
     525                 :          0 :                 return;
     526                 :            :         }
     527                 :            : 
     528                 :     118298 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
     529                 :     118298 :         ctx->cpl.u.blob_basic.cb_fn = cb_fn;
     530                 :     118298 :         ctx->cpl.u.blob_basic.cb_arg = cb_arg;
     531                 :     118298 :         ctx->blob = blob;
     532                 :            : 
     533                 :            :         /* Freeze I/O on blob */
     534                 :     118298 :         blob->frozen_refcnt++;
     535                 :            : 
     536                 :     118298 :         spdk_for_each_channel(blob->bs, blob_io_sync, ctx, blob_io_cpl);
     537                 :            : }
     538                 :            : 
     539                 :            : static void
     540                 :     118226 : blob_unfreeze_io(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
     541                 :            : {
     542                 :            :         struct freeze_io_ctx *ctx;
     543                 :            : 
     544                 :     118226 :         blob_verify_md_op(blob);
     545                 :            : 
     546                 :     118226 :         ctx = calloc(1, sizeof(*ctx));
     547         [ -  + ]:     118226 :         if (!ctx) {
     548                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
     549                 :          0 :                 return;
     550                 :            :         }
     551                 :            : 
     552                 :     118226 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
     553                 :     118226 :         ctx->cpl.u.blob_basic.cb_fn = cb_fn;
     554                 :     118226 :         ctx->cpl.u.blob_basic.cb_arg = cb_arg;
     555                 :     118226 :         ctx->blob = blob;
     556                 :            : 
     557         [ -  + ]:     118226 :         assert(blob->frozen_refcnt > 0);
     558                 :            : 
     559                 :     118226 :         blob->frozen_refcnt--;
     560                 :            : 
     561                 :     118226 :         spdk_for_each_channel(blob->bs, blob_execute_queued_io, ctx, blob_io_cpl);
     562                 :            : }
     563                 :            : 
     564                 :            : static int
     565                 :     232501 : blob_mark_clean(struct spdk_blob *blob)
     566                 :            : {
     567                 :     232501 :         uint32_t *extent_pages = NULL;
     568                 :     232501 :         uint64_t *clusters = NULL;
     569                 :     232501 :         uint32_t *pages = NULL;
     570                 :            : 
     571         [ -  + ]:     232501 :         assert(blob != NULL);
     572                 :            : 
     573         [ +  + ]:     232501 :         if (blob->active.num_extent_pages) {
     574         [ -  + ]:     191039 :                 assert(blob->active.extent_pages);
     575                 :     191039 :                 extent_pages = calloc(blob->active.num_extent_pages, sizeof(*blob->active.extent_pages));
     576         [ -  + ]:     191039 :                 if (!extent_pages) {
     577                 :          0 :                         return -ENOMEM;
     578                 :            :                 }
     579   [ -  +  -  + ]:     191039 :                 memcpy(extent_pages, blob->active.extent_pages,
     580                 :     191039 :                        blob->active.num_extent_pages * sizeof(*extent_pages));
     581                 :            :         }
     582                 :            : 
     583         [ +  + ]:     232501 :         if (blob->active.num_clusters) {
     584         [ -  + ]:     209477 :                 assert(blob->active.clusters);
     585                 :     209477 :                 clusters = calloc(blob->active.num_clusters, sizeof(*blob->active.clusters));
     586         [ -  + ]:     209477 :                 if (!clusters) {
     587                 :          0 :                         free(extent_pages);
     588                 :          0 :                         return -ENOMEM;
     589                 :            :                 }
     590   [ -  +  -  + ]:     209477 :                 memcpy(clusters, blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters));
     591                 :            :         }
     592                 :            : 
     593         [ +  + ]:     232501 :         if (blob->active.num_pages) {
     594         [ -  + ]:     221955 :                 assert(blob->active.pages);
     595                 :     221955 :                 pages = calloc(blob->active.num_pages, sizeof(*blob->active.pages));
     596         [ -  + ]:     221955 :                 if (!pages) {
     597                 :          0 :                         free(extent_pages);
     598                 :          0 :                         free(clusters);
     599                 :          0 :                         return -ENOMEM;
     600                 :            :                 }
     601   [ -  +  -  + ]:     221955 :                 memcpy(pages, blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages));
     602                 :            :         }
     603                 :            : 
     604                 :     232501 :         free(blob->clean.extent_pages);
     605                 :     232501 :         free(blob->clean.clusters);
     606                 :     232501 :         free(blob->clean.pages);
     607                 :            : 
     608                 :     232501 :         blob->clean.num_extent_pages = blob->active.num_extent_pages;
     609                 :     232501 :         blob->clean.extent_pages = blob->active.extent_pages;
     610                 :     232501 :         blob->clean.num_clusters = blob->active.num_clusters;
     611                 :     232501 :         blob->clean.clusters = blob->active.clusters;
     612                 :     232501 :         blob->clean.num_allocated_clusters = blob->active.num_allocated_clusters;
     613                 :     232501 :         blob->clean.num_pages = blob->active.num_pages;
     614                 :     232501 :         blob->clean.pages = blob->active.pages;
     615                 :            : 
     616                 :     232501 :         blob->active.extent_pages = extent_pages;
     617                 :     232501 :         blob->active.clusters = clusters;
     618                 :     232501 :         blob->active.pages = pages;
     619                 :            : 
     620                 :            :         /* If the metadata was dirtied again while the metadata was being written to disk,
     621                 :            :          *  we do not want to revert the DIRTY state back to CLEAN here.
     622                 :            :          */
     623         [ +  + ]:     232501 :         if (blob->state == SPDK_BLOB_STATE_LOADING) {
     624                 :      41675 :                 blob->state = SPDK_BLOB_STATE_CLEAN;
     625                 :            :         }
     626                 :            : 
     627                 :     232501 :         return 0;
     628                 :            : }
     629                 :            : 
     630                 :            : static int
     631                 :      44700 : blob_deserialize_xattr(struct spdk_blob *blob,
     632                 :            :                        struct spdk_blob_md_descriptor_xattr *desc_xattr, bool internal)
     633                 :            : {
     634                 :            :         struct spdk_xattr                       *xattr;
     635                 :            : 
     636                 :      44700 :         if (desc_xattr->length != sizeof(desc_xattr->name_length) +
     637                 :            :             sizeof(desc_xattr->value_length) +
     638         [ -  + ]:      44700 :             desc_xattr->name_length + desc_xattr->value_length) {
     639                 :          0 :                 return -EINVAL;
     640                 :            :         }
     641                 :            : 
     642                 :      44700 :         xattr = calloc(1, sizeof(*xattr));
     643         [ -  + ]:      44700 :         if (xattr == NULL) {
     644                 :          0 :                 return -ENOMEM;
     645                 :            :         }
     646                 :            : 
     647                 :      44700 :         xattr->name = malloc(desc_xattr->name_length + 1);
     648         [ -  + ]:      44700 :         if (xattr->name == NULL) {
     649                 :          0 :                 free(xattr);
     650                 :          0 :                 return -ENOMEM;
     651                 :            :         }
     652                 :            : 
     653                 :      44700 :         xattr->value = malloc(desc_xattr->value_length);
     654         [ -  + ]:      44700 :         if (xattr->value == NULL) {
     655                 :          0 :                 free(xattr->name);
     656                 :          0 :                 free(xattr);
     657                 :          0 :                 return -ENOMEM;
     658                 :            :         }
     659                 :            : 
     660   [ -  +  -  + ]:      44700 :         memcpy(xattr->name, desc_xattr->name, desc_xattr->name_length);
     661                 :      44700 :         xattr->name[desc_xattr->name_length] = '\0';
     662                 :      44700 :         xattr->value_len = desc_xattr->value_length;
     663   [ -  +  -  + ]:      44700 :         memcpy(xattr->value,
     664                 :      44700 :                (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
     665                 :      44700 :                desc_xattr->value_length);
     666                 :            : 
     667   [ +  +  +  +  :      44700 :         TAILQ_INSERT_TAIL(internal ? &blob->xattrs_internal : &blob->xattrs, xattr, link);
                   +  + ]
     668                 :            : 
     669                 :      44700 :         return 0;
     670                 :            : }
     671                 :            : 
     672                 :            : 
     673                 :            : static int
     674                 :      75786 : blob_parse_page(const struct spdk_blob_md_page *page, struct spdk_blob *blob)
     675                 :            : {
     676                 :            :         struct spdk_blob_md_descriptor *desc;
     677                 :      75786 :         size_t  cur_desc = 0;
     678                 :            :         void *tmp;
     679                 :            : 
     680                 :      75786 :         desc = (struct spdk_blob_md_descriptor *)page->descriptors;
     681         [ +  - ]:     235583 :         while (cur_desc < sizeof(page->descriptors)) {
     682         [ +  + ]:     235583 :                 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
     683         [ +  - ]:      75498 :                         if (desc->length == 0) {
     684                 :            :                                 /* If padding and length are 0, this terminates the page */
     685                 :      75498 :                                 break;
     686                 :            :                         }
     687         [ +  + ]:     160085 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
     688                 :            :                         struct spdk_blob_md_descriptor_flags    *desc_flags;
     689                 :            : 
     690                 :      41867 :                         desc_flags = (struct spdk_blob_md_descriptor_flags *)desc;
     691                 :            : 
     692         [ -  + ]:      41867 :                         if (desc_flags->length != sizeof(*desc_flags) - sizeof(*desc)) {
     693                 :          0 :                                 return -EINVAL;
     694                 :            :                         }
     695                 :            : 
     696         [ +  + ]:      41867 :                         if ((desc_flags->invalid_flags | SPDK_BLOB_INVALID_FLAGS_MASK) !=
     697                 :            :                             SPDK_BLOB_INVALID_FLAGS_MASK) {
     698                 :         48 :                                 return -EINVAL;
     699                 :            :                         }
     700                 :            : 
     701         [ +  + ]:      41819 :                         if ((desc_flags->data_ro_flags | SPDK_BLOB_DATA_RO_FLAGS_MASK) !=
     702                 :            :                             SPDK_BLOB_DATA_RO_FLAGS_MASK) {
     703                 :         72 :                                 blob->data_ro = true;
     704                 :         72 :                                 blob->md_ro = true;
     705                 :            :                         }
     706                 :            : 
     707         [ +  + ]:      41819 :                         if ((desc_flags->md_ro_flags | SPDK_BLOB_MD_RO_FLAGS_MASK) !=
     708                 :            :                             SPDK_BLOB_MD_RO_FLAGS_MASK) {
     709                 :         72 :                                 blob->md_ro = true;
     710                 :            :                         }
     711                 :            : 
     712         [ +  + ]:      41819 :                         if ((desc_flags->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
     713                 :       3495 :                                 blob->data_ro = true;
     714                 :       3495 :                                 blob->md_ro = true;
     715                 :            :                         }
     716                 :            : 
     717                 :      41819 :                         blob->invalid_flags = desc_flags->invalid_flags;
     718                 :      41819 :                         blob->data_ro_flags = desc_flags->data_ro_flags;
     719                 :      41819 :                         blob->md_ro_flags = desc_flags->md_ro_flags;
     720                 :            : 
     721         [ +  + ]:     118218 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
     722                 :            :                         struct spdk_blob_md_descriptor_extent_rle       *desc_extent_rle;
     723                 :            :                         unsigned int                            i, j;
     724                 :       8352 :                         unsigned int                            cluster_count = blob->active.num_clusters;
     725                 :            : 
     726   [ -  +  -  + ]:       8352 :                         if (blob->extent_table_found) {
     727                 :            :                                 /* Extent Table already present in the md,
     728                 :            :                                  * both descriptors should never be at the same time. */
     729                 :          0 :                                 return -EINVAL;
     730                 :            :                         }
     731                 :       8352 :                         blob->extent_rle_found = true;
     732                 :            : 
     733                 :       8352 :                         desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
     734                 :            : 
     735         [ +  - ]:       8352 :                         if (desc_extent_rle->length == 0 ||
     736         [ -  + ]:       8352 :                             (desc_extent_rle->length % sizeof(desc_extent_rle->extents[0]) != 0)) {
     737                 :          0 :                                 return -EINVAL;
     738                 :            :                         }
     739                 :            : 
     740         [ +  + ]:      17772 :                         for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
     741         [ +  + ]:     127428 :                                 for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
     742         [ +  + ]:     118008 :                                         if (desc_extent_rle->extents[i].cluster_idx != 0) {
     743         [ -  + ]:      40152 :                                                 if (!spdk_bit_pool_is_allocated(blob->bs->used_clusters,
     744                 :      40152 :                                                                                 desc_extent_rle->extents[i].cluster_idx + j)) {
     745                 :          0 :                                                         return -EINVAL;
     746                 :            :                                                 }
     747                 :            :                                         }
     748                 :     118008 :                                         cluster_count++;
     749                 :            :                                 }
     750                 :            :                         }
     751                 :            : 
     752         [ -  + ]:       8352 :                         if (cluster_count == 0) {
     753                 :          0 :                                 return -EINVAL;
     754                 :            :                         }
     755                 :       8352 :                         tmp = realloc(blob->active.clusters, cluster_count * sizeof(*blob->active.clusters));
     756         [ -  + ]:       8352 :                         if (tmp == NULL) {
     757                 :          0 :                                 return -ENOMEM;
     758                 :            :                         }
     759                 :       8352 :                         blob->active.clusters = tmp;
     760                 :       8352 :                         blob->active.cluster_array_size = cluster_count;
     761                 :            : 
     762         [ +  + ]:      17772 :                         for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
     763         [ +  + ]:     127428 :                                 for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
     764         [ +  + ]:     118008 :                                         if (desc_extent_rle->extents[i].cluster_idx != 0) {
     765                 :      53536 :                                                 blob->active.clusters[blob->active.num_clusters++] = bs_cluster_to_lba(blob->bs,
     766                 :      40152 :                                                                 desc_extent_rle->extents[i].cluster_idx + j);
     767                 :      40152 :                                                 blob->active.num_allocated_clusters++;
     768         [ +  - ]:      77856 :                                         } else if (spdk_blob_is_thin_provisioned(blob)) {
     769                 :      77856 :                                                 blob->active.clusters[blob->active.num_clusters++] = 0;
     770                 :            :                                         } else {
     771                 :          0 :                                                 return -EINVAL;
     772                 :            :                                         }
     773                 :            :                                 }
     774                 :            :                         }
     775         [ +  + ]:     109866 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
     776                 :            :                         struct spdk_blob_md_descriptor_extent_table *desc_extent_table;
     777                 :      31852 :                         uint32_t num_extent_pages = blob->active.num_extent_pages;
     778                 :            :                         uint32_t i, j;
     779                 :            :                         size_t extent_pages_length;
     780                 :            : 
     781                 :      31852 :                         desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc;
     782                 :      31852 :                         extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters);
     783                 :            : 
     784   [ -  +  -  + ]:      31852 :                         if (blob->extent_rle_found) {
     785                 :            :                                 /* This means that Extent RLE is present in MD,
     786                 :            :                                  * both should never be at the same time. */
     787                 :          0 :                                 return -EINVAL;
     788   [ -  +  +  + ]:      31852 :                         } else if (blob->extent_table_found &&
     789         [ -  + ]:          5 :                                    desc_extent_table->num_clusters != blob->remaining_clusters_in_et) {
     790                 :            :                                 /* Number of clusters in this ET does not match number
     791                 :            :                                  * from previously read EXTENT_TABLE. */
     792                 :          0 :                                 return -EINVAL;
     793                 :            :                         }
     794                 :            : 
     795         [ +  - ]:      31852 :                         if (desc_extent_table->length == 0 ||
     796         [ -  + ]:      31852 :                             (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) {
     797                 :          0 :                                 return -EINVAL;
     798                 :            :                         }
     799                 :            : 
     800                 :      31852 :                         blob->extent_table_found = true;
     801                 :            : 
     802         [ +  + ]:      68144 :                         for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
     803                 :      36292 :                                 num_extent_pages += desc_extent_table->extent_page[i].num_pages;
     804                 :            :                         }
     805                 :            : 
     806         [ +  + ]:      31852 :                         if (num_extent_pages > 0) {
     807                 :      26823 :                                 tmp = realloc(blob->active.extent_pages, num_extent_pages * sizeof(uint32_t));
     808         [ -  + ]:      26823 :                                 if (tmp == NULL) {
     809                 :          0 :                                         return -ENOMEM;
     810                 :            :                                 }
     811                 :      26823 :                                 blob->active.extent_pages = tmp;
     812                 :            :                         }
     813                 :      31852 :                         blob->active.extent_pages_array_size = num_extent_pages;
     814                 :            : 
     815                 :      31852 :                         blob->remaining_clusters_in_et = desc_extent_table->num_clusters;
     816                 :            : 
     817                 :            :                         /* Extent table entries contain md page numbers for extent pages.
     818                 :            :                          * Zeroes represent unallocated extent pages, those are run-length-encoded.
     819                 :            :                          */
     820         [ +  + ]:      68144 :                         for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
     821         [ +  + ]:      36292 :                                 if (desc_extent_table->extent_page[i].page_idx != 0) {
     822         [ -  + ]:      33350 :                                         assert(desc_extent_table->extent_page[i].num_pages == 1);
     823                 :      64563 :                                         blob->active.extent_pages[blob->active.num_extent_pages++] =
     824                 :      33350 :                                                 desc_extent_table->extent_page[i].page_idx;
     825         [ +  - ]:       2942 :                                 } else if (spdk_blob_is_thin_provisioned(blob)) {
     826         [ +  + ]:      10930 :                                         for (j = 0; j < desc_extent_table->extent_page[i].num_pages; j++) {
     827                 :       7988 :                                                 blob->active.extent_pages[blob->active.num_extent_pages++] = 0;
     828                 :            :                                         }
     829                 :            :                                 } else {
     830                 :          0 :                                         return -EINVAL;
     831                 :            :                                 }
     832                 :            :                         }
     833         [ +  + ]:      78014 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
     834                 :            :                         struct spdk_blob_md_descriptor_extent_page      *desc_extent;
     835                 :            :                         unsigned int                                    i;
     836                 :      33314 :                         unsigned int                                    cluster_count = 0;
     837                 :            :                         size_t                                          cluster_idx_length;
     838                 :            : 
     839   [ -  +  -  + ]:      33314 :                         if (blob->extent_rle_found) {
     840                 :            :                                 /* This means that Extent RLE is present in MD,
     841                 :            :                                  * both should never be at the same time. */
     842                 :          0 :                                 return -EINVAL;
     843                 :            :                         }
     844                 :            : 
     845                 :      33314 :                         desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
     846                 :      33314 :                         cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx);
     847                 :            : 
     848         [ +  - ]:      33314 :                         if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) ||
     849         [ -  + ]:      33314 :                             (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) {
     850                 :          0 :                                 return -EINVAL;
     851                 :            :                         }
     852                 :            : 
     853         [ +  + ]:    6292009 :                         for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
     854         [ +  + ]:    6258695 :                                 if (desc_extent->cluster_idx[i] != 0) {
     855         [ -  + ]:    5720844 :                                         if (!spdk_bit_pool_is_allocated(blob->bs->used_clusters, desc_extent->cluster_idx[i])) {
     856                 :          0 :                                                 return -EINVAL;
     857                 :            :                                         }
     858                 :            :                                 }
     859                 :    6258695 :                                 cluster_count++;
     860                 :            :                         }
     861                 :            : 
     862         [ -  + ]:      33314 :                         if (cluster_count == 0) {
     863                 :          0 :                                 return -EINVAL;
     864                 :            :                         }
     865                 :            : 
     866                 :            :                         /* When reading extent pages sequentially starting cluster idx should match
     867                 :            :                          * current size of a blob.
     868                 :            :                          * If changed to batch reading, this check shall be removed. */
     869         [ -  + ]:      33314 :                         if (desc_extent->start_cluster_idx != blob->active.num_clusters) {
     870                 :          0 :                                 return -EINVAL;
     871                 :            :                         }
     872                 :            : 
     873                 :      33314 :                         tmp = realloc(blob->active.clusters,
     874                 :      33314 :                                       (cluster_count + blob->active.num_clusters) * sizeof(*blob->active.clusters));
     875         [ -  + ]:      33314 :                         if (tmp == NULL) {
     876                 :          0 :                                 return -ENOMEM;
     877                 :            :                         }
     878                 :      33314 :                         blob->active.clusters = tmp;
     879                 :      33314 :                         blob->active.cluster_array_size = (cluster_count + blob->active.num_clusters);
     880                 :            : 
     881         [ +  + ]:    6292009 :                         for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
     882         [ +  + ]:    6258695 :                                 if (desc_extent->cluster_idx[i] != 0) {
     883                 :    5720844 :                                         blob->active.clusters[blob->active.num_clusters++] = bs_cluster_to_lba(blob->bs,
     884                 :            :                                                         desc_extent->cluster_idx[i]);
     885                 :    5720844 :                                         blob->active.num_allocated_clusters++;
     886         [ +  - ]:     537851 :                                 } else if (spdk_blob_is_thin_provisioned(blob)) {
     887                 :     537851 :                                         blob->active.clusters[blob->active.num_clusters++] = 0;
     888                 :            :                                 } else {
     889                 :          0 :                                         return -EINVAL;
     890                 :            :                                 }
     891                 :            :                         }
     892         [ -  + ]:      33314 :                         assert(desc_extent->start_cluster_idx + cluster_count == blob->active.num_clusters);
     893         [ -  + ]:      33314 :                         assert(blob->remaining_clusters_in_et >= cluster_count);
     894                 :      33314 :                         blob->remaining_clusters_in_et -= cluster_count;
     895         [ +  + ]:      44700 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
     896                 :            :                         int rc;
     897                 :            : 
     898                 :      39143 :                         rc = blob_deserialize_xattr(blob,
     899                 :            :                                                     (struct spdk_blob_md_descriptor_xattr *) desc, false);
     900         [ -  + ]:      39143 :                         if (rc != 0) {
     901                 :          0 :                                 return rc;
     902                 :            :                         }
     903         [ +  - ]:       5557 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
     904                 :            :                         int rc;
     905                 :            : 
     906                 :       5557 :                         rc = blob_deserialize_xattr(blob,
     907                 :            :                                                     (struct spdk_blob_md_descriptor_xattr *) desc, true);
     908         [ -  + ]:       5557 :                         if (rc != 0) {
     909                 :          0 :                                 return rc;
     910                 :            :                         }
     911                 :            :                 } else {
     912                 :            :                         /* Unrecognized descriptor type.  Do not fail - just continue to the
     913                 :            :                          *  next descriptor.  If this descriptor is associated with some feature
     914                 :            :                          *  defined in a newer version of blobstore, that version of blobstore
     915                 :            :                          *  should create and set an associated feature flag to specify if this
     916                 :            :                          *  blob can be loaded or not.
     917                 :            :                          */
     918                 :            :                 }
     919                 :            : 
     920                 :            :                 /* Advance to the next descriptor */
     921                 :     160037 :                 cur_desc += sizeof(*desc) + desc->length;
     922         [ +  + ]:     160037 :                 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
     923                 :        240 :                         break;
     924                 :            :                 }
     925                 :     159797 :                 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
     926                 :            :         }
     927                 :            : 
     928                 :      75738 :         return 0;
     929                 :            : }
     930                 :            : 
     931                 :            : static bool bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page);
     932                 :            : 
     933                 :            : static int
     934                 :      33314 : blob_parse_extent_page(struct spdk_blob_md_page *extent_page, struct spdk_blob *blob)
     935                 :            : {
     936         [ -  + ]:      33314 :         assert(blob != NULL);
     937         [ -  + ]:      33314 :         assert(blob->state == SPDK_BLOB_STATE_LOADING);
     938                 :            : 
     939         [ -  + ]:      33314 :         if (bs_load_cur_extent_page_valid(extent_page) == false) {
     940                 :          0 :                 return -ENOENT;
     941                 :            :         }
     942                 :            : 
     943                 :      33314 :         return blob_parse_page(extent_page, blob);
     944                 :            : }
     945                 :            : 
     946                 :            : static int
     947                 :      41891 : blob_parse(const struct spdk_blob_md_page *pages, uint32_t page_count,
     948                 :            :            struct spdk_blob *blob)
     949                 :            : {
     950                 :            :         const struct spdk_blob_md_page *page;
     951                 :            :         uint32_t i;
     952                 :            :         int rc;
     953                 :            :         void *tmp;
     954                 :            : 
     955         [ -  + ]:      41891 :         assert(page_count > 0);
     956         [ -  + ]:      41891 :         assert(pages[0].sequence_num == 0);
     957         [ -  + ]:      41891 :         assert(blob != NULL);
     958         [ -  + ]:      41891 :         assert(blob->state == SPDK_BLOB_STATE_LOADING);
     959         [ -  + ]:      41891 :         assert(blob->active.clusters == NULL);
     960                 :            : 
     961                 :            :         /* The blobid provided doesn't match what's in the MD, this can
     962                 :            :          * happen for example if a bogus blobid is passed in through open.
     963                 :            :          */
     964         [ +  + ]:      41891 :         if (blob->id != pages[0].id) {
     965                 :         24 :                 SPDK_ERRLOG("Blobid (0x%" PRIx64 ") doesn't match what's in metadata "
     966                 :            :                             "(0x%" PRIx64 ")\n", blob->id, pages[0].id);
     967                 :         24 :                 return -ENOENT;
     968                 :            :         }
     969                 :            : 
     970                 :      41867 :         tmp = realloc(blob->active.pages, page_count * sizeof(*blob->active.pages));
     971         [ -  + ]:      41867 :         if (!tmp) {
     972                 :          0 :                 return -ENOMEM;
     973                 :            :         }
     974                 :      41867 :         blob->active.pages = tmp;
     975                 :            : 
     976                 :      41867 :         blob->active.pages[0] = pages[0].id;
     977                 :            : 
     978         [ +  + ]:      42472 :         for (i = 1; i < page_count; i++) {
     979         [ -  + ]:        605 :                 assert(spdk_bit_array_get(blob->bs->used_md_pages, pages[i - 1].next));
     980                 :        605 :                 blob->active.pages[i] = pages[i - 1].next;
     981                 :            :         }
     982                 :      41867 :         blob->active.num_pages = page_count;
     983                 :            : 
     984         [ +  + ]:      84291 :         for (i = 0; i < page_count; i++) {
     985                 :      42472 :                 page = &pages[i];
     986                 :            : 
     987         [ -  + ]:      42472 :                 assert(page->id == blob->id);
     988         [ -  + ]:      42472 :                 assert(page->sequence_num == i);
     989                 :            : 
     990                 :      42472 :                 rc = blob_parse_page(page, blob);
     991         [ +  + ]:      42472 :                 if (rc != 0) {
     992                 :         48 :                         return rc;
     993                 :            :                 }
     994                 :            :         }
     995                 :            : 
     996                 :      41819 :         return 0;
     997                 :            : }
     998                 :            : 
     999                 :            : static int
    1000                 :     305392 : blob_serialize_add_page(const struct spdk_blob *blob,
    1001                 :            :                         struct spdk_blob_md_page **pages,
    1002                 :            :                         uint32_t *page_count,
    1003                 :            :                         struct spdk_blob_md_page **last_page)
    1004                 :            : {
    1005                 :            :         struct spdk_blob_md_page *page, *tmp_pages;
    1006                 :            : 
    1007         [ -  + ]:     305392 :         assert(pages != NULL);
    1008         [ -  + ]:     305392 :         assert(page_count != NULL);
    1009                 :            : 
    1010                 :     305392 :         *last_page = NULL;
    1011         [ +  + ]:     305392 :         if (*page_count == 0) {
    1012         [ -  + ]:     304259 :                 assert(*pages == NULL);
    1013                 :     304259 :                 *pages = spdk_malloc(SPDK_BS_PAGE_SIZE, 0,
    1014                 :            :                                      NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    1015         [ -  + ]:     304259 :                 if (*pages == NULL) {
    1016                 :          0 :                         return -ENOMEM;
    1017                 :            :                 }
    1018                 :     304259 :                 *page_count = 1;
    1019                 :            :         } else {
    1020         [ -  + ]:       1133 :                 assert(*pages != NULL);
    1021                 :       1133 :                 tmp_pages = spdk_realloc(*pages, SPDK_BS_PAGE_SIZE * (*page_count + 1), 0);
    1022         [ -  + ]:       1133 :                 if (tmp_pages == NULL) {
    1023                 :          0 :                         return -ENOMEM;
    1024                 :            :                 }
    1025                 :       1133 :                 (*page_count)++;
    1026                 :       1133 :                 *pages = tmp_pages;
    1027                 :            :         }
    1028                 :            : 
    1029                 :     305392 :         page = &(*pages)[*page_count - 1];
    1030         [ -  + ]:     305392 :         memset(page, 0, sizeof(*page));
    1031                 :     305392 :         page->id = blob->id;
    1032                 :     305392 :         page->sequence_num = *page_count - 1;
    1033                 :     305392 :         page->next = SPDK_INVALID_MD_PAGE;
    1034                 :     305392 :         *last_page = page;
    1035                 :            : 
    1036                 :     305392 :         return 0;
    1037                 :            : }
    1038                 :            : 
    1039                 :            : /* Transform the in-memory representation 'xattr' into an on-disk xattr descriptor.
    1040                 :            :  * Update required_sz on both success and failure.
    1041                 :            :  *
    1042                 :            :  */
    1043                 :            : static int
    1044                 :     323034 : blob_serialize_xattr(const struct spdk_xattr *xattr,
    1045                 :            :                      uint8_t *buf, size_t buf_sz,
    1046                 :            :                      size_t *required_sz, bool internal)
    1047                 :            : {
    1048                 :            :         struct spdk_blob_md_descriptor_xattr    *desc;
    1049                 :            : 
    1050                 :     642454 :         *required_sz = sizeof(struct spdk_blob_md_descriptor_xattr) +
    1051         [ -  + ]:     323034 :                        strlen(xattr->name) +
    1052                 :     323034 :                        xattr->value_len;
    1053                 :            : 
    1054         [ +  + ]:     323034 :         if (buf_sz < *required_sz) {
    1055                 :        288 :                 return -1;
    1056                 :            :         }
    1057                 :            : 
    1058                 :     322746 :         desc = (struct spdk_blob_md_descriptor_xattr *)buf;
    1059                 :            : 
    1060         [ +  + ]:     322746 :         desc->type = internal ? SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL : SPDK_MD_DESCRIPTOR_TYPE_XATTR;
    1061                 :     322746 :         desc->length = sizeof(desc->name_length) +
    1062                 :            :                        sizeof(desc->value_length) +
    1063         [ -  + ]:     322746 :                        strlen(xattr->name) +
    1064                 :     322746 :                        xattr->value_len;
    1065         [ -  + ]:     322746 :         desc->name_length = strlen(xattr->name);
    1066                 :     322746 :         desc->value_length = xattr->value_len;
    1067                 :            : 
    1068   [ -  +  -  + ]:     322746 :         memcpy(desc->name, xattr->name, desc->name_length);
    1069   [ -  +  -  + ]:     322746 :         memcpy((void *)((uintptr_t)desc->name + desc->name_length),
    1070                 :     322746 :                xattr->value,
    1071                 :     322746 :                desc->value_length);
    1072                 :            : 
    1073                 :     322746 :         return 0;
    1074                 :            : }
    1075                 :            : 
    1076                 :            : static void
    1077                 :     169659 : blob_serialize_extent_table_entry(const struct spdk_blob *blob,
    1078                 :            :                                   uint64_t start_ep, uint64_t *next_ep,
    1079                 :            :                                   uint8_t **buf, size_t *remaining_sz)
    1080                 :            : {
    1081                 :            :         struct spdk_blob_md_descriptor_extent_table *desc;
    1082                 :            :         size_t cur_sz;
    1083                 :            :         uint64_t i, et_idx;
    1084                 :            :         uint32_t extent_page, ep_len;
    1085                 :            : 
    1086                 :            :         /* The buffer must have room for at least num_clusters entry */
    1087                 :     169659 :         cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc->num_clusters);
    1088         [ +  + ]:     169659 :         if (*remaining_sz < cur_sz) {
    1089                 :        120 :                 *next_ep = start_ep;
    1090                 :        120 :                 return;
    1091                 :            :         }
    1092                 :            : 
    1093                 :     169539 :         desc = (struct spdk_blob_md_descriptor_extent_table *)*buf;
    1094                 :     169539 :         desc->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE;
    1095                 :            : 
    1096                 :     169539 :         desc->num_clusters = blob->active.num_clusters;
    1097                 :            : 
    1098                 :     169539 :         ep_len = 1;
    1099                 :     169539 :         et_idx = 0;
    1100         [ +  + ]:    1145097 :         for (i = start_ep; i < blob->active.num_extent_pages; i++) {
    1101         [ +  + ]:     976163 :                 if (*remaining_sz < cur_sz  + sizeof(desc->extent_page[0])) {
    1102                 :            :                         /* If we ran out of buffer space, return */
    1103                 :        605 :                         break;
    1104                 :            :                 }
    1105                 :            : 
    1106                 :     975558 :                 extent_page = blob->active.extent_pages[i];
    1107                 :            :                 /* Verify that next extent_page is unallocated */
    1108         [ +  + ]:     975558 :                 if (extent_page == 0 &&
    1109   [ +  +  +  + ]:     410140 :                     (i + 1 < blob->active.num_extent_pages && blob->active.extent_pages[i + 1] == 0)) {
    1110                 :     268385 :                         ep_len++;
    1111                 :     268385 :                         continue;
    1112                 :            :                 }
    1113                 :     707173 :                 desc->extent_page[et_idx].page_idx = extent_page;
    1114                 :     707173 :                 desc->extent_page[et_idx].num_pages = ep_len;
    1115                 :     707173 :                 et_idx++;
    1116                 :            : 
    1117                 :     707173 :                 ep_len = 1;
    1118                 :     707173 :                 cur_sz += sizeof(desc->extent_page[et_idx]);
    1119                 :            :         }
    1120                 :     169539 :         *next_ep = i;
    1121                 :            : 
    1122                 :     169539 :         desc->length = sizeof(desc->num_clusters) + sizeof(desc->extent_page[0]) * et_idx;
    1123                 :     169539 :         *remaining_sz -= sizeof(struct spdk_blob_md_descriptor) + desc->length;
    1124                 :     169539 :         *buf += sizeof(struct spdk_blob_md_descriptor) + desc->length;
    1125                 :            : }
    1126                 :            : 
    1127                 :            : static int
    1128                 :     168946 : blob_serialize_extent_table(const struct spdk_blob *blob,
    1129                 :            :                             struct spdk_blob_md_page **pages,
    1130                 :            :                             struct spdk_blob_md_page *cur_page,
    1131                 :            :                             uint32_t *page_count, uint8_t **buf,
    1132                 :            :                             size_t *remaining_sz)
    1133                 :            : {
    1134                 :     166767 :         uint64_t                                last_extent_page;
    1135                 :            :         int                                     rc;
    1136                 :            : 
    1137                 :     168946 :         last_extent_page = 0;
    1138                 :            :         /* At least single extent table entry has to be always persisted.
    1139                 :            :          * Such case occurs with num_extent_pages == 0. */
    1140         [ +  - ]:     169659 :         while (last_extent_page <= blob->active.num_extent_pages) {
    1141                 :     169659 :                 blob_serialize_extent_table_entry(blob, last_extent_page, &last_extent_page, buf,
    1142                 :            :                                                   remaining_sz);
    1143                 :            : 
    1144         [ +  + ]:     169659 :                 if (last_extent_page == blob->active.num_extent_pages) {
    1145                 :     168946 :                         break;
    1146                 :            :                 }
    1147                 :            : 
    1148                 :        713 :                 rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
    1149         [ -  + ]:        713 :                 if (rc < 0) {
    1150                 :          0 :                         return rc;
    1151                 :            :                 }
    1152                 :            : 
    1153                 :        713 :                 *buf = (uint8_t *)cur_page->descriptors;
    1154                 :        713 :                 *remaining_sz = sizeof(cur_page->descriptors);
    1155                 :            :         }
    1156                 :            : 
    1157                 :     168946 :         return 0;
    1158                 :            : }
    1159                 :            : 
    1160                 :            : static void
    1161                 :      10422 : blob_serialize_extent_rle(const struct spdk_blob *blob,
    1162                 :            :                           uint64_t start_cluster, uint64_t *next_cluster,
    1163                 :            :                           uint8_t **buf, size_t *buf_sz)
    1164                 :            : {
    1165                 :            :         struct spdk_blob_md_descriptor_extent_rle *desc_extent_rle;
    1166                 :            :         size_t cur_sz;
    1167                 :            :         uint64_t i, extent_idx;
    1168                 :            :         uint64_t lba, lba_per_cluster, lba_count;
    1169                 :            : 
    1170                 :            :         /* The buffer must have room for at least one extent */
    1171                 :      10422 :         cur_sz = sizeof(struct spdk_blob_md_descriptor) + sizeof(desc_extent_rle->extents[0]);
    1172         [ +  + ]:      10422 :         if (*buf_sz < cur_sz) {
    1173                 :        108 :                 *next_cluster = start_cluster;
    1174                 :        108 :                 return;
    1175                 :            :         }
    1176                 :            : 
    1177                 :      10314 :         desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)*buf;
    1178                 :      10314 :         desc_extent_rle->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE;
    1179                 :            : 
    1180                 :      10314 :         lba_per_cluster = bs_cluster_to_lba(blob->bs, 1);
    1181                 :            :         /* Assert for scan-build false positive */
    1182         [ -  + ]:      10314 :         assert(lba_per_cluster > 0);
    1183                 :            : 
    1184                 :      10314 :         lba = blob->active.clusters[start_cluster];
    1185                 :      10314 :         lba_count = lba_per_cluster;
    1186                 :      10314 :         extent_idx = 0;
    1187         [ +  + ]:    4862700 :         for (i = start_cluster + 1; i < blob->active.num_clusters; i++) {
    1188   [ +  +  +  + ]:    4852410 :                 if ((lba + lba_count) == blob->active.clusters[i] && lba != 0) {
    1189                 :            :                         /* Run-length encode sequential non-zero LBA */
    1190                 :      43656 :                         lba_count += lba_per_cluster;
    1191                 :      43656 :                         continue;
    1192   [ +  +  +  + ]:    4808754 :                 } else if (lba == 0 && blob->active.clusters[i] == 0) {
    1193                 :            :                         /* Run-length encode unallocated clusters */
    1194                 :    4801596 :                         lba_count += lba_per_cluster;
    1195                 :    4801596 :                         continue;
    1196                 :            :                 }
    1197         [ -  + ]:       7158 :                 desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
    1198         [ -  + ]:       7158 :                 desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster;
    1199                 :       7158 :                 extent_idx++;
    1200                 :            : 
    1201                 :       7158 :                 cur_sz += sizeof(desc_extent_rle->extents[extent_idx]);
    1202                 :            : 
    1203         [ +  + ]:       7158 :                 if (*buf_sz < cur_sz) {
    1204                 :            :                         /* If we ran out of buffer space, return */
    1205                 :         24 :                         *next_cluster = i;
    1206                 :         24 :                         break;
    1207                 :            :                 }
    1208                 :            : 
    1209                 :       7134 :                 lba = blob->active.clusters[i];
    1210                 :       7134 :                 lba_count = lba_per_cluster;
    1211                 :            :         }
    1212                 :            : 
    1213         [ +  + ]:      10314 :         if (*buf_sz >= cur_sz) {
    1214         [ -  + ]:      10290 :                 desc_extent_rle->extents[extent_idx].cluster_idx = lba / lba_per_cluster;
    1215         [ -  + ]:      10290 :                 desc_extent_rle->extents[extent_idx].length = lba_count / lba_per_cluster;
    1216                 :      10290 :                 extent_idx++;
    1217                 :            : 
    1218                 :      10290 :                 *next_cluster = blob->active.num_clusters;
    1219                 :            :         }
    1220                 :            : 
    1221                 :      10314 :         desc_extent_rle->length = sizeof(desc_extent_rle->extents[0]) * extent_idx;
    1222                 :      10314 :         *buf_sz -= sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length;
    1223                 :      10314 :         *buf += sizeof(struct spdk_blob_md_descriptor) + desc_extent_rle->length;
    1224                 :            : }
    1225                 :            : 
    1226                 :            : static int
    1227                 :      11574 : blob_serialize_extents_rle(const struct spdk_blob *blob,
    1228                 :            :                            struct spdk_blob_md_page **pages,
    1229                 :            :                            struct spdk_blob_md_page *cur_page,
    1230                 :            :                            uint32_t *page_count, uint8_t **buf,
    1231                 :            :                            size_t *remaining_sz)
    1232                 :            : {
    1233                 :       9645 :         uint64_t                                last_cluster;
    1234                 :            :         int                                     rc;
    1235                 :            : 
    1236                 :      11574 :         last_cluster = 0;
    1237         [ +  + ]:      11706 :         while (last_cluster < blob->active.num_clusters) {
    1238                 :      10422 :                 blob_serialize_extent_rle(blob, last_cluster, &last_cluster, buf, remaining_sz);
    1239                 :            : 
    1240         [ +  + ]:      10422 :                 if (last_cluster == blob->active.num_clusters) {
    1241                 :      10290 :                         break;
    1242                 :            :                 }
    1243                 :            : 
    1244                 :        132 :                 rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
    1245         [ -  + ]:        132 :                 if (rc < 0) {
    1246                 :          0 :                         return rc;
    1247                 :            :                 }
    1248                 :            : 
    1249                 :        132 :                 *buf = (uint8_t *)cur_page->descriptors;
    1250                 :        132 :                 *remaining_sz = sizeof(cur_page->descriptors);
    1251                 :            :         }
    1252                 :            : 
    1253                 :      11574 :         return 0;
    1254                 :            : }
    1255                 :            : 
    1256                 :            : static void
    1257                 :     161209 : blob_serialize_extent_page(const struct spdk_blob *blob,
    1258                 :            :                            uint64_t cluster, struct spdk_blob_md_page *page)
    1259                 :            : {
    1260                 :            :         struct spdk_blob_md_descriptor_extent_page *desc_extent;
    1261                 :            :         uint64_t i, extent_idx;
    1262                 :            :         uint64_t lba, lba_per_cluster;
    1263         [ -  + ]:     161209 :         uint64_t start_cluster_idx = (cluster / SPDK_EXTENTS_PER_EP) * SPDK_EXTENTS_PER_EP;
    1264                 :            : 
    1265                 :     161209 :         desc_extent = (struct spdk_blob_md_descriptor_extent_page *) page->descriptors;
    1266                 :     161209 :         desc_extent->type = SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE;
    1267                 :            : 
    1268                 :     161209 :         lba_per_cluster = bs_cluster_to_lba(blob->bs, 1);
    1269                 :            : 
    1270                 :     161209 :         desc_extent->start_cluster_idx = start_cluster_idx;
    1271                 :     161209 :         extent_idx = 0;
    1272         [ +  + ]:   25627560 :         for (i = start_cluster_idx; i < blob->active.num_clusters; i++) {
    1273                 :   25505366 :                 lba = blob->active.clusters[i];
    1274         [ -  + ]:   25505366 :                 desc_extent->cluster_idx[extent_idx++] = lba / lba_per_cluster;
    1275         [ +  + ]:   25505366 :                 if (extent_idx >= SPDK_EXTENTS_PER_EP) {
    1276                 :      39015 :                         break;
    1277                 :            :                 }
    1278                 :            :         }
    1279                 :     161209 :         desc_extent->length = sizeof(desc_extent->start_cluster_idx) +
    1280                 :            :                               sizeof(desc_extent->cluster_idx[0]) * extent_idx;
    1281                 :     161209 : }
    1282                 :            : 
    1283                 :            : static void
    1284                 :     180520 : blob_serialize_flags(const struct spdk_blob *blob,
    1285                 :            :                      uint8_t *buf, size_t *buf_sz)
    1286                 :            : {
    1287                 :            :         struct spdk_blob_md_descriptor_flags *desc;
    1288                 :            : 
    1289                 :            :         /*
    1290                 :            :          * Flags get serialized first, so we should always have room for the flags
    1291                 :            :          *  descriptor.
    1292                 :            :          */
    1293         [ -  + ]:     180520 :         assert(*buf_sz >= sizeof(*desc));
    1294                 :            : 
    1295                 :     180520 :         desc = (struct spdk_blob_md_descriptor_flags *)buf;
    1296                 :     180520 :         desc->type = SPDK_MD_DESCRIPTOR_TYPE_FLAGS;
    1297                 :     180520 :         desc->length = sizeof(*desc) - sizeof(struct spdk_blob_md_descriptor);
    1298                 :     180520 :         desc->invalid_flags = blob->invalid_flags;
    1299                 :     180520 :         desc->data_ro_flags = blob->data_ro_flags;
    1300                 :     180520 :         desc->md_ro_flags = blob->md_ro_flags;
    1301                 :            : 
    1302                 :     180520 :         *buf_sz -= sizeof(*desc);
    1303                 :     180520 : }
    1304                 :            : 
    1305                 :            : static int
    1306                 :     361040 : blob_serialize_xattrs(const struct spdk_blob *blob,
    1307                 :            :                       const struct spdk_xattr_tailq *xattrs, bool internal,
    1308                 :            :                       struct spdk_blob_md_page **pages,
    1309                 :            :                       struct spdk_blob_md_page *cur_page,
    1310                 :            :                       uint32_t *page_count, uint8_t **buf,
    1311                 :            :                       size_t *remaining_sz)
    1312                 :            : {
    1313                 :            :         const struct spdk_xattr *xattr;
    1314                 :            :         int     rc;
    1315                 :            : 
    1316         [ +  + ]:     683786 :         TAILQ_FOREACH(xattr, xattrs, link) {
    1317                 :     322746 :                 size_t required_sz = 0;
    1318                 :            : 
    1319                 :     322746 :                 rc = blob_serialize_xattr(xattr,
    1320                 :            :                                           *buf, *remaining_sz,
    1321                 :            :                                           &required_sz, internal);
    1322         [ +  + ]:     322746 :                 if (rc < 0) {
    1323                 :            :                         /* Need to add a new page to the chain */
    1324                 :        288 :                         rc = blob_serialize_add_page(blob, pages, page_count,
    1325                 :            :                                                      &cur_page);
    1326         [ -  + ]:        288 :                         if (rc < 0) {
    1327                 :          0 :                                 spdk_free(*pages);
    1328                 :          0 :                                 *pages = NULL;
    1329                 :          0 :                                 *page_count = 0;
    1330                 :          0 :                                 return rc;
    1331                 :            :                         }
    1332                 :            : 
    1333                 :        288 :                         *buf = (uint8_t *)cur_page->descriptors;
    1334                 :        288 :                         *remaining_sz = sizeof(cur_page->descriptors);
    1335                 :            : 
    1336                 :            :                         /* Try again */
    1337                 :        288 :                         required_sz = 0;
    1338                 :        288 :                         rc = blob_serialize_xattr(xattr,
    1339                 :            :                                                   *buf, *remaining_sz,
    1340                 :            :                                                   &required_sz, internal);
    1341                 :            : 
    1342         [ -  + ]:        288 :                         if (rc < 0) {
    1343                 :          0 :                                 spdk_free(*pages);
    1344                 :          0 :                                 *pages = NULL;
    1345                 :          0 :                                 *page_count = 0;
    1346                 :          0 :                                 return rc;
    1347                 :            :                         }
    1348                 :            :                 }
    1349                 :            : 
    1350                 :     322746 :                 *remaining_sz -= required_sz;
    1351                 :     322746 :                 *buf += required_sz;
    1352                 :            :         }
    1353                 :            : 
    1354                 :     361040 :         return 0;
    1355                 :            : }
    1356                 :            : 
    1357                 :            : static int
    1358                 :     180520 : blob_serialize(const struct spdk_blob *blob, struct spdk_blob_md_page **pages,
    1359                 :            :                uint32_t *page_count)
    1360                 :            : {
    1361                 :     176412 :         struct spdk_blob_md_page                *cur_page;
    1362                 :            :         int                                     rc;
    1363                 :     176412 :         uint8_t                                 *buf;
    1364                 :     176412 :         size_t                                  remaining_sz;
    1365                 :            : 
    1366         [ -  + ]:     180520 :         assert(pages != NULL);
    1367         [ -  + ]:     180520 :         assert(page_count != NULL);
    1368         [ -  + ]:     180520 :         assert(blob != NULL);
    1369         [ -  + ]:     180520 :         assert(blob->state == SPDK_BLOB_STATE_DIRTY);
    1370                 :            : 
    1371                 :     180520 :         *pages = NULL;
    1372                 :     180520 :         *page_count = 0;
    1373                 :            : 
    1374                 :            :         /* A blob always has at least 1 page, even if it has no descriptors */
    1375                 :     180520 :         rc = blob_serialize_add_page(blob, pages, page_count, &cur_page);
    1376         [ -  + ]:     180520 :         if (rc < 0) {
    1377                 :          0 :                 return rc;
    1378                 :            :         }
    1379                 :            : 
    1380                 :     180520 :         buf = (uint8_t *)cur_page->descriptors;
    1381                 :     180520 :         remaining_sz = sizeof(cur_page->descriptors);
    1382                 :            : 
    1383                 :            :         /* Serialize flags */
    1384                 :     180520 :         blob_serialize_flags(blob, buf, &remaining_sz);
    1385                 :     180520 :         buf += sizeof(struct spdk_blob_md_descriptor_flags);
    1386                 :            : 
    1387                 :            :         /* Serialize xattrs */
    1388                 :     180520 :         rc = blob_serialize_xattrs(blob, &blob->xattrs, false,
    1389                 :            :                                    pages, cur_page, page_count, &buf, &remaining_sz);
    1390         [ -  + ]:     180520 :         if (rc < 0) {
    1391                 :          0 :                 return rc;
    1392                 :            :         }
    1393                 :            : 
    1394                 :            :         /* Serialize internal xattrs */
    1395                 :     180520 :         rc = blob_serialize_xattrs(blob, &blob->xattrs_internal, true,
    1396                 :            :                                    pages, cur_page, page_count, &buf, &remaining_sz);
    1397         [ -  + ]:     180520 :         if (rc < 0) {
    1398                 :          0 :                 return rc;
    1399                 :            :         }
    1400                 :            : 
    1401   [ +  +  +  + ]:     180520 :         if (blob->use_extent_table) {
    1402                 :            :                 /* Serialize extent table */
    1403                 :     168946 :                 rc = blob_serialize_extent_table(blob, pages, cur_page, page_count, &buf, &remaining_sz);
    1404                 :            :         } else {
    1405                 :            :                 /* Serialize extents */
    1406                 :      11574 :                 rc = blob_serialize_extents_rle(blob, pages, cur_page, page_count, &buf, &remaining_sz);
    1407                 :            :         }
    1408                 :            : 
    1409                 :     180520 :         return rc;
    1410                 :            : }
    1411                 :            : 
    1412                 :            : struct spdk_blob_load_ctx {
    1413                 :            :         struct spdk_blob                *blob;
    1414                 :            : 
    1415                 :            :         struct spdk_blob_md_page        *pages;
    1416                 :            :         uint32_t                        num_pages;
    1417                 :            :         uint32_t                        next_extent_page;
    1418                 :            :         spdk_bs_sequence_t              *seq;
    1419                 :            : 
    1420                 :            :         spdk_bs_sequence_cpl            cb_fn;
    1421                 :            :         void                            *cb_arg;
    1422                 :            : };
    1423                 :            : 
    1424                 :            : static uint32_t
    1425                 :     512068 : blob_md_page_calc_crc(void *page)
    1426                 :            : {
    1427                 :            :         uint32_t                crc;
    1428                 :            : 
    1429                 :     512068 :         crc = BLOB_CRC32C_INITIAL;
    1430                 :     512068 :         crc = spdk_crc32c_update(page, SPDK_BS_PAGE_SIZE - 4, crc);
    1431                 :     512068 :         crc ^= BLOB_CRC32C_INITIAL;
    1432                 :            : 
    1433                 :     512068 :         return crc;
    1434                 :            : 
    1435                 :            : }
    1436                 :            : 
    1437                 :            : static void
    1438                 :      42059 : blob_load_final(struct spdk_blob_load_ctx *ctx, int bserrno)
    1439                 :            : {
    1440                 :      42059 :         struct spdk_blob                *blob = ctx->blob;
    1441                 :            : 
    1442         [ +  + ]:      42059 :         if (bserrno == 0) {
    1443                 :      41675 :                 blob_mark_clean(blob);
    1444                 :            :         }
    1445                 :            : 
    1446                 :      42059 :         ctx->cb_fn(ctx->seq, ctx->cb_arg, bserrno);
    1447                 :            : 
    1448                 :            :         /* Free the memory */
    1449                 :      42059 :         spdk_free(ctx->pages);
    1450                 :      42059 :         free(ctx);
    1451                 :      42059 : }
    1452                 :            : 
    1453                 :            : static void
    1454                 :       2872 : blob_load_snapshot_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno)
    1455                 :            : {
    1456                 :       2872 :         struct spdk_blob_load_ctx       *ctx = cb_arg;
    1457                 :       2872 :         struct spdk_blob                *blob = ctx->blob;
    1458                 :            : 
    1459         [ +  + ]:       2872 :         if (bserrno == 0) {
    1460                 :       2836 :                 blob->back_bs_dev = bs_create_blob_bs_dev(snapshot);
    1461         [ -  + ]:       2836 :                 if (blob->back_bs_dev == NULL) {
    1462                 :          0 :                         bserrno = -ENOMEM;
    1463                 :            :                 }
    1464                 :            :         }
    1465         [ +  + ]:       2872 :         if (bserrno != 0) {
    1466                 :         36 :                 SPDK_ERRLOG("Snapshot fail\n");
    1467                 :            :         }
    1468                 :            : 
    1469                 :       2872 :         blob_load_final(ctx, bserrno);
    1470                 :       2872 : }
    1471                 :            : 
    1472                 :            : static void blob_update_clear_method(struct spdk_blob *blob);
    1473                 :            : 
    1474                 :            : static int
    1475                 :        796 : blob_load_esnap(struct spdk_blob *blob, void *blob_ctx)
    1476                 :            : {
    1477                 :        796 :         struct spdk_blob_store *bs = blob->bs;
    1478                 :        796 :         struct spdk_bs_dev *bs_dev = NULL;
    1479                 :        796 :         const void *esnap_id = NULL;
    1480                 :        796 :         size_t id_len = 0;
    1481                 :            :         int rc;
    1482                 :            : 
    1483         [ +  + ]:        796 :         if (bs->esnap_bs_dev_create == NULL) {
    1484                 :         48 :                 SPDK_NOTICELOG("blob 0x%" PRIx64 " is an esnap clone but the blobstore was opened "
    1485                 :            :                                "without support for esnap clones\n", blob->id);
    1486                 :         48 :                 return -ENOTSUP;
    1487                 :            :         }
    1488         [ -  + ]:        748 :         assert(blob->back_bs_dev == NULL);
    1489                 :            : 
    1490                 :        748 :         rc = blob_get_xattr_value(blob, BLOB_EXTERNAL_SNAPSHOT_ID, &esnap_id, &id_len, true);
    1491         [ -  + ]:        748 :         if (rc != 0) {
    1492                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " is an esnap clone but has no esnap ID\n", blob->id);
    1493                 :          0 :                 return -EINVAL;
    1494                 :            :         }
    1495   [ +  -  +  + ]:        748 :         assert(id_len > 0 && id_len < UINT32_MAX);
    1496                 :            : 
    1497   [ -  +  -  + ]:        748 :         SPDK_INFOLOG(blob, "Creating external snapshot device\n");
    1498                 :            : 
    1499                 :        748 :         rc = bs->esnap_bs_dev_create(bs->esnap_ctx, blob_ctx, blob, esnap_id, (uint32_t)id_len,
    1500                 :            :                                      &bs_dev);
    1501         [ -  + ]:        748 :         if (rc != 0) {
    1502   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": failed to load back_bs_dev "
    1503                 :            :                               "with error %d\n", blob->id, rc);
    1504                 :          0 :                 return rc;
    1505                 :            :         }
    1506                 :            : 
    1507                 :            :         /*
    1508                 :            :          * Note: bs_dev might be NULL if the consumer chose to not open the external snapshot.
    1509                 :            :          * This especially might happen during spdk_bs_load() iteration.
    1510                 :            :          */
    1511         [ +  + ]:        748 :         if (bs_dev != NULL) {
    1512   [ -  +  -  + ]:        704 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": loaded back_bs_dev\n", blob->id);
    1513   [ +  +  +  + ]:        704 :                 if ((bs->io_unit_size % bs_dev->blocklen) != 0) {
    1514                 :         24 :                         SPDK_NOTICELOG("blob 0x%" PRIx64 " external snapshot device block size %u "
    1515                 :            :                                        "is not compatible with blobstore block size %u\n",
    1516                 :            :                                        blob->id, bs_dev->blocklen, bs->io_unit_size);
    1517                 :         24 :                         bs_dev->destroy(bs_dev);
    1518                 :         24 :                         return -EINVAL;
    1519                 :            :                 }
    1520                 :            :         }
    1521                 :            : 
    1522                 :        724 :         blob->back_bs_dev = bs_dev;
    1523                 :        724 :         blob->parent_id = SPDK_BLOBID_EXTERNAL_SNAPSHOT;
    1524                 :            : 
    1525                 :        724 :         return 0;
    1526                 :            : }
    1527                 :            : 
    1528                 :            : static void
    1529                 :      41783 : blob_load_backing_dev(spdk_bs_sequence_t *seq, void *cb_arg)
    1530                 :            : {
    1531                 :      41783 :         struct spdk_blob_load_ctx       *ctx = cb_arg;
    1532                 :      41783 :         struct spdk_blob                *blob = ctx->blob;
    1533                 :      37728 :         const void                      *value;
    1534                 :      37728 :         size_t                          len;
    1535                 :            :         int                             rc;
    1536                 :            : 
    1537         [ +  + ]:      41783 :         if (blob_is_esnap_clone(blob)) {
    1538                 :        796 :                 rc = blob_load_esnap(blob, seq->cpl.u.blob_handle.esnap_ctx);
    1539                 :        796 :                 blob_load_final(ctx, rc);
    1540                 :       1272 :                 return;
    1541                 :            :         }
    1542                 :            : 
    1543         [ +  + ]:      40987 :         if (spdk_blob_is_thin_provisioned(blob)) {
    1544                 :       6572 :                 rc = blob_get_xattr_value(blob, BLOB_SNAPSHOT, &value, &len, true);
    1545         [ +  + ]:       6572 :                 if (rc == 0) {
    1546         [ -  + ]:       2872 :                         if (len != sizeof(spdk_blob_id)) {
    1547                 :          0 :                                 blob_load_final(ctx, -EINVAL);
    1548                 :          0 :                                 return;
    1549                 :            :                         }
    1550                 :            :                         /* open snapshot blob and continue in the callback function */
    1551                 :       2872 :                         blob->parent_id = *(spdk_blob_id *)value;
    1552                 :       2872 :                         spdk_bs_open_blob(blob->bs, blob->parent_id,
    1553                 :            :                                           blob_load_snapshot_cpl, ctx);
    1554                 :       2872 :                         return;
    1555                 :            :                 } else {
    1556                 :            :                         /* add zeroes_dev for thin provisioned blob */
    1557                 :       3700 :                         blob->back_bs_dev = bs_create_zeroes_dev();
    1558                 :            :                 }
    1559                 :            :         } else {
    1560                 :            :                 /* standard blob */
    1561                 :      34415 :                 blob->back_bs_dev = NULL;
    1562                 :            :         }
    1563                 :      38115 :         blob_load_final(ctx, 0);
    1564                 :            : }
    1565                 :            : 
    1566                 :            : static void
    1567                 :      65197 : blob_load_cpl_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    1568                 :            : {
    1569                 :      65197 :         struct spdk_blob_load_ctx       *ctx = cb_arg;
    1570                 :      65197 :         struct spdk_blob                *blob = ctx->blob;
    1571                 :            :         struct spdk_blob_md_page        *page;
    1572                 :            :         uint64_t                        i;
    1573                 :            :         uint32_t                        crc;
    1574                 :            :         uint64_t                        lba;
    1575                 :            :         void                            *tmp;
    1576                 :            :         uint64_t                        sz;
    1577                 :            : 
    1578         [ +  + ]:      65197 :         if (bserrno) {
    1579                 :         36 :                 SPDK_ERRLOG("Extent page read failed: %d\n", bserrno);
    1580                 :         36 :                 blob_load_final(ctx, bserrno);
    1581                 :         36 :                 return;
    1582                 :            :         }
    1583                 :            : 
    1584         [ +  + ]:      65161 :         if (ctx->pages == NULL) {
    1585                 :            :                 /* First iteration of this function, allocate buffer for single EXTENT_PAGE */
    1586                 :      31847 :                 ctx->pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
    1587                 :            :                                           NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    1588         [ -  + ]:      31847 :                 if (!ctx->pages) {
    1589                 :          0 :                         blob_load_final(ctx, -ENOMEM);
    1590                 :          0 :                         return;
    1591                 :            :                 }
    1592                 :      31847 :                 ctx->num_pages = 1;
    1593                 :      31847 :                 ctx->next_extent_page = 0;
    1594                 :            :         } else {
    1595                 :      33314 :                 page = &ctx->pages[0];
    1596                 :      33314 :                 crc = blob_md_page_calc_crc(page);
    1597         [ -  + ]:      33314 :                 if (crc != page->crc) {
    1598                 :          0 :                         blob_load_final(ctx, -EINVAL);
    1599                 :          0 :                         return;
    1600                 :            :                 }
    1601                 :            : 
    1602         [ -  + ]:      33314 :                 if (page->next != SPDK_INVALID_MD_PAGE) {
    1603                 :          0 :                         blob_load_final(ctx, -EINVAL);
    1604                 :          0 :                         return;
    1605                 :            :                 }
    1606                 :            : 
    1607                 :      33314 :                 bserrno = blob_parse_extent_page(page, blob);
    1608         [ -  + ]:      33314 :                 if (bserrno) {
    1609                 :          0 :                         blob_load_final(ctx, bserrno);
    1610                 :          0 :                         return;
    1611                 :            :                 }
    1612                 :            :         }
    1613                 :            : 
    1614         [ +  + ]:      73149 :         for (i = ctx->next_extent_page; i < blob->active.num_extent_pages; i++) {
    1615         [ +  + ]:      41338 :                 if (blob->active.extent_pages[i] != 0) {
    1616                 :            :                         /* Extent page was allocated, read and parse it. */
    1617                 :      33350 :                         lba = bs_md_page_to_lba(blob->bs, blob->active.extent_pages[i]);
    1618                 :      33350 :                         ctx->next_extent_page = i + 1;
    1619                 :            : 
    1620                 :      33350 :                         bs_sequence_read_dev(seq, &ctx->pages[0], lba,
    1621                 :      33350 :                                              bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
    1622                 :            :                                              blob_load_cpl_extents_cpl, ctx);
    1623                 :      33350 :                         return;
    1624                 :            :                 } else {
    1625                 :            :                         /* Thin provisioned blobs can point to unallocated extent pages.
    1626                 :            :                          * In this case blob size should be increased by up to the amount left in remaining_clusters_in_et. */
    1627                 :            : 
    1628         [ +  + ]:       7988 :                         sz = spdk_min(blob->remaining_clusters_in_et, SPDK_EXTENTS_PER_EP);
    1629                 :       7988 :                         blob->active.num_clusters += sz;
    1630                 :       7988 :                         blob->remaining_clusters_in_et -= sz;
    1631                 :            : 
    1632         [ -  + ]:       7988 :                         assert(spdk_blob_is_thin_provisioned(blob));
    1633   [ +  +  -  + ]:       7988 :                         assert(i + 1 < blob->active.num_extent_pages || blob->remaining_clusters_in_et == 0);
    1634                 :            : 
    1635                 :       7988 :                         tmp = realloc(blob->active.clusters, blob->active.num_clusters * sizeof(*blob->active.clusters));
    1636         [ -  + ]:       7988 :                         if (tmp == NULL) {
    1637                 :          0 :                                 blob_load_final(ctx, -ENOMEM);
    1638                 :          0 :                                 return;
    1639                 :            :                         }
    1640         [ -  + ]:       7988 :                         memset(tmp + sizeof(*blob->active.clusters) * blob->active.cluster_array_size, 0,
    1641                 :       7988 :                                sizeof(*blob->active.clusters) * (blob->active.num_clusters - blob->active.cluster_array_size));
    1642                 :       7988 :                         blob->active.clusters = tmp;
    1643                 :       7988 :                         blob->active.cluster_array_size = blob->active.num_clusters;
    1644                 :            :                 }
    1645                 :            :         }
    1646                 :            : 
    1647                 :      31811 :         blob_load_backing_dev(seq, ctx);
    1648                 :            : }
    1649                 :            : 
    1650                 :            : static void
    1651                 :      42664 : blob_load_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    1652                 :            : {
    1653                 :      42664 :         struct spdk_blob_load_ctx       *ctx = cb_arg;
    1654                 :      42664 :         struct spdk_blob                *blob = ctx->blob;
    1655                 :            :         struct spdk_blob_md_page        *page;
    1656                 :            :         int                             rc;
    1657                 :            :         uint32_t                        crc;
    1658                 :            :         uint32_t                        current_page;
    1659                 :            : 
    1660         [ +  + ]:      42664 :         if (ctx->num_pages == 1) {
    1661                 :      42059 :                 current_page = bs_blobid_to_page(blob->id);
    1662                 :            :         } else {
    1663         [ -  + ]:        605 :                 assert(ctx->num_pages != 0);
    1664                 :        605 :                 page = &ctx->pages[ctx->num_pages - 2];
    1665                 :        605 :                 current_page = page->next;
    1666                 :            :         }
    1667                 :            : 
    1668         [ +  + ]:      42664 :         if (bserrno) {
    1669                 :        120 :                 SPDK_ERRLOG("Metadata page %d read failed for blobid 0x%" PRIx64 ": %d\n",
    1670                 :            :                             current_page, blob->id, bserrno);
    1671                 :        120 :                 blob_load_final(ctx, bserrno);
    1672                 :        120 :                 return;
    1673                 :            :         }
    1674                 :            : 
    1675                 :      42544 :         page = &ctx->pages[ctx->num_pages - 1];
    1676                 :      42544 :         crc = blob_md_page_calc_crc(page);
    1677         [ +  + ]:      42544 :         if (crc != page->crc) {
    1678                 :         48 :                 SPDK_ERRLOG("Metadata page %d crc mismatch for blobid 0x%" PRIx64 "\n",
    1679                 :            :                             current_page, blob->id);
    1680                 :         48 :                 blob_load_final(ctx, -EINVAL);
    1681                 :         48 :                 return;
    1682                 :            :         }
    1683                 :            : 
    1684         [ +  + ]:      42496 :         if (page->next != SPDK_INVALID_MD_PAGE) {
    1685                 :            :                 struct spdk_blob_md_page *tmp_pages;
    1686                 :        605 :                 uint32_t next_page = page->next;
    1687                 :        605 :                 uint64_t next_lba = bs_md_page_to_lba(blob->bs, next_page);
    1688                 :            : 
    1689                 :            :                 /* Read the next page */
    1690                 :        605 :                 tmp_pages = spdk_realloc(ctx->pages, (sizeof(*page) * (ctx->num_pages + 1)), 0);
    1691         [ -  + ]:        605 :                 if (tmp_pages == NULL) {
    1692                 :          0 :                         blob_load_final(ctx, -ENOMEM);
    1693                 :          0 :                         return;
    1694                 :            :                 }
    1695                 :        605 :                 ctx->num_pages++;
    1696                 :        605 :                 ctx->pages = tmp_pages;
    1697                 :            : 
    1698                 :        605 :                 bs_sequence_read_dev(seq, &ctx->pages[ctx->num_pages - 1],
    1699                 :            :                                      next_lba,
    1700                 :        605 :                                      bs_byte_to_lba(blob->bs, sizeof(*page)),
    1701                 :            :                                      blob_load_cpl, ctx);
    1702                 :        605 :                 return;
    1703                 :            :         }
    1704                 :            : 
    1705                 :            :         /* Parse the pages */
    1706                 :      41891 :         rc = blob_parse(ctx->pages, ctx->num_pages, blob);
    1707         [ +  + ]:      41891 :         if (rc) {
    1708                 :         72 :                 blob_load_final(ctx, rc);
    1709                 :         72 :                 return;
    1710                 :            :         }
    1711                 :            : 
    1712   [ +  +  +  + ]:      41819 :         if (blob->extent_table_found == true) {
    1713                 :            :                 /* If EXTENT_TABLE was found, that means support for it should be enabled. */
    1714   [ -  +  -  + ]:      31847 :                 assert(blob->extent_rle_found == false);
    1715                 :      31847 :                 blob->use_extent_table = true;
    1716                 :            :         } else {
    1717                 :            :                 /* If EXTENT_RLE or no extent_* descriptor was found disable support
    1718                 :            :                  * for extent table. No extent_* descriptors means that blob has length of 0
    1719                 :            :                  * and no extent_rle descriptors were persisted for it.
    1720                 :            :                  * EXTENT_TABLE if used, is always present in metadata regardless of length. */
    1721                 :       9972 :                 blob->use_extent_table = false;
    1722                 :            :         }
    1723                 :            : 
    1724                 :            :         /* Check the clear_method stored in metadata vs what may have been passed
    1725                 :            :          * via spdk_bs_open_blob_ext() and update accordingly.
    1726                 :            :          */
    1727                 :      41819 :         blob_update_clear_method(blob);
    1728                 :            : 
    1729                 :      41819 :         spdk_free(ctx->pages);
    1730                 :      41819 :         ctx->pages = NULL;
    1731                 :            : 
    1732   [ +  +  +  + ]:      41819 :         if (blob->extent_table_found) {
    1733                 :      31847 :                 blob_load_cpl_extents_cpl(seq, ctx, 0);
    1734                 :            :         } else {
    1735                 :       9972 :                 blob_load_backing_dev(seq, ctx);
    1736                 :            :         }
    1737                 :            : }
    1738                 :            : 
    1739                 :            : /* Load a blob from disk given a blobid */
    1740                 :            : static void
    1741                 :      42059 : blob_load(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
    1742                 :            :           spdk_bs_sequence_cpl cb_fn, void *cb_arg)
    1743                 :            : {
    1744                 :            :         struct spdk_blob_load_ctx *ctx;
    1745                 :            :         struct spdk_blob_store *bs;
    1746                 :            :         uint32_t page_num;
    1747                 :            :         uint64_t lba;
    1748                 :            : 
    1749                 :      42059 :         blob_verify_md_op(blob);
    1750                 :            : 
    1751                 :      42059 :         bs = blob->bs;
    1752                 :            : 
    1753                 :      42059 :         ctx = calloc(1, sizeof(*ctx));
    1754         [ -  + ]:      42059 :         if (!ctx) {
    1755                 :          0 :                 cb_fn(seq, cb_arg, -ENOMEM);
    1756                 :          0 :                 return;
    1757                 :            :         }
    1758                 :            : 
    1759                 :      42059 :         ctx->blob = blob;
    1760                 :      42059 :         ctx->pages = spdk_realloc(ctx->pages, SPDK_BS_PAGE_SIZE, 0);
    1761         [ -  + ]:      42059 :         if (!ctx->pages) {
    1762                 :          0 :                 free(ctx);
    1763                 :          0 :                 cb_fn(seq, cb_arg, -ENOMEM);
    1764                 :          0 :                 return;
    1765                 :            :         }
    1766                 :      42059 :         ctx->num_pages = 1;
    1767                 :      42059 :         ctx->cb_fn = cb_fn;
    1768                 :      42059 :         ctx->cb_arg = cb_arg;
    1769                 :      42059 :         ctx->seq = seq;
    1770                 :            : 
    1771                 :      42059 :         page_num = bs_blobid_to_page(blob->id);
    1772                 :      42059 :         lba = bs_md_page_to_lba(blob->bs, page_num);
    1773                 :            : 
    1774                 :      42059 :         blob->state = SPDK_BLOB_STATE_LOADING;
    1775                 :            : 
    1776                 :      42059 :         bs_sequence_read_dev(seq, &ctx->pages[0], lba,
    1777                 :      42059 :                              bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE),
    1778                 :            :                              blob_load_cpl, ctx);
    1779                 :            : }
    1780                 :            : 
    1781                 :            : struct spdk_blob_persist_ctx {
    1782                 :            :         struct spdk_blob                *blob;
    1783                 :            : 
    1784                 :            :         struct spdk_blob_md_page        *pages;
    1785                 :            :         uint32_t                        next_extent_page;
    1786                 :            :         struct spdk_blob_md_page        *extent_page;
    1787                 :            : 
    1788                 :            :         spdk_bs_sequence_t              *seq;
    1789                 :            :         spdk_bs_sequence_cpl            cb_fn;
    1790                 :            :         void                            *cb_arg;
    1791                 :            :         TAILQ_ENTRY(spdk_blob_persist_ctx) link;
    1792                 :            : };
    1793                 :            : 
    1794                 :            : static void
    1795                 :      45677 : bs_batch_clear_dev(struct spdk_blob *blob, spdk_bs_batch_t *batch, uint64_t lba,
    1796                 :            :                    uint64_t lba_count)
    1797                 :            : {
    1798      [ +  -  + ]:      45677 :         switch (blob->clear_method) {
    1799                 :      45676 :         case BLOB_CLEAR_WITH_DEFAULT:
    1800                 :            :         case BLOB_CLEAR_WITH_UNMAP:
    1801                 :      45676 :                 bs_batch_unmap_dev(batch, lba, lba_count);
    1802                 :      45676 :                 break;
    1803                 :          0 :         case BLOB_CLEAR_WITH_WRITE_ZEROES:
    1804                 :          0 :                 bs_batch_write_zeroes_dev(batch, lba, lba_count);
    1805                 :          0 :                 break;
    1806                 :          1 :         case BLOB_CLEAR_WITH_NONE:
    1807                 :            :         default:
    1808                 :          1 :                 break;
    1809                 :            :         }
    1810                 :      45677 : }
    1811                 :            : 
    1812                 :            : static int
    1813                 :      14932 : bs_super_validate(struct spdk_bs_super_block *super, struct spdk_blob_store *bs)
    1814                 :            : {
    1815                 :            :         uint32_t        crc;
    1816                 :            :         static const char zeros[SPDK_BLOBSTORE_TYPE_LENGTH];
    1817                 :            : 
    1818         [ +  + ]:      14932 :         if (super->version > SPDK_BS_VERSION ||
    1819         [ +  + ]:      14223 :             super->version < SPDK_BS_INITIAL_VERSION) {
    1820                 :       7219 :                 return -EILSEQ;
    1821                 :            :         }
    1822                 :            : 
    1823   [ +  +  +  + ]:       7713 :         if (memcmp(super->signature, SPDK_BS_SUPER_BLOCK_SIG,
    1824                 :            :                    sizeof(super->signature)) != 0) {
    1825                 :        349 :                 return -EILSEQ;
    1826                 :            :         }
    1827                 :            : 
    1828                 :       7364 :         crc = blob_md_page_calc_crc(super);
    1829         [ +  + ]:       7364 :         if (crc != super->crc) {
    1830                 :         24 :                 return -EILSEQ;
    1831                 :            :         }
    1832                 :            : 
    1833   [ +  +  -  +  :       7340 :         if (memcmp(&bs->bstype, &super->bstype, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
                   +  + ]
    1834   [ -  +  -  + ]:       7238 :                 SPDK_DEBUGLOG(blob, "Bstype matched - loading blobstore\n");
    1835   [ +  +  +  + ]:        102 :         } else if (memcmp(&bs->bstype, zeros, SPDK_BLOBSTORE_TYPE_LENGTH) == 0) {
    1836   [ -  +  -  + ]:         46 :                 SPDK_DEBUGLOG(blob, "Bstype wildcard used - loading blobstore regardless bstype\n");
    1837                 :            :         } else {
    1838   [ -  +  -  + ]:         56 :                 SPDK_DEBUGLOG(blob, "Unexpected bstype\n");
    1839   [ -  +  -  + ]:         56 :                 SPDK_LOGDUMP(blob, "Expected:", bs->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
    1840   [ -  +  -  + ]:         56 :                 SPDK_LOGDUMP(blob, "Found:", super->bstype.bstype, SPDK_BLOBSTORE_TYPE_LENGTH);
    1841                 :         56 :                 return -ENXIO;
    1842                 :            :         }
    1843                 :            : 
    1844         [ +  + ]:       7284 :         if (super->size > bs->dev->blockcnt * bs->dev->blocklen) {
    1845                 :         48 :                 SPDK_NOTICELOG("Size mismatch, dev size: %" PRIu64 ", blobstore size: %" PRIu64 "\n",
    1846                 :            :                                bs->dev->blockcnt * bs->dev->blocklen, super->size);
    1847                 :         48 :                 return -EILSEQ;
    1848                 :            :         }
    1849                 :            : 
    1850                 :       7236 :         return 0;
    1851                 :            : }
    1852                 :            : 
    1853                 :            : static void bs_mark_dirty(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
    1854                 :            :                           spdk_bs_sequence_cpl cb_fn, void *cb_arg);
    1855                 :            : 
    1856                 :            : static void
    1857                 :     191138 : blob_persist_complete_cb(void *arg)
    1858                 :            : {
    1859                 :     191138 :         struct spdk_blob_persist_ctx *ctx = arg;
    1860                 :            : 
    1861                 :            :         /* Call user callback */
    1862                 :     191138 :         ctx->cb_fn(ctx->seq, ctx->cb_arg, 0);
    1863                 :            : 
    1864                 :            :         /* Free the memory */
    1865                 :     191138 :         spdk_free(ctx->pages);
    1866                 :     191138 :         free(ctx);
    1867                 :     191138 : }
    1868                 :            : 
    1869                 :            : static void blob_persist_start(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno);
    1870                 :            : 
    1871                 :            : static void
    1872                 :     191138 : blob_persist_complete(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx, int bserrno)
    1873                 :            : {
    1874                 :            :         struct spdk_blob_persist_ctx    *next_persist, *tmp;
    1875                 :     191138 :         struct spdk_blob                *blob = ctx->blob;
    1876                 :            : 
    1877         [ +  + ]:     191138 :         if (bserrno == 0) {
    1878                 :     190826 :                 blob_mark_clean(blob);
    1879                 :            :         }
    1880                 :            : 
    1881         [ -  + ]:     191138 :         assert(ctx == TAILQ_FIRST(&blob->persists_to_complete));
    1882                 :            : 
    1883                 :            :         /* Complete all persists that were pending when the current persist started */
    1884         [ +  + ]:     382276 :         TAILQ_FOREACH_SAFE(next_persist, &blob->persists_to_complete, link, tmp) {
    1885         [ -  + ]:     191138 :                 TAILQ_REMOVE(&blob->persists_to_complete, next_persist, link);
    1886                 :     191138 :                 spdk_thread_send_msg(spdk_get_thread(), blob_persist_complete_cb, next_persist);
    1887                 :            :         }
    1888                 :            : 
    1889         [ +  + ]:     191138 :         if (TAILQ_EMPTY(&blob->pending_persists)) {
    1890                 :     190999 :                 return;
    1891                 :            :         }
    1892                 :            : 
    1893                 :            :         /* Queue up all pending persists for completion and start blob persist with first one */
    1894   [ +  -  -  + ]:        139 :         TAILQ_SWAP(&blob->persists_to_complete, &blob->pending_persists, spdk_blob_persist_ctx, link);
    1895                 :        139 :         next_persist = TAILQ_FIRST(&blob->persists_to_complete);
    1896                 :            : 
    1897                 :        139 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    1898                 :        139 :         bs_mark_dirty(seq, blob->bs, blob_persist_start, next_persist);
    1899                 :            : }
    1900                 :            : 
    1901                 :            : static void
    1902                 :     190826 : blob_persist_clear_extents_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    1903                 :            : {
    1904                 :     190826 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    1905                 :     190826 :         struct spdk_blob                *blob = ctx->blob;
    1906                 :     190826 :         struct spdk_blob_store          *bs = blob->bs;
    1907                 :            :         size_t                          i;
    1908                 :            : 
    1909         [ -  + ]:     190826 :         if (bserrno != 0) {
    1910                 :          0 :                 blob_persist_complete(seq, ctx, bserrno);
    1911                 :          0 :                 return;
    1912                 :            :         }
    1913                 :            : 
    1914                 :     190826 :         spdk_spin_lock(&bs->used_lock);
    1915                 :            : 
    1916                 :            :         /* Release all extent_pages that were truncated */
    1917         [ +  + ]:     208047 :         for (i = blob->active.num_extent_pages; i < blob->active.extent_pages_array_size; i++) {
    1918                 :            :                 /* Nothing to release if it was not allocated */
    1919         [ +  + ]:      17221 :                 if (blob->active.extent_pages[i] != 0) {
    1920                 :      10274 :                         bs_release_md_page(bs, blob->active.extent_pages[i]);
    1921                 :            :                 }
    1922                 :            :         }
    1923                 :            : 
    1924                 :     190826 :         spdk_spin_unlock(&bs->used_lock);
    1925                 :            : 
    1926         [ +  + ]:     190826 :         if (blob->active.num_extent_pages == 0) {
    1927                 :      26509 :                 free(blob->active.extent_pages);
    1928                 :      26509 :                 blob->active.extent_pages = NULL;
    1929                 :      26509 :                 blob->active.extent_pages_array_size = 0;
    1930         [ +  + ]:     164317 :         } else if (blob->active.num_extent_pages != blob->active.extent_pages_array_size) {
    1931                 :            : #ifndef __clang_analyzer__
    1932                 :            :                 void *tmp;
    1933                 :            : 
    1934                 :            :                 /* scan-build really can't figure reallocs, workaround it */
    1935                 :         12 :                 tmp = realloc(blob->active.extent_pages, sizeof(uint32_t) * blob->active.num_extent_pages);
    1936         [ -  + ]:         12 :                 assert(tmp != NULL);
    1937                 :         12 :                 blob->active.extent_pages = tmp;
    1938                 :            : #endif
    1939                 :         12 :                 blob->active.extent_pages_array_size = blob->active.num_extent_pages;
    1940                 :            :         }
    1941                 :            : 
    1942                 :     190826 :         blob_persist_complete(seq, ctx, bserrno);
    1943                 :            : }
    1944                 :            : 
    1945                 :            : static void
    1946                 :     190826 : blob_persist_clear_extents(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
    1947                 :            : {
    1948                 :     190826 :         struct spdk_blob                *blob = ctx->blob;
    1949                 :     190826 :         struct spdk_blob_store          *bs = blob->bs;
    1950                 :            :         size_t                          i;
    1951                 :            :         uint64_t                        lba;
    1952                 :            :         uint64_t                        lba_count;
    1953                 :            :         spdk_bs_batch_t                 *batch;
    1954                 :            : 
    1955                 :     190826 :         batch = bs_sequence_to_batch(seq, blob_persist_clear_extents_cpl, ctx);
    1956                 :     190826 :         lba_count = bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
    1957                 :            : 
    1958                 :            :         /* Clear all extent_pages that were truncated */
    1959         [ +  + ]:     208047 :         for (i = blob->active.num_extent_pages; i < blob->active.extent_pages_array_size; i++) {
    1960                 :            :                 /* Nothing to clear if it was not allocated */
    1961         [ +  + ]:      17221 :                 if (blob->active.extent_pages[i] != 0) {
    1962                 :      10274 :                         lba = bs_md_page_to_lba(bs, blob->active.extent_pages[i]);
    1963                 :      10274 :                         bs_batch_write_zeroes_dev(batch, lba, lba_count);
    1964                 :            :                 }
    1965                 :            :         }
    1966                 :            : 
    1967                 :     190826 :         bs_batch_close(batch);
    1968                 :     190826 : }
    1969                 :            : 
    1970                 :            : static void
    1971                 :     190826 : blob_persist_clear_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    1972                 :            : {
    1973                 :     190826 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    1974                 :     190826 :         struct spdk_blob                *blob = ctx->blob;
    1975                 :     190826 :         struct spdk_blob_store          *bs = blob->bs;
    1976                 :            :         size_t                          i;
    1977                 :            : 
    1978         [ -  + ]:     190826 :         if (bserrno != 0) {
    1979                 :          0 :                 blob_persist_complete(seq, ctx, bserrno);
    1980                 :          0 :                 return;
    1981                 :            :         }
    1982                 :            : 
    1983                 :     190826 :         spdk_spin_lock(&bs->used_lock);
    1984                 :            :         /* Release all clusters that were truncated */
    1985         [ +  + ]:    9486842 :         for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
    1986                 :    9296023 :                 uint32_t cluster_num = bs_lba_to_cluster(bs, blob->active.clusters[i]);
    1987                 :            : 
    1988                 :            :                 /* Nothing to release if it was not allocated */
    1989         [ +  + ]:    9296023 :                 if (blob->active.clusters[i] != 0) {
    1990                 :    2336940 :                         bs_release_cluster(bs, cluster_num);
    1991                 :            :                 }
    1992                 :            :         }
    1993                 :     190826 :         spdk_spin_unlock(&bs->used_lock);
    1994                 :            : 
    1995         [ +  + ]:     190826 :         if (blob->active.num_clusters == 0) {
    1996                 :      16327 :                 free(blob->active.clusters);
    1997                 :      16327 :                 blob->active.clusters = NULL;
    1998                 :      16327 :                 blob->active.cluster_array_size = 0;
    1999         [ +  + ]:     174499 :         } else if (blob->active.num_clusters != blob->active.cluster_array_size) {
    2000                 :            : #ifndef __clang_analyzer__
    2001                 :            :                 void *tmp;
    2002                 :            : 
    2003                 :            :                 /* scan-build really can't figure reallocs, workaround it */
    2004                 :         87 :                 tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * blob->active.num_clusters);
    2005         [ -  + ]:         87 :                 assert(tmp != NULL);
    2006                 :         87 :                 blob->active.clusters = tmp;
    2007                 :            : 
    2008                 :            : #endif
    2009                 :         87 :                 blob->active.cluster_array_size = blob->active.num_clusters;
    2010                 :            :         }
    2011                 :            : 
    2012                 :            :         /* Move on to clearing extent pages */
    2013                 :     190826 :         blob_persist_clear_extents(seq, ctx);
    2014                 :            : }
    2015                 :            : 
    2016                 :            : static void
    2017                 :     190826 : blob_persist_clear_clusters(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
    2018                 :            : {
    2019                 :     190826 :         struct spdk_blob                *blob = ctx->blob;
    2020                 :     190826 :         struct spdk_blob_store          *bs = blob->bs;
    2021                 :            :         spdk_bs_batch_t                 *batch;
    2022                 :            :         size_t                          i;
    2023                 :            :         uint64_t                        lba;
    2024                 :            :         uint64_t                        lba_count;
    2025                 :            : 
    2026                 :            :         /* Clusters don't move around in blobs. The list shrinks or grows
    2027                 :            :          * at the end, but no changes ever occur in the middle of the list.
    2028                 :            :          */
    2029                 :            : 
    2030                 :     190826 :         batch = bs_sequence_to_batch(seq, blob_persist_clear_clusters_cpl, ctx);
    2031                 :            : 
    2032                 :            :         /* Clear all clusters that were truncated */
    2033                 :     190826 :         lba = 0;
    2034                 :     190826 :         lba_count = 0;
    2035         [ +  + ]:    9486842 :         for (i = blob->active.num_clusters; i < blob->active.cluster_array_size; i++) {
    2036                 :    9296023 :                 uint64_t next_lba = blob->active.clusters[i];
    2037                 :    9296023 :                 uint64_t next_lba_count = bs_cluster_to_lba(bs, 1);
    2038                 :            : 
    2039   [ +  +  +  + ]:    9296023 :                 if (next_lba > 0 && (lba + lba_count) == next_lba) {
    2040                 :            :                         /* This cluster is contiguous with the previous one. */
    2041                 :    2291300 :                         lba_count += next_lba_count;
    2042                 :    2291300 :                         continue;
    2043         [ +  + ]:    7004721 :                 } else if (next_lba == 0) {
    2044                 :    6959075 :                         continue;
    2045                 :            :                 }
    2046                 :            : 
    2047                 :            :                 /* This cluster is not contiguous with the previous one. */
    2048                 :            : 
    2049                 :            :                 /* If a run of LBAs previously existing, clear them now */
    2050         [ +  + ]:      45640 :                 if (lba_count > 0) {
    2051                 :      36836 :                         bs_batch_clear_dev(ctx->blob, batch, lba, lba_count);
    2052                 :            :                 }
    2053                 :            : 
    2054                 :            :                 /* Start building the next batch */
    2055                 :      45640 :                 lba = next_lba;
    2056         [ +  - ]:      45640 :                 if (next_lba > 0) {
    2057                 :      45640 :                         lba_count = next_lba_count;
    2058                 :            :                 } else {
    2059                 :          0 :                         lba_count = 0;
    2060                 :            :                 }
    2061                 :            :         }
    2062                 :            : 
    2063                 :            :         /* If we ended with a contiguous set of LBAs, clear them now */
    2064         [ +  + ]:     190826 :         if (lba_count > 0) {
    2065                 :       8804 :                 bs_batch_clear_dev(ctx->blob, batch, lba, lba_count);
    2066                 :            :         }
    2067                 :            : 
    2068                 :     190826 :         bs_batch_close(batch);
    2069                 :     190826 : }
    2070                 :            : 
    2071                 :            : static void
    2072                 :     190850 : blob_persist_zero_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2073                 :            : {
    2074                 :     190850 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    2075                 :     190850 :         struct spdk_blob                *blob = ctx->blob;
    2076                 :     190850 :         struct spdk_blob_store          *bs = blob->bs;
    2077                 :            :         size_t                          i;
    2078                 :            : 
    2079         [ +  + ]:     190850 :         if (bserrno != 0) {
    2080                 :         24 :                 blob_persist_complete(seq, ctx, bserrno);
    2081                 :         24 :                 return;
    2082                 :            :         }
    2083                 :            : 
    2084                 :     190826 :         spdk_spin_lock(&bs->used_lock);
    2085                 :            : 
    2086                 :            :         /* This loop starts at 1 because the first page is special and handled
    2087                 :            :          * below. The pages (except the first) are never written in place,
    2088                 :            :          * so any pages in the clean list must be zeroed.
    2089                 :            :          */
    2090         [ +  + ]:     191839 :         for (i = 1; i < blob->clean.num_pages; i++) {
    2091                 :       1013 :                 bs_release_md_page(bs, blob->clean.pages[i]);
    2092                 :            :         }
    2093                 :            : 
    2094         [ +  + ]:     190826 :         if (blob->active.num_pages == 0) {
    2095                 :            :                 uint32_t page_num;
    2096                 :            : 
    2097                 :      10546 :                 page_num = bs_blobid_to_page(blob->id);
    2098                 :      10546 :                 bs_release_md_page(bs, page_num);
    2099                 :            :         }
    2100                 :            : 
    2101                 :     190826 :         spdk_spin_unlock(&bs->used_lock);
    2102                 :            : 
    2103                 :            :         /* Move on to clearing clusters */
    2104                 :     190826 :         blob_persist_clear_clusters(seq, ctx);
    2105                 :            : }
    2106                 :            : 
    2107                 :            : static void
    2108                 :     191090 : blob_persist_zero_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2109                 :            : {
    2110                 :     191090 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    2111                 :     191090 :         struct spdk_blob                *blob = ctx->blob;
    2112                 :     191090 :         struct spdk_blob_store          *bs = blob->bs;
    2113                 :            :         uint64_t                        lba;
    2114                 :            :         uint64_t                        lba_count;
    2115                 :            :         spdk_bs_batch_t                 *batch;
    2116                 :            :         size_t                          i;
    2117                 :            : 
    2118         [ +  + ]:     191090 :         if (bserrno != 0) {
    2119                 :        240 :                 blob_persist_complete(seq, ctx, bserrno);
    2120                 :        240 :                 return;
    2121                 :            :         }
    2122                 :            : 
    2123                 :     190850 :         batch = bs_sequence_to_batch(seq, blob_persist_zero_pages_cpl, ctx);
    2124                 :            : 
    2125                 :     190850 :         lba_count = bs_byte_to_lba(bs, SPDK_BS_PAGE_SIZE);
    2126                 :            : 
    2127                 :            :         /* This loop starts at 1 because the first page is special and handled
    2128                 :            :          * below. The pages (except the first) are never written in place,
    2129                 :            :          * so any pages in the clean list must be zeroed.
    2130                 :            :          */
    2131         [ +  + ]:     191863 :         for (i = 1; i < blob->clean.num_pages; i++) {
    2132                 :       1013 :                 lba = bs_md_page_to_lba(bs, blob->clean.pages[i]);
    2133                 :            : 
    2134                 :       1013 :                 bs_batch_write_zeroes_dev(batch, lba, lba_count);
    2135                 :            :         }
    2136                 :            : 
    2137                 :            :         /* The first page will only be zeroed if this is a delete. */
    2138         [ +  + ]:     190850 :         if (blob->active.num_pages == 0) {
    2139                 :            :                 uint32_t page_num;
    2140                 :            : 
    2141                 :            :                 /* The first page in the metadata goes where the blobid indicates */
    2142                 :      10570 :                 page_num = bs_blobid_to_page(blob->id);
    2143                 :      10570 :                 lba = bs_md_page_to_lba(bs, page_num);
    2144                 :            : 
    2145                 :      10570 :                 bs_batch_write_zeroes_dev(batch, lba, lba_count);
    2146                 :            :         }
    2147                 :            : 
    2148                 :     190850 :         bs_batch_close(batch);
    2149                 :            : }
    2150                 :            : 
    2151                 :            : static void
    2152                 :     180520 : blob_persist_write_page_root(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2153                 :            : {
    2154                 :     180520 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    2155                 :     180520 :         struct spdk_blob                *blob = ctx->blob;
    2156                 :     180520 :         struct spdk_blob_store          *bs = blob->bs;
    2157                 :            :         uint64_t                        lba;
    2158                 :            :         uint32_t                        lba_count;
    2159                 :            :         struct spdk_blob_md_page        *page;
    2160                 :            : 
    2161         [ -  + ]:     180520 :         if (bserrno != 0) {
    2162                 :          0 :                 blob_persist_complete(seq, ctx, bserrno);
    2163                 :          0 :                 return;
    2164                 :            :         }
    2165                 :            : 
    2166         [ -  + ]:     180520 :         if (blob->active.num_pages == 0) {
    2167                 :            :                 /* Move on to the next step */
    2168                 :          0 :                 blob_persist_zero_pages(seq, ctx, 0);
    2169                 :          0 :                 return;
    2170                 :            :         }
    2171                 :            : 
    2172                 :     180520 :         lba_count = bs_byte_to_lba(bs, sizeof(*page));
    2173                 :            : 
    2174                 :     180520 :         page = &ctx->pages[0];
    2175                 :            :         /* The first page in the metadata goes where the blobid indicates */
    2176                 :     180520 :         lba = bs_md_page_to_lba(bs, bs_blobid_to_page(blob->id));
    2177                 :            : 
    2178                 :     180520 :         bs_sequence_write_dev(seq, page, lba, lba_count,
    2179                 :            :                               blob_persist_zero_pages, ctx);
    2180                 :            : }
    2181                 :            : 
    2182                 :            : static void
    2183                 :     180520 : blob_persist_write_page_chain(spdk_bs_sequence_t *seq, struct spdk_blob_persist_ctx *ctx)
    2184                 :            : {
    2185                 :     180520 :         struct spdk_blob                *blob = ctx->blob;
    2186                 :     180520 :         struct spdk_blob_store          *bs = blob->bs;
    2187                 :            :         uint64_t                        lba;
    2188                 :            :         uint32_t                        lba_count;
    2189                 :            :         struct spdk_blob_md_page        *page;
    2190                 :            :         spdk_bs_batch_t                 *batch;
    2191                 :            :         size_t                          i;
    2192                 :            : 
    2193                 :            :         /* Clusters don't move around in blobs. The list shrinks or grows
    2194                 :            :          * at the end, but no changes ever occur in the middle of the list.
    2195                 :            :          */
    2196                 :            : 
    2197                 :     180520 :         lba_count = bs_byte_to_lba(bs, sizeof(*page));
    2198                 :            : 
    2199                 :     180520 :         batch = bs_sequence_to_batch(seq, blob_persist_write_page_root, ctx);
    2200                 :            : 
    2201                 :            :         /* This starts at 1. The root page is not written until
    2202                 :            :          * all of the others are finished
    2203                 :            :          */
    2204         [ +  + ]:     181653 :         for (i = 1; i < blob->active.num_pages; i++) {
    2205                 :       1133 :                 page = &ctx->pages[i];
    2206         [ -  + ]:       1133 :                 assert(page->sequence_num == i);
    2207                 :            : 
    2208                 :       1133 :                 lba = bs_md_page_to_lba(bs, blob->active.pages[i]);
    2209                 :            : 
    2210                 :       1133 :                 bs_batch_write_dev(batch, page, lba, lba_count);
    2211                 :            :         }
    2212                 :            : 
    2213                 :     180520 :         bs_batch_close(batch);
    2214                 :     180520 : }
    2215                 :            : 
    2216                 :            : static int
    2217                 :     141620 : blob_resize(struct spdk_blob *blob, uint64_t sz)
    2218                 :            : {
    2219                 :            :         uint64_t        i;
    2220                 :            :         uint64_t        *tmp;
    2221                 :     137504 :         uint64_t        cluster;
    2222                 :     137504 :         uint32_t        lfmd; /*  lowest free md page */
    2223                 :            :         uint64_t        num_clusters;
    2224                 :            :         uint32_t        *ep_tmp;
    2225                 :     141620 :         uint64_t        new_num_ep = 0, current_num_ep = 0;
    2226                 :            :         struct spdk_blob_store *bs;
    2227                 :            :         int             rc;
    2228                 :            : 
    2229                 :     141620 :         bs = blob->bs;
    2230                 :            : 
    2231                 :     141620 :         blob_verify_md_op(blob);
    2232                 :            : 
    2233         [ +  + ]:     141620 :         if (blob->active.num_clusters == sz) {
    2234                 :       5715 :                 return 0;
    2235                 :            :         }
    2236                 :            : 
    2237         [ -  + ]:     135905 :         if (blob->active.num_clusters < blob->active.cluster_array_size) {
    2238                 :            :                 /* If this blob was resized to be larger, then smaller, then
    2239                 :            :                  * larger without syncing, then the cluster array already
    2240                 :            :                  * contains spare assigned clusters we can use.
    2241                 :            :                  */
    2242                 :          0 :                 num_clusters = spdk_min(blob->active.cluster_array_size,
    2243                 :            :                                         sz);
    2244                 :            :         } else {
    2245                 :     135905 :                 num_clusters = blob->active.num_clusters;
    2246                 :            :         }
    2247                 :            : 
    2248   [ +  +  +  + ]:     135905 :         if (blob->use_extent_table) {
    2249                 :            :                 /* Round up since every cluster beyond current Extent Table size,
    2250                 :            :                  * requires new extent page. */
    2251                 :     126701 :                 new_num_ep = spdk_divide_round_up(sz, SPDK_EXTENTS_PER_EP);
    2252                 :     126701 :                 current_num_ep = spdk_divide_round_up(num_clusters, SPDK_EXTENTS_PER_EP);
    2253                 :            :         }
    2254                 :            : 
    2255         [ -  + ]:     135905 :         assert(!spdk_spin_held(&bs->used_lock));
    2256                 :            : 
    2257                 :            :         /* Check first that we have enough clusters and md pages before we start claiming them.
    2258                 :            :          * bs->used_lock is held to ensure that clusters we think are free are still free when we go
    2259                 :            :          * to claim them later in this function.
    2260                 :            :          */
    2261   [ +  +  +  + ]:     135905 :         if (sz > num_clusters && spdk_blob_is_thin_provisioned(blob) == false) {
    2262                 :     123401 :                 spdk_spin_lock(&bs->used_lock);
    2263         [ +  + ]:     123401 :                 if ((sz - num_clusters) > bs->num_free_clusters) {
    2264                 :         51 :                         rc = -ENOSPC;
    2265                 :         51 :                         goto out;
    2266                 :            :                 }
    2267                 :     123350 :                 lfmd = 0;
    2268         [ +  + ]:     134438 :                 for (i = current_num_ep; i < new_num_ep ; i++) {
    2269                 :      11088 :                         lfmd = spdk_bit_array_find_first_clear(blob->bs->used_md_pages, lfmd);
    2270         [ -  + ]:      11088 :                         if (lfmd == UINT32_MAX) {
    2271                 :            :                                 /* No more free md pages. Cannot satisfy the request */
    2272                 :          0 :                                 rc = -ENOSPC;
    2273                 :          0 :                                 goto out;
    2274                 :            :                         }
    2275                 :            :                 }
    2276                 :            :         }
    2277                 :            : 
    2278         [ +  + ]:     135854 :         if (sz > num_clusters) {
    2279                 :            :                 /* Expand the cluster array if necessary.
    2280                 :            :                  * We only shrink the array when persisting.
    2281                 :            :                  */
    2282                 :     125921 :                 tmp = realloc(blob->active.clusters, sizeof(*blob->active.clusters) * sz);
    2283   [ +  -  -  + ]:     125921 :                 if (sz > 0 && tmp == NULL) {
    2284                 :          0 :                         rc = -ENOMEM;
    2285                 :          0 :                         goto out;
    2286                 :            :                 }
    2287         [ -  + ]:     125921 :                 memset(tmp + blob->active.cluster_array_size, 0,
    2288                 :     125921 :                        sizeof(*blob->active.clusters) * (sz - blob->active.cluster_array_size));
    2289                 :     125921 :                 blob->active.clusters = tmp;
    2290                 :     125921 :                 blob->active.cluster_array_size = sz;
    2291                 :            : 
    2292                 :            :                 /* Expand the extents table, only if enough clusters were added */
    2293   [ +  +  +  +  :     125921 :                 if (new_num_ep > current_num_ep && blob->use_extent_table) {
                   +  - ]
    2294                 :       8244 :                         ep_tmp = realloc(blob->active.extent_pages, sizeof(*blob->active.extent_pages) * new_num_ep);
    2295   [ +  -  -  + ]:       8244 :                         if (new_num_ep > 0 && ep_tmp == NULL) {
    2296                 :          0 :                                 rc = -ENOMEM;
    2297                 :          0 :                                 goto out;
    2298                 :            :                         }
    2299         [ -  + ]:       8244 :                         memset(ep_tmp + blob->active.extent_pages_array_size, 0,
    2300                 :       8244 :                                sizeof(*blob->active.extent_pages) * (new_num_ep - blob->active.extent_pages_array_size));
    2301                 :       8244 :                         blob->active.extent_pages = ep_tmp;
    2302                 :       8244 :                         blob->active.extent_pages_array_size = new_num_ep;
    2303                 :            :                 }
    2304                 :            :         }
    2305                 :            : 
    2306                 :     135854 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    2307                 :            : 
    2308         [ +  + ]:     135854 :         if (spdk_blob_is_thin_provisioned(blob) == false) {
    2309                 :     131577 :                 cluster = 0;
    2310                 :     131577 :                 lfmd = 0;
    2311         [ +  + ]:    2593151 :                 for (i = num_clusters; i < sz; i++) {
    2312                 :    2461574 :                         bs_allocate_cluster(blob, i, &cluster, &lfmd, true);
    2313                 :            :                         /* Do not increment lfmd here.  lfmd will get updated
    2314                 :            :                          * to the md_page allocated (if any) when a new extent
    2315                 :            :                          * page is needed.  Just pass that value again,
    2316                 :            :                          * bs_allocate_cluster will just start at that index
    2317                 :            :                          * to find the next free md_page when needed.
    2318                 :            :                          */
    2319                 :            :                 }
    2320                 :            :         }
    2321                 :            : 
    2322                 :            :         /* If we are shrinking the blob, we must adjust num_allocated_clusters */
    2323         [ +  + ]:    9432117 :         for (i = sz; i < num_clusters; i++) {
    2324         [ +  + ]:    9296263 :                 if (blob->active.clusters[i] != 0) {
    2325                 :    2336940 :                         blob->active.num_allocated_clusters--;
    2326                 :            :                 }
    2327                 :            :         }
    2328                 :            : 
    2329                 :     135854 :         blob->active.num_clusters = sz;
    2330                 :     135854 :         blob->active.num_extent_pages = new_num_ep;
    2331                 :            : 
    2332                 :     135854 :         rc = 0;
    2333                 :     135905 : out:
    2334         [ +  + ]:     135905 :         if (spdk_spin_held(&bs->used_lock)) {
    2335                 :     123401 :                 spdk_spin_unlock(&bs->used_lock);
    2336                 :            :         }
    2337                 :            : 
    2338                 :     135905 :         return rc;
    2339                 :            : }
    2340                 :            : 
    2341                 :            : static void
    2342                 :     180520 : blob_persist_generate_new_md(struct spdk_blob_persist_ctx *ctx)
    2343                 :            : {
    2344                 :     180520 :         spdk_bs_sequence_t *seq = ctx->seq;
    2345                 :     180520 :         struct spdk_blob *blob = ctx->blob;
    2346                 :     180520 :         struct spdk_blob_store *bs = blob->bs;
    2347                 :            :         uint64_t i;
    2348                 :            :         uint32_t page_num;
    2349                 :            :         void *tmp;
    2350                 :            :         int rc;
    2351                 :            : 
    2352                 :            :         /* Generate the new metadata */
    2353                 :     180520 :         rc = blob_serialize(blob, &ctx->pages, &blob->active.num_pages);
    2354         [ -  + ]:     180520 :         if (rc < 0) {
    2355                 :          0 :                 blob_persist_complete(seq, ctx, rc);
    2356                 :          0 :                 return;
    2357                 :            :         }
    2358                 :            : 
    2359         [ -  + ]:     180520 :         assert(blob->active.num_pages >= 1);
    2360                 :            : 
    2361                 :            :         /* Resize the cache of page indices */
    2362                 :     180520 :         tmp = realloc(blob->active.pages, blob->active.num_pages * sizeof(*blob->active.pages));
    2363         [ -  + ]:     180520 :         if (!tmp) {
    2364                 :          0 :                 blob_persist_complete(seq, ctx, -ENOMEM);
    2365                 :          0 :                 return;
    2366                 :            :         }
    2367                 :     180520 :         blob->active.pages = tmp;
    2368                 :            : 
    2369                 :            :         /* Assign this metadata to pages. This requires two passes - one to verify that there are
    2370                 :            :          * enough pages and a second to actually claim them. The used_lock is held across
    2371                 :            :          * both passes to ensure things don't change in the middle.
    2372                 :            :          */
    2373                 :     180520 :         spdk_spin_lock(&bs->used_lock);
    2374                 :     180520 :         page_num = 0;
    2375                 :            :         /* Note that this loop starts at one. The first page location is fixed by the blobid. */
    2376         [ +  + ]:     181653 :         for (i = 1; i < blob->active.num_pages; i++) {
    2377                 :       1133 :                 page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
    2378         [ -  + ]:       1133 :                 if (page_num == UINT32_MAX) {
    2379                 :          0 :                         spdk_spin_unlock(&bs->used_lock);
    2380                 :          0 :                         blob_persist_complete(seq, ctx, -ENOMEM);
    2381                 :          0 :                         return;
    2382                 :            :                 }
    2383                 :       1133 :                 page_num++;
    2384                 :            :         }
    2385                 :            : 
    2386                 :     180520 :         page_num = 0;
    2387                 :     180520 :         blob->active.pages[0] = bs_blobid_to_page(blob->id);
    2388         [ +  + ]:     181653 :         for (i = 1; i < blob->active.num_pages; i++) {
    2389                 :       1133 :                 page_num = spdk_bit_array_find_first_clear(bs->used_md_pages, page_num);
    2390                 :       1133 :                 ctx->pages[i - 1].next = page_num;
    2391                 :            :                 /* Now that previous metadata page is complete, calculate the crc for it. */
    2392                 :       1133 :                 ctx->pages[i - 1].crc = blob_md_page_calc_crc(&ctx->pages[i - 1]);
    2393                 :       1133 :                 blob->active.pages[i] = page_num;
    2394                 :       1133 :                 bs_claim_md_page(bs, page_num);
    2395   [ -  +  -  + ]:       1133 :                 SPDK_DEBUGLOG(blob, "Claiming page %u for blob 0x%" PRIx64 "\n", page_num,
    2396                 :            :                               blob->id);
    2397                 :       1133 :                 page_num++;
    2398                 :            :         }
    2399                 :     180520 :         spdk_spin_unlock(&bs->used_lock);
    2400                 :     180520 :         ctx->pages[i - 1].crc = blob_md_page_calc_crc(&ctx->pages[i - 1]);
    2401                 :            :         /* Start writing the metadata from last page to first */
    2402                 :     180520 :         blob->state = SPDK_BLOB_STATE_CLEAN;
    2403                 :     180520 :         blob_persist_write_page_chain(seq, ctx);
    2404                 :            : }
    2405                 :            : 
    2406                 :            : static void
    2407                 :     249581 : blob_persist_write_extent_pages(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2408                 :            : {
    2409                 :     249581 :         struct spdk_blob_persist_ctx    *ctx = cb_arg;
    2410                 :     249581 :         struct spdk_blob                *blob = ctx->blob;
    2411                 :            :         size_t                          i;
    2412                 :            :         uint32_t                        extent_page_id;
    2413                 :     249581 :         uint32_t                        page_count = 0;
    2414                 :            :         int                             rc;
    2415                 :            : 
    2416         [ +  + ]:     249581 :         if (ctx->extent_page != NULL) {
    2417                 :     123739 :                 spdk_free(ctx->extent_page);
    2418                 :     123739 :                 ctx->extent_page = NULL;
    2419                 :            :         }
    2420                 :            : 
    2421         [ -  + ]:     249581 :         if (bserrno != 0) {
    2422                 :          0 :                 blob_persist_complete(seq, ctx, bserrno);
    2423                 :       2408 :                 return;
    2424                 :            :         }
    2425                 :            : 
    2426                 :            :         /* Only write out Extent Pages when blob was resized. */
    2427         [ +  + ]:     264378 :         for (i = ctx->next_extent_page; i < blob->active.extent_pages_array_size; i++) {
    2428                 :     138536 :                 extent_page_id = blob->active.extent_pages[i];
    2429         [ +  + ]:     138536 :                 if (extent_page_id == 0) {
    2430                 :            :                         /* No Extent Page to persist */
    2431         [ -  + ]:      14797 :                         assert(spdk_blob_is_thin_provisioned(blob));
    2432                 :      14797 :                         continue;
    2433                 :            :                 }
    2434         [ -  + ]:     123739 :                 assert(spdk_bit_array_get(blob->bs->used_md_pages, extent_page_id));
    2435                 :     123739 :                 ctx->next_extent_page = i + 1;
    2436                 :     123739 :                 rc = blob_serialize_add_page(ctx->blob, &ctx->extent_page, &page_count, &ctx->extent_page);
    2437         [ -  + ]:     123739 :                 if (rc < 0) {
    2438                 :          0 :                         blob_persist_complete(seq, ctx, rc);
    2439                 :          0 :                         return;
    2440                 :            :                 }
    2441                 :            : 
    2442                 :     123739 :                 blob->state = SPDK_BLOB_STATE_DIRTY;
    2443                 :     123739 :                 blob_serialize_extent_page(blob, i * SPDK_EXTENTS_PER_EP, ctx->extent_page);
    2444                 :            : 
    2445                 :     123739 :                 ctx->extent_page->crc = blob_md_page_calc_crc(ctx->extent_page);
    2446                 :            : 
    2447                 :     123739 :                 bs_sequence_write_dev(seq, ctx->extent_page, bs_md_page_to_lba(blob->bs, extent_page_id),
    2448                 :     123739 :                                       bs_byte_to_lba(blob->bs, SPDK_BS_PAGE_SIZE),
    2449                 :            :                                       blob_persist_write_extent_pages, ctx);
    2450                 :     123739 :                 return;
    2451                 :            :         }
    2452                 :            : 
    2453                 :     125842 :         blob_persist_generate_new_md(ctx);
    2454                 :            : }
    2455                 :            : 
    2456                 :            : static void
    2457                 :     191138 : blob_persist_start(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2458                 :            : {
    2459                 :     191138 :         struct spdk_blob_persist_ctx *ctx = cb_arg;
    2460                 :     191138 :         struct spdk_blob *blob = ctx->blob;
    2461                 :            : 
    2462         [ +  + ]:     191138 :         if (bserrno != 0) {
    2463                 :         48 :                 blob_persist_complete(seq, ctx, bserrno);
    2464                 :         48 :                 return;
    2465                 :            :         }
    2466                 :            : 
    2467         [ +  + ]:     191090 :         if (blob->active.num_pages == 0) {
    2468                 :            :                 /* This is the signal that the blob should be deleted.
    2469                 :            :                  * Immediately jump to the clean up routine. */
    2470         [ -  + ]:      10570 :                 assert(blob->clean.num_pages > 0);
    2471                 :      10570 :                 blob->state = SPDK_BLOB_STATE_CLEAN;
    2472                 :      10570 :                 blob_persist_zero_pages(seq, ctx, 0);
    2473                 :      10570 :                 return;
    2474                 :            : 
    2475                 :            :         }
    2476                 :            : 
    2477         [ +  + ]:     180520 :         if (blob->clean.num_clusters < blob->active.num_clusters) {
    2478                 :            :                 /* Blob was resized up */
    2479         [ -  + ]:     125753 :                 assert(blob->clean.num_extent_pages <= blob->active.num_extent_pages);
    2480         [ +  + ]:     125753 :                 ctx->next_extent_page = spdk_max(1, blob->clean.num_extent_pages) - 1;
    2481         [ +  + ]:      54767 :         } else if (blob->active.num_clusters < blob->active.cluster_array_size) {
    2482                 :            :                 /* Blob was resized down */
    2483         [ -  + ]:         89 :                 assert(blob->clean.num_extent_pages >= blob->active.num_extent_pages);
    2484         [ +  + ]:         89 :                 ctx->next_extent_page = spdk_max(1, blob->active.num_extent_pages) - 1;
    2485                 :            :         } else {
    2486                 :            :                 /* No change in size occurred */
    2487                 :      54678 :                 blob_persist_generate_new_md(ctx);
    2488                 :      54678 :                 return;
    2489                 :            :         }
    2490                 :            : 
    2491                 :     125842 :         blob_persist_write_extent_pages(seq, ctx, 0);
    2492                 :            : }
    2493                 :            : 
    2494                 :            : struct spdk_bs_mark_dirty {
    2495                 :            :         struct spdk_blob_store          *bs;
    2496                 :            :         struct spdk_bs_super_block      *super;
    2497                 :            :         spdk_bs_sequence_cpl            cb_fn;
    2498                 :            :         void                            *cb_arg;
    2499                 :            : };
    2500                 :            : 
    2501                 :            : static void
    2502                 :        995 : bs_mark_dirty_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2503                 :            : {
    2504                 :        995 :         struct spdk_bs_mark_dirty *ctx = cb_arg;
    2505                 :            : 
    2506         [ +  + ]:        995 :         if (bserrno == 0) {
    2507                 :        947 :                 ctx->bs->clean = 0;
    2508                 :            :         }
    2509                 :            : 
    2510                 :        995 :         ctx->cb_fn(seq, ctx->cb_arg, bserrno);
    2511                 :            : 
    2512                 :        995 :         spdk_free(ctx->super);
    2513                 :        995 :         free(ctx);
    2514                 :        995 : }
    2515                 :            : 
    2516                 :            : static void bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
    2517                 :            :                            struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg);
    2518                 :            : 
    2519                 :            : 
    2520                 :            : static void
    2521                 :        995 : bs_mark_dirty_write(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2522                 :            : {
    2523                 :        995 :         struct spdk_bs_mark_dirty *ctx = cb_arg;
    2524                 :            :         int rc;
    2525                 :            : 
    2526         [ +  + ]:        995 :         if (bserrno != 0) {
    2527                 :         24 :                 bs_mark_dirty_write_cpl(seq, ctx, bserrno);
    2528                 :         24 :                 return;
    2529                 :            :         }
    2530                 :            : 
    2531                 :        971 :         rc = bs_super_validate(ctx->super, ctx->bs);
    2532         [ -  + ]:        971 :         if (rc != 0) {
    2533                 :          0 :                 bs_mark_dirty_write_cpl(seq, ctx, rc);
    2534                 :          0 :                 return;
    2535                 :            :         }
    2536                 :            : 
    2537                 :        971 :         ctx->super->clean = 0;
    2538         [ +  + ]:        971 :         if (ctx->super->size == 0) {
    2539                 :         24 :                 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    2540                 :            :         }
    2541                 :            : 
    2542                 :        971 :         bs_write_super(seq, ctx->bs, ctx->super, bs_mark_dirty_write_cpl, ctx);
    2543                 :            : }
    2544                 :            : 
    2545                 :            : static void
    2546                 :     228608 : bs_mark_dirty(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
    2547                 :            :               spdk_bs_sequence_cpl cb_fn, void *cb_arg)
    2548                 :            : {
    2549                 :            :         struct spdk_bs_mark_dirty *ctx;
    2550                 :            : 
    2551                 :            :         /* Blobstore is already marked dirty */
    2552   [ +  +  +  + ]:     228608 :         if (bs->clean == 0) {
    2553                 :     227613 :                 cb_fn(seq, cb_arg, 0);
    2554                 :     227613 :                 return;
    2555                 :            :         }
    2556                 :            : 
    2557                 :        995 :         ctx = calloc(1, sizeof(*ctx));
    2558         [ -  + ]:        995 :         if (!ctx) {
    2559                 :          0 :                 cb_fn(seq, cb_arg, -ENOMEM);
    2560                 :          0 :                 return;
    2561                 :            :         }
    2562                 :        995 :         ctx->bs = bs;
    2563                 :        995 :         ctx->cb_fn = cb_fn;
    2564                 :        995 :         ctx->cb_arg = cb_arg;
    2565                 :            : 
    2566                 :        995 :         ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
    2567                 :            :                                   SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    2568         [ -  + ]:        995 :         if (!ctx->super) {
    2569                 :          0 :                 free(ctx);
    2570                 :          0 :                 cb_fn(seq, cb_arg, -ENOMEM);
    2571                 :          0 :                 return;
    2572                 :            :         }
    2573                 :            : 
    2574                 :        995 :         bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0),
    2575                 :        995 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    2576                 :            :                              bs_mark_dirty_write, ctx);
    2577                 :            : }
    2578                 :            : 
    2579                 :            : /* Write a blob to disk */
    2580                 :            : static void
    2581                 :     234027 : blob_persist(spdk_bs_sequence_t *seq, struct spdk_blob *blob,
    2582                 :            :              spdk_bs_sequence_cpl cb_fn, void *cb_arg)
    2583                 :            : {
    2584                 :            :         struct spdk_blob_persist_ctx *ctx;
    2585                 :            : 
    2586                 :     234027 :         blob_verify_md_op(blob);
    2587                 :            : 
    2588   [ +  +  +  + ]:     234027 :         if (blob->state == SPDK_BLOB_STATE_CLEAN && TAILQ_EMPTY(&blob->persists_to_complete)) {
    2589                 :      42889 :                 cb_fn(seq, cb_arg, 0);
    2590                 :      42889 :                 return;
    2591                 :            :         }
    2592                 :            : 
    2593                 :     191138 :         ctx = calloc(1, sizeof(*ctx));
    2594         [ -  + ]:     191138 :         if (!ctx) {
    2595                 :          0 :                 cb_fn(seq, cb_arg, -ENOMEM);
    2596                 :          0 :                 return;
    2597                 :            :         }
    2598                 :     191138 :         ctx->blob = blob;
    2599                 :     191138 :         ctx->seq = seq;
    2600                 :     191138 :         ctx->cb_fn = cb_fn;
    2601                 :     191138 :         ctx->cb_arg = cb_arg;
    2602                 :            : 
    2603                 :            :         /* Multiple blob persists can affect one another, via blob->state or
    2604                 :            :          * blob mutable data changes. To prevent it, queue up the persists. */
    2605         [ +  + ]:     191138 :         if (!TAILQ_EMPTY(&blob->persists_to_complete)) {
    2606                 :        139 :                 TAILQ_INSERT_TAIL(&blob->pending_persists, ctx, link);
    2607                 :        139 :                 return;
    2608                 :            :         }
    2609         [ -  + ]:     190999 :         TAILQ_INSERT_HEAD(&blob->persists_to_complete, ctx, link);
    2610                 :            : 
    2611                 :     190999 :         bs_mark_dirty(seq, blob->bs, blob_persist_start, ctx);
    2612                 :            : }
    2613                 :            : 
    2614                 :            : struct spdk_blob_copy_cluster_ctx {
    2615                 :            :         struct spdk_blob *blob;
    2616                 :            :         uint8_t *buf;
    2617                 :            :         uint64_t page;
    2618                 :            :         uint64_t new_cluster;
    2619                 :            :         uint32_t new_extent_page;
    2620                 :            :         spdk_bs_sequence_t *seq;
    2621                 :            :         struct spdk_blob_md_page *new_cluster_page;
    2622                 :            : };
    2623                 :            : 
    2624                 :            : struct spdk_blob_free_cluster_ctx {
    2625                 :            :         struct spdk_blob *blob;
    2626                 :            :         uint64_t page;
    2627                 :            :         struct spdk_blob_md_page *md_page;
    2628                 :            :         uint64_t cluster_num;
    2629                 :            :         uint32_t extent_page;
    2630                 :            :         spdk_bs_sequence_t *seq;
    2631                 :            : };
    2632                 :            : 
    2633                 :            : static void
    2634                 :      39747 : blob_allocate_and_copy_cluster_cpl(void *cb_arg, int bserrno)
    2635                 :            : {
    2636                 :      39747 :         struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
    2637                 :      39747 :         struct spdk_bs_request_set *set = (struct spdk_bs_request_set *)ctx->seq;
    2638                 :      38775 :         TAILQ_HEAD(, spdk_bs_request_set) requests;
    2639                 :            :         spdk_bs_user_op_t *op;
    2640                 :            : 
    2641                 :      39747 :         TAILQ_INIT(&requests);
    2642   [ -  +  +  - ]:      39747 :         TAILQ_SWAP(&set->channel->need_cluster_alloc, &requests, spdk_bs_request_set, link);
    2643                 :            : 
    2644         [ +  + ]:     197705 :         while (!TAILQ_EMPTY(&requests)) {
    2645                 :     157958 :                 op = TAILQ_FIRST(&requests);
    2646         [ +  + ]:     157958 :                 TAILQ_REMOVE(&requests, op, link);
    2647         [ +  - ]:     157958 :                 if (bserrno == 0) {
    2648                 :     157958 :                         bs_user_op_execute(op);
    2649                 :            :                 } else {
    2650                 :          0 :                         bs_user_op_abort(op, bserrno);
    2651                 :            :                 }
    2652                 :            :         }
    2653                 :            : 
    2654                 :      39747 :         spdk_free(ctx->buf);
    2655                 :      39747 :         free(ctx);
    2656                 :      39747 : }
    2657                 :            : 
    2658                 :            : static void
    2659                 :        360 : blob_free_cluster_cpl(void *cb_arg, int bserrno)
    2660                 :            : {
    2661                 :        360 :         struct spdk_blob_free_cluster_ctx *ctx = cb_arg;
    2662                 :        360 :         spdk_bs_sequence_t *seq = ctx->seq;
    2663                 :            : 
    2664                 :        360 :         bs_sequence_finish(seq, bserrno);
    2665                 :            : 
    2666                 :        360 :         free(ctx);
    2667                 :        360 : }
    2668                 :            : 
    2669                 :            : static void
    2670                 :         37 : blob_insert_cluster_revert(struct spdk_blob_copy_cluster_ctx *ctx)
    2671                 :            : {
    2672                 :         37 :         spdk_spin_lock(&ctx->blob->bs->used_lock);
    2673                 :         37 :         bs_release_cluster(ctx->blob->bs, ctx->new_cluster);
    2674         [ +  + ]:         37 :         if (ctx->new_extent_page != 0) {
    2675                 :         13 :                 bs_release_md_page(ctx->blob->bs, ctx->new_extent_page);
    2676                 :            :         }
    2677                 :         37 :         spdk_spin_unlock(&ctx->blob->bs->used_lock);
    2678                 :         37 : }
    2679                 :            : 
    2680                 :            : static void
    2681                 :         37 : blob_insert_cluster_clear_cpl(void *cb_arg, int bserrno)
    2682                 :            : {
    2683                 :         37 :         struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
    2684                 :            : 
    2685         [ -  + ]:         37 :         if (bserrno) {
    2686                 :          0 :                 SPDK_WARNLOG("Failed to clear cluster: %d\n", bserrno);
    2687                 :            :         }
    2688                 :            : 
    2689                 :         37 :         blob_insert_cluster_revert(ctx);
    2690                 :         37 :         bs_sequence_finish(ctx->seq, bserrno);
    2691                 :         37 : }
    2692                 :            : 
    2693                 :            : static void
    2694                 :         37 : blob_insert_cluster_clear(struct spdk_blob_copy_cluster_ctx *ctx)
    2695                 :            : {
    2696                 :         20 :         struct spdk_bs_cpl cpl;
    2697                 :            :         spdk_bs_batch_t *batch;
    2698                 :         37 :         struct spdk_io_channel *ch = spdk_io_channel_from_ctx(ctx->seq->channel);
    2699                 :            : 
    2700                 :            :         /*
    2701                 :            :          * We allocated a cluster and we copied data to it. But now, we realized that we don't need
    2702                 :            :          * this cluster and we want to release it. We must ensure that we clear the data on this
    2703                 :            :          * cluster.
    2704                 :            :          * The cluster may later be re-allocated by a thick-provisioned blob for example. When
    2705                 :            :          * reading from this thick-provisioned blob before writing data, we should read zeroes.
    2706                 :            :          */
    2707                 :            : 
    2708                 :         37 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    2709                 :         37 :         cpl.u.blob_basic.cb_fn = blob_insert_cluster_clear_cpl;
    2710                 :         37 :         cpl.u.blob_basic.cb_arg = ctx;
    2711                 :            : 
    2712                 :         37 :         batch = bs_batch_open(ch, &cpl, ctx->blob);
    2713         [ -  + ]:         37 :         if (!batch) {
    2714                 :          0 :                 blob_insert_cluster_clear_cpl(ctx, -ENOMEM);
    2715                 :          0 :                 return;
    2716                 :            :         }
    2717                 :            : 
    2718                 :         37 :         bs_batch_clear_dev(ctx->blob, batch, bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster),
    2719                 :         37 :                            bs_cluster_to_lba(ctx->blob->bs, 1));
    2720                 :         37 :         bs_batch_close(batch);
    2721                 :            : }
    2722                 :            : 
    2723                 :            : static void
    2724                 :      39747 : blob_insert_cluster_cpl(void *cb_arg, int bserrno)
    2725                 :            : {
    2726                 :      39747 :         struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
    2727                 :            : 
    2728         [ +  + ]:      39747 :         if (bserrno) {
    2729         [ +  - ]:         37 :                 if (bserrno == -EEXIST) {
    2730                 :            :                         /* The metadata insert failed because another thread
    2731                 :            :                          * allocated the cluster first. Clear and free our cluster
    2732                 :            :                          * but continue without error. */
    2733                 :         37 :                         blob_insert_cluster_clear(ctx);
    2734                 :         37 :                         return;
    2735                 :            :                 }
    2736                 :            : 
    2737                 :          0 :                 blob_insert_cluster_revert(ctx);
    2738                 :            :         }
    2739                 :            : 
    2740                 :      39710 :         bs_sequence_finish(ctx->seq, bserrno);
    2741                 :            : }
    2742                 :            : 
    2743                 :            : static void
    2744                 :       2563 : blob_write_copy_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2745                 :            : {
    2746                 :       2563 :         struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
    2747                 :            :         uint32_t cluster_number;
    2748                 :            : 
    2749         [ -  + ]:       2563 :         if (bserrno) {
    2750                 :            :                 /* The write failed, so jump to the final completion handler */
    2751                 :          0 :                 bs_sequence_finish(seq, bserrno);
    2752                 :          0 :                 return;
    2753                 :            :         }
    2754                 :            : 
    2755                 :       2563 :         cluster_number = bs_page_to_cluster(ctx->blob->bs, ctx->page);
    2756                 :            : 
    2757                 :       2563 :         blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
    2758                 :            :                                          ctx->new_extent_page, ctx->new_cluster_page, blob_insert_cluster_cpl, ctx);
    2759                 :            : }
    2760                 :            : 
    2761                 :            : static void
    2762                 :       1741 : blob_write_copy(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    2763                 :            : {
    2764                 :       1741 :         struct spdk_blob_copy_cluster_ctx *ctx = cb_arg;
    2765                 :            : 
    2766         [ -  + ]:       1741 :         if (bserrno != 0) {
    2767                 :            :                 /* The read failed, so jump to the final completion handler */
    2768                 :          0 :                 bs_sequence_finish(seq, bserrno);
    2769                 :          0 :                 return;
    2770                 :            :         }
    2771                 :            : 
    2772                 :            :         /* Write whole cluster */
    2773                 :       1741 :         bs_sequence_write_dev(seq, ctx->buf,
    2774                 :       1741 :                               bs_cluster_to_lba(ctx->blob->bs, ctx->new_cluster),
    2775                 :       1741 :                               bs_cluster_to_lba(ctx->blob->bs, 1),
    2776                 :            :                               blob_write_copy_cpl, ctx);
    2777                 :            : }
    2778                 :            : 
    2779                 :            : static bool
    2780                 :      39652 : blob_can_copy(struct spdk_blob *blob, uint32_t cluster_start_page, uint64_t *base_lba)
    2781                 :            : {
    2782                 :      39652 :         uint64_t lba = bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page);
    2783                 :            : 
    2784   [ +  +  +  +  :      43295 :         return (!blob_is_esnap_clone(blob) && blob->bs->dev->copy != NULL) &&
                   +  + ]
    2785                 :       3643 :                blob->back_bs_dev->translate_lba(blob->back_bs_dev, lba, base_lba);
    2786                 :            : }
    2787                 :            : 
    2788                 :            : static void
    2789                 :        822 : blob_copy(struct spdk_blob_copy_cluster_ctx *ctx, spdk_bs_user_op_t *op, uint64_t src_lba)
    2790                 :            : {
    2791                 :        822 :         struct spdk_blob *blob = ctx->blob;
    2792                 :        822 :         uint64_t lba_count = bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz);
    2793                 :            : 
    2794                 :        822 :         bs_sequence_copy_dev(ctx->seq,
    2795                 :        822 :                              bs_cluster_to_lba(blob->bs, ctx->new_cluster),
    2796                 :            :                              src_lba,
    2797                 :            :                              lba_count,
    2798                 :            :                              blob_write_copy_cpl, ctx);
    2799                 :        822 : }
    2800                 :            : 
    2801                 :            : static void
    2802                 :     157959 : bs_allocate_and_copy_cluster(struct spdk_blob *blob,
    2803                 :            :                              struct spdk_io_channel *_ch,
    2804                 :            :                              uint64_t io_unit, spdk_bs_user_op_t *op)
    2805                 :            : {
    2806                 :     153256 :         struct spdk_bs_cpl cpl;
    2807                 :            :         struct spdk_bs_channel *ch;
    2808                 :            :         struct spdk_blob_copy_cluster_ctx *ctx;
    2809                 :            :         uint32_t cluster_start_page;
    2810                 :            :         uint32_t cluster_number;
    2811                 :            :         bool is_zeroes;
    2812                 :            :         bool can_copy;
    2813                 :            :         bool is_valid_range;
    2814                 :     153256 :         uint64_t copy_src_lba;
    2815                 :            :         int rc;
    2816                 :            : 
    2817                 :     157959 :         ch = spdk_io_channel_get_ctx(_ch);
    2818                 :            : 
    2819         [ +  + ]:     157959 :         if (!TAILQ_EMPTY(&ch->need_cluster_alloc)) {
    2820                 :            :                 /* There are already operations pending. Queue this user op
    2821                 :            :                  * and return because it will be re-executed when the outstanding
    2822                 :            :                  * cluster allocation completes. */
    2823                 :     118211 :                 TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
    2824                 :     118211 :                 return;
    2825                 :            :         }
    2826                 :            : 
    2827                 :            :         /* Round the io_unit offset down to the first page in the cluster */
    2828                 :      39748 :         cluster_start_page = bs_io_unit_to_cluster_start(blob, io_unit);
    2829                 :            : 
    2830                 :            :         /* Calculate which index in the metadata cluster array the corresponding
    2831                 :            :          * cluster is supposed to be at. */
    2832                 :      39748 :         cluster_number = bs_io_unit_to_cluster_number(blob, io_unit);
    2833                 :            : 
    2834                 :      39748 :         ctx = calloc(1, sizeof(*ctx));
    2835         [ -  + ]:      39748 :         if (!ctx) {
    2836                 :          0 :                 bs_user_op_abort(op, -ENOMEM);
    2837                 :          0 :                 return;
    2838                 :            :         }
    2839                 :            : 
    2840   [ -  +  -  + ]:      39748 :         assert(blob->bs->cluster_sz % blob->back_bs_dev->blocklen == 0);
    2841                 :            : 
    2842                 :      39748 :         ctx->blob = blob;
    2843                 :      39748 :         ctx->page = cluster_start_page;
    2844                 :      39748 :         ctx->new_cluster_page = ch->new_cluster_page;
    2845         [ -  + ]:      39748 :         memset(ctx->new_cluster_page, 0, SPDK_BS_PAGE_SIZE);
    2846                 :            : 
    2847                 :            :         /* Check if the cluster that we intend to do CoW for is valid for
    2848                 :            :          * the backing dev. For zeroes backing dev, it'll be always valid.
    2849                 :            :          * For other backing dev e.g. a snapshot, it could be invalid if
    2850                 :            :          * the blob has been resized after snapshot was taken. */
    2851                 :      77866 :         is_valid_range = blob->back_bs_dev->is_range_valid(blob->back_bs_dev,
    2852                 :            :                          bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
    2853                 :      39748 :                          bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz));
    2854                 :            : 
    2855   [ +  +  +  + ]:      39748 :         can_copy = is_valid_range && blob_can_copy(blob, cluster_start_page, &copy_src_lba);
    2856                 :            : 
    2857   [ +  +  +  + ]:      79400 :         is_zeroes = is_valid_range && blob->back_bs_dev->is_zeroes(blob->back_bs_dev,
    2858                 :            :                         bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
    2859                 :      39652 :                         bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz));
    2860   [ +  +  +  +  :      39748 :         if (blob->parent_id != SPDK_BLOBID_INVALID && !is_zeroes && !can_copy) {
                   +  + ]
    2861                 :       1741 :                 ctx->buf = spdk_malloc(blob->bs->cluster_sz, blob->back_bs_dev->blocklen,
    2862                 :            :                                        NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    2863         [ -  + ]:       1741 :                 if (!ctx->buf) {
    2864                 :          0 :                         SPDK_ERRLOG("DMA allocation for cluster of size = %" PRIu32 " failed.\n",
    2865                 :            :                                     blob->bs->cluster_sz);
    2866                 :          0 :                         free(ctx);
    2867                 :          0 :                         bs_user_op_abort(op, -ENOMEM);
    2868                 :          0 :                         return;
    2869                 :            :                 }
    2870                 :            :         }
    2871                 :            : 
    2872                 :      39748 :         spdk_spin_lock(&blob->bs->used_lock);
    2873                 :      39748 :         rc = bs_allocate_cluster(blob, cluster_number, &ctx->new_cluster, &ctx->new_extent_page,
    2874                 :            :                                  false);
    2875                 :      39748 :         spdk_spin_unlock(&blob->bs->used_lock);
    2876         [ +  + ]:      39748 :         if (rc != 0) {
    2877                 :          1 :                 spdk_free(ctx->buf);
    2878                 :          1 :                 free(ctx);
    2879                 :          1 :                 bs_user_op_abort(op, rc);
    2880                 :          1 :                 return;
    2881                 :            :         }
    2882                 :            : 
    2883                 :      39747 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    2884                 :      39747 :         cpl.u.blob_basic.cb_fn = blob_allocate_and_copy_cluster_cpl;
    2885                 :      39747 :         cpl.u.blob_basic.cb_arg = ctx;
    2886                 :            : 
    2887                 :      39747 :         ctx->seq = bs_sequence_start_blob(_ch, &cpl, blob);
    2888         [ -  + ]:      39747 :         if (!ctx->seq) {
    2889                 :          0 :                 spdk_spin_lock(&blob->bs->used_lock);
    2890                 :          0 :                 bs_release_cluster(blob->bs, ctx->new_cluster);
    2891                 :          0 :                 spdk_spin_unlock(&blob->bs->used_lock);
    2892                 :          0 :                 spdk_free(ctx->buf);
    2893                 :          0 :                 free(ctx);
    2894                 :          0 :                 bs_user_op_abort(op, -ENOMEM);
    2895                 :          0 :                 return;
    2896                 :            :         }
    2897                 :            : 
    2898                 :            :         /* Queue the user op to block other incoming operations */
    2899                 :      39747 :         TAILQ_INSERT_TAIL(&ch->need_cluster_alloc, op, link);
    2900                 :            : 
    2901   [ +  +  +  + ]:      39747 :         if (blob->parent_id != SPDK_BLOBID_INVALID && !is_zeroes) {
    2902         [ +  + ]:       2563 :                 if (can_copy) {
    2903                 :        822 :                         blob_copy(ctx, op, copy_src_lba);
    2904                 :            :                 } else {
    2905                 :            :                         /* Read cluster from backing device */
    2906                 :       1741 :                         bs_sequence_read_bs_dev(ctx->seq, blob->back_bs_dev, ctx->buf,
    2907                 :            :                                                 bs_dev_page_to_lba(blob->back_bs_dev, cluster_start_page),
    2908                 :       1741 :                                                 bs_dev_byte_to_lba(blob->back_bs_dev, blob->bs->cluster_sz),
    2909                 :            :                                                 blob_write_copy, ctx);
    2910                 :            :                 }
    2911                 :            : 
    2912                 :            :         } else {
    2913                 :      37184 :                 blob_insert_cluster_on_md_thread(ctx->blob, cluster_number, ctx->new_cluster,
    2914                 :            :                                                  ctx->new_extent_page, ctx->new_cluster_page, blob_insert_cluster_cpl, ctx);
    2915                 :            :         }
    2916                 :            : }
    2917                 :            : 
    2918                 :            : static inline bool
    2919                 :   22240859 : blob_calculate_lba_and_lba_count(struct spdk_blob *blob, uint64_t io_unit, uint64_t length,
    2920                 :            :                                  uint64_t *lba, uint64_t *lba_count)
    2921                 :            : {
    2922                 :   22240859 :         *lba_count = length;
    2923                 :            : 
    2924         [ +  + ]:   22240859 :         if (!bs_io_unit_is_allocated(blob, io_unit)) {
    2925         [ -  + ]:    1094309 :                 assert(blob->back_bs_dev != NULL);
    2926                 :    1094309 :                 *lba = bs_io_unit_to_back_dev_lba(blob, io_unit);
    2927                 :    1094309 :                 *lba_count = bs_io_unit_to_back_dev_lba(blob, *lba_count);
    2928                 :    1094309 :                 return false;
    2929                 :            :         } else {
    2930                 :   21146550 :                 *lba = bs_blob_io_unit_to_lba(blob, io_unit);
    2931                 :   21146550 :                 return true;
    2932                 :            :         }
    2933                 :            : }
    2934                 :            : 
    2935                 :            : struct op_split_ctx {
    2936                 :            :         struct spdk_blob *blob;
    2937                 :            :         struct spdk_io_channel *channel;
    2938                 :            :         uint64_t io_unit_offset;
    2939                 :            :         uint64_t io_units_remaining;
    2940                 :            :         void *curr_payload;
    2941                 :            :         enum spdk_blob_op_type op_type;
    2942                 :            :         spdk_bs_sequence_t *seq;
    2943                 :            :         bool in_submit_ctx;
    2944                 :            :         bool completed_in_submit_ctx;
    2945                 :            :         bool done;
    2946                 :            : };
    2947                 :            : 
    2948                 :            : static void
    2949                 :      58978 : blob_request_submit_op_split_next(void *cb_arg, int bserrno)
    2950                 :            : {
    2951                 :      58978 :         struct op_split_ctx     *ctx = cb_arg;
    2952                 :      58978 :         struct spdk_blob        *blob = ctx->blob;
    2953                 :      58978 :         struct spdk_io_channel  *ch = ctx->channel;
    2954                 :      58978 :         enum spdk_blob_op_type  op_type = ctx->op_type;
    2955                 :            :         uint8_t                 *buf;
    2956                 :            :         uint64_t                offset;
    2957                 :            :         uint64_t                length;
    2958                 :            :         uint64_t                op_length;
    2959                 :            : 
    2960   [ +  -  +  + ]:      58978 :         if (bserrno != 0 || ctx->io_units_remaining == 0) {
    2961                 :       3085 :                 bs_sequence_finish(ctx->seq, bserrno);
    2962   [ +  +  +  + ]:       3085 :                 if (ctx->in_submit_ctx) {
    2963                 :            :                         /* Defer freeing of the ctx object, since it will be
    2964                 :            :                          * accessed when this unwinds back to the submisison
    2965                 :            :                          * context.
    2966                 :            :                          */
    2967                 :        240 :                         ctx->done = true;
    2968                 :            :                 } else {
    2969                 :       2845 :                         free(ctx);
    2970                 :            :                 }
    2971                 :       3085 :                 return;
    2972                 :            :         }
    2973                 :            : 
    2974   [ +  +  +  + ]:      55893 :         if (ctx->in_submit_ctx) {
    2975                 :            :                 /* If this split operation completed in the context
    2976                 :            :                  * of its submission, mark the flag and return immediately
    2977                 :            :                  * to avoid recursion.
    2978                 :            :                  */
    2979                 :        408 :                 ctx->completed_in_submit_ctx = true;
    2980                 :        408 :                 return;
    2981                 :            :         }
    2982                 :            : 
    2983                 :            :         while (true) {
    2984                 :      55893 :                 ctx->completed_in_submit_ctx = false;
    2985                 :            : 
    2986                 :      55893 :                 offset = ctx->io_unit_offset;
    2987                 :      55893 :                 length = ctx->io_units_remaining;
    2988                 :      55893 :                 buf = ctx->curr_payload;
    2989         [ +  + ]:      55893 :                 op_length = spdk_min(length, bs_num_io_units_to_cluster_boundary(blob,
    2990                 :            :                                      offset));
    2991                 :            : 
    2992                 :            :                 /* Update length and payload for next operation */
    2993                 :      55893 :                 ctx->io_units_remaining -= op_length;
    2994                 :      55893 :                 ctx->io_unit_offset += op_length;
    2995   [ +  +  +  + ]:      55893 :                 if (op_type == SPDK_BLOB_WRITE || op_type == SPDK_BLOB_READ) {
    2996                 :       3248 :                         ctx->curr_payload += op_length * blob->bs->io_unit_size;
    2997                 :            :                 }
    2998                 :            : 
    2999   [ -  +  -  + ]:      55893 :                 assert(!ctx->in_submit_ctx);
    3000                 :      55893 :                 ctx->in_submit_ctx = true;
    3001                 :            : 
    3002   [ +  +  +  +  :      55893 :                 switch (op_type) {
                   -  - ]
    3003                 :       2548 :                 case SPDK_BLOB_READ:
    3004                 :       2548 :                         spdk_blob_io_read(blob, ch, buf, offset, op_length,
    3005                 :            :                                           blob_request_submit_op_split_next, ctx);
    3006                 :       2548 :                         break;
    3007                 :        700 :                 case SPDK_BLOB_WRITE:
    3008                 :        700 :                         spdk_blob_io_write(blob, ch, buf, offset, op_length,
    3009                 :            :                                            blob_request_submit_op_split_next, ctx);
    3010                 :        700 :                         break;
    3011                 :      52317 :                 case SPDK_BLOB_UNMAP:
    3012                 :      52317 :                         spdk_blob_io_unmap(blob, ch, offset, op_length,
    3013                 :            :                                            blob_request_submit_op_split_next, ctx);
    3014                 :      52317 :                         break;
    3015                 :        328 :                 case SPDK_BLOB_WRITE_ZEROES:
    3016                 :        328 :                         spdk_blob_io_write_zeroes(blob, ch, offset, op_length,
    3017                 :            :                                                   blob_request_submit_op_split_next, ctx);
    3018                 :        328 :                         break;
    3019                 :          0 :                 case SPDK_BLOB_READV:
    3020                 :            :                 case SPDK_BLOB_WRITEV:
    3021                 :          0 :                         SPDK_ERRLOG("readv/write not valid\n");
    3022                 :          0 :                         bs_sequence_finish(ctx->seq, -EINVAL);
    3023                 :          0 :                         free(ctx);
    3024                 :          0 :                         return;
    3025                 :            :                 }
    3026                 :            : 
    3027                 :            : #ifndef __clang_analyzer__
    3028                 :            :                 /* scan-build reports a false positive around accessing the ctx here. It
    3029                 :            :                  * forms a path that recursively calls this function, but then says
    3030                 :            :                  * "assuming ctx->in_submit_ctx is false", when that isn't possible.
    3031                 :            :                  * This path does free(ctx), returns to here, and reports a use-after-free
    3032                 :            :                  * bug.  Wrapping this bit of code so that scan-build doesn't see it
    3033                 :            :                  * works around the scan-build bug.
    3034                 :            :                  */
    3035   [ -  +  -  + ]:      55893 :                 assert(ctx->in_submit_ctx);
    3036                 :      55893 :                 ctx->in_submit_ctx = false;
    3037                 :            : 
    3038                 :            :                 /* If the operation completed immediately, loop back and submit the
    3039                 :            :                  * next operation.  Otherwise we can return and the next split
    3040                 :            :                  * operation will get submitted when this current operation is
    3041                 :            :                  * later completed asynchronously.
    3042                 :            :                  */
    3043   [ +  +  +  + ]:      55893 :                 if (ctx->completed_in_submit_ctx) {
    3044                 :        408 :                         continue;
    3045   [ +  +  +  + ]:      55485 :                 } else if (ctx->done) {
    3046                 :        240 :                         free(ctx);
    3047                 :            :                 }
    3048                 :            : #endif
    3049                 :      55485 :                 break;
    3050                 :            :         }
    3051                 :            : }
    3052                 :            : 
    3053                 :            : static void
    3054                 :       3085 : blob_request_submit_op_split(struct spdk_io_channel *ch, struct spdk_blob *blob,
    3055                 :            :                              void *payload, uint64_t offset, uint64_t length,
    3056                 :            :                              spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
    3057                 :            : {
    3058                 :            :         struct op_split_ctx *ctx;
    3059                 :            :         spdk_bs_sequence_t *seq;
    3060                 :       2882 :         struct spdk_bs_cpl cpl;
    3061                 :            : 
    3062         [ -  + ]:       3085 :         assert(blob != NULL);
    3063                 :            : 
    3064                 :       3085 :         ctx = calloc(1, sizeof(struct op_split_ctx));
    3065         [ -  + ]:       3085 :         if (ctx == NULL) {
    3066                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    3067                 :          0 :                 return;
    3068                 :            :         }
    3069                 :            : 
    3070                 :       3085 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    3071                 :       3085 :         cpl.u.blob_basic.cb_fn = cb_fn;
    3072                 :       3085 :         cpl.u.blob_basic.cb_arg = cb_arg;
    3073                 :            : 
    3074                 :       3085 :         seq = bs_sequence_start_blob(ch, &cpl, blob);
    3075         [ -  + ]:       3085 :         if (!seq) {
    3076                 :          0 :                 free(ctx);
    3077                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    3078                 :          0 :                 return;
    3079                 :            :         }
    3080                 :            : 
    3081                 :       3085 :         ctx->blob = blob;
    3082                 :       3085 :         ctx->channel = ch;
    3083                 :       3085 :         ctx->curr_payload = payload;
    3084                 :       3085 :         ctx->io_unit_offset = offset;
    3085                 :       3085 :         ctx->io_units_remaining = length;
    3086                 :       3085 :         ctx->op_type = op_type;
    3087                 :       3085 :         ctx->seq = seq;
    3088                 :            : 
    3089                 :       3085 :         blob_request_submit_op_split_next(ctx, 0);
    3090                 :            : }
    3091                 :            : 
    3092                 :            : static void
    3093                 :        360 : spdk_free_cluster_unmap_complete(void *cb_arg, int bserrno)
    3094                 :            : {
    3095                 :        360 :         struct spdk_blob_free_cluster_ctx *ctx = cb_arg;
    3096                 :            : 
    3097         [ -  + ]:        360 :         if (bserrno) {
    3098                 :          0 :                 bs_sequence_finish(ctx->seq, bserrno);
    3099                 :          0 :                 free(ctx);
    3100                 :          0 :                 return;
    3101                 :            :         }
    3102                 :            : 
    3103                 :        360 :         blob_free_cluster_on_md_thread(ctx->blob, ctx->cluster_num,
    3104                 :            :                                        ctx->extent_page, ctx->md_page, blob_free_cluster_cpl, ctx);
    3105                 :            : }
    3106                 :            : 
    3107                 :            : static void
    3108                 :    5958121 : blob_request_submit_op_single(struct spdk_io_channel *_ch, struct spdk_blob *blob,
    3109                 :            :                               void *payload, uint64_t offset, uint64_t length,
    3110                 :            :                               spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
    3111                 :            : {
    3112                 :    5887262 :         struct spdk_bs_cpl cpl;
    3113                 :    5887262 :         uint64_t lba;
    3114                 :    5887262 :         uint64_t lba_count;
    3115                 :            :         bool is_allocated;
    3116                 :            : 
    3117         [ -  + ]:    5958121 :         assert(blob != NULL);
    3118                 :            : 
    3119                 :    5958121 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    3120                 :    5958121 :         cpl.u.blob_basic.cb_fn = cb_fn;
    3121                 :    5958121 :         cpl.u.blob_basic.cb_arg = cb_arg;
    3122                 :            : 
    3123         [ +  + ]:    5958121 :         if (blob->frozen_refcnt) {
    3124                 :            :                 /* This blob I/O is frozen */
    3125                 :            :                 spdk_bs_user_op_t *op;
    3126                 :        195 :                 struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch);
    3127                 :            : 
    3128                 :        195 :                 op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
    3129         [ -  + ]:        195 :                 if (!op) {
    3130                 :          0 :                         cb_fn(cb_arg, -ENOMEM);
    3131                 :         57 :                         return;
    3132                 :            :                 }
    3133                 :            : 
    3134                 :        195 :                 TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link);
    3135                 :            : 
    3136                 :        195 :                 return;
    3137                 :            :         }
    3138                 :            : 
    3139                 :    5957926 :         is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
    3140                 :            : 
    3141   [ +  +  +  -  :    5957926 :         switch (op_type) {
                      - ]
    3142                 :    4785667 :         case SPDK_BLOB_READ: {
    3143                 :            :                 spdk_bs_batch_t *batch;
    3144                 :            : 
    3145                 :    4785667 :                 batch = bs_batch_open(_ch, &cpl, blob);
    3146         [ -  + ]:    4785667 :                 if (!batch) {
    3147                 :          0 :                         cb_fn(cb_arg, -ENOMEM);
    3148                 :          0 :                         return;
    3149                 :            :                 }
    3150                 :            : 
    3151         [ +  + ]:    4785667 :                 if (is_allocated) {
    3152                 :            :                         /* Read from the blob */
    3153                 :    4779139 :                         bs_batch_read_dev(batch, payload, lba, lba_count);
    3154                 :            :                 } else {
    3155                 :            :                         /* Read from the backing block device */
    3156                 :       6528 :                         bs_batch_read_bs_dev(batch, blob->back_bs_dev, payload, lba, lba_count);
    3157                 :            :                 }
    3158                 :            : 
    3159                 :    4785667 :                 bs_batch_close(batch);
    3160                 :    4785667 :                 break;
    3161                 :            :         }
    3162                 :     977923 :         case SPDK_BLOB_WRITE:
    3163                 :            :         case SPDK_BLOB_WRITE_ZEROES: {
    3164         [ +  + ]:     977923 :                 if (is_allocated) {
    3165                 :            :                         /* Write to the blob */
    3166                 :            :                         spdk_bs_batch_t *batch;
    3167                 :            : 
    3168         [ -  + ]:     975859 :                         if (lba_count == 0) {
    3169                 :          0 :                                 cb_fn(cb_arg, 0);
    3170                 :          0 :                                 return;
    3171                 :            :                         }
    3172                 :            : 
    3173                 :     975859 :                         batch = bs_batch_open(_ch, &cpl, blob);
    3174         [ +  + ]:     975859 :                         if (!batch) {
    3175                 :         53 :                                 cb_fn(cb_arg, -ENOMEM);
    3176                 :         53 :                                 return;
    3177                 :            :                         }
    3178                 :            : 
    3179         [ +  + ]:     975806 :                         if (op_type == SPDK_BLOB_WRITE) {
    3180                 :     973976 :                                 bs_batch_write_dev(batch, payload, lba, lba_count);
    3181                 :            :                         } else {
    3182                 :       1830 :                                 bs_batch_write_zeroes_dev(batch, lba, lba_count);
    3183                 :            :                         }
    3184                 :            : 
    3185                 :     975806 :                         bs_batch_close(batch);
    3186                 :            :                 } else {
    3187                 :            :                         /* Queue this operation and allocate the cluster */
    3188                 :            :                         spdk_bs_user_op_t *op;
    3189                 :            : 
    3190                 :       2064 :                         op = bs_user_op_alloc(_ch, &cpl, op_type, blob, payload, 0, offset, length);
    3191         [ -  + ]:       2064 :                         if (!op) {
    3192                 :          0 :                                 cb_fn(cb_arg, -ENOMEM);
    3193                 :          0 :                                 return;
    3194                 :            :                         }
    3195                 :            : 
    3196                 :       2064 :                         bs_allocate_and_copy_cluster(blob, _ch, offset, op);
    3197                 :            :                 }
    3198                 :     977870 :                 break;
    3199                 :            :         }
    3200                 :     194336 :         case SPDK_BLOB_UNMAP: {
    3201                 :     194336 :                 struct spdk_blob_free_cluster_ctx *ctx = NULL;
    3202                 :            :                 spdk_bs_batch_t *batch;
    3203                 :            : 
    3204                 :            :                 /* if aligned with cluster release cluster */
    3205   [ +  +  +  -  :     194744 :                 if (spdk_blob_is_thin_provisioned(blob) && is_allocated &&
                   +  + ]
    3206                 :        408 :                     bs_io_units_per_cluster(blob) == length) {
    3207                 :        360 :                         struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_ch);
    3208                 :            :                         uint32_t cluster_start_page;
    3209                 :            :                         uint32_t cluster_number;
    3210                 :            : 
    3211   [ -  +  -  + ]:        360 :                         assert(offset % bs_io_units_per_cluster(blob) == 0);
    3212                 :            : 
    3213                 :            :                         /* Round the io_unit offset down to the first page in the cluster */
    3214                 :        360 :                         cluster_start_page = bs_io_unit_to_cluster_start(blob, offset);
    3215                 :            : 
    3216                 :            :                         /* Calculate which index in the metadata cluster array the corresponding
    3217                 :            :                          * cluster is supposed to be at. */
    3218                 :        360 :                         cluster_number = bs_io_unit_to_cluster_number(blob, offset);
    3219                 :            : 
    3220                 :        360 :                         ctx = calloc(1, sizeof(*ctx));
    3221         [ -  + ]:        360 :                         if (!ctx) {
    3222                 :          0 :                                 cb_fn(cb_arg, -ENOMEM);
    3223                 :          0 :                                 return;
    3224                 :            :                         }
    3225                 :            :                         /* When freeing a cluster the flow should be (in order):
    3226                 :            :                          * 1. Unmap the underlying area (so if the cluster is reclaimed in the future, it won't leak
    3227                 :            :                          * old data)
    3228                 :            :                          * 2. Once the unmap completes (to avoid any races with incoming writes that may claim the
    3229                 :            :                          * cluster), update and sync metadata freeing the cluster
    3230                 :            :                          * 3. Once metadata update is done, complete the user unmap request
    3231                 :            :                          */
    3232                 :        360 :                         ctx->blob = blob;
    3233                 :        360 :                         ctx->page = cluster_start_page;
    3234                 :        360 :                         ctx->cluster_num = cluster_number;
    3235                 :        360 :                         ctx->md_page = bs_channel->new_cluster_page;
    3236                 :        360 :                         ctx->seq = bs_sequence_start_bs(_ch, &cpl);
    3237         [ -  + ]:        360 :                         if (!ctx->seq) {
    3238                 :          0 :                                 free(ctx);
    3239                 :          0 :                                 cb_fn(cb_arg, -ENOMEM);
    3240                 :          0 :                                 return;
    3241                 :            :                         }
    3242                 :            : 
    3243   [ +  +  +  + ]:        360 :                         if (blob->use_extent_table) {
    3244                 :        180 :                                 ctx->extent_page = *bs_cluster_to_extent_page(blob, cluster_number);
    3245                 :            :                         }
    3246                 :            : 
    3247                 :        360 :                         cpl.u.blob_basic.cb_fn = spdk_free_cluster_unmap_complete;
    3248                 :        360 :                         cpl.u.blob_basic.cb_arg = ctx;
    3249                 :            :                 }
    3250                 :            : 
    3251                 :     194336 :                 batch = bs_batch_open(_ch, &cpl, blob);
    3252         [ -  + ]:     194336 :                 if (!batch) {
    3253                 :          0 :                         free(ctx);
    3254                 :          0 :                         cb_fn(cb_arg, -ENOMEM);
    3255                 :          0 :                         return;
    3256                 :            :                 }
    3257                 :            : 
    3258         [ +  - ]:     194336 :                 if (is_allocated) {
    3259                 :     194336 :                         bs_batch_unmap_dev(batch, lba, lba_count);
    3260                 :            :                 }
    3261                 :            : 
    3262                 :     194336 :                 bs_batch_close(batch);
    3263                 :     194336 :                 break;
    3264                 :            :         }
    3265                 :          0 :         case SPDK_BLOB_READV:
    3266                 :            :         case SPDK_BLOB_WRITEV:
    3267                 :          0 :                 SPDK_ERRLOG("readv/write not valid\n");
    3268                 :          0 :                 cb_fn(cb_arg, -EINVAL);
    3269                 :          0 :                 break;
    3270                 :            :         }
    3271                 :      75660 : }
    3272                 :            : 
    3273                 :            : static void
    3274                 :    5964304 : blob_request_submit_op(struct spdk_blob *blob, struct spdk_io_channel *_channel,
    3275                 :            :                        void *payload, uint64_t offset, uint64_t length,
    3276                 :            :                        spdk_blob_op_complete cb_fn, void *cb_arg, enum spdk_blob_op_type op_type)
    3277                 :            : {
    3278         [ -  + ]:    5964304 :         assert(blob != NULL);
    3279                 :            : 
    3280   [ +  +  +  +  :    5964304 :         if (blob->data_ro && op_type != SPDK_BLOB_READ) {
                   +  + ]
    3281                 :         24 :                 cb_fn(cb_arg, -EPERM);
    3282                 :         24 :                 return;
    3283                 :            :         }
    3284                 :            : 
    3285         [ +  + ]:    5964280 :         if (length == 0) {
    3286                 :       2980 :                 cb_fn(cb_arg, 0);
    3287                 :       2980 :                 return;
    3288                 :            :         }
    3289                 :            : 
    3290         [ +  + ]:    5961300 :         if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) {
    3291                 :        144 :                 cb_fn(cb_arg, -EINVAL);
    3292                 :        144 :                 return;
    3293                 :            :         }
    3294         [ +  + ]:    5961156 :         if (length <= bs_num_io_units_to_cluster_boundary(blob, offset)) {
    3295                 :    5958071 :                 blob_request_submit_op_single(_channel, blob, payload, offset, length,
    3296                 :            :                                               cb_fn, cb_arg, op_type);
    3297                 :            :         } else {
    3298                 :       3085 :                 blob_request_submit_op_split(_channel, blob, payload, offset, length,
    3299                 :            :                                              cb_fn, cb_arg, op_type);
    3300                 :            :         }
    3301                 :            : }
    3302                 :            : 
    3303                 :            : struct rw_iov_ctx {
    3304                 :            :         struct spdk_blob *blob;
    3305                 :            :         struct spdk_io_channel *channel;
    3306                 :            :         spdk_blob_op_complete cb_fn;
    3307                 :            :         void *cb_arg;
    3308                 :            :         bool read;
    3309                 :            :         int iovcnt;
    3310                 :            :         struct iovec *orig_iov;
    3311                 :            :         uint64_t io_unit_offset;
    3312                 :            :         uint64_t io_units_remaining;
    3313                 :            :         uint64_t io_units_done;
    3314                 :            :         struct spdk_blob_ext_io_opts *ext_io_opts;
    3315                 :            :         struct iovec iov[0];
    3316                 :            : };
    3317                 :            : 
    3318                 :            : static void
    3319                 :   16056475 : rw_iov_done(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    3320                 :            : {
    3321         [ -  + ]:   16056475 :         assert(cb_arg == NULL);
    3322                 :   16056475 :         bs_sequence_finish(seq, bserrno);
    3323                 :   16056475 : }
    3324                 :            : 
    3325                 :            : static void
    3326                 :       4464 : rw_iov_split_next(void *cb_arg, int bserrno)
    3327                 :            : {
    3328                 :       4464 :         struct rw_iov_ctx *ctx = cb_arg;
    3329                 :       4464 :         struct spdk_blob *blob = ctx->blob;
    3330                 :            :         struct iovec *iov, *orig_iov;
    3331                 :            :         int iovcnt;
    3332                 :            :         size_t orig_iovoff;
    3333                 :            :         uint64_t io_units_count, io_units_to_boundary, io_unit_offset;
    3334                 :            :         uint64_t byte_count;
    3335                 :            : 
    3336   [ +  -  +  + ]:       4464 :         if (bserrno != 0 || ctx->io_units_remaining == 0) {
    3337                 :       1224 :                 ctx->cb_fn(ctx->cb_arg, bserrno);
    3338                 :       1224 :                 free(ctx);
    3339                 :       1224 :                 return;
    3340                 :            :         }
    3341                 :            : 
    3342                 :       3240 :         io_unit_offset = ctx->io_unit_offset;
    3343                 :       3240 :         io_units_to_boundary = bs_num_io_units_to_cluster_boundary(blob, io_unit_offset);
    3344                 :       3240 :         io_units_count = spdk_min(ctx->io_units_remaining, io_units_to_boundary);
    3345                 :            :         /*
    3346                 :            :          * Get index and offset into the original iov array for our current position in the I/O sequence.
    3347                 :            :          *  byte_count will keep track of how many bytes remaining until orig_iov and orig_iovoff will
    3348                 :            :          *  point to the current position in the I/O sequence.
    3349                 :            :          */
    3350                 :       3240 :         byte_count = ctx->io_units_done * blob->bs->io_unit_size;
    3351                 :       3240 :         orig_iov = &ctx->orig_iov[0];
    3352                 :       3240 :         orig_iovoff = 0;
    3353         [ +  + ]:       6888 :         while (byte_count > 0) {
    3354         [ +  + ]:       3648 :                 if (byte_count >= orig_iov->iov_len) {
    3355                 :       2112 :                         byte_count -= orig_iov->iov_len;
    3356                 :       2112 :                         orig_iov++;
    3357                 :            :                 } else {
    3358                 :       1536 :                         orig_iovoff = byte_count;
    3359                 :       1536 :                         byte_count = 0;
    3360                 :            :                 }
    3361                 :            :         }
    3362                 :            : 
    3363                 :            :         /*
    3364                 :            :          * Build an iov array for the next I/O in the sequence.  byte_count will keep track of how many
    3365                 :            :          *  bytes of this next I/O remain to be accounted for in the new iov array.
    3366                 :            :          */
    3367                 :       3240 :         byte_count = io_units_count * blob->bs->io_unit_size;
    3368                 :       3240 :         iov = &ctx->iov[0];
    3369                 :       3240 :         iovcnt = 0;
    3370         [ +  + ]:       8280 :         while (byte_count > 0) {
    3371         [ -  + ]:       5040 :                 assert(iovcnt < ctx->iovcnt);
    3372                 :       5040 :                 iov->iov_len = spdk_min(byte_count, orig_iov->iov_len - orig_iovoff);
    3373                 :       5040 :                 iov->iov_base = orig_iov->iov_base + orig_iovoff;
    3374                 :       5040 :                 byte_count -= iov->iov_len;
    3375                 :       5040 :                 orig_iovoff = 0;
    3376                 :       5040 :                 orig_iov++;
    3377                 :       5040 :                 iov++;
    3378                 :       5040 :                 iovcnt++;
    3379                 :            :         }
    3380                 :            : 
    3381                 :       3240 :         ctx->io_unit_offset += io_units_count;
    3382                 :       3240 :         ctx->io_units_remaining -= io_units_count;
    3383                 :       3240 :         ctx->io_units_done += io_units_count;
    3384                 :       3240 :         iov = &ctx->iov[0];
    3385                 :            : 
    3386   [ +  +  +  + ]:       3240 :         if (ctx->read) {
    3387                 :       2448 :                 spdk_blob_io_readv_ext(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset,
    3388                 :            :                                        io_units_count, rw_iov_split_next, ctx, ctx->ext_io_opts);
    3389                 :            :         } else {
    3390                 :        792 :                 spdk_blob_io_writev_ext(ctx->blob, ctx->channel, iov, iovcnt, io_unit_offset,
    3391                 :            :                                         io_units_count, rw_iov_split_next, ctx, ctx->ext_io_opts);
    3392                 :            :         }
    3393                 :            : }
    3394                 :            : 
    3395                 :            : static void
    3396                 :   16285760 : blob_request_submit_rw_iov(struct spdk_blob *blob, struct spdk_io_channel *_channel,
    3397                 :            :                            struct iovec *iov, int iovcnt,
    3398                 :            :                            uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg, bool read,
    3399                 :            :                            struct spdk_blob_ext_io_opts *ext_io_opts)
    3400                 :            : {
    3401                 :    5010771 :         struct spdk_bs_cpl      cpl;
    3402                 :            : 
    3403         [ -  + ]:   16285760 :         assert(blob != NULL);
    3404                 :            : 
    3405   [ +  +  +  +  :   16285760 :         if (!read && blob->data_ro) {
                   +  + ]
    3406                 :         57 :                 cb_fn(cb_arg, -EPERM);
    3407                 :       8538 :                 return;
    3408                 :            :         }
    3409                 :            : 
    3410         [ -  + ]:   16285703 :         if (length == 0) {
    3411                 :          0 :                 cb_fn(cb_arg, 0);
    3412                 :          0 :                 return;
    3413                 :            :         }
    3414                 :            : 
    3415         [ -  + ]:   16285703 :         if (offset + length > bs_cluster_to_lba(blob->bs, blob->active.num_clusters)) {
    3416                 :          0 :                 cb_fn(cb_arg, -EINVAL);
    3417                 :          0 :                 return;
    3418                 :            :         }
    3419                 :            : 
    3420                 :            :         /*
    3421                 :            :          * For now, we implement readv/writev using a sequence (instead of a batch) to account for having
    3422                 :            :          *  to split a request that spans a cluster boundary.  For I/O that do not span a cluster boundary,
    3423                 :            :          *  there will be no noticeable difference compared to using a batch.  For I/O that do span a cluster
    3424                 :            :          *  boundary, the target LBAs (after blob offset to LBA translation) may not be contiguous, so we need
    3425                 :            :          *  to allocate a separate iov array and split the I/O such that none of the resulting
    3426                 :            :          *  smaller I/O cross a cluster boundary.  These smaller I/O will be issued in sequence (not in parallel)
    3427                 :            :          *  but since this case happens very infrequently, any performance impact will be negligible.
    3428                 :            :          *
    3429                 :            :          * This could be optimized in the future to allocate a big enough iov array to account for all of the iovs
    3430                 :            :          *  for all of the smaller I/Os, pre-build all of the iov arrays for the smaller I/Os, then issue them
    3431                 :            :          *  in a batch.  That would also require creating an intermediate spdk_bs_cpl that would get called
    3432                 :            :          *  when the batch was completed, to allow for freeing the memory for the iov arrays.
    3433                 :            :          */
    3434         [ +  + ]:   16285703 :         if (spdk_likely(length <= bs_num_io_units_to_cluster_boundary(blob, offset))) {
    3435                 :    5009678 :                 uint64_t lba_count;
    3436                 :    5009678 :                 uint64_t lba;
    3437                 :            :                 bool is_allocated;
    3438                 :            : 
    3439                 :   16284455 :                 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    3440                 :   16284455 :                 cpl.u.blob_basic.cb_fn = cb_fn;
    3441                 :   16284455 :                 cpl.u.blob_basic.cb_arg = cb_arg;
    3442                 :            : 
    3443         [ +  + ]:   16284455 :                 if (blob->frozen_refcnt) {
    3444                 :            :                         /* This blob I/O is frozen */
    3445                 :            :                         enum spdk_blob_op_type op_type;
    3446                 :            :                         spdk_bs_user_op_t *op;
    3447                 :       1522 :                         struct spdk_bs_channel *bs_channel = spdk_io_channel_get_ctx(_channel);
    3448                 :            : 
    3449         [ -  + ]:       1522 :                         op_type = read ? SPDK_BLOB_READV : SPDK_BLOB_WRITEV;
    3450                 :       1522 :                         op = bs_user_op_alloc(_channel, &cpl, op_type, blob, iov, iovcnt, offset, length);
    3451         [ -  + ]:       1522 :                         if (!op) {
    3452                 :          0 :                                 cb_fn(cb_arg, -ENOMEM);
    3453                 :       8477 :                                 return;
    3454                 :            :                         }
    3455                 :            : 
    3456                 :       1522 :                         TAILQ_INSERT_TAIL(&bs_channel->queued_io, op, link);
    3457                 :            : 
    3458                 :       1522 :                         return;
    3459                 :            :                 }
    3460                 :            : 
    3461                 :   16282933 :                 is_allocated = blob_calculate_lba_and_lba_count(blob, offset, length, &lba, &lba_count);
    3462                 :            : 
    3463         [ +  + ]:   16282933 :                 if (read) {
    3464                 :            :                         spdk_bs_sequence_t *seq;
    3465                 :            : 
    3466                 :    7578778 :                         seq = bs_sequence_start_blob(_channel, &cpl, blob);
    3467         [ +  + ]:    7578778 :                         if (!seq) {
    3468                 :        740 :                                 cb_fn(cb_arg, -ENOMEM);
    3469                 :        740 :                                 return;
    3470                 :            :                         }
    3471                 :            : 
    3472                 :    7578038 :                         seq->ext_io_opts = ext_io_opts;
    3473                 :            : 
    3474         [ +  + ]:    7578038 :                         if (is_allocated) {
    3475                 :    6645476 :                                 bs_sequence_readv_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL);
    3476                 :            :                         } else {
    3477                 :     932562 :                                 bs_sequence_readv_bs_dev(seq, blob->back_bs_dev, iov, iovcnt, lba, lba_count,
    3478                 :            :                                                          rw_iov_done, NULL);
    3479                 :            :                         }
    3480                 :            :                 } else {
    3481         [ +  + ]:    8704155 :                         if (is_allocated) {
    3482                 :            :                                 spdk_bs_sequence_t *seq;
    3483                 :            : 
    3484                 :    8551000 :                                 seq = bs_sequence_start_blob(_channel, &cpl, blob);
    3485         [ +  + ]:    8551000 :                                 if (!seq) {
    3486                 :      72563 :                                         cb_fn(cb_arg, -ENOMEM);
    3487                 :      72563 :                                         return;
    3488                 :            :                                 }
    3489                 :            : 
    3490                 :    8478437 :                                 seq->ext_io_opts = ext_io_opts;
    3491                 :            : 
    3492                 :    8478437 :                                 bs_sequence_writev_dev(seq, iov, iovcnt, lba, lba_count, rw_iov_done, NULL);
    3493                 :            :                         } else {
    3494                 :            :                                 /* Queue this operation and allocate the cluster */
    3495                 :            :                                 spdk_bs_user_op_t *op;
    3496                 :            : 
    3497                 :     153155 :                                 op = bs_user_op_alloc(_channel, &cpl, SPDK_BLOB_WRITEV, blob, iov, iovcnt, offset,
    3498                 :            :                                                       length);
    3499         [ -  + ]:     153155 :                                 if (!op) {
    3500                 :          0 :                                         cb_fn(cb_arg, -ENOMEM);
    3501                 :          0 :                                         return;
    3502                 :            :                                 }
    3503                 :            : 
    3504                 :     153155 :                                 op->ext_io_opts = ext_io_opts;
    3505                 :            : 
    3506                 :     153155 :                                 bs_allocate_and_copy_cluster(blob, _channel, offset, op);
    3507                 :            :                         }
    3508                 :            :                 }
    3509                 :            :         } else {
    3510                 :            :                 struct rw_iov_ctx *ctx;
    3511                 :            : 
    3512                 :       1248 :                 ctx = calloc(1, sizeof(struct rw_iov_ctx) + iovcnt * sizeof(struct iovec));
    3513         [ +  + ]:       1248 :                 if (ctx == NULL) {
    3514                 :         24 :                         cb_fn(cb_arg, -ENOMEM);
    3515                 :         24 :                         return;
    3516                 :            :                 }
    3517                 :            : 
    3518                 :       1224 :                 ctx->blob = blob;
    3519                 :       1224 :                 ctx->channel = _channel;
    3520                 :       1224 :                 ctx->cb_fn = cb_fn;
    3521                 :       1224 :                 ctx->cb_arg = cb_arg;
    3522                 :       1224 :                 ctx->read = read;
    3523                 :       1224 :                 ctx->orig_iov = iov;
    3524                 :       1224 :                 ctx->iovcnt = iovcnt;
    3525                 :       1224 :                 ctx->io_unit_offset = offset;
    3526                 :       1224 :                 ctx->io_units_remaining = length;
    3527                 :       1224 :                 ctx->io_units_done = 0;
    3528                 :       1224 :                 ctx->ext_io_opts = ext_io_opts;
    3529                 :            : 
    3530                 :       1224 :                 rw_iov_split_next(ctx, 0);
    3531                 :            :         }
    3532                 :            : }
    3533                 :            : 
    3534                 :            : static struct spdk_blob *
    3535                 :      89019 : blob_lookup(struct spdk_blob_store *bs, spdk_blob_id blobid)
    3536                 :            : {
    3537                 :      80004 :         struct spdk_blob find;
    3538                 :            : 
    3539         [ +  + ]:      89019 :         if (spdk_bit_array_get(bs->open_blobids, blobid) == 0) {
    3540                 :      84105 :                 return NULL;
    3541                 :            :         }
    3542                 :            : 
    3543                 :       4914 :         find.id = blobid;
    3544                 :       4914 :         return RB_FIND(spdk_blob_tree, &bs->open_blobs, &find);
    3545                 :            : }
    3546                 :            : 
    3547                 :            : static void
    3548                 :      12481 : blob_get_snapshot_and_clone_entries(struct spdk_blob *blob,
    3549                 :            :                                     struct spdk_blob_list **snapshot_entry, struct spdk_blob_list **clone_entry)
    3550                 :            : {
    3551         [ -  + ]:      12481 :         assert(blob != NULL);
    3552                 :      12481 :         *snapshot_entry = NULL;
    3553                 :      12481 :         *clone_entry = NULL;
    3554                 :            : 
    3555         [ +  + ]:      12481 :         if (blob->parent_id == SPDK_BLOBID_INVALID) {
    3556                 :      10727 :                 return;
    3557                 :            :         }
    3558                 :            : 
    3559         [ +  + ]:       2629 :         TAILQ_FOREACH(*snapshot_entry, &blob->bs->snapshots, link) {
    3560         [ +  + ]:       2300 :                 if ((*snapshot_entry)->id == blob->parent_id) {
    3561                 :       1425 :                         break;
    3562                 :            :                 }
    3563                 :            :         }
    3564                 :            : 
    3565         [ +  + ]:       1754 :         if (*snapshot_entry != NULL) {
    3566         [ +  - ]:       1703 :                 TAILQ_FOREACH(*clone_entry, &(*snapshot_entry)->clones, link) {
    3567         [ +  + ]:       1703 :                         if ((*clone_entry)->id == blob->id) {
    3568                 :       1425 :                                 break;
    3569                 :            :                         }
    3570                 :            :                 }
    3571                 :            : 
    3572         [ -  + ]:       1425 :                 assert(*clone_entry != NULL);
    3573                 :            :         }
    3574                 :            : }
    3575                 :            : 
    3576                 :            : static int
    3577                 :      12779 : bs_channel_create(void *io_device, void *ctx_buf)
    3578                 :            : {
    3579                 :      12779 :         struct spdk_blob_store          *bs = io_device;
    3580                 :      12779 :         struct spdk_bs_channel          *channel = ctx_buf;
    3581                 :            :         struct spdk_bs_dev              *dev;
    3582                 :      12779 :         uint32_t                        max_ops = bs->max_channel_ops;
    3583                 :            :         uint32_t                        i;
    3584                 :            : 
    3585                 :      12779 :         dev = bs->dev;
    3586                 :            : 
    3587                 :      12779 :         channel->req_mem = calloc(max_ops, sizeof(struct spdk_bs_request_set));
    3588         [ -  + ]:      12779 :         if (!channel->req_mem) {
    3589                 :          0 :                 return -1;
    3590                 :            :         }
    3591                 :            : 
    3592                 :      12779 :         TAILQ_INIT(&channel->reqs);
    3593                 :            : 
    3594         [ +  + ]:    6555627 :         for (i = 0; i < max_ops; i++) {
    3595                 :    6542848 :                 TAILQ_INSERT_TAIL(&channel->reqs, &channel->req_mem[i], link);
    3596                 :            :         }
    3597                 :            : 
    3598                 :      12779 :         channel->bs = bs;
    3599                 :      12779 :         channel->dev = dev;
    3600                 :      12779 :         channel->dev_channel = dev->create_channel(dev);
    3601                 :            : 
    3602         [ -  + ]:      12779 :         if (!channel->dev_channel) {
    3603                 :          0 :                 SPDK_ERRLOG("Failed to create device channel.\n");
    3604                 :          0 :                 free(channel->req_mem);
    3605                 :          0 :                 return -1;
    3606                 :            :         }
    3607                 :            : 
    3608                 :      12779 :         channel->new_cluster_page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, NULL, SPDK_ENV_SOCKET_ID_ANY,
    3609                 :            :                                     SPDK_MALLOC_DMA);
    3610         [ -  + ]:      12779 :         if (!channel->new_cluster_page) {
    3611                 :          0 :                 SPDK_ERRLOG("Failed to allocate new cluster page\n");
    3612                 :          0 :                 free(channel->req_mem);
    3613                 :          0 :                 channel->dev->destroy_channel(channel->dev, channel->dev_channel);
    3614                 :          0 :                 return -1;
    3615                 :            :         }
    3616                 :            : 
    3617                 :      12779 :         TAILQ_INIT(&channel->need_cluster_alloc);
    3618                 :      12779 :         TAILQ_INIT(&channel->queued_io);
    3619                 :      12779 :         RB_INIT(&channel->esnap_channels);
    3620                 :            : 
    3621                 :      12779 :         return 0;
    3622                 :            : }
    3623                 :            : 
    3624                 :            : static void
    3625                 :      12779 : bs_channel_destroy(void *io_device, void *ctx_buf)
    3626                 :            : {
    3627                 :      12779 :         struct spdk_bs_channel *channel = ctx_buf;
    3628                 :            :         spdk_bs_user_op_t *op;
    3629                 :            : 
    3630         [ -  + ]:      12779 :         while (!TAILQ_EMPTY(&channel->need_cluster_alloc)) {
    3631                 :          0 :                 op = TAILQ_FIRST(&channel->need_cluster_alloc);
    3632         [ #  # ]:          0 :                 TAILQ_REMOVE(&channel->need_cluster_alloc, op, link);
    3633                 :          0 :                 bs_user_op_abort(op, -EIO);
    3634                 :            :         }
    3635                 :            : 
    3636         [ -  + ]:      12779 :         while (!TAILQ_EMPTY(&channel->queued_io)) {
    3637                 :          0 :                 op = TAILQ_FIRST(&channel->queued_io);
    3638         [ #  # ]:          0 :                 TAILQ_REMOVE(&channel->queued_io, op, link);
    3639                 :          0 :                 bs_user_op_abort(op, -EIO);
    3640                 :            :         }
    3641                 :            : 
    3642                 :      12779 :         blob_esnap_destroy_bs_channel(channel);
    3643                 :            : 
    3644                 :      12779 :         free(channel->req_mem);
    3645                 :      12779 :         spdk_free(channel->new_cluster_page);
    3646                 :      12779 :         channel->dev->destroy_channel(channel->dev, channel->dev_channel);
    3647                 :      12779 : }
    3648                 :            : 
    3649                 :            : static void
    3650                 :      12494 : bs_dev_destroy(void *io_device)
    3651                 :            : {
    3652                 :      12494 :         struct spdk_blob_store *bs = io_device;
    3653                 :            :         struct spdk_blob        *blob, *blob_tmp;
    3654                 :            : 
    3655                 :      12494 :         bs->dev->destroy(bs->dev);
    3656                 :            : 
    3657   [ -  +  -  - ]:      12494 :         RB_FOREACH_SAFE(blob, spdk_blob_tree, &bs->open_blobs, blob_tmp) {
    3658                 :          0 :                 RB_REMOVE(spdk_blob_tree, &bs->open_blobs, blob);
    3659                 :          0 :                 spdk_bit_array_clear(bs->open_blobids, blob->id);
    3660                 :          0 :                 blob_free(blob);
    3661                 :            :         }
    3662                 :            : 
    3663                 :      12494 :         spdk_spin_destroy(&bs->used_lock);
    3664                 :            : 
    3665                 :      12494 :         spdk_bit_array_free(&bs->open_blobids);
    3666                 :      12494 :         spdk_bit_array_free(&bs->used_blobids);
    3667                 :      12494 :         spdk_bit_array_free(&bs->used_md_pages);
    3668                 :      12494 :         spdk_bit_pool_free(&bs->used_clusters);
    3669                 :            :         /*
    3670                 :            :          * If this function is called for any reason except a successful unload,
    3671                 :            :          * the unload_cpl type will be NONE and this will be a nop.
    3672                 :            :          */
    3673                 :      12494 :         bs_call_cpl(&bs->unload_cpl, bs->unload_err);
    3674                 :            : 
    3675                 :      12494 :         free(bs);
    3676                 :      12494 : }
    3677                 :            : 
    3678                 :            : static int
    3679                 :      12445 : bs_blob_list_add(struct spdk_blob *blob)
    3680                 :            : {
    3681                 :            :         spdk_blob_id snapshot_id;
    3682                 :      12445 :         struct spdk_blob_list *snapshot_entry = NULL;
    3683                 :      12445 :         struct spdk_blob_list *clone_entry = NULL;
    3684                 :            : 
    3685         [ -  + ]:      12445 :         assert(blob != NULL);
    3686                 :            : 
    3687                 :      12445 :         snapshot_id = blob->parent_id;
    3688   [ +  +  +  + ]:      12445 :         if (snapshot_id == SPDK_BLOBID_INVALID ||
    3689                 :            :             snapshot_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
    3690                 :       9885 :                 return 0;
    3691                 :            :         }
    3692                 :            : 
    3693                 :       2560 :         snapshot_entry = bs_get_snapshot_entry(blob->bs, snapshot_id);
    3694         [ +  + ]:       2560 :         if (snapshot_entry == NULL) {
    3695                 :            :                 /* Snapshot not found */
    3696                 :       1757 :                 snapshot_entry = calloc(1, sizeof(struct spdk_blob_list));
    3697         [ -  + ]:       1757 :                 if (snapshot_entry == NULL) {
    3698                 :          0 :                         return -ENOMEM;
    3699                 :            :                 }
    3700                 :       1757 :                 snapshot_entry->id = snapshot_id;
    3701                 :       1757 :                 TAILQ_INIT(&snapshot_entry->clones);
    3702                 :       1757 :                 TAILQ_INSERT_TAIL(&blob->bs->snapshots, snapshot_entry, link);
    3703                 :            :         } else {
    3704         [ +  + ]:       1291 :                 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
    3705         [ -  + ]:        488 :                         if (clone_entry->id == blob->id) {
    3706                 :          0 :                                 break;
    3707                 :            :                         }
    3708                 :            :                 }
    3709                 :            :         }
    3710                 :            : 
    3711         [ +  - ]:       2560 :         if (clone_entry == NULL) {
    3712                 :            :                 /* Clone not found */
    3713                 :       2560 :                 clone_entry = calloc(1, sizeof(struct spdk_blob_list));
    3714         [ -  + ]:       2560 :                 if (clone_entry == NULL) {
    3715                 :          0 :                         return -ENOMEM;
    3716                 :            :                 }
    3717                 :       2560 :                 clone_entry->id = blob->id;
    3718                 :       2560 :                 TAILQ_INIT(&clone_entry->clones);
    3719                 :       2560 :                 TAILQ_INSERT_TAIL(&snapshot_entry->clones, clone_entry, link);
    3720                 :       2560 :                 snapshot_entry->clone_count++;
    3721                 :            :         }
    3722                 :            : 
    3723                 :       2560 :         return 0;
    3724                 :            : }
    3725                 :            : 
    3726                 :            : static void
    3727                 :      12004 : bs_blob_list_remove(struct spdk_blob *blob)
    3728                 :            : {
    3729                 :      12004 :         struct spdk_blob_list *snapshot_entry = NULL;
    3730                 :      12004 :         struct spdk_blob_list *clone_entry = NULL;
    3731                 :            : 
    3732                 :      12004 :         blob_get_snapshot_and_clone_entries(blob, &snapshot_entry, &clone_entry);
    3733                 :            : 
    3734         [ +  + ]:      12004 :         if (snapshot_entry == NULL) {
    3735                 :      10677 :                 return;
    3736                 :            :         }
    3737                 :            : 
    3738                 :       1327 :         blob->parent_id = SPDK_BLOBID_INVALID;
    3739         [ +  + ]:       1327 :         TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
    3740                 :       1327 :         free(clone_entry);
    3741                 :            : 
    3742                 :       1327 :         snapshot_entry->clone_count--;
    3743                 :            : }
    3744                 :            : 
    3745                 :            : static int
    3746                 :      12494 : bs_blob_list_free(struct spdk_blob_store *bs)
    3747                 :            : {
    3748                 :            :         struct spdk_blob_list *snapshot_entry;
    3749                 :            :         struct spdk_blob_list *snapshot_entry_tmp;
    3750                 :            :         struct spdk_blob_list *clone_entry;
    3751                 :            :         struct spdk_blob_list *clone_entry_tmp;
    3752                 :            : 
    3753         [ +  + ]:      13375 :         TAILQ_FOREACH_SAFE(snapshot_entry, &bs->snapshots, link, snapshot_entry_tmp) {
    3754         [ +  + ]:       1818 :                 TAILQ_FOREACH_SAFE(clone_entry, &snapshot_entry->clones, link, clone_entry_tmp) {
    3755         [ +  + ]:        937 :                         TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
    3756                 :        937 :                         free(clone_entry);
    3757                 :            :                 }
    3758         [ +  + ]:        881 :                 TAILQ_REMOVE(&bs->snapshots, snapshot_entry, link);
    3759                 :        881 :                 free(snapshot_entry);
    3760                 :            :         }
    3761                 :            : 
    3762                 :      12494 :         return 0;
    3763                 :            : }
    3764                 :            : 
    3765                 :            : static void
    3766                 :      12494 : bs_free(struct spdk_blob_store *bs)
    3767                 :            : {
    3768                 :      12494 :         bs_blob_list_free(bs);
    3769                 :            : 
    3770                 :      12494 :         bs_unregister_md_thread(bs);
    3771                 :      12494 :         spdk_io_device_unregister(bs, bs_dev_destroy);
    3772                 :      12494 : }
    3773                 :            : 
    3774                 :            : void
    3775                 :      22004 : spdk_bs_opts_init(struct spdk_bs_opts *opts, size_t opts_size)
    3776                 :            : {
    3777                 :            : 
    3778         [ -  + ]:      22004 :         if (!opts) {
    3779                 :          0 :                 SPDK_ERRLOG("opts should not be NULL\n");
    3780                 :          0 :                 return;
    3781                 :            :         }
    3782                 :            : 
    3783         [ -  + ]:      22004 :         if (!opts_size) {
    3784                 :          0 :                 SPDK_ERRLOG("opts_size should not be zero value\n");
    3785                 :          0 :                 return;
    3786                 :            :         }
    3787                 :            : 
    3788         [ -  + ]:      22004 :         memset(opts, 0, opts_size);
    3789                 :      22004 :         opts->opts_size = opts_size;
    3790                 :            : 
    3791                 :            : #define FIELD_OK(field) \
    3792                 :            :         offsetof(struct spdk_bs_opts, field) + sizeof(opts->field) <= opts_size
    3793                 :            : 
    3794                 :            : #define SET_FIELD(field, value) \
    3795                 :            :         if (FIELD_OK(field)) { \
    3796                 :            :                 opts->field = value; \
    3797                 :            :         } \
    3798                 :            : 
    3799         [ +  - ]:      22004 :         SET_FIELD(cluster_sz, SPDK_BLOB_OPTS_CLUSTER_SZ);
    3800         [ +  - ]:      22004 :         SET_FIELD(num_md_pages, SPDK_BLOB_OPTS_NUM_MD_PAGES);
    3801         [ +  - ]:      22004 :         SET_FIELD(max_md_ops, SPDK_BLOB_OPTS_NUM_MD_PAGES);
    3802         [ +  - ]:      22004 :         SET_FIELD(max_channel_ops, SPDK_BLOB_OPTS_DEFAULT_CHANNEL_OPS);
    3803         [ +  - ]:      22004 :         SET_FIELD(clear_method,  BS_CLEAR_WITH_UNMAP);
    3804                 :            : 
    3805         [ +  - ]:      22004 :         if (FIELD_OK(bstype)) {
    3806         [ -  + ]:      22004 :                 memset(&opts->bstype, 0, sizeof(opts->bstype));
    3807                 :            :         }
    3808                 :            : 
    3809         [ +  - ]:      22004 :         SET_FIELD(iter_cb_fn, NULL);
    3810         [ +  - ]:      22004 :         SET_FIELD(iter_cb_arg, NULL);
    3811         [ +  - ]:      22004 :         SET_FIELD(force_recover, false);
    3812         [ +  - ]:      22004 :         SET_FIELD(esnap_bs_dev_create, NULL);
    3813         [ +  - ]:      22004 :         SET_FIELD(esnap_ctx, NULL);
    3814                 :            : 
    3815                 :            : #undef FIELD_OK
    3816                 :            : #undef SET_FIELD
    3817                 :            : }
    3818                 :            : 
    3819                 :            : static int
    3820                 :       3099 : bs_opts_verify(struct spdk_bs_opts *opts)
    3821                 :            : {
    3822   [ +  +  +  -  :       3099 :         if (opts->cluster_sz == 0 || opts->num_md_pages == 0 || opts->max_md_ops == 0 ||
                   +  - ]
    3823         [ -  + ]:       3075 :             opts->max_channel_ops == 0) {
    3824                 :         24 :                 SPDK_ERRLOG("Blobstore options cannot be set to 0\n");
    3825                 :         24 :                 return -1;
    3826                 :            :         }
    3827                 :            : 
    3828                 :       3075 :         return 0;
    3829                 :            : }
    3830                 :            : 
    3831                 :            : /* START spdk_bs_load */
    3832                 :            : 
    3833                 :            : /* spdk_bs_load_ctx is used for init, load, unload and dump code paths. */
    3834                 :            : 
    3835                 :            : struct spdk_bs_load_ctx {
    3836                 :            :         struct spdk_blob_store          *bs;
    3837                 :            :         struct spdk_bs_super_block      *super;
    3838                 :            : 
    3839                 :            :         struct spdk_bs_md_mask          *mask;
    3840                 :            :         bool                            in_page_chain;
    3841                 :            :         uint32_t                        page_index;
    3842                 :            :         uint32_t                        cur_page;
    3843                 :            :         struct spdk_blob_md_page        *page;
    3844                 :            : 
    3845                 :            :         uint64_t                        num_extent_pages;
    3846                 :            :         uint32_t                        *extent_page_num;
    3847                 :            :         struct spdk_blob_md_page        *extent_pages;
    3848                 :            :         struct spdk_bit_array           *used_clusters;
    3849                 :            : 
    3850                 :            :         spdk_bs_sequence_t                      *seq;
    3851                 :            :         spdk_blob_op_with_handle_complete       iter_cb_fn;
    3852                 :            :         void                                    *iter_cb_arg;
    3853                 :            :         struct spdk_blob                        *blob;
    3854                 :            :         spdk_blob_id                            blobid;
    3855                 :            : 
    3856                 :            :         bool                                    force_recover;
    3857                 :            : 
    3858                 :            :         /* These fields are used in the spdk_bs_dump path. */
    3859                 :            :         bool                                    dumping;
    3860                 :            :         FILE                                    *fp;
    3861                 :            :         spdk_bs_dump_print_xattr                print_xattr_fn;
    3862                 :            :         char                                    xattr_name[4096];
    3863                 :            : };
    3864                 :            : 
    3865                 :            : static int
    3866                 :      12568 : bs_alloc(struct spdk_bs_dev *dev, struct spdk_bs_opts *opts, struct spdk_blob_store **_bs,
    3867                 :            :          struct spdk_bs_load_ctx **_ctx)
    3868                 :            : {
    3869                 :            :         struct spdk_blob_store  *bs;
    3870                 :            :         struct spdk_bs_load_ctx *ctx;
    3871                 :            :         uint64_t dev_size;
    3872                 :            :         int rc;
    3873                 :            : 
    3874                 :      12568 :         dev_size = dev->blocklen * dev->blockcnt;
    3875         [ +  + ]:      12568 :         if (dev_size < opts->cluster_sz) {
    3876                 :            :                 /* Device size cannot be smaller than cluster size of blobstore */
    3877   [ -  +  -  + ]:         50 :                 SPDK_INFOLOG(blob, "Device size %" PRIu64 " is smaller than cluster size %" PRIu32 "\n",
    3878                 :            :                              dev_size, opts->cluster_sz);
    3879                 :         50 :                 return -ENOSPC;
    3880                 :            :         }
    3881         [ +  + ]:      12518 :         if (opts->cluster_sz < SPDK_BS_PAGE_SIZE) {
    3882                 :            :                 /* Cluster size cannot be smaller than page size */
    3883                 :         24 :                 SPDK_ERRLOG("Cluster size %" PRIu32 " is smaller than page size %d\n",
    3884                 :            :                             opts->cluster_sz, SPDK_BS_PAGE_SIZE);
    3885                 :         24 :                 return -EINVAL;
    3886                 :            :         }
    3887                 :      12494 :         bs = calloc(1, sizeof(struct spdk_blob_store));
    3888         [ -  + ]:      12494 :         if (!bs) {
    3889                 :          0 :                 return -ENOMEM;
    3890                 :            :         }
    3891                 :            : 
    3892                 :      12494 :         ctx = calloc(1, sizeof(struct spdk_bs_load_ctx));
    3893         [ -  + ]:      12494 :         if (!ctx) {
    3894                 :          0 :                 free(bs);
    3895                 :          0 :                 return -ENOMEM;
    3896                 :            :         }
    3897                 :            : 
    3898                 :      12494 :         ctx->bs = bs;
    3899                 :      12494 :         ctx->iter_cb_fn = opts->iter_cb_fn;
    3900                 :      12494 :         ctx->iter_cb_arg = opts->iter_cb_arg;
    3901                 :      12494 :         ctx->force_recover = opts->force_recover;
    3902                 :            : 
    3903                 :      12494 :         ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
    3904                 :            :                                   SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    3905         [ -  + ]:      12494 :         if (!ctx->super) {
    3906                 :          0 :                 free(ctx);
    3907                 :          0 :                 free(bs);
    3908                 :          0 :                 return -ENOMEM;
    3909                 :            :         }
    3910                 :            : 
    3911                 :      12494 :         RB_INIT(&bs->open_blobs);
    3912                 :      12494 :         TAILQ_INIT(&bs->snapshots);
    3913                 :      12494 :         bs->dev = dev;
    3914                 :      12494 :         bs->md_thread = spdk_get_thread();
    3915         [ -  + ]:      12494 :         assert(bs->md_thread != NULL);
    3916                 :            : 
    3917                 :            :         /*
    3918                 :            :          * Do not use bs_lba_to_cluster() here since blockcnt may not be an
    3919                 :            :          *  even multiple of the cluster size.
    3920                 :            :          */
    3921                 :      12494 :         bs->cluster_sz = opts->cluster_sz;
    3922   [ -  +  -  + ]:      12494 :         bs->total_clusters = dev->blockcnt / (bs->cluster_sz / dev->blocklen);
    3923                 :      12494 :         ctx->used_clusters = spdk_bit_array_create(bs->total_clusters);
    3924         [ -  + ]:      12494 :         if (!ctx->used_clusters) {
    3925                 :          0 :                 spdk_free(ctx->super);
    3926                 :          0 :                 free(ctx);
    3927                 :          0 :                 free(bs);
    3928                 :          0 :                 return -ENOMEM;
    3929                 :            :         }
    3930                 :            : 
    3931                 :      12494 :         bs->pages_per_cluster = bs->cluster_sz / SPDK_BS_PAGE_SIZE;
    3932         [ +  - ]:      12494 :         if (spdk_u32_is_pow2(bs->pages_per_cluster)) {
    3933                 :      12494 :                 bs->pages_per_cluster_shift = spdk_u32log2(bs->pages_per_cluster);
    3934                 :            :         }
    3935                 :      12494 :         bs->num_free_clusters = bs->total_clusters;
    3936                 :      12494 :         bs->io_unit_size = dev->blocklen;
    3937                 :            : 
    3938                 :      12494 :         bs->max_channel_ops = opts->max_channel_ops;
    3939                 :      12494 :         bs->super_blob = SPDK_BLOBID_INVALID;
    3940                 :      12494 :         memcpy(&bs->bstype, &opts->bstype, sizeof(opts->bstype));
    3941                 :      12494 :         bs->esnap_bs_dev_create = opts->esnap_bs_dev_create;
    3942                 :      12494 :         bs->esnap_ctx = opts->esnap_ctx;
    3943                 :            : 
    3944                 :            :         /* The metadata is assumed to be at least 1 page */
    3945                 :      12494 :         bs->used_md_pages = spdk_bit_array_create(1);
    3946                 :      12494 :         bs->used_blobids = spdk_bit_array_create(0);
    3947                 :      12494 :         bs->open_blobids = spdk_bit_array_create(0);
    3948                 :            : 
    3949                 :      12494 :         spdk_spin_init(&bs->used_lock);
    3950                 :            : 
    3951                 :      12494 :         spdk_io_device_register(bs, bs_channel_create, bs_channel_destroy,
    3952                 :            :                                 sizeof(struct spdk_bs_channel), "blobstore");
    3953                 :      12494 :         rc = bs_register_md_thread(bs);
    3954         [ -  + ]:      12494 :         if (rc == -1) {
    3955                 :          0 :                 spdk_io_device_unregister(bs, NULL);
    3956                 :          0 :                 spdk_spin_destroy(&bs->used_lock);
    3957                 :          0 :                 spdk_bit_array_free(&bs->open_blobids);
    3958                 :          0 :                 spdk_bit_array_free(&bs->used_blobids);
    3959                 :          0 :                 spdk_bit_array_free(&bs->used_md_pages);
    3960                 :          0 :                 spdk_bit_array_free(&ctx->used_clusters);
    3961                 :          0 :                 spdk_free(ctx->super);
    3962                 :          0 :                 free(ctx);
    3963                 :          0 :                 free(bs);
    3964                 :            :                 /* FIXME: this is a lie but don't know how to get a proper error code here */
    3965                 :          0 :                 return -ENOMEM;
    3966                 :            :         }
    3967                 :            : 
    3968                 :      12494 :         *_ctx = ctx;
    3969                 :      12494 :         *_bs = bs;
    3970                 :      12494 :         return 0;
    3971                 :            : }
    3972                 :            : 
    3973                 :            : static void
    3974                 :       7672 : bs_load_ctx_fail(struct spdk_bs_load_ctx *ctx, int bserrno)
    3975                 :            : {
    3976         [ -  + ]:       7672 :         assert(bserrno != 0);
    3977                 :            : 
    3978                 :       7672 :         spdk_free(ctx->super);
    3979                 :       7672 :         bs_sequence_finish(ctx->seq, bserrno);
    3980                 :       7672 :         bs_free(ctx->bs);
    3981                 :       7672 :         spdk_bit_array_free(&ctx->used_clusters);
    3982                 :       7672 :         free(ctx);
    3983                 :       7672 : }
    3984                 :            : 
    3985                 :            : static void
    3986                 :       5345 : bs_write_super(spdk_bs_sequence_t *seq, struct spdk_blob_store *bs,
    3987                 :            :                struct spdk_bs_super_block *super, spdk_bs_sequence_cpl cb_fn, void *cb_arg)
    3988                 :            : {
    3989                 :            :         /* Update the values in the super block */
    3990                 :       5345 :         super->super_blob = bs->super_blob;
    3991                 :       5345 :         memcpy(&super->bstype, &bs->bstype, sizeof(bs->bstype));
    3992                 :       5345 :         super->crc = blob_md_page_calc_crc(super);
    3993                 :       5345 :         bs_sequence_write_dev(seq, super, bs_page_to_lba(bs, 0),
    3994                 :       5345 :                               bs_byte_to_lba(bs, sizeof(*super)),
    3995                 :            :                               cb_fn, cb_arg);
    3996                 :       5345 : }
    3997                 :            : 
    3998                 :            : static void
    3999                 :       4726 : bs_write_used_clusters(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
    4000                 :            : {
    4001                 :       4726 :         struct spdk_bs_load_ctx *ctx = arg;
    4002                 :            :         uint64_t        mask_size, lba, lba_count;
    4003                 :            : 
    4004                 :            :         /* Write out the used clusters mask */
    4005                 :       4726 :         mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
    4006                 :       4726 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
    4007                 :            :                                  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    4008         [ -  + ]:       4726 :         if (!ctx->mask) {
    4009                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4010                 :          0 :                 return;
    4011                 :            :         }
    4012                 :            : 
    4013                 :       4726 :         ctx->mask->type = SPDK_MD_MASK_TYPE_USED_CLUSTERS;
    4014                 :       4726 :         ctx->mask->length = ctx->bs->total_clusters;
    4015                 :            :         /* We could get here through the normal unload path, or through dirty
    4016                 :            :          * shutdown recovery.  For the normal unload path, we use the mask from
    4017                 :            :          * the bit pool.  For dirty shutdown recovery, we don't have a bit pool yet -
    4018                 :            :          * only the bit array from the load ctx.
    4019                 :            :          */
    4020         [ +  + ]:       4726 :         if (ctx->bs->used_clusters) {
    4021         [ -  + ]:       4085 :                 assert(ctx->mask->length == spdk_bit_pool_capacity(ctx->bs->used_clusters));
    4022                 :       4085 :                 spdk_bit_pool_store_mask(ctx->bs->used_clusters, ctx->mask->mask);
    4023                 :            :         } else {
    4024         [ -  + ]:        641 :                 assert(ctx->mask->length == spdk_bit_array_capacity(ctx->used_clusters));
    4025                 :        641 :                 spdk_bit_array_store_mask(ctx->used_clusters, ctx->mask->mask);
    4026                 :            :         }
    4027                 :       4726 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
    4028                 :       4726 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
    4029                 :       4726 :         bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
    4030                 :            : }
    4031                 :            : 
    4032                 :            : static void
    4033                 :       4726 : bs_write_used_md(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
    4034                 :            : {
    4035                 :       4726 :         struct spdk_bs_load_ctx *ctx = arg;
    4036                 :            :         uint64_t        mask_size, lba, lba_count;
    4037                 :            : 
    4038                 :       4726 :         mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
    4039                 :       4726 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
    4040                 :            :                                  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    4041         [ -  + ]:       4726 :         if (!ctx->mask) {
    4042                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4043                 :          0 :                 return;
    4044                 :            :         }
    4045                 :            : 
    4046                 :       4726 :         ctx->mask->type = SPDK_MD_MASK_TYPE_USED_PAGES;
    4047                 :       4726 :         ctx->mask->length = ctx->super->md_len;
    4048         [ -  + ]:       4726 :         assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_md_pages));
    4049                 :            : 
    4050                 :       4726 :         spdk_bit_array_store_mask(ctx->bs->used_md_pages, ctx->mask->mask);
    4051                 :       4726 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
    4052                 :       4726 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
    4053                 :       4726 :         bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
    4054                 :            : }
    4055                 :            : 
    4056                 :            : static void
    4057                 :       4726 : bs_write_used_blobids(spdk_bs_sequence_t *seq, void *arg, spdk_bs_sequence_cpl cb_fn)
    4058                 :            : {
    4059                 :       4726 :         struct spdk_bs_load_ctx *ctx = arg;
    4060                 :            :         uint64_t        mask_size, lba, lba_count;
    4061                 :            : 
    4062         [ +  + ]:       4726 :         if (ctx->super->used_blobid_mask_len == 0) {
    4063                 :            :                 /*
    4064                 :            :                  * This is a pre-v3 on-disk format where the blobid mask does not get
    4065                 :            :                  *  written to disk.
    4066                 :            :                  */
    4067                 :        144 :                 cb_fn(seq, arg, 0);
    4068                 :        144 :                 return;
    4069                 :            :         }
    4070                 :            : 
    4071                 :       4582 :         mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
    4072                 :       4582 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
    4073                 :            :                                  SPDK_MALLOC_DMA);
    4074         [ -  + ]:       4582 :         if (!ctx->mask) {
    4075                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4076                 :          0 :                 return;
    4077                 :            :         }
    4078                 :            : 
    4079                 :       4582 :         ctx->mask->type = SPDK_MD_MASK_TYPE_USED_BLOBIDS;
    4080                 :       4582 :         ctx->mask->length = ctx->super->md_len;
    4081         [ -  + ]:       4582 :         assert(ctx->mask->length == spdk_bit_array_capacity(ctx->bs->used_blobids));
    4082                 :            : 
    4083                 :       4582 :         spdk_bit_array_store_mask(ctx->bs->used_blobids, ctx->mask->mask);
    4084                 :       4582 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
    4085                 :       4582 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
    4086                 :       4582 :         bs_sequence_write_dev(seq, ctx->mask, lba, lba_count, cb_fn, arg);
    4087                 :            : }
    4088                 :            : 
    4089                 :            : static void
    4090                 :       4349 : blob_set_thin_provision(struct spdk_blob *blob)
    4091                 :            : {
    4092                 :       4349 :         blob_verify_md_op(blob);
    4093                 :       4349 :         blob->invalid_flags |= SPDK_BLOB_THIN_PROV;
    4094                 :       4349 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    4095                 :       4349 : }
    4096                 :            : 
    4097                 :            : static void
    4098                 :      15939 : blob_set_clear_method(struct spdk_blob *blob, enum blob_clear_method clear_method)
    4099                 :            : {
    4100                 :      15939 :         blob_verify_md_op(blob);
    4101                 :      15939 :         blob->clear_method = clear_method;
    4102                 :      15939 :         blob->md_ro_flags |= (clear_method << SPDK_BLOB_CLEAR_METHOD_SHIFT);
    4103                 :      15939 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    4104                 :      15939 : }
    4105                 :            : 
    4106                 :            : static void bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno);
    4107                 :            : 
    4108                 :            : static void
    4109                 :        144 : bs_delete_corrupted_blob_cpl(void *cb_arg, int bserrno)
    4110                 :            : {
    4111                 :        144 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4112                 :            :         spdk_blob_id id;
    4113                 :            :         int64_t page_num;
    4114                 :            : 
    4115                 :            :         /* Iterate to next blob (we can't use spdk_bs_iter_next function as our
    4116                 :            :          * last blob has been removed */
    4117                 :        144 :         page_num = bs_blobid_to_page(ctx->blobid);
    4118                 :        144 :         page_num++;
    4119                 :        144 :         page_num = spdk_bit_array_find_first_set(ctx->bs->used_blobids, page_num);
    4120         [ +  - ]:        144 :         if (page_num >= spdk_bit_array_capacity(ctx->bs->used_blobids)) {
    4121                 :        144 :                 bs_load_iter(ctx, NULL, -ENOENT);
    4122                 :        144 :                 return;
    4123                 :            :         }
    4124                 :            : 
    4125                 :          0 :         id = bs_page_to_blobid(page_num);
    4126                 :            : 
    4127                 :          0 :         spdk_bs_open_blob(ctx->bs, id, bs_load_iter, ctx);
    4128                 :            : }
    4129                 :            : 
    4130                 :            : static void
    4131                 :        144 : bs_delete_corrupted_close_cb(void *cb_arg, int bserrno)
    4132                 :            : {
    4133                 :        144 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4134                 :            : 
    4135         [ -  + ]:        144 :         if (bserrno != 0) {
    4136                 :          0 :                 SPDK_ERRLOG("Failed to close corrupted blob\n");
    4137                 :          0 :                 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
    4138                 :          0 :                 return;
    4139                 :            :         }
    4140                 :            : 
    4141                 :        144 :         spdk_bs_delete_blob(ctx->bs, ctx->blobid, bs_delete_corrupted_blob_cpl, ctx);
    4142                 :            : }
    4143                 :            : 
    4144                 :            : static void
    4145                 :        144 : bs_delete_corrupted_blob(void *cb_arg, int bserrno)
    4146                 :            : {
    4147                 :        144 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4148                 :            :         uint64_t i;
    4149                 :            : 
    4150         [ -  + ]:        144 :         if (bserrno != 0) {
    4151                 :          0 :                 SPDK_ERRLOG("Failed to close clone of a corrupted blob\n");
    4152                 :          0 :                 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
    4153                 :          0 :                 return;
    4154                 :            :         }
    4155                 :            : 
    4156                 :            :         /* Snapshot and clone have the same copy of cluster map and extent pages
    4157                 :            :          * at this point. Let's clear both for snapshot now,
    4158                 :            :          * so that it won't be cleared for clone later when we remove snapshot.
    4159                 :            :          * Also set thin provision to pass data corruption check */
    4160         [ +  + ]:       1584 :         for (i = 0; i < ctx->blob->active.num_clusters; i++) {
    4161                 :       1440 :                 ctx->blob->active.clusters[i] = 0;
    4162                 :            :         }
    4163         [ +  + ]:        216 :         for (i = 0; i < ctx->blob->active.num_extent_pages; i++) {
    4164                 :         72 :                 ctx->blob->active.extent_pages[i] = 0;
    4165                 :            :         }
    4166                 :            : 
    4167                 :        144 :         ctx->blob->active.num_allocated_clusters = 0;
    4168                 :            : 
    4169                 :        144 :         ctx->blob->md_ro = false;
    4170                 :            : 
    4171                 :        144 :         blob_set_thin_provision(ctx->blob);
    4172                 :            : 
    4173                 :        144 :         ctx->blobid = ctx->blob->id;
    4174                 :            : 
    4175                 :        144 :         spdk_blob_close(ctx->blob, bs_delete_corrupted_close_cb, ctx);
    4176                 :            : }
    4177                 :            : 
    4178                 :            : static void
    4179                 :         72 : bs_update_corrupted_blob(void *cb_arg, int bserrno)
    4180                 :            : {
    4181                 :         72 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4182                 :            : 
    4183         [ -  + ]:         72 :         if (bserrno != 0) {
    4184                 :          0 :                 SPDK_ERRLOG("Failed to close clone of a corrupted blob\n");
    4185                 :          0 :                 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
    4186                 :          0 :                 return;
    4187                 :            :         }
    4188                 :            : 
    4189                 :         72 :         ctx->blob->md_ro = false;
    4190                 :         72 :         blob_remove_xattr(ctx->blob, SNAPSHOT_PENDING_REMOVAL, true);
    4191                 :         72 :         blob_remove_xattr(ctx->blob, SNAPSHOT_IN_PROGRESS, true);
    4192                 :         72 :         spdk_blob_set_read_only(ctx->blob);
    4193                 :            : 
    4194         [ -  + ]:         72 :         if (ctx->iter_cb_fn) {
    4195                 :          0 :                 ctx->iter_cb_fn(ctx->iter_cb_arg, ctx->blob, 0);
    4196                 :            :         }
    4197                 :         72 :         bs_blob_list_add(ctx->blob);
    4198                 :            : 
    4199                 :         72 :         spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
    4200                 :            : }
    4201                 :            : 
    4202                 :            : static void
    4203                 :        216 : bs_examine_clone(void *cb_arg, struct spdk_blob *blob, int bserrno)
    4204                 :            : {
    4205                 :        216 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4206                 :            : 
    4207         [ -  + ]:        216 :         if (bserrno != 0) {
    4208                 :          0 :                 SPDK_ERRLOG("Failed to open clone of a corrupted blob\n");
    4209                 :          0 :                 spdk_bs_iter_next(ctx->bs, ctx->blob, bs_load_iter, ctx);
    4210                 :          0 :                 return;
    4211                 :            :         }
    4212                 :            : 
    4213         [ +  + ]:        216 :         if (blob->parent_id == ctx->blob->id) {
    4214                 :            :                 /* Power failure occurred before updating clone (snapshot delete case)
    4215                 :            :                  * or after updating clone (creating snapshot case) - keep snapshot */
    4216                 :         72 :                 spdk_blob_close(blob, bs_update_corrupted_blob, ctx);
    4217                 :            :         } else {
    4218                 :            :                 /* Power failure occurred after updating clone (snapshot delete case)
    4219                 :            :                  * or before updating clone (creating snapshot case) - remove snapshot */
    4220                 :        144 :                 spdk_blob_close(blob, bs_delete_corrupted_blob, ctx);
    4221                 :            :         }
    4222                 :            : }
    4223                 :            : 
    4224                 :            : static void
    4225                 :      11353 : bs_load_iter(void *arg, struct spdk_blob *blob, int bserrno)
    4226                 :            : {
    4227                 :      11353 :         struct spdk_bs_load_ctx *ctx = arg;
    4228                 :      10593 :         const void *value;
    4229                 :      10593 :         size_t len;
    4230                 :      11353 :         int rc = 0;
    4231                 :            : 
    4232         [ +  + ]:      11353 :         if (bserrno == 0) {
    4233                 :            :                 /* Examine blob if it is corrupted after power failure. Fix
    4234                 :            :                  * the ones that can be fixed and remove any other corrupted
    4235                 :            :                  * ones. If it is not corrupted just process it */
    4236                 :       9582 :                 rc = blob_get_xattr_value(blob, SNAPSHOT_PENDING_REMOVAL, &value, &len, true);
    4237         [ +  + ]:       9582 :                 if (rc != 0) {
    4238                 :       9462 :                         rc = blob_get_xattr_value(blob, SNAPSHOT_IN_PROGRESS, &value, &len, true);
    4239         [ +  + ]:       9462 :                         if (rc != 0) {
    4240                 :            :                                 /* Not corrupted - process it and continue with iterating through blobs */
    4241         [ +  + ]:       9366 :                                 if (ctx->iter_cb_fn) {
    4242                 :       5531 :                                         ctx->iter_cb_fn(ctx->iter_cb_arg, blob, 0);
    4243                 :            :                                 }
    4244                 :       9366 :                                 bs_blob_list_add(blob);
    4245                 :       9366 :                                 spdk_bs_iter_next(ctx->bs, blob, bs_load_iter, ctx);
    4246                 :       9402 :                                 return;
    4247                 :            :                         }
    4248                 :            : 
    4249                 :            :                 }
    4250                 :            : 
    4251         [ -  + ]:        216 :                 assert(len == sizeof(spdk_blob_id));
    4252                 :            : 
    4253                 :        216 :                 ctx->blob = blob;
    4254                 :            : 
    4255                 :            :                 /* Open clone to check if we are able to fix this blob or should we remove it */
    4256                 :        216 :                 spdk_bs_open_blob(ctx->bs, *(spdk_blob_id *)value, bs_examine_clone, ctx);
    4257                 :        216 :                 return;
    4258         [ +  - ]:       1771 :         } else if (bserrno == -ENOENT) {
    4259                 :       1771 :                 bserrno = 0;
    4260                 :            :         } else {
    4261                 :            :                 /*
    4262                 :            :                  * This case needs to be looked at further.  Same problem
    4263                 :            :                  *  exists with applications that rely on explicit blob
    4264                 :            :                  *  iteration.  We should just skip the blob that failed
    4265                 :            :                  *  to load and continue on to the next one.
    4266                 :            :                  */
    4267                 :          0 :                 SPDK_ERRLOG("Error in iterating blobs\n");
    4268                 :            :         }
    4269                 :            : 
    4270                 :       1771 :         ctx->iter_cb_fn = NULL;
    4271                 :            : 
    4272                 :       1771 :         spdk_free(ctx->super);
    4273                 :       1771 :         spdk_free(ctx->mask);
    4274                 :       1771 :         bs_sequence_finish(ctx->seq, bserrno);
    4275                 :       1771 :         free(ctx);
    4276                 :            : }
    4277                 :            : 
    4278                 :            : static void bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg);
    4279                 :            : 
    4280                 :            : static void
    4281                 :       1771 : bs_load_complete(struct spdk_bs_load_ctx *ctx)
    4282                 :            : {
    4283                 :       1771 :         ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters);
    4284   [ -  +  -  + ]:       1771 :         if (ctx->dumping) {
    4285                 :          0 :                 bs_dump_read_md_page(ctx->seq, ctx);
    4286                 :          0 :                 return;
    4287                 :            :         }
    4288                 :       1771 :         spdk_bs_iter_first(ctx->bs, bs_load_iter, ctx);
    4289                 :            : }
    4290                 :            : 
    4291                 :            : static void
    4292                 :       1130 : bs_load_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4293                 :            : {
    4294                 :       1130 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4295                 :            :         int rc;
    4296                 :            : 
    4297                 :            :         /* The type must be correct */
    4298         [ -  + ]:       1130 :         assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_BLOBIDS);
    4299                 :            : 
    4300                 :            :         /* The length of the mask (in bits) must not be greater than
    4301                 :            :          * the length of the buffer (converted to bits) */
    4302         [ -  + ]:       1130 :         assert(ctx->mask->length <= (ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE * 8));
    4303                 :            : 
    4304                 :            :         /* The length of the mask must be exactly equal to the size
    4305                 :            :          * (in pages) of the metadata region */
    4306         [ -  + ]:       1130 :         assert(ctx->mask->length == ctx->super->md_len);
    4307                 :            : 
    4308                 :       1130 :         rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->mask->length);
    4309         [ -  + ]:       1130 :         if (rc < 0) {
    4310                 :          0 :                 spdk_free(ctx->mask);
    4311                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    4312                 :          0 :                 return;
    4313                 :            :         }
    4314                 :            : 
    4315                 :       1130 :         spdk_bit_array_load_mask(ctx->bs->used_blobids, ctx->mask->mask);
    4316                 :       1130 :         bs_load_complete(ctx);
    4317                 :            : }
    4318                 :            : 
    4319                 :            : static void
    4320                 :       1130 : bs_load_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4321                 :            : {
    4322                 :       1130 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4323                 :            :         uint64_t                lba, lba_count, mask_size;
    4324                 :            :         int                     rc;
    4325                 :            : 
    4326         [ -  + ]:       1130 :         if (bserrno != 0) {
    4327                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4328                 :          0 :                 return;
    4329                 :            :         }
    4330                 :            : 
    4331                 :            :         /* The type must be correct */
    4332         [ -  + ]:       1130 :         assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
    4333                 :            :         /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
    4334         [ -  + ]:       1130 :         assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
    4335                 :            :                                              struct spdk_blob_md_page) * 8));
    4336                 :            :         /*
    4337                 :            :          * The length of the mask must be equal to or larger than the total number of clusters. It may be
    4338                 :            :          * larger than the total number of clusters due to a failure spdk_bs_grow.
    4339                 :            :          */
    4340         [ -  + ]:       1130 :         assert(ctx->mask->length >= ctx->bs->total_clusters);
    4341         [ +  + ]:       1130 :         if (ctx->mask->length > ctx->bs->total_clusters) {
    4342                 :         24 :                 SPDK_WARNLOG("Shrink the used_custers mask length to total_clusters");
    4343                 :         24 :                 ctx->mask->length = ctx->bs->total_clusters;
    4344                 :            :         }
    4345                 :            : 
    4346                 :       1130 :         rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->mask->length);
    4347         [ -  + ]:       1130 :         if (rc < 0) {
    4348                 :          0 :                 spdk_free(ctx->mask);
    4349                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    4350                 :          0 :                 return;
    4351                 :            :         }
    4352                 :            : 
    4353                 :       1130 :         spdk_bit_array_load_mask(ctx->used_clusters, ctx->mask->mask);
    4354                 :       1130 :         ctx->bs->num_free_clusters = spdk_bit_array_count_clear(ctx->used_clusters);
    4355         [ -  + ]:       1130 :         assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters);
    4356                 :            : 
    4357                 :       1130 :         spdk_free(ctx->mask);
    4358                 :            : 
    4359                 :            :         /* Read the used blobids mask */
    4360                 :       1130 :         mask_size = ctx->super->used_blobid_mask_len * SPDK_BS_PAGE_SIZE;
    4361                 :       1130 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
    4362                 :            :                                  SPDK_MALLOC_DMA);
    4363         [ -  + ]:       1130 :         if (!ctx->mask) {
    4364                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4365                 :          0 :                 return;
    4366                 :            :         }
    4367                 :       1130 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_start);
    4368                 :       1130 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_blobid_mask_len);
    4369                 :       1130 :         bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
    4370                 :            :                              bs_load_used_blobids_cpl, ctx);
    4371                 :            : }
    4372                 :            : 
    4373                 :            : static void
    4374                 :       1130 : bs_load_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4375                 :            : {
    4376                 :       1130 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4377                 :            :         uint64_t                lba, lba_count, mask_size;
    4378                 :            :         int                     rc;
    4379                 :            : 
    4380         [ -  + ]:       1130 :         if (bserrno != 0) {
    4381                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4382                 :          0 :                 return;
    4383                 :            :         }
    4384                 :            : 
    4385                 :            :         /* The type must be correct */
    4386         [ -  + ]:       1130 :         assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_PAGES);
    4387                 :            :         /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
    4388         [ -  + ]:       1130 :         assert(ctx->mask->length <= (ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE *
    4389                 :            :                                      8));
    4390                 :            :         /* The length of the mask must be exactly equal to the size (in pages) of the metadata region */
    4391         [ -  + ]:       1130 :         if (ctx->mask->length != ctx->super->md_len) {
    4392                 :          0 :                 SPDK_ERRLOG("mismatched md_len in used_pages mask: "
    4393                 :            :                             "mask->length=%" PRIu32 " super->md_len=%" PRIu32 "\n",
    4394                 :            :                             ctx->mask->length, ctx->super->md_len);
    4395                 :          0 :                 assert(false);
    4396                 :            :         }
    4397                 :            : 
    4398                 :       1130 :         rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->mask->length);
    4399         [ -  + ]:       1130 :         if (rc < 0) {
    4400                 :          0 :                 spdk_free(ctx->mask);
    4401                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    4402                 :          0 :                 return;
    4403                 :            :         }
    4404                 :            : 
    4405                 :       1130 :         spdk_bit_array_load_mask(ctx->bs->used_md_pages, ctx->mask->mask);
    4406                 :       1130 :         spdk_free(ctx->mask);
    4407                 :            : 
    4408                 :            :         /* Read the used clusters mask */
    4409                 :       1130 :         mask_size = ctx->super->used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
    4410                 :       1130 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
    4411                 :            :                                  SPDK_MALLOC_DMA);
    4412         [ -  + ]:       1130 :         if (!ctx->mask) {
    4413                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4414                 :          0 :                 return;
    4415                 :            :         }
    4416                 :       1130 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
    4417                 :       1130 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
    4418                 :       1130 :         bs_sequence_read_dev(seq, ctx->mask, lba, lba_count,
    4419                 :            :                              bs_load_used_clusters_cpl, ctx);
    4420                 :            : }
    4421                 :            : 
    4422                 :            : static void
    4423                 :       1130 : bs_load_read_used_pages(struct spdk_bs_load_ctx *ctx)
    4424                 :            : {
    4425                 :            :         uint64_t lba, lba_count, mask_size;
    4426                 :            : 
    4427                 :            :         /* Read the used pages mask */
    4428                 :       1130 :         mask_size = ctx->super->used_page_mask_len * SPDK_BS_PAGE_SIZE;
    4429                 :       1130 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL,
    4430                 :            :                                  SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    4431         [ -  + ]:       1130 :         if (!ctx->mask) {
    4432                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4433                 :          0 :                 return;
    4434                 :            :         }
    4435                 :            : 
    4436                 :       1130 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_start);
    4437                 :       1130 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_page_mask_len);
    4438                 :       1130 :         bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count,
    4439                 :            :                              bs_load_used_pages_cpl, ctx);
    4440                 :            : }
    4441                 :            : 
    4442                 :            : static int
    4443                 :       1492 : bs_load_replay_md_parse_page(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_page *page)
    4444                 :            : {
    4445                 :       1492 :         struct spdk_blob_store *bs = ctx->bs;
    4446                 :            :         struct spdk_blob_md_descriptor *desc;
    4447                 :       1492 :         size_t  cur_desc = 0;
    4448                 :            : 
    4449                 :       1492 :         desc = (struct spdk_blob_md_descriptor *)page->descriptors;
    4450         [ +  - ]:       4370 :         while (cur_desc < sizeof(page->descriptors)) {
    4451         [ +  + ]:       4370 :                 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
    4452         [ +  - ]:       1372 :                         if (desc->length == 0) {
    4453                 :            :                                 /* If padding and length are 0, this terminates the page */
    4454                 :       1372 :                                 break;
    4455                 :            :                         }
    4456         [ +  + ]:       2998 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
    4457                 :            :                         struct spdk_blob_md_descriptor_extent_rle       *desc_extent_rle;
    4458                 :            :                         unsigned int                            i, j;
    4459                 :        408 :                         unsigned int                            cluster_count = 0;
    4460                 :            :                         uint32_t                                cluster_idx;
    4461                 :            : 
    4462                 :        408 :                         desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
    4463                 :            : 
    4464         [ +  + ]:        816 :                         for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
    4465         [ +  + ]:       4968 :                                 for (j = 0; j < desc_extent_rle->extents[i].length; j++) {
    4466                 :       4560 :                                         cluster_idx = desc_extent_rle->extents[i].cluster_idx;
    4467                 :            :                                         /*
    4468                 :            :                                          * cluster_idx = 0 means an unallocated cluster - don't mark that
    4469                 :            :                                          * in the used cluster map.
    4470                 :            :                                          */
    4471         [ +  + ]:       4560 :                                         if (cluster_idx != 0) {
    4472                 :       3240 :                                                 SPDK_NOTICELOG("Recover: cluster %" PRIu32 "\n", cluster_idx + j);
    4473                 :       3240 :                                                 spdk_bit_array_set(ctx->used_clusters, cluster_idx + j);
    4474         [ -  + ]:       3240 :                                                 if (bs->num_free_clusters == 0) {
    4475                 :          0 :                                                         return -ENOSPC;
    4476                 :            :                                                 }
    4477                 :       3240 :                                                 bs->num_free_clusters--;
    4478                 :            :                                         }
    4479                 :       4560 :                                         cluster_count++;
    4480                 :            :                                 }
    4481                 :            :                         }
    4482         [ -  + ]:        408 :                         if (cluster_count == 0) {
    4483                 :          0 :                                 return -EINVAL;
    4484                 :            :                         }
    4485         [ +  + ]:       2590 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
    4486                 :            :                         struct spdk_blob_md_descriptor_extent_page      *desc_extent;
    4487                 :            :                         uint32_t                                        i;
    4488                 :        318 :                         uint32_t                                        cluster_count = 0;
    4489                 :            :                         uint32_t                                        cluster_idx;
    4490                 :            :                         size_t                                          cluster_idx_length;
    4491                 :            : 
    4492                 :        318 :                         desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
    4493                 :        318 :                         cluster_idx_length = desc_extent->length - sizeof(desc_extent->start_cluster_idx);
    4494                 :            : 
    4495         [ +  - ]:        318 :                         if (desc_extent->length <= sizeof(desc_extent->start_cluster_idx) ||
    4496         [ -  + ]:        318 :                             (cluster_idx_length % sizeof(desc_extent->cluster_idx[0]) != 0)) {
    4497                 :          0 :                                 return -EINVAL;
    4498                 :            :                         }
    4499                 :            : 
    4500         [ +  + ]:       4838 :                         for (i = 0; i < cluster_idx_length / sizeof(desc_extent->cluster_idx[0]); i++) {
    4501                 :       4520 :                                 cluster_idx = desc_extent->cluster_idx[i];
    4502                 :            :                                 /*
    4503                 :            :                                  * cluster_idx = 0 means an unallocated cluster - don't mark that
    4504                 :            :                                  * in the used cluster map.
    4505                 :            :                                  */
    4506         [ +  + ]:       4520 :                                 if (cluster_idx != 0) {
    4507         [ +  + ]:       3754 :                                         if (cluster_idx < desc_extent->start_cluster_idx &&
    4508         [ -  + ]:          1 :                                             cluster_idx >= desc_extent->start_cluster_idx + cluster_count) {
    4509                 :          0 :                                                 return -EINVAL;
    4510                 :            :                                         }
    4511                 :       3754 :                                         spdk_bit_array_set(ctx->used_clusters, cluster_idx);
    4512         [ -  + ]:       3754 :                                         if (bs->num_free_clusters == 0) {
    4513                 :          0 :                                                 return -ENOSPC;
    4514                 :            :                                         }
    4515                 :       3754 :                                         bs->num_free_clusters--;
    4516                 :            :                                 }
    4517                 :       4520 :                                 cluster_count++;
    4518                 :            :                         }
    4519                 :            : 
    4520         [ -  + ]:        318 :                         if (cluster_count == 0) {
    4521                 :          0 :                                 return -EINVAL;
    4522                 :            :                         }
    4523         [ +  + ]:       2272 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
    4524                 :            :                         /* Skip this item */
    4525         [ +  + ]:       1796 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
    4526                 :            :                         /* Skip this item */
    4527         [ +  + ]:       1436 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
    4528                 :            :                         /* Skip this item */
    4529         [ +  - ]:        502 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
    4530                 :            :                         struct spdk_blob_md_descriptor_extent_table *desc_extent_table;
    4531                 :        502 :                         uint32_t num_extent_pages = ctx->num_extent_pages;
    4532                 :            :                         uint32_t i;
    4533                 :            :                         size_t extent_pages_length;
    4534                 :            :                         void *tmp;
    4535                 :            : 
    4536                 :        502 :                         desc_extent_table = (struct spdk_blob_md_descriptor_extent_table *)desc;
    4537                 :        502 :                         extent_pages_length = desc_extent_table->length - sizeof(desc_extent_table->num_clusters);
    4538                 :            : 
    4539         [ +  - ]:        502 :                         if (desc_extent_table->length == 0 ||
    4540         [ -  + ]:        502 :                             (extent_pages_length % sizeof(desc_extent_table->extent_page[0]) != 0)) {
    4541                 :          0 :                                 return -EINVAL;
    4542                 :            :                         }
    4543                 :            : 
    4544         [ +  + ]:        977 :                         for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
    4545         [ +  + ]:        475 :                                 if (desc_extent_table->extent_page[i].page_idx != 0) {
    4546         [ -  + ]:        318 :                                         if (desc_extent_table->extent_page[i].num_pages != 1) {
    4547                 :          0 :                                                 return -EINVAL;
    4548                 :            :                                         }
    4549                 :        318 :                                         num_extent_pages += 1;
    4550                 :            :                                 }
    4551                 :            :                         }
    4552                 :            : 
    4553         [ +  + ]:        502 :                         if (num_extent_pages > 0) {
    4554                 :        317 :                                 tmp = realloc(ctx->extent_page_num, num_extent_pages * sizeof(uint32_t));
    4555         [ -  + ]:        317 :                                 if (tmp == NULL) {
    4556                 :          0 :                                         return -ENOMEM;
    4557                 :            :                                 }
    4558                 :        317 :                                 ctx->extent_page_num = tmp;
    4559                 :            : 
    4560                 :            :                                 /* Extent table entries contain md page numbers for extent pages.
    4561                 :            :                                  * Zeroes represent unallocated extent pages, those are run-length-encoded.
    4562                 :            :                                  */
    4563         [ +  + ]:        636 :                                 for (i = 0; i < extent_pages_length / sizeof(desc_extent_table->extent_page[0]); i++) {
    4564         [ +  + ]:        319 :                                         if (desc_extent_table->extent_page[i].page_idx != 0) {
    4565                 :        318 :                                                 ctx->extent_page_num[ctx->num_extent_pages] = desc_extent_table->extent_page[i].page_idx;
    4566                 :        318 :                                                 ctx->num_extent_pages += 1;
    4567                 :            :                                         }
    4568                 :            :                                 }
    4569                 :            :                         }
    4570                 :            :                 } else {
    4571                 :            :                         /* Error */
    4572                 :          0 :                         return -EINVAL;
    4573                 :            :                 }
    4574                 :            :                 /* Advance to the next descriptor */
    4575                 :       2998 :                 cur_desc += sizeof(*desc) + desc->length;
    4576         [ +  + ]:       2998 :                 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
    4577                 :        120 :                         break;
    4578                 :            :                 }
    4579                 :       2878 :                 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
    4580                 :            :         }
    4581                 :       1492 :         return 0;
    4582                 :            : }
    4583                 :            : 
    4584                 :            : static bool
    4585                 :      34830 : bs_load_cur_extent_page_valid(struct spdk_blob_md_page *page)
    4586                 :            : {
    4587                 :            :         uint32_t crc;
    4588                 :      34830 :         struct spdk_blob_md_descriptor *desc = (struct spdk_blob_md_descriptor *)page->descriptors;
    4589                 :            :         size_t desc_len;
    4590                 :            : 
    4591                 :      34830 :         crc = blob_md_page_calc_crc(page);
    4592         [ -  + ]:      34830 :         if (crc != page->crc) {
    4593                 :          0 :                 return false;
    4594                 :            :         }
    4595                 :            : 
    4596                 :            :         /* Extent page should always be of sequence num 0. */
    4597         [ +  + ]:      34830 :         if (page->sequence_num != 0) {
    4598                 :        264 :                 return false;
    4599                 :            :         }
    4600                 :            : 
    4601                 :            :         /* Descriptor type must be EXTENT_PAGE. */
    4602         [ +  + ]:      34566 :         if (desc->type != SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
    4603                 :        934 :                 return false;
    4604                 :            :         }
    4605                 :            : 
    4606                 :            :         /* Descriptor length cannot exceed the page. */
    4607                 :      33632 :         desc_len = sizeof(*desc) + desc->length;
    4608         [ -  + ]:      33632 :         if (desc_len > sizeof(page->descriptors)) {
    4609                 :          0 :                 return false;
    4610                 :            :         }
    4611                 :            : 
    4612                 :            :         /* It has to be the only descriptor in the page. */
    4613         [ +  - ]:      33632 :         if (desc_len + sizeof(*desc) <= sizeof(page->descriptors)) {
    4614                 :      33632 :                 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + desc_len);
    4615         [ -  + ]:      33632 :                 if (desc->length != 0) {
    4616                 :          0 :                         return false;
    4617                 :            :                 }
    4618                 :            :         }
    4619                 :            : 
    4620                 :      33632 :         return true;
    4621                 :            : }
    4622                 :            : 
    4623                 :            : static bool
    4624                 :      42398 : bs_load_cur_md_page_valid(struct spdk_bs_load_ctx *ctx)
    4625                 :            : {
    4626                 :            :         uint32_t crc;
    4627                 :      42398 :         struct spdk_blob_md_page *page = ctx->page;
    4628                 :            : 
    4629                 :      42398 :         crc = blob_md_page_calc_crc(page);
    4630         [ +  + ]:      42398 :         if (crc != page->crc) {
    4631                 :      41092 :                 return false;
    4632                 :            :         }
    4633                 :            : 
    4634                 :            :         /* First page of a sequence should match the blobid. */
    4635         [ +  + ]:       1306 :         if (page->sequence_num == 0 &&
    4636         [ +  + ]:       1042 :             bs_page_to_blobid(ctx->cur_page) != page->id) {
    4637                 :        108 :                 return false;
    4638                 :            :         }
    4639         [ -  + ]:       1198 :         assert(bs_load_cur_extent_page_valid(page) == false);
    4640                 :            : 
    4641                 :       1198 :         return true;
    4642                 :            : }
    4643                 :            : 
    4644                 :            : static void bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx);
    4645                 :            : 
    4646                 :            : static void
    4647                 :        641 : bs_load_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4648                 :            : {
    4649                 :        641 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4650                 :            : 
    4651         [ -  + ]:        641 :         if (bserrno != 0) {
    4652                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4653                 :          0 :                 return;
    4654                 :            :         }
    4655                 :            : 
    4656                 :        641 :         bs_load_complete(ctx);
    4657                 :            : }
    4658                 :            : 
    4659                 :            : static void
    4660                 :        641 : bs_load_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4661                 :            : {
    4662                 :        641 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4663                 :            : 
    4664                 :        641 :         spdk_free(ctx->mask);
    4665                 :        641 :         ctx->mask = NULL;
    4666                 :            : 
    4667         [ -  + ]:        641 :         if (bserrno != 0) {
    4668                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4669                 :          0 :                 return;
    4670                 :            :         }
    4671                 :            : 
    4672                 :        641 :         bs_write_used_clusters(seq, ctx, bs_load_write_used_clusters_cpl);
    4673                 :            : }
    4674                 :            : 
    4675                 :            : static void
    4676                 :        641 : bs_load_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4677                 :            : {
    4678                 :        641 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4679                 :            : 
    4680                 :        641 :         spdk_free(ctx->mask);
    4681                 :        641 :         ctx->mask = NULL;
    4682                 :            : 
    4683         [ -  + ]:        641 :         if (bserrno != 0) {
    4684                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4685                 :          0 :                 return;
    4686                 :            :         }
    4687                 :            : 
    4688                 :        641 :         bs_write_used_blobids(seq, ctx, bs_load_write_used_blobids_cpl);
    4689                 :            : }
    4690                 :            : 
    4691                 :            : static void
    4692                 :        641 : bs_load_write_used_md(struct spdk_bs_load_ctx *ctx)
    4693                 :            : {
    4694                 :        641 :         bs_write_used_md(ctx->seq, ctx, bs_load_write_used_pages_cpl);
    4695                 :        641 : }
    4696                 :            : 
    4697                 :            : static void
    4698                 :      42158 : bs_load_replay_md_chain_cpl(struct spdk_bs_load_ctx *ctx)
    4699                 :            : {
    4700                 :            :         uint64_t num_md_clusters;
    4701                 :            :         uint64_t i;
    4702                 :            : 
    4703                 :      42158 :         ctx->in_page_chain = false;
    4704                 :            : 
    4705                 :            :         do {
    4706                 :      42584 :                 ctx->page_index++;
    4707         [ +  + ]:      42584 :         } while (spdk_bit_array_get(ctx->bs->used_md_pages, ctx->page_index) == true);
    4708                 :            : 
    4709         [ +  + ]:      42158 :         if (ctx->page_index < ctx->super->md_len) {
    4710                 :      41517 :                 ctx->cur_page = ctx->page_index;
    4711                 :      41517 :                 bs_load_replay_cur_md_page(ctx);
    4712                 :            :         } else {
    4713                 :            :                 /* Claim all of the clusters used by the metadata */
    4714                 :        641 :                 num_md_clusters = spdk_divide_round_up(
    4715                 :       1070 :                                           ctx->super->md_start + ctx->super->md_len, ctx->bs->pages_per_cluster);
    4716         [ +  + ]:       2891 :                 for (i = 0; i < num_md_clusters; i++) {
    4717                 :       2250 :                         spdk_bit_array_set(ctx->used_clusters, i);
    4718                 :            :                 }
    4719                 :        641 :                 ctx->bs->num_free_clusters -= num_md_clusters;
    4720                 :        641 :                 spdk_free(ctx->page);
    4721                 :        641 :                 bs_load_write_used_md(ctx);
    4722                 :            :         }
    4723                 :      42158 : }
    4724                 :            : 
    4725                 :            : static void
    4726                 :        317 : bs_load_replay_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4727                 :            : {
    4728                 :        317 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4729                 :            :         uint32_t page_num;
    4730                 :            :         uint64_t i;
    4731                 :            : 
    4732         [ -  + ]:        317 :         if (bserrno != 0) {
    4733                 :          0 :                 spdk_free(ctx->extent_pages);
    4734                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4735                 :          0 :                 return;
    4736                 :            :         }
    4737                 :            : 
    4738         [ +  + ]:        635 :         for (i = 0; i < ctx->num_extent_pages; i++) {
    4739                 :            :                 /* Extent pages are only read when present within in chain md.
    4740                 :            :                  * Integrity of md is not right if that page was not a valid extent page. */
    4741         [ -  + ]:        318 :                 if (bs_load_cur_extent_page_valid(&ctx->extent_pages[i]) != true) {
    4742                 :          0 :                         spdk_free(ctx->extent_pages);
    4743                 :          0 :                         bs_load_ctx_fail(ctx, -EILSEQ);
    4744                 :          0 :                         return;
    4745                 :            :                 }
    4746                 :            : 
    4747                 :        318 :                 page_num = ctx->extent_page_num[i];
    4748                 :        318 :                 spdk_bit_array_set(ctx->bs->used_md_pages, page_num);
    4749         [ -  + ]:        318 :                 if (bs_load_replay_md_parse_page(ctx, &ctx->extent_pages[i])) {
    4750                 :          0 :                         spdk_free(ctx->extent_pages);
    4751                 :          0 :                         bs_load_ctx_fail(ctx, -EILSEQ);
    4752                 :          0 :                         return;
    4753                 :            :                 }
    4754                 :            :         }
    4755                 :            : 
    4756                 :        317 :         spdk_free(ctx->extent_pages);
    4757                 :        317 :         free(ctx->extent_page_num);
    4758                 :        317 :         ctx->extent_page_num = NULL;
    4759                 :        317 :         ctx->num_extent_pages = 0;
    4760                 :            : 
    4761                 :        317 :         bs_load_replay_md_chain_cpl(ctx);
    4762                 :            : }
    4763                 :            : 
    4764                 :            : static void
    4765                 :        317 : bs_load_replay_extent_pages(struct spdk_bs_load_ctx *ctx)
    4766                 :            : {
    4767                 :            :         spdk_bs_batch_t *batch;
    4768                 :            :         uint32_t page;
    4769                 :            :         uint64_t lba;
    4770                 :            :         uint64_t i;
    4771                 :            : 
    4772                 :        317 :         ctx->extent_pages = spdk_zmalloc(SPDK_BS_PAGE_SIZE * ctx->num_extent_pages, 0,
    4773                 :            :                                          NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    4774         [ -  + ]:        317 :         if (!ctx->extent_pages) {
    4775                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4776                 :          0 :                 return;
    4777                 :            :         }
    4778                 :            : 
    4779                 :        317 :         batch = bs_sequence_to_batch(ctx->seq, bs_load_replay_extent_page_cpl, ctx);
    4780                 :            : 
    4781         [ +  + ]:        635 :         for (i = 0; i < ctx->num_extent_pages; i++) {
    4782                 :        318 :                 page = ctx->extent_page_num[i];
    4783         [ -  + ]:        318 :                 assert(page < ctx->super->md_len);
    4784                 :        318 :                 lba = bs_md_page_to_lba(ctx->bs, page);
    4785                 :        318 :                 bs_batch_read_dev(batch, &ctx->extent_pages[i], lba,
    4786                 :        318 :                                   bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE));
    4787                 :            :         }
    4788                 :            : 
    4789                 :        317 :         bs_batch_close(batch);
    4790                 :            : }
    4791                 :            : 
    4792                 :            : static void
    4793                 :      42398 : bs_load_replay_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4794                 :            : {
    4795                 :      42398 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4796                 :            :         uint32_t page_num;
    4797                 :            :         struct spdk_blob_md_page *page;
    4798                 :            : 
    4799         [ -  + ]:      42398 :         if (bserrno != 0) {
    4800                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    4801                 :          0 :                 return;
    4802                 :            :         }
    4803                 :            : 
    4804                 :      42398 :         page_num = ctx->cur_page;
    4805                 :      42398 :         page = ctx->page;
    4806         [ +  + ]:      42398 :         if (bs_load_cur_md_page_valid(ctx) == true) {
    4807   [ +  +  +  +  :       1198 :                 if (page->sequence_num == 0 || ctx->in_page_chain == true) {
                   +  + ]
    4808                 :       1174 :                         spdk_spin_lock(&ctx->bs->used_lock);
    4809                 :       1174 :                         bs_claim_md_page(ctx->bs, page_num);
    4810                 :       1174 :                         spdk_spin_unlock(&ctx->bs->used_lock);
    4811         [ +  + ]:       1174 :                         if (page->sequence_num == 0) {
    4812                 :        934 :                                 SPDK_NOTICELOG("Recover: blob 0x%" PRIx32 "\n", page_num);
    4813                 :        934 :                                 spdk_bit_array_set(ctx->bs->used_blobids, page_num);
    4814                 :            :                         }
    4815         [ -  + ]:       1174 :                         if (bs_load_replay_md_parse_page(ctx, page)) {
    4816                 :          0 :                                 bs_load_ctx_fail(ctx, -EILSEQ);
    4817                 :          0 :                                 return;
    4818                 :            :                         }
    4819         [ +  + ]:       1174 :                         if (page->next != SPDK_INVALID_MD_PAGE) {
    4820                 :        240 :                                 ctx->in_page_chain = true;
    4821                 :        240 :                                 ctx->cur_page = page->next;
    4822                 :        240 :                                 bs_load_replay_cur_md_page(ctx);
    4823                 :        240 :                                 return;
    4824                 :            :                         }
    4825         [ +  + ]:        934 :                         if (ctx->num_extent_pages != 0) {
    4826                 :        317 :                                 bs_load_replay_extent_pages(ctx);
    4827                 :        317 :                                 return;
    4828                 :            :                         }
    4829                 :            :                 }
    4830                 :            :         }
    4831                 :      41841 :         bs_load_replay_md_chain_cpl(ctx);
    4832                 :            : }
    4833                 :            : 
    4834                 :            : static void
    4835                 :      42398 : bs_load_replay_cur_md_page(struct spdk_bs_load_ctx *ctx)
    4836                 :            : {
    4837                 :            :         uint64_t lba;
    4838                 :            : 
    4839         [ -  + ]:      42398 :         assert(ctx->cur_page < ctx->super->md_len);
    4840                 :      42398 :         lba = bs_md_page_to_lba(ctx->bs, ctx->cur_page);
    4841                 :      42398 :         bs_sequence_read_dev(ctx->seq, ctx->page, lba,
    4842                 :      42398 :                              bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
    4843                 :            :                              bs_load_replay_md_cpl, ctx);
    4844                 :      42398 : }
    4845                 :            : 
    4846                 :            : static void
    4847                 :        641 : bs_load_replay_md(struct spdk_bs_load_ctx *ctx)
    4848                 :            : {
    4849                 :        641 :         ctx->page_index = 0;
    4850                 :        641 :         ctx->cur_page = 0;
    4851                 :        641 :         ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
    4852                 :            :                                  NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    4853         [ -  + ]:        641 :         if (!ctx->page) {
    4854                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4855                 :          0 :                 return;
    4856                 :            :         }
    4857                 :        641 :         bs_load_replay_cur_md_page(ctx);
    4858                 :            : }
    4859                 :            : 
    4860                 :            : static void
    4861                 :        641 : bs_recover(struct spdk_bs_load_ctx *ctx)
    4862                 :            : {
    4863                 :            :         int             rc;
    4864                 :            : 
    4865                 :        641 :         SPDK_NOTICELOG("Performing recovery on blobstore\n");
    4866                 :        641 :         rc = spdk_bit_array_resize(&ctx->bs->used_md_pages, ctx->super->md_len);
    4867         [ -  + ]:        641 :         if (rc < 0) {
    4868                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4869                 :          0 :                 return;
    4870                 :            :         }
    4871                 :            : 
    4872                 :        641 :         rc = spdk_bit_array_resize(&ctx->bs->used_blobids, ctx->super->md_len);
    4873         [ -  + ]:        641 :         if (rc < 0) {
    4874                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4875                 :          0 :                 return;
    4876                 :            :         }
    4877                 :            : 
    4878                 :        641 :         rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
    4879         [ -  + ]:        641 :         if (rc < 0) {
    4880                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4881                 :          0 :                 return;
    4882                 :            :         }
    4883                 :            : 
    4884                 :        641 :         rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->super->md_len);
    4885         [ -  + ]:        641 :         if (rc < 0) {
    4886                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    4887                 :          0 :                 return;
    4888                 :            :         }
    4889                 :            : 
    4890                 :        641 :         ctx->bs->num_free_clusters = ctx->bs->total_clusters;
    4891                 :        641 :         bs_load_replay_md(ctx);
    4892                 :            : }
    4893                 :            : 
    4894                 :            : static int
    4895                 :       1747 : bs_parse_super(struct spdk_bs_load_ctx *ctx)
    4896                 :            : {
    4897                 :            :         int rc;
    4898                 :            : 
    4899         [ +  + ]:       1747 :         if (ctx->super->size == 0) {
    4900                 :         48 :                 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    4901                 :            :         }
    4902                 :            : 
    4903         [ +  + ]:       1747 :         if (ctx->super->io_unit_size == 0) {
    4904                 :         48 :                 ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE;
    4905                 :            :         }
    4906                 :            : 
    4907                 :       1747 :         ctx->bs->clean = 1;
    4908                 :       1747 :         ctx->bs->cluster_sz = ctx->super->cluster_size;
    4909         [ -  + ]:       1747 :         ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size;
    4910                 :       1747 :         ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
    4911         [ +  - ]:       1747 :         if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) {
    4912                 :       1747 :                 ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster);
    4913                 :            :         }
    4914                 :       1747 :         ctx->bs->io_unit_size = ctx->super->io_unit_size;
    4915                 :       1747 :         rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
    4916         [ -  + ]:       1747 :         if (rc < 0) {
    4917                 :          0 :                 return -ENOMEM;
    4918                 :            :         }
    4919                 :       1747 :         ctx->bs->md_start = ctx->super->md_start;
    4920                 :       1747 :         ctx->bs->md_len = ctx->super->md_len;
    4921                 :       1747 :         rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len);
    4922         [ -  + ]:       1747 :         if (rc < 0) {
    4923                 :          0 :                 return -ENOMEM;
    4924                 :            :         }
    4925                 :            : 
    4926                 :       4683 :         ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up(
    4927                 :       2936 :                                                ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster);
    4928                 :       1747 :         ctx->bs->super_blob = ctx->super->super_blob;
    4929                 :       1747 :         memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
    4930                 :            : 
    4931                 :       1747 :         return 0;
    4932                 :            : }
    4933                 :            : 
    4934                 :            : static void
    4935                 :       9419 : bs_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    4936                 :            : {
    4937                 :       9419 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    4938                 :            :         int rc;
    4939                 :            : 
    4940                 :       9419 :         rc = bs_super_validate(ctx->super, ctx->bs);
    4941         [ +  + ]:       9419 :         if (rc != 0) {
    4942                 :       7672 :                 bs_load_ctx_fail(ctx, rc);
    4943                 :       7672 :                 return;
    4944                 :            :         }
    4945                 :            : 
    4946                 :       1747 :         rc = bs_parse_super(ctx);
    4947         [ -  + ]:       1747 :         if (rc < 0) {
    4948                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    4949                 :          0 :                 return;
    4950                 :            :         }
    4951                 :            : 
    4952   [ +  +  +  +  :       1747 :         if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0 || ctx->force_recover) {
             -  +  -  + ]
    4953                 :        641 :                 bs_recover(ctx);
    4954                 :            :         } else {
    4955                 :       1106 :                 bs_load_read_used_pages(ctx);
    4956                 :            :         }
    4957                 :            : }
    4958                 :            : 
    4959                 :            : static inline int
    4960                 :       9699 : bs_opts_copy(struct spdk_bs_opts *src, struct spdk_bs_opts *dst)
    4961                 :            : {
    4962                 :            : 
    4963         [ -  + ]:       9699 :         if (!src->opts_size) {
    4964                 :          0 :                 SPDK_ERRLOG("opts_size should not be zero value\n");
    4965                 :          0 :                 return -1;
    4966                 :            :         }
    4967                 :            : 
    4968                 :            : #define FIELD_OK(field) \
    4969                 :            :         offsetof(struct spdk_bs_opts, field) + sizeof(src->field) <= src->opts_size
    4970                 :            : 
    4971                 :            : #define SET_FIELD(field) \
    4972                 :            :         if (FIELD_OK(field)) { \
    4973                 :            :                 dst->field = src->field; \
    4974                 :            :         } \
    4975                 :            : 
    4976         [ +  - ]:       9699 :         SET_FIELD(cluster_sz);
    4977         [ +  - ]:       9699 :         SET_FIELD(num_md_pages);
    4978         [ +  - ]:       9699 :         SET_FIELD(max_md_ops);
    4979         [ +  - ]:       9699 :         SET_FIELD(max_channel_ops);
    4980         [ +  - ]:       9699 :         SET_FIELD(clear_method);
    4981                 :            : 
    4982         [ +  - ]:       9699 :         if (FIELD_OK(bstype)) {
    4983                 :       9699 :                 memcpy(&dst->bstype, &src->bstype, sizeof(dst->bstype));
    4984                 :            :         }
    4985         [ +  - ]:       9699 :         SET_FIELD(iter_cb_fn);
    4986         [ +  - ]:       9699 :         SET_FIELD(iter_cb_arg);
    4987         [ +  - ]:       9699 :         SET_FIELD(force_recover);
    4988         [ +  - ]:       9699 :         SET_FIELD(esnap_bs_dev_create);
    4989         [ +  - ]:       9699 :         SET_FIELD(esnap_ctx);
    4990                 :            : 
    4991                 :       9699 :         dst->opts_size = src->opts_size;
    4992                 :            : 
    4993                 :            :         /* You should not remove this statement, but need to update the assert statement
    4994                 :            :          * if you add a new field, and also add a corresponding SET_FIELD statement */
    4995                 :            :         SPDK_STATIC_ASSERT(sizeof(struct spdk_bs_opts) == 88, "Incorrect size");
    4996                 :            : 
    4997                 :            : #undef FIELD_OK
    4998                 :            : #undef SET_FIELD
    4999                 :            : 
    5000                 :       9699 :         return 0;
    5001                 :            : }
    5002                 :            : 
    5003                 :            : void
    5004                 :       9541 : spdk_bs_load(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
    5005                 :            :              spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
    5006                 :            : {
    5007                 :       6187 :         struct spdk_blob_store  *bs;
    5008                 :       6187 :         struct spdk_bs_cpl      cpl;
    5009                 :       6187 :         struct spdk_bs_load_ctx *ctx;
    5010                 :       9541 :         struct spdk_bs_opts     opts = {};
    5011                 :            :         int err;
    5012                 :            : 
    5013   [ -  +  -  + ]:       9541 :         SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev);
    5014                 :            : 
    5015   [ +  +  +  + ]:       9541 :         if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
    5016   [ -  +  -  + ]:         24 :                 SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen);
    5017                 :         24 :                 dev->destroy(dev);
    5018                 :         24 :                 cb_fn(cb_arg, NULL, -EINVAL);
    5019                 :         54 :                 return;
    5020                 :            :         }
    5021                 :            : 
    5022                 :       9517 :         spdk_bs_opts_init(&opts, sizeof(opts));
    5023         [ +  + ]:       9517 :         if (o) {
    5024         [ -  + ]:       8390 :                 if (bs_opts_copy(o, &opts)) {
    5025                 :          0 :                         return;
    5026                 :            :                 }
    5027                 :            :         }
    5028                 :            : 
    5029   [ +  +  +  + ]:       9517 :         if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
    5030                 :         48 :                 dev->destroy(dev);
    5031                 :         48 :                 cb_fn(cb_arg, NULL, -EINVAL);
    5032                 :         48 :                 return;
    5033                 :            :         }
    5034                 :            : 
    5035                 :       9469 :         err = bs_alloc(dev, &opts, &bs, &ctx);
    5036         [ +  + ]:       9469 :         if (err) {
    5037                 :         50 :                 dev->destroy(dev);
    5038                 :         50 :                 cb_fn(cb_arg, NULL, err);
    5039                 :         50 :                 return;
    5040                 :            :         }
    5041                 :            : 
    5042                 :       9419 :         cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
    5043                 :       9419 :         cpl.u.bs_handle.cb_fn = cb_fn;
    5044                 :       9419 :         cpl.u.bs_handle.cb_arg = cb_arg;
    5045                 :       9419 :         cpl.u.bs_handle.bs = bs;
    5046                 :            : 
    5047                 :       9419 :         ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5048         [ -  + ]:       9419 :         if (!ctx->seq) {
    5049                 :          0 :                 spdk_free(ctx->super);
    5050                 :          0 :                 free(ctx);
    5051                 :          0 :                 bs_free(bs);
    5052                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5053                 :          0 :                 return;
    5054                 :            :         }
    5055                 :            : 
    5056                 :            :         /* Read the super block */
    5057                 :       9419 :         bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
    5058                 :       9419 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    5059                 :            :                              bs_load_super_cpl, ctx);
    5060                 :            : }
    5061                 :            : 
    5062                 :            : /* END spdk_bs_load */
    5063                 :            : 
    5064                 :            : /* START spdk_bs_dump */
    5065                 :            : 
    5066                 :            : static void
    5067                 :          0 : bs_dump_finish(spdk_bs_sequence_t *seq, struct spdk_bs_load_ctx *ctx, int bserrno)
    5068                 :            : {
    5069                 :          0 :         spdk_free(ctx->super);
    5070                 :            : 
    5071                 :            :         /*
    5072                 :            :          * We need to defer calling bs_call_cpl() until after
    5073                 :            :          * dev destruction, so tuck these away for later use.
    5074                 :            :          */
    5075                 :          0 :         ctx->bs->unload_err = bserrno;
    5076   [ #  #  #  # ]:          0 :         memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
    5077                 :          0 :         seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
    5078                 :            : 
    5079                 :          0 :         bs_sequence_finish(seq, 0);
    5080                 :          0 :         bs_free(ctx->bs);
    5081                 :          0 :         free(ctx);
    5082                 :          0 : }
    5083                 :            : 
    5084                 :            : static void
    5085                 :          0 : bs_dump_print_xattr(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
    5086                 :            : {
    5087                 :            :         struct spdk_blob_md_descriptor_xattr *desc_xattr;
    5088                 :            :         uint32_t i;
    5089                 :            :         const char *type;
    5090                 :            : 
    5091                 :          0 :         desc_xattr = (struct spdk_blob_md_descriptor_xattr *)desc;
    5092                 :            : 
    5093                 :          0 :         if (desc_xattr->length !=
    5094                 :            :             sizeof(desc_xattr->name_length) + sizeof(desc_xattr->value_length) +
    5095                 :          0 :             desc_xattr->name_length + desc_xattr->value_length) {
    5096                 :            :         }
    5097                 :            : 
    5098   [ #  #  #  # ]:          0 :         memcpy(ctx->xattr_name, desc_xattr->name, desc_xattr->name_length);
    5099                 :          0 :         ctx->xattr_name[desc_xattr->name_length] = '\0';
    5100         [ #  # ]:          0 :         if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
    5101                 :          0 :                 type = "XATTR";
    5102         [ #  # ]:          0 :         } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
    5103                 :          0 :                 type = "XATTR_INTERNAL";
    5104                 :            :         } else {
    5105                 :          0 :                 assert(false);
    5106                 :            :                 type = "XATTR_?";
    5107                 :            :         }
    5108   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "%s: name = \"%s\"\n", type, ctx->xattr_name);
    5109   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "       value = \"");
    5110                 :          0 :         ctx->print_xattr_fn(ctx->fp, ctx->super->bstype.bstype, ctx->xattr_name,
    5111                 :          0 :                             (void *)((uintptr_t)desc_xattr->name + desc_xattr->name_length),
    5112                 :          0 :                             desc_xattr->value_length);
    5113   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "\"\n");
    5114         [ #  # ]:          0 :         for (i = 0; i < desc_xattr->value_length; i++) {
    5115         [ #  # ]:          0 :                 if (i % 16 == 0) {
    5116   [ #  #  #  # ]:          0 :                         fprintf(ctx->fp, "               ");
    5117                 :            :                 }
    5118   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "%02" PRIx8 " ", *((uint8_t *)desc_xattr->name + desc_xattr->name_length + i));
    5119         [ #  # ]:          0 :                 if ((i + 1) % 16 == 0) {
    5120         [ #  # ]:          0 :                         fprintf(ctx->fp, "\n");
    5121                 :            :                 }
    5122                 :            :         }
    5123         [ #  # ]:          0 :         if (i % 16 != 0) {
    5124         [ #  # ]:          0 :                 fprintf(ctx->fp, "\n");
    5125                 :            :         }
    5126                 :          0 : }
    5127                 :            : 
    5128                 :            : struct type_flag_desc {
    5129                 :            :         uint64_t mask;
    5130                 :            :         uint64_t val;
    5131                 :            :         const char *name;
    5132                 :            : };
    5133                 :            : 
    5134                 :            : static void
    5135                 :          0 : bs_dump_print_type_bits(struct spdk_bs_load_ctx *ctx, uint64_t flags,
    5136                 :            :                         struct type_flag_desc *desc, size_t numflags)
    5137                 :            : {
    5138                 :          0 :         uint64_t covered = 0;
    5139                 :            :         size_t i;
    5140                 :            : 
    5141         [ #  # ]:          0 :         for (i = 0; i < numflags; i++) {
    5142         [ #  # ]:          0 :                 if ((desc[i].mask & flags) != desc[i].val) {
    5143                 :          0 :                         continue;
    5144                 :            :                 }
    5145   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "\t\t 0x%016" PRIx64 " %s", desc[i].val, desc[i].name);
    5146         [ #  # ]:          0 :                 if (desc[i].mask != desc[i].val) {
    5147   [ #  #  #  # ]:          0 :                         fprintf(ctx->fp, " (mask 0x%" PRIx64 " value 0x%" PRIx64 ")",
    5148                 :          0 :                                 desc[i].mask, desc[i].val);
    5149                 :            :                 }
    5150         [ #  # ]:          0 :                 fprintf(ctx->fp, "\n");
    5151                 :          0 :                 covered |= desc[i].mask;
    5152                 :            :         }
    5153         [ #  # ]:          0 :         if ((flags & ~covered) != 0) {
    5154   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "\t\t 0x%016" PRIx64 " Unknown\n", flags & ~covered);
    5155                 :            :         }
    5156                 :          0 : }
    5157                 :            : 
    5158                 :            : static void
    5159                 :          0 : bs_dump_print_type_flags(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
    5160                 :            : {
    5161                 :            :         struct spdk_blob_md_descriptor_flags *type_desc;
    5162                 :            : #define ADD_FLAG(f) { f, f, #f }
    5163                 :            : #define ADD_MASK_VAL(m, v) { m, v, #v }
    5164                 :            :         static struct type_flag_desc invalid[] = {
    5165                 :            :                 ADD_FLAG(SPDK_BLOB_THIN_PROV),
    5166                 :            :                 ADD_FLAG(SPDK_BLOB_INTERNAL_XATTR),
    5167                 :            :                 ADD_FLAG(SPDK_BLOB_EXTENT_TABLE),
    5168                 :            :         };
    5169                 :            :         static struct type_flag_desc data_ro[] = {
    5170                 :            :                 ADD_FLAG(SPDK_BLOB_READ_ONLY),
    5171                 :            :         };
    5172                 :            :         static struct type_flag_desc md_ro[] = {
    5173                 :            :                 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_DEFAULT),
    5174                 :            :                 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_NONE),
    5175                 :            :                 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_UNMAP),
    5176                 :            :                 ADD_MASK_VAL(SPDK_BLOB_MD_RO_FLAGS_MASK, BLOB_CLEAR_WITH_WRITE_ZEROES),
    5177                 :            :         };
    5178                 :            : #undef ADD_FLAG
    5179                 :            : #undef ADD_MASK_VAL
    5180                 :            : 
    5181                 :          0 :         type_desc = (struct spdk_blob_md_descriptor_flags *)desc;
    5182   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Flags:\n");
    5183   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "\tinvalid: 0x%016" PRIx64 "\n", type_desc->invalid_flags);
    5184                 :          0 :         bs_dump_print_type_bits(ctx, type_desc->invalid_flags, invalid,
    5185                 :            :                                 SPDK_COUNTOF(invalid));
    5186   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "\tdata_ro: 0x%016" PRIx64 "\n", type_desc->data_ro_flags);
    5187                 :          0 :         bs_dump_print_type_bits(ctx, type_desc->data_ro_flags, data_ro,
    5188                 :            :                                 SPDK_COUNTOF(data_ro));
    5189   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "\t  md_ro: 0x%016" PRIx64 "\n", type_desc->md_ro_flags);
    5190                 :          0 :         bs_dump_print_type_bits(ctx, type_desc->md_ro_flags, md_ro,
    5191                 :            :                                 SPDK_COUNTOF(md_ro));
    5192                 :          0 : }
    5193                 :            : 
    5194                 :            : static void
    5195                 :          0 : bs_dump_print_extent_table(struct spdk_bs_load_ctx *ctx, struct spdk_blob_md_descriptor *desc)
    5196                 :            : {
    5197                 :            :         struct spdk_blob_md_descriptor_extent_table *et_desc;
    5198                 :            :         uint64_t num_extent_pages;
    5199                 :            :         uint32_t et_idx;
    5200                 :            : 
    5201                 :          0 :         et_desc = (struct spdk_blob_md_descriptor_extent_table *)desc;
    5202                 :          0 :         num_extent_pages = (et_desc->length - sizeof(et_desc->num_clusters)) /
    5203                 :            :                            sizeof(et_desc->extent_page[0]);
    5204                 :            : 
    5205   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Extent table:\n");
    5206         [ #  # ]:          0 :         for (et_idx = 0; et_idx < num_extent_pages; et_idx++) {
    5207         [ #  # ]:          0 :                 if (et_desc->extent_page[et_idx].page_idx == 0) {
    5208                 :            :                         /* Zeroes represent unallocated extent pages. */
    5209                 :          0 :                         continue;
    5210                 :            :                 }
    5211   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "\tExtent page: %5" PRIu32 " length %3" PRIu32
    5212                 :            :                         " at LBA %" PRIu64 "\n", et_desc->extent_page[et_idx].page_idx,
    5213                 :            :                         et_desc->extent_page[et_idx].num_pages,
    5214                 :            :                         bs_md_page_to_lba(ctx->bs, et_desc->extent_page[et_idx].page_idx));
    5215                 :            :         }
    5216                 :          0 : }
    5217                 :            : 
    5218                 :            : static void
    5219                 :          0 : bs_dump_print_md_page(struct spdk_bs_load_ctx *ctx)
    5220                 :            : {
    5221                 :          0 :         uint32_t page_idx = ctx->cur_page;
    5222                 :          0 :         struct spdk_blob_md_page *page = ctx->page;
    5223                 :            :         struct spdk_blob_md_descriptor *desc;
    5224                 :          0 :         size_t cur_desc = 0;
    5225                 :            :         uint32_t crc;
    5226                 :            : 
    5227   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "=========\n");
    5228   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Metadata Page Index: %" PRIu32 " (0x%" PRIx32 ")\n", page_idx, page_idx);
    5229   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Start LBA: %" PRIu64 "\n", bs_md_page_to_lba(ctx->bs, page_idx));
    5230   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Blob ID: 0x%" PRIx64 "\n", page->id);
    5231   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "Sequence: %" PRIu32 "\n", page->sequence_num);
    5232         [ #  # ]:          0 :         if (page->next == SPDK_INVALID_MD_PAGE) {
    5233   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "Next: None\n");
    5234                 :            :         } else {
    5235   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, "Next: %" PRIu32 "\n", page->next);
    5236                 :            :         }
    5237   [ #  #  #  #  :          0 :         fprintf(ctx->fp, "In used bit array%s:", ctx->super->clean ? "" : " (not clean: dubious)");
                   #  # ]
    5238         [ #  # ]:          0 :         if (spdk_bit_array_get(ctx->bs->used_md_pages, page_idx)) {
    5239   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, " md");
    5240                 :            :         }
    5241         [ #  # ]:          0 :         if (spdk_bit_array_get(ctx->bs->used_blobids, page_idx)) {
    5242   [ #  #  #  # ]:          0 :                 fprintf(ctx->fp, " blob");
    5243                 :            :         }
    5244         [ #  # ]:          0 :         fprintf(ctx->fp, "\n");
    5245                 :            : 
    5246                 :          0 :         crc = blob_md_page_calc_crc(page);
    5247   [ #  #  #  #  :          0 :         fprintf(ctx->fp, "CRC: 0x%" PRIx32 " (%s)\n", page->crc, crc == page->crc ? "OK" : "Mismatch");
                   #  # ]
    5248                 :            : 
    5249                 :          0 :         desc = (struct spdk_blob_md_descriptor *)page->descriptors;
    5250         [ #  # ]:          0 :         while (cur_desc < sizeof(page->descriptors)) {
    5251         [ #  # ]:          0 :                 if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_PADDING) {
    5252         [ #  # ]:          0 :                         if (desc->length == 0) {
    5253                 :            :                                 /* If padding and length are 0, this terminates the page */
    5254                 :          0 :                                 break;
    5255                 :            :                         }
    5256         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_RLE) {
    5257                 :            :                         struct spdk_blob_md_descriptor_extent_rle       *desc_extent_rle;
    5258                 :            :                         unsigned int                            i;
    5259                 :            : 
    5260                 :          0 :                         desc_extent_rle = (struct spdk_blob_md_descriptor_extent_rle *)desc;
    5261                 :            : 
    5262         [ #  # ]:          0 :                         for (i = 0; i < desc_extent_rle->length / sizeof(desc_extent_rle->extents[0]); i++) {
    5263         [ #  # ]:          0 :                                 if (desc_extent_rle->extents[i].cluster_idx != 0) {
    5264   [ #  #  #  # ]:          0 :                                         fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32,
    5265                 :            :                                                 desc_extent_rle->extents[i].cluster_idx);
    5266                 :            :                                 } else {
    5267   [ #  #  #  # ]:          0 :                                         fprintf(ctx->fp, "Unallocated Extent - ");
    5268                 :            :                                 }
    5269   [ #  #  #  # ]:          0 :                                 fprintf(ctx->fp, " Length: %" PRIu32, desc_extent_rle->extents[i].length);
    5270         [ #  # ]:          0 :                                 fprintf(ctx->fp, "\n");
    5271                 :            :                         }
    5272         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_PAGE) {
    5273                 :            :                         struct spdk_blob_md_descriptor_extent_page      *desc_extent;
    5274                 :            :                         unsigned int                                    i;
    5275                 :            : 
    5276                 :          0 :                         desc_extent = (struct spdk_blob_md_descriptor_extent_page *)desc;
    5277                 :            : 
    5278         [ #  # ]:          0 :                         for (i = 0; i < desc_extent->length / sizeof(desc_extent->cluster_idx[0]); i++) {
    5279         [ #  # ]:          0 :                                 if (desc_extent->cluster_idx[i] != 0) {
    5280   [ #  #  #  # ]:          0 :                                         fprintf(ctx->fp, "Allocated Extent - Start: %" PRIu32,
    5281                 :            :                                                 desc_extent->cluster_idx[i]);
    5282                 :            :                                 } else {
    5283   [ #  #  #  # ]:          0 :                                         fprintf(ctx->fp, "Unallocated Extent");
    5284                 :            :                                 }
    5285         [ #  # ]:          0 :                                 fprintf(ctx->fp, "\n");
    5286                 :            :                         }
    5287         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR) {
    5288                 :          0 :                         bs_dump_print_xattr(ctx, desc);
    5289         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_XATTR_INTERNAL) {
    5290                 :          0 :                         bs_dump_print_xattr(ctx, desc);
    5291         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_FLAGS) {
    5292                 :          0 :                         bs_dump_print_type_flags(ctx, desc);
    5293         [ #  # ]:          0 :                 } else if (desc->type == SPDK_MD_DESCRIPTOR_TYPE_EXTENT_TABLE) {
    5294                 :          0 :                         bs_dump_print_extent_table(ctx, desc);
    5295                 :            :                 } else {
    5296                 :            :                         /* Error */
    5297   [ #  #  #  # ]:          0 :                         fprintf(ctx->fp, "Unknown descriptor type %" PRIu8 "\n", desc->type);
    5298                 :            :                 }
    5299                 :            :                 /* Advance to the next descriptor */
    5300                 :          0 :                 cur_desc += sizeof(*desc) + desc->length;
    5301         [ #  # ]:          0 :                 if (cur_desc + sizeof(*desc) > sizeof(page->descriptors)) {
    5302                 :          0 :                         break;
    5303                 :            :                 }
    5304                 :          0 :                 desc = (struct spdk_blob_md_descriptor *)((uintptr_t)page->descriptors + cur_desc);
    5305                 :            :         }
    5306                 :          0 : }
    5307                 :            : 
    5308                 :            : static void
    5309                 :          0 : bs_dump_read_md_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5310                 :            : {
    5311                 :          0 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5312                 :            : 
    5313         [ #  # ]:          0 :         if (bserrno != 0) {
    5314                 :          0 :                 bs_dump_finish(seq, ctx, bserrno);
    5315                 :          0 :                 return;
    5316                 :            :         }
    5317                 :            : 
    5318         [ #  # ]:          0 :         if (ctx->page->id != 0) {
    5319                 :          0 :                 bs_dump_print_md_page(ctx);
    5320                 :            :         }
    5321                 :            : 
    5322                 :          0 :         ctx->cur_page++;
    5323                 :            : 
    5324         [ #  # ]:          0 :         if (ctx->cur_page < ctx->super->md_len) {
    5325                 :          0 :                 bs_dump_read_md_page(seq, ctx);
    5326                 :            :         } else {
    5327                 :          0 :                 spdk_free(ctx->page);
    5328                 :          0 :                 bs_dump_finish(seq, ctx, 0);
    5329                 :            :         }
    5330                 :            : }
    5331                 :            : 
    5332                 :            : static void
    5333                 :          0 : bs_dump_read_md_page(spdk_bs_sequence_t *seq, void *cb_arg)
    5334                 :            : {
    5335                 :          0 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5336                 :            :         uint64_t lba;
    5337                 :            : 
    5338         [ #  # ]:          0 :         assert(ctx->cur_page < ctx->super->md_len);
    5339                 :          0 :         lba = bs_page_to_lba(ctx->bs, ctx->super->md_start + ctx->cur_page);
    5340                 :          0 :         bs_sequence_read_dev(seq, ctx->page, lba,
    5341                 :          0 :                              bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
    5342                 :            :                              bs_dump_read_md_page_cpl, ctx);
    5343                 :          0 : }
    5344                 :            : 
    5345                 :            : static void
    5346                 :          0 : bs_dump_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5347                 :            : {
    5348                 :          0 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5349                 :            :         int rc;
    5350                 :            : 
    5351         [ #  # ]:          0 :         fprintf(ctx->fp, "Signature: \"%.8s\" ", ctx->super->signature);
    5352   [ #  #  #  # ]:          0 :         if (memcmp(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
    5353                 :            :                    sizeof(ctx->super->signature)) != 0) {
    5354         [ #  # ]:          0 :                 fprintf(ctx->fp, "(Mismatch)\n");
    5355                 :          0 :                 bs_dump_finish(seq, ctx, bserrno);
    5356                 :          0 :                 return;
    5357                 :            :         } else {
    5358         [ #  # ]:          0 :                 fprintf(ctx->fp, "(OK)\n");
    5359                 :            :         }
    5360         [ #  # ]:          0 :         fprintf(ctx->fp, "Version: %" PRIu32 "\n", ctx->super->version);
    5361   [ #  #  #  # ]:          0 :         fprintf(ctx->fp, "CRC: 0x%x (%s)\n", ctx->super->crc,
    5362                 :          0 :                 (ctx->super->crc == blob_md_page_calc_crc(ctx->super)) ? "OK" : "Mismatch");
    5363         [ #  # ]:          0 :         fprintf(ctx->fp, "Blobstore Type: %.*s\n", SPDK_BLOBSTORE_TYPE_LENGTH, ctx->super->bstype.bstype);
    5364         [ #  # ]:          0 :         fprintf(ctx->fp, "Cluster Size: %" PRIu32 "\n", ctx->super->cluster_size);
    5365         [ #  # ]:          0 :         fprintf(ctx->fp, "Super Blob ID: ");
    5366         [ #  # ]:          0 :         if (ctx->super->super_blob == SPDK_BLOBID_INVALID) {
    5367         [ #  # ]:          0 :                 fprintf(ctx->fp, "(None)\n");
    5368                 :            :         } else {
    5369         [ #  # ]:          0 :                 fprintf(ctx->fp, "0x%" PRIx64 "\n", ctx->super->super_blob);
    5370                 :            :         }
    5371         [ #  # ]:          0 :         fprintf(ctx->fp, "Clean: %" PRIu32 "\n", ctx->super->clean);
    5372         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Metadata Page Mask Start: %" PRIu32 "\n", ctx->super->used_page_mask_start);
    5373         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Metadata Page Mask Length: %" PRIu32 "\n", ctx->super->used_page_mask_len);
    5374         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Cluster Mask Start: %" PRIu32 "\n", ctx->super->used_cluster_mask_start);
    5375         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Cluster Mask Length: %" PRIu32 "\n", ctx->super->used_cluster_mask_len);
    5376         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Blob ID Mask Start: %" PRIu32 "\n", ctx->super->used_blobid_mask_start);
    5377         [ #  # ]:          0 :         fprintf(ctx->fp, "Used Blob ID Mask Length: %" PRIu32 "\n", ctx->super->used_blobid_mask_len);
    5378         [ #  # ]:          0 :         fprintf(ctx->fp, "Metadata Start: %" PRIu32 "\n", ctx->super->md_start);
    5379         [ #  # ]:          0 :         fprintf(ctx->fp, "Metadata Length: %" PRIu32 "\n", ctx->super->md_len);
    5380                 :            : 
    5381                 :          0 :         ctx->cur_page = 0;
    5382                 :          0 :         ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0,
    5383                 :            :                                  NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    5384         [ #  # ]:          0 :         if (!ctx->page) {
    5385                 :          0 :                 bs_dump_finish(seq, ctx, -ENOMEM);
    5386                 :          0 :                 return;
    5387                 :            :         }
    5388                 :            : 
    5389                 :          0 :         rc = bs_parse_super(ctx);
    5390         [ #  # ]:          0 :         if (rc < 0) {
    5391                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    5392                 :          0 :                 return;
    5393                 :            :         }
    5394                 :            : 
    5395                 :          0 :         bs_load_read_used_pages(ctx);
    5396                 :            : }
    5397                 :            : 
    5398                 :            : void
    5399                 :          0 : spdk_bs_dump(struct spdk_bs_dev *dev, FILE *fp, spdk_bs_dump_print_xattr print_xattr_fn,
    5400                 :            :              spdk_bs_op_complete cb_fn, void *cb_arg)
    5401                 :            : {
    5402                 :          0 :         struct spdk_blob_store  *bs;
    5403                 :          0 :         struct spdk_bs_cpl      cpl;
    5404                 :          0 :         struct spdk_bs_load_ctx *ctx;
    5405                 :          0 :         struct spdk_bs_opts     opts = {};
    5406                 :            :         int err;
    5407                 :            : 
    5408   [ #  #  #  # ]:          0 :         SPDK_DEBUGLOG(blob, "Dumping blobstore from dev %p\n", dev);
    5409                 :            : 
    5410                 :          0 :         spdk_bs_opts_init(&opts, sizeof(opts));
    5411                 :            : 
    5412                 :          0 :         err = bs_alloc(dev, &opts, &bs, &ctx);
    5413         [ #  # ]:          0 :         if (err) {
    5414                 :          0 :                 dev->destroy(dev);
    5415                 :          0 :                 cb_fn(cb_arg, err);
    5416                 :          0 :                 return;
    5417                 :            :         }
    5418                 :            : 
    5419                 :          0 :         ctx->dumping = true;
    5420                 :          0 :         ctx->fp = fp;
    5421                 :          0 :         ctx->print_xattr_fn = print_xattr_fn;
    5422                 :            : 
    5423                 :          0 :         cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
    5424                 :          0 :         cpl.u.bs_basic.cb_fn = cb_fn;
    5425                 :          0 :         cpl.u.bs_basic.cb_arg = cb_arg;
    5426                 :            : 
    5427                 :          0 :         ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5428         [ #  # ]:          0 :         if (!ctx->seq) {
    5429                 :          0 :                 spdk_free(ctx->super);
    5430                 :          0 :                 free(ctx);
    5431                 :          0 :                 bs_free(bs);
    5432                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5433                 :          0 :                 return;
    5434                 :            :         }
    5435                 :            : 
    5436                 :            :         /* Read the super block */
    5437                 :          0 :         bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
    5438                 :          0 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    5439                 :            :                              bs_dump_super_cpl, ctx);
    5440                 :            : }
    5441                 :            : 
    5442                 :            : /* END spdk_bs_dump */
    5443                 :            : 
    5444                 :            : /* START spdk_bs_init */
    5445                 :            : 
    5446                 :            : static void
    5447                 :       3026 : bs_init_persist_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5448                 :            : {
    5449                 :       3026 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5450                 :            : 
    5451                 :       3026 :         ctx->bs->used_clusters = spdk_bit_pool_create_from_array(ctx->used_clusters);
    5452                 :       3026 :         spdk_free(ctx->super);
    5453                 :       3026 :         free(ctx);
    5454                 :            : 
    5455                 :       3026 :         bs_sequence_finish(seq, bserrno);
    5456                 :       3026 : }
    5457                 :            : 
    5458                 :            : static void
    5459                 :       3026 : bs_init_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5460                 :            : {
    5461                 :       3026 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5462                 :            : 
    5463                 :            :         /* Write super block */
    5464                 :       3026 :         bs_sequence_write_dev(seq, ctx->super, bs_page_to_lba(ctx->bs, 0),
    5465                 :       3026 :                               bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
    5466                 :            :                               bs_init_persist_super_cpl, ctx);
    5467                 :       3026 : }
    5468                 :            : 
    5469                 :            : void
    5470                 :       3123 : spdk_bs_init(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
    5471                 :            :              spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
    5472                 :            : {
    5473                 :       2563 :         struct spdk_bs_load_ctx *ctx;
    5474                 :       2563 :         struct spdk_blob_store  *bs;
    5475                 :       2563 :         struct spdk_bs_cpl      cpl;
    5476                 :            :         spdk_bs_sequence_t      *seq;
    5477                 :            :         spdk_bs_batch_t         *batch;
    5478                 :            :         uint64_t                num_md_lba;
    5479                 :            :         uint64_t                num_md_pages;
    5480                 :            :         uint64_t                num_md_clusters;
    5481                 :            :         uint64_t                max_used_cluster_mask_len;
    5482                 :            :         uint32_t                i;
    5483                 :       3123 :         struct spdk_bs_opts     opts = {};
    5484                 :            :         int                     rc;
    5485                 :            :         uint64_t                lba, lba_count;
    5486                 :            : 
    5487   [ -  +  -  + ]:       3123 :         SPDK_DEBUGLOG(blob, "Initializing blobstore on dev %p\n", dev);
    5488                 :            : 
    5489   [ +  +  +  + ]:       3123 :         if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
    5490                 :         24 :                 SPDK_ERRLOG("unsupported dev block length of %d\n",
    5491                 :            :                             dev->blocklen);
    5492                 :         24 :                 dev->destroy(dev);
    5493                 :         24 :                 cb_fn(cb_arg, NULL, -EINVAL);
    5494                 :         36 :                 return;
    5495                 :            :         }
    5496                 :            : 
    5497                 :       3099 :         spdk_bs_opts_init(&opts, sizeof(opts));
    5498         [ +  + ]:       3099 :         if (o) {
    5499         [ -  + ]:       1285 :                 if (bs_opts_copy(o, &opts)) {
    5500                 :          0 :                         return;
    5501                 :            :                 }
    5502                 :            :         }
    5503                 :            : 
    5504         [ +  + ]:       3099 :         if (bs_opts_verify(&opts) != 0) {
    5505                 :         24 :                 dev->destroy(dev);
    5506                 :         24 :                 cb_fn(cb_arg, NULL, -EINVAL);
    5507                 :         24 :                 return;
    5508                 :            :         }
    5509                 :            : 
    5510                 :       3075 :         rc = bs_alloc(dev, &opts, &bs, &ctx);
    5511         [ +  + ]:       3075 :         if (rc) {
    5512                 :         24 :                 dev->destroy(dev);
    5513                 :         24 :                 cb_fn(cb_arg, NULL, rc);
    5514                 :         24 :                 return;
    5515                 :            :         }
    5516                 :            : 
    5517         [ +  + ]:       3051 :         if (opts.num_md_pages == SPDK_BLOB_OPTS_NUM_MD_PAGES) {
    5518                 :            :                 /* By default, allocate 1 page per cluster.
    5519                 :            :                  * Technically, this over-allocates metadata
    5520                 :            :                  * because more metadata will reduce the number
    5521                 :            :                  * of usable clusters. This can be addressed with
    5522                 :            :                  * more complex math in the future.
    5523                 :            :                  */
    5524                 :       2813 :                 bs->md_len = bs->total_clusters;
    5525                 :            :         } else {
    5526                 :        238 :                 bs->md_len = opts.num_md_pages;
    5527                 :            :         }
    5528                 :       3051 :         rc = spdk_bit_array_resize(&bs->used_md_pages, bs->md_len);
    5529         [ -  + ]:       3051 :         if (rc < 0) {
    5530                 :          0 :                 spdk_free(ctx->super);
    5531                 :          0 :                 free(ctx);
    5532                 :          0 :                 bs_free(bs);
    5533                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5534                 :          0 :                 return;
    5535                 :            :         }
    5536                 :            : 
    5537                 :       3051 :         rc = spdk_bit_array_resize(&bs->used_blobids, bs->md_len);
    5538         [ -  + ]:       3051 :         if (rc < 0) {
    5539                 :          0 :                 spdk_free(ctx->super);
    5540                 :          0 :                 free(ctx);
    5541                 :          0 :                 bs_free(bs);
    5542                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5543                 :          0 :                 return;
    5544                 :            :         }
    5545                 :            : 
    5546                 :       3051 :         rc = spdk_bit_array_resize(&bs->open_blobids, bs->md_len);
    5547         [ -  + ]:       3051 :         if (rc < 0) {
    5548                 :          0 :                 spdk_free(ctx->super);
    5549                 :          0 :                 free(ctx);
    5550                 :          0 :                 bs_free(bs);
    5551                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5552                 :          0 :                 return;
    5553                 :            :         }
    5554                 :            : 
    5555   [ -  +  -  + ]:       3051 :         memcpy(ctx->super->signature, SPDK_BS_SUPER_BLOCK_SIG,
    5556                 :            :                sizeof(ctx->super->signature));
    5557                 :       3051 :         ctx->super->version = SPDK_BS_VERSION;
    5558                 :       3051 :         ctx->super->length = sizeof(*ctx->super);
    5559                 :       3051 :         ctx->super->super_blob = bs->super_blob;
    5560                 :       3051 :         ctx->super->clean = 0;
    5561                 :       3051 :         ctx->super->cluster_size = bs->cluster_sz;
    5562                 :       3051 :         ctx->super->io_unit_size = bs->io_unit_size;
    5563                 :       3051 :         memcpy(&ctx->super->bstype, &bs->bstype, sizeof(bs->bstype));
    5564                 :            : 
    5565                 :            :         /* Calculate how many pages the metadata consumes at the front
    5566                 :            :          * of the disk.
    5567                 :            :          */
    5568                 :            : 
    5569                 :            :         /* The super block uses 1 page */
    5570                 :       3051 :         num_md_pages = 1;
    5571                 :            : 
    5572                 :            :         /* The used_md_pages mask requires 1 bit per metadata page, rounded
    5573                 :            :          * up to the nearest page, plus a header.
    5574                 :            :          */
    5575                 :       3051 :         ctx->super->used_page_mask_start = num_md_pages;
    5576                 :       3051 :         ctx->super->used_page_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    5577                 :       3051 :                                          spdk_divide_round_up(bs->md_len, 8),
    5578                 :            :                                          SPDK_BS_PAGE_SIZE);
    5579                 :       3051 :         num_md_pages += ctx->super->used_page_mask_len;
    5580                 :            : 
    5581                 :            :         /* The used_clusters mask requires 1 bit per cluster, rounded
    5582                 :            :          * up to the nearest page, plus a header.
    5583                 :            :          */
    5584                 :       3051 :         ctx->super->used_cluster_mask_start = num_md_pages;
    5585                 :       3051 :         ctx->super->used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    5586                 :       3051 :                                             spdk_divide_round_up(bs->total_clusters, 8),
    5587                 :            :                                             SPDK_BS_PAGE_SIZE);
    5588                 :            :         /* The blobstore might be extended, then the used_cluster bitmap will need more space.
    5589                 :            :          * Here we calculate the max clusters we can support according to the
    5590                 :            :          * num_md_pages (bs->md_len).
    5591                 :            :          */
    5592                 :       3051 :         max_used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    5593                 :       3051 :                                     spdk_divide_round_up(bs->md_len, 8),
    5594                 :            :                                     SPDK_BS_PAGE_SIZE);
    5595                 :       3051 :         max_used_cluster_mask_len = spdk_max(max_used_cluster_mask_len,
    5596                 :            :                                              ctx->super->used_cluster_mask_len);
    5597                 :       3051 :         num_md_pages += max_used_cluster_mask_len;
    5598                 :            : 
    5599                 :            :         /* The used_blobids mask requires 1 bit per metadata page, rounded
    5600                 :            :          * up to the nearest page, plus a header.
    5601                 :            :          */
    5602                 :       3051 :         ctx->super->used_blobid_mask_start = num_md_pages;
    5603                 :       3051 :         ctx->super->used_blobid_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    5604                 :       3051 :                                            spdk_divide_round_up(bs->md_len, 8),
    5605                 :            :                                            SPDK_BS_PAGE_SIZE);
    5606                 :       3051 :         num_md_pages += ctx->super->used_blobid_mask_len;
    5607                 :            : 
    5608                 :            :         /* The metadata region size was chosen above */
    5609                 :       3051 :         ctx->super->md_start = bs->md_start = num_md_pages;
    5610                 :       3051 :         ctx->super->md_len = bs->md_len;
    5611                 :       3051 :         num_md_pages += bs->md_len;
    5612                 :            : 
    5613                 :       3051 :         num_md_lba = bs_page_to_lba(bs, num_md_pages);
    5614                 :            : 
    5615                 :       3051 :         ctx->super->size = dev->blockcnt * dev->blocklen;
    5616                 :            : 
    5617                 :       3051 :         ctx->super->crc = blob_md_page_calc_crc(ctx->super);
    5618                 :            : 
    5619                 :       3051 :         num_md_clusters = spdk_divide_round_up(num_md_pages, bs->pages_per_cluster);
    5620         [ +  + ]:       3051 :         if (num_md_clusters > bs->total_clusters) {
    5621                 :         25 :                 SPDK_ERRLOG("Blobstore metadata cannot use more clusters than is available, "
    5622                 :            :                             "please decrease number of pages reserved for metadata "
    5623                 :            :                             "or increase cluster size.\n");
    5624                 :         25 :                 spdk_free(ctx->super);
    5625                 :         25 :                 spdk_bit_array_free(&ctx->used_clusters);
    5626                 :         25 :                 free(ctx);
    5627                 :         25 :                 bs_free(bs);
    5628                 :         25 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5629                 :         25 :                 return;
    5630                 :            :         }
    5631                 :            :         /* Claim all of the clusters used by the metadata */
    5632         [ +  + ]:     533919 :         for (i = 0; i < num_md_clusters; i++) {
    5633                 :     530893 :                 spdk_bit_array_set(ctx->used_clusters, i);
    5634                 :            :         }
    5635                 :            : 
    5636                 :       3026 :         bs->num_free_clusters -= num_md_clusters;
    5637                 :       3026 :         bs->total_data_clusters = bs->num_free_clusters;
    5638                 :            : 
    5639                 :       3026 :         cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
    5640                 :       3026 :         cpl.u.bs_handle.cb_fn = cb_fn;
    5641                 :       3026 :         cpl.u.bs_handle.cb_arg = cb_arg;
    5642                 :       3026 :         cpl.u.bs_handle.bs = bs;
    5643                 :            : 
    5644                 :       3026 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5645         [ -  + ]:       3026 :         if (!seq) {
    5646                 :          0 :                 spdk_free(ctx->super);
    5647                 :          0 :                 free(ctx);
    5648                 :          0 :                 bs_free(bs);
    5649                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    5650                 :          0 :                 return;
    5651                 :            :         }
    5652                 :            : 
    5653                 :       3026 :         batch = bs_sequence_to_batch(seq, bs_init_trim_cpl, ctx);
    5654                 :            : 
    5655                 :            :         /* Clear metadata space */
    5656                 :       3026 :         bs_batch_write_zeroes_dev(batch, 0, num_md_lba);
    5657                 :            : 
    5658                 :       3026 :         lba = num_md_lba;
    5659                 :       3026 :         lba_count = ctx->bs->dev->blockcnt - lba;
    5660      [ +  +  + ]:       3026 :         switch (opts.clear_method) {
    5661                 :       2911 :         case BS_CLEAR_WITH_UNMAP:
    5662                 :            :                 /* Trim data clusters */
    5663                 :       2911 :                 bs_batch_unmap_dev(batch, lba, lba_count);
    5664                 :       2911 :                 break;
    5665                 :          1 :         case BS_CLEAR_WITH_WRITE_ZEROES:
    5666                 :            :                 /* Write_zeroes to data clusters */
    5667                 :          1 :                 bs_batch_write_zeroes_dev(batch, lba, lba_count);
    5668                 :          1 :                 break;
    5669                 :        114 :         case BS_CLEAR_WITH_NONE:
    5670                 :            :         default:
    5671                 :        114 :                 break;
    5672                 :            :         }
    5673                 :            : 
    5674                 :       3026 :         bs_batch_close(batch);
    5675                 :            : }
    5676                 :            : 
    5677                 :            : /* END spdk_bs_init */
    5678                 :            : 
    5679                 :            : /* START spdk_bs_destroy */
    5680                 :            : 
    5681                 :            : static void
    5682                 :        148 : bs_destroy_trim_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5683                 :            : {
    5684                 :        148 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5685                 :        148 :         struct spdk_blob_store *bs = ctx->bs;
    5686                 :            : 
    5687                 :            :         /*
    5688                 :            :          * We need to defer calling bs_call_cpl() until after
    5689                 :            :          * dev destruction, so tuck these away for later use.
    5690                 :            :          */
    5691                 :        148 :         bs->unload_err = bserrno;
    5692   [ -  +  -  + ]:        148 :         memcpy(&bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
    5693                 :        148 :         seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
    5694                 :            : 
    5695                 :        148 :         bs_sequence_finish(seq, bserrno);
    5696                 :            : 
    5697                 :        148 :         bs_free(bs);
    5698                 :        148 :         free(ctx);
    5699                 :        148 : }
    5700                 :            : 
    5701                 :            : void
    5702                 :        148 : spdk_bs_destroy(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn,
    5703                 :            :                 void *cb_arg)
    5704                 :            : {
    5705                 :        103 :         struct spdk_bs_cpl      cpl;
    5706                 :            :         spdk_bs_sequence_t      *seq;
    5707                 :            :         struct spdk_bs_load_ctx *ctx;
    5708                 :            : 
    5709   [ -  +  -  + ]:        148 :         SPDK_DEBUGLOG(blob, "Destroying blobstore\n");
    5710                 :            : 
    5711         [ -  + ]:        148 :         if (!RB_EMPTY(&bs->open_blobs)) {
    5712                 :          0 :                 SPDK_ERRLOG("Blobstore still has open blobs\n");
    5713                 :          0 :                 cb_fn(cb_arg, -EBUSY);
    5714                 :          0 :                 return;
    5715                 :            :         }
    5716                 :            : 
    5717                 :        148 :         cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
    5718                 :        148 :         cpl.u.bs_basic.cb_fn = cb_fn;
    5719                 :        148 :         cpl.u.bs_basic.cb_arg = cb_arg;
    5720                 :            : 
    5721                 :        148 :         ctx = calloc(1, sizeof(*ctx));
    5722         [ -  + ]:        148 :         if (!ctx) {
    5723                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5724                 :          0 :                 return;
    5725                 :            :         }
    5726                 :            : 
    5727                 :        148 :         ctx->bs = bs;
    5728                 :            : 
    5729                 :        148 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5730         [ -  + ]:        148 :         if (!seq) {
    5731                 :          0 :                 free(ctx);
    5732                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5733                 :          0 :                 return;
    5734                 :            :         }
    5735                 :            : 
    5736                 :            :         /* Write zeroes to the super block */
    5737                 :        148 :         bs_sequence_write_zeroes_dev(seq,
    5738                 :            :                                      bs_page_to_lba(bs, 0),
    5739                 :            :                                      bs_byte_to_lba(bs, sizeof(struct spdk_bs_super_block)),
    5740                 :            :                                      bs_destroy_trim_cpl, ctx);
    5741                 :            : }
    5742                 :            : 
    5743                 :            : /* END spdk_bs_destroy */
    5744                 :            : 
    5745                 :            : /* START spdk_bs_unload */
    5746                 :            : 
    5747                 :            : static void
    5748                 :       4085 : bs_unload_finish(struct spdk_bs_load_ctx *ctx, int bserrno)
    5749                 :            : {
    5750                 :       4085 :         spdk_bs_sequence_t *seq = ctx->seq;
    5751                 :            : 
    5752                 :       4085 :         spdk_free(ctx->super);
    5753                 :            : 
    5754                 :            :         /*
    5755                 :            :          * We need to defer calling bs_call_cpl() until after
    5756                 :            :          * dev destruction, so tuck these away for later use.
    5757                 :            :          */
    5758                 :       4085 :         ctx->bs->unload_err = bserrno;
    5759   [ -  +  -  + ]:       4085 :         memcpy(&ctx->bs->unload_cpl, &seq->cpl, sizeof(struct spdk_bs_cpl));
    5760                 :       4085 :         seq->cpl.type = SPDK_BS_CPL_TYPE_NONE;
    5761                 :            : 
    5762                 :       4085 :         bs_sequence_finish(seq, bserrno);
    5763                 :            : 
    5764                 :       4085 :         bs_free(ctx->bs);
    5765                 :       4085 :         free(ctx);
    5766                 :       4085 : }
    5767                 :            : 
    5768                 :            : static void
    5769                 :       4085 : bs_unload_write_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5770                 :            : {
    5771                 :       4085 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5772                 :            : 
    5773                 :       4085 :         bs_unload_finish(ctx, bserrno);
    5774                 :       4085 : }
    5775                 :            : 
    5776                 :            : static void
    5777                 :       4085 : bs_unload_write_used_clusters_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5778                 :            : {
    5779                 :       4085 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5780                 :            : 
    5781                 :       4085 :         spdk_free(ctx->mask);
    5782                 :            : 
    5783         [ -  + ]:       4085 :         if (bserrno != 0) {
    5784                 :          0 :                 bs_unload_finish(ctx, bserrno);
    5785                 :          0 :                 return;
    5786                 :            :         }
    5787                 :            : 
    5788                 :       4085 :         ctx->super->clean = 1;
    5789                 :            : 
    5790                 :       4085 :         bs_write_super(seq, ctx->bs, ctx->super, bs_unload_write_super_cpl, ctx);
    5791                 :            : }
    5792                 :            : 
    5793                 :            : static void
    5794                 :       4085 : bs_unload_write_used_blobids_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5795                 :            : {
    5796                 :       4085 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5797                 :            : 
    5798                 :       4085 :         spdk_free(ctx->mask);
    5799                 :       4085 :         ctx->mask = NULL;
    5800                 :            : 
    5801         [ -  + ]:       4085 :         if (bserrno != 0) {
    5802                 :          0 :                 bs_unload_finish(ctx, bserrno);
    5803                 :          0 :                 return;
    5804                 :            :         }
    5805                 :            : 
    5806                 :       4085 :         bs_write_used_clusters(seq, ctx, bs_unload_write_used_clusters_cpl);
    5807                 :            : }
    5808                 :            : 
    5809                 :            : static void
    5810                 :       4085 : bs_unload_write_used_pages_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5811                 :            : {
    5812                 :       4085 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5813                 :            : 
    5814                 :       4085 :         spdk_free(ctx->mask);
    5815                 :       4085 :         ctx->mask = NULL;
    5816                 :            : 
    5817         [ -  + ]:       4085 :         if (bserrno != 0) {
    5818                 :          0 :                 bs_unload_finish(ctx, bserrno);
    5819                 :          0 :                 return;
    5820                 :            :         }
    5821                 :            : 
    5822                 :       4085 :         bs_write_used_blobids(seq, ctx, bs_unload_write_used_blobids_cpl);
    5823                 :            : }
    5824                 :            : 
    5825                 :            : static void
    5826                 :       4085 : bs_unload_read_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5827                 :            : {
    5828                 :       4085 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    5829                 :            :         int rc;
    5830                 :            : 
    5831         [ -  + ]:       4085 :         if (bserrno != 0) {
    5832                 :          0 :                 bs_unload_finish(ctx, bserrno);
    5833                 :          0 :                 return;
    5834                 :            :         }
    5835                 :            : 
    5836                 :       4085 :         rc = bs_super_validate(ctx->super, ctx->bs);
    5837         [ -  + ]:       4085 :         if (rc != 0) {
    5838                 :          0 :                 bs_unload_finish(ctx, rc);
    5839                 :          0 :                 return;
    5840                 :            :         }
    5841                 :            : 
    5842                 :       4085 :         bs_write_used_md(seq, cb_arg, bs_unload_write_used_pages_cpl);
    5843                 :            : }
    5844                 :            : 
    5845                 :            : void
    5846                 :       4149 : spdk_bs_unload(struct spdk_blob_store *bs, spdk_bs_op_complete cb_fn, void *cb_arg)
    5847                 :            : {
    5848                 :       3443 :         struct spdk_bs_cpl      cpl;
    5849                 :            :         struct spdk_bs_load_ctx *ctx;
    5850                 :            : 
    5851   [ -  +  -  + ]:       4149 :         SPDK_DEBUGLOG(blob, "Syncing blobstore\n");
    5852                 :            : 
    5853                 :            :         /*
    5854                 :            :          * If external snapshot channels are being destroyed while the blobstore is unloaded, the
    5855                 :            :          * unload is deferred until after the channel destruction completes.
    5856                 :            :          */
    5857         [ +  + ]:       4149 :         if (bs->esnap_channels_unloading != 0) {
    5858         [ -  + ]:         40 :                 if (bs->esnap_unload_cb_fn != NULL) {
    5859                 :          0 :                         SPDK_ERRLOG("Blobstore unload in progress\n");
    5860                 :          0 :                         cb_fn(cb_arg, -EBUSY);
    5861                 :          8 :                         return;
    5862                 :            :                 }
    5863   [ -  +  -  + ]:         40 :                 SPDK_DEBUGLOG(blob_esnap, "Blobstore unload deferred: %" PRIu32
    5864                 :            :                               " esnap clones are unloading\n", bs->esnap_channels_unloading);
    5865                 :         40 :                 bs->esnap_unload_cb_fn = cb_fn;
    5866                 :         40 :                 bs->esnap_unload_cb_arg = cb_arg;
    5867                 :         40 :                 return;
    5868                 :            :         }
    5869         [ +  + ]:       4109 :         if (bs->esnap_unload_cb_fn != NULL) {
    5870   [ -  +  -  + ]:         40 :                 SPDK_DEBUGLOG(blob_esnap, "Blobstore deferred unload progressing\n");
    5871         [ -  + ]:         40 :                 assert(bs->esnap_unload_cb_fn == cb_fn);
    5872         [ -  + ]:         40 :                 assert(bs->esnap_unload_cb_arg == cb_arg);
    5873                 :         40 :                 bs->esnap_unload_cb_fn = NULL;
    5874                 :         40 :                 bs->esnap_unload_cb_arg = NULL;
    5875                 :            :         }
    5876                 :            : 
    5877         [ +  + ]:       4109 :         if (!RB_EMPTY(&bs->open_blobs)) {
    5878                 :         24 :                 SPDK_ERRLOG("Blobstore still has open blobs\n");
    5879                 :         24 :                 cb_fn(cb_arg, -EBUSY);
    5880                 :         24 :                 return;
    5881                 :            :         }
    5882                 :            : 
    5883                 :       4085 :         ctx = calloc(1, sizeof(*ctx));
    5884         [ -  + ]:       4085 :         if (!ctx) {
    5885                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5886                 :          0 :                 return;
    5887                 :            :         }
    5888                 :            : 
    5889                 :       4085 :         ctx->bs = bs;
    5890                 :            : 
    5891                 :       4085 :         ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
    5892                 :            :                                   SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    5893         [ -  + ]:       4085 :         if (!ctx->super) {
    5894                 :          0 :                 free(ctx);
    5895                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5896                 :          0 :                 return;
    5897                 :            :         }
    5898                 :            : 
    5899                 :       4085 :         cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
    5900                 :       4085 :         cpl.u.bs_basic.cb_fn = cb_fn;
    5901                 :       4085 :         cpl.u.bs_basic.cb_arg = cb_arg;
    5902                 :            : 
    5903                 :       4085 :         ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5904         [ -  + ]:       4085 :         if (!ctx->seq) {
    5905                 :          0 :                 spdk_free(ctx->super);
    5906                 :          0 :                 free(ctx);
    5907                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5908                 :          0 :                 return;
    5909                 :            :         }
    5910                 :            : 
    5911                 :            :         /* Read super block */
    5912                 :       4085 :         bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
    5913                 :       4085 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    5914                 :            :                              bs_unload_read_super_cpl, ctx);
    5915                 :            : }
    5916                 :            : 
    5917                 :            : /* END spdk_bs_unload */
    5918                 :            : 
    5919                 :            : /* START spdk_bs_set_super */
    5920                 :            : 
    5921                 :            : struct spdk_bs_set_super_ctx {
    5922                 :            :         struct spdk_blob_store          *bs;
    5923                 :            :         struct spdk_bs_super_block      *super;
    5924                 :            : };
    5925                 :            : 
    5926                 :            : static void
    5927                 :        239 : bs_set_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5928                 :            : {
    5929                 :        239 :         struct spdk_bs_set_super_ctx    *ctx = cb_arg;
    5930                 :            : 
    5931         [ -  + ]:        239 :         if (bserrno != 0) {
    5932                 :          0 :                 SPDK_ERRLOG("Unable to write to super block of blobstore\n");
    5933                 :            :         }
    5934                 :            : 
    5935                 :        239 :         spdk_free(ctx->super);
    5936                 :            : 
    5937                 :        239 :         bs_sequence_finish(seq, bserrno);
    5938                 :            : 
    5939                 :        239 :         free(ctx);
    5940                 :        239 : }
    5941                 :            : 
    5942                 :            : static void
    5943                 :        239 : bs_set_super_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    5944                 :            : {
    5945                 :        239 :         struct spdk_bs_set_super_ctx    *ctx = cb_arg;
    5946                 :            :         int rc;
    5947                 :            : 
    5948         [ -  + ]:        239 :         if (bserrno != 0) {
    5949                 :          0 :                 SPDK_ERRLOG("Unable to read super block of blobstore\n");
    5950                 :          0 :                 spdk_free(ctx->super);
    5951                 :          0 :                 bs_sequence_finish(seq, bserrno);
    5952                 :          0 :                 free(ctx);
    5953                 :          0 :                 return;
    5954                 :            :         }
    5955                 :            : 
    5956                 :        239 :         rc = bs_super_validate(ctx->super, ctx->bs);
    5957         [ -  + ]:        239 :         if (rc != 0) {
    5958                 :          0 :                 SPDK_ERRLOG("Not a valid super block\n");
    5959                 :          0 :                 spdk_free(ctx->super);
    5960                 :          0 :                 bs_sequence_finish(seq, rc);
    5961                 :          0 :                 free(ctx);
    5962                 :          0 :                 return;
    5963                 :            :         }
    5964                 :            : 
    5965                 :        239 :         bs_write_super(seq, ctx->bs, ctx->super, bs_set_super_write_cpl, ctx);
    5966                 :            : }
    5967                 :            : 
    5968                 :            : void
    5969                 :        239 : spdk_bs_set_super(struct spdk_blob_store *bs, spdk_blob_id blobid,
    5970                 :            :                   spdk_bs_op_complete cb_fn, void *cb_arg)
    5971                 :            : {
    5972                 :        159 :         struct spdk_bs_cpl              cpl;
    5973                 :            :         spdk_bs_sequence_t              *seq;
    5974                 :            :         struct spdk_bs_set_super_ctx    *ctx;
    5975                 :            : 
    5976   [ -  +  -  + ]:        239 :         SPDK_DEBUGLOG(blob, "Setting super blob id on blobstore\n");
    5977                 :            : 
    5978                 :        239 :         ctx = calloc(1, sizeof(*ctx));
    5979         [ -  + ]:        239 :         if (!ctx) {
    5980                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5981                 :          0 :                 return;
    5982                 :            :         }
    5983                 :            : 
    5984                 :        239 :         ctx->bs = bs;
    5985                 :            : 
    5986                 :        239 :         ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
    5987                 :            :                                   SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    5988         [ -  + ]:        239 :         if (!ctx->super) {
    5989                 :          0 :                 free(ctx);
    5990                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    5991                 :          0 :                 return;
    5992                 :            :         }
    5993                 :            : 
    5994                 :        239 :         cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
    5995                 :        239 :         cpl.u.bs_basic.cb_fn = cb_fn;
    5996                 :        239 :         cpl.u.bs_basic.cb_arg = cb_arg;
    5997                 :            : 
    5998                 :        239 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    5999         [ -  + ]:        239 :         if (!seq) {
    6000                 :          0 :                 spdk_free(ctx->super);
    6001                 :          0 :                 free(ctx);
    6002                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    6003                 :          0 :                 return;
    6004                 :            :         }
    6005                 :            : 
    6006                 :        239 :         bs->super_blob = blobid;
    6007                 :            : 
    6008                 :            :         /* Read super block */
    6009                 :        239 :         bs_sequence_read_dev(seq, ctx->super, bs_page_to_lba(bs, 0),
    6010                 :        239 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    6011                 :            :                              bs_set_super_read_cpl, ctx);
    6012                 :            : }
    6013                 :            : 
    6014                 :            : /* END spdk_bs_set_super */
    6015                 :            : 
    6016                 :            : void
    6017                 :        145 : spdk_bs_get_super(struct spdk_blob_store *bs,
    6018                 :            :                   spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6019                 :            : {
    6020         [ +  + ]:        145 :         if (bs->super_blob == SPDK_BLOBID_INVALID) {
    6021                 :         24 :                 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOENT);
    6022                 :            :         } else {
    6023                 :        121 :                 cb_fn(cb_arg, bs->super_blob, 0);
    6024                 :            :         }
    6025                 :        145 : }
    6026                 :            : 
    6027                 :            : uint64_t
    6028                 :       2767 : spdk_bs_get_cluster_size(struct spdk_blob_store *bs)
    6029                 :            : {
    6030                 :       2767 :         return bs->cluster_sz;
    6031                 :            : }
    6032                 :            : 
    6033                 :            : uint64_t
    6034                 :        419 : spdk_bs_get_page_size(struct spdk_blob_store *bs)
    6035                 :            : {
    6036                 :        419 :         return SPDK_BS_PAGE_SIZE;
    6037                 :            : }
    6038                 :            : 
    6039                 :            : uint64_t
    6040                 :    5536707 : spdk_bs_get_io_unit_size(struct spdk_blob_store *bs)
    6041                 :            : {
    6042                 :    5536707 :         return bs->io_unit_size;
    6043                 :            : }
    6044                 :            : 
    6045                 :            : uint64_t
    6046                 :       3447 : spdk_bs_free_cluster_count(struct spdk_blob_store *bs)
    6047                 :            : {
    6048                 :       3447 :         return bs->num_free_clusters;
    6049                 :            : }
    6050                 :            : 
    6051                 :            : uint64_t
    6052                 :        756 : spdk_bs_total_data_cluster_count(struct spdk_blob_store *bs)
    6053                 :            : {
    6054                 :        756 :         return bs->total_data_clusters;
    6055                 :            : }
    6056                 :            : 
    6057                 :            : static int
    6058                 :      12494 : bs_register_md_thread(struct spdk_blob_store *bs)
    6059                 :            : {
    6060                 :      12494 :         bs->md_channel = spdk_get_io_channel(bs);
    6061         [ -  + ]:      12494 :         if (!bs->md_channel) {
    6062                 :          0 :                 SPDK_ERRLOG("Failed to get IO channel.\n");
    6063                 :          0 :                 return -1;
    6064                 :            :         }
    6065                 :            : 
    6066                 :      12494 :         return 0;
    6067                 :            : }
    6068                 :            : 
    6069                 :            : static int
    6070                 :      12494 : bs_unregister_md_thread(struct spdk_blob_store *bs)
    6071                 :            : {
    6072                 :      12494 :         spdk_put_io_channel(bs->md_channel);
    6073                 :            : 
    6074                 :      12494 :         return 0;
    6075                 :            : }
    6076                 :            : 
    6077                 :            : spdk_blob_id
    6078                 :       9837 : spdk_blob_get_id(struct spdk_blob *blob)
    6079                 :            : {
    6080         [ -  + ]:       9837 :         assert(blob != NULL);
    6081                 :            : 
    6082                 :       9837 :         return blob->id;
    6083                 :            : }
    6084                 :            : 
    6085                 :            : uint64_t
    6086                 :        183 : spdk_blob_get_num_pages(struct spdk_blob *blob)
    6087                 :            : {
    6088         [ -  + ]:        183 :         assert(blob != NULL);
    6089                 :            : 
    6090                 :        183 :         return bs_cluster_to_page(blob->bs, blob->active.num_clusters);
    6091                 :            : }
    6092                 :            : 
    6093                 :            : uint64_t
    6094                 :        182 : spdk_blob_get_num_io_units(struct spdk_blob *blob)
    6095                 :            : {
    6096         [ -  + ]:        182 :         assert(blob != NULL);
    6097                 :            : 
    6098                 :        182 :         return spdk_blob_get_num_pages(blob) * bs_io_unit_per_page(blob->bs);
    6099                 :            : }
    6100                 :            : 
    6101                 :            : uint64_t
    6102                 :    1054736 : spdk_blob_get_num_clusters(struct spdk_blob *blob)
    6103                 :            : {
    6104         [ -  + ]:    1054736 :         assert(blob != NULL);
    6105                 :            : 
    6106                 :    1054736 :         return blob->active.num_clusters;
    6107                 :            : }
    6108                 :            : 
    6109                 :            : uint64_t
    6110                 :       3179 : spdk_blob_get_num_allocated_clusters(struct spdk_blob *blob)
    6111                 :            : {
    6112         [ -  + ]:       3179 :         assert(blob != NULL);
    6113                 :            : 
    6114                 :       3179 :         return blob->active.num_allocated_clusters;
    6115                 :            : }
    6116                 :            : 
    6117                 :            : static uint64_t
    6118                 :        180 : blob_find_io_unit(struct spdk_blob *blob, uint64_t offset, bool is_allocated)
    6119                 :            : {
    6120                 :        180 :         uint64_t blob_io_unit_num = spdk_blob_get_num_io_units(blob);
    6121                 :            : 
    6122         [ +  + ]:        354 :         while (offset < blob_io_unit_num) {
    6123         [ +  + ]:        324 :                 if (bs_io_unit_is_allocated(blob, offset) == is_allocated) {
    6124                 :        150 :                         return offset;
    6125                 :            :                 }
    6126                 :            : 
    6127                 :        174 :                 offset += bs_num_io_units_to_cluster_boundary(blob, offset);
    6128                 :            :         }
    6129                 :            : 
    6130                 :         30 :         return UINT64_MAX;
    6131                 :            : }
    6132                 :            : 
    6133                 :            : uint64_t
    6134                 :         90 : spdk_blob_get_next_allocated_io_unit(struct spdk_blob *blob, uint64_t offset)
    6135                 :            : {
    6136                 :         90 :         return blob_find_io_unit(blob, offset, true);
    6137                 :            : }
    6138                 :            : 
    6139                 :            : uint64_t
    6140                 :         90 : spdk_blob_get_next_unallocated_io_unit(struct spdk_blob *blob, uint64_t offset)
    6141                 :            : {
    6142                 :         90 :         return blob_find_io_unit(blob, offset, false);
    6143                 :            : }
    6144                 :            : 
    6145                 :            : /* START spdk_bs_create_blob */
    6146                 :            : 
    6147                 :            : static void
    6148                 :      14628 : bs_create_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    6149                 :            : {
    6150                 :      14628 :         struct spdk_blob *blob = cb_arg;
    6151                 :      14628 :         uint32_t page_idx = bs_blobid_to_page(blob->id);
    6152                 :            : 
    6153         [ -  + ]:      14628 :         if (bserrno != 0) {
    6154                 :          0 :                 spdk_spin_lock(&blob->bs->used_lock);
    6155                 :          0 :                 spdk_bit_array_clear(blob->bs->used_blobids, page_idx);
    6156                 :          0 :                 bs_release_md_page(blob->bs, page_idx);
    6157                 :          0 :                 spdk_spin_unlock(&blob->bs->used_lock);
    6158                 :            :         }
    6159                 :            : 
    6160                 :      14628 :         blob_free(blob);
    6161                 :            : 
    6162                 :      14628 :         bs_sequence_finish(seq, bserrno);
    6163                 :      14628 : }
    6164                 :            : 
    6165                 :            : static int
    6166                 :      29380 : blob_set_xattrs(struct spdk_blob *blob, const struct spdk_blob_xattr_opts *xattrs,
    6167                 :            :                 bool internal)
    6168                 :            : {
    6169                 :            :         uint64_t i;
    6170                 :      29380 :         size_t value_len = 0;
    6171                 :            :         int rc;
    6172                 :      29380 :         const void *value = NULL;
    6173   [ +  +  +  + ]:      29380 :         if (xattrs->count > 0 && xattrs->get_value == NULL) {
    6174                 :         48 :                 return -EINVAL;
    6175                 :            :         }
    6176         [ +  + ]:      32329 :         for (i = 0; i < xattrs->count; i++) {
    6177                 :       3021 :                 xattrs->get_value(xattrs->ctx, xattrs->names[i], &value, &value_len);
    6178   [ +  +  -  + ]:       3021 :                 if (value == NULL || value_len == 0) {
    6179                 :         24 :                         return -EINVAL;
    6180                 :            :                 }
    6181                 :       2997 :                 rc = blob_set_xattr(blob, xattrs->names[i], value, value_len, internal);
    6182         [ -  + ]:       2997 :                 if (rc < 0) {
    6183                 :          0 :                         return rc;
    6184                 :            :                 }
    6185                 :            :         }
    6186                 :      29308 :         return 0;
    6187                 :            : }
    6188                 :            : 
    6189                 :            : static void
    6190                 :      11681 : blob_opts_copy(const struct spdk_blob_opts *src, struct spdk_blob_opts *dst)
    6191                 :            : {
    6192                 :            : #define FIELD_OK(field) \
    6193                 :            :         offsetof(struct spdk_blob_opts, field) + sizeof(src->field) <= src->opts_size
    6194                 :            : 
    6195                 :            : #define SET_FIELD(field) \
    6196                 :            :         if (FIELD_OK(field)) { \
    6197                 :            :                 dst->field = src->field; \
    6198                 :            :         } \
    6199                 :            : 
    6200         [ +  - ]:      11681 :         SET_FIELD(num_clusters);
    6201   [ +  -  -  + ]:      11681 :         SET_FIELD(thin_provision);
    6202         [ +  - ]:      11681 :         SET_FIELD(clear_method);
    6203                 :            : 
    6204         [ +  - ]:      11681 :         if (FIELD_OK(xattrs)) {
    6205   [ -  +  -  + ]:      11681 :                 memcpy(&dst->xattrs, &src->xattrs, sizeof(src->xattrs));
    6206                 :            :         }
    6207                 :            : 
    6208   [ +  -  -  + ]:      11681 :         SET_FIELD(use_extent_table);
    6209         [ +  - ]:      11681 :         SET_FIELD(esnap_id);
    6210         [ +  - ]:      11681 :         SET_FIELD(esnap_id_len);
    6211                 :            : 
    6212                 :      11681 :         dst->opts_size = src->opts_size;
    6213                 :            : 
    6214                 :            :         /* You should not remove this statement, but need to update the assert statement
    6215                 :            :          * if you add a new field, and also add a corresponding SET_FIELD statement */
    6216                 :            :         SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_opts) == 80, "Incorrect size");
    6217                 :            : 
    6218                 :            : #undef FIELD_OK
    6219                 :            : #undef SET_FIELD
    6220                 :      11681 : }
    6221                 :            : 
    6222                 :            : static void
    6223                 :      14726 : bs_create_blob(struct spdk_blob_store *bs,
    6224                 :            :                const struct spdk_blob_opts *opts,
    6225                 :            :                const struct spdk_blob_xattr_opts *internal_xattrs,
    6226                 :            :                spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6227                 :            : {
    6228                 :            :         struct spdk_blob        *blob;
    6229                 :            :         uint32_t                page_idx;
    6230                 :      12443 :         struct spdk_bs_cpl      cpl;
    6231                 :      12443 :         struct spdk_blob_opts   opts_local;
    6232                 :      12443 :         struct spdk_blob_xattr_opts internal_xattrs_default;
    6233                 :            :         spdk_bs_sequence_t      *seq;
    6234                 :            :         spdk_blob_id            id;
    6235                 :            :         int rc;
    6236                 :            : 
    6237         [ -  + ]:      14726 :         assert(spdk_get_thread() == bs->md_thread);
    6238                 :            : 
    6239                 :      14726 :         spdk_spin_lock(&bs->used_lock);
    6240                 :      14726 :         page_idx = spdk_bit_array_find_first_clear(bs->used_md_pages, 0);
    6241         [ -  + ]:      14726 :         if (page_idx == UINT32_MAX) {
    6242                 :          0 :                 spdk_spin_unlock(&bs->used_lock);
    6243                 :          0 :                 cb_fn(cb_arg, 0, -ENOMEM);
    6244                 :       2267 :                 return;
    6245                 :            :         }
    6246                 :      14726 :         spdk_bit_array_set(bs->used_blobids, page_idx);
    6247                 :      14726 :         bs_claim_md_page(bs, page_idx);
    6248                 :      14726 :         spdk_spin_unlock(&bs->used_lock);
    6249                 :            : 
    6250                 :      14726 :         id = bs_page_to_blobid(page_idx);
    6251                 :            : 
    6252   [ -  +  -  + ]:      14726 :         SPDK_DEBUGLOG(blob, "Creating blob with id 0x%" PRIx64 " at page %u\n", id, page_idx);
    6253                 :            : 
    6254                 :      14726 :         spdk_blob_opts_init(&opts_local, sizeof(opts_local));
    6255         [ +  + ]:      14726 :         if (opts) {
    6256                 :      11681 :                 blob_opts_copy(opts, &opts_local);
    6257                 :            :         }
    6258                 :            : 
    6259                 :      14726 :         blob = blob_alloc(bs, id);
    6260         [ -  + ]:      14726 :         if (!blob) {
    6261                 :          0 :                 rc = -ENOMEM;
    6262                 :          0 :                 goto error;
    6263                 :            :         }
    6264                 :            : 
    6265         [ -  + ]:      14726 :         blob->use_extent_table = opts_local.use_extent_table;
    6266   [ +  +  +  + ]:      14726 :         if (blob->use_extent_table) {
    6267                 :       9182 :                 blob->invalid_flags |= SPDK_BLOB_EXTENT_TABLE;
    6268                 :            :         }
    6269                 :            : 
    6270         [ +  + ]:      14726 :         if (!internal_xattrs) {
    6271                 :      13059 :                 blob_xattrs_init(&internal_xattrs_default);
    6272                 :      13059 :                 internal_xattrs = &internal_xattrs_default;
    6273                 :            :         }
    6274                 :            : 
    6275                 :      14726 :         rc = blob_set_xattrs(blob, &opts_local.xattrs, false);
    6276         [ +  + ]:      14726 :         if (rc < 0) {
    6277                 :         72 :                 goto error;
    6278                 :            :         }
    6279                 :            : 
    6280                 :      14654 :         rc = blob_set_xattrs(blob, internal_xattrs, true);
    6281         [ -  + ]:      14654 :         if (rc < 0) {
    6282                 :          0 :                 goto error;
    6283                 :            :         }
    6284                 :            : 
    6285   [ +  +  +  + ]:      14654 :         if (opts_local.thin_provision) {
    6286                 :       2230 :                 blob_set_thin_provision(blob);
    6287                 :            :         }
    6288                 :            : 
    6289                 :      14654 :         blob_set_clear_method(blob, opts_local.clear_method);
    6290                 :            : 
    6291         [ +  + ]:      14654 :         if (opts_local.esnap_id != NULL) {
    6292         [ -  + ]:        370 :                 if (opts_local.esnap_id_len > UINT16_MAX) {
    6293                 :          0 :                         SPDK_ERRLOG("esnap id length %" PRIu64 "is too long\n",
    6294                 :            :                                     opts_local.esnap_id_len);
    6295                 :          0 :                         rc = -EINVAL;
    6296                 :          0 :                         goto error;
    6297                 :            : 
    6298                 :            :                 }
    6299                 :        370 :                 blob_set_thin_provision(blob);
    6300                 :        370 :                 blob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
    6301                 :        370 :                 rc = blob_set_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID,
    6302                 :        370 :                                     opts_local.esnap_id, opts_local.esnap_id_len, true);
    6303         [ -  + ]:        370 :                 if (rc != 0) {
    6304                 :          0 :                         goto error;
    6305                 :            :                 }
    6306                 :            :         }
    6307                 :            : 
    6308                 :      14654 :         rc = blob_resize(blob, opts_local.num_clusters);
    6309         [ +  + ]:      14654 :         if (rc < 0) {
    6310                 :         26 :                 goto error;
    6311                 :            :         }
    6312                 :      14628 :         cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
    6313                 :      14628 :         cpl.u.blobid.cb_fn = cb_fn;
    6314                 :      14628 :         cpl.u.blobid.cb_arg = cb_arg;
    6315                 :      14628 :         cpl.u.blobid.blobid = blob->id;
    6316                 :            : 
    6317                 :      14628 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    6318         [ -  + ]:      14628 :         if (!seq) {
    6319                 :          0 :                 rc = -ENOMEM;
    6320                 :          0 :                 goto error;
    6321                 :            :         }
    6322                 :            : 
    6323                 :      14628 :         blob_persist(seq, blob, bs_create_blob_cpl, blob);
    6324                 :      14628 :         return;
    6325                 :            : 
    6326                 :         98 : error:
    6327                 :         98 :         SPDK_ERRLOG("Failed to create blob: %s, size in clusters/size: %lu (clusters)\n",
    6328                 :            :                     spdk_strerror(rc), opts_local.num_clusters);
    6329         [ +  - ]:         98 :         if (blob != NULL) {
    6330                 :         98 :                 blob_free(blob);
    6331                 :            :         }
    6332                 :         98 :         spdk_spin_lock(&bs->used_lock);
    6333                 :         98 :         spdk_bit_array_clear(bs->used_blobids, page_idx);
    6334                 :         98 :         bs_release_md_page(bs, page_idx);
    6335                 :         98 :         spdk_spin_unlock(&bs->used_lock);
    6336                 :         98 :         cb_fn(cb_arg, 0, rc);
    6337                 :            : }
    6338                 :            : 
    6339                 :            : void
    6340                 :       2949 : spdk_bs_create_blob(struct spdk_blob_store *bs,
    6341                 :            :                     spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6342                 :            : {
    6343                 :       2949 :         bs_create_blob(bs, NULL, NULL, cb_fn, cb_arg);
    6344                 :       2949 : }
    6345                 :            : 
    6346                 :            : void
    6347                 :      10062 : spdk_bs_create_blob_ext(struct spdk_blob_store *bs, const struct spdk_blob_opts *opts,
    6348                 :            :                         spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6349                 :            : {
    6350                 :      10062 :         bs_create_blob(bs, opts, NULL, cb_fn, cb_arg);
    6351                 :      10062 : }
    6352                 :            : 
    6353                 :            : /* END spdk_bs_create_blob */
    6354                 :            : 
    6355                 :            : /* START blob_cleanup */
    6356                 :            : 
    6357                 :            : struct spdk_clone_snapshot_ctx {
    6358                 :            :         struct spdk_bs_cpl      cpl;
    6359                 :            :         int bserrno;
    6360                 :            :         bool frozen;
    6361                 :            : 
    6362                 :            :         struct spdk_io_channel *channel;
    6363                 :            : 
    6364                 :            :         /* Current cluster for inflate operation */
    6365                 :            :         uint64_t cluster;
    6366                 :            : 
    6367                 :            :         /* For inflation force allocation of all unallocated clusters and remove
    6368                 :            :          * thin-provisioning. Otherwise only decouple parent and keep clone thin. */
    6369                 :            :         bool allocate_all;
    6370                 :            : 
    6371                 :            :         struct {
    6372                 :            :                 spdk_blob_id id;
    6373                 :            :                 struct spdk_blob *blob;
    6374                 :            :                 bool md_ro;
    6375                 :            :         } original;
    6376                 :            :         struct {
    6377                 :            :                 spdk_blob_id id;
    6378                 :            :                 struct spdk_blob *blob;
    6379                 :            :         } new;
    6380                 :            : 
    6381                 :            :         /* xattrs specified for snapshot/clones only. They have no impact on
    6382                 :            :          * the original blobs xattrs. */
    6383                 :            :         const struct spdk_blob_xattr_opts *xattrs;
    6384                 :            : };
    6385                 :            : 
    6386                 :            : static void
    6387                 :       2098 : bs_clone_snapshot_cleanup_finish(void *cb_arg, int bserrno)
    6388                 :            : {
    6389                 :       2098 :         struct spdk_clone_snapshot_ctx *ctx = cb_arg;
    6390                 :       2098 :         struct spdk_bs_cpl *cpl = &ctx->cpl;
    6391                 :            : 
    6392         [ +  + ]:       2098 :         if (bserrno != 0) {
    6393         [ -  + ]:         36 :                 if (ctx->bserrno != 0) {
    6394                 :          0 :                         SPDK_ERRLOG("Cleanup error %d\n", bserrno);
    6395                 :            :                 } else {
    6396                 :         36 :                         ctx->bserrno = bserrno;
    6397                 :            :                 }
    6398                 :            :         }
    6399                 :            : 
    6400      [ +  +  - ]:       2098 :         switch (cpl->type) {
    6401                 :       1730 :         case SPDK_BS_CPL_TYPE_BLOBID:
    6402                 :       1730 :                 cpl->u.blobid.cb_fn(cpl->u.blobid.cb_arg, cpl->u.blobid.blobid, ctx->bserrno);
    6403                 :       1730 :                 break;
    6404                 :        368 :         case SPDK_BS_CPL_TYPE_BLOB_BASIC:
    6405                 :        368 :                 cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno);
    6406                 :        368 :                 break;
    6407                 :          0 :         default:
    6408                 :          0 :                 SPDK_UNREACHABLE();
    6409                 :            :                 break;
    6410                 :            :         }
    6411                 :            : 
    6412                 :       2098 :         free(ctx);
    6413                 :       2098 : }
    6414                 :            : 
    6415                 :            : static void
    6416                 :       2011 : bs_snapshot_unfreeze_cpl(void *cb_arg, int bserrno)
    6417                 :            : {
    6418                 :       2011 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6419                 :       2011 :         struct spdk_blob *origblob = ctx->original.blob;
    6420                 :            : 
    6421         [ -  + ]:       2011 :         if (bserrno != 0) {
    6422         [ #  # ]:          0 :                 if (ctx->bserrno != 0) {
    6423                 :          0 :                         SPDK_ERRLOG("Unfreeze error %d\n", bserrno);
    6424                 :            :                 } else {
    6425                 :          0 :                         ctx->bserrno = bserrno;
    6426                 :            :                 }
    6427                 :            :         }
    6428                 :            : 
    6429                 :       2011 :         ctx->original.id = origblob->id;
    6430                 :       2011 :         origblob->locked_operation_in_progress = false;
    6431                 :            : 
    6432                 :            :         /* Revert md_ro to original state */
    6433         [ -  + ]:       2011 :         origblob->md_ro = ctx->original.md_ro;
    6434                 :            : 
    6435                 :       2011 :         spdk_blob_close(origblob, bs_clone_snapshot_cleanup_finish, ctx);
    6436                 :       2011 : }
    6437                 :            : 
    6438                 :            : static void
    6439                 :       2011 : bs_clone_snapshot_origblob_cleanup(void *cb_arg, int bserrno)
    6440                 :            : {
    6441                 :       2011 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6442                 :       2011 :         struct spdk_blob *origblob = ctx->original.blob;
    6443                 :            : 
    6444         [ +  + ]:       2011 :         if (bserrno != 0) {
    6445         [ +  + ]:        145 :                 if (ctx->bserrno != 0) {
    6446                 :         24 :                         SPDK_ERRLOG("Cleanup error %d\n", bserrno);
    6447                 :            :                 } else {
    6448                 :        121 :                         ctx->bserrno = bserrno;
    6449                 :            :                 }
    6450                 :            :         }
    6451                 :            : 
    6452   [ +  +  +  + ]:       2011 :         if (ctx->frozen) {
    6453                 :            :                 /* Unfreeze any outstanding I/O */
    6454                 :       1285 :                 blob_unfreeze_io(origblob, bs_snapshot_unfreeze_cpl, ctx);
    6455                 :            :         } else {
    6456                 :        726 :                 bs_snapshot_unfreeze_cpl(ctx, 0);
    6457                 :            :         }
    6458                 :            : 
    6459                 :       2011 : }
    6460                 :            : 
    6461                 :            : static void
    6462                 :         24 : bs_clone_snapshot_newblob_cleanup(struct spdk_clone_snapshot_ctx *ctx, int bserrno)
    6463                 :            : {
    6464                 :         24 :         struct spdk_blob *newblob = ctx->new.blob;
    6465                 :            : 
    6466         [ +  - ]:         24 :         if (bserrno != 0) {
    6467         [ -  + ]:         24 :                 if (ctx->bserrno != 0) {
    6468                 :          0 :                         SPDK_ERRLOG("Cleanup error %d\n", bserrno);
    6469                 :            :                 } else {
    6470                 :         24 :                         ctx->bserrno = bserrno;
    6471                 :            :                 }
    6472                 :            :         }
    6473                 :            : 
    6474                 :         24 :         ctx->new.id = newblob->id;
    6475                 :         24 :         spdk_blob_close(newblob, bs_clone_snapshot_origblob_cleanup, ctx);
    6476                 :         24 : }
    6477                 :            : 
    6478                 :            : /* END blob_cleanup */
    6479                 :            : 
    6480                 :            : /* START spdk_bs_create_snapshot */
    6481                 :            : 
    6482                 :            : static void
    6483                 :       1333 : bs_snapshot_swap_cluster_maps(struct spdk_blob *blob1, struct spdk_blob *blob2)
    6484                 :            : {
    6485                 :            :         uint64_t *cluster_temp;
    6486                 :            :         uint64_t num_allocated_clusters_temp;
    6487                 :            :         uint32_t *extent_page_temp;
    6488                 :            : 
    6489                 :       1333 :         cluster_temp = blob1->active.clusters;
    6490                 :       1333 :         blob1->active.clusters = blob2->active.clusters;
    6491                 :       1333 :         blob2->active.clusters = cluster_temp;
    6492                 :            : 
    6493                 :       1333 :         num_allocated_clusters_temp = blob1->active.num_allocated_clusters;
    6494                 :       1333 :         blob1->active.num_allocated_clusters = blob2->active.num_allocated_clusters;
    6495                 :       1333 :         blob2->active.num_allocated_clusters = num_allocated_clusters_temp;
    6496                 :            : 
    6497                 :       1333 :         extent_page_temp = blob1->active.extent_pages;
    6498                 :       1333 :         blob1->active.extent_pages = blob2->active.extent_pages;
    6499                 :       1333 :         blob2->active.extent_pages = extent_page_temp;
    6500                 :       1333 : }
    6501                 :            : 
    6502                 :            : /* Copies an internal xattr */
    6503                 :            : static int
    6504                 :        128 : bs_snapshot_copy_xattr(struct spdk_blob *toblob, struct spdk_blob *fromblob, const char *name)
    6505                 :            : {
    6506                 :        128 :         const void      *val = NULL;
    6507                 :        108 :         size_t          len;
    6508                 :            :         int             bserrno;
    6509                 :            : 
    6510                 :        128 :         bserrno = blob_get_xattr_value(fromblob, name, &val, &len, true);
    6511         [ -  + ]:        128 :         if (bserrno != 0) {
    6512                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " missing %s XATTR\n", fromblob->id, name);
    6513                 :          0 :                 return bserrno;
    6514                 :            :         }
    6515                 :            : 
    6516                 :        128 :         bserrno = blob_set_xattr(toblob, name, val, len, true);
    6517         [ -  + ]:        128 :         if (bserrno != 0) {
    6518                 :          0 :                 SPDK_ERRLOG("could not set %s XATTR on blob 0x%" PRIx64 "\n",
    6519                 :            :                             name, toblob->id);
    6520                 :          0 :                 return bserrno;
    6521                 :            :         }
    6522                 :        128 :         return 0;
    6523                 :            : }
    6524                 :            : 
    6525                 :            : static void
    6526                 :       1261 : bs_snapshot_origblob_sync_cpl(void *cb_arg, int bserrno)
    6527                 :            : {
    6528                 :       1261 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6529                 :       1261 :         struct spdk_blob *origblob = ctx->original.blob;
    6530                 :       1261 :         struct spdk_blob *newblob = ctx->new.blob;
    6531                 :            : 
    6532         [ +  + ]:       1261 :         if (bserrno != 0) {
    6533                 :         24 :                 bs_snapshot_swap_cluster_maps(newblob, origblob);
    6534         [ -  + ]:         24 :                 if (blob_is_esnap_clone(newblob)) {
    6535                 :          0 :                         bs_snapshot_copy_xattr(origblob, newblob, BLOB_EXTERNAL_SNAPSHOT_ID);
    6536                 :          0 :                         origblob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
    6537                 :            :                 }
    6538                 :         24 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6539                 :         24 :                 return;
    6540                 :            :         }
    6541                 :            : 
    6542                 :            :         /* Remove metadata descriptor SNAPSHOT_IN_PROGRESS */
    6543                 :       1237 :         bserrno = blob_remove_xattr(newblob, SNAPSHOT_IN_PROGRESS, true);
    6544         [ -  + ]:       1237 :         if (bserrno != 0) {
    6545                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6546                 :          0 :                 return;
    6547                 :            :         }
    6548                 :            : 
    6549                 :       1237 :         bs_blob_list_add(ctx->original.blob);
    6550                 :            : 
    6551                 :       1237 :         spdk_blob_set_read_only(newblob);
    6552                 :            : 
    6553                 :            :         /* sync snapshot metadata */
    6554                 :       1237 :         spdk_blob_sync_md(newblob, bs_clone_snapshot_origblob_cleanup, ctx);
    6555                 :            : }
    6556                 :            : 
    6557                 :            : static void
    6558                 :       1285 : bs_snapshot_newblob_sync_cpl(void *cb_arg, int bserrno)
    6559                 :            : {
    6560                 :       1285 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6561                 :       1285 :         struct spdk_blob *origblob = ctx->original.blob;
    6562                 :       1285 :         struct spdk_blob *newblob = ctx->new.blob;
    6563                 :            : 
    6564         [ +  + ]:       1285 :         if (bserrno != 0) {
    6565                 :            :                 /* return cluster map back to original */
    6566                 :         24 :                 bs_snapshot_swap_cluster_maps(newblob, origblob);
    6567                 :            : 
    6568                 :            :                 /* Newblob md sync failed. Valid clusters are only present in origblob.
    6569                 :            :                  * Since I/O is frozen on origblob, not changes to zeroed out cluster map should have occurred.
    6570                 :            :                  * Newblob needs to be reverted to thin_provisioned state at creation to properly close. */
    6571                 :         24 :                 blob_set_thin_provision(newblob);
    6572         [ -  + ]:         24 :                 assert(spdk_mem_all_zero(newblob->active.clusters,
    6573                 :            :                                          newblob->active.num_clusters * sizeof(*newblob->active.clusters)));
    6574         [ -  + ]:         24 :                 assert(spdk_mem_all_zero(newblob->active.extent_pages,
    6575                 :            :                                          newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages)));
    6576                 :            : 
    6577                 :         24 :                 bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
    6578                 :         24 :                 return;
    6579                 :            :         }
    6580                 :            : 
    6581                 :            :         /* Set internal xattr for snapshot id */
    6582                 :       1261 :         bserrno = blob_set_xattr(origblob, BLOB_SNAPSHOT, &newblob->id, sizeof(spdk_blob_id), true);
    6583         [ -  + ]:       1261 :         if (bserrno != 0) {
    6584                 :            :                 /* return cluster map back to original */
    6585                 :          0 :                 bs_snapshot_swap_cluster_maps(newblob, origblob);
    6586                 :          0 :                 blob_set_thin_provision(newblob);
    6587                 :          0 :                 bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
    6588                 :          0 :                 return;
    6589                 :            :         }
    6590                 :            : 
    6591                 :            :         /* Create new back_bs_dev for snapshot */
    6592                 :       1261 :         origblob->back_bs_dev = bs_create_blob_bs_dev(newblob);
    6593         [ -  + ]:       1261 :         if (origblob->back_bs_dev == NULL) {
    6594                 :            :                 /* return cluster map back to original */
    6595                 :          0 :                 bs_snapshot_swap_cluster_maps(newblob, origblob);
    6596                 :          0 :                 blob_set_thin_provision(newblob);
    6597                 :          0 :                 bs_clone_snapshot_newblob_cleanup(ctx, -EINVAL);
    6598                 :          0 :                 return;
    6599                 :            :         }
    6600                 :            : 
    6601                 :            :         /* Remove the xattr that references an external snapshot */
    6602         [ +  + ]:       1261 :         if (blob_is_esnap_clone(origblob)) {
    6603                 :         76 :                 origblob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
    6604                 :         76 :                 bserrno = blob_remove_xattr(origblob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
    6605         [ -  + ]:         76 :                 if (bserrno != 0) {
    6606         [ #  # ]:          0 :                         if (bserrno == -ENOENT) {
    6607                 :          0 :                                 SPDK_ERRLOG("blob 0x%" PRIx64 " has no " BLOB_EXTERNAL_SNAPSHOT_ID
    6608                 :            :                                             " xattr to remove\n", origblob->id);
    6609                 :          0 :                                 assert(false);
    6610                 :            :                         } else {
    6611                 :            :                                 /* return cluster map back to original */
    6612                 :          0 :                                 bs_snapshot_swap_cluster_maps(newblob, origblob);
    6613                 :          0 :                                 blob_set_thin_provision(newblob);
    6614                 :          0 :                                 bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
    6615                 :          0 :                                 return;
    6616                 :            :                         }
    6617                 :            :                 }
    6618                 :            :         }
    6619                 :            : 
    6620                 :       1261 :         bs_blob_list_remove(origblob);
    6621                 :       1261 :         origblob->parent_id = newblob->id;
    6622                 :            :         /* set clone blob as thin provisioned */
    6623                 :       1261 :         blob_set_thin_provision(origblob);
    6624                 :            : 
    6625                 :       1261 :         bs_blob_list_add(newblob);
    6626                 :            : 
    6627                 :            :         /* sync clone metadata */
    6628                 :       1261 :         spdk_blob_sync_md(origblob, bs_snapshot_origblob_sync_cpl, ctx);
    6629                 :            : }
    6630                 :            : 
    6631                 :            : static void
    6632                 :       1285 : bs_snapshot_freeze_cpl(void *cb_arg, int rc)
    6633                 :            : {
    6634                 :       1285 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6635                 :       1285 :         struct spdk_blob *origblob = ctx->original.blob;
    6636                 :       1285 :         struct spdk_blob *newblob = ctx->new.blob;
    6637                 :            :         int bserrno;
    6638                 :            : 
    6639         [ -  + ]:       1285 :         if (rc != 0) {
    6640                 :          0 :                 bs_clone_snapshot_newblob_cleanup(ctx, rc);
    6641                 :          0 :                 return;
    6642                 :            :         }
    6643                 :            : 
    6644                 :       1285 :         ctx->frozen = true;
    6645                 :            : 
    6646         [ +  + ]:       1285 :         if (blob_is_esnap_clone(origblob)) {
    6647                 :            :                 /* Clean up any channels associated with the original blob id because future IO will
    6648                 :            :                  * perform IO using the snapshot blob_id.
    6649                 :            :                  */
    6650                 :         76 :                 blob_esnap_destroy_bs_dev_channels(origblob, false, NULL, NULL);
    6651                 :            :         }
    6652         [ +  - ]:       1285 :         if (newblob->back_bs_dev) {
    6653                 :       1285 :                 blob_back_bs_destroy(newblob);
    6654                 :            :         }
    6655                 :            :         /* set new back_bs_dev for snapshot */
    6656                 :       1285 :         newblob->back_bs_dev = origblob->back_bs_dev;
    6657                 :            :         /* Set invalid flags from origblob */
    6658                 :       1285 :         newblob->invalid_flags = origblob->invalid_flags;
    6659                 :            : 
    6660                 :            :         /* inherit parent from original blob if set */
    6661                 :       1285 :         newblob->parent_id = origblob->parent_id;
    6662      [ +  +  + ]:       1285 :         switch (origblob->parent_id) {
    6663                 :         76 :         case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
    6664                 :         76 :                 bserrno = bs_snapshot_copy_xattr(newblob, origblob, BLOB_EXTERNAL_SNAPSHOT_ID);
    6665         [ -  + ]:         76 :                 if (bserrno != 0) {
    6666                 :          0 :                         bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
    6667                 :          0 :                         return;
    6668                 :            :                 }
    6669                 :         76 :                 break;
    6670                 :        892 :         case SPDK_BLOBID_INVALID:
    6671                 :        892 :                 break;
    6672                 :        317 :         default:
    6673                 :            :                 /* Set internal xattr for snapshot id */
    6674                 :        317 :                 bserrno = blob_set_xattr(newblob, BLOB_SNAPSHOT,
    6675                 :        317 :                                          &origblob->parent_id, sizeof(spdk_blob_id), true);
    6676         [ -  + ]:        317 :                 if (bserrno != 0) {
    6677                 :          0 :                         bs_clone_snapshot_newblob_cleanup(ctx, bserrno);
    6678                 :          0 :                         return;
    6679                 :            :                 }
    6680                 :            :         }
    6681                 :            : 
    6682                 :            :         /* swap cluster maps */
    6683                 :       1285 :         bs_snapshot_swap_cluster_maps(newblob, origblob);
    6684                 :            : 
    6685                 :            :         /* Set the clear method on the new blob to match the original. */
    6686                 :       1285 :         blob_set_clear_method(newblob, origblob->clear_method);
    6687                 :            : 
    6688                 :            :         /* sync snapshot metadata */
    6689                 :       1285 :         spdk_blob_sync_md(newblob, bs_snapshot_newblob_sync_cpl, ctx);
    6690                 :            : }
    6691                 :            : 
    6692                 :            : static void
    6693                 :       1309 : bs_snapshot_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    6694                 :            : {
    6695                 :       1309 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6696                 :       1309 :         struct spdk_blob *origblob = ctx->original.blob;
    6697                 :       1309 :         struct spdk_blob *newblob = _blob;
    6698                 :            : 
    6699         [ +  + ]:       1309 :         if (bserrno != 0) {
    6700                 :         24 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6701                 :         24 :                 return;
    6702                 :            :         }
    6703                 :            : 
    6704                 :       1285 :         ctx->new.blob = newblob;
    6705         [ -  + ]:       1285 :         assert(spdk_blob_is_thin_provisioned(newblob));
    6706         [ -  + ]:       1285 :         assert(spdk_mem_all_zero(newblob->active.clusters,
    6707                 :            :                                  newblob->active.num_clusters * sizeof(*newblob->active.clusters)));
    6708         [ -  + ]:       1285 :         assert(spdk_mem_all_zero(newblob->active.extent_pages,
    6709                 :            :                                  newblob->active.num_extent_pages * sizeof(*newblob->active.extent_pages)));
    6710                 :            : 
    6711                 :       1285 :         blob_freeze_io(origblob, bs_snapshot_freeze_cpl, ctx);
    6712                 :            : }
    6713                 :            : 
    6714                 :            : static void
    6715                 :       1333 : bs_snapshot_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno)
    6716                 :            : {
    6717                 :       1333 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6718                 :       1333 :         struct spdk_blob *origblob = ctx->original.blob;
    6719                 :            : 
    6720         [ +  + ]:       1333 :         if (bserrno != 0) {
    6721                 :         24 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6722                 :         24 :                 return;
    6723                 :            :         }
    6724                 :            : 
    6725                 :       1309 :         ctx->new.id = blobid;
    6726                 :       1309 :         ctx->cpl.u.blobid.blobid = blobid;
    6727                 :            : 
    6728                 :       1309 :         spdk_bs_open_blob(origblob->bs, ctx->new.id, bs_snapshot_newblob_open_cpl, ctx);
    6729                 :            : }
    6730                 :            : 
    6731                 :            : 
    6732                 :            : static void
    6733                 :       1333 : bs_xattr_snapshot(void *arg, const char *name,
    6734                 :            :                   const void **value, size_t *value_len)
    6735                 :            : {
    6736   [ -  +  -  + ]:       1333 :         assert(strncmp(name, SNAPSHOT_IN_PROGRESS, sizeof(SNAPSHOT_IN_PROGRESS)) == 0);
    6737                 :            : 
    6738                 :       1333 :         struct spdk_blob *blob = (struct spdk_blob *)arg;
    6739                 :       1333 :         *value = &blob->id;
    6740                 :       1333 :         *value_len = sizeof(blob->id);
    6741                 :       1333 : }
    6742                 :            : 
    6743                 :            : static void
    6744                 :       1394 : bs_snapshot_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    6745                 :            : {
    6746                 :       1394 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6747                 :       1163 :         struct spdk_blob_opts opts;
    6748                 :       1163 :         struct spdk_blob_xattr_opts internal_xattrs;
    6749                 :       1394 :         char *xattrs_names[] = { SNAPSHOT_IN_PROGRESS };
    6750                 :            : 
    6751         [ +  + ]:       1394 :         if (bserrno != 0) {
    6752                 :         36 :                 bs_clone_snapshot_cleanup_finish(ctx, bserrno);
    6753                 :         40 :                 return;
    6754                 :            :         }
    6755                 :            : 
    6756                 :       1358 :         ctx->original.blob = _blob;
    6757                 :            : 
    6758   [ +  +  +  +  :       1358 :         if (_blob->data_ro || _blob->md_ro) {
             -  +  -  + ]
    6759   [ -  +  -  + ]:         25 :                 SPDK_DEBUGLOG(blob, "Cannot create snapshot from read only blob with id 0x%"
    6760                 :            :                               PRIx64 "\n", _blob->id);
    6761                 :         25 :                 ctx->bserrno = -EINVAL;
    6762                 :         25 :                 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
    6763                 :         25 :                 return;
    6764                 :            :         }
    6765                 :            : 
    6766   [ -  +  -  + ]:       1333 :         if (_blob->locked_operation_in_progress) {
    6767   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "Cannot create snapshot - another operation in progress\n");
    6768                 :          0 :                 ctx->bserrno = -EBUSY;
    6769                 :          0 :                 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
    6770                 :          0 :                 return;
    6771                 :            :         }
    6772                 :            : 
    6773                 :       1333 :         _blob->locked_operation_in_progress = true;
    6774                 :            : 
    6775                 :       1333 :         spdk_blob_opts_init(&opts, sizeof(opts));
    6776                 :       1333 :         blob_xattrs_init(&internal_xattrs);
    6777                 :            : 
    6778                 :            :         /* Change the size of new blob to the same as in original blob,
    6779                 :            :          * but do not allocate clusters */
    6780                 :       1333 :         opts.thin_provision = true;
    6781                 :       1333 :         opts.num_clusters = spdk_blob_get_num_clusters(_blob);
    6782         [ -  + ]:       1333 :         opts.use_extent_table = _blob->use_extent_table;
    6783                 :            : 
    6784                 :            :         /* If there are any xattrs specified for snapshot, set them now */
    6785         [ +  + ]:       1333 :         if (ctx->xattrs) {
    6786   [ -  +  -  + ]:         61 :                 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs));
    6787                 :            :         }
    6788                 :            :         /* Set internal xattr SNAPSHOT_IN_PROGRESS */
    6789                 :       1333 :         internal_xattrs.count = 1;
    6790                 :       1333 :         internal_xattrs.ctx = _blob;
    6791                 :       1333 :         internal_xattrs.names = xattrs_names;
    6792                 :       1333 :         internal_xattrs.get_value = bs_xattr_snapshot;
    6793                 :            : 
    6794                 :       1333 :         bs_create_blob(_blob->bs, &opts, &internal_xattrs,
    6795                 :            :                        bs_snapshot_newblob_create_cpl, ctx);
    6796                 :            : }
    6797                 :            : 
    6798                 :            : void
    6799                 :       1394 : spdk_bs_create_snapshot(struct spdk_blob_store *bs, spdk_blob_id blobid,
    6800                 :            :                         const struct spdk_blob_xattr_opts *snapshot_xattrs,
    6801                 :            :                         spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6802                 :            : {
    6803                 :       1394 :         struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
    6804                 :            : 
    6805         [ -  + ]:       1394 :         if (!ctx) {
    6806                 :          0 :                 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM);
    6807                 :          0 :                 return;
    6808                 :            :         }
    6809                 :       1394 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
    6810                 :       1394 :         ctx->cpl.u.blobid.cb_fn = cb_fn;
    6811                 :       1394 :         ctx->cpl.u.blobid.cb_arg = cb_arg;
    6812                 :       1394 :         ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID;
    6813                 :       1394 :         ctx->bserrno = 0;
    6814                 :       1394 :         ctx->frozen = false;
    6815                 :       1394 :         ctx->original.id = blobid;
    6816                 :       1394 :         ctx->xattrs = snapshot_xattrs;
    6817                 :            : 
    6818                 :       1394 :         spdk_bs_open_blob(bs, ctx->original.id, bs_snapshot_origblob_open_cpl, ctx);
    6819                 :            : }
    6820                 :            : /* END spdk_bs_create_snapshot */
    6821                 :            : 
    6822                 :            : /* START spdk_bs_create_clone */
    6823                 :            : 
    6824                 :            : static void
    6825                 :        310 : bs_xattr_clone(void *arg, const char *name,
    6826                 :            :                const void **value, size_t *value_len)
    6827                 :            : {
    6828   [ -  +  -  + ]:        310 :         assert(strncmp(name, BLOB_SNAPSHOT, sizeof(BLOB_SNAPSHOT)) == 0);
    6829                 :            : 
    6830                 :        310 :         struct spdk_blob *blob = (struct spdk_blob *)arg;
    6831                 :        310 :         *value = &blob->id;
    6832                 :        310 :         *value_len = sizeof(blob->id);
    6833                 :        310 : }
    6834                 :            : 
    6835                 :            : static void
    6836                 :        310 : bs_clone_newblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    6837                 :            : {
    6838                 :        310 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6839                 :        310 :         struct spdk_blob *clone = _blob;
    6840                 :            : 
    6841                 :        310 :         ctx->new.blob = clone;
    6842                 :        310 :         bs_blob_list_add(clone);
    6843                 :            : 
    6844                 :        310 :         spdk_blob_close(clone, bs_clone_snapshot_origblob_cleanup, ctx);
    6845                 :        310 : }
    6846                 :            : 
    6847                 :            : static void
    6848                 :        310 : bs_clone_newblob_create_cpl(void *cb_arg, spdk_blob_id blobid, int bserrno)
    6849                 :            : {
    6850                 :        310 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6851                 :            : 
    6852                 :        310 :         ctx->cpl.u.blobid.blobid = blobid;
    6853                 :        310 :         spdk_bs_open_blob(ctx->original.blob->bs, blobid, bs_clone_newblob_open_cpl, ctx);
    6854                 :        310 : }
    6855                 :            : 
    6856                 :            : static void
    6857                 :        336 : bs_clone_origblob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    6858                 :            : {
    6859                 :        336 :         struct spdk_clone_snapshot_ctx  *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6860                 :        279 :         struct spdk_blob_opts           opts;
    6861                 :        279 :         struct spdk_blob_xattr_opts internal_xattrs;
    6862                 :        336 :         char *xattr_names[] = { BLOB_SNAPSHOT };
    6863                 :            : 
    6864         [ -  + ]:        336 :         if (bserrno != 0) {
    6865                 :          0 :                 bs_clone_snapshot_cleanup_finish(ctx, bserrno);
    6866                 :          4 :                 return;
    6867                 :            :         }
    6868                 :            : 
    6869                 :        336 :         ctx->original.blob = _blob;
    6870         [ -  + ]:        336 :         ctx->original.md_ro = _blob->md_ro;
    6871                 :            : 
    6872   [ +  +  +  +  :        336 :         if (!_blob->data_ro || !_blob->md_ro) {
             -  +  -  + ]
    6873   [ -  +  -  + ]:         26 :                 SPDK_DEBUGLOG(blob, "Clone not from read-only blob\n");
    6874                 :         26 :                 ctx->bserrno = -EINVAL;
    6875                 :         26 :                 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
    6876                 :         26 :                 return;
    6877                 :            :         }
    6878                 :            : 
    6879   [ -  +  -  + ]:        310 :         if (_blob->locked_operation_in_progress) {
    6880   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "Cannot create clone - another operation in progress\n");
    6881                 :          0 :                 ctx->bserrno = -EBUSY;
    6882                 :          0 :                 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
    6883                 :          0 :                 return;
    6884                 :            :         }
    6885                 :            : 
    6886                 :        310 :         _blob->locked_operation_in_progress = true;
    6887                 :            : 
    6888                 :        310 :         spdk_blob_opts_init(&opts, sizeof(opts));
    6889                 :        310 :         blob_xattrs_init(&internal_xattrs);
    6890                 :            : 
    6891                 :        310 :         opts.thin_provision = true;
    6892                 :        310 :         opts.num_clusters = spdk_blob_get_num_clusters(_blob);
    6893         [ -  + ]:        310 :         opts.use_extent_table = _blob->use_extent_table;
    6894         [ +  + ]:        310 :         if (ctx->xattrs) {
    6895   [ -  +  -  + ]:         46 :                 memcpy(&opts.xattrs, ctx->xattrs, sizeof(*ctx->xattrs));
    6896                 :            :         }
    6897                 :            : 
    6898                 :            :         /* Set internal xattr BLOB_SNAPSHOT */
    6899                 :        310 :         internal_xattrs.count = 1;
    6900                 :        310 :         internal_xattrs.ctx = _blob;
    6901                 :        310 :         internal_xattrs.names = xattr_names;
    6902                 :        310 :         internal_xattrs.get_value = bs_xattr_clone;
    6903                 :            : 
    6904                 :        310 :         bs_create_blob(_blob->bs, &opts, &internal_xattrs,
    6905                 :            :                        bs_clone_newblob_create_cpl, ctx);
    6906                 :            : }
    6907                 :            : 
    6908                 :            : void
    6909                 :        336 : spdk_bs_create_clone(struct spdk_blob_store *bs, spdk_blob_id blobid,
    6910                 :            :                      const struct spdk_blob_xattr_opts *clone_xattrs,
    6911                 :            :                      spdk_blob_op_with_id_complete cb_fn, void *cb_arg)
    6912                 :            : {
    6913                 :        336 :         struct spdk_clone_snapshot_ctx  *ctx = calloc(1, sizeof(*ctx));
    6914                 :            : 
    6915         [ -  + ]:        336 :         if (!ctx) {
    6916                 :          0 :                 cb_fn(cb_arg, SPDK_BLOBID_INVALID, -ENOMEM);
    6917                 :          0 :                 return;
    6918                 :            :         }
    6919                 :            : 
    6920                 :        336 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOBID;
    6921                 :        336 :         ctx->cpl.u.blobid.cb_fn = cb_fn;
    6922                 :        336 :         ctx->cpl.u.blobid.cb_arg = cb_arg;
    6923                 :        336 :         ctx->cpl.u.blobid.blobid = SPDK_BLOBID_INVALID;
    6924                 :        336 :         ctx->bserrno = 0;
    6925                 :        336 :         ctx->xattrs = clone_xattrs;
    6926                 :        336 :         ctx->original.id = blobid;
    6927                 :            : 
    6928                 :        336 :         spdk_bs_open_blob(bs, ctx->original.id, bs_clone_origblob_open_cpl, ctx);
    6929                 :            : }
    6930                 :            : 
    6931                 :            : /* END spdk_bs_create_clone */
    6932                 :            : 
    6933                 :            : /* START spdk_bs_inflate_blob */
    6934                 :            : 
    6935                 :            : static void
    6936                 :         73 : bs_inflate_blob_set_parent_cpl(void *cb_arg, struct spdk_blob *_parent, int bserrno)
    6937                 :            : {
    6938                 :         73 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    6939                 :         73 :         struct spdk_blob *_blob = ctx->original.blob;
    6940                 :            : 
    6941         [ -  + ]:         73 :         if (bserrno != 0) {
    6942                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6943                 :          0 :                 return;
    6944                 :            :         }
    6945                 :            : 
    6946                 :            :         /* Temporarily override md_ro flag for MD modification */
    6947                 :         73 :         _blob->md_ro = false;
    6948                 :            : 
    6949                 :         73 :         bserrno = blob_set_xattr(_blob, BLOB_SNAPSHOT, &_parent->id, sizeof(spdk_blob_id), true);
    6950         [ -  + ]:         73 :         if (bserrno != 0) {
    6951                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    6952                 :          0 :                 return;
    6953                 :            :         }
    6954                 :            : 
    6955         [ -  + ]:         73 :         assert(_parent != NULL);
    6956                 :            : 
    6957                 :         73 :         bs_blob_list_remove(_blob);
    6958                 :         73 :         _blob->parent_id = _parent->id;
    6959                 :            : 
    6960                 :         73 :         blob_back_bs_destroy(_blob);
    6961                 :         73 :         _blob->back_bs_dev = bs_create_blob_bs_dev(_parent);
    6962                 :         73 :         bs_blob_list_add(_blob);
    6963                 :            : 
    6964                 :         73 :         spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx);
    6965                 :            : }
    6966                 :            : 
    6967                 :            : static void
    6968                 :        343 : bs_inflate_blob_done(struct spdk_clone_snapshot_ctx *ctx)
    6969                 :            : {
    6970                 :        343 :         struct spdk_blob *_blob = ctx->original.blob;
    6971                 :            :         struct spdk_blob *_parent;
    6972                 :            : 
    6973   [ +  +  +  + ]:        343 :         if (ctx->allocate_all) {
    6974                 :            :                 /* remove thin provisioning */
    6975                 :        197 :                 bs_blob_list_remove(_blob);
    6976         [ +  + ]:        197 :                 if (_blob->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
    6977                 :         48 :                         blob_remove_xattr(_blob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
    6978                 :         48 :                         _blob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
    6979                 :            :                 } else {
    6980                 :        149 :                         blob_remove_xattr(_blob, BLOB_SNAPSHOT, true);
    6981                 :            :                 }
    6982                 :        197 :                 _blob->invalid_flags = _blob->invalid_flags & ~SPDK_BLOB_THIN_PROV;
    6983                 :        197 :                 blob_back_bs_destroy(_blob);
    6984                 :        197 :                 _blob->parent_id = SPDK_BLOBID_INVALID;
    6985                 :            :         } else {
    6986                 :            :                 /* For now, esnap clones always have allocate_all set. */
    6987         [ -  + ]:        146 :                 assert(!blob_is_esnap_clone(_blob));
    6988                 :            : 
    6989                 :        146 :                 _parent = ((struct spdk_blob_bs_dev *)(_blob->back_bs_dev))->blob;
    6990         [ +  + ]:        146 :                 if (_parent->parent_id != SPDK_BLOBID_INVALID) {
    6991                 :            :                         /* We must change the parent of the inflated blob */
    6992                 :         73 :                         spdk_bs_open_blob(_blob->bs, _parent->parent_id,
    6993                 :            :                                           bs_inflate_blob_set_parent_cpl, ctx);
    6994                 :         73 :                         return;
    6995                 :            :                 }
    6996                 :            : 
    6997                 :         73 :                 bs_blob_list_remove(_blob);
    6998                 :         73 :                 _blob->parent_id = SPDK_BLOBID_INVALID;
    6999                 :         73 :                 blob_back_bs_destroy(_blob);
    7000                 :         73 :                 _blob->back_bs_dev = bs_create_zeroes_dev();
    7001                 :            :         }
    7002                 :            : 
    7003                 :            :         /* Temporarily override md_ro flag for MD modification */
    7004                 :        270 :         _blob->md_ro = false;
    7005                 :        270 :         blob_remove_xattr(_blob, BLOB_SNAPSHOT, true);
    7006                 :        270 :         _blob->state = SPDK_BLOB_STATE_DIRTY;
    7007                 :            : 
    7008                 :        270 :         spdk_blob_sync_md(_blob, bs_clone_snapshot_origblob_cleanup, ctx);
    7009                 :            : }
    7010                 :            : 
    7011                 :            : /* Check if cluster needs allocation */
    7012                 :            : static inline bool
    7013                 :       7274 : bs_cluster_needs_allocation(struct spdk_blob *blob, uint64_t cluster, bool allocate_all)
    7014                 :            : {
    7015                 :            :         struct spdk_blob_bs_dev *b;
    7016                 :            : 
    7017         [ -  + ]:       7274 :         assert(blob != NULL);
    7018                 :            : 
    7019         [ +  + ]:       7274 :         if (blob->active.clusters[cluster] != 0) {
    7020                 :            :                 /* Cluster is already allocated */
    7021                 :        202 :                 return false;
    7022                 :            :         }
    7023                 :            : 
    7024         [ +  + ]:       7072 :         if (blob->parent_id == SPDK_BLOBID_INVALID) {
    7025                 :            :                 /* Blob have no parent blob */
    7026                 :        480 :                 return allocate_all;
    7027                 :            :         }
    7028                 :            : 
    7029         [ +  + ]:       6592 :         if (blob->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
    7030                 :        384 :                 return true;
    7031                 :            :         }
    7032                 :            : 
    7033                 :       6208 :         b = (struct spdk_blob_bs_dev *)blob->back_bs_dev;
    7034   [ +  +  +  + ]:       6208 :         return (allocate_all || b->blob->active.clusters[cluster] != 0);
    7035                 :            : }
    7036                 :            : 
    7037                 :            : static void
    7038                 :       3083 : bs_inflate_blob_touch_next(void *cb_arg, int bserrno)
    7039                 :            : {
    7040                 :       3083 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    7041                 :       3083 :         struct spdk_blob *_blob = ctx->original.blob;
    7042                 :       2551 :         struct spdk_bs_cpl cpl;
    7043                 :            :         spdk_bs_user_op_t *op;
    7044                 :            :         uint64_t offset;
    7045                 :            : 
    7046         [ -  + ]:       3083 :         if (bserrno != 0) {
    7047                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, bserrno);
    7048                 :          0 :                 return;
    7049                 :            :         }
    7050                 :            : 
    7051         [ +  + ]:       3980 :         for (; ctx->cluster < _blob->active.num_clusters; ctx->cluster++) {
    7052   [ +  +  +  + ]:       3637 :                 if (bs_cluster_needs_allocation(_blob, ctx->cluster, ctx->allocate_all)) {
    7053                 :       2740 :                         break;
    7054                 :            :                 }
    7055                 :            :         }
    7056                 :            : 
    7057         [ +  + ]:       3083 :         if (ctx->cluster < _blob->active.num_clusters) {
    7058                 :       2740 :                 offset = bs_cluster_to_lba(_blob->bs, ctx->cluster);
    7059                 :            : 
    7060                 :            :                 /* We may safely increment a cluster before copying */
    7061                 :       2740 :                 ctx->cluster++;
    7062                 :            : 
    7063                 :            :                 /* Use a dummy 0B read as a context for cluster copy */
    7064                 :       2740 :                 cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    7065                 :       2740 :                 cpl.u.blob_basic.cb_fn = bs_inflate_blob_touch_next;
    7066                 :       2740 :                 cpl.u.blob_basic.cb_arg = ctx;
    7067                 :            : 
    7068                 :       2740 :                 op = bs_user_op_alloc(ctx->channel, &cpl, SPDK_BLOB_READ, _blob,
    7069                 :            :                                       NULL, 0, offset, 0);
    7070         [ -  + ]:       2740 :                 if (!op) {
    7071                 :          0 :                         bs_clone_snapshot_origblob_cleanup(ctx, -ENOMEM);
    7072                 :          0 :                         return;
    7073                 :            :                 }
    7074                 :            : 
    7075                 :       2740 :                 bs_allocate_and_copy_cluster(_blob, ctx->channel, offset, op);
    7076                 :            :         } else {
    7077                 :        343 :                 bs_inflate_blob_done(ctx);
    7078                 :            :         }
    7079                 :            : }
    7080                 :            : 
    7081                 :            : static void
    7082                 :        368 : bs_inflate_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    7083                 :            : {
    7084                 :        368 :         struct spdk_clone_snapshot_ctx *ctx = (struct spdk_clone_snapshot_ctx *)cb_arg;
    7085                 :            :         uint64_t clusters_needed;
    7086                 :            :         uint64_t i;
    7087                 :            : 
    7088         [ -  + ]:        368 :         if (bserrno != 0) {
    7089                 :          0 :                 bs_clone_snapshot_cleanup_finish(ctx, bserrno);
    7090                 :          0 :                 return;
    7091                 :            :         }
    7092                 :            : 
    7093                 :        368 :         ctx->original.blob = _blob;
    7094         [ -  + ]:        368 :         ctx->original.md_ro = _blob->md_ro;
    7095                 :            : 
    7096   [ -  +  -  + ]:        368 :         if (_blob->locked_operation_in_progress) {
    7097   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "Cannot inflate blob - another operation in progress\n");
    7098                 :          0 :                 ctx->bserrno = -EBUSY;
    7099                 :          0 :                 spdk_blob_close(_blob, bs_clone_snapshot_cleanup_finish, ctx);
    7100                 :          0 :                 return;
    7101                 :            :         }
    7102                 :            : 
    7103                 :        368 :         _blob->locked_operation_in_progress = true;
    7104                 :            : 
    7105      [ +  +  + ]:        368 :         switch (_blob->parent_id) {
    7106                 :         49 :         case SPDK_BLOBID_INVALID:
    7107   [ +  +  +  + ]:         49 :                 if (!ctx->allocate_all) {
    7108                 :            :                         /* This blob has no parent, so we cannot decouple it. */
    7109                 :         25 :                         SPDK_ERRLOG("Cannot decouple parent of blob with no parent.\n");
    7110                 :         25 :                         bs_clone_snapshot_origblob_cleanup(ctx, -EINVAL);
    7111                 :         25 :                         return;
    7112                 :            :                 }
    7113                 :         24 :                 break;
    7114                 :         48 :         case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
    7115                 :            :                 /*
    7116                 :            :                  * It would be better to rely on back_bs_dev->is_zeroes(), to determine which
    7117                 :            :                  * clusters require allocation. Until there is a blobstore consumer that
    7118                 :            :                  * uses esnaps with an spdk_bs_dev that implements a useful is_zeroes() it is not
    7119                 :            :                  * worth the effort.
    7120                 :            :                  */
    7121                 :         48 :                 ctx->allocate_all = true;
    7122                 :         48 :                 break;
    7123                 :        271 :         default:
    7124                 :        271 :                 break;
    7125                 :            :         }
    7126                 :            : 
    7127         [ -  + ]:        343 :         if (spdk_blob_is_thin_provisioned(_blob) == false) {
    7128                 :            :                 /* This is not thin provisioned blob. No need to inflate. */
    7129                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, 0);
    7130                 :          0 :                 return;
    7131                 :            :         }
    7132                 :            : 
    7133                 :            :         /* Do two passes - one to verify that we can obtain enough clusters
    7134                 :            :          * and another to actually claim them.
    7135                 :            :          */
    7136                 :        343 :         clusters_needed = 0;
    7137         [ +  + ]:       3980 :         for (i = 0; i < _blob->active.num_clusters; i++) {
    7138   [ +  +  +  + ]:       3637 :                 if (bs_cluster_needs_allocation(_blob, i, ctx->allocate_all)) {
    7139                 :       2740 :                         clusters_needed++;
    7140                 :            :                 }
    7141                 :            :         }
    7142                 :            : 
    7143         [ -  + ]:        343 :         if (clusters_needed > _blob->bs->num_free_clusters) {
    7144                 :            :                 /* Not enough free clusters. Cannot satisfy the request. */
    7145                 :          0 :                 bs_clone_snapshot_origblob_cleanup(ctx, -ENOSPC);
    7146                 :          0 :                 return;
    7147                 :            :         }
    7148                 :            : 
    7149                 :        343 :         ctx->cluster = 0;
    7150                 :        343 :         bs_inflate_blob_touch_next(ctx, 0);
    7151                 :            : }
    7152                 :            : 
    7153                 :            : static void
    7154                 :        368 : bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
    7155                 :            :                 spdk_blob_id blobid, bool allocate_all, spdk_blob_op_complete cb_fn, void *cb_arg)
    7156                 :            : {
    7157                 :        368 :         struct spdk_clone_snapshot_ctx *ctx = calloc(1, sizeof(*ctx));
    7158                 :            : 
    7159         [ -  + ]:        368 :         if (!ctx) {
    7160                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    7161                 :          0 :                 return;
    7162                 :            :         }
    7163                 :        368 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    7164                 :        368 :         ctx->cpl.u.bs_basic.cb_fn = cb_fn;
    7165                 :        368 :         ctx->cpl.u.bs_basic.cb_arg = cb_arg;
    7166                 :        368 :         ctx->bserrno = 0;
    7167                 :        368 :         ctx->original.id = blobid;
    7168                 :        368 :         ctx->channel = channel;
    7169                 :        368 :         ctx->allocate_all = allocate_all;
    7170                 :            : 
    7171                 :        368 :         spdk_bs_open_blob(bs, ctx->original.id, bs_inflate_blob_open_cpl, ctx);
    7172                 :            : }
    7173                 :            : 
    7174                 :            : void
    7175                 :        173 : spdk_bs_inflate_blob(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
    7176                 :            :                      spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg)
    7177                 :            : {
    7178                 :        173 :         bs_inflate_blob(bs, channel, blobid, true, cb_fn, cb_arg);
    7179                 :        173 : }
    7180                 :            : 
    7181                 :            : void
    7182                 :        195 : spdk_bs_blob_decouple_parent(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
    7183                 :            :                              spdk_blob_id blobid, spdk_blob_op_complete cb_fn, void *cb_arg)
    7184                 :            : {
    7185                 :        195 :         bs_inflate_blob(bs, channel, blobid, false, cb_fn, cb_arg);
    7186                 :        195 : }
    7187                 :            : /* END spdk_bs_inflate_blob */
    7188                 :            : 
    7189                 :            : /* START spdk_bs_blob_shallow_copy */
    7190                 :            : 
    7191                 :            : struct shallow_copy_ctx {
    7192                 :            :         struct spdk_bs_cpl cpl;
    7193                 :            :         int bserrno;
    7194                 :            : 
    7195                 :            :         /* Blob source for copy */
    7196                 :            :         struct spdk_blob_store *bs;
    7197                 :            :         spdk_blob_id blobid;
    7198                 :            :         struct spdk_blob *blob;
    7199                 :            :         struct spdk_io_channel *blob_channel;
    7200                 :            : 
    7201                 :            :         /* Destination device for copy */
    7202                 :            :         struct spdk_bs_dev *ext_dev;
    7203                 :            :         struct spdk_io_channel *ext_channel;
    7204                 :            : 
    7205                 :            :         /* Current cluster for copy operation */
    7206                 :            :         uint64_t cluster;
    7207                 :            : 
    7208                 :            :         /* Buffer for blob reading */
    7209                 :            :         uint8_t *read_buff;
    7210                 :            : 
    7211                 :            :         /* Struct for external device writing */
    7212                 :            :         struct spdk_bs_dev_cb_args ext_args;
    7213                 :            : 
    7214                 :            :         /* Actual number of copied clusters */
    7215                 :            :         uint64_t copied_clusters_count;
    7216                 :            : 
    7217                 :            :         /* Status callback for updates about the ongoing operation */
    7218                 :            :         spdk_blob_shallow_copy_status status_cb;
    7219                 :            : 
    7220                 :            :         /* Argument passed to function status_cb */
    7221                 :            :         void *status_cb_arg;
    7222                 :            : };
    7223                 :            : 
    7224                 :            : static void
    7225                 :         97 : bs_shallow_copy_cleanup_finish(void *cb_arg, int bserrno)
    7226                 :            : {
    7227                 :         97 :         struct shallow_copy_ctx *ctx = cb_arg;
    7228                 :         97 :         struct spdk_bs_cpl *cpl = &ctx->cpl;
    7229                 :            : 
    7230         [ -  + ]:         97 :         if (bserrno != 0) {
    7231                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, cleanup error %d\n", ctx->blob->id, bserrno);
    7232                 :          0 :                 ctx->bserrno = bserrno;
    7233                 :            :         }
    7234                 :            : 
    7235                 :         97 :         ctx->ext_dev->destroy_channel(ctx->ext_dev, ctx->ext_channel);
    7236                 :         97 :         spdk_free(ctx->read_buff);
    7237                 :            : 
    7238                 :         97 :         cpl->u.blob_basic.cb_fn(cpl->u.blob_basic.cb_arg, ctx->bserrno);
    7239                 :            : 
    7240                 :         97 :         free(ctx);
    7241                 :         97 : }
    7242                 :            : 
    7243                 :            : static void
    7244                 :         50 : bs_shallow_copy_bdev_write_cpl(struct spdk_io_channel *channel, void *cb_arg, int bserrno)
    7245                 :            : {
    7246                 :         50 :         struct shallow_copy_ctx *ctx = cb_arg;
    7247                 :         50 :         struct spdk_blob *_blob = ctx->blob;
    7248                 :            : 
    7249         [ -  + ]:         50 :         if (bserrno != 0) {
    7250                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, ext dev write error %d\n", ctx->blob->id, bserrno);
    7251                 :          0 :                 ctx->bserrno = bserrno;
    7252                 :          0 :                 _blob->locked_operation_in_progress = false;
    7253                 :          0 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7254                 :          0 :                 return;
    7255                 :            :         }
    7256                 :            : 
    7257                 :         50 :         ctx->cluster++;
    7258         [ +  - ]:         50 :         if (ctx->status_cb) {
    7259                 :         50 :                 ctx->copied_clusters_count++;
    7260                 :         50 :                 ctx->status_cb(ctx->copied_clusters_count, ctx->status_cb_arg);
    7261                 :            :         }
    7262                 :            : 
    7263                 :         50 :         bs_shallow_copy_cluster_find_next(ctx);
    7264                 :            : }
    7265                 :            : 
    7266                 :            : static void
    7267                 :         50 : bs_shallow_copy_blob_read_cpl(void *cb_arg, int bserrno)
    7268                 :            : {
    7269                 :         50 :         struct shallow_copy_ctx *ctx = cb_arg;
    7270                 :         50 :         struct spdk_bs_dev *ext_dev = ctx->ext_dev;
    7271                 :         50 :         struct spdk_blob *_blob = ctx->blob;
    7272                 :            : 
    7273         [ -  + ]:         50 :         if (bserrno != 0) {
    7274                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, blob read error %d\n", ctx->blob->id, bserrno);
    7275                 :          0 :                 ctx->bserrno = bserrno;
    7276                 :          0 :                 _blob->locked_operation_in_progress = false;
    7277                 :          0 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7278                 :          0 :                 return;
    7279                 :            :         }
    7280                 :            : 
    7281                 :         50 :         ctx->ext_args.channel = ctx->ext_channel;
    7282                 :         50 :         ctx->ext_args.cb_fn = bs_shallow_copy_bdev_write_cpl;
    7283                 :         50 :         ctx->ext_args.cb_arg = ctx;
    7284                 :            : 
    7285                 :         84 :         ext_dev->write(ext_dev, ctx->ext_channel, ctx->read_buff,
    7286                 :         50 :                        bs_cluster_to_lba(_blob->bs, ctx->cluster),
    7287                 :         50 :                        bs_dev_byte_to_lba(_blob->bs->dev, _blob->bs->cluster_sz),
    7288                 :            :                        &ctx->ext_args);
    7289                 :            : }
    7290                 :            : 
    7291                 :            : static void
    7292                 :         75 : bs_shallow_copy_cluster_find_next(void *cb_arg)
    7293                 :            : {
    7294                 :         75 :         struct shallow_copy_ctx *ctx = cb_arg;
    7295                 :         75 :         struct spdk_blob *_blob = ctx->blob;
    7296                 :            : 
    7297         [ +  + ]:        125 :         while (ctx->cluster < _blob->active.num_clusters) {
    7298         [ +  + ]:        100 :                 if (_blob->active.clusters[ctx->cluster] != 0) {
    7299                 :         50 :                         break;
    7300                 :            :                 }
    7301                 :            : 
    7302                 :         50 :                 ctx->cluster++;
    7303                 :            :         }
    7304                 :            : 
    7305         [ +  + ]:         75 :         if (ctx->cluster < _blob->active.num_clusters) {
    7306                 :         84 :                 blob_request_submit_op_single(ctx->blob_channel, _blob, ctx->read_buff,
    7307                 :         50 :                                               bs_cluster_to_lba(_blob->bs, ctx->cluster),
    7308                 :         50 :                                               bs_dev_byte_to_lba(_blob->bs->dev, _blob->bs->cluster_sz),
    7309                 :            :                                               bs_shallow_copy_blob_read_cpl, ctx, SPDK_BLOB_READ);
    7310                 :            :         } else {
    7311                 :         25 :                 _blob->locked_operation_in_progress = false;
    7312                 :         25 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7313                 :            :         }
    7314                 :         75 : }
    7315                 :            : 
    7316                 :            : static void
    7317                 :         97 : bs_shallow_copy_blob_open_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    7318                 :            : {
    7319                 :         97 :         struct shallow_copy_ctx *ctx = cb_arg;
    7320                 :         97 :         struct spdk_bs_dev *ext_dev = ctx->ext_dev;
    7321                 :            :         uint32_t blob_block_size;
    7322                 :            :         uint64_t blob_total_size;
    7323                 :            : 
    7324         [ -  + ]:         97 :         if (bserrno != 0) {
    7325                 :          0 :                 SPDK_ERRLOG("Shallow copy blob open error %d\n", bserrno);
    7326                 :          0 :                 ctx->bserrno = bserrno;
    7327                 :          0 :                 bs_shallow_copy_cleanup_finish(ctx, 0);
    7328                 :          0 :                 return;
    7329                 :            :         }
    7330                 :            : 
    7331         [ +  + ]:         97 :         if (!spdk_blob_is_read_only(_blob)) {
    7332                 :         24 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, blob must be read only\n", _blob->id);
    7333                 :         24 :                 ctx->bserrno = -EPERM;
    7334                 :         24 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7335                 :         24 :                 return;
    7336                 :            :         }
    7337                 :            : 
    7338                 :         73 :         blob_block_size = _blob->bs->dev->blocklen;
    7339                 :         73 :         blob_total_size = spdk_blob_get_num_clusters(_blob) * spdk_bs_get_cluster_size(_blob->bs);
    7340                 :            : 
    7341         [ +  + ]:         73 :         if (blob_total_size > ext_dev->blockcnt * ext_dev->blocklen) {
    7342                 :         24 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, external device must have at least blob size\n",
    7343                 :            :                             _blob->id);
    7344                 :         24 :                 ctx->bserrno = -EINVAL;
    7345                 :         24 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7346                 :         24 :                 return;
    7347                 :            :         }
    7348                 :            : 
    7349   [ +  +  +  + ]:         49 :         if (blob_block_size % ext_dev->blocklen != 0) {
    7350                 :         24 :                 SPDK_ERRLOG("blob 0x%" PRIx64 " shallow copy, external device block size is not compatible with \
    7351                 :            : blobstore block size\n", _blob->id);
    7352                 :         24 :                 ctx->bserrno = -EINVAL;
    7353                 :         24 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7354                 :         24 :                 return;
    7355                 :            :         }
    7356                 :            : 
    7357                 :         25 :         ctx->blob = _blob;
    7358                 :            : 
    7359   [ -  +  -  + ]:         25 :         if (_blob->locked_operation_in_progress) {
    7360   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "blob 0x%" PRIx64 " shallow copy - another operation in progress\n", _blob->id);
    7361                 :          0 :                 ctx->bserrno = -EBUSY;
    7362                 :          0 :                 spdk_blob_close(_blob, bs_shallow_copy_cleanup_finish, ctx);
    7363                 :          0 :                 return;
    7364                 :            :         }
    7365                 :            : 
    7366                 :         25 :         _blob->locked_operation_in_progress = true;
    7367                 :            : 
    7368                 :         25 :         ctx->cluster = 0;
    7369                 :         25 :         bs_shallow_copy_cluster_find_next(ctx);
    7370                 :            : }
    7371                 :            : 
    7372                 :            : int
    7373                 :         97 : spdk_bs_blob_shallow_copy(struct spdk_blob_store *bs, struct spdk_io_channel *channel,
    7374                 :            :                           spdk_blob_id blobid, struct spdk_bs_dev *ext_dev,
    7375                 :            :                           spdk_blob_shallow_copy_status status_cb_fn, void *status_cb_arg,
    7376                 :            :                           spdk_blob_op_complete cb_fn, void *cb_arg)
    7377                 :            : {
    7378                 :            :         struct shallow_copy_ctx *ctx;
    7379                 :            :         struct spdk_io_channel *ext_channel;
    7380                 :            : 
    7381                 :         97 :         ctx = calloc(1, sizeof(*ctx));
    7382         [ -  + ]:         97 :         if (!ctx) {
    7383                 :          0 :                 return -ENOMEM;
    7384                 :            :         }
    7385                 :            : 
    7386                 :         97 :         ctx->bs = bs;
    7387                 :         97 :         ctx->blobid = blobid;
    7388                 :         97 :         ctx->cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    7389                 :         97 :         ctx->cpl.u.bs_basic.cb_fn = cb_fn;
    7390                 :         97 :         ctx->cpl.u.bs_basic.cb_arg = cb_arg;
    7391                 :         97 :         ctx->bserrno = 0;
    7392                 :         97 :         ctx->blob_channel = channel;
    7393                 :         97 :         ctx->status_cb = status_cb_fn;
    7394                 :         97 :         ctx->status_cb_arg = status_cb_arg;
    7395                 :         97 :         ctx->read_buff = spdk_malloc(bs->cluster_sz, bs->dev->blocklen, NULL,
    7396                 :            :                                      SPDK_ENV_LCORE_ID_ANY, SPDK_MALLOC_DMA);
    7397         [ -  + ]:         97 :         if (!ctx->read_buff) {
    7398                 :          0 :                 free(ctx);
    7399                 :          0 :                 return -ENOMEM;
    7400                 :            :         }
    7401                 :            : 
    7402                 :         97 :         ext_channel = ext_dev->create_channel(ext_dev);
    7403         [ -  + ]:         97 :         if (!ext_channel) {
    7404                 :          0 :                 spdk_free(ctx->read_buff);
    7405                 :          0 :                 free(ctx);
    7406                 :          0 :                 return -ENOMEM;
    7407                 :            :         }
    7408                 :         97 :         ctx->ext_dev = ext_dev;
    7409                 :         97 :         ctx->ext_channel = ext_channel;
    7410                 :            : 
    7411                 :         97 :         spdk_bs_open_blob(ctx->bs, ctx->blobid, bs_shallow_copy_blob_open_cpl, ctx);
    7412                 :            : 
    7413                 :         97 :         return 0;
    7414                 :            : }
    7415                 :            : /* END spdk_bs_blob_shallow_copy */
    7416                 :            : 
    7417                 :            : /* START spdk_bs_blob_set_parent */
    7418                 :            : 
    7419                 :            : struct set_parent_ctx {
    7420                 :            :         struct spdk_blob_store *bs;
    7421                 :            :         int                     bserrno;
    7422                 :            :         spdk_bs_op_complete     cb_fn;
    7423                 :            :         void                    *cb_arg;
    7424                 :            : 
    7425                 :            :         struct spdk_blob        *blob;
    7426                 :            :         bool                    blob_md_ro;
    7427                 :            : 
    7428                 :            :         struct blob_parent      parent;
    7429                 :            : };
    7430                 :            : 
    7431                 :            : static void
    7432                 :        148 : bs_set_parent_cleanup_finish(void *cb_arg, int bserrno)
    7433                 :            : {
    7434                 :        148 :         struct set_parent_ctx *ctx = cb_arg;
    7435                 :            : 
    7436         [ -  + ]:        148 :         assert(ctx != NULL);
    7437                 :            : 
    7438         [ -  + ]:        148 :         if (bserrno != 0) {
    7439                 :          0 :                 SPDK_ERRLOG("blob set parent finish error %d\n", bserrno);
    7440         [ #  # ]:          0 :                 if (ctx->bserrno == 0) {
    7441                 :          0 :                         ctx->bserrno = bserrno;
    7442                 :            :                 }
    7443                 :            :         }
    7444                 :            : 
    7445                 :        148 :         ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
    7446                 :            : 
    7447                 :        148 :         free(ctx);
    7448                 :        148 : }
    7449                 :            : 
    7450                 :            : static void
    7451                 :        123 : bs_set_parent_close_snapshot(void *cb_arg, int bserrno)
    7452                 :            : {
    7453                 :        123 :         struct set_parent_ctx *ctx = cb_arg;
    7454                 :            : 
    7455         [ +  + ]:        123 :         if (ctx->bserrno != 0) {
    7456                 :         48 :                 spdk_blob_close(ctx->parent.u.snapshot.blob, bs_set_parent_cleanup_finish, ctx);
    7457                 :         48 :                 return;
    7458                 :            :         }
    7459                 :            : 
    7460         [ -  + ]:         75 :         if (bserrno != 0) {
    7461                 :          0 :                 SPDK_ERRLOG("blob close error %d\n", bserrno);
    7462                 :          0 :                 ctx->bserrno = bserrno;
    7463                 :            :         }
    7464                 :            : 
    7465                 :         75 :         bs_set_parent_cleanup_finish(ctx, ctx->bserrno);
    7466                 :            : }
    7467                 :            : 
    7468                 :            : static void
    7469                 :         75 : bs_set_parent_close_blob(void *cb_arg, int bserrno)
    7470                 :            : {
    7471                 :         75 :         struct set_parent_ctx *ctx = cb_arg;
    7472                 :         75 :         struct spdk_blob *blob = ctx->blob;
    7473                 :         75 :         struct spdk_blob *snapshot = ctx->parent.u.snapshot.blob;
    7474                 :            : 
    7475   [ -  +  -  - ]:         75 :         if (bserrno != 0 && ctx->bserrno == 0) {
    7476                 :          0 :                 SPDK_ERRLOG("error %d in metadata sync\n", bserrno);
    7477                 :          0 :                 ctx->bserrno = bserrno;
    7478                 :            :         }
    7479                 :            : 
    7480                 :            :         /* Revert md_ro to original state */
    7481         [ -  + ]:         75 :         blob->md_ro = ctx->blob_md_ro;
    7482                 :            : 
    7483                 :         75 :         blob->locked_operation_in_progress = false;
    7484                 :         75 :         snapshot->locked_operation_in_progress = false;
    7485                 :            : 
    7486                 :         75 :         spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
    7487                 :         75 : }
    7488                 :            : 
    7489                 :            : static void
    7490                 :         75 : bs_set_parent_set_back_bs_dev_done(void *cb_arg, int bserrno)
    7491                 :            : {
    7492                 :         75 :         struct set_parent_ctx *ctx = cb_arg;
    7493                 :         75 :         struct spdk_blob *blob = ctx->blob;
    7494                 :            : 
    7495         [ -  + ]:         75 :         if (bserrno != 0) {
    7496                 :          0 :                 SPDK_ERRLOG("error %d setting back_bs_dev\n", bserrno);
    7497                 :          0 :                 ctx->bserrno = bserrno;
    7498                 :          0 :                 bs_set_parent_close_blob(ctx, bserrno);
    7499                 :          0 :                 return;
    7500                 :            :         }
    7501                 :            : 
    7502                 :         75 :         spdk_blob_sync_md(blob, bs_set_parent_close_blob, ctx);
    7503                 :            : }
    7504                 :            : 
    7505                 :            : static int
    7506                 :         75 : bs_set_parent_refs(struct spdk_blob *blob, struct blob_parent *parent)
    7507                 :            : {
    7508                 :            :         int rc;
    7509                 :            : 
    7510                 :         75 :         bs_blob_list_remove(blob);
    7511                 :            : 
    7512                 :         75 :         rc = blob_set_xattr(blob, BLOB_SNAPSHOT, &parent->u.snapshot.id, sizeof(spdk_blob_id), true);
    7513         [ -  + ]:         75 :         if (rc != 0) {
    7514                 :          0 :                 SPDK_ERRLOG("error %d setting snapshot xattr\n", rc);
    7515                 :          0 :                 return rc;
    7516                 :            :         }
    7517                 :         75 :         blob->parent_id = parent->u.snapshot.id;
    7518                 :            : 
    7519         [ +  + ]:         75 :         if (blob_is_esnap_clone(blob)) {
    7520                 :            :                 /* Remove the xattr that references the external snapshot */
    7521                 :         25 :                 blob->invalid_flags &= ~SPDK_BLOB_EXTERNAL_SNAPSHOT;
    7522                 :         25 :                 blob_remove_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID, true);
    7523                 :            :         }
    7524                 :            : 
    7525                 :         75 :         bs_blob_list_add(blob);
    7526                 :            : 
    7527                 :         75 :         return 0;
    7528                 :            : }
    7529                 :            : 
    7530                 :            : static void
    7531                 :        124 : bs_set_parent_snapshot_open_cpl(void *cb_arg, struct spdk_blob *snapshot, int bserrno)
    7532                 :            : {
    7533                 :        124 :         struct set_parent_ctx *ctx = cb_arg;
    7534                 :        124 :         struct spdk_blob *blob = ctx->blob;
    7535                 :            :         struct spdk_bs_dev *back_bs_dev;
    7536                 :            : 
    7537         [ +  + ]:        124 :         if (bserrno != 0) {
    7538                 :          1 :                 SPDK_ERRLOG("snapshot open error %d\n", bserrno);
    7539                 :          1 :                 ctx->bserrno = bserrno;
    7540                 :          1 :                 spdk_blob_close(blob, bs_set_parent_cleanup_finish, ctx);
    7541                 :          1 :                 return;
    7542                 :            :         }
    7543                 :            : 
    7544                 :        123 :         ctx->parent.u.snapshot.blob = snapshot;
    7545                 :        123 :         ctx->parent.u.snapshot.id = snapshot->id;
    7546                 :            : 
    7547         [ +  + ]:        123 :         if (!spdk_blob_is_snapshot(snapshot)) {
    7548                 :         24 :                 SPDK_ERRLOG("parent blob is not a snapshot\n");
    7549                 :         24 :                 ctx->bserrno = -EINVAL;
    7550                 :         24 :                 spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
    7551                 :         24 :                 return;
    7552                 :            :         }
    7553                 :            : 
    7554         [ +  + ]:         99 :         if (blob->active.num_clusters != snapshot->active.num_clusters) {
    7555                 :         24 :                 SPDK_ERRLOG("parent blob has a number of clusters different from child's ones\n");
    7556                 :         24 :                 ctx->bserrno = -EINVAL;
    7557                 :         24 :                 spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
    7558                 :         24 :                 return;
    7559                 :            :         }
    7560                 :            : 
    7561   [ +  +  +  +  :         75 :         if (blob->locked_operation_in_progress || snapshot->locked_operation_in_progress) {
             -  +  -  + ]
    7562                 :          0 :                 SPDK_ERRLOG("cannot set parent of blob, another operation in progress\n");
    7563                 :          0 :                 ctx->bserrno = -EBUSY;
    7564                 :          0 :                 spdk_blob_close(blob, bs_set_parent_close_snapshot, ctx);
    7565                 :          0 :                 return;
    7566                 :            :         }
    7567                 :            : 
    7568                 :         75 :         blob->locked_operation_in_progress = true;
    7569                 :         75 :         snapshot->locked_operation_in_progress = true;
    7570                 :            : 
    7571                 :            :         /* Temporarily override md_ro flag for MD modification */
    7572                 :         75 :         blob->md_ro = false;
    7573                 :            : 
    7574                 :         75 :         back_bs_dev = bs_create_blob_bs_dev(snapshot);
    7575                 :            : 
    7576                 :         75 :         blob_set_back_bs_dev(blob, back_bs_dev, bs_set_parent_refs, &ctx->parent,
    7577                 :            :                              bs_set_parent_set_back_bs_dev_done,
    7578                 :            :                              ctx);
    7579                 :            : }
    7580                 :            : 
    7581                 :            : static void
    7582                 :        148 : bs_set_parent_blob_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
    7583                 :            : {
    7584                 :        148 :         struct set_parent_ctx *ctx = cb_arg;
    7585                 :            : 
    7586         [ -  + ]:        148 :         if (bserrno != 0) {
    7587                 :          0 :                 SPDK_ERRLOG("blob open error %d\n", bserrno);
    7588                 :          0 :                 ctx->bserrno = bserrno;
    7589                 :          0 :                 bs_set_parent_cleanup_finish(ctx, 0);
    7590                 :          0 :                 return;
    7591                 :            :         }
    7592                 :            : 
    7593         [ +  + ]:        148 :         if (!spdk_blob_is_thin_provisioned(blob)) {
    7594                 :         24 :                 SPDK_ERRLOG("blob is not thin-provisioned\n");
    7595                 :         24 :                 ctx->bserrno = -EINVAL;
    7596                 :         24 :                 spdk_blob_close(blob, bs_set_parent_cleanup_finish, ctx);
    7597                 :         24 :                 return;
    7598                 :            :         }
    7599                 :            : 
    7600                 :        124 :         ctx->blob = blob;
    7601         [ -  + ]:        124 :         ctx->blob_md_ro = blob->md_ro;
    7602                 :            : 
    7603                 :        124 :         spdk_bs_open_blob(ctx->bs, ctx->parent.u.snapshot.id, bs_set_parent_snapshot_open_cpl, ctx);
    7604                 :            : }
    7605                 :            : 
    7606                 :            : void
    7607                 :        223 : spdk_bs_blob_set_parent(struct spdk_blob_store *bs, spdk_blob_id blob_id,
    7608                 :            :                         spdk_blob_id snapshot_id, spdk_blob_op_complete cb_fn, void *cb_arg)
    7609                 :            : {
    7610                 :            :         struct set_parent_ctx *ctx;
    7611                 :            : 
    7612         [ +  + ]:        223 :         if (snapshot_id == SPDK_BLOBID_INVALID) {
    7613                 :         24 :                 SPDK_ERRLOG("snapshot id not valid\n");
    7614                 :         24 :                 cb_fn(cb_arg, -EINVAL);
    7615                 :         24 :                 return;
    7616                 :            :         }
    7617                 :            : 
    7618         [ +  + ]:        199 :         if (blob_id == snapshot_id) {
    7619                 :         24 :                 SPDK_ERRLOG("blob id and snapshot id cannot be the same\n");
    7620                 :         24 :                 cb_fn(cb_arg, -EINVAL);
    7621                 :         24 :                 return;
    7622                 :            :         }
    7623                 :            : 
    7624         [ +  + ]:        175 :         if (spdk_blob_get_parent_snapshot(bs, blob_id) == snapshot_id) {
    7625                 :         27 :                 SPDK_NOTICELOG("snapshot is already the parent of blob\n");
    7626                 :         27 :                 cb_fn(cb_arg, -EEXIST);
    7627                 :         27 :                 return;
    7628                 :            :         }
    7629                 :            : 
    7630                 :        148 :         ctx = calloc(1, sizeof(*ctx));
    7631         [ -  + ]:        148 :         if (!ctx) {
    7632                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    7633                 :          0 :                 return;
    7634                 :            :         }
    7635                 :            : 
    7636                 :        148 :         ctx->bs = bs;
    7637                 :        148 :         ctx->parent.u.snapshot.id = snapshot_id;
    7638                 :        148 :         ctx->cb_fn = cb_fn;
    7639                 :        148 :         ctx->cb_arg = cb_arg;
    7640                 :        148 :         ctx->bserrno = 0;
    7641                 :            : 
    7642                 :        148 :         spdk_bs_open_blob(bs, blob_id, bs_set_parent_blob_open_cpl, ctx);
    7643                 :            : }
    7644                 :            : /* END spdk_bs_blob_set_parent */
    7645                 :            : 
    7646                 :            : /* START spdk_bs_blob_set_external_parent */
    7647                 :            : 
    7648                 :            : static void
    7649                 :        102 : bs_set_external_parent_cleanup_finish(void *cb_arg, int bserrno)
    7650                 :            : {
    7651                 :        102 :         struct set_parent_ctx *ctx = cb_arg;
    7652                 :            : 
    7653         [ -  + ]:        102 :         if (bserrno != 0) {
    7654                 :          0 :                 SPDK_ERRLOG("blob set external parent finish error %d\n", bserrno);
    7655         [ #  # ]:          0 :                 if (ctx->bserrno == 0) {
    7656                 :          0 :                         ctx->bserrno = bserrno;
    7657                 :            :                 }
    7658                 :            :         }
    7659                 :            : 
    7660                 :        102 :         ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
    7661                 :            : 
    7662                 :        102 :         free(ctx->parent.u.esnap.id);
    7663                 :        102 :         free(ctx);
    7664                 :        102 : }
    7665                 :            : 
    7666                 :            : static void
    7667                 :         51 : bs_set_external_parent_close_blob(void *cb_arg, int bserrno)
    7668                 :            : {
    7669                 :         51 :         struct set_parent_ctx *ctx = cb_arg;
    7670                 :         51 :         struct spdk_blob *blob = ctx->blob;
    7671                 :            : 
    7672   [ -  +  -  - ]:         51 :         if (bserrno != 0 && ctx->bserrno == 0) {
    7673                 :          0 :                 SPDK_ERRLOG("error %d in metadata sync\n", bserrno);
    7674                 :          0 :                 ctx->bserrno = bserrno;
    7675                 :            :         }
    7676                 :            : 
    7677                 :            :         /* Revert md_ro to original state */
    7678         [ -  + ]:         51 :         blob->md_ro = ctx->blob_md_ro;
    7679                 :            : 
    7680                 :         51 :         blob->locked_operation_in_progress = false;
    7681                 :            : 
    7682                 :         51 :         spdk_blob_close(blob, bs_set_external_parent_cleanup_finish, ctx);
    7683                 :         51 : }
    7684                 :            : 
    7685                 :            : static void
    7686                 :         51 : bs_set_external_parent_unfrozen(void *cb_arg, int bserrno)
    7687                 :            : {
    7688                 :         51 :         struct set_parent_ctx *ctx = cb_arg;
    7689                 :         51 :         struct spdk_blob *blob = ctx->blob;
    7690                 :            : 
    7691         [ -  + ]:         51 :         if (bserrno != 0) {
    7692                 :          0 :                 SPDK_ERRLOG("error %d setting back_bs_dev\n", bserrno);
    7693                 :          0 :                 ctx->bserrno = bserrno;
    7694                 :          0 :                 bs_set_external_parent_close_blob(ctx, bserrno);
    7695                 :          0 :                 return;
    7696                 :            :         }
    7697                 :            : 
    7698                 :         51 :         spdk_blob_sync_md(blob, bs_set_external_parent_close_blob, ctx);
    7699                 :            : }
    7700                 :            : 
    7701                 :            : static int
    7702                 :         51 : bs_set_external_parent_refs(struct spdk_blob *blob, struct blob_parent *parent)
    7703                 :            : {
    7704                 :            :         int rc;
    7705                 :            : 
    7706                 :         51 :         bs_blob_list_remove(blob);
    7707                 :            : 
    7708         [ -  + ]:         51 :         if (spdk_blob_is_clone(blob)) {
    7709                 :            :                 /* Remove the xattr that references the snapshot */
    7710                 :          0 :                 blob->parent_id = SPDK_BLOBID_INVALID;
    7711                 :          0 :                 blob_remove_xattr(blob, BLOB_SNAPSHOT, true);
    7712                 :            :         }
    7713                 :            : 
    7714                 :         51 :         rc = blob_set_xattr(blob, BLOB_EXTERNAL_SNAPSHOT_ID, parent->u.esnap.id,
    7715                 :         51 :                             parent->u.esnap.id_len, true);
    7716         [ -  + ]:         51 :         if (rc != 0) {
    7717                 :          0 :                 SPDK_ERRLOG("error %d setting external snapshot xattr\n", rc);
    7718                 :          0 :                 return rc;
    7719                 :            :         }
    7720                 :         51 :         blob->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
    7721                 :            : 
    7722                 :         51 :         bs_blob_list_add(blob);
    7723                 :            : 
    7724                 :         51 :         return 0;
    7725                 :            : }
    7726                 :            : 
    7727                 :            : static void
    7728                 :        102 : bs_set_external_parent_blob_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
    7729                 :            : {
    7730                 :        102 :         struct set_parent_ctx *ctx = cb_arg;
    7731                 :         86 :         const void *esnap_id;
    7732                 :         86 :         size_t esnap_id_len;
    7733                 :            :         int rc;
    7734                 :            : 
    7735         [ -  + ]:        102 :         if (bserrno != 0) {
    7736                 :          0 :                 SPDK_ERRLOG("blob open error %d\n", bserrno);
    7737                 :          0 :                 ctx->bserrno = bserrno;
    7738                 :          0 :                 bs_set_parent_cleanup_finish(ctx, 0);
    7739                 :          8 :                 return;
    7740                 :            :         }
    7741                 :            : 
    7742                 :        102 :         ctx->blob = blob;
    7743         [ -  + ]:        102 :         ctx->blob_md_ro = blob->md_ro;
    7744                 :            : 
    7745                 :        102 :         rc = spdk_blob_get_esnap_id(blob, &esnap_id, &esnap_id_len);
    7746   [ +  +  +  -  :        102 :         if (rc == 0 && esnap_id != NULL && esnap_id_len == ctx->parent.u.esnap.id_len &&
                   +  - ]
    7747   [ +  +  -  +  :         28 :             memcmp(esnap_id, ctx->parent.u.esnap.id, esnap_id_len) == 0) {
                   +  + ]
    7748                 :         27 :                 SPDK_ERRLOG("external snapshot is already the parent of blob\n");
    7749                 :         27 :                 ctx->bserrno = -EEXIST;
    7750                 :         27 :                 goto error;
    7751                 :            :         }
    7752                 :            : 
    7753         [ +  + ]:         75 :         if (!spdk_blob_is_thin_provisioned(blob)) {
    7754                 :         24 :                 SPDK_ERRLOG("blob is not thin-provisioned\n");
    7755                 :         24 :                 ctx->bserrno = -EINVAL;
    7756                 :         24 :                 goto error;
    7757                 :            :         }
    7758                 :            : 
    7759   [ -  +  -  + ]:         51 :         if (blob->locked_operation_in_progress) {
    7760                 :          0 :                 SPDK_ERRLOG("cannot set external parent of blob, another operation in progress\n");
    7761                 :          0 :                 ctx->bserrno = -EBUSY;
    7762                 :          0 :                 goto error;
    7763                 :            :         }
    7764                 :            : 
    7765                 :         51 :         blob->locked_operation_in_progress = true;
    7766                 :            : 
    7767                 :            :         /* Temporarily override md_ro flag for MD modification */
    7768                 :         51 :         blob->md_ro = false;
    7769                 :            : 
    7770                 :         51 :         blob_set_back_bs_dev(blob, ctx->parent.u.esnap.back_bs_dev, bs_set_external_parent_refs,
    7771                 :            :                              &ctx->parent, bs_set_external_parent_unfrozen, ctx);
    7772                 :         51 :         return;
    7773                 :            : 
    7774                 :         51 : error:
    7775                 :         51 :         spdk_blob_close(blob, bs_set_external_parent_cleanup_finish, ctx);
    7776                 :            : }
    7777                 :            : 
    7778                 :            : void
    7779                 :        150 : spdk_bs_blob_set_external_parent(struct spdk_blob_store *bs, spdk_blob_id blob_id,
    7780                 :            :                                  struct spdk_bs_dev *esnap_bs_dev, const void *esnap_id,
    7781                 :            :                                  uint32_t esnap_id_len, spdk_blob_op_complete cb_fn, void *cb_arg)
    7782                 :            : {
    7783                 :            :         struct set_parent_ctx *ctx;
    7784                 :            :         uint64_t esnap_dev_size, cluster_sz;
    7785                 :            : 
    7786   [ +  +  +  +  :        150 :         if (sizeof(blob_id) == esnap_id_len && memcmp(&blob_id, esnap_id, sizeof(blob_id)) == 0) {
                   +  - ]
    7787                 :         24 :                 SPDK_ERRLOG("blob id and external snapshot id cannot be the same\n");
    7788                 :         24 :                 cb_fn(cb_arg, -EINVAL);
    7789                 :         24 :                 return;
    7790                 :            :         }
    7791                 :            : 
    7792                 :        126 :         esnap_dev_size = esnap_bs_dev->blockcnt * esnap_bs_dev->blocklen;
    7793                 :        126 :         cluster_sz = spdk_bs_get_cluster_size(bs);
    7794   [ +  +  +  + ]:        126 :         if ((esnap_dev_size % cluster_sz) != 0) {
    7795                 :         24 :                 SPDK_ERRLOG("Esnap device size %" PRIu64 " is not an integer multiple of "
    7796                 :            :                             "cluster size %" PRIu64 "\n", esnap_dev_size, cluster_sz);
    7797                 :         24 :                 cb_fn(cb_arg, -EINVAL);
    7798                 :         24 :                 return;
    7799                 :            :         }
    7800                 :            : 
    7801                 :        102 :         ctx = calloc(1, sizeof(*ctx));
    7802         [ -  + ]:        102 :         if (!ctx) {
    7803                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    7804                 :          0 :                 return;
    7805                 :            :         }
    7806                 :            : 
    7807                 :        102 :         ctx->parent.u.esnap.id = calloc(1, esnap_id_len);
    7808         [ -  + ]:        102 :         if (!ctx->parent.u.esnap.id) {
    7809                 :          0 :                 free(ctx);
    7810                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    7811                 :          0 :                 return;
    7812                 :            :         }
    7813                 :            : 
    7814                 :        102 :         ctx->bs = bs;
    7815                 :        102 :         ctx->parent.u.esnap.back_bs_dev = esnap_bs_dev;
    7816   [ -  +  -  + ]:        102 :         memcpy(ctx->parent.u.esnap.id, esnap_id, esnap_id_len);
    7817                 :        102 :         ctx->parent.u.esnap.id_len = esnap_id_len;
    7818                 :        102 :         ctx->cb_fn = cb_fn;
    7819                 :        102 :         ctx->cb_arg = cb_arg;
    7820                 :        102 :         ctx->bserrno = 0;
    7821                 :            : 
    7822                 :        102 :         spdk_bs_open_blob(bs, blob_id, bs_set_external_parent_blob_open_cpl, ctx);
    7823                 :            : }
    7824                 :            : /* END spdk_bs_blob_set_external_parent */
    7825                 :            : 
    7826                 :            : /* START spdk_blob_resize */
    7827                 :            : struct spdk_bs_resize_ctx {
    7828                 :            :         spdk_blob_op_complete cb_fn;
    7829                 :            :         void *cb_arg;
    7830                 :            :         struct spdk_blob *blob;
    7831                 :            :         uint64_t sz;
    7832                 :            :         int rc;
    7833                 :            : };
    7834                 :            : 
    7835                 :            : static void
    7836                 :     116396 : bs_resize_unfreeze_cpl(void *cb_arg, int rc)
    7837                 :            : {
    7838                 :     116396 :         struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg;
    7839                 :            : 
    7840         [ -  + ]:     116396 :         if (rc != 0) {
    7841                 :          0 :                 SPDK_ERRLOG("Unfreeze failed, rc=%d\n", rc);
    7842                 :            :         }
    7843                 :            : 
    7844         [ +  + ]:     116396 :         if (ctx->rc != 0) {
    7845                 :         25 :                 SPDK_ERRLOG("Unfreeze failed, ctx->rc=%d\n", ctx->rc);
    7846                 :         25 :                 rc = ctx->rc;
    7847                 :            :         }
    7848                 :            : 
    7849                 :     116396 :         ctx->blob->locked_operation_in_progress = false;
    7850                 :            : 
    7851                 :     116396 :         ctx->cb_fn(ctx->cb_arg, rc);
    7852                 :     116396 :         free(ctx);
    7853                 :     116396 : }
    7854                 :            : 
    7855                 :            : static void
    7856                 :     116396 : bs_resize_freeze_cpl(void *cb_arg, int rc)
    7857                 :            : {
    7858                 :     116396 :         struct spdk_bs_resize_ctx *ctx = (struct spdk_bs_resize_ctx *)cb_arg;
    7859                 :            : 
    7860         [ -  + ]:     116396 :         if (rc != 0) {
    7861                 :          0 :                 ctx->blob->locked_operation_in_progress = false;
    7862                 :          0 :                 ctx->cb_fn(ctx->cb_arg, rc);
    7863                 :          0 :                 free(ctx);
    7864                 :          0 :                 return;
    7865                 :            :         }
    7866                 :            : 
    7867                 :     116396 :         ctx->rc = blob_resize(ctx->blob, ctx->sz);
    7868                 :            : 
    7869                 :     116396 :         blob_unfreeze_io(ctx->blob, bs_resize_unfreeze_cpl, ctx);
    7870                 :            : }
    7871                 :            : 
    7872                 :            : void
    7873                 :     116480 : spdk_blob_resize(struct spdk_blob *blob, uint64_t sz, spdk_blob_op_complete cb_fn, void *cb_arg)
    7874                 :            : {
    7875                 :            :         struct spdk_bs_resize_ctx *ctx;
    7876                 :            : 
    7877                 :     116480 :         blob_verify_md_op(blob);
    7878                 :            : 
    7879   [ -  +  -  + ]:     116480 :         SPDK_DEBUGLOG(blob, "Resizing blob 0x%" PRIx64 " to %" PRIu64 " clusters\n", blob->id, sz);
    7880                 :            : 
    7881   [ +  +  +  + ]:     116480 :         if (blob->md_ro) {
    7882                 :         24 :                 cb_fn(cb_arg, -EPERM);
    7883                 :         24 :                 return;
    7884                 :            :         }
    7885                 :            : 
    7886         [ +  + ]:     116456 :         if (sz == blob->active.num_clusters) {
    7887                 :         60 :                 cb_fn(cb_arg, 0);
    7888                 :         60 :                 return;
    7889                 :            :         }
    7890                 :            : 
    7891   [ -  +  -  + ]:     116396 :         if (blob->locked_operation_in_progress) {
    7892                 :          0 :                 cb_fn(cb_arg, -EBUSY);
    7893                 :          0 :                 return;
    7894                 :            :         }
    7895                 :            : 
    7896                 :     116396 :         ctx = calloc(1, sizeof(*ctx));
    7897         [ -  + ]:     116396 :         if (!ctx) {
    7898                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    7899                 :          0 :                 return;
    7900                 :            :         }
    7901                 :            : 
    7902                 :     116396 :         blob->locked_operation_in_progress = true;
    7903                 :     116396 :         ctx->cb_fn = cb_fn;
    7904                 :     116396 :         ctx->cb_arg = cb_arg;
    7905                 :     116396 :         ctx->blob = blob;
    7906                 :     116396 :         ctx->sz = sz;
    7907                 :     116396 :         blob_freeze_io(blob, bs_resize_freeze_cpl, ctx);
    7908                 :            : }
    7909                 :            : 
    7910                 :            : /* END spdk_blob_resize */
    7911                 :            : 
    7912                 :            : 
    7913                 :            : /* START spdk_bs_delete_blob */
    7914                 :            : 
    7915                 :            : static void
    7916                 :      10570 : bs_delete_close_cpl(void *cb_arg, int bserrno)
    7917                 :            : {
    7918                 :      10570 :         spdk_bs_sequence_t *seq = cb_arg;
    7919                 :            : 
    7920                 :      10570 :         bs_sequence_finish(seq, bserrno);
    7921                 :      10570 : }
    7922                 :            : 
    7923                 :            : static void
    7924                 :      10570 : bs_delete_persist_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    7925                 :            : {
    7926                 :      10570 :         struct spdk_blob *blob = cb_arg;
    7927                 :            : 
    7928         [ -  + ]:      10570 :         if (bserrno != 0) {
    7929                 :            :                 /*
    7930                 :            :                  * We already removed this blob from the blobstore tailq, so
    7931                 :            :                  *  we need to free it here since this is the last reference
    7932                 :            :                  *  to it.
    7933                 :            :                  */
    7934                 :          0 :                 blob_free(blob);
    7935                 :          0 :                 bs_delete_close_cpl(seq, bserrno);
    7936                 :          0 :                 return;
    7937                 :            :         }
    7938                 :            : 
    7939                 :            :         /*
    7940                 :            :          * This will immediately decrement the ref_count and call
    7941                 :            :          *  the completion routine since the metadata state is clean.
    7942                 :            :          *  By calling spdk_blob_close, we reduce the number of call
    7943                 :            :          *  points into code that touches the blob->open_ref count
    7944                 :            :          *  and the blobstore's blob list.
    7945                 :            :          */
    7946                 :      10570 :         spdk_blob_close(blob, bs_delete_close_cpl, seq);
    7947                 :            : }
    7948                 :            : 
    7949                 :            : struct delete_snapshot_ctx {
    7950                 :            :         struct spdk_blob_list *parent_snapshot_entry;
    7951                 :            :         struct spdk_blob *snapshot;
    7952                 :            :         struct spdk_blob_md_page *page;
    7953                 :            :         bool snapshot_md_ro;
    7954                 :            :         struct spdk_blob *clone;
    7955                 :            :         bool clone_md_ro;
    7956                 :            :         spdk_blob_op_with_handle_complete cb_fn;
    7957                 :            :         void *cb_arg;
    7958                 :            :         int bserrno;
    7959                 :            :         uint32_t next_extent_page;
    7960                 :            : };
    7961                 :            : 
    7962                 :            : static void
    7963                 :        668 : delete_blob_cleanup_finish(void *cb_arg, int bserrno)
    7964                 :            : {
    7965                 :        668 :         struct delete_snapshot_ctx *ctx = cb_arg;
    7966                 :            : 
    7967         [ -  + ]:        668 :         if (bserrno != 0) {
    7968                 :          0 :                 SPDK_ERRLOG("Snapshot cleanup error %d\n", bserrno);
    7969                 :            :         }
    7970                 :            : 
    7971         [ -  + ]:        668 :         assert(ctx != NULL);
    7972                 :            : 
    7973   [ -  +  -  - ]:        668 :         if (bserrno != 0 && ctx->bserrno == 0) {
    7974                 :          0 :                 ctx->bserrno = bserrno;
    7975                 :            :         }
    7976                 :            : 
    7977                 :        668 :         ctx->cb_fn(ctx->cb_arg, ctx->snapshot, ctx->bserrno);
    7978                 :        668 :         spdk_free(ctx->page);
    7979                 :        668 :         free(ctx);
    7980                 :        668 : }
    7981                 :            : 
    7982                 :            : static void
    7983                 :        132 : delete_snapshot_cleanup_snapshot(void *cb_arg, int bserrno)
    7984                 :            : {
    7985                 :        132 :         struct delete_snapshot_ctx *ctx = cb_arg;
    7986                 :            : 
    7987         [ -  + ]:        132 :         if (bserrno != 0) {
    7988                 :          0 :                 ctx->bserrno = bserrno;
    7989                 :          0 :                 SPDK_ERRLOG("Clone cleanup error %d\n", bserrno);
    7990                 :            :         }
    7991                 :            : 
    7992         [ +  - ]:        132 :         if (ctx->bserrno != 0) {
    7993         [ -  + ]:        132 :                 assert(blob_lookup(ctx->snapshot->bs, ctx->snapshot->id) == NULL);
    7994                 :        132 :                 RB_INSERT(spdk_blob_tree, &ctx->snapshot->bs->open_blobs, ctx->snapshot);
    7995                 :        132 :                 spdk_bit_array_set(ctx->snapshot->bs->open_blobids, ctx->snapshot->id);
    7996                 :            :         }
    7997                 :            : 
    7998                 :        132 :         ctx->snapshot->locked_operation_in_progress = false;
    7999         [ -  + ]:        132 :         ctx->snapshot->md_ro = ctx->snapshot_md_ro;
    8000                 :            : 
    8001                 :        132 :         spdk_blob_close(ctx->snapshot, delete_blob_cleanup_finish, ctx);
    8002                 :        132 : }
    8003                 :            : 
    8004                 :            : static void
    8005                 :         72 : delete_snapshot_cleanup_clone(void *cb_arg, int bserrno)
    8006                 :            : {
    8007                 :         72 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8008                 :            : 
    8009                 :         72 :         ctx->clone->locked_operation_in_progress = false;
    8010         [ -  + ]:         72 :         ctx->clone->md_ro = ctx->clone_md_ro;
    8011                 :            : 
    8012                 :         72 :         spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx);
    8013                 :         72 : }
    8014                 :            : 
    8015                 :            : static void
    8016                 :        296 : delete_snapshot_unfreeze_cpl(void *cb_arg, int bserrno)
    8017                 :            : {
    8018                 :        296 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8019                 :            : 
    8020         [ -  + ]:        296 :         if (bserrno) {
    8021                 :          0 :                 ctx->bserrno = bserrno;
    8022                 :          0 :                 delete_snapshot_cleanup_clone(ctx, 0);
    8023                 :          0 :                 return;
    8024                 :            :         }
    8025                 :            : 
    8026                 :        296 :         ctx->clone->locked_operation_in_progress = false;
    8027                 :        296 :         spdk_blob_close(ctx->clone, delete_blob_cleanup_finish, ctx);
    8028                 :            : }
    8029                 :            : 
    8030                 :            : static void
    8031                 :        320 : delete_snapshot_sync_snapshot_cpl(void *cb_arg, int bserrno)
    8032                 :            : {
    8033                 :        320 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8034                 :        320 :         struct spdk_blob_list *parent_snapshot_entry = NULL;
    8035                 :        320 :         struct spdk_blob_list *snapshot_entry = NULL;
    8036                 :        320 :         struct spdk_blob_list *clone_entry = NULL;
    8037                 :        320 :         struct spdk_blob_list *snapshot_clone_entry = NULL;
    8038                 :            : 
    8039         [ +  + ]:        320 :         if (bserrno) {
    8040                 :         24 :                 SPDK_ERRLOG("Failed to sync MD on blob\n");
    8041                 :         24 :                 ctx->bserrno = bserrno;
    8042                 :         24 :                 delete_snapshot_cleanup_clone(ctx, 0);
    8043                 :         24 :                 return;
    8044                 :            :         }
    8045                 :            : 
    8046                 :            :         /* Get snapshot entry for the snapshot we want to remove */
    8047                 :        296 :         snapshot_entry = bs_get_snapshot_entry(ctx->snapshot->bs, ctx->snapshot->id);
    8048                 :            : 
    8049         [ -  + ]:        296 :         assert(snapshot_entry != NULL);
    8050                 :            : 
    8051                 :            :         /* Remove clone entry in this snapshot (at this point there can be only one clone) */
    8052                 :        296 :         clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
    8053         [ -  + ]:        296 :         assert(clone_entry != NULL);
    8054         [ -  + ]:        296 :         TAILQ_REMOVE(&snapshot_entry->clones, clone_entry, link);
    8055                 :        296 :         snapshot_entry->clone_count--;
    8056         [ -  + ]:        296 :         assert(TAILQ_EMPTY(&snapshot_entry->clones));
    8057                 :            : 
    8058         [ +  + ]:        296 :         switch (ctx->snapshot->parent_id) {
    8059                 :        247 :         case SPDK_BLOBID_INVALID:
    8060                 :            :         case SPDK_BLOBID_EXTERNAL_SNAPSHOT:
    8061                 :            :                 /* No parent snapshot - just remove clone entry */
    8062                 :        247 :                 free(clone_entry);
    8063                 :        247 :                 break;
    8064                 :         49 :         default:
    8065                 :            :                 /* This snapshot is at the same time a clone of another snapshot - we need to
    8066                 :            :                  * update parent snapshot (remove current clone, add new one inherited from
    8067                 :            :                  * the snapshot that is being removed) */
    8068                 :            : 
    8069                 :            :                 /* Get snapshot entry for parent snapshot and clone entry within that snapshot for
    8070                 :            :                  * snapshot that we are removing */
    8071                 :         49 :                 blob_get_snapshot_and_clone_entries(ctx->snapshot, &parent_snapshot_entry,
    8072                 :            :                                                     &snapshot_clone_entry);
    8073                 :            : 
    8074                 :            :                 /* Switch clone entry in parent snapshot */
    8075                 :         49 :                 TAILQ_INSERT_TAIL(&parent_snapshot_entry->clones, clone_entry, link);
    8076         [ +  - ]:         49 :                 TAILQ_REMOVE(&parent_snapshot_entry->clones, snapshot_clone_entry, link);
    8077                 :         49 :                 free(snapshot_clone_entry);
    8078                 :            :         }
    8079                 :            : 
    8080                 :            :         /* Restore md_ro flags */
    8081         [ -  + ]:        296 :         ctx->clone->md_ro = ctx->clone_md_ro;
    8082         [ -  + ]:        296 :         ctx->snapshot->md_ro = ctx->snapshot_md_ro;
    8083                 :            : 
    8084                 :        296 :         blob_unfreeze_io(ctx->clone, delete_snapshot_unfreeze_cpl, ctx);
    8085                 :            : }
    8086                 :            : 
    8087                 :            : static void
    8088                 :        344 : delete_snapshot_sync_clone_cpl(void *cb_arg, int bserrno)
    8089                 :            : {
    8090                 :        344 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8091                 :            :         uint64_t i;
    8092                 :            : 
    8093                 :        344 :         ctx->snapshot->md_ro = false;
    8094                 :            : 
    8095         [ +  + ]:        344 :         if (bserrno) {
    8096                 :         24 :                 SPDK_ERRLOG("Failed to sync MD on clone\n");
    8097                 :         24 :                 ctx->bserrno = bserrno;
    8098                 :            : 
    8099                 :            :                 /* Restore snapshot to previous state */
    8100                 :         24 :                 bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true);
    8101         [ -  + ]:         24 :                 if (bserrno != 0) {
    8102                 :          0 :                         delete_snapshot_cleanup_clone(ctx, bserrno);
    8103                 :          0 :                         return;
    8104                 :            :                 }
    8105                 :            : 
    8106                 :         24 :                 spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx);
    8107                 :         24 :                 return;
    8108                 :            :         }
    8109                 :            : 
    8110                 :            :         /* Clear cluster map entries for snapshot */
    8111   [ +  +  +  - ]:       3545 :         for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) {
    8112         [ +  + ]:       3225 :                 if (ctx->clone->active.clusters[i] == ctx->snapshot->active.clusters[i]) {
    8113         [ +  + ]:       3161 :                         if (ctx->snapshot->active.clusters[i] != 0) {
    8114                 :       1981 :                                 ctx->snapshot->active.num_allocated_clusters--;
    8115                 :            :                         }
    8116                 :       3161 :                         ctx->snapshot->active.clusters[i] = 0;
    8117                 :            :                 }
    8118                 :            :         }
    8119         [ +  + ]:        484 :         for (i = 0; i < ctx->snapshot->active.num_extent_pages &&
    8120         [ +  - ]:        328 :              i < ctx->clone->active.num_extent_pages; i++) {
    8121         [ +  + ]:        164 :                 if (ctx->clone->active.extent_pages[i] == ctx->snapshot->active.extent_pages[i]) {
    8122                 :        148 :                         ctx->snapshot->active.extent_pages[i] = 0;
    8123                 :            :                 }
    8124                 :            :         }
    8125                 :            : 
    8126                 :        320 :         blob_set_thin_provision(ctx->snapshot);
    8127                 :        320 :         ctx->snapshot->state = SPDK_BLOB_STATE_DIRTY;
    8128                 :            : 
    8129         [ +  + ]:        320 :         if (ctx->parent_snapshot_entry != NULL) {
    8130                 :         49 :                 ctx->snapshot->back_bs_dev = NULL;
    8131                 :            :         }
    8132                 :            : 
    8133                 :        320 :         spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_cpl, ctx);
    8134                 :            : }
    8135                 :            : 
    8136                 :            : static void
    8137                 :        344 : delete_snapshot_update_extent_pages_cpl(struct delete_snapshot_ctx *ctx)
    8138                 :            : {
    8139                 :            :         int bserrno;
    8140                 :            : 
    8141                 :            :         /* Delete old backing bs_dev from clone (related to snapshot that will be removed) */
    8142                 :        344 :         blob_back_bs_destroy(ctx->clone);
    8143                 :            : 
    8144                 :            :         /* Set/remove snapshot xattr and switch parent ID and backing bs_dev on clone... */
    8145         [ +  + ]:        344 :         if (ctx->snapshot->parent_id == SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
    8146                 :         52 :                 bserrno = bs_snapshot_copy_xattr(ctx->clone, ctx->snapshot,
    8147                 :            :                                                  BLOB_EXTERNAL_SNAPSHOT_ID);
    8148         [ -  + ]:         52 :                 if (bserrno != 0) {
    8149                 :          0 :                         ctx->bserrno = bserrno;
    8150                 :            : 
    8151                 :            :                         /* Restore snapshot to previous state */
    8152                 :          0 :                         bserrno = blob_remove_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, true);
    8153         [ #  # ]:          0 :                         if (bserrno != 0) {
    8154                 :          0 :                                 delete_snapshot_cleanup_clone(ctx, bserrno);
    8155                 :          0 :                                 return;
    8156                 :            :                         }
    8157                 :            : 
    8158                 :          0 :                         spdk_blob_sync_md(ctx->snapshot, delete_snapshot_cleanup_clone, ctx);
    8159                 :          0 :                         return;
    8160                 :            :                 }
    8161                 :         52 :                 ctx->clone->parent_id = SPDK_BLOBID_EXTERNAL_SNAPSHOT;
    8162                 :         52 :                 ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev;
    8163                 :            :                 /* Do not delete the external snapshot along with this snapshot */
    8164                 :         52 :                 ctx->snapshot->back_bs_dev = NULL;
    8165                 :         52 :                 ctx->clone->invalid_flags |= SPDK_BLOB_EXTERNAL_SNAPSHOT;
    8166         [ +  + ]:        292 :         } else if (ctx->parent_snapshot_entry != NULL) {
    8167                 :            :                 /* ...to parent snapshot */
    8168                 :         49 :                 ctx->clone->parent_id = ctx->parent_snapshot_entry->id;
    8169                 :         49 :                 ctx->clone->back_bs_dev = ctx->snapshot->back_bs_dev;
    8170                 :         49 :                 blob_set_xattr(ctx->clone, BLOB_SNAPSHOT, &ctx->parent_snapshot_entry->id,
    8171                 :            :                                sizeof(spdk_blob_id),
    8172                 :            :                                true);
    8173                 :            :         } else {
    8174                 :            :                 /* ...to blobid invalid and zeroes dev */
    8175                 :        243 :                 ctx->clone->parent_id = SPDK_BLOBID_INVALID;
    8176                 :        243 :                 ctx->clone->back_bs_dev = bs_create_zeroes_dev();
    8177                 :        243 :                 blob_remove_xattr(ctx->clone, BLOB_SNAPSHOT, true);
    8178                 :            :         }
    8179                 :            : 
    8180                 :        344 :         spdk_blob_sync_md(ctx->clone, delete_snapshot_sync_clone_cpl, ctx);
    8181                 :            : }
    8182                 :            : 
    8183                 :            : static void
    8184                 :        360 : delete_snapshot_update_extent_pages(void *cb_arg, int bserrno)
    8185                 :            : {
    8186                 :        360 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8187                 :            :         uint32_t *extent_page;
    8188                 :            :         uint64_t i;
    8189                 :            : 
    8190         [ +  + ]:        520 :         for (i = ctx->next_extent_page; i < ctx->snapshot->active.num_extent_pages &&
    8191         [ +  - ]:        336 :              i < ctx->clone->active.num_extent_pages; i++) {
    8192         [ +  + ]:        176 :                 if (ctx->snapshot->active.extent_pages[i] == 0) {
    8193                 :            :                         /* No extent page to use from snapshot */
    8194                 :         52 :                         continue;
    8195                 :            :                 }
    8196                 :            : 
    8197                 :        124 :                 extent_page = &ctx->clone->active.extent_pages[i];
    8198         [ +  + ]:        124 :                 if (*extent_page == 0) {
    8199                 :            :                         /* Copy extent page from snapshot when clone did not have a matching one */
    8200                 :        108 :                         *extent_page = ctx->snapshot->active.extent_pages[i];
    8201                 :        108 :                         continue;
    8202                 :            :                 }
    8203                 :            : 
    8204                 :            :                 /* Clone and snapshot both contain partially filled matching extent pages.
    8205                 :            :                  * Update the clone extent page in place with cluster map containing the mix of both. */
    8206                 :         16 :                 ctx->next_extent_page = i + 1;
    8207         [ -  + ]:         16 :                 memset(ctx->page, 0, SPDK_BS_PAGE_SIZE);
    8208                 :            : 
    8209                 :         16 :                 blob_write_extent_page(ctx->clone, *extent_page, i * SPDK_EXTENTS_PER_EP, ctx->page,
    8210                 :            :                                        delete_snapshot_update_extent_pages, ctx);
    8211                 :         16 :                 return;
    8212                 :            :         }
    8213                 :        344 :         delete_snapshot_update_extent_pages_cpl(ctx);
    8214                 :            : }
    8215                 :            : 
    8216                 :            : static void
    8217                 :        368 : delete_snapshot_sync_snapshot_xattr_cpl(void *cb_arg, int bserrno)
    8218                 :            : {
    8219                 :        368 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8220                 :            :         uint64_t i;
    8221                 :            : 
    8222                 :            :         /* Temporarily override md_ro flag for clone for MD modification */
    8223         [ -  + ]:        368 :         ctx->clone_md_ro = ctx->clone->md_ro;
    8224                 :        368 :         ctx->clone->md_ro = false;
    8225                 :            : 
    8226         [ +  + ]:        368 :         if (bserrno) {
    8227                 :         24 :                 SPDK_ERRLOG("Failed to sync MD with xattr on blob\n");
    8228                 :         24 :                 ctx->bserrno = bserrno;
    8229                 :         24 :                 delete_snapshot_cleanup_clone(ctx, 0);
    8230                 :         24 :                 return;
    8231                 :            :         }
    8232                 :            : 
    8233                 :            :         /* Copy snapshot map to clone map (only unallocated clusters in clone) */
    8234   [ +  +  +  - ]:       3809 :         for (i = 0; i < ctx->snapshot->active.num_clusters && i < ctx->clone->active.num_clusters; i++) {
    8235         [ +  + ]:       3465 :                 if (ctx->clone->active.clusters[i] == 0) {
    8236                 :       3401 :                         ctx->clone->active.clusters[i] = ctx->snapshot->active.clusters[i];
    8237         [ +  + ]:       3401 :                         if (ctx->clone->active.clusters[i] != 0) {
    8238                 :       2221 :                                 ctx->clone->active.num_allocated_clusters++;
    8239                 :            :                         }
    8240                 :            :                 }
    8241                 :            :         }
    8242                 :        344 :         ctx->next_extent_page = 0;
    8243                 :        344 :         delete_snapshot_update_extent_pages(ctx, 0);
    8244                 :            : }
    8245                 :            : 
    8246                 :            : static void
    8247                 :         52 : delete_snapshot_esnap_channels_destroyed_cb(void *cb_arg, struct spdk_blob *blob, int bserrno)
    8248                 :            : {
    8249                 :         52 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8250                 :            : 
    8251         [ -  + ]:         52 :         if (bserrno != 0) {
    8252                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to destroy esnap channels: %d\n",
    8253                 :            :                             blob->id, bserrno);
    8254                 :            :                 /* That error should not stop us from syncing metadata. */
    8255                 :            :         }
    8256                 :            : 
    8257                 :         52 :         spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx);
    8258                 :         52 : }
    8259                 :            : 
    8260                 :            : static void
    8261                 :        368 : delete_snapshot_freeze_io_cb(void *cb_arg, int bserrno)
    8262                 :            : {
    8263                 :        368 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8264                 :            : 
    8265         [ -  + ]:        368 :         if (bserrno) {
    8266                 :          0 :                 SPDK_ERRLOG("Failed to freeze I/O on clone\n");
    8267                 :          0 :                 ctx->bserrno = bserrno;
    8268                 :          0 :                 delete_snapshot_cleanup_clone(ctx, 0);
    8269                 :          0 :                 return;
    8270                 :            :         }
    8271                 :            : 
    8272                 :            :         /* Temporarily override md_ro flag for snapshot for MD modification */
    8273         [ -  + ]:        368 :         ctx->snapshot_md_ro = ctx->snapshot->md_ro;
    8274                 :        368 :         ctx->snapshot->md_ro = false;
    8275                 :            : 
    8276                 :            :         /* Mark blob as pending for removal for power failure safety, use clone id for recovery */
    8277                 :        368 :         ctx->bserrno = blob_set_xattr(ctx->snapshot, SNAPSHOT_PENDING_REMOVAL, &ctx->clone->id,
    8278                 :            :                                       sizeof(spdk_blob_id), true);
    8279         [ -  + ]:        368 :         if (ctx->bserrno != 0) {
    8280                 :          0 :                 delete_snapshot_cleanup_clone(ctx, 0);
    8281                 :          0 :                 return;
    8282                 :            :         }
    8283                 :            : 
    8284         [ +  + ]:        368 :         if (blob_is_esnap_clone(ctx->snapshot)) {
    8285                 :         52 :                 blob_esnap_destroy_bs_dev_channels(ctx->snapshot, false,
    8286                 :            :                                                    delete_snapshot_esnap_channels_destroyed_cb,
    8287                 :            :                                                    ctx);
    8288                 :         52 :                 return;
    8289                 :            :         }
    8290                 :            : 
    8291                 :        316 :         spdk_blob_sync_md(ctx->snapshot, delete_snapshot_sync_snapshot_xattr_cpl, ctx);
    8292                 :            : }
    8293                 :            : 
    8294                 :            : static void
    8295                 :        428 : delete_snapshot_open_clone_cb(void *cb_arg, struct spdk_blob *clone, int bserrno)
    8296                 :            : {
    8297                 :        428 :         struct delete_snapshot_ctx *ctx = cb_arg;
    8298                 :            : 
    8299         [ +  + ]:        428 :         if (bserrno) {
    8300                 :         60 :                 SPDK_ERRLOG("Failed to open clone\n");
    8301                 :         60 :                 ctx->bserrno = bserrno;
    8302                 :         60 :                 delete_snapshot_cleanup_snapshot(ctx, 0);
    8303                 :         60 :                 return;
    8304                 :            :         }
    8305                 :            : 
    8306                 :        368 :         ctx->clone = clone;
    8307                 :            : 
    8308   [ -  +  -  + ]:        368 :         if (clone->locked_operation_in_progress) {
    8309   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress on its clone\n");
    8310                 :          0 :                 ctx->bserrno = -EBUSY;
    8311                 :          0 :                 spdk_blob_close(ctx->clone, delete_snapshot_cleanup_snapshot, ctx);
    8312                 :          0 :                 return;
    8313                 :            :         }
    8314                 :            : 
    8315                 :        368 :         clone->locked_operation_in_progress = true;
    8316                 :            : 
    8317                 :        368 :         blob_freeze_io(clone, delete_snapshot_freeze_io_cb, ctx);
    8318                 :            : }
    8319                 :            : 
    8320                 :            : static void
    8321                 :        428 : update_clone_on_snapshot_deletion(struct spdk_blob *snapshot, struct delete_snapshot_ctx *ctx)
    8322                 :            : {
    8323                 :        428 :         struct spdk_blob_list *snapshot_entry = NULL;
    8324                 :        428 :         struct spdk_blob_list *clone_entry = NULL;
    8325                 :        428 :         struct spdk_blob_list *snapshot_clone_entry = NULL;
    8326                 :            : 
    8327                 :            :         /* Get snapshot entry for the snapshot we want to remove */
    8328                 :        428 :         snapshot_entry = bs_get_snapshot_entry(snapshot->bs, snapshot->id);
    8329                 :            : 
    8330         [ -  + ]:        428 :         assert(snapshot_entry != NULL);
    8331                 :            : 
    8332                 :            :         /* Get clone of the snapshot (at this point there can be only one clone) */
    8333                 :        428 :         clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
    8334         [ -  + ]:        428 :         assert(snapshot_entry->clone_count == 1);
    8335         [ -  + ]:        428 :         assert(clone_entry != NULL);
    8336                 :            : 
    8337                 :            :         /* Get snapshot entry for parent snapshot and clone entry within that snapshot for
    8338                 :            :          * snapshot that we are removing */
    8339                 :        428 :         blob_get_snapshot_and_clone_entries(snapshot, &ctx->parent_snapshot_entry,
    8340                 :            :                                             &snapshot_clone_entry);
    8341                 :            : 
    8342                 :        428 :         spdk_bs_open_blob(snapshot->bs, clone_entry->id, delete_snapshot_open_clone_cb, ctx);
    8343                 :        428 : }
    8344                 :            : 
    8345                 :            : static void
    8346                 :      10942 : bs_delete_blob_finish(void *cb_arg, struct spdk_blob *blob, int bserrno)
    8347                 :            : {
    8348                 :      10942 :         spdk_bs_sequence_t *seq = cb_arg;
    8349                 :      10942 :         struct spdk_blob_list *snapshot_entry = NULL;
    8350                 :            :         uint32_t page_num;
    8351                 :            : 
    8352         [ +  + ]:      10942 :         if (bserrno) {
    8353                 :        372 :                 SPDK_ERRLOG("Failed to remove blob\n");
    8354                 :        372 :                 bs_sequence_finish(seq, bserrno);
    8355                 :        372 :                 return;
    8356                 :            :         }
    8357                 :            : 
    8358                 :            :         /* Remove snapshot from the list */
    8359                 :      10570 :         snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
    8360         [ +  + ]:      10570 :         if (snapshot_entry != NULL) {
    8361         [ +  + ]:        876 :                 TAILQ_REMOVE(&blob->bs->snapshots, snapshot_entry, link);
    8362                 :        876 :                 free(snapshot_entry);
    8363                 :            :         }
    8364                 :            : 
    8365                 :      10570 :         page_num = bs_blobid_to_page(blob->id);
    8366                 :      10570 :         spdk_bit_array_clear(blob->bs->used_blobids, page_num);
    8367                 :      10570 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    8368                 :      10570 :         blob->active.num_pages = 0;
    8369                 :      10570 :         blob_resize(blob, 0);
    8370                 :            : 
    8371                 :      10570 :         blob_persist(seq, blob, bs_delete_persist_cpl, blob);
    8372                 :            : }
    8373                 :            : 
    8374                 :            : static int
    8375                 :      10942 : bs_is_blob_deletable(struct spdk_blob *blob, bool *update_clone)
    8376                 :            : {
    8377                 :      10942 :         struct spdk_blob_list *snapshot_entry = NULL;
    8378                 :      10942 :         struct spdk_blob_list *clone_entry = NULL;
    8379                 :      10942 :         struct spdk_blob *clone = NULL;
    8380                 :      10942 :         bool has_one_clone = false;
    8381                 :            : 
    8382                 :            :         /* Check if this is a snapshot with clones */
    8383                 :      10942 :         snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
    8384         [ +  + ]:      10942 :         if (snapshot_entry != NULL) {
    8385         [ +  + ]:       1176 :                 if (snapshot_entry->clone_count > 1) {
    8386                 :        144 :                         SPDK_ERRLOG("Cannot remove snapshot with more than one clone\n");
    8387                 :        144 :                         return -EBUSY;
    8388         [ +  + ]:       1032 :                 } else if (snapshot_entry->clone_count == 1) {
    8389                 :        428 :                         has_one_clone = true;
    8390                 :            :                 }
    8391                 :            :         }
    8392                 :            : 
    8393                 :            :         /* Check if someone has this blob open (besides this delete context):
    8394                 :            :          * - open_ref = 1 - only this context opened blob, so it is ok to remove it
    8395                 :            :          * - open_ref <= 2 && has_one_clone = true - clone is holding snapshot
    8396                 :            :          *      and that is ok, because we will update it accordingly */
    8397   [ +  +  +  + ]:      10798 :         if (blob->open_ref <= 2 && has_one_clone) {
    8398                 :        428 :                 clone_entry = TAILQ_FIRST(&snapshot_entry->clones);
    8399         [ -  + ]:        428 :                 assert(clone_entry != NULL);
    8400                 :        428 :                 clone = blob_lookup(blob->bs, clone_entry->id);
    8401                 :            : 
    8402   [ +  +  -  + ]:        428 :                 if (blob->open_ref == 2 && clone == NULL) {
    8403                 :            :                         /* Clone is closed and someone else opened this blob */
    8404                 :          0 :                         SPDK_ERRLOG("Cannot remove snapshot because it is open\n");
    8405                 :          0 :                         return -EBUSY;
    8406                 :            :                 }
    8407                 :            : 
    8408                 :        428 :                 *update_clone = true;
    8409                 :        428 :                 return 0;
    8410                 :            :         }
    8411                 :            : 
    8412         [ +  + ]:      10370 :         if (blob->open_ref > 1) {
    8413                 :         96 :                 SPDK_ERRLOG("Cannot remove snapshot because it is open\n");
    8414                 :         96 :                 return -EBUSY;
    8415                 :            :         }
    8416                 :            : 
    8417         [ -  + ]:      10274 :         assert(has_one_clone == false);
    8418                 :      10274 :         *update_clone = false;
    8419                 :      10274 :         return 0;
    8420                 :            : }
    8421                 :            : 
    8422                 :            : static void
    8423                 :          0 : bs_delete_enomem_close_cpl(void *cb_arg, int bserrno)
    8424                 :            : {
    8425                 :          0 :         spdk_bs_sequence_t *seq = cb_arg;
    8426                 :            : 
    8427                 :          0 :         bs_sequence_finish(seq, -ENOMEM);
    8428                 :          0 : }
    8429                 :            : 
    8430                 :            : static void
    8431                 :      11002 : bs_delete_open_cpl(void *cb_arg, struct spdk_blob *blob, int bserrno)
    8432                 :            : {
    8433                 :      11002 :         spdk_bs_sequence_t *seq = cb_arg;
    8434                 :            :         struct delete_snapshot_ctx *ctx;
    8435                 :      11002 :         bool update_clone = false;
    8436                 :            : 
    8437         [ +  + ]:      11002 :         if (bserrno != 0) {
    8438                 :         60 :                 bs_sequence_finish(seq, bserrno);
    8439                 :        100 :                 return;
    8440                 :            :         }
    8441                 :            : 
    8442                 :      10942 :         blob_verify_md_op(blob);
    8443                 :            : 
    8444                 :      10942 :         ctx = calloc(1, sizeof(*ctx));
    8445         [ -  + ]:      10942 :         if (ctx == NULL) {
    8446                 :          0 :                 spdk_blob_close(blob, bs_delete_enomem_close_cpl, seq);
    8447                 :          0 :                 return;
    8448                 :            :         }
    8449                 :            : 
    8450                 :      10942 :         ctx->snapshot = blob;
    8451                 :      10942 :         ctx->cb_fn = bs_delete_blob_finish;
    8452                 :      10942 :         ctx->cb_arg = seq;
    8453                 :            : 
    8454                 :            :         /* Check if blob can be removed and if it is a snapshot with clone on top of it */
    8455                 :      10942 :         ctx->bserrno = bs_is_blob_deletable(blob, &update_clone);
    8456         [ +  + ]:      10942 :         if (ctx->bserrno) {
    8457                 :        240 :                 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
    8458                 :        240 :                 return;
    8459                 :            :         }
    8460                 :            : 
    8461   [ -  +  -  + ]:      10702 :         if (blob->locked_operation_in_progress) {
    8462   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "Cannot remove blob - another operation in progress\n");
    8463                 :          0 :                 ctx->bserrno = -EBUSY;
    8464                 :          0 :                 spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
    8465                 :          0 :                 return;
    8466                 :            :         }
    8467                 :            : 
    8468                 :      10702 :         blob->locked_operation_in_progress = true;
    8469                 :            : 
    8470                 :            :         /*
    8471                 :            :          * Remove the blob from the blob_store list now, to ensure it does not
    8472                 :            :          *  get returned after this point by blob_lookup().
    8473                 :            :          */
    8474                 :      10702 :         spdk_bit_array_clear(blob->bs->open_blobids, blob->id);
    8475                 :      10702 :         RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob);
    8476                 :            : 
    8477   [ +  +  +  + ]:      10702 :         if (update_clone) {
    8478                 :        428 :                 ctx->page = spdk_zmalloc(SPDK_BS_PAGE_SIZE, 0, NULL, SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    8479         [ -  + ]:        428 :                 if (!ctx->page) {
    8480                 :          0 :                         ctx->bserrno = -ENOMEM;
    8481                 :          0 :                         spdk_blob_close(blob, delete_blob_cleanup_finish, ctx);
    8482                 :          0 :                         return;
    8483                 :            :                 }
    8484                 :            :                 /* This blob is a snapshot with active clone - update clone first */
    8485                 :        428 :                 update_clone_on_snapshot_deletion(blob, ctx);
    8486                 :            :         } else {
    8487                 :            :                 /* This blob does not have any clones - just remove it */
    8488                 :      10274 :                 bs_blob_list_remove(blob);
    8489                 :      10274 :                 bs_delete_blob_finish(seq, blob, 0);
    8490                 :      10274 :                 free(ctx);
    8491                 :            :         }
    8492                 :            : }
    8493                 :            : 
    8494                 :            : void
    8495                 :      11002 : spdk_bs_delete_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
    8496                 :            :                     spdk_blob_op_complete cb_fn, void *cb_arg)
    8497                 :            : {
    8498                 :       9291 :         struct spdk_bs_cpl      cpl;
    8499                 :            :         spdk_bs_sequence_t      *seq;
    8500                 :            : 
    8501   [ -  +  -  + ]:      11002 :         SPDK_DEBUGLOG(blob, "Deleting blob 0x%" PRIx64 "\n", blobid);
    8502                 :            : 
    8503         [ -  + ]:      11002 :         assert(spdk_get_thread() == bs->md_thread);
    8504                 :            : 
    8505                 :      11002 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    8506                 :      11002 :         cpl.u.blob_basic.cb_fn = cb_fn;
    8507                 :      11002 :         cpl.u.blob_basic.cb_arg = cb_arg;
    8508                 :            : 
    8509                 :      11002 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    8510         [ -  + ]:      11002 :         if (!seq) {
    8511                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    8512                 :          0 :                 return;
    8513                 :            :         }
    8514                 :            : 
    8515                 :      11002 :         spdk_bs_open_blob(bs, blobid, bs_delete_open_cpl, seq);
    8516                 :            : }
    8517                 :            : 
    8518                 :            : /* END spdk_bs_delete_blob */
    8519                 :            : 
    8520                 :            : /* START spdk_bs_open_blob */
    8521                 :            : 
    8522                 :            : static void
    8523                 :      42059 : bs_open_blob_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    8524                 :            : {
    8525                 :      42059 :         struct spdk_blob *blob = cb_arg;
    8526                 :            :         struct spdk_blob *existing;
    8527                 :            : 
    8528         [ +  + ]:      42059 :         if (bserrno != 0) {
    8529                 :        384 :                 blob_free(blob);
    8530                 :        384 :                 seq->cpl.u.blob_handle.blob = NULL;
    8531                 :        384 :                 bs_sequence_finish(seq, bserrno);
    8532                 :        384 :                 return;
    8533                 :            :         }
    8534                 :            : 
    8535                 :      41675 :         existing = blob_lookup(blob->bs, blob->id);
    8536         [ +  + ]:      41675 :         if (existing) {
    8537                 :         37 :                 blob_free(blob);
    8538                 :         37 :                 existing->open_ref++;
    8539                 :         37 :                 seq->cpl.u.blob_handle.blob = existing;
    8540                 :         37 :                 bs_sequence_finish(seq, 0);
    8541                 :         37 :                 return;
    8542                 :            :         }
    8543                 :            : 
    8544                 :      41638 :         blob->open_ref++;
    8545                 :            : 
    8546                 :      41638 :         spdk_bit_array_set(blob->bs->open_blobids, blob->id);
    8547                 :      41638 :         RB_INSERT(spdk_blob_tree, &blob->bs->open_blobs, blob);
    8548                 :            : 
    8549                 :      41638 :         bs_sequence_finish(seq, bserrno);
    8550                 :            : }
    8551                 :            : 
    8552                 :            : static inline void
    8553                 :        615 : blob_open_opts_copy(const struct spdk_blob_open_opts *src, struct spdk_blob_open_opts *dst)
    8554                 :            : {
    8555                 :            : #define FIELD_OK(field) \
    8556                 :            :         offsetof(struct spdk_blob_open_opts, field) + sizeof(src->field) <= src->opts_size
    8557                 :            : 
    8558                 :            : #define SET_FIELD(field) \
    8559                 :            :         if (FIELD_OK(field)) { \
    8560                 :            :                 dst->field = src->field; \
    8561                 :            :         } \
    8562                 :            : 
    8563         [ +  - ]:        615 :         SET_FIELD(clear_method);
    8564         [ +  - ]:        615 :         SET_FIELD(esnap_ctx);
    8565                 :            : 
    8566                 :        615 :         dst->opts_size = src->opts_size;
    8567                 :            : 
    8568                 :            :         /* You should not remove this statement, but need to update the assert statement
    8569                 :            :          * if you add a new field, and also add a corresponding SET_FIELD statement */
    8570                 :            :         SPDK_STATIC_ASSERT(sizeof(struct spdk_blob_open_opts) == 24, "Incorrect size");
    8571                 :            : 
    8572                 :            : #undef FIELD_OK
    8573                 :            : #undef SET_FIELD
    8574                 :        615 : }
    8575                 :            : 
    8576                 :            : static void
    8577                 :      47073 : bs_open_blob(struct spdk_blob_store *bs,
    8578                 :            :              spdk_blob_id blobid,
    8579                 :            :              struct spdk_blob_open_opts *opts,
    8580                 :            :              spdk_blob_op_with_handle_complete cb_fn,
    8581                 :            :              void *cb_arg)
    8582                 :            : {
    8583                 :            :         struct spdk_blob                *blob;
    8584                 :      42139 :         struct spdk_bs_cpl              cpl;
    8585                 :      42139 :         struct spdk_blob_open_opts      opts_local;
    8586                 :            :         spdk_bs_sequence_t              *seq;
    8587                 :            :         uint32_t                        page_num;
    8588                 :            : 
    8589   [ -  +  -  + ]:      47073 :         SPDK_DEBUGLOG(blob, "Opening blob 0x%" PRIx64 "\n", blobid);
    8590         [ -  + ]:      47073 :         assert(spdk_get_thread() == bs->md_thread);
    8591                 :            : 
    8592                 :      47073 :         page_num = bs_blobid_to_page(blobid);
    8593         [ +  + ]:      47073 :         if (spdk_bit_array_get(bs->used_blobids, page_num) == false) {
    8594                 :            :                 /* Invalid blobid */
    8595                 :        289 :                 cb_fn(cb_arg, NULL, -ENOENT);
    8596                 :       1074 :                 return;
    8597                 :            :         }
    8598                 :            : 
    8599                 :      46784 :         blob = blob_lookup(bs, blobid);
    8600         [ +  + ]:      46784 :         if (blob) {
    8601                 :       4725 :                 blob->open_ref++;
    8602                 :       4725 :                 cb_fn(cb_arg, blob, 0);
    8603                 :       4725 :                 return;
    8604                 :            :         }
    8605                 :            : 
    8606                 :      42059 :         blob = blob_alloc(bs, blobid);
    8607         [ -  + ]:      42059 :         if (!blob) {
    8608                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    8609                 :          0 :                 return;
    8610                 :            :         }
    8611                 :            : 
    8612                 :      42059 :         spdk_blob_open_opts_init(&opts_local, sizeof(opts_local));
    8613         [ +  + ]:      42059 :         if (opts) {
    8614                 :        615 :                 blob_open_opts_copy(opts, &opts_local);
    8615                 :            :         }
    8616                 :            : 
    8617                 :      42059 :         blob->clear_method = opts_local.clear_method;
    8618                 :            : 
    8619                 :      42059 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_HANDLE;
    8620                 :      42059 :         cpl.u.blob_handle.cb_fn = cb_fn;
    8621                 :      42059 :         cpl.u.blob_handle.cb_arg = cb_arg;
    8622                 :      42059 :         cpl.u.blob_handle.blob = blob;
    8623                 :      42059 :         cpl.u.blob_handle.esnap_ctx = opts_local.esnap_ctx;
    8624                 :            : 
    8625                 :      42059 :         seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    8626         [ -  + ]:      42059 :         if (!seq) {
    8627                 :          0 :                 blob_free(blob);
    8628                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    8629                 :          0 :                 return;
    8630                 :            :         }
    8631                 :            : 
    8632                 :      42059 :         blob_load(seq, blob, bs_open_blob_cpl, blob);
    8633                 :            : }
    8634                 :            : 
    8635                 :            : void
    8636                 :      46421 : spdk_bs_open_blob(struct spdk_blob_store *bs, spdk_blob_id blobid,
    8637                 :            :                   spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
    8638                 :            : {
    8639                 :      46421 :         bs_open_blob(bs, blobid, NULL, cb_fn, cb_arg);
    8640                 :      46421 : }
    8641                 :            : 
    8642                 :            : void
    8643                 :        652 : spdk_bs_open_blob_ext(struct spdk_blob_store *bs, spdk_blob_id blobid,
    8644                 :            :                       struct spdk_blob_open_opts *opts, spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
    8645                 :            : {
    8646                 :        652 :         bs_open_blob(bs, blobid, opts, cb_fn, cb_arg);
    8647                 :        652 : }
    8648                 :            : 
    8649                 :            : /* END spdk_bs_open_blob */
    8650                 :            : 
    8651                 :            : /* START spdk_blob_set_read_only */
    8652                 :            : int
    8653                 :       1433 : spdk_blob_set_read_only(struct spdk_blob *blob)
    8654                 :            : {
    8655                 :       1433 :         blob_verify_md_op(blob);
    8656                 :            : 
    8657                 :       1433 :         blob->data_ro_flags |= SPDK_BLOB_READ_ONLY;
    8658                 :            : 
    8659                 :       1433 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    8660                 :       1433 :         return 0;
    8661                 :            : }
    8662                 :            : /* END spdk_blob_set_read_only */
    8663                 :            : 
    8664                 :            : /* START spdk_blob_sync_md */
    8665                 :            : 
    8666                 :            : static void
    8667                 :     162429 : blob_sync_md_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    8668                 :            : {
    8669                 :     162429 :         struct spdk_blob *blob = cb_arg;
    8670                 :            : 
    8671   [ +  -  +  + ]:     162429 :         if (bserrno == 0 && (blob->data_ro_flags & SPDK_BLOB_READ_ONLY)) {
    8672                 :       2458 :                 blob->data_ro = true;
    8673                 :       2458 :                 blob->md_ro = true;
    8674                 :            :         }
    8675                 :            : 
    8676                 :     162429 :         bs_sequence_finish(seq, bserrno);
    8677                 :     162429 : }
    8678                 :            : 
    8679                 :            : static void
    8680                 :     162429 : blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
    8681                 :            : {
    8682                 :     160719 :         struct spdk_bs_cpl      cpl;
    8683                 :            :         spdk_bs_sequence_t      *seq;
    8684                 :            : 
    8685                 :     162429 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    8686                 :     162429 :         cpl.u.blob_basic.cb_fn = cb_fn;
    8687                 :     162429 :         cpl.u.blob_basic.cb_arg = cb_arg;
    8688                 :            : 
    8689                 :     162429 :         seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
    8690         [ -  + ]:     162429 :         if (!seq) {
    8691                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    8692                 :          0 :                 return;
    8693                 :            :         }
    8694                 :            : 
    8695                 :     162429 :         blob_persist(seq, blob, blob_sync_md_cpl, blob);
    8696                 :            : }
    8697                 :            : 
    8698                 :            : void
    8699                 :     158407 : spdk_blob_sync_md(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
    8700                 :            : {
    8701                 :     158407 :         blob_verify_md_op(blob);
    8702                 :            : 
    8703   [ -  +  -  + ]:     158407 :         SPDK_DEBUGLOG(blob, "Syncing blob 0x%" PRIx64 "\n", blob->id);
    8704                 :            : 
    8705   [ +  +  +  + ]:     158407 :         if (blob->md_ro) {
    8706         [ -  + ]:         24 :                 assert(blob->state == SPDK_BLOB_STATE_CLEAN);
    8707                 :         24 :                 cb_fn(cb_arg, 0);
    8708                 :         24 :                 return;
    8709                 :            :         }
    8710                 :            : 
    8711                 :     158383 :         blob_sync_md(blob, cb_fn, cb_arg);
    8712                 :            : }
    8713                 :            : 
    8714                 :            : /* END spdk_blob_sync_md */
    8715                 :            : 
    8716                 :            : struct spdk_blob_cluster_op_ctx {
    8717                 :            :         struct spdk_thread      *thread;
    8718                 :            :         struct spdk_blob        *blob;
    8719                 :            :         uint32_t                cluster_num;    /* cluster index in blob */
    8720                 :            :         uint32_t                cluster;        /* cluster on disk */
    8721                 :            :         uint32_t                extent_page;    /* extent page on disk */
    8722                 :            :         struct spdk_blob_md_page *page; /* preallocated extent page */
    8723                 :            :         int                     rc;
    8724                 :            :         spdk_blob_op_complete   cb_fn;
    8725                 :            :         void                    *cb_arg;
    8726                 :            : };
    8727                 :            : 
    8728                 :            : static void
    8729                 :      40131 : blob_op_cluster_msg_cpl(void *arg)
    8730                 :            : {
    8731                 :      40131 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8732                 :            : 
    8733                 :      40131 :         ctx->cb_fn(ctx->cb_arg, ctx->rc);
    8734                 :      40131 :         free(ctx);
    8735                 :      40131 : }
    8736                 :            : 
    8737                 :            : static void
    8738                 :      39938 : blob_op_cluster_msg_cb(void *arg, int bserrno)
    8739                 :            : {
    8740                 :      39938 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8741                 :            : 
    8742                 :      39938 :         ctx->rc = bserrno;
    8743                 :      39938 :         spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
    8744                 :      39938 : }
    8745                 :            : 
    8746                 :            : static void
    8747                 :       1454 : blob_insert_new_ep_cb(void *arg, int bserrno)
    8748                 :            : {
    8749                 :       1454 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8750                 :            :         uint32_t *extent_page;
    8751                 :            : 
    8752                 :       1454 :         extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
    8753                 :       1454 :         *extent_page = ctx->extent_page;
    8754                 :       1454 :         ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
    8755                 :       1454 :         blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
    8756                 :       1454 : }
    8757                 :            : 
    8758                 :            : struct spdk_blob_write_extent_page_ctx {
    8759                 :            :         struct spdk_blob_store          *bs;
    8760                 :            : 
    8761                 :            :         uint32_t                        extent;
    8762                 :            :         struct spdk_blob_md_page        *page;
    8763                 :            : };
    8764                 :            : 
    8765                 :            : static void
    8766                 :        156 : blob_free_cluster_msg_cb(void *arg, int bserrno)
    8767                 :            : {
    8768                 :        156 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8769                 :            : 
    8770                 :        156 :         spdk_spin_lock(&ctx->blob->bs->used_lock);
    8771                 :        156 :         bs_release_cluster(ctx->blob->bs, bs_lba_to_cluster(ctx->blob->bs, ctx->cluster));
    8772                 :        156 :         spdk_spin_unlock(&ctx->blob->bs->used_lock);
    8773                 :            : 
    8774                 :        156 :         ctx->rc = bserrno;
    8775                 :        156 :         spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
    8776                 :        156 : }
    8777                 :            : 
    8778                 :            : static void
    8779                 :        156 : blob_free_cluster_update_ep_cb(void *arg, int bserrno)
    8780                 :            : {
    8781                 :        156 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8782                 :            : 
    8783   [ +  -  +  +  :        156 :         if (bserrno != 0 || ctx->blob->bs->clean == 0) {
                   +  - ]
    8784                 :        156 :                 blob_free_cluster_msg_cb(ctx, bserrno);
    8785                 :        156 :                 return;
    8786                 :            :         }
    8787                 :            : 
    8788                 :          0 :         ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
    8789                 :          0 :         blob_sync_md(ctx->blob, blob_free_cluster_msg_cb, ctx);
    8790                 :            : }
    8791                 :            : 
    8792                 :            : static void
    8793                 :          0 : blob_free_cluster_free_ep_cb(void *arg, int bserrno)
    8794                 :            : {
    8795                 :          0 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8796                 :            : 
    8797                 :          0 :         spdk_spin_lock(&ctx->blob->bs->used_lock);
    8798         [ #  # ]:          0 :         assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
    8799                 :          0 :         bs_release_md_page(ctx->blob->bs, ctx->extent_page);
    8800                 :          0 :         spdk_spin_unlock(&ctx->blob->bs->used_lock);
    8801                 :          0 :         ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
    8802                 :          0 :         blob_sync_md(ctx->blob, blob_free_cluster_msg_cb, ctx);
    8803                 :          0 : }
    8804                 :            : 
    8805                 :            : static void
    8806                 :      37470 : blob_persist_extent_page_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    8807                 :            : {
    8808                 :      37470 :         struct spdk_blob_write_extent_page_ctx *ctx = cb_arg;
    8809                 :            : 
    8810                 :      37470 :         free(ctx);
    8811                 :      37470 :         bs_sequence_finish(seq, bserrno);
    8812                 :      37470 : }
    8813                 :            : 
    8814                 :            : static void
    8815                 :      37470 : blob_write_extent_page_ready(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    8816                 :            : {
    8817                 :      37470 :         struct spdk_blob_write_extent_page_ctx *ctx = cb_arg;
    8818                 :            : 
    8819         [ -  + ]:      37470 :         if (bserrno != 0) {
    8820                 :          0 :                 blob_persist_extent_page_cpl(seq, ctx, bserrno);
    8821                 :          0 :                 return;
    8822                 :            :         }
    8823                 :      37470 :         bs_sequence_write_dev(seq, ctx->page, bs_md_page_to_lba(ctx->bs, ctx->extent),
    8824                 :      37470 :                               bs_byte_to_lba(ctx->bs, SPDK_BS_PAGE_SIZE),
    8825                 :            :                               blob_persist_extent_page_cpl, ctx);
    8826                 :            : }
    8827                 :            : 
    8828                 :            : static void
    8829                 :      37470 : blob_write_extent_page(struct spdk_blob *blob, uint32_t extent, uint64_t cluster_num,
    8830                 :            :                        struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg)
    8831                 :            : {
    8832                 :            :         struct spdk_blob_write_extent_page_ctx  *ctx;
    8833                 :            :         spdk_bs_sequence_t                      *seq;
    8834                 :      36889 :         struct spdk_bs_cpl                      cpl;
    8835                 :            : 
    8836                 :      37470 :         ctx = calloc(1, sizeof(*ctx));
    8837         [ -  + ]:      37470 :         if (!ctx) {
    8838                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    8839                 :          0 :                 return;
    8840                 :            :         }
    8841                 :      37470 :         ctx->bs = blob->bs;
    8842                 :      37470 :         ctx->extent = extent;
    8843                 :      37470 :         ctx->page = page;
    8844                 :            : 
    8845                 :      37470 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    8846                 :      37470 :         cpl.u.blob_basic.cb_fn = cb_fn;
    8847                 :      37470 :         cpl.u.blob_basic.cb_arg = cb_arg;
    8848                 :            : 
    8849                 :      37470 :         seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
    8850         [ -  + ]:      37470 :         if (!seq) {
    8851                 :          0 :                 free(ctx);
    8852                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    8853                 :          0 :                 return;
    8854                 :            :         }
    8855                 :            : 
    8856         [ -  + ]:      37470 :         assert(page);
    8857                 :      37470 :         page->next = SPDK_INVALID_MD_PAGE;
    8858                 :      37470 :         page->id = blob->id;
    8859                 :      37470 :         page->sequence_num = 0;
    8860                 :            : 
    8861                 :      37470 :         blob_serialize_extent_page(blob, cluster_num, page);
    8862                 :            : 
    8863                 :      37470 :         page->crc = blob_md_page_calc_crc(page);
    8864                 :            : 
    8865         [ -  + ]:      37470 :         assert(spdk_bit_array_get(blob->bs->used_md_pages, extent) == true);
    8866                 :            : 
    8867                 :      37470 :         bs_mark_dirty(seq, blob->bs, blob_write_extent_page_ready, ctx);
    8868                 :            : }
    8869                 :            : 
    8870                 :            : static void
    8871                 :      39771 : blob_insert_cluster_msg(void *arg)
    8872                 :            : {
    8873                 :      39771 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8874                 :            :         uint32_t *extent_page;
    8875                 :            : 
    8876                 :      39771 :         ctx->rc = blob_insert_cluster(ctx->blob, ctx->cluster_num, ctx->cluster);
    8877         [ +  + ]:      39771 :         if (ctx->rc != 0) {
    8878                 :         37 :                 spdk_thread_send_msg(ctx->thread, blob_op_cluster_msg_cpl, ctx);
    8879                 :         37 :                 return;
    8880                 :            :         }
    8881                 :            : 
    8882   [ +  +  +  + ]:      39734 :         if (ctx->blob->use_extent_table == false) {
    8883                 :            :                 /* Extent table is not used, proceed with sync of md that will only use extents_rle. */
    8884                 :       2436 :                 ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
    8885                 :       2436 :                 blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
    8886                 :       2436 :                 return;
    8887                 :            :         }
    8888                 :            : 
    8889                 :      37298 :         extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
    8890         [ +  + ]:      37298 :         if (*extent_page == 0) {
    8891                 :            :                 /* Extent page requires allocation.
    8892                 :            :                  * It was already claimed in the used_md_pages map and placed in ctx. */
    8893         [ -  + ]:       1454 :                 assert(ctx->extent_page != 0);
    8894         [ -  + ]:       1454 :                 assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
    8895                 :       1454 :                 blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, ctx->page,
    8896                 :            :                                        blob_insert_new_ep_cb, ctx);
    8897                 :            :         } else {
    8898                 :            :                 /* It is possible for original thread to allocate extent page for
    8899                 :            :                  * different cluster in the same extent page. In such case proceed with
    8900                 :            :                  * updating the existing extent page, but release the additional one. */
    8901         [ +  + ]:      35844 :                 if (ctx->extent_page != 0) {
    8902                 :          3 :                         spdk_spin_lock(&ctx->blob->bs->used_lock);
    8903         [ -  + ]:          3 :                         assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
    8904                 :          3 :                         bs_release_md_page(ctx->blob->bs, ctx->extent_page);
    8905                 :          3 :                         spdk_spin_unlock(&ctx->blob->bs->used_lock);
    8906                 :          3 :                         ctx->extent_page = 0;
    8907                 :            :                 }
    8908                 :            :                 /* Extent page already allocated.
    8909                 :            :                  * Every cluster allocation, requires just an update of single extent page. */
    8910                 :      35844 :                 blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, ctx->page,
    8911                 :            :                                        blob_op_cluster_msg_cb, ctx);
    8912                 :            :         }
    8913                 :            : }
    8914                 :            : 
    8915                 :            : static void
    8916                 :      39771 : blob_insert_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num,
    8917                 :            :                                  uint64_t cluster, uint32_t extent_page, struct spdk_blob_md_page *page,
    8918                 :            :                                  spdk_blob_op_complete cb_fn, void *cb_arg)
    8919                 :            : {
    8920                 :            :         struct spdk_blob_cluster_op_ctx *ctx;
    8921                 :            : 
    8922                 :      39771 :         ctx = calloc(1, sizeof(*ctx));
    8923         [ -  + ]:      39771 :         if (ctx == NULL) {
    8924                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    8925                 :          0 :                 return;
    8926                 :            :         }
    8927                 :            : 
    8928                 :      39771 :         ctx->thread = spdk_get_thread();
    8929                 :      39771 :         ctx->blob = blob;
    8930                 :      39771 :         ctx->cluster_num = cluster_num;
    8931                 :      39771 :         ctx->cluster = cluster;
    8932                 :      39771 :         ctx->extent_page = extent_page;
    8933                 :      39771 :         ctx->page = page;
    8934                 :      39771 :         ctx->cb_fn = cb_fn;
    8935                 :      39771 :         ctx->cb_arg = cb_arg;
    8936                 :            : 
    8937                 :      39771 :         spdk_thread_send_msg(blob->bs->md_thread, blob_insert_cluster_msg, ctx);
    8938                 :            : }
    8939                 :            : 
    8940                 :            : static void
    8941                 :        360 : blob_free_cluster_msg(void *arg)
    8942                 :            : {
    8943                 :        360 :         struct spdk_blob_cluster_op_ctx *ctx = arg;
    8944                 :            :         uint32_t *extent_page;
    8945                 :            :         uint32_t start_cluster_idx;
    8946                 :        360 :         bool free_extent_page = true;
    8947                 :            :         size_t i;
    8948                 :            : 
    8949                 :        360 :         ctx->cluster = ctx->blob->active.clusters[ctx->cluster_num];
    8950                 :            : 
    8951                 :            :         /* There were concurrent unmaps to the same cluster, only release the cluster on the first one */
    8952         [ +  + ]:        360 :         if (ctx->cluster == 0) {
    8953                 :         48 :                 blob_op_cluster_msg_cb(ctx, 0);
    8954                 :         48 :                 return;
    8955                 :            :         }
    8956                 :            : 
    8957                 :        312 :         ctx->blob->active.clusters[ctx->cluster_num] = 0;
    8958         [ +  - ]:        312 :         if (ctx->cluster != 0) {
    8959                 :        312 :                 ctx->blob->active.num_allocated_clusters--;
    8960                 :            :         }
    8961                 :            : 
    8962   [ +  +  +  + ]:        312 :         if (ctx->blob->use_extent_table == false) {
    8963                 :            :                 /* Extent table is not used, proceed with sync of md that will only use extents_rle. */
    8964                 :        156 :                 spdk_spin_lock(&ctx->blob->bs->used_lock);
    8965                 :        156 :                 bs_release_cluster(ctx->blob->bs, bs_lba_to_cluster(ctx->blob->bs, ctx->cluster));
    8966                 :        156 :                 spdk_spin_unlock(&ctx->blob->bs->used_lock);
    8967                 :        156 :                 ctx->blob->state = SPDK_BLOB_STATE_DIRTY;
    8968                 :        156 :                 blob_sync_md(ctx->blob, blob_op_cluster_msg_cb, ctx);
    8969                 :        156 :                 return;
    8970                 :            :         }
    8971                 :            : 
    8972                 :        156 :         extent_page = bs_cluster_to_extent_page(ctx->blob, ctx->cluster_num);
    8973                 :            : 
    8974                 :            :         /* There shouldn't be parallel release operations on same cluster */
    8975         [ -  + ]:        156 :         assert(*extent_page == ctx->extent_page);
    8976                 :            : 
    8977         [ -  + ]:        156 :         start_cluster_idx = (ctx->cluster_num / SPDK_EXTENTS_PER_EP) * SPDK_EXTENTS_PER_EP;
    8978         [ +  - ]:        288 :         for (i = 0; i < SPDK_EXTENTS_PER_EP; ++i) {
    8979         [ +  + ]:        288 :                 if (ctx->blob->active.clusters[start_cluster_idx + i] != 0) {
    8980                 :        156 :                         free_extent_page = false;
    8981                 :        156 :                         break;
    8982                 :            :                 }
    8983                 :            :         }
    8984                 :            : 
    8985         [ -  + ]:        156 :         if (free_extent_page) {
    8986         [ #  # ]:          0 :                 assert(ctx->extent_page != 0);
    8987         [ #  # ]:          0 :                 assert(spdk_bit_array_get(ctx->blob->bs->used_md_pages, ctx->extent_page) == true);
    8988                 :          0 :                 ctx->blob->active.extent_pages[bs_cluster_to_extent_table_id(ctx->cluster_num)] = 0;
    8989                 :          0 :                 blob_write_extent_page(ctx->blob, ctx->extent_page, ctx->cluster_num, ctx->page,
    8990                 :            :                                        blob_free_cluster_free_ep_cb, ctx);
    8991                 :            :         } else {
    8992                 :        156 :                 blob_write_extent_page(ctx->blob, *extent_page, ctx->cluster_num, ctx->page,
    8993                 :            :                                        blob_free_cluster_update_ep_cb, ctx);
    8994                 :            :         }
    8995                 :            : }
    8996                 :            : 
    8997                 :            : 
    8998                 :            : static void
    8999                 :        360 : blob_free_cluster_on_md_thread(struct spdk_blob *blob, uint32_t cluster_num, uint32_t extent_page,
    9000                 :            :                                struct spdk_blob_md_page *page, spdk_blob_op_complete cb_fn, void *cb_arg)
    9001                 :            : {
    9002                 :            :         struct spdk_blob_cluster_op_ctx *ctx;
    9003                 :            : 
    9004                 :        360 :         ctx = calloc(1, sizeof(*ctx));
    9005         [ -  + ]:        360 :         if (ctx == NULL) {
    9006                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    9007                 :          0 :                 return;
    9008                 :            :         }
    9009                 :            : 
    9010                 :        360 :         ctx->thread = spdk_get_thread();
    9011                 :        360 :         ctx->blob = blob;
    9012                 :        360 :         ctx->cluster_num = cluster_num;
    9013                 :        360 :         ctx->extent_page = extent_page;
    9014                 :        360 :         ctx->page = page;
    9015                 :        360 :         ctx->cb_fn = cb_fn;
    9016                 :        360 :         ctx->cb_arg = cb_arg;
    9017                 :            : 
    9018                 :        360 :         spdk_thread_send_msg(blob->bs->md_thread, blob_free_cluster_msg, ctx);
    9019                 :            : }
    9020                 :            : 
    9021                 :            : /* START spdk_blob_close */
    9022                 :            : 
    9023                 :            : static void
    9024                 :      46400 : blob_close_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9025                 :            : {
    9026                 :      46400 :         struct spdk_blob *blob = cb_arg;
    9027                 :            : 
    9028         [ +  - ]:      46400 :         if (bserrno == 0) {
    9029                 :      46400 :                 blob->open_ref--;
    9030         [ +  + ]:      46400 :                 if (blob->open_ref == 0) {
    9031                 :            :                         /*
    9032                 :            :                          * Blobs with active.num_pages == 0 are deleted blobs.
    9033                 :            :                          *  these blobs are removed from the blob_store list
    9034                 :            :                          *  when the deletion process starts - so don't try to
    9035                 :            :                          *  remove them again.
    9036                 :            :                          */
    9037         [ +  + ]:      41638 :                         if (blob->active.num_pages > 0) {
    9038                 :      31068 :                                 spdk_bit_array_clear(blob->bs->open_blobids, blob->id);
    9039                 :      31068 :                                 RB_REMOVE(spdk_blob_tree, &blob->bs->open_blobs, blob);
    9040                 :            :                         }
    9041                 :      41638 :                         blob_free(blob);
    9042                 :            :                 }
    9043                 :            :         }
    9044                 :            : 
    9045                 :      46400 :         bs_sequence_finish(seq, bserrno);
    9046                 :      46400 : }
    9047                 :            : 
    9048                 :            : static void
    9049                 :        750 : blob_close_esnap_done(void *cb_arg, struct spdk_blob *blob, int bserrno)
    9050                 :            : {
    9051                 :        750 :         spdk_bs_sequence_t      *seq = cb_arg;
    9052                 :            : 
    9053         [ -  + ]:        750 :         if (bserrno != 0) {
    9054   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": close failed with error %d\n",
    9055                 :            :                               blob->id, bserrno);
    9056                 :          0 :                 bs_sequence_finish(seq, bserrno);
    9057                 :          0 :                 return;
    9058                 :            :         }
    9059                 :            : 
    9060   [ -  +  -  + ]:        750 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": closed, syncing metadata on thread %s\n",
    9061                 :            :                       blob->id, spdk_thread_get_name(spdk_get_thread()));
    9062                 :            : 
    9063                 :            :         /* Sync metadata */
    9064                 :        750 :         blob_persist(seq, blob, blob_close_cpl, blob);
    9065                 :            : }
    9066                 :            : 
    9067                 :            : void
    9068                 :      46400 : spdk_blob_close(struct spdk_blob *blob, spdk_blob_op_complete cb_fn, void *cb_arg)
    9069                 :            : {
    9070                 :      41578 :         struct spdk_bs_cpl      cpl;
    9071                 :            :         spdk_bs_sequence_t      *seq;
    9072                 :            : 
    9073                 :      46400 :         blob_verify_md_op(blob);
    9074                 :            : 
    9075   [ -  +  -  + ]:      46400 :         SPDK_DEBUGLOG(blob, "Closing blob 0x%" PRIx64 "\n", blob->id);
    9076                 :            : 
    9077         [ -  + ]:      46400 :         if (blob->open_ref == 0) {
    9078                 :          0 :                 cb_fn(cb_arg, -EBADF);
    9079                 :        112 :                 return;
    9080                 :            :         }
    9081                 :            : 
    9082                 :      46400 :         cpl.type = SPDK_BS_CPL_TYPE_BLOB_BASIC;
    9083                 :      46400 :         cpl.u.blob_basic.cb_fn = cb_fn;
    9084                 :      46400 :         cpl.u.blob_basic.cb_arg = cb_arg;
    9085                 :            : 
    9086                 :      46400 :         seq = bs_sequence_start_bs(blob->bs->md_channel, &cpl);
    9087         [ -  + ]:      46400 :         if (!seq) {
    9088                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    9089                 :          0 :                 return;
    9090                 :            :         }
    9091                 :            : 
    9092   [ +  +  +  + ]:      46400 :         if (blob->open_ref == 1 && blob_is_esnap_clone(blob)) {
    9093                 :        750 :                 blob_esnap_destroy_bs_dev_channels(blob, false, blob_close_esnap_done, seq);
    9094                 :        750 :                 return;
    9095                 :            :         }
    9096                 :            : 
    9097                 :            :         /* Sync metadata */
    9098                 :      45650 :         blob_persist(seq, blob, blob_close_cpl, blob);
    9099                 :            : }
    9100                 :            : 
    9101                 :            : /* END spdk_blob_close */
    9102                 :            : 
    9103                 :       2612 : struct spdk_io_channel *spdk_bs_alloc_io_channel(struct spdk_blob_store *bs)
    9104                 :            : {
    9105                 :       2612 :         return spdk_get_io_channel(bs);
    9106                 :            : }
    9107                 :            : 
    9108                 :            : void
    9109                 :       1419 : spdk_bs_free_io_channel(struct spdk_io_channel *channel)
    9110                 :            : {
    9111                 :       1419 :         blob_esnap_destroy_bs_channel(spdk_io_channel_get_ctx(channel));
    9112                 :       1419 :         spdk_put_io_channel(channel);
    9113                 :       1419 : }
    9114                 :            : 
    9115                 :            : void
    9116                 :     196357 : spdk_blob_io_unmap(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9117                 :            :                    uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
    9118                 :            : {
    9119                 :     196357 :         blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
    9120                 :            :                                SPDK_BLOB_UNMAP);
    9121                 :     196357 : }
    9122                 :            : 
    9123                 :            : void
    9124                 :       2031 : spdk_blob_io_write_zeroes(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9125                 :            :                           uint64_t offset, uint64_t length, spdk_blob_op_complete cb_fn, void *cb_arg)
    9126                 :            : {
    9127                 :       2031 :         blob_request_submit_op(blob, channel, NULL, offset, length, cb_fn, cb_arg,
    9128                 :            :                                SPDK_BLOB_WRITE_ZEROES);
    9129                 :       2031 : }
    9130                 :            : 
    9131                 :            : void
    9132                 :     976525 : spdk_blob_io_write(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9133                 :            :                    void *payload, uint64_t offset, uint64_t length,
    9134                 :            :                    spdk_blob_op_complete cb_fn, void *cb_arg)
    9135                 :            : {
    9136                 :     976525 :         blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
    9137                 :            :                                SPDK_BLOB_WRITE);
    9138                 :     976525 : }
    9139                 :            : 
    9140                 :            : void
    9141                 :    4789391 : spdk_blob_io_read(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9142                 :            :                   void *payload, uint64_t offset, uint64_t length,
    9143                 :            :                   spdk_blob_op_complete cb_fn, void *cb_arg)
    9144                 :            : {
    9145                 :    4789391 :         blob_request_submit_op(blob, channel, payload, offset, length, cb_fn, cb_arg,
    9146                 :            :                                SPDK_BLOB_READ);
    9147                 :    4789391 : }
    9148                 :            : 
    9149                 :            : void
    9150                 :        840 : spdk_blob_io_writev(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9151                 :            :                     struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
    9152                 :            :                     spdk_blob_op_complete cb_fn, void *cb_arg)
    9153                 :            : {
    9154                 :        840 :         blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false, NULL);
    9155                 :        840 : }
    9156                 :            : 
    9157                 :            : void
    9158                 :       5640 : spdk_blob_io_readv(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9159                 :            :                    struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
    9160                 :            :                    spdk_blob_op_complete cb_fn, void *cb_arg)
    9161                 :            : {
    9162                 :       5640 :         blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true, NULL);
    9163                 :       5640 : }
    9164                 :            : 
    9165                 :            : void
    9166                 :    8705206 : spdk_blob_io_writev_ext(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9167                 :            :                         struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
    9168                 :            :                         spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
    9169                 :            : {
    9170                 :    8705206 :         blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, false,
    9171                 :            :                                    io_opts);
    9172                 :    8705206 : }
    9173                 :            : 
    9174                 :            : void
    9175                 :    7574074 : spdk_blob_io_readv_ext(struct spdk_blob *blob, struct spdk_io_channel *channel,
    9176                 :            :                        struct iovec *iov, int iovcnt, uint64_t offset, uint64_t length,
    9177                 :            :                        spdk_blob_op_complete cb_fn, void *cb_arg, struct spdk_blob_ext_io_opts *io_opts)
    9178                 :            : {
    9179                 :    7574074 :         blob_request_submit_rw_iov(blob, channel, iov, iovcnt, offset, length, cb_fn, cb_arg, true,
    9180                 :            :                                    io_opts);
    9181                 :    7574074 : }
    9182                 :            : 
    9183                 :            : struct spdk_bs_iter_ctx {
    9184                 :            :         int64_t page_num;
    9185                 :            :         struct spdk_blob_store *bs;
    9186                 :            : 
    9187                 :            :         spdk_blob_op_with_handle_complete cb_fn;
    9188                 :            :         void *cb_arg;
    9189                 :            : };
    9190                 :            : 
    9191                 :            : static void
    9192                 :      21371 : bs_iter_cpl(void *cb_arg, struct spdk_blob *_blob, int bserrno)
    9193                 :            : {
    9194                 :      21371 :         struct spdk_bs_iter_ctx *ctx = cb_arg;
    9195                 :      21371 :         struct spdk_blob_store *bs = ctx->bs;
    9196                 :            :         spdk_blob_id id;
    9197                 :            : 
    9198         [ +  + ]:      21371 :         if (bserrno == 0) {
    9199                 :       9776 :                 ctx->cb_fn(ctx->cb_arg, _blob, bserrno);
    9200                 :       9776 :                 free(ctx);
    9201                 :       9776 :                 return;
    9202                 :            :         }
    9203                 :            : 
    9204                 :      11595 :         ctx->page_num++;
    9205                 :      11595 :         ctx->page_num = spdk_bit_array_find_first_set(bs->used_blobids, ctx->page_num);
    9206         [ +  + ]:      11595 :         if (ctx->page_num >= spdk_bit_array_capacity(bs->used_blobids)) {
    9207                 :       1771 :                 ctx->cb_fn(ctx->cb_arg, NULL, -ENOENT);
    9208                 :       1771 :                 free(ctx);
    9209                 :       1771 :                 return;
    9210                 :            :         }
    9211                 :            : 
    9212                 :       9824 :         id = bs_page_to_blobid(ctx->page_num);
    9213                 :            : 
    9214                 :       9824 :         spdk_bs_open_blob(bs, id, bs_iter_cpl, ctx);
    9215                 :            : }
    9216                 :            : 
    9217                 :            : void
    9218                 :       1915 : spdk_bs_iter_first(struct spdk_blob_store *bs,
    9219                 :            :                    spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
    9220                 :            : {
    9221                 :            :         struct spdk_bs_iter_ctx *ctx;
    9222                 :            : 
    9223                 :       1915 :         ctx = calloc(1, sizeof(*ctx));
    9224         [ -  + ]:       1915 :         if (!ctx) {
    9225                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    9226                 :          0 :                 return;
    9227                 :            :         }
    9228                 :            : 
    9229                 :       1915 :         ctx->page_num = -1;
    9230                 :       1915 :         ctx->bs = bs;
    9231                 :       1915 :         ctx->cb_fn = cb_fn;
    9232                 :       1915 :         ctx->cb_arg = cb_arg;
    9233                 :            : 
    9234                 :       1915 :         bs_iter_cpl(ctx, NULL, -1);
    9235                 :            : }
    9236                 :            : 
    9237                 :            : static void
    9238                 :       9632 : bs_iter_close_cpl(void *cb_arg, int bserrno)
    9239                 :            : {
    9240                 :       9632 :         struct spdk_bs_iter_ctx *ctx = cb_arg;
    9241                 :            : 
    9242                 :       9632 :         bs_iter_cpl(ctx, NULL, -1);
    9243                 :       9632 : }
    9244                 :            : 
    9245                 :            : void
    9246                 :       9632 : spdk_bs_iter_next(struct spdk_blob_store *bs, struct spdk_blob *blob,
    9247                 :            :                   spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
    9248                 :            : {
    9249                 :            :         struct spdk_bs_iter_ctx *ctx;
    9250                 :            : 
    9251         [ -  + ]:       9632 :         assert(blob != NULL);
    9252                 :            : 
    9253                 :       9632 :         ctx = calloc(1, sizeof(*ctx));
    9254         [ -  + ]:       9632 :         if (!ctx) {
    9255                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    9256                 :          0 :                 return;
    9257                 :            :         }
    9258                 :            : 
    9259                 :       9632 :         ctx->page_num = bs_blobid_to_page(blob->id);
    9260                 :       9632 :         ctx->bs = bs;
    9261                 :       9632 :         ctx->cb_fn = cb_fn;
    9262                 :       9632 :         ctx->cb_arg = cb_arg;
    9263                 :            : 
    9264                 :            :         /* Close the existing blob */
    9265                 :       9632 :         spdk_blob_close(blob, bs_iter_close_cpl, ctx);
    9266                 :            : }
    9267                 :            : 
    9268                 :            : static int
    9269                 :      53118 : blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
    9270                 :            :                uint16_t value_len, bool internal)
    9271                 :            : {
    9272                 :            :         struct spdk_xattr_tailq *xattrs;
    9273                 :            :         struct spdk_xattr       *xattr;
    9274                 :            :         size_t                  desc_size;
    9275                 :            :         void                    *tmp;
    9276                 :            : 
    9277                 :      53118 :         blob_verify_md_op(blob);
    9278                 :            : 
    9279   [ +  +  +  + ]:      53118 :         if (blob->md_ro) {
    9280                 :         24 :                 return -EPERM;
    9281                 :            :         }
    9282                 :            : 
    9283         [ -  + ]:      53094 :         desc_size = sizeof(struct spdk_blob_md_descriptor_xattr) + strlen(name) + value_len;
    9284         [ +  + ]:      53094 :         if (desc_size > SPDK_BS_MAX_DESC_SIZE) {
    9285   [ -  +  -  + ]:         24 :                 SPDK_DEBUGLOG(blob, "Xattr '%s' of size %zu does not fix into single page %zu\n", name,
    9286                 :            :                               desc_size, SPDK_BS_MAX_DESC_SIZE);
    9287                 :         24 :                 return -ENOMEM;
    9288                 :            :         }
    9289                 :            : 
    9290         [ +  + ]:      53070 :         if (internal) {
    9291                 :       4479 :                 xattrs = &blob->xattrs_internal;
    9292                 :       4479 :                 blob->invalid_flags |= SPDK_BLOB_INTERNAL_XATTR;
    9293                 :            :         } else {
    9294                 :      48591 :                 xattrs = &blob->xattrs;
    9295                 :            :         }
    9296                 :            : 
    9297         [ +  + ]:      98352 :         TAILQ_FOREACH(xattr, xattrs, link) {
    9298   [ +  +  -  +  :      86504 :                 if (!strcmp(name, xattr->name)) {
                   +  + ]
    9299                 :      41222 :                         tmp = malloc(value_len);
    9300         [ -  + ]:      41222 :                         if (!tmp) {
    9301                 :          0 :                                 return -ENOMEM;
    9302                 :            :                         }
    9303                 :            : 
    9304                 :      41222 :                         free(xattr->value);
    9305                 :      41222 :                         xattr->value_len = value_len;
    9306                 :      41222 :                         xattr->value = tmp;
    9307   [ -  +  -  + ]:      41222 :                         memcpy(xattr->value, value, value_len);
    9308                 :            : 
    9309                 :      41222 :                         blob->state = SPDK_BLOB_STATE_DIRTY;
    9310                 :            : 
    9311                 :      41222 :                         return 0;
    9312                 :            :                 }
    9313                 :            :         }
    9314                 :            : 
    9315                 :      11848 :         xattr = calloc(1, sizeof(*xattr));
    9316         [ -  + ]:      11848 :         if (!xattr) {
    9317                 :          0 :                 return -ENOMEM;
    9318                 :            :         }
    9319                 :            : 
    9320         [ -  + ]:      11848 :         xattr->name = strdup(name);
    9321         [ -  + ]:      11848 :         if (!xattr->name) {
    9322                 :          0 :                 free(xattr);
    9323                 :          0 :                 return -ENOMEM;
    9324                 :            :         }
    9325                 :            : 
    9326                 :      11848 :         xattr->value_len = value_len;
    9327                 :      11848 :         xattr->value = malloc(value_len);
    9328         [ -  + ]:      11848 :         if (!xattr->value) {
    9329                 :          0 :                 free(xattr->name);
    9330                 :          0 :                 free(xattr);
    9331                 :          0 :                 return -ENOMEM;
    9332                 :            :         }
    9333   [ -  +  -  + ]:      11848 :         memcpy(xattr->value, value, value_len);
    9334                 :      11848 :         TAILQ_INSERT_TAIL(xattrs, xattr, link);
    9335                 :            : 
    9336                 :      11848 :         blob->state = SPDK_BLOB_STATE_DIRTY;
    9337                 :            : 
    9338                 :      11848 :         return 0;
    9339                 :            : }
    9340                 :            : 
    9341                 :            : int
    9342                 :      47357 : spdk_blob_set_xattr(struct spdk_blob *blob, const char *name, const void *value,
    9343                 :            :                     uint16_t value_len)
    9344                 :            : {
    9345                 :      47357 :         return blob_set_xattr(blob, name, value, value_len, false);
    9346                 :            : }
    9347                 :            : 
    9348                 :            : static int
    9349                 :       2481 : blob_remove_xattr(struct spdk_blob *blob, const char *name, bool internal)
    9350                 :            : {
    9351                 :            :         struct spdk_xattr_tailq *xattrs;
    9352                 :            :         struct spdk_xattr       *xattr;
    9353                 :            : 
    9354                 :       2481 :         blob_verify_md_op(blob);
    9355                 :            : 
    9356   [ +  +  +  + ]:       2481 :         if (blob->md_ro) {
    9357                 :         24 :                 return -EPERM;
    9358                 :            :         }
    9359         [ +  + ]:       2457 :         xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
    9360                 :            : 
    9361         [ +  + ]:       2530 :         TAILQ_FOREACH(xattr, xattrs, link) {
    9362   [ +  +  -  +  :       2213 :                 if (!strcmp(name, xattr->name)) {
                   +  + ]
    9363         [ +  + ]:       2140 :                         TAILQ_REMOVE(xattrs, xattr, link);
    9364                 :       2140 :                         free(xattr->value);
    9365                 :       2140 :                         free(xattr->name);
    9366                 :       2140 :                         free(xattr);
    9367                 :            : 
    9368   [ +  +  +  + ]:       2140 :                         if (internal && TAILQ_EMPTY(&blob->xattrs_internal)) {
    9369                 :       1477 :                                 blob->invalid_flags &= ~SPDK_BLOB_INTERNAL_XATTR;
    9370                 :            :                         }
    9371                 :       2140 :                         blob->state = SPDK_BLOB_STATE_DIRTY;
    9372                 :            : 
    9373                 :       2140 :                         return 0;
    9374                 :            :                 }
    9375                 :            :         }
    9376                 :            : 
    9377                 :        317 :         return -ENOENT;
    9378                 :            : }
    9379                 :            : 
    9380                 :            : int
    9381                 :        217 : spdk_blob_remove_xattr(struct spdk_blob *blob, const char *name)
    9382                 :            : {
    9383                 :        217 :         return blob_remove_xattr(blob, name, false);
    9384                 :            : }
    9385                 :            : 
    9386                 :            : static int
    9387                 :      44313 : blob_get_xattr_value(struct spdk_blob *blob, const char *name,
    9388                 :            :                      const void **value, size_t *value_len, bool internal)
    9389                 :            : {
    9390                 :            :         struct spdk_xattr       *xattr;
    9391                 :            :         struct spdk_xattr_tailq *xattrs;
    9392                 :            : 
    9393         [ +  + ]:      44313 :         xattrs = internal ? &blob->xattrs_internal : &blob->xattrs;
    9394                 :            : 
    9395         [ +  + ]:      64304 :         TAILQ_FOREACH(xattr, xattrs, link) {
    9396   [ +  +  -  +  :      35741 :                 if (!strcmp(name, xattr->name)) {
                   +  + ]
    9397                 :      15750 :                         *value = xattr->value;
    9398                 :      15750 :                         *value_len = xattr->value_len;
    9399                 :      15750 :                         return 0;
    9400                 :            :                 }
    9401                 :            :         }
    9402                 :      28563 :         return -ENOENT;
    9403                 :            : }
    9404                 :            : 
    9405                 :            : int
    9406                 :      17242 : spdk_blob_get_xattr_value(struct spdk_blob *blob, const char *name,
    9407                 :            :                           const void **value, size_t *value_len)
    9408                 :            : {
    9409                 :      17242 :         blob_verify_md_op(blob);
    9410                 :            : 
    9411                 :      17242 :         return blob_get_xattr_value(blob, name, value, value_len, false);
    9412                 :            : }
    9413                 :            : 
    9414                 :            : struct spdk_xattr_names {
    9415                 :            :         uint32_t        count;
    9416                 :            :         const char      *names[0];
    9417                 :            : };
    9418                 :            : 
    9419                 :            : static int
    9420                 :         25 : blob_get_xattr_names(struct spdk_xattr_tailq *xattrs, struct spdk_xattr_names **names)
    9421                 :            : {
    9422                 :            :         struct spdk_xattr       *xattr;
    9423                 :         25 :         int                     count = 0;
    9424                 :            : 
    9425         [ +  + ]:         74 :         TAILQ_FOREACH(xattr, xattrs, link) {
    9426                 :         49 :                 count++;
    9427                 :            :         }
    9428                 :            : 
    9429                 :         25 :         *names = calloc(1, sizeof(struct spdk_xattr_names) + count * sizeof(char *));
    9430         [ -  + ]:         25 :         if (*names == NULL) {
    9431                 :          0 :                 return -ENOMEM;
    9432                 :            :         }
    9433                 :            : 
    9434         [ +  + ]:         74 :         TAILQ_FOREACH(xattr, xattrs, link) {
    9435                 :         49 :                 (*names)->names[(*names)->count++] = xattr->name;
    9436                 :            :         }
    9437                 :            : 
    9438                 :         25 :         return 0;
    9439                 :            : }
    9440                 :            : 
    9441                 :            : int
    9442                 :         25 : spdk_blob_get_xattr_names(struct spdk_blob *blob, struct spdk_xattr_names **names)
    9443                 :            : {
    9444                 :         25 :         blob_verify_md_op(blob);
    9445                 :            : 
    9446                 :         25 :         return blob_get_xattr_names(&blob->xattrs, names);
    9447                 :            : }
    9448                 :            : 
    9449                 :            : uint32_t
    9450                 :         27 : spdk_xattr_names_get_count(struct spdk_xattr_names *names)
    9451                 :            : {
    9452         [ -  + ]:         27 :         assert(names != NULL);
    9453                 :            : 
    9454                 :         27 :         return names->count;
    9455                 :            : }
    9456                 :            : 
    9457                 :            : const char *
    9458                 :         50 : spdk_xattr_names_get_name(struct spdk_xattr_names *names, uint32_t index)
    9459                 :            : {
    9460         [ -  + ]:         50 :         if (index >= names->count) {
    9461                 :          0 :                 return NULL;
    9462                 :            :         }
    9463                 :            : 
    9464                 :         50 :         return names->names[index];
    9465                 :            : }
    9466                 :            : 
    9467                 :            : void
    9468                 :         25 : spdk_xattr_names_free(struct spdk_xattr_names *names)
    9469                 :            : {
    9470                 :         25 :         free(names);
    9471                 :         25 : }
    9472                 :            : 
    9473                 :            : struct spdk_bs_type
    9474                 :         23 : spdk_bs_get_bstype(struct spdk_blob_store *bs)
    9475                 :            : {
    9476                 :         23 :         return bs->bstype;
    9477                 :            : }
    9478                 :            : 
    9479                 :            : void
    9480                 :          0 : spdk_bs_set_bstype(struct spdk_blob_store *bs, struct spdk_bs_type bstype)
    9481                 :            : {
    9482                 :          0 :         memcpy(&bs->bstype, &bstype, sizeof(bstype));
    9483                 :          0 : }
    9484                 :            : 
    9485                 :            : bool
    9486                 :      12383 : spdk_blob_is_read_only(struct spdk_blob *blob)
    9487                 :            : {
    9488         [ -  + ]:      12383 :         assert(blob != NULL);
    9489   [ +  +  +  +  :      12383 :         return (blob->data_ro || blob->md_ro);
             -  +  -  + ]
    9490                 :            : }
    9491                 :            : 
    9492                 :            : bool
    9493                 :       2735 : spdk_blob_is_snapshot(struct spdk_blob *blob)
    9494                 :            : {
    9495                 :            :         struct spdk_blob_list *snapshot_entry;
    9496                 :            : 
    9497         [ -  + ]:       2735 :         assert(blob != NULL);
    9498                 :            : 
    9499                 :       2735 :         snapshot_entry = bs_get_snapshot_entry(blob->bs, blob->id);
    9500         [ +  + ]:       2735 :         if (snapshot_entry == NULL) {
    9501                 :       2510 :                 return false;
    9502                 :            :         }
    9503                 :            : 
    9504                 :        225 :         return true;
    9505                 :            : }
    9506                 :            : 
    9507                 :            : bool
    9508                 :       2831 : spdk_blob_is_clone(struct spdk_blob *blob)
    9509                 :            : {
    9510         [ -  + ]:       2831 :         assert(blob != NULL);
    9511                 :            : 
    9512         [ +  + ]:       2831 :         if (blob->parent_id != SPDK_BLOBID_INVALID &&
    9513         [ +  + ]:        428 :             blob->parent_id != SPDK_BLOBID_EXTERNAL_SNAPSHOT) {
    9514         [ -  + ]:        348 :                 assert(spdk_blob_is_thin_provisioned(blob));
    9515                 :        348 :                 return true;
    9516                 :            :         }
    9517                 :            : 
    9518                 :       2483 :         return false;
    9519                 :            : }
    9520                 :            : 
    9521                 :            : bool
    9522                 :    2239460 : spdk_blob_is_thin_provisioned(struct spdk_blob *blob)
    9523                 :            : {
    9524         [ -  + ]:    2239460 :         assert(blob != NULL);
    9525                 :    2239460 :         return !!(blob->invalid_flags & SPDK_BLOB_THIN_PROV);
    9526                 :            : }
    9527                 :            : 
    9528                 :            : bool
    9529                 :   22137713 : spdk_blob_is_esnap_clone(const struct spdk_blob *blob)
    9530                 :            : {
    9531                 :   22137713 :         return blob_is_esnap_clone(blob);
    9532                 :            : }
    9533                 :            : 
    9534                 :            : static void
    9535                 :      41819 : blob_update_clear_method(struct spdk_blob *blob)
    9536                 :            : {
    9537                 :            :         enum blob_clear_method stored_cm;
    9538                 :            : 
    9539         [ -  + ]:      41819 :         assert(blob != NULL);
    9540                 :            : 
    9541                 :            :         /* If BLOB_CLEAR_WITH_DEFAULT was passed in, use the setting stored
    9542                 :            :          * in metadata previously.  If something other than the default was
    9543                 :            :          * specified, ignore stored value and used what was passed in.
    9544                 :            :          */
    9545                 :      41819 :         stored_cm = ((blob->md_ro_flags & SPDK_BLOB_CLEAR_METHOD) >> SPDK_BLOB_CLEAR_METHOD_SHIFT);
    9546                 :            : 
    9547         [ +  + ]:      41819 :         if (blob->clear_method == BLOB_CLEAR_WITH_DEFAULT) {
    9548                 :      41816 :                 blob->clear_method = stored_cm;
    9549         [ -  + ]:          3 :         } else if (blob->clear_method != stored_cm) {
    9550                 :          0 :                 SPDK_WARNLOG("Using passed in clear method 0x%x instead of stored value of 0x%x\n",
    9551                 :            :                              blob->clear_method, stored_cm);
    9552                 :            :         }
    9553                 :      41819 : }
    9554                 :            : 
    9555                 :            : spdk_blob_id
    9556                 :       1604 : spdk_blob_get_parent_snapshot(struct spdk_blob_store *bs, spdk_blob_id blob_id)
    9557                 :            : {
    9558                 :       1604 :         struct spdk_blob_list *snapshot_entry = NULL;
    9559                 :       1604 :         struct spdk_blob_list *clone_entry = NULL;
    9560                 :            : 
    9561         [ +  + ]:       3026 :         TAILQ_FOREACH(snapshot_entry, &bs->snapshots, link) {
    9562         [ +  + ]:       4478 :                 TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
    9563         [ +  + ]:       3056 :                         if (clone_entry->id == blob_id) {
    9564                 :       1061 :                                 return snapshot_entry->id;
    9565                 :            :                         }
    9566                 :            :                 }
    9567                 :            :         }
    9568                 :            : 
    9569                 :        543 :         return SPDK_BLOBID_INVALID;
    9570                 :            : }
    9571                 :            : 
    9572                 :            : int
    9573                 :       1889 : spdk_blob_get_clones(struct spdk_blob_store *bs, spdk_blob_id blobid, spdk_blob_id *ids,
    9574                 :            :                      size_t *count)
    9575                 :            : {
    9576                 :            :         struct spdk_blob_list *snapshot_entry, *clone_entry;
    9577                 :            :         size_t n;
    9578                 :            : 
    9579                 :       1889 :         snapshot_entry = bs_get_snapshot_entry(bs, blobid);
    9580         [ +  + ]:       1889 :         if (snapshot_entry == NULL) {
    9581                 :        728 :                 *count = 0;
    9582                 :        728 :                 return 0;
    9583                 :            :         }
    9584                 :            : 
    9585   [ +  +  +  + ]:       1161 :         if (ids == NULL || *count < snapshot_entry->clone_count) {
    9586                 :        132 :                 *count = snapshot_entry->clone_count;
    9587                 :        132 :                 return -ENOMEM;
    9588                 :            :         }
    9589                 :       1029 :         *count = snapshot_entry->clone_count;
    9590                 :            : 
    9591                 :       1029 :         n = 0;
    9592         [ +  + ]:       2161 :         TAILQ_FOREACH(clone_entry, &snapshot_entry->clones, link) {
    9593                 :       1132 :                 ids[n++] = clone_entry->id;
    9594                 :            :         }
    9595                 :            : 
    9596                 :       1029 :         return 0;
    9597                 :            : }
    9598                 :            : 
    9599                 :            : static void
    9600                 :         24 : bs_load_grow_continue(struct spdk_bs_load_ctx *ctx)
    9601                 :            : {
    9602                 :            :         int rc;
    9603                 :            : 
    9604         [ -  + ]:         24 :         if (ctx->super->size == 0) {
    9605                 :          0 :                 ctx->super->size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    9606                 :            :         }
    9607                 :            : 
    9608         [ -  + ]:         24 :         if (ctx->super->io_unit_size == 0) {
    9609                 :          0 :                 ctx->super->io_unit_size = SPDK_BS_PAGE_SIZE;
    9610                 :            :         }
    9611                 :            : 
    9612                 :            :         /* Parse the super block */
    9613                 :         24 :         ctx->bs->clean = 1;
    9614                 :         24 :         ctx->bs->cluster_sz = ctx->super->cluster_size;
    9615         [ -  + ]:         24 :         ctx->bs->total_clusters = ctx->super->size / ctx->super->cluster_size;
    9616                 :         24 :         ctx->bs->pages_per_cluster = ctx->bs->cluster_sz / SPDK_BS_PAGE_SIZE;
    9617         [ +  - ]:         24 :         if (spdk_u32_is_pow2(ctx->bs->pages_per_cluster)) {
    9618                 :         24 :                 ctx->bs->pages_per_cluster_shift = spdk_u32log2(ctx->bs->pages_per_cluster);
    9619                 :            :         }
    9620                 :         24 :         ctx->bs->io_unit_size = ctx->super->io_unit_size;
    9621                 :         24 :         rc = spdk_bit_array_resize(&ctx->used_clusters, ctx->bs->total_clusters);
    9622         [ -  + ]:         24 :         if (rc < 0) {
    9623                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    9624                 :          0 :                 return;
    9625                 :            :         }
    9626                 :         24 :         ctx->bs->md_start = ctx->super->md_start;
    9627                 :         24 :         ctx->bs->md_len = ctx->super->md_len;
    9628                 :         24 :         rc = spdk_bit_array_resize(&ctx->bs->open_blobids, ctx->bs->md_len);
    9629         [ -  + ]:         24 :         if (rc < 0) {
    9630                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    9631                 :          0 :                 return;
    9632                 :            :         }
    9633                 :            : 
    9634                 :         64 :         ctx->bs->total_data_clusters = ctx->bs->total_clusters - spdk_divide_round_up(
    9635                 :         40 :                                                ctx->bs->md_start + ctx->bs->md_len, ctx->bs->pages_per_cluster);
    9636                 :         24 :         ctx->bs->super_blob = ctx->super->super_blob;
    9637                 :         24 :         memcpy(&ctx->bs->bstype, &ctx->super->bstype, sizeof(ctx->super->bstype));
    9638                 :            : 
    9639   [ +  -  -  + ]:         24 :         if (ctx->super->used_blobid_mask_len == 0 || ctx->super->clean == 0) {
    9640                 :          0 :                 SPDK_ERRLOG("Can not grow an unclean blobstore, please load it normally to clean it.\n");
    9641                 :          0 :                 bs_load_ctx_fail(ctx, -EIO);
    9642                 :          0 :                 return;
    9643                 :            :         } else {
    9644                 :         24 :                 bs_load_read_used_pages(ctx);
    9645                 :            :         }
    9646                 :            : }
    9647                 :            : 
    9648                 :            : static void
    9649                 :         24 : bs_load_grow_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9650                 :            : {
    9651                 :         24 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    9652                 :            : 
    9653         [ -  + ]:         24 :         if (bserrno != 0) {
    9654                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    9655                 :          0 :                 return;
    9656                 :            :         }
    9657                 :         24 :         bs_load_grow_continue(ctx);
    9658                 :            : }
    9659                 :            : 
    9660                 :            : static void
    9661                 :         24 : bs_load_grow_used_clusters_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9662                 :            : {
    9663                 :         24 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    9664                 :            : 
    9665         [ -  + ]:         24 :         if (bserrno != 0) {
    9666                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    9667                 :          0 :                 return;
    9668                 :            :         }
    9669                 :            : 
    9670                 :         24 :         spdk_free(ctx->mask);
    9671                 :            : 
    9672                 :         24 :         bs_sequence_write_dev(ctx->seq, ctx->super, bs_page_to_lba(ctx->bs, 0),
    9673                 :         24 :                               bs_byte_to_lba(ctx->bs, sizeof(*ctx->super)),
    9674                 :            :                               bs_load_grow_super_write_cpl, ctx);
    9675                 :            : }
    9676                 :            : 
    9677                 :            : static void
    9678                 :         24 : bs_load_grow_used_clusters_read_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9679                 :            : {
    9680                 :         24 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    9681                 :            :         uint64_t                lba, lba_count;
    9682                 :            :         uint64_t                dev_size;
    9683                 :            :         uint64_t                total_clusters;
    9684                 :            : 
    9685         [ -  + ]:         24 :         if (bserrno != 0) {
    9686                 :          0 :                 bs_load_ctx_fail(ctx, bserrno);
    9687                 :          0 :                 return;
    9688                 :            :         }
    9689                 :            : 
    9690                 :            :         /* The type must be correct */
    9691         [ -  + ]:         24 :         assert(ctx->mask->type == SPDK_MD_MASK_TYPE_USED_CLUSTERS);
    9692                 :            :         /* The length of the mask (in bits) must not be greater than the length of the buffer (converted to bits) */
    9693         [ -  + ]:         24 :         assert(ctx->mask->length <= (ctx->super->used_cluster_mask_len * sizeof(
    9694                 :            :                                              struct spdk_blob_md_page) * 8));
    9695                 :         24 :         dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    9696         [ -  + ]:         24 :         total_clusters = dev_size / ctx->super->cluster_size;
    9697                 :         24 :         ctx->mask->length = total_clusters;
    9698                 :            : 
    9699                 :         24 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
    9700                 :         24 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
    9701                 :         24 :         bs_sequence_write_dev(ctx->seq, ctx->mask, lba, lba_count,
    9702                 :            :                               bs_load_grow_used_clusters_write_cpl, ctx);
    9703                 :            : }
    9704                 :            : 
    9705                 :            : static void
    9706                 :         24 : bs_load_try_to_grow(struct spdk_bs_load_ctx *ctx)
    9707                 :            : {
    9708                 :            :         uint64_t dev_size, total_clusters, used_cluster_mask_len, max_used_cluster_mask;
    9709                 :            :         uint64_t lba, lba_count, mask_size;
    9710                 :            : 
    9711                 :         24 :         dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    9712         [ -  + ]:         24 :         total_clusters = dev_size / ctx->super->cluster_size;
    9713                 :         24 :         used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    9714                 :         24 :                                 spdk_divide_round_up(total_clusters, 8),
    9715                 :            :                                 SPDK_BS_PAGE_SIZE);
    9716                 :         24 :         max_used_cluster_mask = ctx->super->used_blobid_mask_start - ctx->super->used_cluster_mask_start;
    9717                 :            :         /* No necessary to grow or no space to grow */
    9718   [ +  -  -  + ]:         24 :         if (ctx->super->size >= dev_size || used_cluster_mask_len > max_used_cluster_mask) {
    9719   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "No grow\n");
    9720                 :          0 :                 bs_load_grow_continue(ctx);
    9721                 :          0 :                 return;
    9722                 :            :         }
    9723                 :            : 
    9724   [ -  +  -  + ]:         24 :         SPDK_DEBUGLOG(blob, "Resize blobstore\n");
    9725                 :            : 
    9726                 :         24 :         ctx->super->size = dev_size;
    9727                 :         24 :         ctx->super->used_cluster_mask_len = used_cluster_mask_len;
    9728                 :         24 :         ctx->super->crc = blob_md_page_calc_crc(ctx->super);
    9729                 :            : 
    9730                 :         24 :         mask_size = used_cluster_mask_len * SPDK_BS_PAGE_SIZE;
    9731                 :         24 :         ctx->mask = spdk_zmalloc(mask_size, 0x1000, NULL, SPDK_ENV_SOCKET_ID_ANY,
    9732                 :            :                                  SPDK_MALLOC_DMA);
    9733         [ -  + ]:         24 :         if (!ctx->mask) {
    9734                 :          0 :                 bs_load_ctx_fail(ctx, -ENOMEM);
    9735                 :          0 :                 return;
    9736                 :            :         }
    9737                 :         24 :         lba = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_start);
    9738                 :         24 :         lba_count = bs_page_to_lba(ctx->bs, ctx->super->used_cluster_mask_len);
    9739                 :         24 :         bs_sequence_read_dev(ctx->seq, ctx->mask, lba, lba_count,
    9740                 :            :                              bs_load_grow_used_clusters_read_cpl, ctx);
    9741                 :            : }
    9742                 :            : 
    9743                 :            : static void
    9744                 :         24 : bs_grow_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9745                 :            : {
    9746                 :         24 :         struct spdk_bs_load_ctx *ctx = cb_arg;
    9747                 :            :         int rc;
    9748                 :            : 
    9749                 :         24 :         rc = bs_super_validate(ctx->super, ctx->bs);
    9750         [ -  + ]:         24 :         if (rc != 0) {
    9751                 :          0 :                 bs_load_ctx_fail(ctx, rc);
    9752                 :          0 :                 return;
    9753                 :            :         }
    9754                 :            : 
    9755                 :         24 :         bs_load_try_to_grow(ctx);
    9756                 :            : }
    9757                 :            : 
    9758                 :            : struct spdk_bs_grow_ctx {
    9759                 :            :         struct spdk_blob_store          *bs;
    9760                 :            :         struct spdk_bs_super_block      *super;
    9761                 :            : 
    9762                 :            :         struct spdk_bit_pool            *new_used_clusters;
    9763                 :            :         struct spdk_bs_md_mask          *new_used_clusters_mask;
    9764                 :            : 
    9765                 :            :         spdk_bs_sequence_t              *seq;
    9766                 :            : };
    9767                 :            : 
    9768                 :            : static void
    9769                 :        194 : bs_grow_live_done(struct spdk_bs_grow_ctx *ctx, int bserrno)
    9770                 :            : {
    9771         [ +  + ]:        194 :         if (bserrno != 0) {
    9772                 :         48 :                 spdk_bit_pool_free(&ctx->new_used_clusters);
    9773                 :            :         }
    9774                 :            : 
    9775                 :        194 :         bs_sequence_finish(ctx->seq, bserrno);
    9776                 :        194 :         free(ctx->new_used_clusters_mask);
    9777                 :        194 :         spdk_free(ctx->super);
    9778                 :        194 :         free(ctx);
    9779                 :        194 : }
    9780                 :            : 
    9781                 :            : static void
    9782                 :         50 : bs_grow_live_super_write_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9783                 :            : {
    9784                 :         50 :         struct spdk_bs_grow_ctx *ctx = cb_arg;
    9785                 :         50 :         struct spdk_blob_store *bs = ctx->bs;
    9786                 :            :         uint64_t total_clusters;
    9787                 :            : 
    9788         [ -  + ]:         50 :         if (bserrno != 0) {
    9789                 :          0 :                 bs_grow_live_done(ctx, bserrno);
    9790                 :          0 :                 return;
    9791                 :            :         }
    9792                 :            : 
    9793                 :            :         /*
    9794                 :            :          * Blobstore is not clean until unload, for now only the super block is up to date.
    9795                 :            :          * This is similar to state right after blobstore init, when bs_write_used_md() didn't
    9796                 :            :          * yet execute.
    9797                 :            :          * When cleanly unloaded, the used md pages will be written out.
    9798                 :            :          * In case of unclean shutdown, loading blobstore will go through recovery path correctly
    9799                 :            :          * filling out the used_clusters with new size and writing it out.
    9800                 :            :          */
    9801                 :         50 :         bs->clean = 0;
    9802                 :            : 
    9803                 :            :         /* Reverting the super->size past this point is complex, avoid any error paths
    9804                 :            :          * that require to do so. */
    9805                 :         50 :         spdk_spin_lock(&bs->used_lock);
    9806                 :            : 
    9807         [ -  + ]:         50 :         total_clusters = ctx->super->size / ctx->super->cluster_size;
    9808                 :            : 
    9809         [ -  + ]:         50 :         assert(total_clusters >= spdk_bit_pool_capacity(bs->used_clusters));
    9810                 :         50 :         spdk_bit_pool_store_mask(bs->used_clusters, ctx->new_used_clusters_mask);
    9811                 :            : 
    9812         [ -  + ]:         50 :         assert(total_clusters == spdk_bit_pool_capacity(ctx->new_used_clusters));
    9813                 :         50 :         spdk_bit_pool_load_mask(ctx->new_used_clusters, ctx->new_used_clusters_mask);
    9814                 :            : 
    9815                 :         50 :         spdk_bit_pool_free(&bs->used_clusters);
    9816                 :         50 :         bs->used_clusters = ctx->new_used_clusters;
    9817                 :            : 
    9818                 :         50 :         bs->total_clusters = total_clusters;
    9819                 :        100 :         bs->total_data_clusters = bs->total_clusters - spdk_divide_round_up(
    9820                 :         50 :                                           bs->md_start + bs->md_len, bs->pages_per_cluster);
    9821                 :            : 
    9822                 :         50 :         bs->num_free_clusters = spdk_bit_pool_count_free(bs->used_clusters);
    9823         [ -  + ]:         50 :         assert(ctx->bs->num_free_clusters <= ctx->bs->total_clusters);
    9824                 :         50 :         spdk_spin_unlock(&bs->used_lock);
    9825                 :            : 
    9826                 :         50 :         bs_grow_live_done(ctx, 0);
    9827                 :            : }
    9828                 :            : 
    9829                 :            : static void
    9830                 :        194 : bs_grow_live_load_super_cpl(spdk_bs_sequence_t *seq, void *cb_arg, int bserrno)
    9831                 :            : {
    9832                 :        194 :         struct spdk_bs_grow_ctx *ctx = cb_arg;
    9833                 :            :         uint64_t dev_size, total_clusters, used_cluster_mask_len, max_used_cluster_mask;
    9834                 :            :         int rc;
    9835                 :            : 
    9836         [ -  + ]:        194 :         if (bserrno != 0) {
    9837                 :          0 :                 bs_grow_live_done(ctx, bserrno);
    9838                 :          0 :                 return;
    9839                 :            :         }
    9840                 :            : 
    9841                 :        194 :         rc = bs_super_validate(ctx->super, ctx->bs);
    9842         [ +  + ]:        194 :         if (rc != 0) {
    9843                 :         24 :                 bs_grow_live_done(ctx, rc);
    9844                 :         24 :                 return;
    9845                 :            :         }
    9846                 :            : 
    9847                 :        170 :         dev_size = ctx->bs->dev->blockcnt * ctx->bs->dev->blocklen;
    9848         [ -  + ]:        170 :         total_clusters = dev_size / ctx->super->cluster_size;
    9849                 :        170 :         used_cluster_mask_len = spdk_divide_round_up(sizeof(struct spdk_bs_md_mask) +
    9850                 :        170 :                                 spdk_divide_round_up(total_clusters, 8),
    9851                 :            :                                 SPDK_BS_PAGE_SIZE);
    9852                 :        170 :         max_used_cluster_mask = ctx->super->used_blobid_mask_start - ctx->super->used_cluster_mask_start;
    9853                 :            :         /* Only checking dev_size. Since it can change, but total_clusters remain the same. */
    9854         [ +  + ]:        170 :         if (dev_size == ctx->super->size) {
    9855   [ -  +  -  + ]:         96 :                 SPDK_DEBUGLOG(blob, "No need to grow blobstore\n");
    9856                 :         96 :                 bs_grow_live_done(ctx, 0);
    9857                 :         96 :                 return;
    9858                 :            :         }
    9859                 :            :         /*
    9860                 :            :          * Blobstore cannot be shrunk, so check before if:
    9861                 :            :          * - new size of the device is smaller than size in super_block
    9862                 :            :          * - new total number of clusters is smaller than used_clusters bit_pool
    9863                 :            :          * - there is enough space in metadata for used_cluster_mask to be written out
    9864                 :            :          */
    9865         [ +  - ]:         74 :         if (dev_size < ctx->super->size ||
    9866   [ +  -  +  + ]:         74 :             total_clusters < spdk_bit_pool_capacity(ctx->bs->used_clusters) ||
    9867                 :            :             used_cluster_mask_len > max_used_cluster_mask) {
    9868   [ -  +  -  + ]:         24 :                 SPDK_DEBUGLOG(blob, "No space to grow blobstore\n");
    9869                 :         24 :                 bs_grow_live_done(ctx, -ENOSPC);
    9870                 :         24 :                 return;
    9871                 :            :         }
    9872                 :            : 
    9873   [ -  +  -  + ]:         50 :         SPDK_DEBUGLOG(blob, "Resizing blobstore\n");
    9874                 :            : 
    9875                 :         50 :         ctx->new_used_clusters_mask = calloc(1, total_clusters);
    9876         [ -  + ]:         50 :         if (!ctx->new_used_clusters_mask) {
    9877                 :          0 :                 bs_grow_live_done(ctx, -ENOMEM);
    9878                 :          0 :                 return;
    9879                 :            :         }
    9880                 :         50 :         ctx->new_used_clusters = spdk_bit_pool_create(total_clusters);
    9881         [ -  + ]:         50 :         if (!ctx->new_used_clusters) {
    9882                 :          0 :                 bs_grow_live_done(ctx, -ENOMEM);
    9883                 :          0 :                 return;
    9884                 :            :         }
    9885                 :            : 
    9886                 :         50 :         ctx->super->clean = 0;
    9887                 :         50 :         ctx->super->size = dev_size;
    9888                 :         50 :         ctx->super->used_cluster_mask_len = used_cluster_mask_len;
    9889                 :         50 :         bs_write_super(seq, ctx->bs, ctx->super, bs_grow_live_super_write_cpl, ctx);
    9890                 :            : }
    9891                 :            : 
    9892                 :            : void
    9893                 :        194 : spdk_bs_grow_live(struct spdk_blob_store *bs,
    9894                 :            :                   spdk_bs_op_complete cb_fn, void *cb_arg)
    9895                 :            : {
    9896                 :        162 :         struct spdk_bs_cpl      cpl;
    9897                 :            :         struct spdk_bs_grow_ctx *ctx;
    9898                 :            : 
    9899         [ -  + ]:        194 :         assert(spdk_get_thread() == bs->md_thread);
    9900                 :            : 
    9901   [ -  +  -  + ]:        194 :         SPDK_DEBUGLOG(blob, "Growing blobstore on dev %p\n", bs->dev);
    9902                 :            : 
    9903                 :        194 :         cpl.type = SPDK_BS_CPL_TYPE_BS_BASIC;
    9904                 :        194 :         cpl.u.bs_basic.cb_fn = cb_fn;
    9905                 :        194 :         cpl.u.bs_basic.cb_arg = cb_arg;
    9906                 :            : 
    9907                 :        194 :         ctx = calloc(1, sizeof(struct spdk_bs_grow_ctx));
    9908         [ -  + ]:        194 :         if (!ctx) {
    9909                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    9910                 :          0 :                 return;
    9911                 :            :         }
    9912                 :        194 :         ctx->bs = bs;
    9913                 :            : 
    9914                 :        194 :         ctx->super = spdk_zmalloc(sizeof(*ctx->super), 0x1000, NULL,
    9915                 :            :                                   SPDK_ENV_SOCKET_ID_ANY, SPDK_MALLOC_DMA);
    9916         [ -  + ]:        194 :         if (!ctx->super) {
    9917                 :          0 :                 free(ctx);
    9918                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    9919                 :          0 :                 return;
    9920                 :            :         }
    9921                 :            : 
    9922                 :        194 :         ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    9923         [ -  + ]:        194 :         if (!ctx->seq) {
    9924                 :          0 :                 spdk_free(ctx->super);
    9925                 :          0 :                 free(ctx);
    9926                 :          0 :                 cb_fn(cb_arg, -ENOMEM);
    9927                 :          0 :                 return;
    9928                 :            :         }
    9929                 :            : 
    9930                 :            :         /* Read the super block */
    9931                 :        194 :         bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
    9932                 :        194 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    9933                 :            :                              bs_grow_live_load_super_cpl, ctx);
    9934                 :            : }
    9935                 :            : 
    9936                 :            : void
    9937                 :         24 : spdk_bs_grow(struct spdk_bs_dev *dev, struct spdk_bs_opts *o,
    9938                 :            :              spdk_bs_op_with_handle_complete cb_fn, void *cb_arg)
    9939                 :            : {
    9940                 :         20 :         struct spdk_blob_store  *bs;
    9941                 :         20 :         struct spdk_bs_cpl      cpl;
    9942                 :         20 :         struct spdk_bs_load_ctx *ctx;
    9943                 :         24 :         struct spdk_bs_opts     opts = {};
    9944                 :            :         int err;
    9945                 :            : 
    9946   [ -  +  -  + ]:         24 :         SPDK_DEBUGLOG(blob, "Loading blobstore from dev %p\n", dev);
    9947                 :            : 
    9948   [ -  +  -  + ]:         24 :         if ((SPDK_BS_PAGE_SIZE % dev->blocklen) != 0) {
    9949   [ #  #  #  # ]:          0 :                 SPDK_DEBUGLOG(blob, "unsupported dev block length of %d\n", dev->blocklen);
    9950                 :          0 :                 dev->destroy(dev);
    9951                 :          0 :                 cb_fn(cb_arg, NULL, -EINVAL);
    9952                 :          0 :                 return;
    9953                 :            :         }
    9954                 :            : 
    9955                 :         24 :         spdk_bs_opts_init(&opts, sizeof(opts));
    9956         [ +  - ]:         24 :         if (o) {
    9957         [ -  + ]:         24 :                 if (bs_opts_copy(o, &opts)) {
    9958                 :          0 :                         return;
    9959                 :            :                 }
    9960                 :            :         }
    9961                 :            : 
    9962   [ +  -  -  + ]:         24 :         if (opts.max_md_ops == 0 || opts.max_channel_ops == 0) {
    9963                 :          0 :                 dev->destroy(dev);
    9964                 :          0 :                 cb_fn(cb_arg, NULL, -EINVAL);
    9965                 :          0 :                 return;
    9966                 :            :         }
    9967                 :            : 
    9968                 :         24 :         err = bs_alloc(dev, &opts, &bs, &ctx);
    9969         [ -  + ]:         24 :         if (err) {
    9970                 :          0 :                 dev->destroy(dev);
    9971                 :          0 :                 cb_fn(cb_arg, NULL, err);
    9972                 :          0 :                 return;
    9973                 :            :         }
    9974                 :            : 
    9975                 :         24 :         cpl.type = SPDK_BS_CPL_TYPE_BS_HANDLE;
    9976                 :         24 :         cpl.u.bs_handle.cb_fn = cb_fn;
    9977                 :         24 :         cpl.u.bs_handle.cb_arg = cb_arg;
    9978                 :         24 :         cpl.u.bs_handle.bs = bs;
    9979                 :            : 
    9980                 :         24 :         ctx->seq = bs_sequence_start_bs(bs->md_channel, &cpl);
    9981         [ -  + ]:         24 :         if (!ctx->seq) {
    9982                 :          0 :                 spdk_free(ctx->super);
    9983                 :          0 :                 free(ctx);
    9984                 :          0 :                 bs_free(bs);
    9985                 :          0 :                 cb_fn(cb_arg, NULL, -ENOMEM);
    9986                 :          0 :                 return;
    9987                 :            :         }
    9988                 :            : 
    9989                 :            :         /* Read the super block */
    9990                 :         24 :         bs_sequence_read_dev(ctx->seq, ctx->super, bs_page_to_lba(bs, 0),
    9991                 :         24 :                              bs_byte_to_lba(bs, sizeof(*ctx->super)),
    9992                 :            :                              bs_grow_load_super_cpl, ctx);
    9993                 :            : }
    9994                 :            : 
    9995                 :            : int
    9996                 :        173 : spdk_blob_get_esnap_id(struct spdk_blob *blob, const void **id, size_t *len)
    9997                 :            : {
    9998         [ +  + ]:        173 :         if (!blob_is_esnap_clone(blob)) {
    9999                 :         74 :                 return -EINVAL;
   10000                 :            :         }
   10001                 :            : 
   10002                 :         99 :         return blob_get_xattr_value(blob, BLOB_EXTERNAL_SNAPSHOT_ID, id, len, true);
   10003                 :            : }
   10004                 :            : 
   10005                 :            : struct spdk_io_channel *
   10006                 :      66815 : blob_esnap_get_io_channel(struct spdk_io_channel *ch, struct spdk_blob *blob)
   10007                 :            : {
   10008                 :      66815 :         struct spdk_bs_channel          *bs_channel = spdk_io_channel_get_ctx(ch);
   10009                 :      66815 :         struct spdk_bs_dev              *bs_dev = blob->back_bs_dev;
   10010                 :      66815 :         struct blob_esnap_channel       find = {};
   10011                 :            :         struct blob_esnap_channel       *esnap_channel, *existing;
   10012                 :            : 
   10013                 :      66815 :         find.blob_id = blob->id;
   10014                 :      66815 :         esnap_channel = RB_FIND(blob_esnap_channel_tree, &bs_channel->esnap_channels, &find);
   10015         [ +  + ]:      66815 :         if (spdk_likely(esnap_channel != NULL)) {
   10016   [ -  +  -  + ]:      66530 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": using cached channel on thread %s\n",
   10017                 :            :                               blob->id, spdk_thread_get_name(spdk_get_thread()));
   10018                 :      66530 :                 return esnap_channel->channel;
   10019                 :            :         }
   10020                 :            : 
   10021   [ -  +  -  + ]:        285 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": allocating channel on thread %s\n",
   10022                 :            :                       blob->id, spdk_thread_get_name(spdk_get_thread()));
   10023                 :            : 
   10024                 :        285 :         esnap_channel = calloc(1, sizeof(*esnap_channel));
   10025         [ -  + ]:        285 :         if (esnap_channel == NULL) {
   10026                 :          0 :                 SPDK_NOTICELOG("blob 0x%" PRIx64 " channel allocation failed: no memory\n",
   10027                 :            :                                find.blob_id);
   10028                 :          0 :                 return NULL;
   10029                 :            :         }
   10030                 :        285 :         esnap_channel->channel = bs_dev->create_channel(bs_dev);
   10031         [ -  + ]:        285 :         if (esnap_channel->channel == NULL) {
   10032                 :          0 :                 SPDK_NOTICELOG("blob 0x%" PRIx64 " back channel allocation failed\n", blob->id);
   10033                 :          0 :                 free(esnap_channel);
   10034                 :          0 :                 return NULL;
   10035                 :            :         }
   10036                 :        285 :         esnap_channel->blob_id = find.blob_id;
   10037                 :        285 :         existing = RB_INSERT(blob_esnap_channel_tree, &bs_channel->esnap_channels, esnap_channel);
   10038         [ -  + ]:        285 :         if (spdk_unlikely(existing != NULL)) {
   10039                 :            :                 /*
   10040                 :            :                  * This should be unreachable: all modifications to this tree happen on this thread.
   10041                 :            :                  */
   10042                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 "lost race to allocate a channel\n", find.blob_id);
   10043                 :          0 :                 assert(false);
   10044                 :            : 
   10045                 :            :                 bs_dev->destroy_channel(bs_dev, esnap_channel->channel);
   10046                 :            :                 free(esnap_channel);
   10047                 :            : 
   10048                 :            :                 return existing->channel;
   10049                 :            :         }
   10050                 :            : 
   10051                 :        285 :         return esnap_channel->channel;
   10052                 :            : }
   10053                 :            : 
   10054                 :            : static int
   10055                 :      66671 : blob_esnap_channel_compare(struct blob_esnap_channel *c1, struct blob_esnap_channel *c2)
   10056                 :            : {
   10057         [ +  - ]:      66671 :         return (c1->blob_id < c2->blob_id ? -1 : c1->blob_id > c2->blob_id);
   10058                 :            : }
   10059                 :            : 
   10060                 :            : struct blob_esnap_destroy_ctx {
   10061                 :            :         spdk_blob_op_with_handle_complete       cb_fn;
   10062                 :            :         void                                    *cb_arg;
   10063                 :            :         struct spdk_blob                        *blob;
   10064                 :            :         struct spdk_bs_dev                      *back_bs_dev;
   10065                 :            :         bool                                    abort_io;
   10066                 :            : };
   10067                 :            : 
   10068                 :            : static void
   10069                 :        859 : blob_esnap_destroy_channels_done(struct spdk_io_channel_iter *i, int status)
   10070                 :            : {
   10071                 :        859 :         struct blob_esnap_destroy_ctx   *ctx = spdk_io_channel_iter_get_ctx(i);
   10072                 :        859 :         struct spdk_blob                *blob = ctx->blob;
   10073                 :        859 :         struct spdk_blob_store          *bs = blob->bs;
   10074                 :            : 
   10075   [ -  +  -  + ]:        859 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": done destroying channels for this blob\n",
   10076                 :            :                       blob->id);
   10077                 :            : 
   10078         [ +  + ]:        859 :         if (ctx->cb_fn != NULL) {
   10079                 :        783 :                 ctx->cb_fn(ctx->cb_arg, blob, status);
   10080                 :            :         }
   10081                 :        859 :         free(ctx);
   10082                 :            : 
   10083                 :        859 :         bs->esnap_channels_unloading--;
   10084   [ +  -  +  + ]:        859 :         if (bs->esnap_channels_unloading == 0 && bs->esnap_unload_cb_fn != NULL) {
   10085                 :         40 :                 spdk_bs_unload(bs, bs->esnap_unload_cb_fn, bs->esnap_unload_cb_arg);
   10086                 :            :         }
   10087                 :        859 : }
   10088                 :            : 
   10089                 :            : static void
   10090                 :        907 : blob_esnap_destroy_one_channel(struct spdk_io_channel_iter *i)
   10091                 :            : {
   10092                 :        907 :         struct blob_esnap_destroy_ctx   *ctx = spdk_io_channel_iter_get_ctx(i);
   10093                 :        907 :         struct spdk_blob                *blob = ctx->blob;
   10094                 :        907 :         struct spdk_bs_dev              *bs_dev = ctx->back_bs_dev;
   10095                 :        907 :         struct spdk_io_channel          *channel = spdk_io_channel_iter_get_channel(i);
   10096                 :        907 :         struct spdk_bs_channel          *bs_channel = spdk_io_channel_get_ctx(channel);
   10097                 :            :         struct blob_esnap_channel       *esnap_channel;
   10098                 :        907 :         struct blob_esnap_channel       find = {};
   10099                 :            : 
   10100         [ -  + ]:        907 :         assert(spdk_get_thread() == spdk_io_channel_get_thread(channel));
   10101                 :            : 
   10102                 :        907 :         find.blob_id = blob->id;
   10103                 :        907 :         esnap_channel = RB_FIND(blob_esnap_channel_tree, &bs_channel->esnap_channels, &find);
   10104         [ +  + ]:        907 :         if (esnap_channel != NULL) {
   10105   [ -  +  -  + ]:         93 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": destroying channel on thread %s\n",
   10106                 :            :                               blob->id, spdk_thread_get_name(spdk_get_thread()));
   10107                 :         93 :                 RB_REMOVE(blob_esnap_channel_tree, &bs_channel->esnap_channels, esnap_channel);
   10108                 :            : 
   10109   [ +  +  +  + ]:         93 :                 if (ctx->abort_io) {
   10110                 :            :                         spdk_bs_user_op_t *op, *tmp;
   10111                 :            : 
   10112         [ -  + ]:         50 :                         TAILQ_FOREACH_SAFE(op, &bs_channel->queued_io, link, tmp) {
   10113         [ #  # ]:          0 :                                 if (op->back_channel == esnap_channel->channel) {
   10114         [ #  # ]:          0 :                                         TAILQ_REMOVE(&bs_channel->queued_io, op, link);
   10115                 :          0 :                                         bs_user_op_abort(op, -EIO);
   10116                 :            :                                 }
   10117                 :            :                         }
   10118                 :            :                 }
   10119                 :            : 
   10120                 :         93 :                 bs_dev->destroy_channel(bs_dev, esnap_channel->channel);
   10121                 :         93 :                 free(esnap_channel);
   10122                 :            :         }
   10123                 :            : 
   10124                 :        907 :         spdk_for_each_channel_continue(i, 0);
   10125                 :        907 : }
   10126                 :            : 
   10127                 :            : /*
   10128                 :            :  * Destroy the channels for a specific blob on each thread with a blobstore channel. This should be
   10129                 :            :  * used when closing an esnap clone blob and after decoupling from the parent.
   10130                 :            :  */
   10131                 :            : static void
   10132                 :       3027 : blob_esnap_destroy_bs_dev_channels(struct spdk_blob *blob, bool abort_io,
   10133                 :            :                                    spdk_blob_op_with_handle_complete cb_fn, void *cb_arg)
   10134                 :            : {
   10135                 :            :         struct blob_esnap_destroy_ctx   *ctx;
   10136                 :            : 
   10137   [ +  +  +  + ]:       3027 :         if (!blob_is_esnap_clone(blob) || blob->back_bs_dev == NULL) {
   10138         [ +  - ]:       2168 :                 if (cb_fn != NULL) {
   10139                 :       2168 :                         cb_fn(cb_arg, blob, 0);
   10140                 :            :                 }
   10141                 :       2168 :                 return;
   10142                 :            :         }
   10143                 :            : 
   10144                 :        859 :         ctx = calloc(1, sizeof(*ctx));
   10145         [ -  + ]:        859 :         if (ctx == NULL) {
   10146         [ #  # ]:          0 :                 if (cb_fn != NULL) {
   10147                 :          0 :                         cb_fn(cb_arg, blob, -ENOMEM);
   10148                 :            :                 }
   10149                 :          0 :                 return;
   10150                 :            :         }
   10151                 :        859 :         ctx->cb_fn = cb_fn;
   10152                 :        859 :         ctx->cb_arg = cb_arg;
   10153                 :        859 :         ctx->blob = blob;
   10154                 :        859 :         ctx->back_bs_dev = blob->back_bs_dev;
   10155                 :        859 :         ctx->abort_io = abort_io;
   10156                 :            : 
   10157   [ -  +  -  + ]:        859 :         SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64 ": destroying channels for this blob\n",
   10158                 :            :                       blob->id);
   10159                 :            : 
   10160                 :        859 :         blob->bs->esnap_channels_unloading++;
   10161                 :        859 :         spdk_for_each_channel(blob->bs, blob_esnap_destroy_one_channel, ctx,
   10162                 :            :                               blob_esnap_destroy_channels_done);
   10163                 :            : }
   10164                 :            : 
   10165                 :            : /*
   10166                 :            :  * Destroy all bs_dev channels on a specific blobstore channel. This should be used when a
   10167                 :            :  * bs_channel is destroyed.
   10168                 :            :  */
   10169                 :            : static void
   10170                 :      14198 : blob_esnap_destroy_bs_channel(struct spdk_bs_channel *ch)
   10171                 :            : {
   10172                 :            :         struct blob_esnap_channel *esnap_channel, *esnap_channel_tmp;
   10173                 :            : 
   10174         [ -  + ]:      14198 :         assert(spdk_get_thread() == spdk_io_channel_get_thread(spdk_io_channel_from_ctx(ch)));
   10175                 :            : 
   10176   [ -  +  -  + ]:      14198 :         SPDK_DEBUGLOG(blob_esnap, "destroying channels on thread %s\n",
   10177                 :            :                       spdk_thread_get_name(spdk_get_thread()));
   10178   [ +  +  +  - ]:      14390 :         RB_FOREACH_SAFE(esnap_channel, blob_esnap_channel_tree, &ch->esnap_channels,
   10179                 :            :                         esnap_channel_tmp) {
   10180   [ -  +  -  + ]:        192 :                 SPDK_DEBUGLOG(blob_esnap, "blob 0x%" PRIx64
   10181                 :            :                               ": destroying one channel in thread %s\n",
   10182                 :            :                               esnap_channel->blob_id, spdk_thread_get_name(spdk_get_thread()));
   10183                 :        192 :                 RB_REMOVE(blob_esnap_channel_tree, &ch->esnap_channels, esnap_channel);
   10184                 :        192 :                 spdk_put_io_channel(esnap_channel->channel);
   10185                 :        192 :                 free(esnap_channel);
   10186                 :            :         }
   10187   [ -  +  -  + ]:      14198 :         SPDK_DEBUGLOG(blob_esnap, "done destroying channels on thread %s\n",
   10188                 :            :                       spdk_thread_get_name(spdk_get_thread()));
   10189                 :      14198 : }
   10190                 :            : 
   10191                 :            : static void
   10192                 :        177 : blob_set_back_bs_dev_done(void *_ctx, int bserrno)
   10193                 :            : {
   10194                 :        177 :         struct set_bs_dev_ctx   *ctx = _ctx;
   10195                 :            : 
   10196         [ -  + ]:        177 :         if (bserrno != 0) {
   10197                 :            :                 /* Even though the unfreeze failed, the update may have succeed. */
   10198                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": unfreeze failed with error %d\n", ctx->blob->id,
   10199                 :            :                             bserrno);
   10200                 :            :         }
   10201                 :        177 :         ctx->cb_fn(ctx->cb_arg, ctx->bserrno);
   10202                 :        177 :         free(ctx);
   10203                 :        177 : }
   10204                 :            : 
   10205                 :            : static void
   10206                 :        177 : blob_frozen_set_back_bs_dev(void *_ctx, struct spdk_blob *blob, int bserrno)
   10207                 :            : {
   10208                 :        177 :         struct set_bs_dev_ctx   *ctx = _ctx;
   10209                 :            :         int rc;
   10210                 :            : 
   10211         [ -  + ]:        177 :         if (bserrno != 0) {
   10212                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to release old back_bs_dev with error %d\n",
   10213                 :            :                             blob->id, bserrno);
   10214                 :          0 :                 ctx->bserrno = bserrno;
   10215                 :          0 :                 blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
   10216                 :          0 :                 return;
   10217                 :            :         }
   10218                 :            : 
   10219         [ +  - ]:        177 :         if (blob->back_bs_dev != NULL) {
   10220                 :        177 :                 blob->back_bs_dev->destroy(blob->back_bs_dev);
   10221                 :        177 :                 blob->back_bs_dev = NULL;
   10222                 :            :         }
   10223                 :            : 
   10224         [ +  + ]:        177 :         if (ctx->parent_refs_cb_fn) {
   10225                 :        126 :                 rc = ctx->parent_refs_cb_fn(blob, ctx->parent_refs_cb_arg);
   10226         [ -  + ]:        126 :                 if (rc != 0) {
   10227                 :          0 :                         ctx->bserrno = rc;
   10228                 :          0 :                         blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
   10229                 :          0 :                         return;
   10230                 :            :                 }
   10231                 :            :         }
   10232                 :            : 
   10233                 :        177 :         SPDK_NOTICELOG("blob 0x%" PRIx64 ": hotplugged back_bs_dev\n", blob->id);
   10234                 :        177 :         blob->back_bs_dev = ctx->back_bs_dev;
   10235                 :        177 :         ctx->bserrno = 0;
   10236                 :            : 
   10237                 :        177 :         blob_unfreeze_io(blob, blob_set_back_bs_dev_done, ctx);
   10238                 :            : }
   10239                 :            : 
   10240                 :            : static void
   10241                 :        177 : blob_set_back_bs_dev_frozen(void *_ctx, int bserrno)
   10242                 :            : {
   10243                 :        177 :         struct set_bs_dev_ctx   *ctx = _ctx;
   10244                 :        177 :         struct spdk_blob        *blob = ctx->blob;
   10245                 :            : 
   10246         [ -  + ]:        177 :         if (bserrno != 0) {
   10247                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": failed to freeze with error %d\n", blob->id,
   10248                 :            :                             bserrno);
   10249                 :          0 :                 ctx->cb_fn(ctx->cb_arg, bserrno);
   10250                 :          0 :                 free(ctx);
   10251                 :          0 :                 return;
   10252                 :            :         }
   10253                 :            : 
   10254                 :            :         /*
   10255                 :            :          * This does not prevent future reads from the esnap device because any future IO will
   10256                 :            :          * lazily create a new esnap IO channel.
   10257                 :            :          */
   10258                 :        177 :         blob_esnap_destroy_bs_dev_channels(blob, true, blob_frozen_set_back_bs_dev, ctx);
   10259                 :            : }
   10260                 :            : 
   10261                 :            : void
   10262                 :         51 : spdk_blob_set_esnap_bs_dev(struct spdk_blob *blob, struct spdk_bs_dev *back_bs_dev,
   10263                 :            :                            spdk_blob_op_complete cb_fn, void *cb_arg)
   10264                 :            : {
   10265         [ -  + ]:         51 :         if (!blob_is_esnap_clone(blob)) {
   10266                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": not an esnap clone\n", blob->id);
   10267                 :          0 :                 cb_fn(cb_arg, -EINVAL);
   10268                 :          0 :                 return;
   10269                 :            :         }
   10270                 :            : 
   10271                 :         51 :         blob_set_back_bs_dev(blob, back_bs_dev, NULL, NULL, cb_fn, cb_arg);
   10272                 :            : }
   10273                 :            : 
   10274                 :            : struct spdk_bs_dev *
   10275                 :        176 : spdk_blob_get_esnap_bs_dev(const struct spdk_blob *blob)
   10276                 :            : {
   10277         [ -  + ]:        176 :         if (!blob_is_esnap_clone(blob)) {
   10278                 :          0 :                 SPDK_ERRLOG("blob 0x%" PRIx64 ": not an esnap clone\n", blob->id);
   10279                 :          0 :                 return NULL;
   10280                 :            :         }
   10281                 :            : 
   10282                 :        176 :         return blob->back_bs_dev;
   10283                 :            : }
   10284                 :            : 
   10285                 :            : bool
   10286                 :       1209 : spdk_blob_is_degraded(const struct spdk_blob *blob)
   10287                 :            : {
   10288   [ +  +  +  + ]:       1209 :         if (blob->bs->dev->is_degraded != NULL && blob->bs->dev->is_degraded(blob->bs->dev)) {
   10289                 :         24 :                 return true;
   10290                 :            :         }
   10291   [ +  +  +  + ]:       1185 :         if (blob->back_bs_dev == NULL || blob->back_bs_dev->is_degraded == NULL) {
   10292                 :        982 :                 return false;
   10293                 :            :         }
   10294                 :            : 
   10295                 :        203 :         return blob->back_bs_dev->is_degraded(blob->back_bs_dev);
   10296                 :            : }
   10297                 :            : 
   10298                 :       2407 : SPDK_LOG_REGISTER_COMPONENT(blob)
   10299                 :       2407 : SPDK_LOG_REGISTER_COMPONENT(blob_esnap)

Generated by: LCOV version 1.14